View Full Version : How to read Pixel data from the MultiSampled framebuffer ?

08-01-2011, 12:13 AM
Presently i am working on iphone app that includes OpenGl es 1.X and trying to read the pixel data from the frame Buffer in order to capture the screen in IOS. GlreadPixels command work fine when using the following code to setup frame buffer :-

// Create a depth buffer that has the same size as the color buffer.
glGenRenderbuffersOES(1, &m_depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT24_OES, width, height);

// Create the framebuffer object.

glGenFramebuffersOES(1, &framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
GL_RENDERBUFFER_OES, m_colorRenderbuffer);
GL_RENDERBUFFER_OES, m_depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_colorRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);

but when i use depth buffer and colorbuffer for Multi-Sampling glreadpixels() dont capture any pixel data as with earlier code ....for multi-sampling i use following code :-

glGenFramebuffersOES(1, &sampleFramebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, sampleFramebuffer);

//GLuint sampleColorRenderbuffer;
glGenRenderbuffersOES(1, &sampleColorRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, sampleColorRenderbuffer);
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUF FER_OES, 4, GL_RGBA8_OES, width, height);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, sampleColorRenderbuffer);

//glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUF FER_OES, 4, GL_RGBA8_OES, width, height);
//glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, sampleColorRenderbuffer);

GLuint sampleDepthRenderbuffer;
glGenRenderbuffersOES(1, &sampleDepthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, sampleDepthRenderbuffer);
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUF FER_OES, 4, GL_DEPTH_COMPONENT24_OES, width, height);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, sampleDepthRenderbuffer);
//glBindRenderbufferOES(GL_RENDERBUFFER_OES,sampleCo lorRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_colorRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, sampleFramebuffer);

i use the follwing code to read pixel data :-

CGRect screenBounds = [[UIScreen mainScreen] bounds];

int backingWidth = screenBounds.size.width;
int backingHeight =screenBounds.size.height;

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OE S, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OE S, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
NSInteger myDataLength = backingWidth * backingHeight * 4;

buffer= (GLuint *) malloc(myDataLength);
glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

Any idea how to capture correct pixel data with multi-Sampling technique....or Am i doing something wrong. Please guide me in right direction. Thanks

08-01-2011, 04:19 AM
You cannot read pixel data from a multisampled framebuffer, you need to blit it to a single-sampled framebuffer first.

08-01-2011, 05:22 AM
You cannot read pixel data from a multisampled framebuffer, you need to blit it to a single-sampled framebuffer first.

Thanks for Replying...hmm k following blitting code is right or wrong ?

glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, sampleFramebuffer);
glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, framebuffer);

08-07-2011, 10:01 PM
Hey all done.... got another issue regarding the performance while using glReadpixels() for reading the frame buffer ....the performance decreases unexpectedly.... without using glReadPixels() the FPS is around 30 and with glreadPixels the fps is 2-3 . is it normal or m lacking something ?

01-06-2012, 08:30 PM
Using glReadPixels is always expensive, how expensive depends on the platform. But most platforms need to stall rendering and wait for rendering to finish, with a possible resolve to another buffer, then an un-cached buffer generally needs to be read then the format needs to be converted. You should avoid glReadPixels at all cost for anything that you want to perform well. If you really need to use it, you might be able try various pixel format combinations, you can also try reading the target at the latest possible point in your frame.