Nowibananatzki
Nowibananatzki

Reputation: 786

how to use opengl transform feedback, I always get 0 in the buffers

I was trying to use transform feedback to retrieve data from vertex shader for debugging as my perspective matrix seemed to be miscalculated. I got nothing in the transform feedback buffer. Can anyone help me figure out which step I did wrong?

Following is snippet of my code of setting up transform feedback:

//vertex to be passed into shader
const float vertexPos[] =
{
     0.75f, 0.75f, 0.0f, 1.0f,
     0.75f, -0.75f, 0.0f, 1.0f,
     -0.75f, -0.75f, 0.0f, 1.0f
};

//set up transform feedback
const char *variableName = "retrieve";//the variable to be retreived in shader
float dateFromShader[16];//receive the data from shader
GLuint tf_bo;//transform feedback buffer id

glTransformFeedbackVaryings(shaderProgram, 1, &variableName, GL_INTERLEAVED_ATTRIBS);
glLinkProgram(shaderProgram);

glGenBuffers(1, &tf_bo);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, tf_bo);// Is this necessary to call glBindBuffer before glBindBufferBase?
     glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tf_bo);
     glBufferDate(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(float) * 16, NULL, GL_DYNAMIC_COPY);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);

//retrieve data from vertex shader
glEnable(GL_RASTERIZER_DICARD);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, tf_bo);
glBeginTransformFeedback(GL_TRIANGLES);
     glDrawArrays(GL_TRIANGLES, 0, 3);//
glEndTransformFeedback();

glBindBuffer(GL_ARRAY_BUFFER, tf_bo);
glGetBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * 16, dataFromShader);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable(GL_RASTERIZER_DISCARD);

and my vertex shader is like this:

#version 420

layout(location = 0) in vec4 position;


uniform mat4 perspectiveMatrix;
out mat4 retrieve;

void main()
{
gl_Position = perspectiveMatrix * position;

retrieve = perspectiveMatrix;
}

Upvotes: 7

Views: 3823

Answers (2)

Nasser
Nasser

Reputation: 65

 glBufferDate(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(float) * 16, NULL, GL_DYNAMIC_COPY);

Is this line not supposed to be:

 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, sizeof(float) * 16, NULL, GL_DYNAMIC_COPY);

Note the "glBufferData" not "glBufferDate". This will also support the fact that you are not getting any data in the buffer / 0 is being returned....

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 474236

glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tf_bo);

As stated in the documentation, glBindBufferBase requires that the buffer object have data storage (ie: that you have called glBufferData on it). glBindBufferBase is not a replacement for glBindBuffer; is a different kind of function that has a different use.

Next, you're capturing a matrix. The problem is that you're drawing three vertices, which means that you're going to capture three vertices. You only allocated enough space for one. That's going to cause undefined behavior.

Most importantly of all... what exactly do you think this will accomplish? You're capturing a uniform value you directly uploaded. Unless you believe that the implementation's version of glUniformMatrix is broken in some way, that's not very helpful in debugging anything.

Upvotes: 5

Related Questions