-
Notifications
You must be signed in to change notification settings - Fork 27
/
shader_common.h
512 lines (356 loc) · 13.5 KB
/
shader_common.h
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#ifndef SHADER_COMMON_H
#define SHADER_COMMON_H
#include "resource.h"
#include "regex.h"
#include "vectors.h"
#include <map>
#include <list>
#include <string>
#include <exception>
#include <vector>
extern std::string gSDLAppShaderDir;
enum { SHADER_UNIFORM_INT,
SHADER_UNIFORM_FLOAT,
SHADER_UNIFORM_BOOL,
SHADER_UNIFORM_SAMPLER_1D,
SHADER_UNIFORM_SAMPLER_2D,
SHADER_UNIFORM_SAMPLER_3D,
SHADER_UNIFORM_VEC2,
SHADER_UNIFORM_VEC3,
SHADER_UNIFORM_VEC4,
SHADER_UNIFORM_MAT3,
SHADER_UNIFORM_MAT4,
SHADER_UNIFORM_INT_ARRAY,
SHADER_UNIFORM_FLOAT_ARRAY,
SHADER_UNIFORM_VEC2_ARRAY,
SHADER_UNIFORM_VEC3_ARRAY,
SHADER_UNIFORM_VEC4_ARRAY
};
extern Regex Shader_pre_version;
extern Regex Shader_pre_extension;
extern Regex Shader_pre_include;
extern Regex Shader_uniform_def;
extern Regex Shader_error_line;
extern Regex Shader_error2_line;
extern Regex Shader_error3_line;
extern Regex Shader_warning_line;
extern Regex Shader_redefine_line;
class ShaderException : public std::exception {
protected:
std::string message;
std::string source;
public:
ShaderException(const std::string& message);
ShaderException(const std::string& message, const std::string& source);
~ShaderException() throw () {};
virtual const char* what() const throw() { return message.c_str(); }
const std::string& getSource() const;
};
class AbstractShader;
class ShaderUniform {
protected:
std::string name;
std::string comment;
int location;
AbstractShader* shader;
int uniform_type;
std::string type_name;
bool modified;
bool initialized;
bool baked;
public:
ShaderUniform(AbstractShader* shader, const std::string& name, int uniform_type, const std::string& type_name);
virtual ~ShaderUniform() {};
virtual void unload();
int getLocation();
int getType() { return uniform_type; };
virtual void write(std::string& content) const {};
const std::string& getName() const;
bool isInitialized() const { return initialized; };
bool isBaked() const { return baked; };
bool isModified() const { return modified; };
void setComment(const std::string& comment);
const std::string& getComment() const;
void setInitialized(bool initialized) { this->initialized = initialized; };
virtual void setBaked(bool baked);
virtual void setModified(bool modified) { this->modified = modified; };
};
class FloatShaderUniform : public ShaderUniform {
float value;
public:
FloatShaderUniform(AbstractShader* shader, const std::string& name, float value = 0.0f);
void write(std::string& content) const;
void setValue(float value);
float& getValue();
};
class IntShaderUniform : public ShaderUniform {
int value;
public:
IntShaderUniform(AbstractShader* shader, const std::string& name, int value = 0);
void write(std::string& content) const;
void setValue(int value);
int& getValue();
};
class BoolShaderUniform : public ShaderUniform {
bool value;
public:
BoolShaderUniform(AbstractShader* shader, const std::string& name, bool value = false);
void write(std::string& content) const;
void setValue(bool value);
bool& getValue();
};
class Sampler1DShaderUniform : public ShaderUniform {
int value;
public:
Sampler1DShaderUniform(AbstractShader* shader, const std::string& name, int value = 0);
void write(std::string& content) const;
void setBaked(bool baked);
void setValue(int value);
int& getValue();
};
class Sampler2DShaderUniform : public ShaderUniform {
int value;
public:
Sampler2DShaderUniform(AbstractShader* shader, const std::string& name, int value = 0);
void write(std::string& content) const;
void setBaked(bool baked);
void setValue(int value);
int& getValue();
};
class Sampler3DShaderUniform : public ShaderUniform {
int value;
public:
Sampler3DShaderUniform(AbstractShader* shader, const std::string& name, int value = 0);
void write(std::string& content) const;
void setBaked(bool baked);
void setValue(int value);
int& getValue();
};
class Vec2ShaderUniform : public ShaderUniform {
vec2 value;
public:
Vec2ShaderUniform(AbstractShader* shader, const std::string& name, const vec2& value = vec2(0.0f)) ;
void write(std::string& content) const;
void setValue(const vec2& value);
vec2& getValue();
};
class Vec3ShaderUniform : public ShaderUniform {
vec3 value;
public:
Vec3ShaderUniform(AbstractShader* shader, const std::string& name, const vec3& value = vec3(0.0f));
void write(std::string& content) const;
void setValue(const vec3& value);
vec3& getValue();
};
class Vec4ShaderUniform : public ShaderUniform {
vec4 value;
public:
Vec4ShaderUniform(AbstractShader* shader, const std::string& name, const vec4& value = vec4(0.0f));
void write(std::string& content) const;
void setValue(const vec4& value);
vec4& getValue();
};
class Mat3ShaderUniform : public ShaderUniform {
mat3 value;
public:
Mat3ShaderUniform(AbstractShader* shader, const std::string& name, const mat3& value = mat3(1.0f));
void write(std::string& content) const;
void setValue(const mat3& value);
mat3& getValue();
};
class Mat4ShaderUniform : public ShaderUniform {
mat4 value;
public:
Mat4ShaderUniform(AbstractShader* shader, const std::string& name, const mat4& value = mat4(1.0f));
void write(std::string& content) const;
void setValue(const mat4& value);
mat4& getValue();
};
class IntegerArrayShaderUniform : public ShaderUniform {
std::vector<int> value;
size_t length;
void copyValue(const std::vector<int>& value);
public:
IntegerArrayShaderUniform(AbstractShader* shader, const std::string& name, size_t length);
~IntegerArrayShaderUniform();
void write(std::string& content) const;
void setValue(const std::vector<int>& value);
const std::vector<int>& getValue();
size_t getLength() const;
};
class FloatArrayShaderUniform : public ShaderUniform {
std::vector<float> value;
size_t length;
void copyValue(const std::vector<float>& value);
public:
FloatArrayShaderUniform(AbstractShader* shader, const std::string& name, size_t length);
~FloatArrayShaderUniform();
void write(std::string& content) const;
void setValue(const std::vector<float>& value);
const std::vector<float>& getValue();
size_t getLength() const;
};
class Vec2ArrayShaderUniform : public ShaderUniform {
std::vector<vec2> value;
size_t length;
void copyValue(const vec2* value);
void copyValue(const std::vector<vec2>& value);
public:
Vec2ArrayShaderUniform(AbstractShader* shader, const std::string& name, size_t length, const vec2* value = 0);
~Vec2ArrayShaderUniform();
void write(std::string& content) const;
void setValue(const vec2* value);
void setValue(const std::vector<vec2>& value);
const std::vector<vec2>& getValue();
size_t getLength() const;
};
class Vec3ArrayShaderUniform : public ShaderUniform {
std::vector<vec3> value;
size_t length;
void copyValue(const vec3* value);
void copyValue(const std::vector<vec3>& value);
public:
Vec3ArrayShaderUniform(AbstractShader* shader, const std::string& name, size_t length, const vec3* value = 0);
~Vec3ArrayShaderUniform();
void write(std::string& content) const;
void setValue(const vec3* value);
void setValue(const std::vector<vec3>& value);
const std::vector<vec3>& getValue();
size_t getLength() const;
};
class Vec4ArrayShaderUniform : public ShaderUniform {
std::vector<vec4> value;
size_t length;
void copyValue(const vec4* value);
void copyValue(const std::vector<vec4>& value);
public:
Vec4ArrayShaderUniform(AbstractShader* shader, const std::string& name, size_t length, const vec4* value = 0);
~Vec4ArrayShaderUniform();
void write(std::string& content) const;
void setValue(const vec4* value);
void setValue(const std::vector<vec4>& value);
const std::vector<vec4>& getValue();
size_t getLength() const;
};
class ShaderPart {
std::string filename;
std::string raw_source;
std::string processed_source;
std::map<std::string,std::string> defines;
std::map<std::string,std::string> substitutions;
void preprocess();
void loadSourceFile();
void applySubstitution(std::string& source, const std::string& name, const std::string& value);
void applyDefines(std::string& source);
void applySubstitutions(std::string& source);
public:
ShaderPart();
void setSourceFile(const std::string& filename);
void setSource(const std::string& source);
void reload();
void reset();
void substitute(const std::string& name, const char *value, ...);
void substitute(const std::string& name, const std::string& value);
void define(const std::string& name);
void define(const std::string& name, const char *value, ...);
void define(const std::string& name, const std::string& value);
const std::string& getSource();
};
class AbstractShaderPass {
protected:
int shader_object_type;
std::string shader_object_desc;
unsigned int shader_object;
int version;
std::map<std::string,std::string> extensions;
AbstractShader* parent;
std::string source;
std::string shader_object_source;
std::list<ShaderUniform*> uniforms;
void showContext(std::string& context, int line_no, int amount);
bool errorContext(const std::string& log_message, std::string& context);
bool preprocess(const std::string& line);
public:
AbstractShaderPass(AbstractShader* parent, int shader_object_type, const std::string& shader_object_desc);
virtual ~AbstractShaderPass() {};
int getType() { return shader_object_type; };
bool isEmpty();
void toString(std::string& out);
const std::string& getObjectSource();
ShaderUniform* addArrayUniform(const std::string& name, const std::string& type, size_t length);
ShaderUniform* addUniform(const std::string& name, const std::string& type);
void includeSource(const std::string& source);
void includeFile(const std::string& filename);
std::list<ShaderUniform*>& getUniforms();
virtual void attachTo(unsigned int program) = 0;
virtual void unload() = 0;
virtual void compile() = 0;
virtual void checkError() = 0;
};
class AbstractShader : public Resource {
protected:
std::map<std::string, ShaderUniform*> uniforms;
std::list<ShaderUniform*> uniform_list;
std::map<std::string,std::string> substitutions;
std::string prefix;
unsigned int program;
bool dynamic_compile;
void setDefaults();
virtual void loadPrefix() = 0;
virtual void checkProgramError() = 0;
public:
AbstractShaderPass* vertex_shader;
AbstractShaderPass* geometry_shader;
AbstractShaderPass* compute_shader;
AbstractShaderPass* fragment_shader;
AbstractShader();
AbstractShader(const std::string& prefix);
unsigned int getProgram();
void clear();
void reload(bool force = false);
bool isEmpty();
void includeSource(unsigned int shader_object_type, const std::string& source);
void includeFile(unsigned int shader_object_type, const std::string& filename);
static void substitute(std::string& source, const std::string& name, const std::string& value);
void addSubstitute(const std::string& name, const std::string& value);
void addSubstitute(const std::string& name, const char *value, ...);
void applySubstitutions(std::string& source);
void addUniform(ShaderUniform* uniform);
ShaderUniform* getUniform(const std::string& name);
void setDynamicCompile(bool dynamic_compile);
bool needsCompile();
const std::list<ShaderUniform*>& getUniforms();
void applyUniforms();
void setBool(const std::string& name, bool value);
void setInteger (const std::string& name, int value);
void setSampler1D(const std::string& name, int value);
void setSampler2D(const std::string& name, int value);
void setSampler3D(const std::string& name, int value);
void setFloat(const std::string& name, float value);
void setVec2 (const std::string& name, const vec2& value);
void setVec3 (const std::string& name, const vec3& value);
void setVec4 (const std::string& name, const vec4& value);
void setMat3 (const std::string& name, const mat3& value);
void setMat4 (const std::string& name, const mat4& value);
void setIntegerArray(const std::string& name, std::vector<int>& value);
void setFloatArray(const std::string& name, std::vector<float>& value);
void setVec2Array(const std::string& name, vec2* value);
void setVec2Array(const std::string& name, std::vector<vec2>& value);
void setVec3Array(const std::string& name, vec3* value);
void setVec3Array(const std::string& name, std::vector<vec3>& value);
void setVec4Array(const std::string& name, vec4* value);
void setVec4Array(const std::string& name, std::vector<vec4>& value);
void setBaked(const std::string& name, bool baked);
void setBakedUniforms(bool baked);
virtual AbstractShaderPass* grabShaderPass(unsigned int shader_object_type) = 0;
virtual void applyUniform(ShaderUniform* u) = 0;
virtual int getUniformLocation(const std::string& uniform_name) = 0;
virtual void compile() = 0;
virtual void link() = 0;
virtual void load() = 0;
virtual void unload() = 0;
virtual void bind() = 0;
virtual void unbind() = 0;
void use();
};
#endif