forked from lquatrin/cpp_volume_rendering
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcomputeshader.cpp
More file actions
173 lines (135 loc) · 4.85 KB
/
computeshader.cpp
File metadata and controls
173 lines (135 loc) · 4.85 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "computeshader.h"
#include <gl_utils/utils.h>
namespace gl
{
ComputeShader::ComputeShader ()
{
num_groups_x = 0;
num_groups_y = 0;
num_groups_z = 0;
vec_compute_shader_names.clear();
vec_compute_shader_ids.clear();
}
ComputeShader::~ComputeShader ()
{
Unbind();
for (int i = 0; i < vec_compute_shader_ids.size(); i++)
{
glDetachShader(shader_program, vec_compute_shader_ids[i]);
glDeleteShader(vec_compute_shader_ids[i]);
}
vec_compute_shader_names.clear();
vec_compute_shader_ids.clear();
}
bool ComputeShader::LoadAndLink ()
{
// Creates a compute shader and the respective program that contains the shader.
if (shader_program == -1)
shader_program = glCreateProgram();
for (int i = 0; i < vec_compute_shader_names.size(); i++)
{
GLuint new_shader = glCreateShader(GL_COMPUTE_SHADER);
CompileShader(new_shader, vec_compute_shader_names[i]);
vec_compute_shader_ids.push_back(new_shader);
assert(vec_compute_shader_ids[i] == new_shader);
glAttachShader(shader_program, new_shader);
}
glLinkProgram(shader_program);
// Check link status
int is_linked;
glGetProgramiv(shader_program, GL_LINK_STATUS, &is_linked);
if (!is_linked) {
GLint max_length = 0;
glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &max_length);
if (max_length != 0) {
std::vector<GLchar> info_log(max_length);
glGetProgramInfoLog(shader_program, max_length, &max_length, &info_log[0]);
std::string info_log_str(info_log.begin(), info_log.end());
std::cout << info_log_str << std::endl;
}
else {
std::cout << "No info log from linking compute shader program" << std::endl;
}
fprintf(stderr, "Error in linking compute shader program\n");
exit(41);
}
gl::ExitOnGLError("gl::ComputeShader >> Unable to link shaders.");
glValidateProgram(shader_program);
gl::ExitOnGLError("gl::ComputeShader >> Unable to load and link shaders.");
return true;
}
bool ComputeShader::Reload ()
{
Unbind();
for (int i = 0; i < vec_compute_shader_ids.size(); i++)
{
glDetachShader(shader_program, vec_compute_shader_ids[i]);
glDeleteShader(vec_compute_shader_ids[i]);
}
vec_compute_shader_ids.clear();
LoadAndLink();
Bind();
for (std::map<std::string, UniformVariable>::iterator it = uniform_variables.begin(); it != uniform_variables.end(); ++it)
uniform_variables[it->first].location = glGetUniformLocation(shader_program, it->first.c_str());
BindUniforms();
gl::Shader::Unbind();
return true;
}
void ComputeShader::SetShaderFile (std::string filename)
{
vec_compute_shader_names.clear();
vec_compute_shader_names.push_back(filename);
}
void ComputeShader::AddShaderFile (std::string filepath)
{
vec_compute_shader_names.push_back(filepath);
}
void ComputeShader::RecomputeNumberOfGroups (GLuint w, GLuint h, GLuint d, GLuint t_x, GLuint t_y, GLuint t_z)
{
num_groups_x = 1;
if (w > 0)
num_groups_x = ceil((float)w / (float)t_x);
num_groups_y = 1;
if (h > 0)
num_groups_y = ceil((float)h / (float)t_y);
num_groups_z = 1;
if (d > 0)
num_groups_z = ceil((float)d / (float)t_z);
}
void ComputeShader::Dispatch ()
{
glDispatchCompute(num_groups_x, num_groups_y, num_groups_z);
gl::ExitOnGLError("ComputeShader: After glDispatchCompute.");
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
gl::ExitOnGLError("ComputeShader: After glMemoryBarrier.");
}
void ComputeShader::BindImageTexture (gl::Texture3D* tex, GLuint unit,
GLint level, GLenum access,
GLenum format, GLboolean layered,
GLint layer)
{
glBindTexture(GL_TEXTURE_3D, tex->GetTextureID());
glBindImageTexture(unit, tex->GetTextureID(), level, layered, layer, access, format);
}
void ComputeShader::CompileShader (GLuint shader_id, std::string filename)
{
char* shader_source = gl::TextFileRead(filename.c_str());
const char* const_shader_source = shader_source;
// Second parameters can be > 1 if const_shader_source is an array.
glShaderSource(shader_id, 1, &const_shader_source, NULL);
free(shader_source);
glCompileShader(shader_id);
// Check compilation status
int rvalue;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &rvalue);
if (!rvalue) {
fprintf(stderr, "Error in compiling the compute shader\n");
GLchar log[10240];
GLsizei length;
glGetShaderInfoLog(shader_id, 10239, &length, log);
fprintf(stderr, "Compiler log:\n%s\n", log);
exit(40);
}
gl::ExitOnGLError("gl::ComputeShader >> Could not compile the shader file!");
}
}