diff options
author | Adrian Kummerlaender | 2018-12-16 15:16:21 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2018-12-16 15:16:21 +0100 |
commit | 52cbbff72bd5516b5f325d1c88e4fcd8ca67f989 (patch) | |
tree | 2dcc47ca89aebe3e5fca68d7e0ef484af3085452 /src | |
parent | 2eb7f9925315989735b4fb746cbd7bda6c9bd5bb (diff) | |
download | compustream-52cbbff72bd5516b5f325d1c88e4fcd8ca67f989.tar compustream-52cbbff72bd5516b5f325d1c88e4fcd8ca67f989.tar.gz compustream-52cbbff72bd5516b5f325d1c88e4fcd8ca67f989.tar.bz2 compustream-52cbbff72bd5516b5f325d1c88e4fcd8ca67f989.tar.lz compustream-52cbbff72bd5516b5f325d1c88e4fcd8ca67f989.tar.xz compustream-52cbbff72bd5516b5f325d1c88e4fcd8ca67f989.tar.zst compustream-52cbbff72bd5516b5f325d1c88e4fcd8ca67f989.zip |
Regeneralize ComputeShader wrapper
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cc | 42 | ||||
-rw-r--r-- | src/shader/wrap/compute_shader.cc | 6 | ||||
-rw-r--r-- | 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<GraphicShader> scene_shader; - std::unique_ptr<LatticeCellBuffer> lattice_buffer_a; - std::unique_ptr<LatticeCellBuffer> lattice_buffer_b; - std::unique_ptr<FluidCellBuffer> fluid_buffer; + std::unique_ptr<LatticeCellBuffer> lattice_a; + std::unique_ptr<LatticeCellBuffer> lattice_b; + std::unique_ptr<FluidCellBuffer> fluid; std::unique_ptr<ComputeShader> collide_shader; std::unique_ptr<ComputeShader> stream_shader; @@ -74,16 +74,16 @@ int renderWindow() { scene_shader = std::make_unique<GraphicShader>( VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE); - lattice_buffer_a = std::make_unique<LatticeCellBuffer>(nX, nY); - lattice_buffer_b = std::make_unique<LatticeCellBuffer>(nX, nY); - fluid_buffer = std::make_unique< FluidCellBuffer>(nX, nY); + lattice_a = std::make_unique<LatticeCellBuffer>(nX, nY); + lattice_b = std::make_unique<LatticeCellBuffer>(nX, nY); + fluid = std::make_unique< FluidCellBuffer>(nX, nY); collide_shader = std::make_unique<ComputeShader>(COLLIDE_SHADER_CODE); stream_shader = std::make_unique<ComputeShader>(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<GLuint>& 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 <string> +#include <vector> #include <GL/glew.h> @@ -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<GLuint>& buffers) const; + void dispatch(GLuint nX, GLuint nY) const; }; |