GLSL Problem when using pow ()

I am currently using a shader to execute some directional lights. I follow the tutorial on Lighthouse 3d ( http://www.lighthouse3d.com/tutorials/glsl-core-tutorial/directional-lights-per-pixel/ )

The problem I am facing is the following lines:

vec3 h = normalize(l_dir + e); float intSpec = max(dot(h, n), 0.0); float specularPower = pow(intSpec, shininess); 

If I leave the line “float specularPower”, then the shader will no longer “work” .... I say “works” in quotes because I do not get any output or errors from Shader Log, however now there are single places for my entire return -1, and I can not set the texture location, etc.

If I remove it, the rest of the shader works as expected (but producing incorrect results due to the lack of Specular Power).

What is even more curious is that if I have nVidia Nsight Debugger connected, I get the output on the screen and it seems to work, but if I just use VS2012 in debug mode, I can’t see anything on the screen, and following error message:

 First-chance exception at 0x000000005EB066E6 (nvoglv64.dll) in application.exe: 0xC0000005: Access violation reading location 0x0000000000000008. First-chance exception at 0x000000005EB066E6 (nvoglv64.dll) in application.exe: 0xC0000005: Access violation reading location 0x0000000000000008. 

This behavior has been demonstrated on both the GTX 480 and GTX 560, on a PC with 64-bit Windows 8.

I know that -1, returned as a unified location, either means the wrong name, or the compiler optimized it, but there is no point in adding it to one line, the result of which is then not used at all after that. Even less does it make sense that the behavior is different from when the NSight debugger is connected or not.

What can i do wrong?

Edit:

The following are the simplest Vertex / Fragment shaders that I could create that replicate the problem while maintaining the real world shader.

Vertex Shader:

 // Default Vertex Shader #version 430 core uniform mat4 projMatrix; uniform mat4 modelMatrix; uniform mat4 viewMatrix; in vec4 in_Position; in vec3 in_Normal; in vec2 in_TexCoords; out vec2 pass_TexCoords; out vec3 v; out Data { vec3 normal; vec4 eye; } DataOut; void main(void) { DataOut.normal = normalize(in_Normal); DataOut.eye = (viewMatrix * modelMatrix) * in_Position; pass_TexCoords = in_TexCoords; v = vec3(in_Position); gl_Position = projMatrix * viewMatrix * modelMatrix * in_Position; } 

Fragment Shader:

 // Default Fragment Shader #version 430 core uniform sampler2D material[3]; in vec2 pass_TexCoords; in Data { vec3 normal; vec4 eye; } DataIn; layout (location = 0) out vec4 out_Colour; void main(void) { float shininess = 0.5; vec3 l_dir = vec3(0.0, 0.0, 1.0); vec3 n = normalize(DataIn.normal); vec3 e = normalize(vec3(DataIn.eye)); float intensity = max(dot(n, l_dir), 0.0); float specularPower; if(intensity > 0.0) { vec3 h = normalize(l_dir + e); float dHN = dot(h, n); float intSpec = max(dHN, 0.0); float specularPower = pow(intSpec, shininess); } else { specularPower = 1.0; } vec4 diffuse_Colour = texture(material[0], pass_TexCoords); out_Colour = diffuse_Colour * specularPower; } 

I also checked the program information log and did not return any errors. Once again, with these shaders, it does not work (the uniform returns -1) when launched through VS2012, but it "works" when the nVidia Nsight debugger is connected.

+7
c ++ shader opengl glsl
source share
1 answer

There are interesting things in this shader, and all of them are related to the fact that this line:

 float specularPower = pow(intSpec, shininess); 

Does absolutely nothing in the final compiled shader. You have shaded a variable with the same name in the scope.

Here is what your shader does (as written):

 // Default Fragment Shader #version 430 core uniform sampler2D material[3]; in vec2 pass_TexCoords; in Data { vec3 normal; vec4 eye; } DataIn; layout (location = 0) out vec4 out_Colour; void main(void) { vec3 l_dir = vec3(0.0, 0.0, 1.0); vec3 n = normalize(DataIn.normal); float intensity = max(dot(n, l_dir), 0.0); float specularPower; if(intensity > 0.0) { // This branch did not affect anything outside of this scope, so it is gone... // specularPower is uninitialized if this branch is triggered } else { specularPower = 1.0; } vec4 diffuse_Colour = texture(material[0], pass_TexCoords); out_Colour = diffuse_Colour * specularPower; } 

If you replace the first expression that I mentioned:

 specularPower = pow(intSpec, shininess); 

Your shader will really do something in the if (intensity > 0.0) { ... } branch.

Since shininess is a constant in this program, you can also replace this statement as follows:

 specularPower = sqrt (intSpec); 
+4
source share

All Articles