#include "RsxShaderUniforms.h" #include "RsxShaderProgram.h" #include "RsxBufferObject.h" #include namespace nCine::RHI::RSX { namespace { // Matches a name against the null-separated, double-null-terminated include/exclude lists exactly // like the OpenGL backend's uniform importers do bool ShouldImport(const char* name, const char* includeOnly, const char* exclude) { bool shouldImport = true; if (includeOnly != nullptr) { shouldImport = false; const char* current = includeOnly; while (current != nullptr && current[0] != '\0') { if (std::strcmp(current, name) == 0) { shouldImport = true; break; } current += std::strlen(current) + 1; } } if (exclude != nullptr) { const char* current = exclude; while (current != nullptr && current[0] != '\0') { if (std::strcmp(current, name) == 0) { shouldImport = false; break; } current += std::strlen(current) + 1; } } return shouldImport; } } RsxShaderUniforms::RsxShaderUniforms() : _shaderProgram(nullptr), _maybeDirty(true) { } RsxShaderUniforms::RsxShaderUniforms(RsxShaderProgram* shaderProgram) : RsxShaderUniforms() { SetProgram(shaderProgram, nullptr, nullptr); } RsxShaderUniforms::RsxShaderUniforms(RsxShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) : RsxShaderUniforms() { SetProgram(shaderProgram, includeOnly, exclude); } void RsxShaderUniforms::SetProgram(RsxShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) { _shaderProgram = shaderProgram; _uniformCaches.clear(); _maybeDirty = true; if (_shaderProgram->GetStatus() == RsxShaderProgram::Status::LinkedWithIntrospection) { ImportUniforms(includeOnly, exclude); } } void RsxShaderUniforms::SetUniformsDataPointer(std::uint8_t* dataPointer) { if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != RsxShaderProgram::Status::LinkedWithIntrospection) { return; } _maybeDirty = true; std::uint32_t offset = 0; for (RsxUniformCache& cache : _uniformCaches) { cache.SetDataPointer(dataPointer + offset); offset += cache.GetUniform()->GetMemorySize(); } } void RsxShaderUniforms::SetDirty(bool isDirty) { if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != RsxShaderProgram::Status::LinkedWithIntrospection) { return; } _maybeDirty = isDirty; for (RsxUniformCache& cache : _uniformCaches) { cache.SetDirty(isDirty); } } bool RsxShaderUniforms::HasUniform(const char* name) const { for (const RsxUniformCache& cache : _uniformCaches) { if (std::strcmp(cache.GetUniform()->GetName(), name) == 0) { return true; } } return false; } RsxUniformCache* RsxShaderUniforms::GetUniform(const char* name) { for (RsxUniformCache& cache : _uniformCaches) { if (std::strcmp(cache.GetUniform()->GetName(), name) == 0) { _maybeDirty = true; return &cache; } } return nullptr; } void RsxShaderUniforms::CommitUniforms() { if (_shaderProgram == nullptr) { return; } if (_maybeDirty && _shaderProgram->GetStatus() == RsxShaderProgram::Status::LinkedWithIntrospection) { _shaderProgram->Use(); for (RsxUniformCache& cache : _uniformCaches) { cache.CommitValue(); } _maybeDirty = false; } } void RsxShaderUniforms::ImportUniforms(const char* includeOnly, const char* exclude) { for (const RsxUniform& uniform : _shaderProgram->_uniforms) { if (ShouldImport(uniform.GetName(), includeOnly, exclude)) { _uniformCaches.push_back(RsxUniformCache(&uniform)); } } } // ------------------------------------------------------------------------------------------------- RsxShaderUniformBlocks::UniformRangeAllocator RsxShaderUniformBlocks::_uniformRangeAllocator = nullptr; void RsxShaderUniformBlocks::SetUniformRangeAllocator(UniformRangeAllocator allocator) { _uniformRangeAllocator = allocator; } RsxShaderUniformBlocks::RsxShaderUniformBlocks() : _shaderProgram(nullptr), _dataPointer(nullptr) { } RsxShaderUniformBlocks::RsxShaderUniformBlocks(RsxShaderProgram* shaderProgram) : RsxShaderUniformBlocks() { SetProgram(shaderProgram, nullptr, nullptr); } RsxShaderUniformBlocks::RsxShaderUniformBlocks(RsxShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) : RsxShaderUniformBlocks() { SetProgram(shaderProgram, includeOnly, exclude); } void RsxShaderUniformBlocks::SetProgram(RsxShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) { _shaderProgram = shaderProgram; _uniformBlockCaches.clear(); if (_shaderProgram->GetStatus() == RsxShaderProgram::Status::LinkedWithIntrospection) { ImportUniformBlocks(includeOnly, exclude); } } void RsxShaderUniformBlocks::SetUniformsDataPointer(std::uint8_t* dataPointer) { if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != RsxShaderProgram::Status::LinkedWithIntrospection) { return; } _dataPointer = dataPointer; std::int32_t offset = 0; for (RsxUniformBlockCache& cache : _uniformBlockCaches) { cache.SetDataPointer(dataPointer + offset); offset += cache.uniformBlock()->GetSize() - cache.uniformBlock()->GetAlignAmount(); } } bool RsxShaderUniformBlocks::HasUniformBlock(const char* name) const { for (const RsxUniformBlockCache& cache : _uniformBlockCaches) { if (std::strcmp(cache.uniformBlock()->GetName(), name) == 0) { return true; } } return false; } RsxUniformBlockCache* RsxShaderUniformBlocks::GetUniformBlock(const char* name) { for (RsxUniformBlockCache& cache : _uniformBlockCaches) { if (std::strcmp(cache.uniformBlock()->GetName(), name) == 0) { return &cache; } } return nullptr; } void RsxShaderUniformBlocks::CommitUniformBlocks() { if (_shaderProgram == nullptr || _shaderProgram->GetStatus() != RsxShaderProgram::Status::LinkedWithIntrospection) { return; } std::int32_t totalUsedSize = 0; bool hasMemoryGaps = false; for (RsxUniformBlockCache& cache : _uniformBlockCaches) { if (cache.GetDataPointer() != _dataPointer + totalUsedSize) { hasMemoryGaps = true; } totalUsedSize += cache.usedSize(); } if (totalUsedSize > 0 && _uniformRangeAllocator != nullptr) { _uboParams = _uniformRangeAllocator(std::uint32_t(totalUsedSize)); if (_uboParams.mapBase != nullptr) { if (hasMemoryGaps) { std::int32_t offset = 0; for (RsxUniformBlockCache& cache : _uniformBlockCaches) { std::memcpy(_uboParams.mapBase + _uboParams.offset + offset, cache.GetDataPointer(), cache.usedSize()); offset += cache.usedSize(); } } else { std::memcpy(_uboParams.mapBase + _uboParams.offset, _dataPointer, totalUsedSize); } } } } void RsxShaderUniformBlocks::Bind() { if (_uboParams.object == nullptr) { return; } _uboParams.object->Bind(); std::size_t moreOffset = 0; for (RsxUniformBlockCache& cache : _uniformBlockCaches) { cache.SetBlockBinding(std::int32_t(cache.GetIndex())); const std::size_t offset = std::size_t(_uboParams.offset) + moreOffset; _uboParams.object->BindBufferRange(std::uint32_t(cache.GetBindingIndex()), offset, std::size_t(cache.usedSize())); moreOffset += std::size_t(cache.usedSize()); } } void RsxShaderUniformBlocks::ImportUniformBlocks(const char* includeOnly, const char* exclude) { for (RsxUniformBlock& block : _shaderProgram->_uniformBlocks) { if (ShouldImport(block.GetName(), includeOnly, exclude)) { _uniformBlockCaches.push_back(RsxUniformBlockCache(&block)); } } } }