Batching Issues

I was wondering if there is any documentation on how batching works in UIToolkit or how to debug it?

I have a high count of 172 batches when all my UI is showing. I removed everything except a few pieces of my UI.

13 batches seems like a lot for just a few buttons and icons. Shouldn’t all these batch together and only be 1 batch or am I missing something?

Note: They are all UIToolkit svgs images.

Did you put your icons within the same atlas ?
You can use the Frame debugger to understand how unity builds and draws the scene

They are in a sprite atlas, but since they are svgs, they don’t get put into it. But I think they are automatically batched anyways? (I tested by having two different svgs on the screen and it still showed the same batch number). But I could be wrong.

7428584--909578--Screen Shot 2021-08-18 at 11.10.00 AM.png

Thank! This is very useful! I am still confused thought why they aren’t batched.

hknxy9

In the video, a button outline svg and icon svg aren’t batched together? I guess probably the rotated mesh isn’t batched, but why it is batched with the label?

Thanks!

I’d expect them to be batched together. Can you share your project so we can investigate?

I tried to reproduce it consistently, but I cannot even get it to happen again. I was deleting UIDocuments from my scene and turning off UI with the UIDebugger by setting display:flex to none, but I cannot get that same situation to happen again. It is batching that button outline/icon correctly whenever I try to reproduce it.

When I get the 172 batches:

  • I have a lot of menus set to Visibility: none, but they are also positioned to be off the camera screen (as I transition them into view if I need to). Do I just need to not worry about 172 batches or should I set it to display: none? I heard though that this is bad because it rebuilds the UI?
  • I have a level button that is not batched at all. I am guessing because the green creature is position: absolute, the colored circle is changing color every frame, the square background is just a VE rotated and the 12 is a font? I have 12 of these in the menu so that makes 4x12.

7428767--909602--Screen Shot 2021-08-18 at 12.06.29 PM.png

It seems that the batching (at least in the editor) happens sporadically. This is probably what happened with the previous video. The first video below is what happened when I simply unset the visibility for the root VE in the uxml from visible to visible.

lnmi8y

Then I just changed scenes and came back.

r2khar

I suspect what you are noticing is multiple draw calls caused by elements being reordered inside our internal vertex buffers. If new elements are inserted/removed in the draw chain, we may have to issue multiple draw calls with different vertex offsets to make sure the elements are rendered in a depth-first order. I’m pretty sure that visibility changes can trigger that kind of vertex shuffling.

However, those are draw calls that don’t have any state changes in-between (the bound textures/buffers/graphic states are the same). They are much cheaper than draw calls with state changes. Unfortunately, the frame debugger and stats display doesn’t tell whether these draw calls are cheap or not.

It is on our roadmap to improve the vertex allocations to reduce that kind of fragmentation that causes batch breaks.

In the short term, it will probably help to minimize the visibility changes to keep your buffers in order. You can maybe set the opacity to 0% to set the elements invisible instead of changing the visibility state. This should help avoid batch breaks.

1 Like

Thank you for the information! This helps a lot and can’t wait for the improvements!