Search Unity

Batch Breaking Optimisation/Reduction with GPU Instancing vs Static Batching

Discussion in 'General Graphics' started by Aviryx, Jul 5, 2020.

  1. Aviryx

    Aviryx

    Joined:
    May 30, 2020
    Posts:
    97
    I'm wondering if there is any detailed information about how Unity handles batching when using a single material applied to objects that will break a batch - vs using a material per "object group".

    For example:

    - a single texture atlas
    - 1,000 cubes
    - 2 low poly cars (marked as static batching)

    So let's say I create a material that uses the texture atlas called some_mat and apply it to all the gameobjects and then enable GPU instancing for the material.

    I know Unity can not GPU instance something that is static batched so will cause the render state to update. My question is would it be more efficient to have:

    - a single material (using a texture atlas) for all objects that can be GPU instanced
    - a single material (using a texture atlas) for all objects that are static batched

    vs

    - a single material for all gameobjects regardless of if the can be GPU instanced, are static batched, or neither.

    From my understanding a single material where possible is more efficient as, even though a gpu instanced gameobject and static batched gameobject can not be batched together, there is no additional call to set a new material. Whereas using two materials will require the render state to be updated as a new material needs to be set depending on the gameobject.

    Am I wrong in thinking this? If I use a single material for gpu instanced and static batched gameobjects, what happens when Unity is rendering?

    - Object 1 is gpu instanced
    - object 2 is static batched

    does unity have to essentially reload the same material? or does it recognize that even though the material has "gpu instanced" enabled, the current object to draw is static batched?
     
  2. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    GPU instancing only works with same mesh. It's a GPU feature where it can render multiple, well, instances of the dispatched geometry in one draw call, the shader doing the work of fetching instance-specific shader data. Both the mesh and the material should be the same for all instances in a group.

    Static batching works by merging multiple meshes into a larger mesh in the Editor, and using the combined mesh instead of the separate meshes during run time. It does support different meshes as long as they use the same material, and doesn't require any special GPU features. The downside is that it requires more RAM and disk space: if you have 100 objects, the geometry will be duplicated 100 times in disk/memory.
     
  3. Aviryx

    Aviryx

    Joined:
    May 30, 2020
    Posts:
    97
    Yes I already know that hence the example I gave. 1000 cubes using GPU instancing and 2 low poly car models using static batching.

    Again, I know this and it doesn't address my question. I'm not sure why you are explaining to me the basics of GPU instancing and static batching work.... I already know that stuff. What I don't know (and can't find in the Unity docs) is what happens in a specific case when you have a single material on gameobjects that can be static batched, GPU instanced, or neither (with the gpu instancing enabled on the material)

    Does this cause Unity to recall the material (as if it were a new material) or is it able to recognize that, when using a GPU instanced material to draw a static batched gameobject, that it can not GPU instance the object but does not require a new Get/Set Texture graphics call.
     
  4. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    The static bathed objects and the instanced objects will use a different variant of their material shader, so Unity will treat them as two different materials.

    If you want to check exactly what Unity is telling the GPU to do between each draw call, use Renderdoc.