1

我很困惑,因为我的工作代码给了我带有 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
4

0 回答 0