glReadPixels return garbage on some cards

Hi,

call to glReadPixels return garbage on some cards. This is what i do
glGenFrameBuffer()
glBindFrameBuffer()
glGenTexture()
glBindTexture()
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glBindFramebuffer()
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
render my geometry
lReadBuffer((GLenum)GL_COLOR_ATTACHMENT0_EXT);
glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
when i loop over the returned pixels, i see some garbage values, some colors which i have not rendered at all

please help

TIA
satya

I even do not understand how this code would work on any card. Has the texture stuff something to do with rest of your framebuffer setup here?

First, I advise you to check the completeness of your framebuffer. You can read pixels only from a complete framebuffer.

Hi,

sorry about that, i didn’t post the entire code, in fact i do check for the completeness of the framebuffer, I generate 2 textures one for color and one for depth attachment and i do check the completeness using if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT)
and then only render to frame buffer and then read from that framebuffer using glReadPixels

TIA
satya

Ok I understand. try to call glFinish before reading pixels to be sure that all issued rendering commands are done. verify the packing alignment (see glPixelStore IIRC the default one is 4 (word-alignment) and usually 1 one used (byte alignment).

Are you rendering to a texture or a renderbuffer?

I did try calling glFinish with no luck, and yes, i am rendering to a texture, i checked the packing and unpacking alignment, its set to 4. i should also mention that the return of glReadPixels is not completely garbage, it looks some values in between only are junk (rather they are not matching, the color that i use to render)

TIA
satya

i checked the packing and unpacking alignment, its set to 4

And you really need a word row alignment in pixels data?

Perhaps more code and a screenshot would help to find out… :wink:

I notice you are using GL_BGR on read back.

glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, pixels);

Should you not be using GL_RGB or GL_RGBA since OpenGL is RGBA ordered internally?

This would explain the colour swapping.

Have you also set your pixel datastore to the viewport dimensions * bitsperpixel to capture the pixels being read?