Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why is batch broken if materials are the same?

Discussion in 'Shaders' started by yaaashua, Jun 23, 2021.

  1. yaaashua

    yaaashua

    Joined:
    Jan 27, 2013
    Posts:
    4
    Hi everyone, I have been searching a lot and I can't find the real answer:

    I created simple meshes:

    Everything is perfect and all of them are rendered in 1 drawcall ONLY if all of the meshes have the same color (or all white or all green), but as soon as there are more than one variant in scene... everything is drawn in a lot of drawcalls, which makes no sense for me.

    - all of the meshes are using the same material
    - no textures
    - no MaterialPorpertyBlock
    - color is set by vertex color ( sharedMesh.colors) (only set if color is required)
    - same shader for all: Sprites-Default

    - Why is the batching breaking if they actually have the same everything?
    - Why is the batching breaking that many batches.. why not just 2?? (one white, one green)
    - even if it would break into several batches because of the color gradients... why it doesnt happens when all of them are green?

    I haven't been able to find the logic here, I already read the documentation about DrawCallBatching and GPU instancing and aI cant find the reason.

    Please, can anyone explain this to me?
    Thank you in advance

    upload_2021-6-23_3-3-54.png
     
    Last edited: Jun 23, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The Frame Debugger window will tell you why they're not batching on the side of the window you've cropped out of your screen grabs. Select on of those draw calls and there will be a bold:

    Why this draw call can't be batched with the previous one

    Literally, that's the text you'll see, followed by the exact reason why. If you don't understand the reason, or how to fix it, post the reason it says here.
     
    yaaashua likes this.
  3. yaaashua

    yaaashua

    Joined:
    Jan 27, 2013
    Posts:
    4
    there is no such a message.
    And as I go trough each step I can see in the shader properties that all of them have the same settings
     

    Attached Files:

  4. yaaashua

    yaaashua

    Joined:
    Jan 27, 2013
    Posts:
    4
    this what is being drawn on each step.

    If some meshes uses vertex color and some other meses doesnt,
    Then almost every mesh is rendered in an individual drawcall, and this get worst and worst the more elements I add in screen.

    - I have tried using MaterialPropertyBlocks instead of vertex color, but same result
     

    Attached Files:

    Last edited: Jun 23, 2021
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    That's wild that it doesn't show any reason for not batching. It seems like you've found a case they totally missed. I might suggest reporting this as a bug using the Help -> Report A Bug menu in the editor.

    However I believe the reason they aren't batching is this:
    Specifically the fact that not all of the meshes have vertex color assigned. Try assigning vertex color to all of the meshes, even when you want them to be solid white.
     
    yaaashua likes this.
  6. yaaashua

    yaaashua

    Joined:
    Jan 27, 2013
    Posts:
    4
    I tried in a newer version (this project has set urp unlit shaders) , and the error says:
    unity dynamic batching is turned off in the player settings or is disabled temporarily in the current context to avoid z-fighting.

    the funny part is that the dynamic batching option does not appears in the place it should be (settings/ player / right under static batching)
    so the last option would be "is disabled temporarily in the current context to avoid z-fighting"

    Any idea why this could be?
     
    Last edited: Jun 25, 2021
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    So, the reason I didn't reply is because the answer is ... no, I have no idea why. I know dynamic batching gets disabled for some things like the shadow caster passes, but I don't full understand why, and I don't know why it would only sometimes disable it for what you're doing here. My expectation is if all objects have the same set of vertex data (ie: they all have the vertex color added) then they can be batched. It doesn't make any sense that they'd only batch when they're all using the red to green vertex coloring, unless that code is also adding other vertex properties as well.
     
    yaaashua likes this.
  8. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233
    @yaaashua
    To get the dynamic batching option with URP check the settings. In the Project -> Assets -> Settings.

    dynamic.png

    Then select one of the URP packages High Medium or Low and the dynamic batching check is there

    dynamic 2.png



    I am on Unity 2020.3 because all versions before it has the URP Stats window broken regarding batching (It is not updating and also showing negative values in saved by batching)

    But still I am struggling to understand what is wrong because it seems the batching does not work. A simple test scene with cubes marked as static and some moving cubes. Both groups sharing their own material and saved by batching is still 0.

    And when I check the Frame Debugger the draw calls are same as the quantity of these simple cube primitives.

    "SRP: First call from ScriptableRenderLoopJob." The reason for not batching, but after googling I can not find anything.

    This rabbit hole is driving me crazy
     
  9. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233