From 01b47b49462a3b6ade6e8819edad9f2455fe1143 Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Sat, 10 Mar 2018 13:31:14 +0100 Subject: Make local work group size tweakable. (Not yet exposed to user) --- BuddhaTest/Shaders/BuddhaCompute.glsl | 5 +++-- BuddhaTest/include/Helpers.h | 2 +- BuddhaTest/src/BuddhaTest.cpp | 17 ++++++++++++----- BuddhaTest/src/Helpers.cpp | 9 +++++++-- 4 files changed, 23 insertions(+), 10 deletions(-) (limited to 'BuddhaTest') diff --git a/BuddhaTest/Shaders/BuddhaCompute.glsl b/BuddhaTest/Shaders/BuddhaCompute.glsl index 7a363ec..263e4c0 100644 --- a/BuddhaTest/Shaders/BuddhaCompute.glsl +++ b/BuddhaTest/Shaders/BuddhaCompute.glsl @@ -1,4 +1,6 @@ -#version 430 +//commented out, added by c-code that loads this shader. +//#version 430 +//layout (local_size_x = 1024) in; //to be safe, we limit our local work group size to 1024. That's the minimum a GL 4.3 capable driver must support. layout(std430, binding=2) buffer renderedData { @@ -187,7 +189,6 @@ void drawOrbit(vec2 offset) } } -layout (local_size_x = 1024) in; //to be safe, we limit our local work group size to 1024. That's the minimum a GL 4.3 capable driver must support. void main() { //we need to know how many total work groups are running this iteration diff --git a/BuddhaTest/include/Helpers.h b/BuddhaTest/include/Helpers.h index ad14305..8c897d5 100644 --- a/BuddhaTest/include/Helpers.h +++ b/BuddhaTest/include/Helpers.h @@ -7,7 +7,7 @@ namespace Helpers { GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path); - GLuint LoadComputeShader(const char * compute_file_path); + GLuint LoadComputeShader(const char * compute_file_path, unsigned int localSizeX, unsigned int localSizeY, unsigned int localSizeZ); void WriteOutputPNG(const std::vector& data, unsigned int width, unsigned int height); diff --git a/BuddhaTest/src/BuddhaTest.cpp b/BuddhaTest/src/BuddhaTest.cpp index d1b6a66..f8b9074 100644 --- a/BuddhaTest/src/BuddhaTest.cpp +++ b/BuddhaTest/src/BuddhaTest.cpp @@ -14,6 +14,9 @@ int main() unsigned int bufferWidth = 1600; unsigned int bufferHeight = 900; + unsigned int windowWidth = 1600; + unsigned int windowHeight = 900; + unsigned int orbitLengthRed = 10; unsigned int orbitLengthGreen = 100; unsigned int orbitLengthBlue = 1000; @@ -31,7 +34,7 @@ int main() glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); /* Create a windowed mode window and its OpenGL context */ - window = glfwCreateWindow(1600, 900, "Buddhabrot", NULL, NULL); + window = glfwCreateWindow(windowWidth, windowHeight, "Buddhabrot", NULL, NULL); if (!window) { std::cerr << "Failed to create OpenGL 4.3 core context. We do not support compatibility contexts." << std::endl; @@ -77,7 +80,7 @@ int main() // Create and compile our GLSL program from the shaders GLuint VertexAndFragmentShaders = Helpers::LoadShaders("Shaders/BuddhaVertex.glsl", "Shaders/BuddhaFragment.glsl"); //Do the same for the compute shader: - GLuint ComputeShader = Helpers::LoadComputeShader("Shaders/BuddhaCompute.glsl"); + GLuint ComputeShader = Helpers::LoadComputeShader("Shaders/BuddhaCompute.glsl", 1024, 1, 1); uint32_t iterationCount{0}; glUseProgram(ComputeShader); @@ -103,7 +106,7 @@ int main() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(VertexAndFragmentShaders); - glEnableVertexAttribArray(0); + glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. @@ -113,12 +116,12 @@ int main() 0, // stride (void*)0 // array buffer offset ); - // Draw the triangle ! + // Draw the triangle strip! glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // Triangle strip with 4 vertices -> quad. glDisableVertexAttribArray(0); /* Swap front and back buffers */ - glfwSwapBuffers(window); + glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); @@ -133,6 +136,10 @@ int main() Helpers::WriteOutputPNG(readBackBuffer,bufferWidth,bufferHeight); } + //a bit of cleanup + glDeleteBuffers(1,&vertexbuffer); + glDeleteBuffers(1,&drawBuffer); + glfwTerminate(); return 0; } diff --git a/BuddhaTest/src/Helpers.cpp b/BuddhaTest/src/Helpers.cpp index 3512d0f..b092dc2 100644 --- a/BuddhaTest/src/Helpers.cpp +++ b/BuddhaTest/src/Helpers.cpp @@ -98,7 +98,7 @@ namespace Helpers return ProgramID; } - GLuint LoadComputeShader(const char* compute_file_path) + GLuint LoadComputeShader(const char* compute_file_path, unsigned int localSizeX, unsigned int localSizeY, unsigned int localSizeZ) { GLuint ComputeShaderID = glCreateShader(GL_COMPUTE_SHADER); // Read the compute shader @@ -107,6 +107,11 @@ namespace Helpers std::ifstream ComputeShaderCodeStream(compute_file_path, std::ios::in); if (ComputeShaderCodeStream.is_open()) { std::stringstream sstr; + sstr << "#version 430" << + std::endl << + "layout (local_size_x = " << localSizeX << + ", local_size_y = " << localSizeY << + ", local_size_z = " << localSizeZ << ") in;" << std::endl; sstr << ComputeShaderCodeStream.rdbuf(); ComputeShaderCode = sstr.str(); ComputeShaderCodeStream.close(); @@ -158,7 +163,7 @@ namespace Helpers rows[i] = pngData.data()+3*width*i; } - uint32_t maxValue{UINT32_C(0)}; + uint32_t maxValue{UINT32_C(1)}; for(unsigned int i = 0; i < data.size();++i) { maxValue = std::max(maxValue,data[i]); -- cgit v1.2.3