Dot product crashing shader

I’m working on a BlinnPhong shader and it crashes. The way it is written below, it works. However, if you remove the line “SpecularHighlightAmount = 0.2f;” it will crash. This line of code is unwanted and is just there to prove it runs. I have no idea what’s going on here.

The problem would appear to be in the line before it " SpecularHighlightAmount = max(0.0, dot(ReflectedLightDirection, CameraDirection));". But that line does not crash as long as you then change the value as shown. So, it appears to be calculating an invalid floating point number.

I’ve been hacking at this all day and the code’s just getting worse as I continue to desperately try changing pretty much random things.

Since the problem is in the shader, I don’t seem to have any way to get an error message and I can’t step through it with the debugger.


#version 450 core

smooth in vec2 TextureCoordinates;
smooth in vec3 VertexNormal;
smooth in vec4 RGBAColor;
in vec3 WorldSpacePosition;

layout (location = 0) out vec4 OutputColor;


uniform vec4 AmbientLightColor;
uniform vec3 DiffuseLightDirection;
uniform vec3 DiffuseLightColor;
uniform vec3 CameraPosition;
uniform sampler2D Texture0;


vec3 PhongSpecular(in vec3 LightDirection, in vec3 LightColor, in vec3 PixelNormal, in vec3 CameraDirection, in float SpecularPower)
{
	vec3 ReflectedLightDirection;	
	vec3 SpecularLight;
	float SpecularHighlightAmount;

	//SpecularLight = vec3(1.0f, 1.0f, 1.0f);
	ReflectedLightDirection = 2.0f * PixelNormal * clamp(dot(PixelNormal, LightDirection), 0.0f, 1.0f) - LightDirection;
	SpecularHighlightAmount = max(0.0, dot(ReflectedLightDirection, CameraDirection));
		SpecularHighlightAmount = 0.2f;
				//SpecularHighlightAmount = clamp(SpecularHighlightAmount, 0.0, 1.0);
	SpecularLight = vec3(SpecularHighlightAmount, SpecularHighlightAmount, SpecularHighlightAmount); 
	//(SpecularHighlightAmount * LightColor; 
	

	return (SpecularLight);
}


void main()
{
	vec3 LightDirection;
	float DiffuseLightPercentage;
	vec3 SpecularColor;
	vec3 CameraDirection;	//Float3 because the w component really doesn't belong in a 3D vector normal.
	vec4 AmbientLight;
	vec3 DiffuseLight;
	vec4 InputColor;


	
	//OutputColor = mix(texture(Texture0, TextureCoordinates), texture(Texture1, TextureCoordinates), MixTextures.r);
	InputColor = texture(Texture0, TextureCoordinates);

	LightDirection = -DiffuseLightDirection;	//Normal must face into the light, rather than WITH the light to be lit up.
	//DiffuseLightPercentage = max(dot(VertexNormal, LightDirection), 0.0);	//Percentage is based on angle between the direction of light and the vertex's normal. 
	DiffuseLightPercentage = dot(VertexNormal, LightDirection);	//Percentage is based on angle between the direction of light and the vertex's normal. 
	DiffuseLight = clamp((DiffuseLightColor * InputColor.xyz) * DiffuseLightPercentage, 0.0, 1.0);	//Apply only the percentage of the diffuse color. Saturate clamps output between 0.0 and 1.0.

	CameraDirection = normalize(CameraPosition - WorldSpacePosition);	//Create a normal that points in the direction from the pixel to the camera.

	if (DiffuseLightPercentage == 0.0f) 
	{
		SpecularColor  = vec3(0.0f, 0.0f, 0.0f);
	}
	else
	{
		SpecularColor = PhongSpecular(LightDirection, DiffuseLightColor, VertexNormal, CameraDirection, 0.3f);
	}

	OutputColor = RGBAColor * (AmbientLightColor * InputColor) + vec4(DiffuseLight, 1.0f) + vec4(SpecularColor, 1.0f);
	//OutputColor  = vec4(1.0f, 1.0f, 1.0f, 1.0f);
	
};

Actually, it is throwing a “GL_INVALID_OPERATION” error when it attempts to compile (not as written but when I remove the magic line of code).

message: GL_INVALID_OPERATION error generated. Object is not a program or shader object.
type: ERROR
id: 1282
severity: HIGH

Once I realized that it is actually throwing errors I was able to use the errors.

Interestingly enough, this problem was not directly related to the lines of code in question. The problem was that WorldSpacePosition is a vec4 in the vertex shader output. It was getting truncated to a vec3 on the input of this shader and making it go crazy.

Anyways, got it solved.

That line causes the previous calculation of that variable’s value to be discarded. In turn, it results in the variables used by that calculation becoming unused, which will cause any calculations of those variables’ values to be discarded, and so on. In summary, any calculation or variable which doesn’t affect the final result will be removed.

A call to glCompileShader() should be followed by a call to glGetShader() for GL_COMPILE_STATUS. If the result is GL_FALSE, call glGetShaderInfoLog() to retrieve the log messages.

Similarly, a call to glLinkProgram() should be followed by a call to glGetProgram() for GL_LINK_STATUS. If the result is GL_FALSE, call glGetProgramInfoLog() to retrieve the log messages.

Note that neither glCompileShader() nor glLinkProgram() will report an error (via glGetError()) because of errors in GLSL code.

glCompilerShader() will generate GL_INVALID_OPERATION if its argument isn’t a shader, while glLinkProgram() will generate GL_INVALID_OPERATION if its argument isn’t a program. glUseProgram() will generate an error if the program is invalid or isn’t consistent with the current state. Draw calls may generate errors for the same reasons.

Thanks.

I am calling glGetShaderiv() and glGetProgramiv() but the way I have the error handling set up I had to actually step through the program in the debugger and get the error because it was not outputting it anywhere. Probably need to work on my error handler a bit.

After I stepped through and pulled the actual error message from the debugger that was when I figured out it was the WorldSpacePosition tuple causing the problem. Once I knew where the problem was at solving it was easy.

I probably need to mod my error handler to write the messages out to a text file where I can easily read them.

I got my mind in “GLSL debug” mode where I’m used to not being able to step through the code and so I just modify it and run it one line at a time. For some reason it didn’t occur to me to step through the code and see if it was throwing an error outside of the shader. I had the error handling code there, but it outputs to the Output window and the main error didn’t have any output. It noted the error and then promptly ignored it rather than writing it anywhere. Guess it’s been a long day.

i speak vietnamese , its change vietnamese

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.