Weird Performance Problem with GLSL Shader

Hello,



I'm having a very weird problem with my shader. The shader has two main goals : update the saturation of an image and mix 2 textures. The thing is I don't need to update the saturation if I have a zero value in an uniform.

When I comment the function to update the saturation, I only have texture blending, and then I have around 70 fps. But when I'm adding the function, even if I set a zero value in the uniform, I get around 30 fps. The weird part is that nothing about saturation is computed, but performances are the same if the saturation is computed or not.



I don't know if  you understand my problem so I'll show you the fragment I developped :


uniform float alphaShadow;
uniform bool enableHSVAdjust;
uniform vec3 hsvAdjust;
uniform sampler2D normalTex;
uniform sampler2D shadowTex;

varying vec3 position;
varying vec3 lightDir;
varying vec3 normal;

(...)

void main()
{
   vec4 color = texture2D(normalTex,gl_TexCoord[0].st);
   vec4 shadowColor;// = texture2D(shadowTex,vec2(position.x,position.y));
   vec3 hsv;


   if (enableHSVAdjust)
   {
      color = adjustHSV(color,hsvAdjust);
   }

   //light
   vec3 ct,cf;
   float intensity,at,af;

   intensity = max(dot(lightDir,normalize(normal)),0.0);
   cf = intensity * (gl_FrontMaterial.diffuse).rgb +                    
      gl_FrontMaterial.ambient.rgb;      

   af = gl_FrontMaterial.diffuse.a;            
   ct = color.rgb;      
   at = color.a;
   
   shadowColor = texture2D(shadowTex,(vec2(position.x/position.z,position.y/position.z)+1.0)* 0.5);
   
   if (alphaShadow <=1.0)
   {
      shadowColor=1.0-shadowColor;
      shadowColor*=alphaShadow;
      shadowColor=1.0-shadowColor;
   }

   gl_FragColor  = vec4(shadowColor*cf*ct,at*af);
}



The part with a performance trouble is this one :

if (enableHSVAdjust)
{
   color = adjustHSV(color,hsvAdjust);
}



If I set enableHSVAdjust to false, it computes 30fps, and if I set it to true, also 30fps. But if I comment this code, I'll get 70 fps!!!  I thought it was a glsl compiler issue but even  if the code is not executed it shouldn't divide the fps by 2.

I hope someone will know why I'm having this issue.
Thanks in advance for your help,
Boris

It's not wierd… GPUs don't like flow control, e.g if statements. Therefore it's best to avoid them if possible. In your case you can make one shader that adjusts HSV and one that doesn't, and then switch shader states in code.

systems that doesnt support branching (most out there) will execute all the code in the shader, without regard to the conditionals. i recommend using glsl functions like step/smoothstep to achieve similar features, or replace whole shaders like momoko says.

Hi!



Thanks for your answers. I actually did an other shader without statements and I just switch from my jme program. Anyway, it's very good to know that!



Thanks again,

Boris