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

Sprite Diffuse shader breaks dynamic batching

Discussion in '2D' started by Zilk, May 10, 2014.

  1. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    329
    I'm working on a project that uses sprite renderers and the sprite-diffuse shader. I can't seem to get dynamic batching to work. For instance I have bullet shells that are spawned when shooting but they are counted as one drawcall per shell. Even the floor and wall tiles is counted as one drawcall per sprite.

    I have even tried creating a new scene and just adding sprites and lights and batching breaks creating one draw call per sprite. If I remove all lights they are batched. Anyone got a solution? Included a screenshot to give you an idea how it looks.

    $8ENjze8.jpg
     
  2. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    I don't know if there's a solution for that sadly... I noticed the same thing when I was playing around with lighting in my game, in the end I chose for a fake/custom light effect ^^'
     
  3. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    329
    It's really odd. But it might be because the sprites are transparent and transparency often breaks batching. Although then a Diffuse-Cutoff shader should work as it then a opaque material. Annyoing as hell since I end up with over 500 draw calls for some simple 2D sprites.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Do some searches, this topic's been covered a lot of times why batching breaks, and how to avoid it.
     
  5. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    329
    I actually have been but all I could find was that it happened when changing the material in script, which I'm not. I even tried a clean scene without scripts and batching broke.

    Do you have any link to a thread where it has been covered that I might have missed?
    Thanks
     
  6. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Whoa this looks bad. Send us a repro case please!
     
  7. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    329
    TomasJ: Sorry I've been away from the forum for a while and yesterday stumbled onto this issue again. I could send a sample project showing the issue if you want?

    Have there been any similar reports where Sprite Diffuse breaks batching even when atlased?
     
  8. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Sent it over!
    There were no reports. I expect to find some other issue in your project :)
     
  9. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    329
    Great, check your messages for a download url :)
     
  10. 8bitCartridge

    8bitCartridge

    Joined:
    Feb 26, 2014
    Posts:
    144
    I have the same problem in a simple 2D game. Sprite Diffuse material breaks batching completely and gives me an extra 50 draw calls.
    Did anyone find a solution for this?

    Thanks.
     
  11. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
  12. 8bitCartridge

    8bitCartridge

    Joined:
    Feb 26, 2014
    Posts:
    144
  13. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    329
    8bitCartridge: Did you get this to work? I'm still getting massive amounts of draw calls and nothing seems to help it. Deferred rendering doesn't do anything, no shader I'm testing improves anything, transparent or not/with or without multipass. I have a lot of lights but 4-8 lights on screen at the same time shouldn't totally break all batching, right?
     
  14. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Did you try the shader from the link?
     
  15. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    329
    Yeah, I tried it but no batching. I didn't rewrite it to be transparent though. Have tried a bunch of different shaders but batching of sprites just doesn't want to work for me.

    Batching seem to break as soon as I use a lit shader of any kind with the SpriteRenderer.

    How does deferred work with sprites, shouldn't deferred be suggested when using many lights to keep draw calls down or is this something I have misunderstood?
     
  16. Kiori

    Kiori

    Joined:
    Jun 25, 2014
    Posts:
    161
    I know this is an old post but for those wondering, batching is working fine atm, at least for me.
    The limiting factor is as always the ammount of materials on scene.
    So if i have a bunch of sprites with sprite-difuse and some others with transparent-specular whatnot, i still get 2 draw calls, as intended, provided they are all in an altas.

    I hope we get Sprite/Specular in the future.
     
  17. Randy-O

    Randy-O

    Joined:
    Aug 15, 2014
    Posts:
    8
    Was there ever a solution to this?

    I tested the shader linked above to no avail, and have very clearly arrived at the same result that every sprite affected by lights creates a draw call. I have a scene with 1 spotlight and 1 spritesheet, where all sprites have one of 2 materials, I can still end up with 120 draw calls.
     
  18. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    This thread has been cold for a while, wondering if I can revive it. Using Sprite / Diffuse at the moment, it does appear that every sprite (not texture or sprite sheet) hit by a single light source results in a separate Batch. I tried the shader linked above, it changes nothing.

    If I have a scene with the hero and a single enemy, all of which use the same Material and Texture I get 110 Batches, with 0 Saved. If I switch the Material to Sprite / Default I get 22 Batch, still 0 Saved.

    Switching to diffuse is 5x less performant? Is that right? What is the performant way to use 2D lighting?

    Scene stats

    Single material (Sprite / Diffuse)
    One light source (Spot, Realtime)
    One texture atlas

    Camera Effects

    Noise And Grain +1 Batch
    Vignette and Chromatic Aberration +7 Batch

    Graphics

    Batches 110
    Saved by batching 0
    Tris 1.3k
    Verts 1.6k
    SetPass calls 95
    Shadow Casters 0
    Skinned Meshes 0
    Animations 0

    All of the material I've read say the same thing, Batches are about Materials, Textures and Density of 3D primitives.

    What am I doing wrong here? This feels like a very simple scene.

    Here is what I would expect:

    +1 Batch for Material + Texture (same throughout scene)
    +1 Batch for light source

    +1 Batch for transparency calculations

    +1 Batch for Noise and Grain

    +7 Batch for Vignette

    11 Batch total

    How are we getting to 110?
     
  19. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    960
    So I'm going to necro this thread again. There is definitely an issue with the SpriteRenderer and dynmaic pixel lights. Here is a list of step to reproduce the issue.

    -Create a material and assign the 'Sprites/Diffuse' shader
    -Create a square sprite using Unity's built-in tools. Ensure it is entirely opaque.
    -Assign the aforementioned material to sprite.
    -Duplicate sprite several times (I made 11), leave all sorting settings at default with 0 for sort order.
    -Ensure no sprites are overlapping. At this point conditions are 'perfect' for batching.
    -Add light to scene and set it to 'Not Important'.
    -Ensure light's range covers all of the sprites.
    -Start scene and ensure draw calls are a 2 (1 for screen-clear, 1 for batched sprites).
    -Sometimes, depending on light conditions and sprites there may be more batches but never anything unreasonable.
    -Change light to 'Important' to enable pixel lighting.
    -Batching is now completely broken and each sprite is being rendered one at a time with two passes each. Total batches at 1 + (2N) batches - where 1 is the screen-clear and N is the number of sprites.

    It is entirely understandable that each pass would be breaking batches, but in this case each sprite both on the ForwardBase and the ForwardAdd is being rendered separately with its own draw call. That's two draw calls per sprite which is entirely unreasonable.
     
  20. HeBin

    HeBin

    Joined:
    May 14, 2015
    Posts:
    4
    The same problem I met. Hope there is a way to solve it.
     
  21. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    I'm finding the exact same thing.

    @Sluggy where did you learn / read -about the impact of 'Not Important' vs 'Important' wrt to pixel vs vertex shading?
     
    Last edited: Oct 10, 2016
  22. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    960

    Its in the documentation here https://docs.unity3d.com/Manual/class-Light.html

    You should also note that even when all lights are set to 'important' Unity will only render the maximum supported number that are closest to the camera as pixel lights (as defined by your quality settings). The rest will be treated as vertex lights or Spherical Harmonics.

    Edit: my mistake, it actually says 'important' always renders as pixel light. 'Auto' is what I was thinking of.