From 52cbbff72bd5516b5f325d1c88e4fcd8ca67f989 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 16 Dec 2018 15:16:21 +0100 Subject: Regeneralize ComputeShader wrapper --- src/main.cc | 42 +++++++++++++++++++++------------------ src/shader/wrap/compute_shader.cc | 6 ++---- src/shader/wrap/compute_shader.h | 4 +++- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/main.cc b/src/main.cc index 1feb649..5ac18e1 100644 --- a/src/main.cc +++ b/src/main.cc @@ -63,9 +63,9 @@ int renderWindow() { std::unique_ptr scene_shader; - std::unique_ptr lattice_buffer_a; - std::unique_ptr lattice_buffer_b; - std::unique_ptr fluid_buffer; + std::unique_ptr lattice_a; + std::unique_ptr lattice_b; + std::unique_ptr fluid; std::unique_ptr collide_shader; std::unique_ptr stream_shader; @@ -74,16 +74,16 @@ int renderWindow() { scene_shader = std::make_unique( VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE); - lattice_buffer_a = std::make_unique(nX, nY); - lattice_buffer_b = std::make_unique(nX, nY); - fluid_buffer = std::make_unique< FluidCellBuffer>(nX, nY); + lattice_a = std::make_unique(nX, nY); + lattice_b = std::make_unique(nX, nY); + fluid = std::make_unique< FluidCellBuffer>(nX, nY); collide_shader = std::make_unique(COLLIDE_SHADER_CODE); stream_shader = std::make_unique(STREAM_SHADER_CODE); }); if ( !collide_shader->isGood() || !stream_shader->isGood() ) { - std::cerr << "Compute shader error. Check vector field definition." << std::endl; + std::cerr << "Compute shader error." << std::endl; return -1; } @@ -94,6 +94,9 @@ int renderWindow() { auto pause_key = window.getKeyWatcher(GLFW_KEY_SPACE); + auto tick_buffers = { lattice_a->getBuffer(), lattice_b->getBuffer(), fluid->getBuffer() }; + auto tock_buffers = { lattice_b->getBuffer(), lattice_a->getBuffer(), fluid->getBuffer() }; + window.render([&]() { if ( pause_key.wasClicked() ) { update_lattice = !update_lattice; @@ -103,21 +106,22 @@ int renderWindow() { || window.getHeight() != window_height ) { window_width = window.getWidth(); window_height = window.getHeight(); + glViewport(0, 0, window_width, window_height); - world_height = getWorldHeight(window_width, window_height, world_width); + world_height = getWorldHeight(window_width, window_height, world_width); MVP = getMVP(world_width, world_height); } if ( update_lattice ) { - if ( timer::millisecondsSince(last_frame) >= 1000/50 ) { + if ( timer::millisecondsSince(last_frame) >= 1000/25 ) { if ( tick ) { - collide_shader->workOn(lattice_buffer_a->getBuffer(), lattice_buffer_b->getBuffer(), fluid_buffer->getBuffer()); - stream_shader->workOn(lattice_buffer_a->getBuffer(), lattice_buffer_b->getBuffer(), fluid_buffer->getBuffer()); + collide_shader->workOn(tick_buffers); + stream_shader->workOn(tick_buffers); tick = false; } else { - collide_shader->workOn(lattice_buffer_b->getBuffer(), lattice_buffer_a->getBuffer(), fluid_buffer->getBuffer()); - stream_shader->workOn(lattice_buffer_b->getBuffer(), lattice_buffer_a->getBuffer(), fluid_buffer->getBuffer()); + collide_shader->workOn(tock_buffers); + stream_shader->workOn(tock_buffers); tick = true; } @@ -132,15 +136,15 @@ int renderWindow() { last_frame = timer::now(); } + } - { - auto sdrGuard = scene_shader->use(); + { + auto guard = scene_shader->use(); - scene_shader->setUniform("MVP", MVP); + scene_shader->setUniform("MVP", MVP); - glClear(GL_COLOR_BUFFER_BIT); - fluid_buffer->draw(); - } + glClear(GL_COLOR_BUFFER_BIT); + fluid->draw(); } }); diff --git a/src/shader/wrap/compute_shader.cc b/src/shader/wrap/compute_shader.cc index ab5d4c4..54f50f8 100644 --- a/src/shader/wrap/compute_shader.cc +++ b/src/shader/wrap/compute_shader.cc @@ -46,10 +46,8 @@ GLuint ComputeShader::setUniform(const std::string& name, unsigned int value) co return id; } -void ComputeShader::workOn(GLuint bufferA, GLuint bufferB, GLuint bufferC) const { - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, bufferA); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, bufferB); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, bufferC); +void ComputeShader::workOn(const std::vector& buffers) const { + glBindBuffersBase(GL_SHADER_STORAGE_BUFFER, 1, buffers.size(), buffers.data()); } void ComputeShader::dispatch(GLuint nX, GLuint nY) const { diff --git a/src/shader/wrap/compute_shader.h b/src/shader/wrap/compute_shader.h index 816b925..f2ab182 100644 --- a/src/shader/wrap/compute_shader.h +++ b/src/shader/wrap/compute_shader.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include @@ -28,6 +29,7 @@ public: GLuint setUniform(const std::string& name, float x, float y) const; GLuint setUniform(const std::string& name, unsigned int value) const; - void workOn(GLuint bufferA, GLuint bufferB, GLuint bufferC) const; + void workOn(const std::vector& buffers) const; + void dispatch(GLuint nX, GLuint nY) const; }; -- cgit v1.2.3