Hello Guest

Author Topic: Z position influences draw calls  (Read 6917 times)

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Z position influences draw calls
« on: July 23, 2013, 05:56:56 pm »
Not sure if this is a Unity specific or tk2d specific question.

I noticed I have more draw calls than expected, so I started to investigate.
Bottom line, it seems that when instantiated objects have a different Z position, the result is a higher draw call count than if not.

The experiment is simple:
I instantiate object 1, followed by instantiation of object 2, then 1 then 2 again.
Each object uses a different sprite collection.
The result: With the same Z, 2 draw calls, with different Z, 4 draw calls.
Also, when instantiating at a different order (1 1 2 2) the result is 2 draw calls regardless of the Z.

Screenshot:


Code:
Code: [Select]
using UnityEngine;
using System.Collections;

public class DrawCallTester : MonoBehaviour {

// Attach two sprites that use a different collection
public GameObject proto1;
public GameObject proto2;

void Start() {
// Result:
// When Z is the same, success  ( 2 draw calls )
// When Z is different, failure ( 4 draw calls )
// When order is 1 1 2 2, success ( 2 draw calls )
int x = 1;
Instantiate( proto1, new Vector3( x++*100, 300, x*10 ), Quaternion.identity );
Instantiate( proto2, new Vector3( x++*100, 300, x*10 ), Quaternion.identity );
Instantiate( proto1, new Vector3( x++*100, 300, x*10 ), Quaternion.identity );
Instantiate( proto2, new Vector3( x++*100, 300, x*10 ), Quaternion.identity );
}

}



Is this to be expected?
Is there anything I can do about it?

Thanks in advance.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Z position influences draw calls
« Reply #1 on: July 23, 2013, 09:52:11 pm »
It is expected.
#1 is random, don't rely on this behaviour. Everything else is deterministic.
A B A B = 4 draw calls
A A B B = 2 draw calls.

Always sort by sprite collection / atlas where possible.

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Z position influences draw calls
« Reply #2 on: July 23, 2013, 10:00:59 pm »
Hmm... thats annoying.
Controlling the instantiation order will require significant changes in my code.

Do you know why is this happening? I could not find anything in the Unity docs or by googling.

I have about 80 draw calls instead of what should be around 8.
I am wondering if this is worth optimizing at all, but in any case, I am curious to understand this phenomenon.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Z position influences draw calls
« Reply #3 on: July 23, 2013, 10:11:21 pm »
You don't have to control your instantiation order, but rather give items from one collection a fixed z value. Eg. collection0 = 0.1, collection1 = 0.2. Wouldn't that work?

Unitys sort is probably sorting by depth to camera and material, and its basically handling this case with a bunch of objects at the same Z value arbitrarily. In this case, based on the order of instantiation.

* Also if you'd like to try out a super secret feature which may or may not be in the next version, drop me an email.

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Z position influences draw calls
« Reply #4 on: July 23, 2013, 10:33:10 pm »
Well... no, I guess I cannot force Z order by collection, as the objects overlap, the Z order is kind of important to be kept based on the position of the object, rather than its collection affiliation.

This is a screenshot.
Tiles on the left-top-front are closer to the camera, so Z has a lot of meaning here.