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
|
#include "shader.h"
#include "msgException.h"
#include <algorithm>
#include <array>
#include <format>
#include <stdexcept>
#include <string>
namespace {
auto
getInt(GLenum e)
{
GLint i {};
glGetIntegerv(e, &i);
return std::to_string(i);
}
using LookUpFunction = std::string (*)(GLenum);
constexpr std::array<std::tuple<std::string_view, GLenum, LookUpFunction>, 1> LOOKUPS {{
{"GL_MAX_GEOMETRY_OUTPUT_VERTICES", GL_MAX_GEOMETRY_OUTPUT_VERTICES, getInt},
}};
struct ShaderCompileError : public MsgException<std::invalid_argument> {
explicit ShaderCompileError(GLuint shader, Shader::Source src) :
MsgException<std::invalid_argument> {"Error compiling shader"}, shader {shader}, source {src},
msg {getShaderText(GL_INFO_LOG_LENGTH, glGetShaderInfoLog)}
{
}
[[nodiscard]] std::string
getMsg() const noexcept override
{
return std::format("Error compiling shader: '{}'\nSource:\n{}",
getShaderText(GL_INFO_LOG_LENGTH, glGetShaderInfoLog), source);
}
private:
std::string
getShaderText(GLenum param, auto getTextFunc) const
{
std::string text;
text.resize_and_overwrite(static_cast<size_t>(Shader::getShaderParam(shader, param)),
[this, getTextFunc](auto buf, auto len) {
GLsizei outLen {};
getTextFunc(shader, static_cast<GLsizei>(len), &outLen, buf);
return outLen;
});
return text;
}
const GLuint shader;
const Shader::Source source;
const std::string msg;
};
}
Shader::ShaderRef
Shader::compile() const
{
ShaderRef shader {type};
auto source = [&shader](auto text, GLint len) {
glShaderSource(shader, 1, &text, &len);
};
if (lookups) {
std::basic_string<GLchar> textMod {text};
for (const auto & match : ctre::range<R"(\bGL_[A-Z_]+\b)">(textMod)) {
if (const auto lookup = std::find_if(LOOKUPS.begin(), LOOKUPS.end(),
[&match](const auto & lookup) {
return std::get<std::string_view>(lookup) == match;
});
lookup != LOOKUPS.end()) {
const auto & [name, pname, getFunction] = *lookup;
textMod.replace(match.begin(), match.end(), getFunction(pname));
}
}
source(textMod.c_str(), static_cast<GLint>(textMod.length()));
}
else {
source(text.data(), static_cast<GLint>(text.length()));
}
glCompileShader(shader);
checkShaderError(shader);
return shader;
}
void
Shader::checkShaderError(GLuint shader) const
{
if (getShaderParam(shader, GL_COMPILE_STATUS) == GL_FALSE) {
throw ShaderCompileError {shader, text};
}
}
GLint
Shader::getShaderParam(GLuint shader, GLenum pname)
{
GLint pvalue {};
glGetShaderiv(shader, pname, &pvalue);
return pvalue;
}
|