From a8feed9d7951c9a947562ed687c704851a31ea9b Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 21 May 2018 14:35:43 +0200 Subject: Fix VAO, VBO setup, compute shader coupling --- src/compute_shader.h | 6 +++ src/graphic_shader.h | 2 + src/main.cc | 113 ++++++++++++++++--------------------------- src/particle_vertex_buffer.h | 44 +++++++++++++++++ src/shader/compute.glsl | 6 +-- src/texture_buffer.h | 2 + src/util.h | 2 + 7 files changed, 102 insertions(+), 73 deletions(-) create mode 100644 src/particle_vertex_buffer.h diff --git a/src/compute_shader.h b/src/compute_shader.h index 298a454..a7e997a 100644 --- a/src/compute_shader.h +++ b/src/compute_shader.h @@ -1,3 +1,5 @@ +#pragma once + #include "util.h" class ComputeShader { @@ -35,6 +37,10 @@ public: return id; } + void workOn(GLuint buffer) { + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffer); + } + void dispatch(std::size_t dimX) { glDispatchCompute(dimX, 1, 1); } diff --git a/src/graphic_shader.h b/src/graphic_shader.h index 2dd8c41..0422ec9 100644 --- a/src/graphic_shader.h +++ b/src/graphic_shader.h @@ -1,3 +1,5 @@ +#pragma once + #include "util.h" class GraphicShader { diff --git a/src/main.cc b/src/main.cc index c6945fb..7b4415b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -4,8 +4,6 @@ #include #include -#include -#include #include #include #include @@ -13,6 +11,7 @@ #include "graphic_shader.h" #include "compute_shader.h" #include "texture_buffer.h" +#include "particle_vertex_buffer.h" #include "shader/vertex.glsl" #include "shader/fragment.glsl" @@ -24,7 +23,9 @@ int window_width = 800; int window_height = 600; float world_width, world_height; glm::mat4 MVP; -std::unique_ptr textureBuffer; + +std::unique_ptr textureBuffer; +std::unique_ptr particleBuffer; void updateMVP() { world_width = 20.f; @@ -101,48 +102,38 @@ int main() { updateMVP(); - textureBuffer = std::make_unique(window_width, window_height); - - { - auto guard = textureBuffer->use(); - - glClearColor(0.0f, 0.0f, 0.4f, 0.0f); - } - GraphicShader sceneShader(VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE); - sceneShader.setUniform("MVP", MVP); - - ComputeShader computeShader(COMPUTE_SHADER_CODE); - - auto vertex_buffer_data = makeInitialParticles(particle_count); + GraphicShader displayShader(R"( + #version 330 core + layout (location = 0) in vec2 screen_vertex; + layout (location = 1) in vec2 texture_vertex; + out vec2 TexCoords; - GLuint VertexArrayID; - GLuint VertexBufferID; + void main() { + gl_Position = vec4(screen_vertex, 0.0, 1.0); + TexCoords = texture_vertex; + } + )", R"( + #version 330 core + out vec4 FragColor; + in vec2 TexCoords; + uniform sampler2D screen_texture; - { - auto guard = textureBuffer->use(); + void main() { + FragColor = texture(screen_texture, TexCoords); + } + )"); - glGenVertexArrays(1, &VertexArrayID); - glBindVertexArray(VertexArrayID); - glEnableVertexAttribArray(0); + sceneShader.setUniform("MVP", MVP); + displayShader.setUniform("screen_texture", 0); - glGenBuffers(1, &VertexBufferID); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, VertexBufferID); + textureBuffer = std::make_unique(window_width, window_height); + particleBuffer = std::make_unique( + makeInitialParticles(particle_count)); - glBufferData( - GL_SHADER_STORAGE_BUFFER, - vertex_buffer_data.size() * sizeof(GLfloat), - vertex_buffer_data.data(), - GL_STATIC_DRAW - ); - } + ComputeShader computeShader(COMPUTE_SHADER_CODE); + computeShader.workOn(particleBuffer->getBuffer()); - GLuint QuadVertexArrayID; - glGenVertexArrays(1, &QuadVertexArrayID); - glBindVertexArray(QuadVertexArrayID); - GLuint QuadVertexBufferID; - glGenBuffers(1, &QuadVertexBufferID); - glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBufferID); const std::vector quad_buffer_data{ -1.f, 1.f, 0.f, 1.f, -1.f, -1.f, 0.f, 0.f, @@ -152,36 +143,26 @@ int main() { 1.f, -1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 1.f }; + + GLuint QuadVertexArrayID; + GLuint QuadVertexBufferID; + + glGenVertexArrays(1, &QuadVertexArrayID); + glGenBuffers(1, &QuadVertexBufferID); + + glBindVertexArray(QuadVertexArrayID); + glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBufferID); glBufferData( GL_ARRAY_BUFFER, quad_buffer_data.size() * sizeof(GLfloat), quad_buffer_data.data(), GL_STATIC_DRAW ); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0); glEnableVertexAttribArray(1); - - GraphicShader displayShader(R"( - #version 330 core - layout (location = 0) in vec2 screen_vertex; - layout (location = 1) in vec2 texture_vertex; - out vec2 TexCoords; - - void main() { - gl_Position = vec4(screen_vertex, 0.0, 1.0); - TexCoords = texture_vertex; - } - )", R"( - #version 330 core - out vec4 FragColor; - in vec2 TexCoords; - uniform sampler2D screen_texture; - - void main() { - FragColor = texture(screen_texture, TexCoords); - } - )"); - displayShader.setUniform("screen_texture", 0); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat))); auto lastFrame = std::chrono::high_resolution_clock::now(); @@ -204,18 +185,14 @@ int main() { sceneShader.setUniform("MVP", MVP); - glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); - glDrawArrays(GL_POINTS, 0, 3*particle_count); + particleBuffer->draw(); } { auto guard = displayShader.use(); + glBindVertexArray(QuadVertexArrayID); glBindTexture(GL_TEXTURE_2D, textureBuffer->getTexture()); - glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBufferID); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat))); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -227,10 +204,6 @@ int main() { while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 ); - glDeleteBuffers(1, &VertexBufferID); - glDisableVertexAttribArray(0); - glDeleteVertexArrays(1, &VertexArrayID); - glfwTerminate(); return 0; diff --git a/src/particle_vertex_buffer.h b/src/particle_vertex_buffer.h new file mode 100644 index 0000000..58daf38 --- /dev/null +++ b/src/particle_vertex_buffer.h @@ -0,0 +1,44 @@ +#pragma once + +#include + +class ParticleVertexBuffer { +private: + std::vector _data; + + GLuint _array; + GLuint _buffer; + +public: + ParticleVertexBuffer(std::vector&& data): + _data{ std::move(data) } { + glGenVertexArrays(1, &_array); + glGenBuffers(1, &_buffer); + + glBindVertexArray(_array); + glBindBuffer(GL_ARRAY_BUFFER, _buffer); + glBufferData( + GL_ARRAY_BUFFER, + _data.size() * sizeof(GLfloat), + _data.data(), + GL_STATIC_DRAW + ); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); + } + + ~ParticleVertexBuffer() { + glDeleteBuffers(1, &_buffer); + glDeleteVertexArrays(1, &_buffer); + } + + void draw() { + glBindVertexArray(_array); + glDrawArrays(GL_POINTS, 0, 3*_data.size()); + } + + GLuint getBuffer() const { + return _buffer; + } +}; diff --git a/src/shader/compute.glsl b/src/shader/compute.glsl index 58bf08b..e322a08 100644 --- a/src/shader/compute.glsl +++ b/src/shader/compute.glsl @@ -12,9 +12,9 @@ float rand(vec2 co){ bool insideWorld(vec2 v) { return v.x > -world.x/2. - && v.x < world.x/2. - && v.y > -world.y/2. - && v.y < world.y/2.; + && v.x < world.x/2. + && v.y > -world.y/2. + && v.y < world.y/2.; } void main() { diff --git a/src/texture_buffer.h b/src/texture_buffer.h index 851833c..d6cce53 100644 --- a/src/texture_buffer.h +++ b/src/texture_buffer.h @@ -1,3 +1,5 @@ +#pragma once + class TextureBuffer { private: GLuint _id; diff --git a/src/util.h b/src/util.h index ce8d012..7aec674 100644 --- a/src/util.h +++ b/src/util.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace util { GLint getUniform(GLuint program, const std::string& name) { -- cgit v1.2.3