#include "RsxUniformCache.h" #include "RsxShaderProgram.h" #include namespace nCine::RHI::RSX { const float* RsxUniformCache::GetFloatVector() const { return (_dataPointer != nullptr ? reinterpret_cast(_dataPointer) : nullptr); } float RsxUniformCache::GetFloatValue(std::uint32_t index) const { return (_dataPointer != nullptr ? reinterpret_cast(_dataPointer)[index] : 0.0f); } const std::int32_t* RsxUniformCache::GetIntVector() const { return (_dataPointer != nullptr ? reinterpret_cast(_dataPointer) : nullptr); } std::int32_t RsxUniformCache::GetIntValue(std::uint32_t index) const { return (_dataPointer != nullptr ? reinterpret_cast(_dataPointer)[index] : 0); } bool RsxUniformCache::SetFloatVector(const float* vec) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat()) { return false; } _isDirty = true; std::memcpy(_dataPointer, vec, sizeof(float) * _uniform->GetComponentCount()); return true; } bool RsxUniformCache::SetFloatValue(float v0) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(1)) { return false; } _isDirty = true; reinterpret_cast(_dataPointer)[0] = v0; return true; } bool RsxUniformCache::SetFloatValue(float v0, float v1) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(2)) { return false; } _isDirty = true; float* data = reinterpret_cast(_dataPointer); data[0] = v0; data[1] = v1; return true; } bool RsxUniformCache::SetFloatValue(float v0, float v1, float v2) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(3)) { return false; } _isDirty = true; float* data = reinterpret_cast(_dataPointer); data[0] = v0; data[1] = v1; data[2] = v2; return true; } bool RsxUniformCache::SetFloatValue(float v0, float v1, float v2, float v3) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckFloat() || !CheckComponents(4)) { return false; } _isDirty = true; float* data = reinterpret_cast(_dataPointer); data[0] = v0; data[1] = v1; data[2] = v2; data[3] = v3; return true; } bool RsxUniformCache::SetIntVector(const std::int32_t* vec) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt()) { return false; } _isDirty = true; std::memcpy(_dataPointer, vec, sizeof(std::int32_t) * _uniform->GetComponentCount()); return true; } bool RsxUniformCache::SetIntValue(std::int32_t v0) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(1)) { return false; } _isDirty = true; reinterpret_cast(_dataPointer)[0] = v0; return true; } bool RsxUniformCache::SetIntValue(std::int32_t v0, std::int32_t v1) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(2)) { return false; } _isDirty = true; std::int32_t* data = reinterpret_cast(_dataPointer); data[0] = v0; data[1] = v1; return true; } bool RsxUniformCache::SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(3)) { return false; } _isDirty = true; std::int32_t* data = reinterpret_cast(_dataPointer); data[0] = v0; data[1] = v1; data[2] = v2; return true; } bool RsxUniformCache::SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2, std::int32_t v3) { if (_uniform == nullptr || _dataPointer == nullptr || !CheckInt() || !CheckComponents(4)) { return false; } _isDirty = true; std::int32_t* data = reinterpret_cast(_dataPointer); data[0] = v0; data[1] = v1; data[2] = v2; data[3] = v3; return true; } bool RsxUniformCache::CommitValue() { if (_uniform == nullptr || _dataPointer == nullptr || !_isDirty) { return false; } // A uniform inside a block is uploaded as part of the whole block, not individually if (_uniform->GetBlockIndex() == -1 && _uniform->GetOwner() != nullptr) { _uniform->GetOwner()->SetResolvedUniform(_uniform->GetName(), _dataPointer); } _isDirty = false; return true; } bool RsxUniformCache::CheckFloat() const { return (_uniform != nullptr && _uniform->IsFloat()); } bool RsxUniformCache::CheckInt() const { return (_uniform != nullptr && !_uniform->IsFloat()); } bool RsxUniformCache::CheckComponents(std::uint32_t requiredComponents) const { return (_uniform != nullptr && _uniform->GetComponentCount() == requiredComponents); } // ------------------------------------------------------------------------------------------------- RsxUniformBlockCache::RsxUniformBlockCache(RsxUniformBlock* uniformBlock) : _uniformBlock(uniformBlock), _dataPointer(nullptr), _usedSize(0) { _usedSize = uniformBlock->GetSize(); _uniformCaches.reserve(uniformBlock->_members.size()); for (RsxUniform& member : uniformBlock->_members) { _uniformCaches.push_back({member.GetName(), RsxUniformCache(&member)}); } } std::uint32_t RsxUniformBlockCache::GetIndex() const { return (_uniformBlock != nullptr ? _uniformBlock->GetIndex() : 0); } std::int32_t RsxUniformBlockCache::GetBindingIndex() const { return (_uniformBlock != nullptr ? _uniformBlock->GetBindingIndex() : 0); } std::int32_t RsxUniformBlockCache::GetSize() const { return (_uniformBlock != nullptr ? _uniformBlock->GetSize() : 0); } std::uint8_t RsxUniformBlockCache::GetAlignAmount() const { return (_uniformBlock != nullptr ? _uniformBlock->GetAlignAmount() : 0); } void RsxUniformBlockCache::SetDataPointer(std::uint8_t* dataPointer) { _dataPointer = dataPointer; for (NamedCache& named : _uniformCaches) { named.Cache.SetDataPointer(_dataPointer + named.Cache.GetUniform()->GetOffset()); } } void RsxUniformBlockCache::SetUsedSize(std::int32_t usedSize) { if (usedSize >= 0) { _usedSize = usedSize; } } bool RsxUniformBlockCache::CopyData(std::uint32_t destIndex, const std::uint8_t* src, std::uint32_t numBytes) { if (destIndex + numBytes > std::uint32_t(GetSize()) || numBytes == 0 || src == nullptr || _dataPointer == nullptr) { return false; } std::memcpy(&_dataPointer[destIndex], src, numBytes); return true; } RsxUniformCache* RsxUniformBlockCache::GetUniform(StringView name) { for (NamedCache& named : _uniformCaches) { if (name == named.Name) { return &named.Cache; } } return nullptr; } void RsxUniformBlockCache::SetBlockBinding(std::int32_t blockBinding) { if (_uniformBlock != nullptr) { _uniformBlock->SetBlockBinding(blockBinding); } } }