View Full Version : A fast way to read data from the framebuffer

09-15-2004, 06:22 PM
I found a fast way to read data from the framebuffer. It's faster than read data using glReadPixels directly.
First we draw the geometry into the buffer. Then a texture is binded. At last using the following code to read the data from the texture:

void ReadTexture(GLenum target, GLuint texture, float* pImage, GLenum format = GL_RGB,
GLint level = 0, char *argstring = NULL)
int w, h;
int prevTexBind;
glGetIntegerv(target, &prevTexBind);

glBindTexture(target, texture);
glGetTexLevelParameteriv(target, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameteriv(target, 0, GL_TEXTURE_HEIGHT, &h);
glGetTexImage(target, level, format, GL_FLOAT, pImage);

std::string s = (NULL == argstring) ? format2string(format) : argstring;

glBindTexture(target, prevTexBind);
}I don't know why it's faster than glReadPixels, but I tested it using the following codes.

for(int i = 0; i < 256; i++)

for(int i = 0; i < 256; i++)
}Who can explain the reasons?
Thank you.

09-15-2004, 11:55 PM
I have not tested or benchmarked it, but the same approach has been suggested (http://lists.apple.com/archives/mac-opengl/2004/May/msg00043.html) by John Stauffer, Eng Mgr of Apple's GL team. So it must be fast on some hardware.

09-16-2004, 12:37 AM
IIRC readpixels stops any opengl processing until it's finished.

Did you try the asynchronous readpixels with PBO?
You'll need the GL_EXT_pixel_buffer_object extension though.