Hello Guest

Author Topic: Overlay a color completely? -Performance cost?  (Read 5344 times)

Dajuice

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Overlay a color completely? -Performance cost?
« on: November 01, 2012, 08:30:13 pm »
In this topic
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,118.msg311.html#msg311

you propose a shader I imagine you made for the guy, nice! But when I wanted to use it in my game so I changed every shader for this, changed some parts of the shader so I could have black as well as white shades of my sprites, then I tried all that on the iPad1... the framerate was around 30fps (the game is supposed to run at 60fps, it is a really basic game). The part I changed to do this was:

Code: [Select]
SetTexture [_MainTex] { combine texture+primary, texture * primary}to
Code: [Select]
SetTexture [_MainTex] { combine texture+-primary}
SetTexture [_MainTex] { combine previous+-primary, texture * primary}

Then I figured that settings a texture twice would be rather GPU intensive so I reverted to the original version, but it was only slightly better, setting all the materials to the original shader gave me a steady 60fps.

I only have one element to flash to white so this is not so much of an issue for me right now, but I'm planning to make a game that will hold many more blinking elements on screen, do you have any idea as to why the shader is so much heavier and if there could be another way/shader to achieve white blinking?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Overlay a color completely? -Performance cost?
« Reply #1 on: November 02, 2012, 12:21:43 am »
You should probably get a bit better performance doing this in CG, but can't say for sure until trying it out. If you're using a recent version, it'll be using CG shaders anyway. Get one of those and apply the changes here to it to see what the perf is like - it WILL be slower, as it's one more instruction at least, but perhaps it could be faster than this...

The fragment shader probably wants to be something like this:
Code: [Select]
fixed4 col = tex2D(_MainTex, i.texcoord);
col.rgb += i.color.rgb;
col.a *= i.color.a;

If you don't need to be able to fade out, then the alpha channel can be exactly the same as rgb and that'll save one instruction. You could probably try that on the original fixed function one as well by changing it to:
Code: [Select]
SetTexture [_MainTex] { combine texture +- primary } 
This way can make it white or black, and in theory do some form of alpha offset fade out.

Dajuice

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Overlay a color completely? -Performance cost?
« Reply #2 on: November 08, 2012, 05:43:47 pm »
Thanks for the feedback! I did some testing and your additive shader looks to be similar to the new solution you gave in term of performance, maybe an overall gain of 1 frame on 50 fps, yet again this "gain" is too low to determine if it is really attributable to the new shader.