Search Unity

Batching and Sorting group

Discussion in '2D' started by rarac, Oct 26, 2021.

  1. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    I have a game object with a sprite renderer and a sprite mask

    the sprite renderer is creating adding 1 draw call while the sprite mask is adding 2 draw calls, for a total of 3 draw calls

    When I spawn 100 game objects its now 300 draw calls

    These objects all share the same sprite and sprite mask, and I have tried adding them to an atlas to no avail. They all share the same material too. I'm using the URP 2d renderer pipeline with a lit shader, and all sprites are lit by the same global light.

    this is annoying because I am managing to batch the tiles in my tilemap and Im saving a lot by batching those, I wanted to save while using sprite renderers aswell.

    Am I doing something wrong or is it normal that 1 sprite renderer always generates 1 draw call that cant be saved by batching?
     
    Last edited: Oct 26, 2021
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    So i think I homed in on the problem, sorting groups seem to be breaking the batching? Is it possible to batch with sorting groups?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    With SpriteRenderers I seem to recall having diffrent .color properties will trigger fresh batch calls.

    Annoyingly sometimes getting the drawcalls down requires reorganizing your GameObject hierarchy so that Unity is able to collapse things that you think can be collapsed.

    Now this here is for UI, but perhaps there's some meaty info in here regarding your issue?

    https://learn.unity.com/tutorial/optimizing-unity-ui
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you share some details of your setup with the SpriteRenderers, SortingGroup and SpriteMask, eg. how are they set up in the Hierarchy? Also, would it be possible to share the details of how batching is (not) done by using the FrameDebugger? The FrameDebugger will show a reason why the SpriteRenderers are not batching.

    Generally, each SpriteMask does not batch with each other and that can cause the SpriteRenderers not to batch if each SpriteRenderer uses its own SpriteMask.
     
  5. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    yes that is what is causing it I have for hierarchy

    Game Object
    -Sorting group1
    --Sprite mask1
    ---Masked sprite1

    -Sorting group2
    --Sprite mask2
    ---Masked sprite2

    this object produces 6 draw calls, after i clone it 12 etc etc
    if i take the stuff out of the sorting groups and remove the masks its only 2 draw calls no matter how many clones