-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cpp
341 lines (289 loc) · 9.22 KB
/
Shader.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "Shader.h"
#include <glm/gtc/type_ptr.hpp>
#include <filesystem>
namespace fs = std::filesystem;
// OpenGL Shader Class Implementation
int typeFromExt(string path) {
if (path.find(".vert") != string::npos) {
return GL_VERTEX_SHADER;
} else if (path.find(".frag") != string::npos) {
return GL_FRAGMENT_SHADER;
} else if (path.find(".geom") != string::npos) {
return GL_GEOMETRY_SHADER;
} else {
cerr << "SHADER LOAD ERROR: file " << path.c_str()
<< " wasn't marked in a way we'd know what it is." << endl;
exit(101);
}
}
bool checkStatusPart(GLuint shaderPart) {
GLint status;
char statusBuffer[512];
glGetShaderiv(shaderPart, GL_COMPILE_STATUS, &status);
glGetShaderInfoLog(shaderPart, 512, NULL, statusBuffer);
#ifdef NDEBUG
if (status != GL_TRUE) {
#endif
#if defined(NDEBUG) || defined(DEBUG_SHADERS)
cout << "Shader Compile Log:" << endl;
cout << statusBuffer << endl << endl;
#endif
#ifdef NDEBUG
}
#endif
if (status != GL_TRUE) {
return false;
}
return true;
}
Shader::Shader() {
DS("shader constructor")
reset();
}
Shader::~Shader() {
DS("shader deconstructor")
glDeleteProgram(program);
}
void Shader::loadFiles(vector<string> files) {
DS("shader load files")
for (auto const &file : files) {
loadFile(file);
}
}
void Shader::loadFile(string file) {
DS("shader load file")
ifstream fileStream(file);
stringstream code;
if (fileStream.is_open()) {
code << fileStream.rdbuf();
#ifdef DEBUG_SHADERS
cout << "SHADER FILE: " << file.c_str() << endl;
#endif // DEBUG
loadString(code.str(), typeFromExt(file));
}
}
void Shader::loadFileCombined(string file) {
DS("shader load combined file")
ifstream fileStream(file);
stringstream code;
if (fileStream.is_open()) {
code << fileStream.rdbuf();
map<int, string> splitShaders = preprocessGLSLPragma(code.str());
for (const auto &c : splitShaders) {
loadString(c.second, c.first);
}
}
}
void Shader::loadString(string shaderCode, int type) {
DS("shader load string")
preprocessGLSLIncludes(&shaderCode);
GLuint part = glCreateShader(type);
const char *code = shaderCode.c_str();
glShaderSource(part, 1, &code, NULL);
glCompileShader(part);
checkStatusPart(part);
#ifdef DEBUG_SHADERS
cout << code << endl << endl;
#endif // DEBUG
// commit and delete
glAttachShader(program, part);
glDeleteShader(part);
}
void Shader::link() {
DS("shader link")
glLinkProgram(program);
uniforms.clear();
linked = true;
}
void Shader::use() {
// DS("shader use")
if (linked == false) {
link();
}
glUseProgram(program);
}
void Shader::reset() {
D("shader reset")
glDeleteProgram(program);
program = glCreateProgram();
linked = false;
}
string _renderVert(string version, string code, string func, string typeName) {
stringstream buffer;
buffer << version << endl;
buffer << "#define VERTEX" << endl;
buffer << code << endl;
buffer << "// -- Generated Vertex" << endl;
buffer << "out " << typeName << " _VERT2FRAG;" << endl;
buffer << "void main() {" << endl
// << " " << typeName << " _VERT2FRAG;" << endl
<< " _VERT2FRAG = " << func << "(gl_Position);" << endl
<< "}";
return buffer.str();
}
string _renderFrag(string version, string code, string func, string typeName, bool vface) {
stringstream buffer;
buffer << version << endl;
buffer << "#define FRAGMENT" << endl;
buffer << "out vec4 OUT_COLOR;" << endl;
buffer << code << endl;
buffer << "// -- Generated Fragment" << endl;
buffer << "in " << typeName << " _VERT2FRAG;" << endl;
buffer << "void main() {" << endl;
if (vface) {
buffer << " OUT_COLOR = " << func << "(_VERT2FRAG, gl_FrontFacing);" << endl;
} else {
buffer << " OUT_COLOR = " << func << "(_VERT2FRAG);" << endl;
}
buffer << "}" << endl;
return buffer.str();
}
static regex
glslPragmaFV("\n\\#pragma (\\bfragment|vertex\\b) ([a-zA-Z0-9_]+)");
static regex glslPragmaVER("\n\\#pragma glsl ([0-9]+ ?[a-z]*)");
map<int, string> Shader::preprocessGLSLPragma(string code) {
DS("preprocess #pragma")
smatch directive;
map<int, string> precompiledShaders; // GLint mapped to a shader string.
vector<string> toRemove; // matched strings that we need to remove afterwards.
// technically speaking, we remove it after because
// of the moving target it becomes.
string version = "#version ";
if (regex_search(code, directive, glslPragmaVER)) {
version.append(directive.str(1));
} else {
version.append(DEFAULT_GLSL_VERSION);
}
string vertFunc;
string vertType;
string fragFunc;
bool fragVface;
while (regex_search(code, directive, glslPragmaFV)) {
stringstream buffer;
string arg1 = directive.str(1); // the type
string arg2 = directive.str(2); // the function name
bool ok = false;
if (arg1.compare("vertex") == 0) {
regex vertOutputType("\n\\s*([a-zA-Z0-9_]+)\\s+" + arg2 + "\\s*\\(");
smatch typeName;
regex_search(code, typeName, vertOutputType);
vertFunc = arg2;
vertType = typeName.str(1);
// buffer << version << endl;
// buffer << "#define VERTEX" << endl;
// buffer << code << endl;
// buffer << "// -- Generated Vertex" << endl;
// buffer << "void main() {" << endl
// << " " << arg2 << "(gl_Position);" << endl
// << "}";
// precompiledShaders.insert_or_assign(GL_VERTEX_SHADER, buffer.str());
ok = true;
} else if (arg1.compare("fragment") == 0) {
fragFunc = arg2;
regex fragVfaceDetect(" " + arg2 + "\\s*\\([^,{]+\\)");
smatch typeName;
regex_search(code, typeName, fragVfaceDetect);
fragVface = typeName.size() == 0;
cout << "match:" << typeName.str(0) << endl;
cout << typeName.str(1) << "<==" << endl;
ok = true;
}
if (ok) {
toRemove.push_back(directive.str(0));
}
// and destroy the evidence.
code.erase(directive.position(), directive.length());
// #ifdef DEBUG_SHADERS
// cout << "=== *** REMAINING SEARCH *** ===" << endl
// << code << endl
// << "=== *** END REMAINING SEARCH ***" << endl;
// #endif
}
precompiledShaders.insert_or_assign(
GL_FRAGMENT_SHADER, _renderFrag(version, code, fragFunc, vertType, fragVface));
precompiledShaders.insert_or_assign(
GL_VERTEX_SHADER, _renderVert(version, code, vertFunc, vertType));
return precompiledShaders;
}
static map<string, string> includeCache;
static regex glslInclude("\n\\#include \"(.*)\"");
void Shader::preprocessGLSLIncludes(string *code) {
DS("preprocess #include")
// find includes
smatch directive;
while (regex_search(*code, directive, glslInclude)) {
stringstream injectedCode;
string fileName = directive.str(1);
injectedCode << endl << "// INCLUDED FROM: " << fileName << endl;
if (includeCache.count(fileName) > 0) {
injectedCode << includeCache[fileName];
} else {
stringstream loadedCode;
auto pathname = fs::absolute(SHADER_PATH "/./" + fileName);
#if DEBUG_SHADERS
cout << pathname.c_str() << endl;
#endif
ifstream data;
data.open(pathname);
if (!data.is_open()) {
pathname = fs::absolute(SHADER_INCLUDES "/./" + fileName);
#if DEBUG_SHADERS
cout << pathname.c_str() << endl;
#endif
data.open(pathname);
if (!data.is_open()) {
cout << "WARNING: Shader include " << fileName << " wasn't found."
<< endl;
// includeCache.insert_or_assign(fileName, "");
continue;
}
}
loadedCode << data.rdbuf();
includeCache.insert_or_assign(fileName, loadedCode.str());
injectedCode << loadedCode.rdbuf();
}
injectedCode << endl << "// -- END " << fileName << endl;
code->replace(directive.position(), directive.length(),
injectedCode.str().c_str());
}
}
GLint Shader::uniform(string uniformName) {
if (uniforms.count(uniformName) > 0) {
return uniforms[uniformName];
} else {
const char *n = uniformName.c_str();
GLint u = glGetUniformLocation(program, n);
uniforms.insert_or_assign(uniformName, u);
return u;
}
}
void Shader::updateUniformTime(map<string, glm::vec4> times) {
for (const auto &t : times) {
GLint i = uniform(t.first);
glUniform4fv(i, 1, glm::value_ptr(t.second));
}
}
void Shader::updateAllUniformTimes(
chrono::time_point<chrono::high_resolution_clock> start,
chrono::time_point<chrono::high_resolution_clock> cur) {
float t = chrono::duration_cast<chrono::milliseconds>(start - cur).count();
glm::vec4 u_Time(t / 20, t, t * 2, t * 3);
map<string, glm::vec4> times = {{"u_Time", u_Time},
{"u_SinTime", glm::sin(u_Time)},
{"u_CosTime", glm::cos(u_Time)}};
for (const auto &s : shaders) {
s.second->updateUniformTime(times);
}
}
void Shader::set(string uniformName, glm::vec4 v4) {
GLint u = uniform(uniformName);
glUniform4fv(u, 1, glm::value_ptr(v4));
}
void Shader::set(string uniformName, glm::vec3 v3) {
GLint u = uniform(uniformName);
glUniform3fv(u, 1, glm::value_ptr(v3));
}
void Shader::set(string uniformName, float f) {
GLint u = uniform(uniformName);
glUniform1f(u, f);
}