| 1 |
#include "GLShaderUniformBlocks.h" |
| 2 |
#include "GLShaderProgram.h" |
| 3 |
#include "GLBufferObject.h" |
| 4 |
#include "../IRhiCapabilities.h" |
| 5 |
#include "../../../ServiceLocator.h" |
| 6 |
#include "../../../../Main.h" |
| 7 |
|
| 8 |
#include <cstring> // for memcpy() |
| 9 |
|
| 10 |
namespace nCine::RHI::GL |
| 11 |
{ |
| 12 |
GLShaderUniformBlocks::UniformRangeAllocator GLShaderUniformBlocks::_uniformRangeAllocator = nullptr; |
| 13 |
|
| 14 |
void GLShaderUniformBlocks::SetUniformRangeAllocator(UniformRangeAllocator allocator) |
| 15 |
{ |
| 16 |
_uniformRangeAllocator = allocator; |
| 17 |
} |
| 18 |
|
| 19 |
GLShaderUniformBlocks::GLShaderUniformBlocks() |
| 20 |
: _shaderProgram(nullptr), _dataPointer(nullptr) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
GLShaderUniformBlocks::GLShaderUniformBlocks(GLShaderProgram* shaderProgram) |
| 25 |
: GLShaderUniformBlocks(shaderProgram, nullptr, nullptr) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
GLShaderUniformBlocks::GLShaderUniformBlocks(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) |
| 30 |
: GLShaderUniformBlocks() |
| 31 |
{ |
| 32 |
SetProgram(shaderProgram, includeOnly, exclude); |
| 33 |
} |
| 34 |
|
| 35 |
void GLShaderUniformBlocks::Bind() |
| 36 |
{ |
| 37 |
#if defined(RHI_GL_PROFILE_ES2) |
| 38 |
// ES2 has no uniform buffer objects: push each managed block's members to the program's loose |
| 39 |
// uniforms. Material::Bind() has already called Use(), so the program is current for glUniform*. |
| 40 |
for (GLUniformBlockCache& uniformBlockCache : _uniformBlockCaches) { |
| 41 |
uniformBlockCache.CommitAsLooseUniforms(); |
| 42 |
} |
| 43 |
// Dead code when the software backend is selected: `RHI::BufferRange::object` is then a `SwBuffer*` |
| 44 |
// whose definition this GL translation unit does not include, and the pipeline never calls this here |
| 45 |
#elif !defined(WITH_RHI_SOFTWARE) |
| 46 |
#if defined(DEATH_DEBUG) |
| 47 |
static const std::int32_t offsetAlignment = theServiceLocator().GetRhiCapabilities().GetValue(IRhiCapabilities::IntValues::UniformBufferOffsetAlignment); |
| 48 |
#endif |
| 49 |
if (_uboParams.object) { |
| 50 |
_uboParams.object->Bind(); |
| 51 |
|
| 52 |
GLintptr moreOffset = 0; |
| 53 |
for (GLUniformBlockCache& uniformBlockCache : _uniformBlockCaches) { |
| 54 |
uniformBlockCache.SetBlockBinding(uniformBlockCache.GetIndex()); |
| 55 |
const GLintptr offset = static_cast<GLintptr>(_uboParams.offset) + moreOffset; |
| 56 |
#if defined(DEATH_DEBUG) |
| 57 |
DEATH_DEBUG_ASSERT(offset % offsetAlignment == 0); |
| 58 |
#endif |
| 59 |
_uboParams.object->BindBufferRange(uniformBlockCache.GetBindingIndex(), offset, uniformBlockCache.usedSize()); |
| 60 |
moreOffset += uniformBlockCache.usedSize(); |
| 61 |
} |
| 62 |
} |
| 63 |
#endif |
| 64 |
} |
| 65 |
|
| 66 |
void GLShaderUniformBlocks::SetProgram(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude) |
| 67 |
{ |
| 68 |
DEATH_ASSERT(shaderProgram); |
| 69 |
|
| 70 |
_shaderProgram = shaderProgram; |
| 71 |
_shaderProgram->ProcessDeferredQueries(); |
| 72 |
_uniformBlockCaches.clear(); |
| 73 |
|
| 74 |
if (shaderProgram->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) { |
| 75 |
ImportUniformBlocks(includeOnly, exclude); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void GLShaderUniformBlocks::SetUniformsDataPointer(GLubyte* dataPointer) |
| 80 |
{ |
| 81 |
DEATH_ASSERT(dataPointer); |
| 82 |
|
| 83 |
if (_shaderProgram->GetStatus() != GLShaderProgram::Status::LinkedWithIntrospection) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
_dataPointer = dataPointer; |
| 88 |
std::int32_t offset = 0; |
| 89 |
for (GLUniformBlockCache& uniformBlockCache : _uniformBlockCaches) { |
| 90 |
uniformBlockCache.SetDataPointer(dataPointer + offset); |
| 91 |
offset += uniformBlockCache.uniformBlock()->GetSize() - uniformBlockCache.uniformBlock()->GetAlignAmount(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
GLUniformBlockCache* GLShaderUniformBlocks::GetUniformBlock(const char* name) |
| 96 |
{ |
| 97 |
DEATH_ASSERT(name); |
| 98 |
GLUniformBlockCache* uniformBlockCache = nullptr; |
| 99 |
|
| 100 |
if (_shaderProgram != nullptr) { |
| 101 |
uniformBlockCache = _uniformBlockCaches.find(String::nullTerminatedView(name)); |
| 102 |
} else { |
| 103 |
LOGE("Cannot find uniform block \"{}\", no shader program associated", name); |
| 104 |
} |
| 105 |
return uniformBlockCache; |
| 106 |
} |
| 107 |
|
| 108 |
void GLShaderUniformBlocks::CommitUniformBlocks() |
| 109 |
{ |
| 110 |
#if defined(RHI_GL_PROFILE_ES2) |
| 111 |
// ES2 has no UBOs: block data is pushed to loose uniforms at Bind() time, nothing to commit here |
| 112 |
return; |
| 113 |
#else |
| 114 |
if (_shaderProgram != nullptr) { |
| 115 |
if (_shaderProgram->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) { |
| 116 |
std::int32_t totalUsedSize = 0; |
| 117 |
bool hasMemoryGaps = false; |
| 118 |
for (GLUniformBlockCache& uniformBlockCache : _uniformBlockCaches) { |
| 119 |
// There is a gap if at least one block cache (not in last position) uses less memory than its size |
| 120 |
if (uniformBlockCache.GetDataPointer() != _dataPointer + totalUsedSize) { |
| 121 |
hasMemoryGaps = true; |
| 122 |
} |
| 123 |
totalUsedSize += uniformBlockCache.usedSize(); |
| 124 |
} |
| 125 |
|
| 126 |
if (totalUsedSize > 0) { |
| 127 |
DEATH_ASSERT(_uniformRangeAllocator != nullptr); |
| 128 |
_uboParams = _uniformRangeAllocator(std::uint32_t(totalUsedSize)); |
| 129 |
if (_uboParams.mapBase != nullptr) { |
| 130 |
if (hasMemoryGaps) { |
| 131 |
std::int32_t offset = 0; |
| 132 |
for (GLUniformBlockCache& uniformBlockCache : _uniformBlockCaches) { |
| 133 |
std::memcpy(_uboParams.mapBase + _uboParams.offset + offset, uniformBlockCache.GetDataPointer(), uniformBlockCache.usedSize()); |
| 134 |
offset += uniformBlockCache.usedSize(); |
| 135 |
} |
| 136 |
} else { |
| 137 |
std::memcpy(_uboParams.mapBase + _uboParams.offset, _dataPointer, totalUsedSize); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} else { |
| 143 |
LOGE("No shader program associated"); |
| 144 |
} |
| 145 |
#endif |
| 146 |
} |
| 147 |
|
| 148 |
void GLShaderUniformBlocks::ImportUniformBlocks(const char* includeOnly, const char* exclude) |
| 149 |
{ |
| 150 |
const std::uint32_t MaxUniformBlockName = 128; |
| 151 |
|
| 152 |
std::uint32_t importedCount = 0; |
| 153 |
for (GLUniformBlock& uniformBlock : _shaderProgram->_uniformBlocks) { |
| 154 |
const char* uniformBlockName = uniformBlock.GetName(); |
| 155 |
const char* currentIncludeOnly = includeOnly; |
| 156 |
const char* currentExclude = exclude; |
| 157 |
bool shouldImport = true; |
| 158 |
|
| 159 |
if (includeOnly != nullptr) { |
| 160 |
shouldImport = false; |
| 161 |
while (currentIncludeOnly != nullptr && currentIncludeOnly[0] != '\0') { |
| 162 |
if (strncmp(currentIncludeOnly, uniformBlockName, MaxUniformBlockName) == 0) { |
| 163 |
shouldImport = true; |
| 164 |
break; |
| 165 |
} |
| 166 |
currentIncludeOnly += strnlen(currentIncludeOnly, MaxUniformBlockName) + 1; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if (exclude != nullptr) { |
| 171 |
while (currentExclude != nullptr && currentExclude[0] != '\0') { |
| 172 |
if (strncmp(currentExclude, uniformBlockName, MaxUniformBlockName) == 0) { |
| 173 |
shouldImport = false; |
| 174 |
break; |
| 175 |
} |
| 176 |
currentExclude += strnlen(currentExclude, MaxUniformBlockName) + 1; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if (shouldImport) { |
| 181 |
_uniformBlockCaches.emplace(uniformBlockName, &uniformBlock); |
| 182 |
importedCount++; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (importedCount > UniformBlockCachesHashSize) { |
| 187 |
LOGW("More imported uniform blocks ({}) than hashmap buckets ({})", importedCount, UniformBlockCachesHashSize); |
| 188 |
} |
| 189 |
} |
| 190 |
} |