glReadPixels可以工作,但在使用PBO时会返回INVALID_OPERATION

时间:2014-04-21 06:48:19

标签: opengl fbo off-screen pbo

我很困惑,因为我有工作代码,它给了我带glreadpixels的INVALID_OPERATION 我的猜测是它导致/将导致性能问题,即使它不是,我不知道它是否会导致不可靠的代码。

我正在对FBO进行屏幕外渲染,然后从PBO读取系统内存。

glReadPixels返回INVALID_OPERATION,但是除了第一帧之外的每一帧。不知何故,它第一次没有出现任何错误,但从那时起就抱怨了。我必须强调,尽管有这个错误,但代码工作正常。

我的代码如下所示:

初始化(仅在启动时调用):

glGenFramebuffers(1, &fbo);

glBindFramebuffer(GL_FRAMEBUFFER, fbo);

//render buffer

glGenRenderbuffers(1, &render_buf);

glBindRenderbuffer(GL_RENDERBUFFER, render_buf);

glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB8, _width, _height);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, render_buf);

//depth buffer

glGenRenderbuffers(1, &depth_buf);

glBindRenderbuffer(GL_RENDERBUFFER, depth_buf);

glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, _width, _height);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buf);


glBindFramebuffer(GL_FRAMEBUFFER, fbo);


glBindFramebuffer(GL_FRAMEBUFFER, 0);

GLenum fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);


//Add the pixel buffer object
glGenBuffers(1, &pbo);

glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);

glBufferData(GL_PIXEL_PACK_BUFFER, _width* _height * 4, NULL, GL_DYNAMIC_READ);

glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glReadBuffer(GL_COLOR_ATTACHMENT0);

glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);

离屏渲染(每帧调用一次):

glReadPixels(0, 0, _width, _height, GL_BGR, GL_UNSIGNED_BYTE, 0); 

void* mappedRegion = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);

return (uint8_t*)mappedRegion; //I am only reading, not writing to this one

0 个答案:

没有答案