- Home /
Draw call depending of sorting Order of SpriteRedenderer(2D)
Hello,
Clear problem:
2 sprites Atlasses A and B.
One sprite form Atlasses A (SA) and another from B (SB).
The drawCalls increasses if the order of the sprites is
Sorting Order: SA - SB - SA
This will draw SA first, after the GPU will change to TextureAtlass B (+1 drawcall). Finally the GPU will change the TextureAtlass again to Atlass A to draw SA(+1 drawcall).
Total drawCalls = 3. Expected drawcalls = 2. (Because only 2 Atlasses)
Is this normal or Can it be optimized for less drawcalls? The order is important for the game, and also I already know that one possibility is combine the 2 TextureAtlass.
Thanks.
Answer by codebeans · Apr 08, 2016 at 09:07 PM
3 draw calls is correct in this case, here is why.
A sprite is really just a bunch of vertices, triangles, uvs and a shader that has information like the current texture. read more These information will then be gathered and sent to GPU. Unity reduces draw calls under the hood by batching these sprite data together and sends them in one big chunk to the gpu instead of each sprite's data individually.Each sending operation means one addional drawcall.
The first criteria to batch sprites is that they reference the same texture.
So why does SA1-SB-SA2 result in 3 draw calls instead of 2 then?
Having the sprite (SB) between SA1,SA2 will break the batch because it references a different texture
Step by step:
Unity figures out render order by depth to be SA1-SB-SA2
Open first drawcall (vertexbuffer, indexbuffer,...) and add SA1
Find next sprite SB
Textures do not match (stop batch, send to GPU)
Open second drawcall and add SB
Find next sprite SA2
Textures do not match (stop batch, send to GPU)
Open third drawcall and add SA2
No more sprites, finish and send to GPU
Now what about SA1-SA2-SB
Unity figures out render order by depth to be SA1-SA2-SB
Open first drawcall (vertexbuffer, indexbuffer,...) and add SA1
Find next sprite SA2
Textures do match, append SA2's data to the drawcall
Find next sprite SB
Textures do not match (stop batch, send to GPU)
Open second drawcall and add SB
No more sprites, finish and send to GPU
Bad news is unless you want to dig deep into OpenGL that you have to combine both your Atlases A and B.
Note: If this happens a lot in your scene (SA-SB-SC-SA-SC......) and you can't combine the atlases that makes texture atlasing useless and you shouldn't use it then.
In that case runtime packing is the way to go.
Your answer
Follow this Question
Related Questions
How come setting sprite tint/alpha does not increase draw calls? 0 Answers
How can i get my gameobject as a sprite from its collider name? 1 Answer
Sprite Alpha Performance 0 Answers
Is there a way to change the sprite for all instances of the same object simultaneously? 1 Answer
Sorting Layer Logic Issue 0 Answers