PDA

View Full Version : Transform feedback code bug, help please



sam_thedancer
06-29-2013, 04:39 AM
Greetings:
I seem to have hit a wall with my toy should-be-simple first TF program. Really appreciate if someone can help find the bug. What I want to do is draw two points (which are actually the same) and output their world coords (should be same, of course) to the TF buffer. That's it, just to see TF working.

The plan is to do two passes in the same drawing routine. The first pass outputs to the TF buffer, while the second pass draws. Relevant part of the main, VS and FS are below.

Bug: As you can see from the FS code, the way I test if the TF buffer is written correctly is by fetching the corresponding "texel" and checking its distance with what I calculate by hand to be correct (to avoid testing float equality with 0, if the distance is less than 0.001 I specify a particular color). The first point which presumably should be from texelFetch(TFBuffer, 0) comes out correct but the second point (identical to the first) from texelFetch(TFBuffer,1) does not.

Any suggestions as to what I am doing wrong would be highly appreciated.
Thanks,
Sam

Main:

void init(void)
{
...
glGenTransformFeedbacks(1, &TFId);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, TFId);
glGenBuffers(1, &TFBuffer);
glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, TFBuffer);
glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, 1024, NULL, GL_DYNAMIC_COPY);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, TFBuffer);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);

glTransformFeedbackVaryings(programId, 1, varyings, GL_INTERLEAVED_ATTRIBS);

glLinkProgram(programId);
glUseProgram(programId);

glGenTextures(1, &TFTex);
glBindTexture(GL_TEXTURE_BUFFER, TFTex);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGB32F, TFBuffer);
...
}

void draw(void)
{
...
// TF pass
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, TFId);
glEnable(GL_RASTERIZER_DISCARD);
glBeginTransformFeedback(GL_POINTS);

some MV transforms

glDrawArrays(GL_POINTS, 0, 2); // Two identical points!

glEndTransformFeedback();
glDisable(GL_RASTERIZER_DISCARD);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);

// Drawing pass.
same MV transforms

glDrawArrays(GL_POINTS, 0, 2); // Same two points.
...
}

VS:

...
out vec4 center; // Varying

void main(void)
{
...
center = modelViewMat * coords;
gl_Position = projMat * modelViewMat * coords;
...
}

FS:

...
uniform samplerBuffer TFBuffer;
...
void main(void)
{
v0 = texelFetch(TFBuffer, 0).xyz;
v1 = texelFetch(TFBuffer, 1).xyz;
d = distance(v0, vec3(10.0f, 0.0f, -15.0f) );
if ( d < 0.001f) colorsOut = vec4(0.0f, 0.0f, 1.0f, 1.0f);
...
}

GClements
06-29-2013, 11:34 AM
This:


glTexBuffer(GL_TEXTURE_BUFFER, GL_RGB32F, TFBuffer);

assumes each entry is 3 floats, while this:


out vec4 center; // Varying

writes four floats per entry.

The result is that all elements after the first are misaligned. The buffer contains [x0,y0,z0,w0,x1,y1,z1,w1,...] which glTexBuffer() reads as [(x0,y0,z0),(w0,x1,y1),(z1,w1,?,?),...]. So the first texelFetch() returns the expected value but the rest are garbage.

Either change center to vec3 or change the texture format to GL_RGBA32F.

sam_thedancer
06-30-2013, 12:48 AM
GClements:
Problem solved, can't thank you enough...