Debian Codegraph @

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

raw
1 #include "GLShaderUniforms.h"
2 #include "GLShaderProgram.h"
3 #include "GLUniformCache.h"
4 #include "../../RenderResources.h"
5 #include "../../../Base/StaticHashMapIterator.h"
6 #include "../../../../Main.h"
7
8 namespace nCine::RHI::GL
9 {
10 GLShaderUniforms::GLShaderUniforms()
11 : _shaderProgram(nullptr), _maybeDirty(true)
12 {
13 }
14
15 GLShaderUniforms::GLShaderUniforms(GLShaderProgram* shaderProgram)
16 : GLShaderUniforms(shaderProgram, nullptr, nullptr)
17 {
18 SetProgram(shaderProgram);
19 }
20
21 GLShaderUniforms::GLShaderUniforms(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
22 : GLShaderUniforms()
23 {
24 SetProgram(shaderProgram, includeOnly, exclude);
25 }
26
27 void GLShaderUniforms::SetProgram(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
28 {
29 DEATH_ASSERT(shaderProgram != nullptr);
30
31 _shaderProgram = shaderProgram;
32 _shaderProgram->ProcessDeferredQueries();
33 _uniformCaches.clear();
34 _maybeDirty = true;
35
36 if (_shaderProgram->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) {
37 ImportUniforms(includeOnly, exclude);
38 }
39 }
40
41 void GLShaderUniforms::SetUniformsDataPointer(GLubyte* dataPointer)
42 {
43 DEATH_ASSERT(dataPointer != nullptr);
44
45 if (_shaderProgram->GetStatus() != GLShaderProgram::Status::LinkedWithIntrospection) {
46 return;
47 }
48
49 _maybeDirty = true;
50 std::uint32_t offset = 0;
51 for (GLUniformCache& uniformCache : _uniformCaches) {
52 uniformCache.SetDataPointer(dataPointer + offset);
53 offset += uniformCache.GetUniform()->GetMemorySize();
54 }
55 }
56
57 void GLShaderUniforms::SetDirty(bool isDirty)
58 {
59 if (_shaderProgram->GetStatus() != GLShaderProgram::Status::LinkedWithIntrospection) {
60 return;
61 }
62
63 _maybeDirty = isDirty;
64 for (auto& uniform : _uniformCaches) {
65 uniform.SetDirty(isDirty);
66 }
67 }
68
69 GLUniformCache* GLShaderUniforms::GetUniform(const char* name)
70 {
71 DEATH_ASSERT(name != nullptr);
72 GLUniformCache* uniformCache = nullptr;
73
74 if (_shaderProgram != nullptr) {
75 uniformCache = _uniformCaches.find(String::nullTerminatedView(name));
76 // The caller may write to the returned cache, so the commit early-out has to be pessimistic
77 if (uniformCache != nullptr) {
78 _maybeDirty = true;
79 }
80 } else {
81 LOGE("Cannot find uniform \"{}\", no shader program associated", name);
82 }
83 return uniformCache;
84 }
85
86 void GLShaderUniforms::CommitUniforms()
87 {
88 if (_shaderProgram != nullptr) {
89 if (_maybeDirty && _shaderProgram->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) {
90 _shaderProgram->Use();
91 for (auto& uniform : _uniformCaches) {
92 uniform.CommitValue();
93 }
94 _maybeDirty = false;
95 }
96 } else {
97 LOGE("No shader program associated");
98 }
99 }
100
101 void GLShaderUniforms::ImportUniforms(const char* includeOnly, const char* exclude)
102 {
103 constexpr std::uint32_t MaxUniformName = 128;
104
105 std::uint32_t importedCount = 0;
106 for (const GLUniform& uniform : _shaderProgram->_uniforms) {
107 const char* uniformName = uniform.GetName();
108 const char* currentIncludeOnly = includeOnly;
109 const char* currentExclude = exclude;
110 bool shouldImport = true;
111
112 if (includeOnly != nullptr) {
113 shouldImport = false;
114 while (currentIncludeOnly != nullptr && currentIncludeOnly[0] != '\0') {
115 if (strncmp(currentIncludeOnly, uniformName, MaxUniformName) == 0) {
116 shouldImport = true;
117 break;
118 }
119 currentIncludeOnly += strnlen(currentIncludeOnly, MaxUniformName) + 1;
120 }
121 }
122
123 if (exclude != nullptr) {
124 while (currentExclude != nullptr && currentExclude[0] != '\0') {
125 if (strncmp(currentExclude, uniformName, MaxUniformName) == 0) {
126 shouldImport = false;
127 break;
128 }
129 currentExclude += strnlen(currentExclude, MaxUniformName) + 1;
130 }
131 }
132
133 if (shouldImport) {
134 GLUniformCache uniformCache(&uniform);
135 _uniformCaches[uniformName] = uniformCache;
136 importedCount++;
137 }
138 }
139
140 if (importedCount > UniformCachesHashSize) {
141 LOGW("More imported uniform blocks ({}) than hashmap buckets ({})", importedCount, UniformCachesHashSize);
142 }
143 }
144 }