Search Unity

Each Sprite in Atlas taking up a Draw call.

Discussion in '2D' started by xCyborg, Dec 6, 2013.

  1. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    I don't know what's wrong with the stats window, but it's completely unpredictable and not very precise when it comes to draw calls, isn't it supposed to show only one draw call per atlas, in my case it shows a draw call per sprite(at least not by clone of sprites, but per sprite instance), is it how it's supposed to work, or the stat window is off, or maybe I need to make it static or....
     
  2. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    Make sure you're not making copies of the sprite in the project bar. Otherwise, I can't seem to find any other solution.
     
  3. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    What you mean by copies, in the atlas or in the assembled object in the scene, anyway Im doing them both, after I changed the name of copies, here is my atlas and the object that uses all of its sprites, and a double of side cannon, it takes 7 to 8 drawcalls for the first instance.
    $atlas.PNG $object.PNG
    Do I have to do something to get this right, or is 8 draw calls normal for this setup?
     
  4. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Hi,

    Cool artwork!
    Make sure you have Sprite packing enabled in play mode if using the built-in packer. Editor Settings -> Sprite Packer -> Always Enabled. That should pack your sprites when entering play mode and display correct drawcall count.
    Also make sure it's the same material on all SpriteRenderers.
     
    Txangu likes this.
  5. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    I added a packing tag to the atlas and enabled Packing as Always, but nothing happens, tha atlas integrates well in the packed atlas in the Sprite Packer Window but no actual natching. That's really weird, I have other characters composed of various sprites, but they consume only one drawcall, I only encountered this problem with this perticular atlas, so I suspect maybe because of the close spaces or the setup of the atlas.
    Edit: with another random scene view setting, I got only 2 draw calls! is the Animator component, layer, a bug in the stats window, I reall cant figure this out.
     
  6. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    YES! confirming the underlying cause: Orders in Sorting Layer! Assigning more orders to different sprites in the setup doubles the drawcall.
    This seems to me like a bug, or is it intended like that, eitherway Unity please do something about that, in my case I got tons of sorting layers and orders. and I can't afford a draw call for each sprite in each order.
     
  7. eviltenchi_84

    eviltenchi_84

    Joined:
    Feb 18, 2010
    Posts:
    99
    Having the same issue. Not sure if it doubles the draw calls but there are far more than I had prior to using the new sprite system. Also, I did notice that if I disable all of the sprites while playing and re-enable them, my draw calls go down and many of the sprites are now batched. Perhaps sprites on different layers are not properly getting batched? Anxious to hear from someone at Unity if this is a bug or intended behavior.
     
  8. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    I meant each order in a Sorting Layer on a perticular atlas uses 1 draw call.
    You are right about the disable/enable phenomenon, it happens all the time with me, I have no idea why,
    Reported a bug:1) What happened
    Sprite Batching shutdown on multiple sorting layer order sprites.
     
  9. meta87

    meta87

    Joined:
    Dec 31, 2012
    Posts:
    254
    I am seeing the same thing and wondering if it is an expected behavior...
     
  10. eviltenchi_84

    eviltenchi_84

    Joined:
    Feb 18, 2010
    Posts:
    99
    enable/disable bug still present in 4.3.3
     
  11. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    I too have the same issues ... and it is quite annoying.

    To add more info (and maybe confusion)

    I have a scene with about 100 sprites that uses about 50 draw calls !!!

    All sprites are:
    - in the same sorting layer (the default)
    - using the same material (default sprite)
    - "order in layer" is 0 (same as SpriteRenderer.sortingOrder???)


    Clues ...
    - The camera rendering the sprites is perspective (but changing to orthographic doesn't help :)
    - The atlas is normally in two pages of 2048x2048 (but fitting them on one page doesn't help)
    - The number of draw calls varies a little different every time I press play: 49, 50, 51, 50, 48, 52, etc.
    - The sum of the "draw calls" and "saved by batching" is constant :D
    - If I disable and reenable the sprites the draw call drops to 35 (and, its the same number every time!)

    I'm so puzzled ... please help me Unity :D
     
  12. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    Ahaaa & hurray !!! ... I've found the two bugs at play ... :D

    1) When you select multiple textures and try to set them to use the same packing tag Unity will not always do it! Say I have two textures with the packing tags "a" and "" (nothing). If I select them both they will show up in the multi-inspector as both being tagged "a" ... and even if I type "a" and press apply Unity wont set the tag of the second one! Solution: select all and call it "b" and they will all change. There are probably more bugs down this hole :D

    2) I had a script that at Start of the sprites would randomly toggle the sign of their x/y-scale (to give a little randomness to the graphics). This apparently destroys unity's ability to batch. Must also be a bug, right?

    Cheers :D
     
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Yes, objects with different and non-uniform (XYZ not all the same as each other) scales do not batch. I don't think it's a bug so much as a design decision (apparently somehow the transformation math on those is slower?), but it is a known thing about batching.
     
  14. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    So since you bumped this up, I have now retested the issue on Unity 4.5.0f6 and it's seems all good, any given numer of sprites from a single atlas takes just one draw call no matter the sorting layer and order in layer they're in. until of course I scale the sprite's x and y to opposite sense values (both negative is fine) or negative z: in this case batching is normally broke, but also the sorting order comes into play again, I was gonna write a simple formula but it looks like it's much more complicates than I thought.
    Anyway it's more than likely now just the normal inner working of 2D, keep the sprites scale values in check or use sprite packer and you'll always get that much sought after 1 draw call.
     
  15. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Hey xCyborg,

    I'm not sure I understood your post. Are you saying there's still a way to break batching?
     
  16. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    I said sprite batching works perfectly for me even if I scale the sprites uniformly negative, now if you guys want to go an extra mile and figure out a way to batch non-uniformly scaled sprites I wouldn't say no.
     
  17. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    Hi all.

    I think there still is a problem in the batching mechanism.

    The number of draw calls goes up by 2 for each sprite that has a flipped scale !!

    That is:
    10.000 identical sprites without any scale flips -> 1 draw call.
    10.000 identical sprites with the same scale flip -> 20.000 draw calls !!

    At least I think Unity should group those scale flipped sprites,

    But ... I've found a nice solution ...

    Instead of flipping sprites with scale I now do it with rotation :D

    Cheers,
    Jesper

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RandomizeGfx : MonoBehaviour
    5. {
    6.     public bool random180Z;
    7.     public bool randomFlipX;
    8.     public bool randomFlipY;
    9.  
    10.     void Start()
    11.     {
    12.         if ( random180Z && (Random.value < .5f) ) transform.Rotate( 0, 0, 180 );
    13.         if ( randomFlipX && (Random.value < .5f) ) transform.Rotate( 0, 180, 0 );
    14.         if ( randomFlipY && (Random.value < .5f) ) transform.Rotate( 180, 0, 0 );
    15.     }
    16. }
     
  18. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Will investigate. Case no. 616057. Hopefully will fix in next release.
     
    Last edited: Jun 26, 2014
  19. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    Kewl ... :D

    If you cannot get Unity to batch scale flipped sprite without causing extra draw calls, then the trick should be discouraged all over: Unity documentation, the forums and example projects (it is currently common knowledge that this is how to flip sprites !!)

    Instead ppl should be told to use rotation witch works like a charm.
     
    Last edited: Jun 26, 2014
  20. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    ... and btw. I've also reported the multi edit bug ... Case no. 616075

    Cheers
     
  21. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    And, while I got your attention ... please also kick someone to take a good look at case 552331 ... :D
     
    Last edited: Jun 26, 2014