-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
shader_program.cpp
666 lines (540 loc) · 22 KB
/
shader_program.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Copyright 2013-2024 the openage authors. See copying.md for legal info.
#include "shader_program.h"
#include <algorithm>
#include <cstdio>
#include <unordered_set>
#include "datastructure/constexpr_map.h"
#include "error/error.h"
#include "log/log.h"
#include "renderer/opengl/context.h"
#include "renderer/opengl/error.h"
#include "renderer/opengl/geometry.h"
#include "renderer/opengl/lookup.h"
#include "renderer/opengl/shader.h"
#include "renderer/opengl/texture.h"
#include "renderer/opengl/uniform_buffer.h"
#include "renderer/opengl/uniform_input.h"
#include "renderer/opengl/util.h"
namespace openage::renderer::opengl {
static void check_program_status(GLuint program, GLenum what_to_check) {
GLint status = GL_FALSE;
glGetProgramiv(program, what_to_check, &status);
GlContext::check_error();
if (status != GL_TRUE) {
const char *what_str = [=] {
switch (what_to_check) {
case GL_LINK_STATUS:
return "linking";
case GL_VALIDATE_STATUS:
return "validation";
case GL_COMPILE_STATUS:
return "compilation";
default:
return "unknown shader creation task";
}
}();
GLint loglen = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &loglen);
std::vector<char> infolog(loglen);
GLint real_loglen;
glGetProgramInfoLog(program, loglen, &real_loglen, infolog.data());
std::string gl_message = "no reason returned";
if (infolog.size() and strlen(infolog.data())) {
gl_message = infolog.data();
}
throw Error(ERR << "OpenGL shader program " << what_str << " failed (len " << loglen << ", real: " << real_loglen << "):\n"
<< gl_message,
true);
}
}
GlShaderProgram::GlShaderProgram(const std::shared_ptr<GlContext> &context,
const std::vector<resources::ShaderSource> &srcs) :
GlSimpleObject(context,
[](GLuint handle) { glDeleteProgram(handle); }),
validated(false) {
const gl_context_spec &caps = context->get_specs();
GLuint handle = glCreateProgram();
this->handle = handle;
std::vector<GlShader> shaders;
for (auto const &src : srcs) {
GlShader shader{context, src};
glAttachShader(handle, shader.get_handle());
shaders.push_back(std::move(shader));
}
glLinkProgram(handle);
check_program_status(handle, GL_LINK_STATUS);
// after linking we can delete the shaders
for (auto const &shdr : shaders) {
glDetachShader(handle, shdr.get_handle());
}
// query program information
GLint val;
glGetProgramiv(handle, GL_ACTIVE_ATTRIBUTES, &val);
size_t attrib_count = val;
glGetProgramiv(handle, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &val);
size_t max_name_len = val;
glGetProgramiv(handle, GL_ACTIVE_UNIFORMS, &val);
size_t unif_count = val;
glGetProgramiv(handle, GL_ACTIVE_UNIFORM_MAX_LENGTH, &val);
max_name_len = std::max(size_t(val), max_name_len);
glGetProgramiv(handle, GL_ACTIVE_UNIFORM_BLOCKS, &val);
size_t unif_block_count = val;
glGetProgramiv(handle, GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, &val);
max_name_len = std::max(size_t(val), max_name_len);
std::vector<char> name(max_name_len);
// Indices of uniforms within named blocks.
std::unordered_set<GLuint> in_block_unifs;
GLuint block_binding = 0;
// Extract uniform block descriptions.
for (GLuint i_unif_block = 0; i_unif_block < unif_block_count; ++i_unif_block) {
glGetActiveUniformBlockName(
handle,
i_unif_block,
name.size(),
nullptr,
name.data());
std::string block_name(name.data());
GLint data_size;
glGetActiveUniformBlockiv(handle, i_unif_block, GL_UNIFORM_BLOCK_DATA_SIZE, &data_size);
glGetActiveUniformBlockiv(handle, i_unif_block, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &val);
std::vector<GLint> uniform_indices(val);
glGetActiveUniformBlockiv(handle, i_unif_block, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, uniform_indices.data());
std::vector<GlInBlockUniform> uniforms;
for (GLuint const i_unif : uniform_indices) {
in_block_unifs.insert(i_unif);
GLenum type;
GLint offset, count, stride;
glGetActiveUniform(
handle,
i_unif,
name.size(),
nullptr,
&count,
&type,
name.data());
glGetActiveUniformsiv(handle, 1, &i_unif, GL_UNIFORM_OFFSET, &offset);
glGetActiveUniformsiv(handle, 1, &i_unif, GL_UNIFORM_ARRAY_STRIDE, &stride);
if (stride == 0) {
// The uniform is not an array, but it's declared in a named block and hence might
// be a matrix whose stride we need to know.
glGetActiveUniformsiv(handle, 1, &i_unif, GL_UNIFORM_MATRIX_STRIDE, &stride);
}
// We do not need to handle sampler types here like in the uniform loop below,
// because named blocks cannot contain samplers.
uniforms.push_back(
GlInBlockUniform{
type,
size_t(offset),
size_t(count) * GL_UNIFORM_TYPE_SIZE.get(type),
size_t(stride),
size_t(count),
std::string(name.data())});
}
// ENSURE(block_binding < caps.max_uniform_buffer_bindings,
// "Tried to create an OpenGL shader that uses more uniform blocks "
// << "than there are binding points (" << caps.max_uniform_buffer_bindings
// << " available).");
//
// glUniformBlockBinding(handle, i_unif_block, block_binding);
// block_binding += 1;
this->uniform_blocks.insert(std::make_pair(
block_name,
GlUniformBlock{
i_unif_block,
size_t(data_size),
std::move(uniforms),
block_binding}));
}
GLuint tex_unit_id = 0;
// Extract information about uniforms in the default block.
// the uniform ID is the index in the uniforms vector
uniform_id_t unif_id = 0;
for (GLuint i_unif = 0; i_unif < unif_count; ++i_unif) {
if (in_block_unifs.count(i_unif) == 1) {
// Skip uniforms within named blocks.
continue;
}
GLint count;
GLenum type;
glGetActiveUniform(
handle,
i_unif,
name.size(),
nullptr,
&count,
&type,
name.data());
GLuint loc = glGetUniformLocation(handle, name.data());
this->uniforms.push_back({type, loc, std::nullopt});
this->uniforms_by_name.insert(std::make_pair(
name.data(),
unif_id));
if (type == GL_SAMPLER_2D) {
ENSURE(tex_unit_id < caps.max_texture_slots,
"Tried to create an OpenGL shader that uses more texture sampler uniforms "
<< "than there are texture unit slots (" << caps.max_texture_slots << " available).");
this->uniforms[unif_id].tex_unit = tex_unit_id;
tex_unit_id += 1;
}
// Increment uniform ID
unif_id += 1;
}
// Resize the texture unit bindings
// to number of texture units used by the shader
this->textures_per_texunits.resize(tex_unit_id);
// Extract vertex attribute descriptions.
for (GLuint i_attrib = 0; i_attrib < attrib_count; ++i_attrib) {
GLint size;
GLenum type;
glGetActiveAttrib(
handle,
i_attrib,
name.size(),
nullptr,
&size,
&type,
name.data());
this->attribs.insert(std::make_pair(
name.data(),
GlVertexAttrib{
type,
GLint(i_attrib),
size,
}));
}
log::log(MSG(info) << "Created OpenGL shader program");
if (!this->uniform_blocks.empty()) {
log::log(MSG(dbg) << "Uniform blocks: ");
for (auto const &pair : this->uniform_blocks) {
log::log(MSG(dbg) << "(" << pair.second.index << ") " << pair.first
<< " (size: " << pair.second.data_size << ") {");
for (auto const &unif : pair.second.uniforms) {
log::log(MSG(dbg) << "\t+" << unif.offset
<< " " << unif.name << ": "
<< GLSL_TYPE_NAME.get(unif.type));
}
log::log(MSG(dbg) << "}");
}
}
if (!this->uniforms.empty()) {
log::log(MSG(dbg) << "Uniforms: ");
for (const auto &pair : this->uniforms_by_name) {
const auto &unif_info = this->uniforms[pair.second];
log::log(MSG(dbg) << "(" << unif_info.location << ") "
<< pair.first << ": "
<< GLSL_TYPE_NAME.get(unif_info.type));
}
}
if (!this->attribs.empty()) {
log::log(MSG(dbg) << "Vertex attributes: ");
for (auto const &pair : this->attribs) {
log::log(MSG(dbg) << "(" << pair.second.location << ") " << pair.first << ": "
<< GLSL_TYPE_NAME.get(pair.second.type));
}
}
}
void GlShaderProgram::use() {
// the setup doesn't need to be done if this program is already active.
if (this->in_use()) {
return;
}
if (!this->validated) {
// TODO(Vtec234): validation depends on the context state, so this might be worth calling
// more than once. However, once per frame is probably too much.
glValidateProgram(*this->handle);
check_program_status(*this->handle, GL_VALIDATE_STATUS);
this->validated = true;
}
glUseProgram(*this->handle);
// store this program as the active one
this->context->set_current_program(
std::static_pointer_cast<GlShaderProgram>(
this->shared_from_this()));
for (size_t i = 0; i < this->textures_per_texunits.size(); ++i) {
auto &tex_unit = this->textures_per_texunits[i];
if (not tex_unit) {
continue;
}
if (not glIsTexture(*tex_unit)) {
// By the time we use the shader again, the texture may have been deleted
// but if it's fixed afterwards using update_uniforms, the render state
// will still be fine
// We can free the texture unit in this case
tex_unit = std::nullopt;
continue;
}
// We have to bind the texture to their texture units here because
// the texture unit bindings are global to the context. Each time
// the shader switches, it is possible that some other shader overwrote
// these, and since we want the uniform values to persist across update_uniforms
// calls, we have to set them more often than just on update_uniforms.
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, *tex_unit);
}
}
bool GlShaderProgram::in_use() const {
return this->context->get_current_program().lock().get() == this;
}
void GlShaderProgram::update_uniforms(std::shared_ptr<GlUniformInput> const &unif_in) {
ENSURE(unif_in->get_program().get() == this, "Uniform input passed to different shader than it was created with.");
// TODO: use glProgramUniform when we're on OpenGL 4.1
// then we don't need to "use" and then call glUniform*
if (not this->in_use()) {
this->use();
}
const auto &update_offs = unif_in->update_offs;
const auto &used_uniforms = unif_in->used_uniforms;
const auto &uniforms = this->uniforms;
uint8_t const *data = unif_in->update_data.data();
size_t unif_count = used_uniforms.size();
for (size_t i = 0; i < unif_count; ++i) {
uniform_id_t unif_id = used_uniforms[i];
const auto &update_off = update_offs[unif_id];
uint8_t const *ptr = data + update_off.offset;
const auto &unif = uniforms[unif_id];
auto loc = unif.location;
switch (unif.type) {
case GL_INT:
glUniform1i(loc, *reinterpret_cast<const GLint *>(ptr));
break;
case GL_UNSIGNED_INT:
glUniform1ui(loc, *reinterpret_cast<const GLuint *>(ptr));
break;
case GL_FLOAT:
glUniform1f(loc, *reinterpret_cast<const float *>(ptr));
break;
case GL_DOUBLE:
// TODO requires an extension
glUniform1d(loc, *reinterpret_cast<const double *>(ptr));
break;
case GL_BOOL:
glUniform1ui(loc, *reinterpret_cast<const bool *>(ptr));
break;
case GL_FLOAT_VEC2:
glUniform2fv(loc, 1, reinterpret_cast<const float *>(ptr));
break;
case GL_FLOAT_VEC3:
glUniform3fv(loc, 1, reinterpret_cast<const float *>(ptr));
break;
case GL_FLOAT_VEC4:
glUniform4fv(loc, 1, reinterpret_cast<const float *>(ptr));
break;
case GL_INT_VEC2:
glUniform2iv(loc, 1, reinterpret_cast<const GLint *>(ptr));
break;
case GL_INT_VEC3:
glUniform3iv(loc, 1, reinterpret_cast<const GLint *>(ptr));
break;
case GL_INT_VEC4:
glUniform4iv(loc, 1, reinterpret_cast<const GLint *>(ptr));
break;
case GL_UNSIGNED_INT_VEC2:
glUniform2uiv(loc, 1, reinterpret_cast<const GLuint *>(ptr));
break;
case GL_UNSIGNED_INT_VEC3:
glUniform3uiv(loc, 1, reinterpret_cast<const GLuint *>(ptr));
break;
case GL_UNSIGNED_INT_VEC4:
glUniform4uiv(loc, 1, reinterpret_cast<const GLuint *>(ptr));
break;
case GL_FLOAT_MAT3:
glUniformMatrix3fv(loc, 1, GLboolean(false), reinterpret_cast<const float *>(ptr));
break;
case GL_FLOAT_MAT4:
glUniformMatrix4fv(loc, 1, GLboolean(false), reinterpret_cast<const float *>(ptr));
break;
case GL_SAMPLER_2D: {
ENSURE(unif.tex_unit,
"Tried to access texture unit for uniform that has no texture unit assigned.");
GLuint tex_unit_id = *unif.tex_unit;
GLuint tex = *reinterpret_cast<const GLuint *>(ptr);
glActiveTexture(GL_TEXTURE0 + tex_unit_id);
glBindTexture(GL_TEXTURE_2D, tex);
// TODO: maybe call this at a more appropriate position
glUniform1i(loc, tex_unit_id);
auto &tex_value = *this->textures_per_texunits[tex_unit_id];
tex_value = tex;
break;
}
default:
throw Error(MSG(err) << "Tried to upload unknown uniform type to GL shader.");
}
}
}
const GlUniformBlock &GlShaderProgram::get_uniform_block(const char *name) const {
return this->uniform_blocks.at(name);
}
std::map<size_t, resources::vertex_input_t> GlShaderProgram::vertex_attributes() const {
std::map<size_t, resources::vertex_input_t> attrib_map;
for (auto const &attr : this->attribs) {
attrib_map[attr.second.location] = GL_VERT_IN_TYPE.get(attr.second.type);
}
return attrib_map;
}
std::shared_ptr<UniformInput> GlShaderProgram::new_unif_in() {
auto in = std::make_shared<GlUniformInput>(this->shared_from_this());
return in;
}
uniform_id_t GlShaderProgram::get_uniform_id(const char *name) {
return this->uniforms_by_name.at(name);
}
const std::vector<GlUniform> &GlShaderProgram::get_uniforms() const {
return this->uniforms;
}
const std::unordered_map<std::string, GlUniformBlock> &GlShaderProgram::get_uniform_blocks() const {
return this->uniform_blocks;
}
bool GlShaderProgram::has_uniform(const char *name) {
return this->uniforms_by_name.contains(name);
}
void GlShaderProgram::bind_uniform_buffer(const char *block_name, std::shared_ptr<UniformBuffer> const &buffer) {
ENSURE(this->uniform_blocks.count(block_name) == 1,
"Tried to set binding point for uniform block " << block_name << " that does not exist in the shader program.");
auto gl_buffer = std::dynamic_pointer_cast<GlUniformBuffer>(buffer);
auto &block = this->uniform_blocks[block_name];
// Check if the uniform buffer matches the block definition
for (auto const &unif : block.uniforms) {
ENSURE(gl_buffer->has_uniform(unif.name.c_str()),
"Uniform buffer does not contain uniform '" << unif.name << "' required by block " << block_name);
}
block.binding_point = gl_buffer->get_binding_point();
glUniformBlockBinding(*this->handle, block.index, block.binding_point);
}
void GlShaderProgram::set_unif(UniformInput &in,
const char *unif,
void const *val,
size_t size,
GLenum type) {
auto uniform_id = this->uniforms_by_name.find(unif);
ENSURE(uniform_id != std::end(this->uniforms_by_name),
"Tried to set uniform '" << unif << "' that does not exist in the shader program.");
this->set_unif(in, uniform_id->second, val, size, type);
}
void GlShaderProgram::set_unif(UniformInput &in,
uniform_id_t unif_id,
void const *val,
size_t size,
GLenum type) {
auto &unif_in = dynamic_cast<GlUniformInput &>(in);
ENSURE(unif_id < this->uniforms.size(),
"Tried to set uniform '" << unif_id << "' that does not exist in the shader program.");
ENSURE(unif_id < this->uniforms.size(),
"Tried to set uniform with invalid ID " << unif_id);
auto const &unif_info = this->uniforms[unif_id];
ENSURE(type == unif_info.type,
"Tried to set uniform '" << unif_id << "' to a value of the wrong type.");
auto &update_off = unif_in.update_offs[unif_id];
auto offset = update_off.offset;
memcpy(unif_in.update_data.data() + offset, val, size);
if (not update_off.used) [[unlikely]] { // only true if the uniform value was not set before
auto lower_bound = std::lower_bound(
std::begin(unif_in.used_uniforms),
std::end(unif_in.used_uniforms),
unif_id);
unif_in.used_uniforms.insert(lower_bound, unif_id);
update_off.used = true;
}
}
void GlShaderProgram::set_i32(UniformInput &in, const char *unif, int32_t val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_INT), GL_INT);
}
void GlShaderProgram::set_u32(UniformInput &in, const char *unif, uint32_t val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_UNSIGNED_INT), GL_UNSIGNED_INT);
}
void GlShaderProgram::set_f32(UniformInput &in, const char *unif, float val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_FLOAT), GL_FLOAT);
}
void GlShaderProgram::set_f64(UniformInput &in, const char *unif, double val) {
// TODO requires extension
this->set_unif(in, unif, &val, get_uniform_type_size(GL_DOUBLE), GL_DOUBLE);
}
void GlShaderProgram::set_bool(UniformInput &in, const char *unif, bool val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_BOOL), GL_BOOL);
}
void GlShaderProgram::set_v2f32(UniformInput &in, const char *unif, Eigen::Vector2f const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_FLOAT_VEC2), GL_FLOAT_VEC2);
}
void GlShaderProgram::set_v3f32(UniformInput &in, const char *unif, Eigen::Vector3f const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_FLOAT_VEC3), GL_FLOAT_VEC3);
}
void GlShaderProgram::set_v4f32(UniformInput &in, const char *unif, Eigen::Vector4f const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_FLOAT_VEC4), GL_FLOAT_VEC4);
}
void GlShaderProgram::set_v2i32(UniformInput &in, const char *unif, Eigen::Vector2i const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_INT_VEC2), GL_INT_VEC2);
}
void GlShaderProgram::set_v3i32(UniformInput &in, const char *unif, Eigen::Vector3i const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_INT_VEC3), GL_INT_VEC3);
}
void GlShaderProgram::set_v4i32(UniformInput &in, const char *unif, Eigen::Vector4i const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_INT_VEC4), GL_INT_VEC4);
}
void GlShaderProgram::set_v2ui32(UniformInput &in, const char *unif, Eigen::Vector2<uint32_t> const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_UNSIGNED_INT_VEC2), GL_UNSIGNED_INT_VEC2);
}
void GlShaderProgram::set_v3ui32(UniformInput &in, const char *unif, Eigen::Vector3<uint32_t> const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_UNSIGNED_INT_VEC3), GL_UNSIGNED_INT_VEC3);
}
void GlShaderProgram::set_v4ui32(UniformInput &in, const char *unif, Eigen::Vector4<uint32_t> const &val) {
this->set_unif(in, unif, &val, get_uniform_type_size(GL_UNSIGNED_INT_VEC4), GL_UNSIGNED_INT_VEC4);
}
void GlShaderProgram::set_m4f32(UniformInput &in, const char *unif, Eigen::Matrix4f const &val) {
this->set_unif(in, unif, val.data(), get_uniform_type_size(GL_FLOAT_MAT4), GL_FLOAT_MAT4);
}
void GlShaderProgram::set_tex(UniformInput &in, const char *unif, std::shared_ptr<Texture2d> const &val) {
auto tex = std::dynamic_pointer_cast<GlTexture2d>(val);
GLuint handle = tex->get_handle();
this->set_unif(in, unif, &handle, get_uniform_type_size(GL_SAMPLER_2D), GL_SAMPLER_2D);
}
void GlShaderProgram::set_i32(UniformInput &in, uniform_id_t id, int32_t val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_INT), GL_INT);
}
void GlShaderProgram::set_u32(UniformInput &in, uniform_id_t id, uint32_t val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_UNSIGNED_INT), GL_UNSIGNED_INT);
}
void GlShaderProgram::set_f32(UniformInput &in, uniform_id_t id, float val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_FLOAT), GL_FLOAT);
}
void GlShaderProgram::set_f64(UniformInput &in, uniform_id_t id, double val) {
// TODO requires extension
this->set_unif(in, id, &val, get_uniform_type_size(GL_DOUBLE), GL_DOUBLE);
}
void GlShaderProgram::set_bool(UniformInput &in, uniform_id_t id, bool val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_BOOL), GL_BOOL);
}
void GlShaderProgram::set_v2f32(UniformInput &in, uniform_id_t id, Eigen::Vector2f const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_FLOAT_VEC2), GL_FLOAT_VEC2);
}
void GlShaderProgram::set_v3f32(UniformInput &in, uniform_id_t id, Eigen::Vector3f const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_FLOAT_VEC3), GL_FLOAT_VEC3);
}
void GlShaderProgram::set_v4f32(UniformInput &in, uniform_id_t id, Eigen::Vector4f const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_FLOAT_VEC4), GL_FLOAT_VEC4);
}
void GlShaderProgram::set_v2i32(UniformInput &in, uniform_id_t id, Eigen::Vector2i const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_INT_VEC2), GL_INT_VEC2);
}
void GlShaderProgram::set_v3i32(UniformInput &in, uniform_id_t id, Eigen::Vector3i const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_INT_VEC3), GL_INT_VEC3);
}
void GlShaderProgram::set_v4i32(UniformInput &in, uniform_id_t id, Eigen::Vector4i const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_INT_VEC4), GL_INT_VEC4);
}
void GlShaderProgram::set_v2ui32(UniformInput &in, uniform_id_t id, Eigen::Vector2<uint32_t> const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_UNSIGNED_INT_VEC2), GL_UNSIGNED_INT_VEC2);
}
void GlShaderProgram::set_v3ui32(UniformInput &in, uniform_id_t id, Eigen::Vector3<uint32_t> const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_UNSIGNED_INT_VEC3), GL_UNSIGNED_INT_VEC3);
}
void GlShaderProgram::set_v4ui32(UniformInput &in, uniform_id_t id, Eigen::Vector4<uint32_t> const &val) {
this->set_unif(in, id, &val, get_uniform_type_size(GL_UNSIGNED_INT_VEC4), GL_UNSIGNED_INT_VEC4);
}
void GlShaderProgram::set_m4f32(UniformInput &in, uniform_id_t id, Eigen::Matrix4f const &val) {
this->set_unif(in, id, val.data(), get_uniform_type_size(GL_FLOAT_MAT4), GL_FLOAT_MAT4);
}
void GlShaderProgram::set_tex(UniformInput &in, uniform_id_t id, std::shared_ptr<Texture2d> const &val) {
auto tex = std::dynamic_pointer_cast<GlTexture2d>(val);
GLuint handle = tex->get_handle();
this->set_unif(in, id, &handle, get_uniform_type_size(GL_SAMPLER_2D), GL_SAMPLER_2D);
}
} // namespace openage::renderer::opengl