PDA

View Full Version : Reading back pixels (texture) using FBO



satya
04-13-2009, 01:05 AM
Hi,

whats the fastest way to read back the pixel information from a framebuffer? PBO is the only way? all i need to read back is the color information. for now i have the following code, but i see a huge drop in the fps with this

if i remove the call to glReadPixels, my fps just shoots back up (i get around 60fps and if i make a call to glReadPixels, it drops down to around 30 fps)

widht = 1024 and height = 800

_glGenFramebuffersEXT(1, &_bufferInfo.fb_id);
_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _bufferInfo.fb_id);
/*init color texture*/
glGenTextures(1, &_bufferInfo.fb_col_txr_id);
glBindTexture(GL_TEXTURE_2D, _bufferInfo.fb_col_txr_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

/*init depth texture*/
glGenTextures(1, &_bufferInfo.fb_dpth_txr_id);
glBindTexture(GL_TEXTURE_2D, _bufferInfo.fb_dpth_txr_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);

glBindTexture(GL_TEXTURE_2D, 0);

// Associate the depth and color rendering textures to the FBO
_glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, _bufferInfo.fb_col_txr_id, 0);
_glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, _bufferInfo.fb_dpth_txr_id, 0);

_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _bufferInfo.fb_id);

// code to render
// i render around 2 million triangles using VertexArray and VBO (i get around 60fps for this render)

// read back the pixels
glReadBuffer((GLenum)GL_COLOR_ATTACHMENT0_EXT);

glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)_bufferInfo.pixels);

//process the read pixels

Please help

TIA
satya

yooyo
04-13-2009, 04:44 AM
Use GL_BGRA instead of GL_RGBA in glReadPixels call.

dletozeun
04-13-2009, 06:12 AM
In addition, you may use pbo to benefit from asynchronous read/write and your glReadPixels calls won't be blocking anymore.

rexguo
04-13-2009, 09:57 PM
Check out this article for a good write-up on using PBOs
including how to use it with glReadPixels:

http://www.mathematik.uni-dortmund.de/~goeddeke/gpgpu/tutorial3.html