Skip to content

Improve instance batching. - #125808

Open
blueskythlikesclouds wants to merge 1 commit into
godotengine:masterfrom
blueskythlikesclouds:instance-batch-opt
Open

blueskythlikesclouds wants to merge 1 commit into
godotengine:masterfrom
blueskythlikesclouds:instance-batch-opt

Conversation

@blueskythlikesclouds

Copy link
Copy Markdown
Member

What problem(s) does this PR solve?

The Forward+ renderer compares sort keys to determine whether surfaces are identical, and can be batched into a single draw call. This comparison also included the depth_layer and priority values, which do not actually matter for equivalency and are only used for sorting. To improve this, we set both values to 0 before comparing, allowing identical surfaces to be batched together.

I also moved depth_layer to the least significant position in the sort key. The intention behind this value seems to be for sorting identical surfaces from front to back for early Z culling. However, since it previously had higher priority than lod_index, draw calls could constantly alternate between different LOD levels. By making it the least significant value, all LOD levels in the MRP now merge into a single draw call for each respective LOD level.

In TPS Demo, this also reduced the number of Vulkan calls from 22K to 18K.

Additional information

N/A.

@clayjohn

Copy link
Copy Markdown
Member

For the depth layer to be beneficial it needs to be in the most significant position possible.

Think of it this way, we want all objects that are up close to the viewer to be drawn before all objects that are far from the viewer. Otherwise we won't benefit from the depth sorting at all.

@Ansraer

Ansraer commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Damnit, I have only just started benchmarking stuff. You beat me to it by like a day or two.

For the depth layer to be beneficial it needs to be in the most significant position possible.

Not sure I agree. depth layer is a 16 bit quantization, so in most scene everything ends up in layer 0. It honestly doesn't do much that isn't also done by 'lod index' (which in a way also encodes depth information).

I think some kind of occlusion culling will do more for performance than the current depth layer sorting.

Oh wait, directional light cascades! Their old is based on the scene camera, while depth layer is based on the actual cascade. More importantly, it uses the cascades far and near distance, so it should make better use of the 16 bits.

In TPS Demo, this also reduced the number of Vulkan calls from 22K to 18K.

That's honestly a lot less improvement than I expected. Maybe the TPS demo has a lot of unique meshes or materials?

@blueskythlikesclouds

blueskythlikesclouds commented Nov 26, 2026 •

Copy link
Copy Markdown
Member Author

I tried bringing depth layer to the front in TPS Demo and the amount of Vulkan calls jumped to 28K, and in the MRP they all don't merge perfectly, though the amount of calls is still much lower than upstream. So I guess we should see which tradeoff we want, depth sorting or less draw calls. I'll do some profiling on my device.

@Ansraer

Ansraer commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Hmmm. I am working on HiZ culling, which might change the math a fair bit. If culling already removes many of the occluded instances depth sorting becomes a lot less important.

Should I push my current wip branch when I am back from lunch so that you can take a look at it Skyth? Be warned, it's fairly messy.

@blueskythlikesclouds

Copy link
Copy Markdown
Member Author

HiZ culling would only help with the opaque pass, shadow passes don't use that, so depth sorting would still be beneficial there.

@blueskythlikesclouds

blueskythlikesclouds commented Nov 26, 2026 •

Copy link
Copy Markdown
Member Author

Did some tests in TPS Demo. I spawned a bunch of omni lights in player view and forced them to update every frame. Here are the numbers:

Upstream:

  • CPU: 1.35 ms
  • GPU: 6.82 ms

This PR:

  • CPU: 1.27 ms
  • GPU: 6.82 ms

This PR with depth layer more important in the sort key:

  • CPU: 1.4 ms
  • GPU: 6.7 ms

I feel like it'd be possible to get a balance of both by sorting surfaces by depth anyway but having identical surfaces next to each other so they still end up getting merged. I'm gonna experiment with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mesh instance auto-batching very slow in shadow renders

4 participants