I am confused because I have working code that gives me INVALID_OPERATION with glreadpixels My guess is that it is causing / will cause performance issues, and even if it does not I wonder if it may result in unreliable code.
I am doing offscreen rendering to an FBO and then reading to system memory from a PBO.
glReadPixels returns INVALID_OPERATION but every frame but the first one. Somehow it gives no error the first time but it complains from then on. I have to stress that despite that error the code works fine.
My code looks like this:
Initialization (called only at startup):
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);
Offscreen rendering (called once per frame):
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