From 34052b51e00c939a35294d7085cadb5111484dd3 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 26 May 2018 13:20:47 +0200 Subject: Separate headers into compilation units --- CMakeLists.txt | 13 ++++- shell.nix | 2 +- src/buffer/frame/texture_framebuffer.cc | 51 ++++++++++++++++++ src/buffer/frame/texture_framebuffer.h | 54 +++++-------------- src/buffer/vertex/particle_vertex_buffer.cc | 33 ++++++++++++ src/buffer/vertex/particle_vertex_buffer.h | 35 +++--------- src/buffer/vertex/texture_display_vertex_buffer.cc | 46 ++++++++++++++++ src/buffer/vertex/texture_display_vertex_buffer.h | 48 +++-------------- src/glfw/guard.cc | 16 ++++++ src/glfw/guard.h | 15 ++---- src/glfw/window.cc | 24 +++++++++ src/glfw/window.h | 63 ++++++++++------------ src/main.cc | 23 ++++---- src/shader/util.cc | 47 ++++++++++++++++ src/shader/util.h | 43 ++------------- src/shader/wrap/compute_shader.cc | 40 ++++++++++++++ src/shader/wrap/compute_shader.h | 45 +++++----------- src/shader/wrap/graphic_shader.cc | 45 ++++++++++++++++ src/shader/wrap/graphic_shader.h | 51 ++++++------------ src/timer.cc | 16 ++++++ src/timer.h | 12 +++++ src/util.h | 13 ----- 22 files changed, 441 insertions(+), 294 deletions(-) create mode 100644 src/buffer/frame/texture_framebuffer.cc create mode 100644 src/buffer/vertex/particle_vertex_buffer.cc create mode 100644 src/buffer/vertex/texture_display_vertex_buffer.cc create mode 100644 src/glfw/guard.cc create mode 100644 src/glfw/window.cc create mode 100644 src/shader/util.cc create mode 100644 src/shader/wrap/compute_shader.cc create mode 100644 src/shader/wrap/graphic_shader.cc create mode 100644 src/timer.cc create mode 100644 src/timer.h delete mode 100644 src/util.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6415420..9268050 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(shader) +project(computicle) set( CMAKE_CXX_FLAGS @@ -13,11 +13,20 @@ include_directories( add_executable( computicle src/main.cc + src/timer.cc + src/glfw/guard.cc + src/glfw/window.cc + src/shader/util.cc + src/shader/wrap/compute_shader.cc + src/shader/wrap/graphic_shader.cc + src/buffer/frame/texture_framebuffer.cc + src/buffer/vertex/particle_vertex_buffer.cc + src/buffer/vertex/texture_display_vertex_buffer.cc ) target_link_libraries( computicle GL - GLEW glfw + GLEW ) diff --git a/shell.nix b/shell.nix index 2a16377..c57c8a5 100644 --- a/shell.nix +++ b/shell.nix @@ -5,8 +5,8 @@ stdenv.mkDerivation rec { env = buildEnv { name = name; paths = buildInputs; }; buildInputs = [ git cmake gcc gdb cgdb - glew glfw3 + glew glm ]; } diff --git a/src/buffer/frame/texture_framebuffer.cc b/src/buffer/frame/texture_framebuffer.cc new file mode 100644 index 0000000..b585985 --- /dev/null +++ b/src/buffer/frame/texture_framebuffer.cc @@ -0,0 +1,51 @@ +#include "texture_framebuffer.h" + +TextureFramebuffer::Guard::Guard(GLuint id): + _id(id) { + glBindFramebuffer(GL_FRAMEBUFFER, _id); +} + +TextureFramebuffer::Guard::~Guard() { + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +TextureFramebuffer::Guard TextureFramebuffer::use() const { + return Guard(_id); +} + +TextureFramebuffer::TextureFramebuffer(std::size_t width, std::size_t height) { + glGenFramebuffers(1, &_id); + + auto guard = use(); + + glGenTextures(1, &_texture); + glBindTexture(GL_TEXTURE_2D, _texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0); + + if ( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE ) { + _good = true; + } +} + +TextureFramebuffer::~TextureFramebuffer() { + glDeleteFramebuffers(1, &_id); +} + +bool TextureFramebuffer::isGood() const { + return _good; +} + +void TextureFramebuffer::resize(std::size_t width, std::size_t height) const { + auto guard = use(); + + glViewport(0, 0, width, height); + glBindTexture(GL_TEXTURE_2D, _texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0); +} + +GLuint TextureFramebuffer::getTexture() const { + return _texture; +} diff --git a/src/buffer/frame/texture_framebuffer.h b/src/buffer/frame/texture_framebuffer.h index 31153ff..f6b20e6 100644 --- a/src/buffer/frame/texture_framebuffer.h +++ b/src/buffer/frame/texture_framebuffer.h @@ -1,5 +1,9 @@ #pragma once +#include + +#include + class TextureFramebuffer { private: GLuint _id; @@ -11,52 +15,18 @@ public: struct Guard { const GLuint _id; - Guard(GLuint id): _id(id) { - glBindFramebuffer(GL_FRAMEBUFFER, _id); - } - ~Guard() { - glBindFramebuffer(GL_FRAMEBUFFER, 0); - } + Guard(GLuint id); + ~Guard(); }; - Guard use() const { - return Guard(_id); - } - - TextureFramebuffer(std::size_t width, std::size_t height) { - glGenFramebuffers(1, &_id); - - auto guard = use(); - - glGenTextures(1, &_texture); - glBindTexture(GL_TEXTURE_2D, _texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0); - - if ( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE ) { - _good = true; - } - } - - ~TextureFramebuffer() { - glDeleteFramebuffers(1, &_id); - } + Guard use() const; - bool isGood() const { - return _good; - } + TextureFramebuffer(std::size_t width, std::size_t height); + ~TextureFramebuffer(); - void resize(std::size_t width, std::size_t height) const { - auto guard = use(); + bool isGood() const; - glViewport(0, 0, width, height); - glBindTexture(GL_TEXTURE_2D, _texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0); - } + void resize(std::size_t width, std::size_t height) const; - GLuint getTexture() const { - return _texture; - } + GLuint getTexture() const; }; diff --git a/src/buffer/vertex/particle_vertex_buffer.cc b/src/buffer/vertex/particle_vertex_buffer.cc new file mode 100644 index 0000000..fc61cc5 --- /dev/null +++ b/src/buffer/vertex/particle_vertex_buffer.cc @@ -0,0 +1,33 @@ +#include "particle_vertex_buffer.h" + +ParticleVertexBuffer::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::~ParticleVertexBuffer() { + glDeleteBuffers(1, &_buffer); + glDeleteVertexArrays(1, &_array); +} + +GLuint ParticleVertexBuffer::getBuffer() const { + return _buffer; +} + +void ParticleVertexBuffer::draw() const { + glBindVertexArray(_array); + glDrawArrays(GL_POINTS, 0, 3*_data.size()); +} diff --git a/src/buffer/vertex/particle_vertex_buffer.h b/src/buffer/vertex/particle_vertex_buffer.h index 25855a2..8fb96a4 100644 --- a/src/buffer/vertex/particle_vertex_buffer.h +++ b/src/buffer/vertex/particle_vertex_buffer.h @@ -2,6 +2,8 @@ #include +#include + class ParticleVertexBuffer { private: std::vector _data; @@ -10,35 +12,10 @@ private: 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, &_array); - } + ParticleVertexBuffer(std::vector&& data); + ~ParticleVertexBuffer(); - void draw() const { - glBindVertexArray(_array); - glDrawArrays(GL_POINTS, 0, 3*_data.size()); - } + GLuint getBuffer() const; - GLuint getBuffer() const { - return _buffer; - } + void draw() const; }; diff --git a/src/buffer/vertex/texture_display_vertex_buffer.cc b/src/buffer/vertex/texture_display_vertex_buffer.cc new file mode 100644 index 0000000..005ea76 --- /dev/null +++ b/src/buffer/vertex/texture_display_vertex_buffer.cc @@ -0,0 +1,46 @@ +#include "texture_display_vertex_buffer.h" + +TextureDisplayVertexBuffer::TextureDisplayVertexBuffer(): + _data{ + -1.f, 1.f, 0.f, 1.f, + -1.f, -1.f, 0.f, 0.f, + 1.f, -1.f, 1.f, 0.f, + + -1.f, 1.f, 0.f, 1.f, + 1.f, -1.f, 1.f, 0.f, + 1.f, 1.f, 1.f, 1.f + } { + 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, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer( + 1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat))); +} + +TextureDisplayVertexBuffer::~TextureDisplayVertexBuffer() { + glDeleteBuffers(1, &_buffer); + glDeleteVertexArrays(1, &_array); +} + +GLuint TextureDisplayVertexBuffer::getBuffer() const { + return _buffer; +} + +void TextureDisplayVertexBuffer::draw(const std::vector& textures) const { + glBindVertexArray(_array); + glBindTextures(textures[0], textures.size(), textures.data()); + glDrawArrays(GL_TRIANGLES, 0, 6); +} diff --git a/src/buffer/vertex/texture_display_vertex_buffer.h b/src/buffer/vertex/texture_display_vertex_buffer.h index 6d4eec2..6febb1e 100644 --- a/src/buffer/vertex/texture_display_vertex_buffer.h +++ b/src/buffer/vertex/texture_display_vertex_buffer.h @@ -2,6 +2,8 @@ #include +#include + class TextureDisplayVertexBuffer { private: const std::vector _data; @@ -10,48 +12,10 @@ private: GLuint _buffer; public: - TextureDisplayVertexBuffer(): - _data{ - -1.f, 1.f, 0.f, 1.f, - -1.f, -1.f, 0.f, 0.f, - 1.f, -1.f, 1.f, 0.f, - - -1.f, 1.f, 0.f, 1.f, - 1.f, -1.f, 1.f, 0.f, - 1.f, 1.f, 1.f, 1.f - } { - 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, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer( - 1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat))); - } - - ~TextureDisplayVertexBuffer() { - glDeleteBuffers(1, &_buffer); - glDeleteVertexArrays(1, &_array); - } + TextureDisplayVertexBuffer(); + ~TextureDisplayVertexBuffer(); - void draw(const std::vector& textures) const { - glBindVertexArray(_array); - glBindTextures(textures[0], textures.size(), textures.data()); - glDrawArrays(GL_TRIANGLES, 0, 6); - } + GLuint getBuffer() const; - GLuint getBuffer() const { - return _buffer; - } + void draw(const std::vector& textures) const; }; diff --git a/src/glfw/guard.cc b/src/glfw/guard.cc new file mode 100644 index 0000000..5ba853f --- /dev/null +++ b/src/glfw/guard.cc @@ -0,0 +1,16 @@ +#include "guard.h" + +#include +#include + +GlfwGuard::GlfwGuard() { + _good = glfwInit(); +} + +GlfwGuard::~GlfwGuard() { + glfwTerminate(); +} + +bool GlfwGuard::isGood() const { + return _good; +} diff --git a/src/glfw/guard.h b/src/glfw/guard.h index 37d3ffb..f68954d 100644 --- a/src/glfw/guard.h +++ b/src/glfw/guard.h @@ -1,20 +1,11 @@ #pragma once -#include -#include - class GlfwGuard { private: bool _good = false; public: - GlfwGuard() { - _good = glfwInit(); - } - ~GlfwGuard() { - glfwTerminate(); - } + GlfwGuard(); + ~GlfwGuard(); - bool isGood() const { - return _good; - } + bool isGood() const; }; diff --git a/src/glfw/window.cc b/src/glfw/window.cc new file mode 100644 index 0000000..17fc36c --- /dev/null +++ b/src/glfw/window.cc @@ -0,0 +1,24 @@ +#include "window.h" + +Window::Window(const std::string& title): + _handle(glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL)) { + if ( _handle != nullptr ) { + glfwMakeContextCurrent(_handle); + if ( glewInit() == GLEW_OK ) { + _good = true; + } + glfwMakeContextCurrent(nullptr); + } +} + +bool Window::isGood() const { + return _good; +} + +int Window::getWidth() const { + return _width; +} + +int Window::getHeight() const { + return _height; +} diff --git a/src/glfw/window.h b/src/glfw/window.h index 794dd5f..b858e9a 100644 --- a/src/glfw/window.h +++ b/src/glfw/window.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -12,51 +14,40 @@ private: GLFWwindow* const _handle; public: - Window(const std::string& title): - _handle(glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL)) { - if ( _handle != nullptr ) { - glfwMakeContextCurrent(_handle); - if ( glewInit() == GLEW_OK ) { - _good = true; - } - glfwMakeContextCurrent(nullptr); - } - } + Window(const std::string& title); - bool isGood() const { - return _good; - } - - int getWidth() const { - return _width; - } + bool isGood() const; - int getHeight() const { - return _height; - } + int getWidth() const; + int getHeight() const; template - void init(F f) { - glfwMakeContextCurrent(_handle); - f(); - glfwMakeContextCurrent(nullptr); - } + void init(F f); template - void render(F loop) { - glfwMakeContextCurrent(_handle); + void render(F loop); +}; - while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE ) != GLFW_PRESS && - glfwWindowShouldClose(_handle) == 0 ) { - glfwGetWindowSize(_handle, &_width, &_height); +template +void Window::init(F f) { + glfwMakeContextCurrent(_handle); + f(); + glfwMakeContextCurrent(nullptr); +} - loop(); +template +void Window::render(F loop) { + glfwMakeContextCurrent(_handle); - glfwSwapBuffers(_handle); - glfwPollEvents(); - } + while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE ) != GLFW_PRESS && + glfwWindowShouldClose(_handle) == 0 ) { + glfwGetWindowSize(_handle, &_width, &_height); - glfwMakeContextCurrent(nullptr); + loop(); + + glfwSwapBuffers(_handle); + glfwPollEvents(); } -}; + glfwMakeContextCurrent(nullptr); +} diff --git a/src/main.cc b/src/main.cc index 9f32bc3..cd8668e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,9 +1,10 @@ -#include -#include - #include #include #include +#include + +#include +#include #include "glfw/guard.h" #include "glfw/window.h" @@ -23,7 +24,7 @@ #include "shader/code/display_vertex.glsl" #include "shader/code/display_fragment.glsl" -#include "util.h" +#include "timer.h" const unsigned int particle_count = 2500; const unsigned int max_ups = 100; @@ -86,7 +87,7 @@ int main() { Window window("computicle"); if ( !window.isGood() ) { - std::cerr << "Failed to open window." << std::endl; + std::cerr << "Failed to open GLFW window." << std::endl; return -1; } @@ -135,8 +136,8 @@ int main() { return -1; } - auto lastFrame = std::chrono::high_resolution_clock::now(); - auto lastRotate = std::chrono::high_resolution_clock::now(); + auto lastFrame = timer::now(); + auto lastRotate = timer::now(); bool justRotated = true; std::vector textures; @@ -158,21 +159,21 @@ int main() { } } - if ( util::millisecondsSince(lastFrame) >= 1000/max_ups ) { + if ( timer::millisecondsSince(lastFrame) >= 1000/max_ups ) { auto guard = compute_shader->use(); compute_shader->setUniform("world", world_width, world_height); compute_shader->dispatch(particle_count); - lastFrame = std::chrono::high_resolution_clock::now(); + lastFrame = timer::now(); } - if ( util::millisecondsSince(lastRotate) >= 1000/10 ) { + if ( timer::millisecondsSince(lastRotate) >= 1000/10 ) { std::rotate(textures.begin(), textures.end()-1, textures.end()); std::rotate(texture_framebuffers.begin(), texture_framebuffers.end()-1, texture_framebuffers.end()); justRotated = true; - lastRotate = std::chrono::high_resolution_clock::now(); + lastRotate = timer::now(); } { diff --git a/src/shader/util.cc b/src/shader/util.cc new file mode 100644 index 0000000..07a1128 --- /dev/null +++ b/src/shader/util.cc @@ -0,0 +1,47 @@ +#include "util.h" + +#include +#include + +namespace util { + +GLint getUniform(GLuint program, const std::string& name) { + const GLint uniform = glGetUniformLocation(program, name.c_str()); + if ( uniform == -1 ) { + std::cerr << "Could not bind uniform " << name << std::endl; + } + return uniform; +} + +GLint compileShader(const std::string& source, GLenum type) { + GLint shader = glCreateShader(type); + + if ( !shader ) { + std::cerr << "Cannot create a shader of type " << type << std::endl; + exit(-1); + } + + const char* source_data = source.c_str(); + const int source_length = source.size(); + + glShaderSource(shader, 1, &source_data, &source_length); + glCompileShader(shader); + + GLint compiled; + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + if ( !compiled ) { + std::cerr << "Cannot compile shader" << std::endl; + GLint maxLength = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); + std::vector errorLog(maxLength); + glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]); + for( auto c : errorLog ) { + std::cerr << c; + } + std::cerr << std::endl; + } + + return shader; +} + +} diff --git a/src/shader/util.h b/src/shader/util.h index 7aec674..31224e9 100644 --- a/src/shader/util.h +++ b/src/shader/util.h @@ -1,46 +1,13 @@ #pragma once -#include +#include -namespace util { - -GLint getUniform(GLuint program, const std::string& name) { - const GLint uniform = glGetUniformLocation(program, name.c_str()); - if ( uniform == -1 ) { - std::cerr << "Could not bind uniform " << name << std::endl; - } - return uniform; -} - -GLint compileShader(const std::string& source, GLenum type) { - GLint shader = glCreateShader(type); +#include - if ( !shader ) { - std::cerr << "Cannot create a shader of type " << type << std::endl; - exit(-1); - } - - const char* source_data = source.c_str(); - const int source_length = source.size(); - - glShaderSource(shader, 1, &source_data, &source_length); - glCompileShader(shader); +namespace util { - GLint compiled; - glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); - if ( !compiled ) { - std::cerr << "Cannot compile shader" << std::endl; - GLint maxLength = 0; - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); - std::vector errorLog(maxLength); - glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]); - for( auto c : errorLog ) { - std::cerr << c; - } - std::cerr << std::endl; - } +GLint getUniform(GLuint program, const std::string& name); - return shader; -} +GLint compileShader(const std::string& source, GLenum type); } diff --git a/src/shader/wrap/compute_shader.cc b/src/shader/wrap/compute_shader.cc new file mode 100644 index 0000000..97ce52a --- /dev/null +++ b/src/shader/wrap/compute_shader.cc @@ -0,0 +1,40 @@ +#include "compute_shader.h" + +#include "shader/util.h" + +ComputeShader::Guard::Guard(GLuint id): + _id(id) { + glUseProgram(_id); +} + +ComputeShader::Guard::~Guard() { + glUseProgram(0); +} + +ComputeShader::Guard ComputeShader::use() const { + return Guard(_id); +} + +ComputeShader::ComputeShader(const std::string& src): + _id(glCreateProgram()) { + glAttachShader(_id, util::compileShader(src, GL_COMPUTE_SHADER)); + glLinkProgram(_id); +} + +ComputeShader::~ComputeShader() { + glDeleteProgram(_id); +} + +GLuint ComputeShader::setUniform(const std::string& name, float x, float y) const { + GLuint id = util::getUniform(_id, name); + glUniform2f(id, x, y); + return id; +} + +void ComputeShader::workOn(GLuint buffer) const { + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffer); +} + +void ComputeShader::dispatch(std::size_t dimX) const { + glDispatchCompute(dimX, 1, 1); +} diff --git a/src/shader/wrap/compute_shader.h b/src/shader/wrap/compute_shader.h index 9f5c5cb..e9e88ca 100644 --- a/src/shader/wrap/compute_shader.h +++ b/src/shader/wrap/compute_shader.h @@ -1,6 +1,8 @@ #pragma once -#include "util.h" +#include + +#include class ComputeShader { private: @@ -10,38 +12,17 @@ public: struct Guard { const GLuint _id; - Guard(GLuint id): _id(id) { - glUseProgram(_id); - } - ~Guard() { - glUseProgram(0); - } + Guard(GLuint id); + ~Guard(); }; - Guard use() const { - return Guard(_id); - } + Guard use() const; - ComputeShader(const std::string& src): - _id(glCreateProgram()) { - glAttachShader(_id, util::compileShader(src, GL_COMPUTE_SHADER)); - glLinkProgram(_id); - }; - ~ComputeShader() { - glDeleteProgram(_id); - } - - GLuint setUniform(const std::string& name, float x, float y) const { - GLuint id = util::getUniform(_id, name); - glUniform2f(id, x, y); - return id; - } - - void workOn(GLuint buffer) const { - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffer); - } - - void dispatch(std::size_t dimX) const { - glDispatchCompute(dimX, 1, 1); - } + ComputeShader(const std::string& src); + ~ComputeShader(); + + GLuint setUniform(const std::string& name, float x, float y) const; + + void workOn(GLuint buffer) const; + void dispatch(std::size_t dimX) const; }; diff --git a/src/shader/wrap/graphic_shader.cc b/src/shader/wrap/graphic_shader.cc new file mode 100644 index 0000000..0ed37ff --- /dev/null +++ b/src/shader/wrap/graphic_shader.cc @@ -0,0 +1,45 @@ +#include "graphic_shader.h" + +#include "shader/util.h" + +GraphicShader::Guard::Guard(GLuint id): + _id(id) { + glUseProgram(_id); +} + +GraphicShader::Guard::~Guard() { + glUseProgram(0); +} + +GraphicShader::Guard GraphicShader::use() const { + return Guard(_id); +} + +GraphicShader::GraphicShader(const std::string& vertex, const std::string fragment): + _id(glCreateProgram()) { + glAttachShader(_id, util::compileShader(vertex, GL_VERTEX_SHADER)); + glAttachShader(_id, util::compileShader(fragment, GL_FRAGMENT_SHADER)); + glLinkProgram(_id); +} + +GraphicShader::~GraphicShader() { + glDeleteProgram(_id); +} + +GLuint GraphicShader::setUniform(const std::string& name, int value) const { + GLuint id = util::getUniform(_id, name); + glUniform1i(id, value); + return id; +} + +GLuint GraphicShader::setUniform(const std::string& name, const std::vector& v) const { + GLuint id = util::getUniform(_id, name); + glUniform1iv(id, v.size(), reinterpret_cast(v.data())); + return id; +} + +GLuint GraphicShader::setUniform(const std::string& name, glm::mat4& M) const { + GLuint id = util::getUniform(_id, name); + glUniformMatrix4fv(id, 1, GL_FALSE, &M[0][0]); + return id; +} diff --git a/src/shader/wrap/graphic_shader.h b/src/shader/wrap/graphic_shader.h index 03249d5..82f7e7a 100644 --- a/src/shader/wrap/graphic_shader.h +++ b/src/shader/wrap/graphic_shader.h @@ -1,5 +1,11 @@ #pragma once +#include +#include + +#include +#include + #include "shader/util.h" class GraphicShader { @@ -10,43 +16,16 @@ public: struct Guard { const GLuint _id; - Guard(GLuint id): _id(id) { - glUseProgram(_id); - } - ~Guard() { - glUseProgram(0); - } + Guard(GLuint id); + ~Guard(); }; - Guard use() const { - return Guard(_id); - } + Guard use() const; - GraphicShader(const std::string& vertex, const std::string fragment): - _id(glCreateProgram()) { - glAttachShader(_id, util::compileShader(vertex, GL_VERTEX_SHADER)); - glAttachShader(_id, util::compileShader(fragment, GL_FRAGMENT_SHADER)); - glLinkProgram(_id); - }; - ~GraphicShader() { - glDeleteProgram(_id); - } - - GLuint setUniform(const std::string& name, int value) const { - GLuint id = util::getUniform(_id, name); - glUniform1i(id, value); - return id; - } - - GLuint setUniform(const std::string& name, const std::vector& v) const { - GLuint id = util::getUniform(_id, name); - glUniform1iv(id, v.size(), reinterpret_cast(v.data())); - return id; - } - - GLuint setUniform(const std::string& name, glm::mat4& M) const { - GLuint id = util::getUniform(_id, name); - glUniformMatrix4fv(id, 1, GL_FALSE, &M[0][0]); - return id; - } + GraphicShader(const std::string& vertex, const std::string fragment); + ~GraphicShader(); + + GLuint setUniform(const std::string& name, int value) const; + GLuint setUniform(const std::string& name, const std::vector& v) const; + GLuint setUniform(const std::string& name, glm::mat4& M) const; }; diff --git a/src/timer.cc b/src/timer.cc new file mode 100644 index 0000000..c46017c --- /dev/null +++ b/src/timer.cc @@ -0,0 +1,16 @@ +#include "timer.h" + +namespace timer { + +std::chrono::time_point now() { + return std::chrono::high_resolution_clock::now(); +} + +double millisecondsSince( + std::chrono::time_point& pit) { + return std::chrono::duration_cast( + now() - pit + ).count(); +} + +} diff --git a/src/timer.h b/src/timer.h new file mode 100644 index 0000000..aed1b61 --- /dev/null +++ b/src/timer.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace timer { + +std::chrono::time_point now(); + +double millisecondsSince( + std::chrono::time_point& pit); + +} diff --git a/src/util.h b/src/util.h deleted file mode 100644 index 1863563..0000000 --- a/src/util.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - -namespace util { - -double millisecondsSince(std::chrono::time_point& pit) { - return std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - pit - ).count(); -} - -} -- cgit v1.2.3