Hi guys,
I need to use Transform Feedback in order to speed up a certain rendering process. I have a good amount of OpenGL experience and I think I understand how to use Transform Feedback. I’m just missing a detail here and looking for someone to review my code.
Not sure if this is really an advanced topic though. I have read all the other topics on transform feedback on this forum.
I set up a sample project to test certain things. I will later apply it to my (much more complex) scenario.
Here is my initialization section:
// setup shader, create program
...
// bind input variables
glBindAttribLocation(m_vertexExcludeProgram, 0, "in_Vertex");
glBindAttribLocation(m_vertexExcludeProgram, 3, "in_Color");
// specify the set of vertex attributes recorded in transform feedback mode
const char * outputVaryings[] = {"out_Position", out_Color"};
glTransformFeedbackVaryings(m_vertexExcludeProgram, 2, &outputVaryings[0], GL_INTERLEAVED_ATTRIBS);
// link
glLinkProgram(m_vertexExcludeProgram);
// gives me '2' -> correct
int numAttribs;
glGetProgramiv(m_vertexExcludeProgram, GL_ACTIVE_ATTRIBUTES, &numAttribs);
// I don't get the locations of the output variables. Is that normal?
GLint locationVertex = glGetAttribLocation(m_vertexExcludeProgram, "in_Vertex"); // 0
GLint locationColor = glGetAttribLocation(m_vertexExcludeProgram, "in_Color"); // 3
GLint locationPosition = glGetAttribLocation(m_vertexExcludeProgram, "out_Position"); // -1
GLint locationColorO = glGetAttribLocation(m_vertexExcludeProgram, "out_Color"); // -1
First thing I noticed is, that I cannot get the indices of the output variables.
After that i perform the transform feedback once:
glUseProgram(m_vertexExcludeProgram);
unsigned int numPrimitivesWritten = 0;
// bind current vertexbuffer for rendering
int numEntities = vbuffer->getSize() / vbuffer->getVertexFormat()->getStride();
vbuffer->apply();
// bind the transform feedback buffer glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfBufferHandle);
// render
glBeginTransformFeedback(GL_TRIANGLES);
// No rasterisation will be performed
glEnable(GL_RASTERIZER_DISCARD);
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, query);
glDrawArrays(GL_QUADS, 0, numEntities);
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
glDisable(GL_RASTERIZER_DISCARD);
glEndTransformFeedback();
// Query the number of primitives written in the transform buffer.
glGetQueryObjectuiv(query, GL_QUERY_RESULT, &numPrimitivesWritten);
glUseProgram(0);
This should be pretty standard. One thing is mysterious though. The original vertexbuffer contains 24 vertices, while numPrimitivesWritten tells me it recorded 8.
And here is my simple test vertexshader:
in vec4 in_Vertex;
in vec4 in_Color;
out vec4 out_Position;
out vec4 out_Color;
void transformFeedbackTest()
{
out_Position = vec4(in_Vertex.x + 1, in_Vertex.y + 1, in_Vertex.z, 1.0);
out_Color = vec4(0.0, in_Color.x, 0.0, 1.0);
}
All I want to see is that the mesh is offset by a bit and the color has changed. Instead, nothing changes. It seems that the shader is not being applied or OpenGL can’t write to my output variables and just skips the shader.
I hope someone is able to spot my mistake.
Thank you