| 1 |
#include "GuShaderUniforms.h" |
| 2 |
#include "GuShaderProgram.h" |
| 3 |
#include "GuBuffer.h" |
| 4 |
|
| 5 |
#include <cstring> |
| 6 |
|
| 7 |
namespace nCine::RHI::GU |
| 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 |
GuShaderUniforms::GuShaderUniforms() |
| 42 |
: _shaderProgram(nullptr), _maybeDirty(true) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
GuShaderUniforms::GuShaderUniforms(GuShaderProgram* shaderProgram) |
| 47 |
: GuShaderUniforms() |
| 48 |
{ |
| 49 |
SetProgram(shaderProgram, nullptr, nullptr); |
| 50 |
} |
| 51 |
|
| 52 |
GuShaderUniforms::GuShaderUniforms(GuShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) |
| 53 |
: GuShaderUniforms() |
| 54 |
{ |
| 55 |
SetProgram(shaderProgram, includeOnly, exclude); |
| 56 |
} |
| 57 |
|
| 58 |
void GuShaderUniforms::SetProgram(GuShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) |
| 59 |
{ |
| 60 |
_shaderProgram = shaderProgram; |
| 61 |
_uniformCaches.clear(); |
| 62 |
_maybeDirty = true; |
| 63 |
|
| 64 |
if (_shaderProgram->GetStatus() == GuShaderProgram::Status::LinkedWithIntrospection) { |
| 65 |
ImportUniforms(includeOnly, exclude); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
void GuShaderUniforms::SetUniformsDataPointer(std::uint8_t* dataPointer) |
| 70 |
{ |
| 71 |
if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != GuShaderProgram::Status::LinkedWithIntrospection) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
_maybeDirty = true; |
| 76 |
std::uint32_t offset = 0; |
| 77 |
for (GuUniformCache& cache : _uniformCaches) { |
| 78 |
cache.SetDataPointer(dataPointer + offset); |
| 79 |
offset += cache.GetUniform()->GetMemorySize(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void GuShaderUniforms::SetDirty(bool isDirty) |
| 84 |
{ |
| 85 |
if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != GuShaderProgram::Status::LinkedWithIntrospection) { |
| 86 |
return; |
| 87 |
} |
| 88 |
_maybeDirty = isDirty; |
| 89 |
for (GuUniformCache& cache : _uniformCaches) { |
| 90 |
cache.SetDirty(isDirty); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
bool GuShaderUniforms::HasUniform(const char* name) const |
| 95 |
{ |
| 96 |
for (const GuUniformCache& cache : _uniformCaches) { |
| 97 |
if (std::strcmp(cache.GetUniform()->GetName(), name) == 0) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
} |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
GuUniformCache* GuShaderUniforms::GetUniform(const char* name) |
| 105 |
{ |
| 106 |
for (GuUniformCache& 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 GuShaderUniforms::CommitUniforms() |
| 116 |
{ |
| 117 |
if (_shaderProgram == nullptr) { |
| 118 |
return; |
| 119 |
} |
| 120 |
if (_maybeDirty && _shaderProgram->GetStatus() == GuShaderProgram::Status::LinkedWithIntrospection) { |
| 121 |
_shaderProgram->Use(); |
| 122 |
for (GuUniformCache& cache : _uniformCaches) { |
| 123 |
cache.CommitValue(); |
| 124 |
} |
| 125 |
_maybeDirty = false; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
void GuShaderUniforms::ImportUniforms(const char* includeOnly, const char* exclude) |
| 130 |
{ |
| 131 |
for (const GuUniform& uniform : _shaderProgram->_uniforms) { |
| 132 |
if (ShouldImport(uniform.GetName(), includeOnly, exclude)) { |
| 133 |
_uniformCaches.push_back(GuUniformCache(&uniform)); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// ------------------------------------------------------------------------------------------------- |
| 139 |
|
| 140 |
GuShaderUniformBlocks::UniformRangeAllocator GuShaderUniformBlocks::_uniformRangeAllocator = nullptr; |
| 141 |
|
| 142 |
void GuShaderUniformBlocks::SetUniformRangeAllocator(UniformRangeAllocator allocator) |
| 143 |
{ |
| 144 |
_uniformRangeAllocator = allocator; |
| 145 |
} |
| 146 |
|
| 147 |
GuShaderUniformBlocks::GuShaderUniformBlocks() |
| 148 |
: _shaderProgram(nullptr), _dataPointer(nullptr) |
| 149 |
{ |
| 150 |
} |
| 151 |
|
| 152 |
GuShaderUniformBlocks::GuShaderUniformBlocks(GuShaderProgram* shaderProgram) |
| 153 |
: GuShaderUniformBlocks() |
| 154 |
{ |
| 155 |
SetProgram(shaderProgram, nullptr, nullptr); |
| 156 |
} |
| 157 |
|
| 158 |
GuShaderUniformBlocks::GuShaderUniformBlocks(GuShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) |
| 159 |
: GuShaderUniformBlocks() |
| 160 |
{ |
| 161 |
SetProgram(shaderProgram, includeOnly, exclude); |
| 162 |
} |
| 163 |
|
| 164 |
void GuShaderUniformBlocks::SetProgram(GuShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) |
| 165 |
{ |
| 166 |
_shaderProgram = shaderProgram; |
| 167 |
_uniformBlockCaches.clear(); |
| 168 |
|
| 169 |
if (_shaderProgram->GetStatus() == GuShaderProgram::Status::LinkedWithIntrospection) { |
| 170 |
ImportUniformBlocks(includeOnly, exclude); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void GuShaderUniformBlocks::SetUniformsDataPointer(std::uint8_t* dataPointer) |
| 175 |
{ |
| 176 |
if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != GuShaderProgram::Status::LinkedWithIntrospection) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
_dataPointer = dataPointer; |
| 181 |
std::int32_t offset = 0; |
| 182 |
for (GuUniformBlockCache& cache : _uniformBlockCaches) { |
| 183 |
cache.SetDataPointer(dataPointer + offset); |
| 184 |
offset += cache.uniformBlock()->GetSize() - cache.uniformBlock()->GetAlignAmount(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
bool GuShaderUniformBlocks::HasUniformBlock(const char* name) const |
| 189 |
{ |
| 190 |
for (const GuUniformBlockCache& cache : _uniformBlockCaches) { |
| 191 |
if (std::strcmp(cache.uniformBlock()->GetName(), name) == 0) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
} |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
GuUniformBlockCache* GuShaderUniformBlocks::GetUniformBlock(const char* name) |
| 199 |
{ |
| 200 |
for (GuUniformBlockCache& cache : _uniformBlockCaches) { |
| 201 |
if (std::strcmp(cache.uniformBlock()->GetName(), name) == 0) { |
| 202 |
return &cache; |
| 203 |
} |
| 204 |
} |
| 205 |
return nullptr; |
| 206 |
} |
| 207 |
|
| 208 |
void GuShaderUniformBlocks::CommitUniformBlocks() |
| 209 |
{ |
| 210 |
if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != GuShaderProgram::Status::LinkedWithIntrospection) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
std::int32_t totalUsedSize = 0; |
| 215 |
bool hasMemoryGaps = false; |
| 216 |
for (GuUniformBlockCache& 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 (GuUniformBlockCache& 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 GuShaderUniformBlocks::Bind() |
| 240 |
{ |
| 241 |
if (_uboParams.object == nullptr) { |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
_uboParams.object->Bind(); |
| 246 |
std::size_t moreOffset = 0; |
| 247 |
for (GuUniformBlockCache& 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 GuShaderUniformBlocks::ImportUniformBlocks(const char* includeOnly, const char* exclude) |
| 256 |
{ |
| 257 |
for (GuUniformBlock& block : _shaderProgram->_uniformBlocks) { |
| 258 |
if (ShouldImport(block.GetName(), includeOnly, exclude)) { |
| 259 |
_uniformBlockCaches.push_back(GuUniformBlockCache(&block)); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
} |