diff options
author | Andreas Grois <andi@grois.info> | 2018-03-17 19:35:55 +0100 |
---|---|---|
committer | Andreas Grois <andi@grois.info> | 2018-03-17 19:35:55 +0100 |
commit | 873409ffdc8f172fb9fe6c46b6ddbad9239cf02d (patch) | |
tree | 7a7ede3966758aaacaa24e9177c1fb42483ca9a1 | |
parent | e1e7d9fed39cb8fc281925960c71f7dc197e6568 (diff) |
Fix memory allocation for state buffer. How could that ever work?
-rw-r--r-- | BuddhaTest/Shaders/BuddhaCompute.glsl | 3 | ||||
-rw-r--r-- | BuddhaTest/src/BuddhaTest.cpp | 10 |
2 files changed, 11 insertions, 2 deletions
diff --git a/BuddhaTest/Shaders/BuddhaCompute.glsl b/BuddhaTest/Shaders/BuddhaCompute.glsl index 61bbb6d..5998d6e 100644 --- a/BuddhaTest/Shaders/BuddhaCompute.glsl +++ b/BuddhaTest/Shaders/BuddhaCompute.glsl @@ -15,6 +15,7 @@ layout(std430, binding=4) restrict buffer renderedDataBlue restrict uint counts_SSBOBlue[]; }; +layout(packed) struct individualData { uint phase; @@ -23,7 +24,7 @@ struct individualData vec2 lastPosition; }; -layout(std430, binding=5) restrict buffer statusBuffer +layout(packed, binding=5) restrict buffer statusBuffer { restrict individualData state[]; }; diff --git a/BuddhaTest/src/BuddhaTest.cpp b/BuddhaTest/src/BuddhaTest.cpp index 95bd22f..20bd1ac 100644 --- a/BuddhaTest/src/BuddhaTest.cpp +++ b/BuddhaTest/src/BuddhaTest.cpp @@ -124,11 +124,19 @@ int main(int argc, char * argv[]) } glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); + //the state buffer is special. While making each entry 8 bytes large and using std430 layout, the data is never read back, + //so we can get away with being more lenient and allowing the compiler to choose layout without much extra work. + //We need to query the size for allocation though. + GLuint stateBufferIndex = glGetProgramResourceIndex(ComputeShader,GL_SHADER_STORAGE_BLOCK,"statusBuffer"); + GLint requiredStateBufferSizePerWorker; + GLenum t = GL_BUFFER_DATA_SIZE; + glGetProgramResourceiv(ComputeShader,GL_SHADER_STORAGE_BLOCK,stateBufferIndex,1,&t,1,nullptr,&requiredStateBufferSizePerWorker); + const uint32_t workersPerFrame = settings.globalWorkGroupSizeX*settings.globalWorkGroupSizeY*settings.globalWorkGroupSizeZ*settings.localWorkgroupSizeX*settings.localWorkgroupSizeY*settings.localWorkgroupSizeZ; GLuint stateBuffer; glGenBuffers(1,&stateBuffer); glBindBuffer(GL_SHADER_STORAGE_BUFFER,stateBuffer); - glBufferData(GL_SHADER_STORAGE_BUFFER, 4*(7*workersPerFrame),nullptr,GL_DYNAMIC_COPY); + glBufferData(GL_SHADER_STORAGE_BUFFER, requiredStateBufferSizePerWorker*workersPerFrame,nullptr,GL_DYNAMIC_COPY); glClearBufferData(GL_SHADER_STORAGE_BUFFER,GL_R8,GL_RED,GL_UNSIGNED_INT,nullptr); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, stateBuffer); |