Antkeeper  0.0.1
gl-shader-variables.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
21 #include <engine/gl/texture.hpp>
22 #include <numeric>
23 
24 namespace gl {
25 
26 gl_shader_bool::gl_shader_bool(std::size_t size, GLint gl_uniform_location):
27  shader_variable(size),
28  gl_uniform_location{gl_uniform_location},
29  ivalues(size)
30 {}
31 
32 void gl_shader_bool::update(bool value) const noexcept
33 {
34  glUniform1i(gl_uniform_location, static_cast<GLint>(value));
35 }
36 
37 void gl_shader_bool::update(bool value, std::size_t index) const
38 {
39  glUniform1i(gl_uniform_location + static_cast<GLint>(index), static_cast<GLint>(value));
40 }
41 
42 void gl_shader_bool::update(std::span<const bool> values, std::size_t index) const
43 {
44  for (std::size_t i = 0; i < values.size(); ++i)
45  {
46  ivalues[i] = static_cast<GLint>(values[i]);
47  }
48 
49  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), ivalues.data());
50 }
51 
52 gl_shader_bvec2::gl_shader_bvec2(std::size_t size, GLint gl_uniform_location):
53  shader_variable(size),
54  gl_uniform_location{gl_uniform_location},
55  ivalues(size * 2)
56 {}
57 
58 void gl_shader_bvec2::update(const math::bvec2& value) const noexcept
59 {
60  glUniform2i(gl_uniform_location, static_cast<GLint>(value[0]), static_cast<GLint>(value[1]));
61 }
62 
63 void gl_shader_bvec2::update(const math::bvec2& value, std::size_t index) const
64 {
65  glUniform2i(gl_uniform_location + static_cast<GLint>(index), static_cast<GLint>(value[0]), static_cast<GLint>(value[1]));
66 }
67 
68 void gl_shader_bvec2::update(std::span<const math::bvec2> values, std::size_t index) const
69 {
70  GLint* ivalue = ivalues.data();
71  for (const auto& value: values)
72  {
73  *(++ivalue) = static_cast<GLint>(value[0]);
74  *(++ivalue) = static_cast<GLint>(value[1]);
75  }
76 
77  glUniform2iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), ivalues.data());
78 }
79 
80 gl_shader_bvec3::gl_shader_bvec3(std::size_t size, GLint gl_uniform_location):
81  shader_variable(size),
82  gl_uniform_location{gl_uniform_location},
83  ivalues(size * 3)
84 {}
85 
86 void gl_shader_bvec3::update(const math::bvec3& value) const noexcept
87 {
88  glUniform3i(gl_uniform_location, static_cast<GLint>(value[0]), static_cast<GLint>(value[1]), static_cast<GLint>(value[2]));
89 }
90 
91 void gl_shader_bvec3::update(const math::bvec3& value, std::size_t index) const
92 {
93  glUniform3i(gl_uniform_location + static_cast<GLint>(index), static_cast<GLint>(value[0]), static_cast<GLint>(value[1]), static_cast<GLint>(value[2]));
94 }
95 
96 void gl_shader_bvec3::update(std::span<const math::bvec3> values, std::size_t index) const
97 {
98  GLint* ivalue = ivalues.data();
99  for (const auto& value: values)
100  {
101  *(++ivalue) = static_cast<GLint>(value[0]);
102  *(++ivalue) = static_cast<GLint>(value[1]);
103  *(++ivalue) = static_cast<GLint>(value[2]);
104  }
105 
106  glUniform3iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), ivalues.data());
107 }
108 
109 gl_shader_bvec4::gl_shader_bvec4(std::size_t size, GLint gl_uniform_location):
110  shader_variable(size),
111  gl_uniform_location{gl_uniform_location},
112  ivalues(size * 4)
113 {}
114 
115 void gl_shader_bvec4::update(const math::bvec4& value) const noexcept
116 {
117  glUniform4i(gl_uniform_location, static_cast<GLint>(value[0]), static_cast<GLint>(value[1]), static_cast<GLint>(value[2]), static_cast<GLint>(value[3]));
118 }
119 
120 void gl_shader_bvec4::update(const math::bvec4& value, std::size_t index) const
121 {
122  glUniform4i(gl_uniform_location + static_cast<GLint>(index), static_cast<GLint>(value[0]), static_cast<GLint>(value[1]), static_cast<GLint>(value[2]), static_cast<GLint>(value[3]));
123 }
124 
125 void gl_shader_bvec4::update(std::span<const math::bvec4> values, std::size_t index) const
126 {
127  GLint* ivalue = ivalues.data();
128  for (const auto& value: values)
129  {
130  *(++ivalue) = static_cast<GLint>(value[0]);
131  *(++ivalue) = static_cast<GLint>(value[1]);
132  *(++ivalue) = static_cast<GLint>(value[2]);
133  *(++ivalue) = static_cast<GLint>(value[3]);
134  }
135 
136  glUniform4iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), ivalues.data());
137 }
138 
139 gl_shader_int::gl_shader_int(std::size_t size, GLint gl_uniform_location):
140  shader_variable(size),
141  gl_uniform_location{gl_uniform_location}
142 {}
143 
144 void gl_shader_int::update(int value) const noexcept
145 {
146  glUniform1i(gl_uniform_location, value);
147 }
148 
149 void gl_shader_int::update(int value, std::size_t index) const
150 {
151  glUniform1i(gl_uniform_location + static_cast<GLint>(index), value);
152 }
153 
154 void gl_shader_int::update(std::span<const int> values, std::size_t index) const
155 {
156  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data());
157 }
158 
159 gl_shader_ivec2::gl_shader_ivec2(std::size_t size, GLint gl_uniform_location):
160  shader_variable(size),
161  gl_uniform_location{gl_uniform_location}
162 {}
163 
164 void gl_shader_ivec2::update(const math::ivec2& value) const noexcept
165 {
166  glUniform2iv(gl_uniform_location, 1, value.data());
167 }
168 
169 void gl_shader_ivec2::update(const math::ivec2& value, std::size_t index) const
170 {
171  glUniform2iv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
172 }
173 
174 void gl_shader_ivec2::update(std::span<const math::ivec2> values, std::size_t index) const
175 {
176  glUniform2iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
177 }
178 
179 gl_shader_ivec3::gl_shader_ivec3(std::size_t size, GLint gl_uniform_location):
180  shader_variable(size),
181  gl_uniform_location{gl_uniform_location}
182 {}
183 
184 void gl_shader_ivec3::update(const math::ivec3& value) const noexcept
185 {
186  glUniform3iv(gl_uniform_location, 1, value.data());
187 }
188 
189 void gl_shader_ivec3::update(const math::ivec3& value, std::size_t index) const
190 {
191  glUniform3iv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
192 }
193 
194 void gl_shader_ivec3::update(std::span<const math::ivec3> values, std::size_t index) const
195 {
196  glUniform3iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
197 }
198 
199 gl_shader_ivec4::gl_shader_ivec4(std::size_t size, GLint gl_uniform_location):
200  shader_variable(size),
201  gl_uniform_location{gl_uniform_location}
202 {}
203 
204 void gl_shader_ivec4::update(const math::ivec4& value) const noexcept
205 {
206  glUniform4iv(gl_uniform_location, 1, value.data());
207 }
208 
209 void gl_shader_ivec4::update(const math::ivec4& value, std::size_t index) const
210 {
211  glUniform4iv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
212 }
213 
214 void gl_shader_ivec4::update(std::span<const math::ivec4> values, std::size_t index) const
215 {
216  glUniform4iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
217 }
218 
219 gl_shader_uint::gl_shader_uint(std::size_t size, GLint gl_uniform_location):
220  shader_variable(size),
221  gl_uniform_location{gl_uniform_location}
222 {}
223 
224 void gl_shader_uint::update(unsigned int value) const noexcept
225 {
226  glUniform1ui(gl_uniform_location, value);
227 }
228 
229 void gl_shader_uint::update(unsigned int value, std::size_t index) const
230 {
231  glUniform1ui(gl_uniform_location + static_cast<GLint>(index), value);
232 }
233 
234 void gl_shader_uint::update(std::span<const unsigned int> values, std::size_t index) const
235 {
236  glUniform1uiv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data());
237 }
238 
239 gl_shader_uvec2::gl_shader_uvec2(std::size_t size, GLint gl_uniform_location):
240  shader_variable(size),
241  gl_uniform_location{gl_uniform_location}
242 {}
243 
244 void gl_shader_uvec2::update(const math::uvec2& value) const noexcept
245 {
246  glUniform2uiv(gl_uniform_location, 1, value.data());
247 }
248 
249 void gl_shader_uvec2::update(const math::uvec2& value, std::size_t index) const
250 {
251  glUniform2uiv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
252 }
253 
254 void gl_shader_uvec2::update(std::span<const math::uvec2> values, std::size_t index) const
255 {
256  glUniform2uiv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
257 }
258 
259 gl_shader_uvec3::gl_shader_uvec3(std::size_t size, GLint gl_uniform_location):
260  shader_variable(size),
261  gl_uniform_location{gl_uniform_location}
262 {}
263 
264 void gl_shader_uvec3::update(const math::uvec3& value) const noexcept
265 {
266  glUniform3uiv(gl_uniform_location, 1, value.data());
267 }
268 
269 void gl_shader_uvec3::update(const math::uvec3& value, std::size_t index) const
270 {
271  glUniform3uiv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
272 }
273 
274 void gl_shader_uvec3::update(std::span<const math::uvec3> values, std::size_t index) const
275 {
276  glUniform3uiv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
277 }
278 
279 gl_shader_uvec4::gl_shader_uvec4(std::size_t size, GLint gl_uniform_location):
280  shader_variable(size),
281  gl_uniform_location{gl_uniform_location}
282 {}
283 
284 void gl_shader_uvec4::update(const math::uvec4& value) const noexcept
285 {
286  glUniform4uiv(gl_uniform_location, 1, value.data());
287 }
288 
289 void gl_shader_uvec4::update(const math::uvec4& value, std::size_t index) const
290 {
291  glUniform4uiv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
292 }
293 
294 void gl_shader_uvec4::update(std::span<const math::uvec4> values, std::size_t index) const
295 {
296  glUniform4uiv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
297 }
298 
299 gl_shader_float::gl_shader_float(std::size_t size, GLint gl_uniform_location):
300  shader_variable(size),
301  gl_uniform_location{gl_uniform_location}
302 {}
303 
304 void gl_shader_float::update(float value) const noexcept
305 {
306  glUniform1f(gl_uniform_location, value);
307 }
308 
309 void gl_shader_float::update(float value, std::size_t index) const
310 {
311  glUniform1f(gl_uniform_location + static_cast<GLint>(index), value);
312 }
313 
314 void gl_shader_float::update(std::span<const float> values, std::size_t index) const
315 {
316  glUniform1fv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data());
317 }
318 
319 gl_shader_fvec2::gl_shader_fvec2(std::size_t size, GLint gl_uniform_location):
320  shader_variable(size),
321  gl_uniform_location{gl_uniform_location}
322 {}
323 
324 void gl_shader_fvec2::update(const math::fvec2& value) const noexcept
325 {
326  glUniform2fv(gl_uniform_location, 1, value.data());
327 }
328 
329 void gl_shader_fvec2::update(const math::fvec2& value, std::size_t index) const
330 {
331  glUniform2fv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
332 }
333 
334 void gl_shader_fvec2::update(std::span<const math::fvec2> values, std::size_t index) const
335 {
336  glUniform2fv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
337 }
338 
339 gl_shader_fvec3::gl_shader_fvec3(std::size_t size, GLint gl_uniform_location):
340  shader_variable(size),
341  gl_uniform_location{gl_uniform_location}
342 {}
343 
344 void gl_shader_fvec3::update(const math::fvec3& value) const noexcept
345 {
346  glUniform3fv(gl_uniform_location, 1, value.data());
347 }
348 
349 void gl_shader_fvec3::update(const math::fvec3& value, std::size_t index) const
350 {
351  glUniform3fv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
352 }
353 
354 void gl_shader_fvec3::update(std::span<const math::fvec3> values, std::size_t index) const
355 {
356  glUniform3fv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
357 }
358 
359 gl_shader_fvec4::gl_shader_fvec4(std::size_t size, GLint gl_uniform_location):
360  shader_variable(size),
361  gl_uniform_location{gl_uniform_location}
362 {}
363 
364 void gl_shader_fvec4::update(const math::fvec4& value) const noexcept
365 {
366  glUniform4fv(gl_uniform_location, 1, value.data());
367 }
368 
369 void gl_shader_fvec4::update(const math::fvec4& value, std::size_t index) const
370 {
371  glUniform4fv(gl_uniform_location + static_cast<GLint>(index), 1, value.data());
372 }
373 
374 void gl_shader_fvec4::update(std::span<const math::fvec4> values, std::size_t index) const
375 {
376  glUniform4fv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), values.data()->data());
377 }
378 
379 gl_shader_fmat2::gl_shader_fmat2(std::size_t size, GLint gl_uniform_location):
380  shader_variable(size),
381  gl_uniform_location{gl_uniform_location}
382 {}
383 
384 void gl_shader_fmat2::update(const math::fmat2& value) const noexcept
385 {
386  glUniformMatrix2fv(gl_uniform_location, 1, GL_FALSE, value.data());
387 }
388 
389 void gl_shader_fmat2::update(const math::fmat2& value, std::size_t index) const
390 {
391  glUniformMatrix2fv(gl_uniform_location + static_cast<GLint>(index) * 2, 1, GL_FALSE, value.data());
392 }
393 
394 void gl_shader_fmat2::update(std::span<const math::fmat2> values, std::size_t index) const
395 {
396  glUniformMatrix2fv(gl_uniform_location + static_cast<GLint>(index) * 2, static_cast<GLsizei>(values.size()), GL_FALSE, values.data()->data());
397 }
398 
399 gl_shader_fmat3::gl_shader_fmat3(std::size_t size, GLint gl_uniform_location):
400  shader_variable(size),
401  gl_uniform_location{gl_uniform_location}
402 {}
403 
404 void gl_shader_fmat3::update(const math::fmat3& value) const noexcept
405 {
406  glUniformMatrix3fv(gl_uniform_location, 1, GL_FALSE, value.data());
407 }
408 
409 void gl_shader_fmat3::update(const math::fmat3& value, std::size_t index) const
410 {
411  glUniformMatrix3fv(gl_uniform_location + static_cast<GLint>(index) * 3, 1, GL_FALSE, value.data());
412 }
413 
414 void gl_shader_fmat3::update(std::span<const math::fmat3> values, std::size_t index) const
415 {
416  glUniformMatrix3fv(gl_uniform_location + static_cast<GLint>(index) * 3, static_cast<GLsizei>(values.size()), GL_FALSE, values.data()->data());
417 }
418 
419 gl_shader_fmat4::gl_shader_fmat4(std::size_t size, GLint gl_uniform_location):
420  shader_variable(size),
421  gl_uniform_location{gl_uniform_location}
422 {}
423 
424 void gl_shader_fmat4::update(const math::fmat4& value) const noexcept
425 {
426  glUniformMatrix4fv(gl_uniform_location, 1, GL_FALSE, value.data());
427 }
428 
429 void gl_shader_fmat4::update(const math::fmat4& value, std::size_t index) const
430 {
431  glUniformMatrix4fv(gl_uniform_location + static_cast<GLint>(index) * 4, 1, GL_FALSE, value.data());
432 }
433 
434 void gl_shader_fmat4::update(std::span<const math::fmat4> values, std::size_t index) const
435 {
436  glUniformMatrix4fv(gl_uniform_location + static_cast<GLint>(index) * 4, static_cast<GLsizei>(values.size()), GL_FALSE, values.data()->data());
437 }
438 
439 gl_shader_texture_1d::gl_shader_texture_1d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_unit_index):
440  shader_variable(size),
441  gl_uniform_location{gl_uniform_location},
442  gl_texture_unit_indices(size)
443 {
444  std::iota(gl_texture_unit_indices.begin(), gl_texture_unit_indices.end(), gl_first_texture_unit_index);
445 }
446 
447 void gl_shader_texture_1d::update(const texture_1d& value) const noexcept
448 {
449  // Bind texture to texture unit
450  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices.front()), value.get_image_view() ? value.get_image_view()->m_gl_texture_name : 0);
451  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices.front()), value.get_sampler() ? value.get_sampler()->m_gl_named_sampler : 0);
452 
453  // Pass texture unit index to shader
454  glUniform1i(gl_uniform_location, gl_texture_unit_indices.front());
455 }
456 
457 void gl_shader_texture_1d::update(const texture_1d& value, std::size_t index) const
458 {
459  const GLint gl_texture_index = gl_texture_unit_indices[index];
460 
461  // Bind texture to texture unit
462  glBindTextureUnit(static_cast<GLuint>(gl_texture_index), value.get_image_view() ? value.get_image_view()->m_gl_texture_name : 0);
463  glBindSampler(static_cast<GLuint>(gl_texture_index), value.get_sampler() ? value.get_sampler()->m_gl_named_sampler : 0);
464 
465  // Pass texture unit index to shader
466  glUniform1i(gl_uniform_location + static_cast<GLint>(index), gl_texture_index);
467 }
468 
469 void gl_shader_texture_1d::update(std::span<const texture_1d* const> values, std::size_t index) const
470 {
471  // Bind textures
472  for (std::size_t i = 0; i < values.size(); ++i)
473  {
474  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_image_view() ? values[i]->get_image_view()->m_gl_texture_name : 0);
475  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_sampler() ? values[i]->get_sampler()->m_gl_named_sampler : 0);
476  }
477 
478  // Pass texture unit indices to shader
479  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), &gl_texture_unit_indices[index]);
480 }
481 
482 void gl_shader_texture_1d::update(std::span<const std::shared_ptr<texture_1d>> values, std::size_t index) const
483 {
484  // Bind textures
485  for (std::size_t i = 0; i < values.size(); ++i)
486  {
487  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_image_view() ? values[i]->get_image_view()->m_gl_texture_name : 0);
488  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_sampler() ? values[i]->get_sampler()->m_gl_named_sampler : 0);
489  }
490 
491  // Pass texture unit indices to shader
492  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), &gl_texture_unit_indices[index]);
493 }
494 
495 gl_shader_texture_2d::gl_shader_texture_2d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_unit_index):
496  shader_variable(size),
497  gl_uniform_location{gl_uniform_location},
498  gl_texture_unit_indices(size)
499 {
500  std::iota(gl_texture_unit_indices.begin(), gl_texture_unit_indices.end(), gl_first_texture_unit_index);
501 }
502 
503 void gl_shader_texture_2d::update(const texture_2d& value) const noexcept
504 {
505  // Bind texture to texture unit
506  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices.front()), value.get_image_view() ? value.get_image_view()->m_gl_texture_name : 0);
507  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices.front()), value.get_sampler() ? value.get_sampler()->m_gl_named_sampler : 0);
508 
509  // Pass texture unit index to shader
510  glUniform1i(gl_uniform_location, gl_texture_unit_indices.front());
511 }
512 
513 void gl_shader_texture_2d::update(const texture_2d& value, std::size_t index) const
514 {
515  const GLint gl_texture_index = gl_texture_unit_indices[index];
516 
517  // Bind texture to texture unit
518  glBindTextureUnit(static_cast<GLuint>(gl_texture_index), value.get_image_view() ? value.get_image_view()->m_gl_texture_name : 0);
519  glBindSampler(static_cast<GLuint>(gl_texture_index), value.get_sampler() ? value.get_sampler()->m_gl_named_sampler : 0);
520 
521  // Pass texture unit index to shader
522  glUniform1i(gl_uniform_location + static_cast<GLint>(index), gl_texture_index);
523 }
524 
525 void gl_shader_texture_2d::update(std::span<const texture_2d* const> values, std::size_t index) const
526 {
527  // Bind textures
528  for (std::size_t i = 0; i < values.size(); ++i)
529  {
530  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_image_view() ? values[i]->get_image_view()->m_gl_texture_name : 0);
531  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_sampler() ? values[i]->get_sampler()->m_gl_named_sampler : 0);
532  }
533 
534  // Pass texture unit indices to shader
535  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), &gl_texture_unit_indices[index]);
536 }
537 
538 void gl_shader_texture_2d::update(std::span<const std::shared_ptr<texture_2d>> values, std::size_t index) const
539 {
540  // Bind textures
541  for (std::size_t i = 0; i < values.size(); ++i)
542  {
543  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_image_view() ? values[i]->get_image_view()->m_gl_texture_name : 0);
544  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_sampler() ? values[i]->get_sampler()->m_gl_named_sampler : 0);
545  }
546 
547  // Pass texture unit indices to shader
548  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), &gl_texture_unit_indices[index]);
549 }
550 
551 gl_shader_texture_3d::gl_shader_texture_3d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_unit_index):
552  shader_variable(size),
553  gl_uniform_location{gl_uniform_location},
554  gl_texture_unit_indices(size)
555 {
556  std::iota(gl_texture_unit_indices.begin(), gl_texture_unit_indices.end(), gl_first_texture_unit_index);
557 }
558 
559 void gl_shader_texture_3d::update(const texture_3d& value) const noexcept
560 {
561  // Bind texture to texture unit
562  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices.front()), value.get_image_view() ? value.get_image_view()->m_gl_texture_name : 0);
563  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices.front()), value.get_sampler() ? value.get_sampler()->m_gl_named_sampler : 0);
564 
565  // Pass texture unit index to shader
566  glUniform1i(gl_uniform_location, gl_texture_unit_indices.front());
567 }
568 
569 void gl_shader_texture_3d::update(const texture_3d& value, std::size_t index) const
570 {
571  const GLint gl_texture_index = gl_texture_unit_indices[index];
572 
573  // Bind texture to texture unit
574  glBindTextureUnit(static_cast<GLuint>(gl_texture_index), value.get_image_view() ? value.get_image_view()->m_gl_texture_name : 0);
575  glBindSampler(static_cast<GLuint>(gl_texture_index), value.get_sampler() ? value.get_sampler()->m_gl_named_sampler : 0);
576 
577  // Pass texture unit index to shader
578  glUniform1i(gl_uniform_location + static_cast<GLint>(index), gl_texture_index);
579 }
580 
581 void gl_shader_texture_3d::update(std::span<const texture_3d* const> values, std::size_t index) const
582 {
583  // Bind textures
584  for (std::size_t i = 0; i < values.size(); ++i)
585  {
586  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_image_view() ? values[i]->get_image_view()->m_gl_texture_name : 0);
587  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_sampler() ? values[i]->get_sampler()->m_gl_named_sampler : 0);
588  }
589 
590  // Pass texture unit indices to shader
591  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), &gl_texture_unit_indices[index]);
592 }
593 
594 void gl_shader_texture_3d::update(std::span<const std::shared_ptr<texture_3d>> values, std::size_t index) const
595 {
596  // Bind textures
597  for (std::size_t i = 0; i < values.size(); ++i)
598  {
599  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_image_view() ? values[i]->get_image_view()->m_gl_texture_name : 0);
600  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_sampler() ? values[i]->get_sampler()->m_gl_named_sampler : 0);
601  }
602 
603  // Pass texture unit indices to shader
604  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), &gl_texture_unit_indices[index]);
605 }
606 
607 gl_shader_texture_cube::gl_shader_texture_cube(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_unit_index):
608  shader_variable(size),
609  gl_uniform_location{gl_uniform_location},
610  gl_texture_unit_indices(size)
611 {
612  std::iota(gl_texture_unit_indices.begin(), gl_texture_unit_indices.end(), gl_first_texture_unit_index);
613 }
614 
615 void gl_shader_texture_cube::update(const texture_cube& value) const noexcept
616 {
617  // Bind texture to texture unit
618  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices.front()), value.get_image_view() ? value.get_image_view()->m_gl_texture_name : 0);
619  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices.front()), value.get_sampler() ? value.get_sampler()->m_gl_named_sampler : 0);
620 
621  // Pass texture unit index to shader
622  glUniform1i(gl_uniform_location, gl_texture_unit_indices.front());
623 }
624 
625 void gl_shader_texture_cube::update(const texture_cube& value, std::size_t index) const
626 {
627  const GLint gl_texture_index = gl_texture_unit_indices[index];
628 
629  // Bind texture to texture unit
630  glBindTextureUnit(static_cast<GLuint>(gl_texture_index), value.get_image_view() ? value.get_image_view()->m_gl_texture_name : 0);
631  glBindSampler(static_cast<GLuint>(gl_texture_index), value.get_sampler() ? value.get_sampler()->m_gl_named_sampler : 0);
632 
633  // Pass texture unit index to shader
634  glUniform1i(gl_uniform_location + static_cast<GLint>(index), gl_texture_index);
635 }
636 
637 void gl_shader_texture_cube::update(std::span<const texture_cube* const> values, std::size_t index) const
638 {
639  // Bind textures
640  for (std::size_t i = 0; i < values.size(); ++i)
641  {
642  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_image_view() ? values[i]->get_image_view()->m_gl_texture_name : 0);
643  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_sampler() ? values[i]->get_sampler()->m_gl_named_sampler : 0);
644  }
645 
646  // Pass texture unit indices to shader
647  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), &gl_texture_unit_indices[index]);
648 }
649 
650 void gl_shader_texture_cube::update(std::span<const std::shared_ptr<texture_cube>> values, std::size_t index) const
651 {
652  // Bind textures
653  for (std::size_t i = 0; i < values.size(); ++i)
654  {
655  glBindTextureUnit(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_image_view() ? values[i]->get_image_view()->m_gl_texture_name : 0);
656  glBindSampler(static_cast<GLuint>(gl_texture_unit_indices[index + i]), values[i]->get_sampler() ? values[i]->get_sampler()->m_gl_named_sampler : 0);
657  }
658 
659  // Pass texture unit indices to shader
660  glUniform1iv(gl_uniform_location + static_cast<GLint>(index), static_cast<GLsizei>(values.size()), &gl_texture_unit_indices[index]);
661 }
662 
663 } // namespace gl
void update(bool value) const noexcept override
Updates the value of the variable.
gl_shader_bool(std::size_t size, GLint gl_uniform_location)
gl_shader_bvec2(std::size_t size, GLint gl_uniform_location)
void update(const math::bvec2 &value) const noexcept override
Updates the value of the variable.
void update(const math::bvec3 &value) const noexcept override
Updates the value of the variable.
gl_shader_bvec3(std::size_t size, GLint gl_uniform_location)
void update(const math::bvec4 &value) const noexcept override
Updates the value of the variable.
gl_shader_bvec4(std::size_t size, GLint gl_uniform_location)
gl_shader_float(std::size_t size, GLint gl_uniform_location)
void update(float value) const noexcept override
Updates the value of the variable.
void update(const math::fmat2 &value) const noexcept override
Updates the value of the variable.
gl_shader_fmat2(std::size_t size, GLint gl_uniform_location)
void update(const math::fmat3 &value) const noexcept override
Updates the value of the variable.
gl_shader_fmat3(std::size_t size, GLint gl_uniform_location)
void update(const math::fmat4 &value) const noexcept override
Updates the value of the variable.
gl_shader_fmat4(std::size_t size, GLint gl_uniform_location)
void update(const math::fvec2 &value) const noexcept override
Updates the value of the variable.
gl_shader_fvec2(std::size_t size, GLint gl_uniform_location)
void update(const math::fvec3 &value) const noexcept override
Updates the value of the variable.
gl_shader_fvec3(std::size_t size, GLint gl_uniform_location)
gl_shader_fvec4(std::size_t size, GLint gl_uniform_location)
void update(const math::fvec4 &value) const noexcept override
Updates the value of the variable.
void update(int value) const noexcept override
Updates the value of the variable.
gl_shader_int(std::size_t size, GLint gl_uniform_location)
void update(const math::ivec2 &value) const noexcept override
Updates the value of the variable.
gl_shader_ivec2(std::size_t size, GLint gl_uniform_location)
void update(const math::ivec3 &value) const noexcept override
Updates the value of the variable.
gl_shader_ivec3(std::size_t size, GLint gl_uniform_location)
void update(const math::ivec4 &value) const noexcept override
Updates the value of the variable.
gl_shader_ivec4(std::size_t size, GLint gl_uniform_location)
void update(const texture_1d &value) const noexcept override
Updates the value of the variable.
gl_shader_texture_1d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_index)
gl_shader_texture_2d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_unit_index)
void update(const texture_2d &value) const noexcept override
Updates the value of the variable.
void update(const texture_3d &value) const noexcept override
Updates the value of the variable.
gl_shader_texture_3d(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_index)
gl_shader_texture_cube(std::size_t size, GLint gl_uniform_location, GLint gl_first_texture_index)
void update(const texture_cube &value) const noexcept override
Updates the value of the variable.
void update(unsigned int value) const noexcept override
Updates the value of the variable.
gl_shader_uint(std::size_t size, GLint gl_uniform_location)
void update(const math::uvec2 &value) const noexcept override
Updates the value of the variable.
gl_shader_uvec2(std::size_t size, GLint gl_uniform_location)
gl_shader_uvec3(std::size_t size, GLint gl_uniform_location)
void update(const math::uvec3 &value) const noexcept override
Updates the value of the variable.
void update(const math::uvec4 &value) const noexcept override
Updates the value of the variable.
gl_shader_uvec4(std::size_t size, GLint gl_uniform_location)
Shader program variable.
1D texture.
Definition: texture.hpp:73
constexpr const std::shared_ptr< image_view_1d > & get_image_view() const noexcept
Returns the image view.
Definition: texture.hpp:94
2D texture.
Definition: texture.hpp:141
constexpr const std::shared_ptr< image_view_2d > & get_image_view() const noexcept
Returns the image view.
Definition: texture.hpp:162
3D texture.
Definition: texture.hpp:209
constexpr const std::shared_ptr< image_view_3d > & get_image_view() const noexcept
Returns the image view.
Definition: texture.hpp:230
Cube texture.
Definition: texture.hpp:243
constexpr const std::shared_ptr< image_view_cube > & get_image_view() const noexcept
Returns the image view.
Definition: texture.hpp:264
constexpr const std::shared_ptr< sampler > & get_sampler() const noexcept
Returns the sampler object.
Definition: texture.hpp:60
Graphics library interface.
Definition: window.hpp:28
n by m column-major matrix.
Definition: math/matrix.hpp:44
constexpr element_type * data() noexcept
Returns a pointer to the first element.
n-dimensional vector.
Definition: vector.hpp:44
constexpr element_type * data() noexcept
Returns a pointer to the element array.
Definition: vector.hpp:152