-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputeShader.h
145 lines (117 loc) · 4.71 KB
/
ComputeShader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef SLIMEMOLDSIMULATION_COMPUTESHADER_H
#define SLIMEMOLDSIMULATION_COMPUTESHADER_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
class ComputeShader {
const std::string enumValues[2] = {"COMPUTE", "PROGRAM"};
enum ComputeErrorType {
COMPUTE, PROGRAM
};
public:
unsigned int ID;
GLuint m_ssbo = 0;
GLuint m_ubo = 0;
explicit ComputeShader(const char *computeShaderPath) {
std::string computeShader;
std::ifstream computeShaderFile;
computeShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
computeShaderFile.open(computeShaderPath);
std::stringstream computeStream;
computeStream << computeShaderFile.rdbuf();
computeShaderFile.close();
computeShader = computeStream.str();
} catch (std::ifstream::failure &e) {
std::cout << "ERROR: Could not read compute shader file: " << &e << std::endl;
}
const char *cShader = computeShader.c_str();
uint compute;
compute = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(compute, 1, &cShader, NULL);
glCompileShader(compute);
checkCompileErrors(compute, ComputeErrorType::COMPUTE);
ID = glCreateProgram();
glAttachShader(ID, compute);
glLinkProgram(ID);
checkCompileErrors(ID, ComputeErrorType::PROGRAM);
glDeleteShader(compute);
}
void use() {
glUseProgram(ID);
}
void useSSBO(int index, size_t size, void *data) {
glGenBuffers(1, &m_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, size, data, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, m_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
std::cout << "Binding SSBO of size: " << std::to_string(size) << std::endl;
}
void useUBO(int index, size_t size, void *data) {
glGenBuffers(1, &m_ubo);
glBindBuffer(GL_UNIFORM_BUFFER, m_ubo);
glBufferData(GL_UNIFORM_BUFFER, size, data, GL_DYNAMIC_COPY);
glBindBufferBase(GL_UNIFORM_BUFFER, index, m_ubo);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
void clearSSBO(int index) {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, 0);
}
void clearUBO(int index) {
glBindBufferBase(GL_UNIFORM_BUFFER, index, 0);
}
static void dispatch(unsigned int xGroup, unsigned int yGroup, unsigned int zGroup) {
glDispatchCompute(xGroup, yGroup, zGroup);
}
[[maybe_unused]]
void setBool(const std::string &name, bool value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value);
}
void setFloat(const std::string &name, float value) const {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
[[maybe_unused]]
void setInt(const std::string &name, int value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
[[maybe_unused]]
void setUnsignedInt(const std::string &name, uint value) const {
glUniform1ui(glGetUniformLocation(ID, name.c_str()), value);
}
[[maybe_unused]]
void setVec4(const std::string &name, const glm::vec4 &value) const {
glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
[[maybe_unused]]
void setVec3(const std::string &name, const glm::vec3 &value) const {
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
[[maybe_unused]]
void setVec3(const std::string &name, float x, float y, float z) const {
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
}
private:
void checkCompileErrors(GLuint shader, ComputeErrorType type) {
GLint success;
uint32_t logSize = 1024;
GLchar infoLog[logSize];
if (type != ComputeErrorType::PROGRAM) {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << enumValues[type] << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
} else {
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << enumValues[type] << "\n"
<< infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
};
#endif