Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLUniformBlock.cpp

raw
1 #include "GLUniformBlock.h"
2 #include "GLShaderProgram.h"
3 #include "GLDebug.h"
4 #include "../IRhiCapabilities.h"
5 #include "../../../ServiceLocator.h"
6
7 #include <cstring>
8
9 namespace nCine::RHI::GL
10 {
11 GLUniformBlock::GLUniformBlock()
12 : _program(0), _index(0), _size(0), _alignAmount(0), _bindingIndex(-1)
13 {
14 _name[0] = '\0';
15 }
16
17 GLUniformBlock::GLUniformBlock(GLuint program, GLuint index, DiscoverUniforms discover)
18 : GLUniformBlock()
19 {
20 #if defined(RHI_GL_PROFILE_ES2)
21 // Uniform buffer objects are unused under the OpenGL|ES 2.0 profile, whose reflection path never
22 // invokes this discovery constructor (DiscoverUniformBlocks() is a no-op there). The block-introspection
23 // entry points (glGetActiveUniformBlock*/glGetActiveUniformsiv) and their GL_UNIFORM_BLOCK_* enums are
24 // ES 3.0 and strict ES 2.0 headers such as vitaGL's declare none of them, so leave the members at their
25 // default-constructed values.
26 static_cast<void>(program);
27 static_cast<void>(index);
28 static_cast<void>(discover);
29 #else
30 GLint nameLength = 0;
31 GLint uniformCount = 0;
32 _program = program;
33 _index = index;
34
35 glGetActiveUniformBlockiv(program, index, GL_UNIFORM_BLOCK_DATA_SIZE, &_size);
36 glGetActiveUniformBlockiv(program, index, GL_UNIFORM_BLOCK_NAME_LENGTH, &nameLength);
37 DEATH_ASSERT(nameLength <= MaxNameLength);
38 glGetActiveUniformBlockName(program, index, MaxNameLength, &nameLength, _name);
39 glGetActiveUniformBlockiv(program, index, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &uniformCount);
40
41 if (discover == DiscoverUniforms::Enabled && uniformCount > 0) {
42 DEATH_ASSERT(uniformCount <= MaxNumBlockUniforms);
43 GLuint uniformIndices[MaxNumBlockUniforms];
44 GLint uniformTypes[MaxNumBlockUniforms];
45 GLint uniformSizes[MaxNumBlockUniforms];
46 GLint uniformOffsets[MaxNumBlockUniforms];
47 GLint uniformNameLengths[MaxNumBlockUniforms];
48
49 GLint uniformQueryIndices[MaxNumBlockUniforms];
50 glGetActiveUniformBlockiv(program, index, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, uniformQueryIndices);
51 for (std::int32_t i = 0; i < uniformCount; i++)
52 uniformIndices[i] = static_cast<GLuint>(uniformQueryIndices[i]);
53
54 glGetActiveUniformsiv(program, uniformCount, uniformIndices, GL_UNIFORM_TYPE, uniformTypes);
55 glGetActiveUniformsiv(program, uniformCount, uniformIndices, GL_UNIFORM_SIZE, uniformSizes);
56 glGetActiveUniformsiv(program, uniformCount, uniformIndices, GL_UNIFORM_OFFSET, uniformOffsets);
57 #if !defined(DEATH_TARGET_EMSCRIPTEN)
58 glGetActiveUniformsiv(program, uniformCount, uniformIndices, GL_UNIFORM_NAME_LENGTH, uniformNameLengths);
59 #endif
60
61 for (std::int32_t i = 0; i < uniformCount; i++) {
62 GLUniform blockUniform;
63 blockUniform._index = uniformIndices[i];
64 blockUniform._blockIndex = static_cast<GLint>(index);
65 blockUniform._type = static_cast<GLenum>(uniformTypes[i]);
66 blockUniform._size = uniformSizes[i];
67 blockUniform._offset = uniformOffsets[i];
68
69 #if !defined(DEATH_TARGET_EMSCRIPTEN)
70 DEATH_ASSERT(uniformNameLengths[i] <= GLUniform::MaxNameLength,
71 ("Uniform {} name length is {}, which is more than {}", i, uniformNameLengths[i], GLUniform::MaxNameLength), );
72 #endif
73
74 #if defined(RHI_GL_PROFILE_CORE) && !defined(DEATH_TARGET_EMSCRIPTEN)
75 glGetActiveUniformName(program, uniformIndices[i], MaxNameLength, &uniformNameLengths[i], blockUniform._name);
76 #else
77 // Some drivers do not accept a `nullptr` for size and type
78 GLint unusedSize;
79 GLenum unusedType;
80 glGetActiveUniform(program, uniformIndices[i], MaxNameLength, &uniformNameLengths[i], &unusedSize, &unusedType, blockUniform._name);
81 #endif
82 _blockUniforms[blockUniform._name] = blockUniform;
83 }
84 }
85
86 GL_LOG_ERRORS();
87
88 // Align to the uniform buffer offset alignment or `glBindBufferRange()` will generate an `INVALID_VALUE` error
89 static const std::int32_t offsetAlignment = theServiceLocator().GetRhiCapabilities().GetValue(IRhiCapabilities::IntValues::UniformBufferOffsetAlignment);
90 _alignAmount = (offsetAlignment - _size % offsetAlignment) % offsetAlignment;
91 _size += _alignAmount;
92 #endif
93 }
94
95 GLUniformBlock::GLUniformBlock(GLuint program, GLuint index)
96 : GLUniformBlock(program, index, DiscoverUniforms::Enabled)
97 {
98 }
99
100 GLUniformBlock::GLUniformBlock(GLuint program, const char* name, GLuint index, GLint dataSize)
101 : GLUniformBlock()
102 {
103 _program = program;
104 _index = index;
105 _size = dataSize;
106
107 std::size_t length = strnlen(name, MaxNameLength);
108 DEATH_ASSERT(length < MaxNameLength);
109 std::memcpy(_name, name, length);
110 _name[length] = '\0';
111
112 // Align to the uniform buffer offset alignment or `glBindBufferRange()` will generate an `INVALID_VALUE` error
113 static const std::int32_t offsetAlignment = theServiceLocator().GetRhiCapabilities().GetValue(IRhiCapabilities::IntValues::UniformBufferOffsetAlignment);
114 _alignAmount = (offsetAlignment - _size % offsetAlignment) % offsetAlignment;
115 _size += _alignAmount;
116 }
117
118 void GLUniformBlock::SetBlockBinding(GLuint blockBinding)
119 {
120 #if defined(RHI_GL_PROFILE_ES2)
121 // glUniformBlockBinding is ES 3.0; the OpenGL|ES 2.0 profile has no uniform blocks to bind, so this is
122 // never called (block members are pushed as loose uniforms)
123 static_cast<void>(blockBinding);
124 #else
125 DEATH_ASSERT(_program != 0);
126
127 if (_bindingIndex != static_cast<GLint>(blockBinding)) {
128 glUniformBlockBinding(_program, _index, blockBinding);
129 GL_LOG_ERRORS();
130 _bindingIndex = static_cast<GLint>(blockBinding);
131 }
132 #endif
133 }
134 }