Simple GLSL program crashes

I was trying to debug a problem with a bump mapping shader, when I was surprised when my simple glsl code for debug did not work.

The following code crashes:




uniform sampler2D diffuse_map;

uniform sampler2D normal_map;


varying vec3 lightVec;
varying vec3 halfVec;
varying vec3 eyeVec;


void main (void)
{


        gl_FragColor= vec4(texture2D (normal_map, gl_TexCoord[0].st).rgb, 1.0);

}


But this other code did not:


uniform sampler2D diffuse_map;
uniform sampler2D normal_map;

varying vec3 lightVec;
varying vec3 halfVec;
varying vec3 eyeVec;


void main (void)
{
        vec2 texCoords;

      
       
        texCoords = gl_TexCoord[0].st;
        


        // fetch normal from normal map, expand to the [-1, 1] range, and normalize
        vec3 normal = 2.0 * texture2D (normal_map, gl_TexCoord[0].st).rgb - 1.0;
        normal = normalize (normal);

                // compute diffuse lighting
        vec3 diffuse = max (dot (lightVec, normal), 0.0) * vec3 (gl_LightSource[1].diffuse);
        vec3 decalColor = texture2D (diffuse_map, texCoords).rgb;

        // compute specular lighting
        //vec3 specularCoeff = texture2D (glossMap, texCoords).rgb;
        float specular = max (dot (halfVec, normal), 0.0);
        specular = pow (specular, 8.0);
        
        // output final color
        gl_FragColor = vec4 (diffuse * decalColor + vec3 (specular), 1.0) + gl_LightSource[1].ambient;
}


Have you tried removing the unused uniforms and varyings?

Regards,
Patrick

Thanks pjcozzi, I needed to clean not only the fragment shader, but also the vertex shader for this piece to run.

This seems very inefficient for testing purposes. I tried only changing the last line of the shader that compiled, like:


gl_FragColor= vec4(decalColor, 1.0);

But it made it crash. Very strange, seems like the GLSL compiler is discarding everything not used and thus giving a problem with the parameters being passed from the CPU. This is the only possibility I can think.

I bet that is exactly what is happening. If your GLSL code does not use a uniform, the uniform is optimized out, and it not part of the “active uniforms.” From the perspective of your application, the shader program doesn’t even have the uniform.

Regards,
Patrick

Good to know I am not alone on that.

If there was an way to compile the GLSL code without optimizations… that would be nice for debugging!