Repository navigation
Expand file tree
/
Copy pathshader_program.cpp
More file actions
163 lines (136 loc) · 4.59 KB
/
Copy pathshader_program.cpp
File metadata and controls
163 lines (136 loc) · 4.59 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
#include "graphics/shader_program.hpp"
#include <cstddef>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace gps {
namespace {
class ShaderObject final {
public:
explicit ShaderObject(GLuint shader) noexcept : shader_{shader} {}
~ShaderObject() {
if (shader_ != 0) {
glDeleteShader(shader_);
}
}
ShaderObject(const ShaderObject&) = delete;
ShaderObject& operator=(const ShaderObject&) = delete;
ShaderObject(ShaderObject&& other) noexcept : shader_{std::exchange(other.shader_, 0)} {}
ShaderObject& operator=(ShaderObject&&) = delete;
[[nodiscard]] GLuint id() const noexcept {
return shader_;
}
private:
GLuint shader_{0};
};
[[nodiscard]] std::string_view shader_stage_name(ShaderStage stage) noexcept {
switch (stage) {
case ShaderStage::vertex:
return "vertex";
case ShaderStage::tessellation_control:
return "tessellation control";
case ShaderStage::tessellation_evaluation:
return "tessellation evaluation";
case ShaderStage::geometry:
return "geometry";
case ShaderStage::fragment:
return "fragment";
case ShaderStage::compute:
return "compute";
default:
return "unknown-stage";
}
}
std::string shader_log(GLuint shader) {
GLint log_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
if (log_length <= 1) {
return {};
}
std::string log(static_cast<std::size_t>(log_length), '\0');
GLsizei written = 0;
glGetShaderInfoLog(shader, log_length, &written, log.data());
log.resize(static_cast<std::size_t>(written));
return log;
}
std::string program_log(GLuint program) {
GLint log_length = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
if (log_length <= 1) {
return {};
}
std::string log(static_cast<std::size_t>(log_length), '\0');
GLsizei written = 0;
glGetProgramInfoLog(program, log_length, &written, log.data());
log.resize(static_cast<std::size_t>(written));
return log;
}
[[nodiscard]] ShaderObject compile_shader(const ShaderStageSource& stage) {
ShaderObject shader{glCreateShader(std::to_underlying(stage.stage))};
if (shader.id() == 0) {
throw std::runtime_error{"OpenGL failed to create the " +
std::string{shader_stage_name(stage.stage)} + " shader object."};
}
const GLchar* source_data = stage.source.data();
// Keep the OpenGL API width visible at this narrowing boundary.
// NOLINTNEXTLINE(modernize-use-auto)
const GLint source_length = static_cast<GLint>(stage.source.size());
glShaderSource(shader.id(), 1, &source_data, &source_length);
glCompileShader(shader.id());
GLint compiled = GL_FALSE;
glGetShaderiv(shader.id(), GL_COMPILE_STATUS, &compiled);
if (compiled != GL_TRUE) {
throw std::runtime_error{std::string{shader_stage_name(stage.stage)} +
" shader compilation failed:\n" + shader_log(shader.id())};
}
return shader;
}
} // namespace
ShaderProgram::ShaderProgram(std::span<const ShaderStageSource> stages) {
if (stages.empty()) {
throw std::invalid_argument{"A shader program requires at least one stage."};
}
for (const ShaderStageSource& stage : stages) {
if (stage.source.empty()) {
throw std::invalid_argument{std::string{shader_stage_name(stage.stage)} +
" shader source must not be empty."};
}
}
std::vector<ShaderObject> shaders;
shaders.reserve(stages.size());
for (const ShaderStageSource& stage : stages) {
shaders.push_back(compile_shader(stage));
}
try {
program_ = glCreateProgram();
if (program_ == 0) {
throw std::runtime_error{"OpenGL failed to allocate a program object."};
}
for (const ShaderObject& shader : shaders) {
glAttachShader(program_, shader.id());
}
glLinkProgram(program_);
GLint linked = GL_FALSE;
glGetProgramiv(program_, GL_LINK_STATUS, &linked);
if (linked != GL_TRUE) {
throw std::runtime_error{"Shader link failed:\n" + program_log(program_)};
}
} catch (...) {
if (program_ != 0) {
glDeleteProgram(program_);
program_ = 0;
}
throw;
}
}
ShaderProgram::~ShaderProgram() {
if (program_ != 0) {
glDeleteProgram(program_);
}
}
GLuint ShaderProgram::id() const noexcept {
return program_;
}
} // namespace gps