Guest User

Untitled

a guest
Nov 20th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. /// PBO init
  2.  
  3. f->glGenBuffers(2, _pboIDs); // Create two PBOs
  4.  
  5. for (int i = 0; i < 2; i++)
  6. {
  7. f->glBindBuffer(GL_PIXEL_PACK_BUFFER, _pboIDs[i]);
  8. f->glBufferData(GL_PIXEL_PACK_BUFFER, _renderResolution.width() * _renderResolution.height() * 4, nullptr, GL_STREAM_READ);
  9. }
  10.  
  11. f->glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  12.  
  13. /// part of render function
  14.  
  15. _renderControl->beginFrame();
  16. _renderControl->polishItems();
  17. _renderControl->sync();
  18. _renderControl->render();
  19. _renderControl->endFrame();
  20.  
  21. QOpenGLFramebufferObject::bindDefault();
  22.  
  23. unsigned int fbo = 1;
  24. unsigned int texture = 0;
  25.  
  26. _context->functions()->glGenFramebuffers(1, &fbo);
  27. _context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  28.  
  29. if (_textureIndex == 0)
  30. {
  31. _context->functions()->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture1, 0);
  32. texture = _texture1;
  33. }
  34. else
  35. {
  36. _context->functions()->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture2, 0);
  37. texture = _texture2;
  38. }
  39.  
  40. QElapsedTimer timer;
  41. timer.start();
  42.  
  43. // Ping-pong index
  44. static int index = 0;
  45. int nextIndex = (index + 1) % 2;
  46.  
  47. // Read pixels asynchronously into the current PBO
  48. _context->functions()->glBindBuffer(GL_PIXEL_PACK_BUFFER, _pboIDs[index]);
  49. _context->functions()->glReadPixels(0, 0, _renderResolution.width(), _renderResolution.height(), GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  50.  
  51. GLsync sync = _context->extraFunctions()->glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  52. GLenum result = GL_UNSIGNALED;
  53. while (result != GL_ALREADY_SIGNALED && result != GL_CONDITION_SATISFIED)
  54. {
  55. result = _context->extraFunctions()->glClientWaitSync(sync, GL_SYNC_FLUSH_COMMANDS_BIT, 1000000); // Wait up to 1ms
  56. }
  57. _context->extraFunctions()->glDeleteSync(sync);
  58.  
  59. // Map the previous PBO to access its data
  60. _context->functions()->glBindBuffer(GL_PIXEL_PACK_BUFFER, _pboIDs[nextIndex]);
  61. GLvoid *ptr = _context->extraFunctions()->glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, _renderResolution.width() * _renderResolution.height() * 4, GL_MAP_READ_BIT);
  62.  
  63. if (ptr)
  64. {
  65. qDebug() << "frame ready";
  66.  
  67. // Unmap the buffer after processing
  68. _context->extraFunctions()->glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
  69. }
  70. else
  71. {
  72. qDebug() << "frame not ready";
  73. // Data is not ready yet; skip this frame
  74. }
  75.  
  76.  
  77. GLenum error = glGetError();
  78.  
  79. if (error != GL_NO_ERROR)
  80. {
  81. qDebug() << "OpenGL Error:" << error;
  82. }
  83.  
  84. _context->functions()->glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  85. index = nextIndex; // Swap indices for next frame
Advertisement
Add Comment
Please, Sign In to add comment