PDA

View Full Version : Bind texture AND attach to framebuffer object



soconne
03-29-2009, 11:33 AM
Is this legal? I wrote a simple demo that binds a texture as GL_COLOR_ATTACHMENT0, and then immediately after binds it to the first texture unit. glGetError reports no errors.

What I do is render vertical lines across the screen one by one. The first line that is drawn has pixel values corresponding to each pixel's y position. Then each vertical line after simply copies the value from its left neighbor.



#version 130
#extension GL_EXT_gpu_shader4 : enable

out uint outColor;
uniform usampler2D tex;

void main()
{
ivec2 screenPos = ivec2(gl_FragCoord.xy);
if (screenPos.x == 0)
{
outColor = uint(screenPos.y);
}
else
{
outColor = texelFetch2D(tex, screenPos + ivec2(-1, 0), 0).x;
}
}




glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);
glBindTexture(GL_TEXTURE_2D, tex);
glUseProgram(program);

for (int x = 0; x < 16; x++) {
glBegin(GL_LINES);
glVertex2f(x + 0.375f, 0.0f + 0.375f);
glVertex2f(x + 0.375f, 16.0f + 0.375f);
glEnd();
}


And I get correct output. Every column in the texture is the same as the other ones. Simply the series: 0, 1, .. 15, 16 going down each column.

I read the specs on the EXT_framebuffer_object (http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt) extension and it lists a few instances where the values written to the framebuffer are unknown, but none of these instances specifically pertain to my situation.

I figured perhaps my method works since no pixel is ever read AND written to. For a given column in the texture, I write to that column and read from the previous column. Is this something I can count on across drivers and video cards?

dletozeun
03-29-2009, 03:56 PM
The gl spec clearly says that in in this kind of situation the output is undefined. Don't expect it to work on all hardwares. (See page 288 in the gl 3.0 spec).