From f728e4c8d202de241673a13ce61570b6acb4bba7 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 25 May 2018 23:47:27 +0200 Subject: Restructure source directory --- src/shader/wrap/compute_shader.h | 47 ++++++++++++++++++++++++++++++++++++ src/shader/wrap/graphic_shader.h | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 src/shader/wrap/compute_shader.h create mode 100644 src/shader/wrap/graphic_shader.h (limited to 'src/shader/wrap') diff --git a/src/shader/wrap/compute_shader.h b/src/shader/wrap/compute_shader.h new file mode 100644 index 0000000..9f5c5cb --- /dev/null +++ b/src/shader/wrap/compute_shader.h @@ -0,0 +1,47 @@ +#pragma once + +#include "util.h" + +class ComputeShader { +private: + const GLuint _id; + +public: + struct Guard { + const GLuint _id; + + Guard(GLuint id): _id(id) { + glUseProgram(_id); + } + ~Guard() { + glUseProgram(0); + } + }; + + Guard use() const { + return Guard(_id); + } + + 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); + } +}; diff --git a/src/shader/wrap/graphic_shader.h b/src/shader/wrap/graphic_shader.h new file mode 100644 index 0000000..03249d5 --- /dev/null +++ b/src/shader/wrap/graphic_shader.h @@ -0,0 +1,52 @@ +#pragma once + +#include "shader/util.h" + +class GraphicShader { +private: + const GLuint _id; + +public: + struct Guard { + const GLuint _id; + + Guard(GLuint id): _id(id) { + glUseProgram(_id); + } + ~Guard() { + glUseProgram(0); + } + }; + + Guard use() const { + return Guard(_id); + } + + 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; + } +}; -- cgit v1.2.3