1

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
cloudraven
  • 2,484
  • 1
  • 24
  • 49
  • 2
    From the code parts yoy have posted so far, it is impossible to say what triggers the error. However, what you are doing is completely _undefined behavior_: you are trying to read from the mapped memory after you unmapped it. The pointer you got throught the mapping will become **invalid** as soon as you unmap it. You might be lucky in that it may seem to work for you, but it might just _crash_ or show any conceivable odd behavior. – derhass Apr 21 '14 at 13:28
  • Yeah, I was wondering if that was reasonable. the first thing I am doing is to copy it to somewhere else, so maybe thats why I have been lucky. I will do the memcopy before the unmap. Thanks – cloudraven Apr 21 '14 at 22:28

0 Answers0