Hello Guest

Author Topic: tk2d sprites not dynamic batching  (Read 8808 times)

hemdanw

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
tk2d sprites not dynamic batching
« on: February 13, 2013, 12:37:02 pm »
Hi,

Using v1.80 (final), I noticed that my tk2d sprites were not dynamic batching. They are all local scale (1,1,1), not sure what other parameters to check. But, my tk2d animated sprites were all dynamic batching fine.

As an experiment, I converted each tk2d sprite into an animated sprite, duplicating the image twice (I think there's an optimization that catches same image - so I tricked it into thinking they were different images) and creating 2 identical animation frames so it looks like the original sprite. Interestingly enough, my draw calls dropped from ~100 draw calls to about ~20 draw calls. I am assuming that the animation is more CPU intensive than a regular sprite - so I would like to get dynamic batching working with regular sprites. Is there a bug with tk2d sprites preventing dynamic batching or am I doing something wrong?

Thanks,
   - hemdanw

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d sprites not dynamic batching
« Reply #1 on: February 13, 2013, 12:40:00 pm »
No they should work fine, animated or not - animated sprites just inherits from tk2dSprite, so they do exactly the same thing.

What version of Unity are you using? You're not getting any batching at all?

hemdanw

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: tk2d sprites not dynamic batching
« Reply #2 on: February 14, 2013, 12:30:31 am »
Hi,

I'm using unity 4.0.1f2.

I'm displaying ~100 copies of the same sprite, I tested overlapped and non-overlapped - no difference. The draw calls goes up by exactly 100 to 120 draw calls. When I made it an Animated Sprite, the draw calls fell to 20 with both overlapping and non-overlapping scenarios... it's definitely not batching at all...

Any suggestions to further test this out are welcome.

Thanks,
   - hemdanw

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d sprites not dynamic batching
« Reply #3 on: February 14, 2013, 01:02:04 am »
Would it be possible to create a repro case for me to look at? (support@unikronsoftware.com)

I can't explain this. If they are the same sprite, there should be absolutely no difference to the animated sprite or the regular sprite. Would be interesting to get to the bottom of it

eddieB

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: tk2d sprites not dynamic batching
« Reply #4 on: February 15, 2013, 06:07:33 am »
I have the same problem with my project (sorry i can't tie you repro case)

- i'm adding a character with a lot of part to make it anim (leg arm head...)
- with one character added and change his collection in script draw call 1 OK

- Add an other one, Draw Call 26 (batching 1)
   - if I deactivate one character batching is working (batching 2)
   - if i reactivate the character Draw call 2
some time activate and deactivate change the draw call and the batching
- if y replay a lot with activate the 26 DrawCall are back 

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d sprites not dynamic batching
« Reply #5 on: February 15, 2013, 01:48:06 pm »
I have the same problem with my project (sorry i can't tie you repro case)

- i'm adding a character with a lot of part to make it anim (leg arm head...)
- with one character added and change his collection in script draw call 1 OK

- Add an other one, Draw Call 26 (batching 1)
   - if I deactivate one character batching is working (batching 2)
   - if i reactivate the character Draw call 2
some time activate and deactivate change the draw call and the batching
- if y replay a lot with activate the 26 DrawCall are back

In your case, is there ever any z overlap between the characters? That could wreak havok with dynamic batching.
Also what version of Unity are you using?

eddieB

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: tk2d sprites not dynamic batching
« Reply #6 on: February 16, 2013, 06:59:17 am »
i'm using unity 4.01

Your resolve my big problem.
Character are spawning on the same Z
if i'm make move character on different Z no problem with batching. DrawCall became normal (2) 

Thanks A lot.

hemdanw

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: tk2d sprites not dynamic batching
« Reply #7 on: April 22, 2013, 04:40:55 am »
Hi,

I have an update on what was happening in my particular case and what was preventing dynamic batching on my tk2dsprite objects... though I don't quite fully understand it yet.

I had a component on my prefab that had 2 shader class member variables. The variables were being initialized in the start method:
flashShader = Shader.Find ("BlendAdditiveVertexColor");
origShader= renderer.material.shader;

I was intending to use these shaders in the code, but had not implemented that part of the code yet, so the variables were assigned but never used. Interestingly enough, even though these variables were NOT being used anywhere else in the component, this simple initialization somehow resulted in dynamic batching on tk2dsprite's to break. Having the same component on tk2d animated sprites did not break dynamic batching.

Thanks,
 - hemdanw

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d sprites not dynamic batching
« Reply #8 on: April 22, 2013, 10:23:47 am »
That is because of your line here:
renderer.material

According to Unity docs:
"If the material is used by any other renderers, this will clone the shared material and start using it from now on. Important note: This function automatically instantiates the materials and makes them unique to this renderer. It is your responsibility to destroy the materials when the game object is being destroyed. Resources.UnloadUnusedAssets also destroys the materials but it is usually only called when loading a new level."

What you're doing is you're creating a new material for each of these sprites, and each of these sprites then has a unique material. These won't batch.

If you want to get the instanced material, you should use:
renderer.sharedMatarial