From: Luke Lau Date: Mon, 13 Apr 2020 23:23:40 +0000 (+0100) Subject: Read whole buffer to get it proper asynchronous X-Git-Url: https://git.lukelau.me/?p=clouds.git;a=commitdiff_plain;h=60211392a44ab8b080cfc88035379e92a2fe6f39 Read whole buffer to get it proper asynchronous --- diff --git a/clouds.cpp b/clouds.cpp index f0f3a95..ff6e59d 100644 --- a/clouds.cpp +++ b/clouds.cpp @@ -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) {