Debian Codegraph @

Sources/nCine/Graphics/RHI/Vulkan/VulkanShaderUniforms.cpp

raw
1 #include "VulkanShaderUniforms.h"
2 #include "VulkanShaderProgram.h"
3 #include "VulkanBufferObject.h"
4
5 #include <cstring>
6
7 namespace nCine::RHI::Vulkan
8 {
9 namespace
10 {
11 // Matches a name against the null-separated, double-null-terminated include/exclude lists exactly
12 // like the OpenGL backend's uniform importers do
13 bool ShouldImport(const char* name, const char* includeOnly, const char* exclude)
14 {
15 bool shouldImport = true;
16 if (includeOnly != nullptr) {
17 shouldImport = false;
18 const char* current = includeOnly;
19 while (current != nullptr && current[0] != '\0') {
20 if (std::strcmp(current, name) == 0) {
21 shouldImport = true;
22 break;
23 }
24 current += std::strlen(current) + 1;
25 }
26 }
27 if (exclude != nullptr) {
28 const char* current = exclude;
29 while (current != nullptr && current[0] != '\0') {
30 if (std::strcmp(current, name) == 0) {
31 shouldImport = false;
32 break;
33 }
34 current += std::strlen(current) + 1;
35 }
36 }
37 return shouldImport;
38 }
39 }
40
41 VulkanShaderUniforms::VulkanShaderUniforms()
42 : _shaderProgram(nullptr), _maybeDirty(true)
43 {
44 }
45
46 VulkanShaderUniforms::VulkanShaderUniforms(VulkanShaderProgram* shaderProgram)
47 : VulkanShaderUniforms()
48 {
49 SetProgram(shaderProgram, nullptr, nullptr);
50 }
51
52 VulkanShaderUniforms::VulkanShaderUniforms(VulkanShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
53 : VulkanShaderUniforms()
54 {
55 SetProgram(shaderProgram, includeOnly, exclude);
56 }
57
58 void VulkanShaderUniforms::SetProgram(VulkanShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
59 {
60 _shaderProgram = shaderProgram;
61 _uniformCaches.clear();
62 _maybeDirty = true;
63
64 if (_shaderProgram->GetStatus() == VulkanShaderProgram::Status::LinkedWithIntrospection) {
65 ImportUniforms(includeOnly, exclude);
66 }
67 }
68
69 void VulkanShaderUniforms::SetUniformsDataPointer(std::uint8_t* dataPointer)
70 {
71 if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != VulkanShaderProgram::Status::LinkedWithIntrospection) {
72 return;
73 }
74
75 _maybeDirty = true;
76 std::uint32_t offset = 0;
77 for (VulkanUniformCache& cache : _uniformCaches) {
78 cache.SetDataPointer(dataPointer + offset);
79 offset += cache.GetUniform()->GetMemorySize();
80 }
81 }
82
83 void VulkanShaderUniforms::SetDirty(bool isDirty)
84 {
85 if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != VulkanShaderProgram::Status::LinkedWithIntrospection) {
86 return;
87 }
88 _maybeDirty = isDirty;
89 for (VulkanUniformCache& cache : _uniformCaches) {
90 cache.SetDirty(isDirty);
91 }
92 }
93
94 bool VulkanShaderUniforms::HasUniform(const char* name) const
95 {
96 for (const VulkanUniformCache& cache : _uniformCaches) {
97 if (std::strcmp(cache.GetUniform()->GetName(), name) == 0) {
98 return true;
99 }
100 }
101 return false;
102 }
103
104 VulkanUniformCache* VulkanShaderUniforms::GetUniform(const char* name)
105 {
106 for (VulkanUniformCache& cache : _uniformCaches) {
107 if (std::strcmp(cache.GetUniform()->GetName(), name) == 0) {
108 _maybeDirty = true;
109 return &cache;
110 }
111 }
112 return nullptr;
113 }
114
115 void VulkanShaderUniforms::CommitUniforms()
116 {
117 if (_shaderProgram == nullptr) {
118 return;
119 }
120 if (_maybeDirty && _shaderProgram->GetStatus() == VulkanShaderProgram::Status::LinkedWithIntrospection) {
121 _shaderProgram->Use();
122 for (VulkanUniformCache& cache : _uniformCaches) {
123 cache.CommitValue();
124 }
125 _maybeDirty = false;
126 }
127 }
128
129 void VulkanShaderUniforms::ImportUniforms(const char* includeOnly, const char* exclude)
130 {
131 for (const VulkanUniform& uniform : _shaderProgram->_uniforms) {
132 if (ShouldImport(uniform.GetName(), includeOnly, exclude)) {
133 _uniformCaches.push_back(VulkanUniformCache(&uniform));
134 }
135 }
136 }
137
138 // -------------------------------------------------------------------------------------------------
139
140 VulkanShaderUniformBlocks::UniformRangeAllocator VulkanShaderUniformBlocks::_uniformRangeAllocator = nullptr;
141
142 void VulkanShaderUniformBlocks::SetUniformRangeAllocator(UniformRangeAllocator allocator)
143 {
144 _uniformRangeAllocator = allocator;
145 }
146
147 VulkanShaderUniformBlocks::VulkanShaderUniformBlocks()
148 : _shaderProgram(nullptr), _dataPointer(nullptr)
149 {
150 }
151
152 VulkanShaderUniformBlocks::VulkanShaderUniformBlocks(VulkanShaderProgram* shaderProgram)
153 : VulkanShaderUniformBlocks()
154 {
155 SetProgram(shaderProgram, nullptr, nullptr);
156 }
157
158 VulkanShaderUniformBlocks::VulkanShaderUniformBlocks(VulkanShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
159 : VulkanShaderUniformBlocks()
160 {
161 SetProgram(shaderProgram, includeOnly, exclude);
162 }
163
164 void VulkanShaderUniformBlocks::SetProgram(VulkanShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
165 {
166 _shaderProgram = shaderProgram;
167 _uniformBlockCaches.clear();
168
169 if (_shaderProgram->GetStatus() == VulkanShaderProgram::Status::LinkedWithIntrospection) {
170 ImportUniformBlocks(includeOnly, exclude);
171 }
172 }
173
174 void VulkanShaderUniformBlocks::SetUniformsDataPointer(std::uint8_t* dataPointer)
175 {
176 if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != VulkanShaderProgram::Status::LinkedWithIntrospection) {
177 return;
178 }
179
180 _dataPointer = dataPointer;
181 std::int32_t offset = 0;
182 for (VulkanUniformBlockCache& cache : _uniformBlockCaches) {
183 cache.SetDataPointer(dataPointer + offset);
184 offset += cache.uniformBlock()->GetSize() - cache.uniformBlock()->GetAlignAmount();
185 }
186 }
187
188 bool VulkanShaderUniformBlocks::HasUniformBlock(const char* name) const
189 {
190 for (const VulkanUniformBlockCache& cache : _uniformBlockCaches) {
191 if (std::strcmp(cache.uniformBlock()->GetName(), name) == 0) {
192 return true;
193 }
194 }
195 return false;
196 }
197
198 VulkanUniformBlockCache* VulkanShaderUniformBlocks::GetUniformBlock(const char* name)
199 {
200 for (VulkanUniformBlockCache& cache : _uniformBlockCaches) {
201 if (std::strcmp(cache.uniformBlock()->GetName(), name) == 0) {
202 return &cache;
203 }
204 }
205 return nullptr;
206 }
207
208 void VulkanShaderUniformBlocks::CommitUniformBlocks()
209 {
210 if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != VulkanShaderProgram::Status::LinkedWithIntrospection) {
211 return;
212 }
213
214 std::int32_t totalUsedSize = 0;
215 bool hasMemoryGaps = false;
216 for (VulkanUniformBlockCache& cache : _uniformBlockCaches) {
217 if (cache.GetDataPointer() != _dataPointer + totalUsedSize) {
218 hasMemoryGaps = true;
219 }
220 totalUsedSize += cache.usedSize();
221 }
222
223 if (totalUsedSize > 0 && _uniformRangeAllocator != nullptr) {
224 _uboParams = _uniformRangeAllocator(std::uint32_t(totalUsedSize));
225 if (_uboParams.mapBase != nullptr) {
226 if (hasMemoryGaps) {
227 std::int32_t offset = 0;
228 for (VulkanUniformBlockCache& cache : _uniformBlockCaches) {
229 std::memcpy(_uboParams.mapBase + _uboParams.offset + offset, cache.GetDataPointer(), cache.usedSize());
230 offset += cache.usedSize();
231 }
232 } else {
233 std::memcpy(_uboParams.mapBase + _uboParams.offset, _dataPointer, totalUsedSize);
234 }
235 }
236 }
237 }
238
239 void VulkanShaderUniformBlocks::Bind()
240 {
241 if (_uboParams.object == nullptr) {
242 return;
243 }
244
245 _uboParams.object->Bind();
246 std::size_t moreOffset = 0;
247 for (VulkanUniformBlockCache& cache : _uniformBlockCaches) {
248 cache.SetBlockBinding(std::int32_t(cache.GetIndex()));
249 const std::size_t offset = std::size_t(_uboParams.offset) + moreOffset;
250 _uboParams.object->BindBufferRange(std::uint32_t(cache.GetBindingIndex()), offset, std::size_t(cache.usedSize()));
251 moreOffset += std::size_t(cache.usedSize());
252 }
253 }
254
255 void VulkanShaderUniformBlocks::ImportUniformBlocks(const char* includeOnly, const char* exclude)
256 {
257 for (VulkanUniformBlock& block : _shaderProgram->_uniformBlocks) {
258 if (ShouldImport(block.GetName(), includeOnly, exclude)) {
259 _uniformBlockCaches.push_back(VulkanUniformBlockCache(&block));
260 }
261 }
262 }
263 }