aboutsummaryrefslogtreecommitdiff
path: root/src/shader
diff options
context:
space:
mode:
authorAdrian Kummerlaender2018-05-26 13:20:47 +0200
committerAdrian Kummerlaender2018-05-26 13:21:40 +0200
commit34052b51e00c939a35294d7085cadb5111484dd3 (patch)
tree03a5b1e350e47e2aa8551393ef67b2124ad50ddb /src/shader
parentf728e4c8d202de241673a13ce61570b6acb4bba7 (diff)
downloadcomputicle-34052b51e00c939a35294d7085cadb5111484dd3.tar
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.gz
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.bz2
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.lz
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.xz
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.zst
computicle-34052b51e00c939a35294d7085cadb5111484dd3.zip
Separate headers into compilation units
Diffstat (limited to 'src/shader')
-rw-r--r--src/shader/util.cc47
-rw-r--r--src/shader/util.h43
-rw-r--r--src/shader/wrap/compute_shader.cc40
-rw-r--r--src/shader/wrap/compute_shader.h45
-rw-r--r--src/shader/wrap/graphic_shader.cc45
-rw-r--r--src/shader/wrap/graphic_shader.h51
6 files changed, 165 insertions, 106 deletions
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 <iostream>
+#include <vector>
+
+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/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 <iostream>
+#include <string>
-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 <GL/glew.h>
- 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<GLchar> 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 <string>
+
+#include <GL/glew.h>
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<GLuint>& v) const {
+ GLuint id = util::getUniform(_id, name);
+ glUniform1iv(id, v.size(), reinterpret_cast<const GLint*>(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 <vector>
+#include <string>
+
+#include <GL/glew.h>
+#include <glm/glm.hpp>
+
#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<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;
- }
+ 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<GLuint>& v) const;
+ GLuint setUniform(const std::string& name, glm::mat4& M) const;
};