来自FBO的glReadPixels因多重采样而失败

时间:2009-04-19 13:39:43

标签: opengl fbo multisampling

我有一个带有颜色和深度附件的FBO对象,我使用glReadPixels()进行渲染然后阅读,我正在尝试添加多重采样支持。
而不是glRenderbufferStorage()我正在为颜色附件和深度附件调用glRenderbufferStorageMultisampleEXT()。帧缓冲区对象似乎已成功创建并报告为完成 渲染后我试图用glReadPixels()读取它。当样本数为0时,即多重采样禁用它完美地工作,我得到我想要的图像。当我将样本数量设置为其他内容时,比如说4,帧缓冲区仍然构建好,但glReadPixels()失败且INVALID_OPERATION

任何人都知道这里可能出现什么问题?

编辑:glReadPixels的代码:

glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ptr);

其中ptr指向宽度为* height uints的数组。

2 个答案:

答案 0 :(得分:24)

我认为你不能用glReadPixels()读取多重采样的FBO。您需要从多重采样FBO blit到普通FBO,绑定普通FBO,然后从普通FBO读取像素。

这样的事情:

// Bind the multisampled FBO for reading
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, my_multisample_fbo);
// Bind the normal FBO for drawing
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, my_fbo);
// Blit the multisampled FBO to the normal FBO
glBlitFramebufferEXT(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
//Bind the normal FBO for reading
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, my_fbo);
// Read the pixels!
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

答案 1 :(得分:1)

您无法直接使用glReadPixels读取多重采样缓冲区,因为它会引发GL_INVALID_OPERATION错误。您需要向另一个表面进行blit,以便GPU可以进行下采样。您可以对后备缓冲器进行blit,但是存在“像素所有者发货测试”的问题。最好再做一个FBO。让我们假设你做了另一个FBO,现在你想要blit。这需要GL_EXT_framebuffer_blit。通常,当您的驱动程序支持GL_EXT_framebuffer_multisample时,它还支持GL_EXT_framebuffer_blit,例如nVidia Geforce 8系列。

 //Bind the MS FBO
 glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, multisample_fboID);
 //Bind the standard FBO
 glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fboID);
 //Let's say I want to copy the entire surface
 //Let's say I only want to copy the color buffer only
 //Let's say I don't need the GPU to do filtering since both surfaces have the same dimension
 glBlitFramebufferEXT(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
 //--------------------
 //Bind the standard FBO for reading
 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
 glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, pixels);

来源:GL EXT framebuffer multisample