Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// PBO init
- f->glGenBuffers(2, _pboIDs); // Create two PBOs
- for (int i = 0; i < 2; i++)
- {
- f->glBindBuffer(GL_PIXEL_PACK_BUFFER, _pboIDs[i]);
- f->glBufferData(GL_PIXEL_PACK_BUFFER, _renderResolution.width() * _renderResolution.height() * 4, nullptr, GL_STREAM_READ);
- }
- f->glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
- /// part of render function
- _renderControl->beginFrame();
- _renderControl->polishItems();
- _renderControl->sync();
- _renderControl->render();
- _renderControl->endFrame();
- QOpenGLFramebufferObject::bindDefault();
- unsigned int fbo = 1;
- unsigned int texture = 0;
- _context->functions()->glGenFramebuffers(1, &fbo);
- _context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
- if (_textureIndex == 0)
- {
- _context->functions()->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture1, 0);
- texture = _texture1;
- }
- else
- {
- _context->functions()->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture2, 0);
- texture = _texture2;
- }
- QElapsedTimer timer;
- timer.start();
- // Ping-pong index
- static int index = 0;
- int nextIndex = (index + 1) % 2;
- // Read pixels asynchronously into the current PBO
- _context->functions()->glBindBuffer(GL_PIXEL_PACK_BUFFER, _pboIDs[index]);
- _context->functions()->glReadPixels(0, 0, _renderResolution.width(), _renderResolution.height(), GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
- GLsync sync = _context->extraFunctions()->glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
- GLenum result = GL_UNSIGNALED;
- while (result != GL_ALREADY_SIGNALED && result != GL_CONDITION_SATISFIED)
- {
- result = _context->extraFunctions()->glClientWaitSync(sync, GL_SYNC_FLUSH_COMMANDS_BIT, 1000000); // Wait up to 1ms
- }
- _context->extraFunctions()->glDeleteSync(sync);
- // Map the previous PBO to access its data
- _context->functions()->glBindBuffer(GL_PIXEL_PACK_BUFFER, _pboIDs[nextIndex]);
- GLvoid *ptr = _context->extraFunctions()->glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, _renderResolution.width() * _renderResolution.height() * 4, GL_MAP_READ_BIT);
- if (ptr)
- {
- qDebug() << "frame ready";
- // Unmap the buffer after processing
- _context->extraFunctions()->glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
- }
- else
- {
- qDebug() << "frame not ready";
- // Data is not ready yet; skip this frame
- }
- GLenum error = glGetError();
- if (error != GL_NO_ERROR)
- {
- qDebug() << "OpenGL Error:" << error;
- }
- _context->functions()->glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
- index = nextIndex; // Swap indices for next frame
Advertisement
Add Comment
Please, Sign In to add comment