aboutsummaryrefslogtreecommitdiff
path: root/src/shader
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader')
-rw-r--r--src/shader/code/compute.glsl (renamed from src/shader/compute.glsl)0
-rw-r--r--src/shader/code/display_fragment.glsl (renamed from src/shader/display_fragment.glsl)0
-rw-r--r--src/shader/code/display_vertex.glsl (renamed from src/shader/display_vertex.glsl)0
-rw-r--r--src/shader/code/fragment.glsl (renamed from src/shader/fragment.glsl)0
-rw-r--r--src/shader/code/vertex.glsl (renamed from src/shader/vertex.glsl)0
-rw-r--r--src/shader/util.h46
-rw-r--r--src/shader/wrap/compute_shader.h47
-rw-r--r--src/shader/wrap/graphic_shader.h52
8 files changed, 145 insertions, 0 deletions
diff --git a/src/shader/compute.glsl b/src/shader/code/compute.glsl
index eaf5579..eaf5579 100644
--- a/src/shader/compute.glsl
+++ b/src/shader/code/compute.glsl
diff --git a/src/shader/display_fragment.glsl b/src/shader/code/display_fragment.glsl
index b731ebe..b731ebe 100644
--- a/src/shader/display_fragment.glsl
+++ b/src/shader/code/display_fragment.glsl
diff --git a/src/shader/display_vertex.glsl b/src/shader/code/display_vertex.glsl
index 60bbfc7..60bbfc7 100644
--- a/src/shader/display_vertex.glsl
+++ b/src/shader/code/display_vertex.glsl
diff --git a/src/shader/fragment.glsl b/src/shader/code/fragment.glsl
index 37e18bd..37e18bd 100644
--- a/src/shader/fragment.glsl
+++ b/src/shader/code/fragment.glsl
diff --git a/src/shader/vertex.glsl b/src/shader/code/vertex.glsl
index 4c307d8..4c307d8 100644
--- a/src/shader/vertex.glsl
+++ b/src/shader/code/vertex.glsl
diff --git a/src/shader/util.h b/src/shader/util.h
new file mode 100644
index 0000000..7aec674
--- /dev/null
+++ b/src/shader/util.h
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <iostream>
+
+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<GLchar> 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/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<GLuint>& v) const {
+ GLuint id = util::getUniform(_id, name);
+ glUniform1iv(id, v.size(), reinterpret_cast<const GLint*>(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;
+ }
+};