Read whole buffer to get it proper asynchronous
authorLuke Lau <luke_lau@icloud.com>
Mon, 13 Apr 2020 23:23:40 +0000 (00:23 +0100)
committerLuke Lau <luke_lau@icloud.com>
Mon, 13 Apr 2020 23:23:40 +0000 (00:23 +0100)
clouds.cpp

index f0f3a957576d17da6fdf5b60c521174e060eaeec..ff6e59d8faca801d28a5638897a28e115ffb0b37 100644 (file)
@@ -63,7 +63,8 @@ void precalculateBillboardTextures() {
         float r = distance(vec2(i, j), vec2(16, 16)) / 16;
         float density = (float)d / NQ;
         data[i + j * 32] =
-            1 - fmin(1, (3 * density * (metaballField(r) / normalizationFactor)));
+            1 -
+            fmin(1, (3 * density * (metaballField(r) / normalizationFactor)));
       }
     }
 
@@ -168,12 +169,15 @@ const int shadeWidth = 256, shadeHeight = 256;
 #ifdef PBO
 const int numPbos = 64;
 GLuint pboBufs[numPbos];
+GLuint pboOffsets[numPbos]; // offsets into GL_PIXEL_PACK_BUFFER that
+                            // mapPixelRead should read from
 GLbyte sink[shadeWidth * shadeHeight * 4];
 
 void inline mapPixelRead(int pboBuf, int metaball) {
   glBindBuffer(GL_PIXEL_PACK_BUFFER, pboBufs[pboBuf]);
-      GLubyte *src = (GLubyte *)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, 4 * sizeof(GLubyte),
-          GL_MAP_READ_BIT);
+  GLubyte *src =
+      (GLubyte *)glMapBufferRange(GL_PIXEL_PACK_BUFFER, pboOffsets[pboBuf],
+                                  4 * sizeof(GLubyte), GL_MAP_READ_BIT);
   vec4 pixel = vec4(src[0], src[1], src[2], src[3]) / vec4(255.f);
   glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
 
@@ -263,11 +267,14 @@ void shadeClouds() {
 #else
 
     glBindBuffer(GL_PIXEL_PACK_BUFFER, pboBufs[pboIdx]);
-    glReadPixels(screenPos.x, screenPos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
-                 NULL);
-    // TODO: use this
-    /* glReadPixels(0, 0, shadeWidth, shadeHeight, GL_RGBA, GL_UNSIGNED_BYTE, */
+    // It would be nice if this worked. But it doesn't
+    // macOS driver does this synchronously
+    /* glReadPixels(screenPos.x, screenPos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
+     */
     /*              NULL); */
+    glReadPixels(0, 0, shadeWidth, shadeHeight, GL_RGBA, GL_UNSIGNED_BYTE,
+                 NULL);
+    pboOffsets[pboIdx] = screenPos.x * 4 + screenPos.y * shadeWidth * 4;
 
     int nextPbo = (pboIdx + 1) % numPbos;
     if (i >= numPbos - 1) {