I am experiencing an extremely odd situation with the introduction of the tesselation control and evaluation shaders in my OpenGL rendering pipeline.
I defined an instanced indexed mesh whose rendering is carried out by the vertex, geometry and fragment shaders. Everything is rendered correctly. I could have animated the mesh in the VS but I wanted to exercise the GS to get more acquainted with it and it worked out.
I now introduced the Tesselation shader and the rendering simply does not happen: I do not see anything on the screen.
What I checked:
- I changed my rendering call to
glDrawElementsInstanced(GL_PATCHES, indexCount, GL_UNSIGNED_INT, (GLvoid *)0, instances);, - I preponed the invocation to the rendering call with
glPatchParameteri(GL_PATCH_VERTICES, GLint(this->patchSize));, - I constrainted
patchSize(3) to not exceed the value ofGL_MAX_PATCH_VERTICES(128 on my machine), - I checked the shader compilation and linking results (they were ok).
Till yesterday, I compiled and executed the shader employing OpenGL 4.1 and the compilation and linking were ok, still I could not see anything. Today, I switched to OpenGL 4.5 and the linking phase resulted into a weird linking error:
Link info
---------
error: "TEInstanceID" not declared as an output from the previous stage
When I employ the Tesselation shader without the GS, I can see the geometry (I could simply remove the GS altogether); however, I want to know how to make TS and GS cooperate. I have been checking the issue for hours but I could not understand what is wrong with my code. I will share the code of my shaders, hoping someone can direct me to the solution. I designed the tesselation shaders to be passthrough so they do very little.
VS:
#version 450 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 textureCoordinates;
out vec2 VTextureCoordinates;
flat out int VInstanceID;
void main() {
VInstanceID = gl_InstanceID;
VTextureCoordinates = textureCoordinates;
gl_Position = vec4(position, 1.0);
};
TCS:
#version 450 core
layout(vertices = 3) out;
in vec2 VTextureCoordinates[];
flat in int VInstanceID[];
out vec2 TCTextureCoordinates[];
patch out int TCInstanceID;
void main() {
if (gl_InvocationID==0){
gl_TessLevelInner[0] = 1.0;
gl_TessLevelOuter[0] = 1.0;
gl_TessLevelOuter[1] = 1.0;
gl_TessLevelOuter[2] = 1.0;
}
/* Pass through positions and data. */
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
TCTextureCoordinates[gl_InvocationID] = VTextureCoordinates[gl_InvocationID];
TCInstanceID = VInstanceID[0];
};
TES:
#version 450 core
layout(triangles, equal_spacing, ccw) in;
in vec2 TCTextureCoordinates[];
patch in int TCInstanceID;
out vec2 TETextureCoordinates;
flat out int TEInstanceID;
void main() {
// Interpolate the vertex positions using gl_TessCoord
gl_Position = gl_TessCoord.x * gl_in[0].gl_Position +
gl_TessCoord.y * gl_in[1].gl_Position +
gl_TessCoord.z * gl_in[2].gl_Position;
TETextureCoordinates = TCTextureCoordinates[0] * gl_TessCoord.x +
TCTextureCoordinates[1] * gl_TessCoord.y +
TCTextureCoordinates[2] * gl_TessCoord.z;
TEInstanceID = TCInstanceID;
};
GS:
#version 450 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
in vec2 TETextureCoordinates[];
flat in int TEInstanceID[];
out vec2 GTextureCoordinates;
flat out int GInstanceID;
uniform mat4 eye;
uniform mat4 model;
uniform float turning = 0.0;
uniform float width = 1.0;
uniform float height = 1.0;
float degreesToRadians(float degrees) {
return degrees * (3.14159265359 / 180.0);
}
void main() {
float a[] = float[](
degreesToRadians(0.0),
degreesToRadians(180.0) * turning,
degreesToRadians(180.0)
);
int i = TEInstanceID[0];
float cosTheta = cos(a[i]);
float sinTheta = sin(a[i]);
vec4 position = gl_in[0].gl_Position;
GTextureCoordinates = TETextureCoordinates[0];
GInstanceID = TEInstanceID[0];
gl_Position = eye * (/*model **/ vec4(position.x * cosTheta * width, position.y * sinTheta * width, position.z * height, 1.0));
EmitVertex();
position = gl_in[1].gl_Position;
GTextureCoordinates = TETextureCoordinates[1];
GInstanceID = TEInstanceID[1];
gl_Position = eye * (/*model **/ vec4(position.x * cosTheta * width, position.y * sinTheta * width, position.z * height, 1.0));
EmitVertex();
position = gl_in[2].gl_Position;
GTextureCoordinates = TETextureCoordinates[2];
GInstanceID = TEInstanceID[2];
gl_Position = eye * (/*model **/ vec4(position.x * cosTheta * width, position.y * sinTheta * width, position.z * height, 1.0));
EmitVertex();
EndPrimitive();
};
FS:
#version 450 core
out vec4 fRgbaColor;
uniform vec4 color;
flat in int GInstanceID;
in vec2 GTextureCoordinates;
void main() {
int textureIndex = GInstanceID * 2;
fRgbaColor = color;
};