-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshader.cpp
More file actions
178 lines (170 loc) · 5.43 KB
/
shader.cpp
File metadata and controls
178 lines (170 loc) · 5.43 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
174
175
176
177
178
#include "shader.h"
// compile‑time dispatch for glUniform*
template<typename T> inline constexpr bool always_false_v = false;
void Shader::Build(const char* vertexPath, const char* fragmentPath)
{
s_vertexPath = std::string(vertexPath);
s_fragmentPath = std::string(fragmentPath);
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
auto glhelper = OpenGLHelper::GetInstance();
vertexCode += *glhelper->get_glsl_version() + std::string("\n#define VERTEX\n") + vShaderStream.str();
fragmentCode += *glhelper->get_glsl_version() + std::string("\n#define FRAGMENT\n") + fShaderStream.str();
// std::cout << fragmentCode << std::endl;
}
catch (std::ifstream::failure& e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
exit(1);
}
_Compile(&vertexCode, &fragmentCode);
}
void Shader::_Compile(const std::string* pvertexCode, const std::string* pfragmentCode)
{
uniformCache.clear();
const char* vShaderCode = pvertexCode->c_str();
const char* fShaderCode = pfragmentCode->c_str();
// 2. compile shaders
unsigned int vertex, fragment;
// vertex shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
CheckCompileErrors(vertex, "VERTEX");
// fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
CheckCompileErrors(fragment, "FRAGMENT");
// shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
CheckCompileErrors(ID, "PROGRAM");
// delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex);
glDeleteShader(fragment);
isReady = true;
}
void Shader::CacheUniform(std::string const& name) {
uniformCache[name] = glGetUniformLocation(ID, name.c_str());
}
void Shader::SetUniform(std::string const& name, UniformValue const& v) {
if (!isInUse) {
this->Use();
if (!isInUse)
{
std::cerr << "Shader error: Calling glUseProgram() returns an error" << std::endl;
return;
}
}
GLint loc = -1;
auto it = uniformCache.find(name);
if (it == uniformCache.end()) {
loc = glGetUniformLocation(ID, name.c_str());
uniformCache[name] = loc;
if (loc < 0) // uniform not available in shader
{
std::cout << "Uniform " << name << " not available in shader" << std::endl;
return;
}
} else {
loc = it->second;
}
std::visit([&](auto const& arg){
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T,bool>) {
glUniform1i(loc, arg ? 1 : 0);
}
else if constexpr (std::is_same_v<T,int>) {
glUniform1i(loc, arg);
}
else if constexpr (std::is_same_v<T,uint32_t>) {
glUniform1ui(loc, arg);
}
else if constexpr (std::is_same_v<T,float>) {
glUniform1f(loc, arg);
}
else if constexpr (std::is_same_v<T,glm::vec2>) {
glUniform2fv(loc, 1, &arg.x);
}
else if constexpr (std::is_same_v<T,glm::ivec2>) {
glUniform2iv(loc, 1, &arg.x);
}
else if constexpr (std::is_same_v<T,glm::uvec2>) {
glUniform2uiv(loc, 1, &arg.x);
}
else if constexpr (std::is_same_v<T,glm::vec3>) {
glUniform3fv(loc, 1, &arg.x);
}
else if constexpr (std::is_same_v<T,glm::vec4>) {
glUniform4fv(loc, 1, &arg.x);
}
else if constexpr (std::is_same_v<T,glm::mat2>) {
glUniformMatrix2fv(loc, 1, GL_FALSE, &arg[0][0]);
}
else if constexpr (std::is_same_v<T,glm::mat3>) {
glUniformMatrix3fv(loc, 1, GL_FALSE, &arg[0][0]);
}
else if constexpr (std::is_same_v<T,glm::mat4>) {
glUniformMatrix4fv(loc, 1, GL_FALSE, &arg[0][0]);
}
else {
static_assert(always_false_v<T>, "Unsupported uniform type");
}
#ifdef DEBUG
if ((glGetError()) != GL_NO_ERROR) {
std::cerr << "Set Uniform error for: "<< name << std::endl;
}
#endif
}, v);
}
void Shader::CheckCompileErrors(GLuint shader, std::string type)
{
GLint success;
GLchar infoLog[1024];
if (type != "PROGRAM")
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
std::cout << " Vertex: " << s_vertexPath << std::endl;
std::cout << " Fragment: " << s_fragmentPath << std::endl;
exit(1);
}
}
else
{
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
std::cout << " Vertex: " << s_vertexPath << std::endl;
std::cout << " Fragment: " << s_fragmentPath << std::endl;
exit(1);
}
}
}