Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Static batching docs contradict themselves

Discussion in 'Documentation' started by arkano22, Aug 30, 2022.

  1. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,603
    alinajosephclass likes this.
  2. berniegp

    berniegp

    Unity Technologies

    Joined:
    Sep 9, 2020
    Posts:
    40
    Hi! Indeed this is a contradictory.

    That first paragraph could be:
    So it's the same number of draw calls, but there is no/less state change between them like binding the meshes' vertex buffers.

    I logged a task to improve this page.
     
    arkano22 likes this.
  3. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,603
    So static batching basically:
    - Merges all meshes into one pair of large vertex/index arrays.
    - Culls individual meshes as usual.
    - Sorts all meshes that have passed culling by material/render settings to minimize API state changes (binding shaders, textures, etc).
    - Issues one drawcall for each of these meshes, using its start/count into the large index array.

    Is this roughly correct?

    If so, there’s a potential performance advantage to merging meshes (that share material, textures, etc) into a single larger mesh and drawing it in one drawcall, compared to statically batching them, right? You lose culling (as the entire combined mesh must be rendered) but reduce the amount of drawcalls.
     
    Last edited: Sep 2, 2022
  4. berniegp

    berniegp

    Unity Technologies

    Joined:
    Sep 9, 2020
    Posts:
    40
    Almost.

    In step 1, it's not all meshes together. They are merged in groups based on having the same material. More details here: https://docs.unity3d.com/Manual/DrawCallBatching.html

    There's also a limit on how large a statically batched mesh can be (something around 60k vertices, but don't quote me on the number).

    So your step 3 isn't quite what happens because statically batched meshes are already grouped by their same material. In other words, sorting doesn't start from scratch with the rest of the scene.

    I'll add though that if you're using URP or HDRP (or a custom SRP), you most often won't get a performance gain from static batching because the SRP batcher already saves a lot of state changes. So be sure to profile to validate that the extra memory costs for read/write meshes are worth it.
     
    arkano22 likes this.