Hello,
I am trying to visualize my tessellations by using a lookup-table inside fragment shader which is indexed using a gl_PrimitiveID like this:
precision mediump float;
layout(location = 0) out vec4 color;
// Color Look-up table of 8 entries
vec3 color_lut[8] = vec3[]( vec3( 0.0, 0.0, 0.0 ),
vec3( 0.5, 0.5, 0.5 ),
vec3( 1.0, 0.5, 0.5 ),
vec3( 0.5, 1.0, 0.5 ),
vec3( 0.5, 0.5, 1.0 ),
vec3( 0.5, 1.0, 1.0 ),
vec3( 1.0, 0.5, 1.0 ),
vec3( 1.0, 1.0, 0.5 ) );
void main() {
int prim_color_index = gl_PrimitiveID % 8;
color = vec4(color_lut[prim_color_index], 1.0f);
}
But I am not seeing the result as expected and only see a single color over my original untessellated triangle patch. I then checked if gl_PrimitiveID varies for each tessellated triangle and that wasn’t the case. I am getting a 0 for all of them. I was under the impression that gl_PrimitiveID would be unique for each tessellated triangle. I then checked the OpenGL Wiki and it says that gl_PrimitiveID is supposed to be unique for each primitive: Built-in Variable (GLSL) - OpenGL Wiki
gl_PrimitiveID : This value is the index of the current primitive being rendered by this drawing command. This includes any Tessellation applied to the mesh, so each individual primitive will have a unique index. However, if a Geometry Shader is active, then the gl_PrimitiveID is exactly and only what the GS provided as output. Normally, gl_PrimitiveID is guaranteed to be unique, so if two FS invocations have the same primitive ID, they come from the same primitive. But if a GS is active and outputs non-unique values, then different fragment shader invocations for different primitives will get the same value. If the GS did not output a value for gl_PrimitiveID, then the fragment shader gets an undefined value.
So it looks like my GPU isn’t following the OpenGL spec well?