1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include "GLShaderUniformBlocks.h"
#include "GLShaderProgram.h"
#include "../RenderResources.h"
#include "../../ServiceLocator.h"
#include "../../../Main.h"
#include <cstring> // for memcpy()
namespace nCine
{
GLShaderUniformBlocks::GLShaderUniformBlocks()
: shaderProgram_(nullptr), dataPointer_(nullptr)
{
}
GLShaderUniformBlocks::GLShaderUniformBlocks(GLShaderProgram* shaderProgram)
: GLShaderUniformBlocks(shaderProgram, nullptr, nullptr)
{
}
GLShaderUniformBlocks::GLShaderUniformBlocks(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
: GLShaderUniformBlocks()
{
SetProgram(shaderProgram, includeOnly, exclude);
}
void GLShaderUniformBlocks::Bind()
{
#if defined(DEATH_DEBUG)
static const std::int32_t offsetAlignment = theServiceLocator().GetGfxCapabilities().GetValue(IGfxCapabilities::GLIntValues::UNIFORM_BUFFER_OFFSET_ALIGNMENT);
#endif
if (uboParams_.object) {
uboParams_.object->Bind();
GLintptr moreOffset = 0;
for (GLUniformBlockCache& uniformBlockCache : uniformBlockCaches_) {
uniformBlockCache.SetBlockBinding(uniformBlockCache.GetIndex());
const GLintptr offset = static_cast<GLintptr>(uboParams_.offset) + moreOffset;
#if defined(DEATH_DEBUG)
DEATH_DEBUG_ASSERT(offset % offsetAlignment == 0);
#endif
uboParams_.object->BindBufferRange(uniformBlockCache.GetBindingIndex(), offset, uniformBlockCache.usedSize());
moreOffset += uniformBlockCache.usedSize();
}
}
}
void GLShaderUniformBlocks::SetProgram(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
{
DEATH_ASSERT(shaderProgram);
shaderProgram_ = shaderProgram;
shaderProgram_->ProcessDeferredQueries();
uniformBlockCaches_.clear();
if (shaderProgram->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) {
ImportUniformBlocks(includeOnly, exclude);
}
}
void GLShaderUniformBlocks::SetUniformsDataPointer(GLubyte* dataPointer)
{
DEATH_ASSERT(dataPointer);
if (shaderProgram_->GetStatus() != GLShaderProgram::Status::LinkedWithIntrospection) {
return;
}
dataPointer_ = dataPointer;
std::int32_t offset = 0;
for (GLUniformBlockCache& uniformBlockCache : uniformBlockCaches_) {
uniformBlockCache.SetDataPointer(dataPointer + offset);
offset += uniformBlockCache.uniformBlock()->GetSize() - uniformBlockCache.uniformBlock()->GetAlignAmount();
}
}
GLUniformBlockCache* GLShaderUniformBlocks::GetUniformBlock(const char* name)
{
DEATH_ASSERT(name);
GLUniformBlockCache* uniformBlockCache = nullptr;
if (shaderProgram_ != nullptr) {
uniformBlockCache = uniformBlockCaches_.find(String::nullTerminatedView(name));
} else {
LOGE("Cannot find uniform block \"{}\", no shader program associated", name);
}
return uniformBlockCache;
}
void GLShaderUniformBlocks::CommitUniformBlocks()
{
if (shaderProgram_ != nullptr) {
if (shaderProgram_->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) {
std::int32_t totalUsedSize = 0;
bool hasMemoryGaps = false;
for (GLUniformBlockCache& uniformBlockCache : uniformBlockCaches_) {
// There is a gap if at least one block cache (not in last position) uses less memory than its size
if (uniformBlockCache.GetDataPointer() != dataPointer_ + totalUsedSize) {
hasMemoryGaps = true;
}
totalUsedSize += uniformBlockCache.usedSize();
}
if (totalUsedSize > 0) {
const RenderBuffersManager::BufferTypes bufferType = RenderBuffersManager::BufferTypes::Uniform;
uboParams_ = RenderResources::GetBuffersManager().AcquireMemory(bufferType, totalUsedSize);
if (uboParams_.mapBase != nullptr) {
if (hasMemoryGaps) {
std::int32_t offset = 0;
for (GLUniformBlockCache& uniformBlockCache : uniformBlockCaches_) {
std::memcpy(uboParams_.mapBase + uboParams_.offset + offset, uniformBlockCache.GetDataPointer(), uniformBlockCache.usedSize());
offset += uniformBlockCache.usedSize();
}
} else {
std::memcpy(uboParams_.mapBase + uboParams_.offset, dataPointer_, totalUsedSize);
}
}
}
}
} else {
LOGE("No shader program associated");
}
}
void GLShaderUniformBlocks::ImportUniformBlocks(const char* includeOnly, const char* exclude)
{
const std::uint32_t MaxUniformBlockName = 128;
std::uint32_t importedCount = 0;
for (GLUniformBlock& uniformBlock : shaderProgram_->uniformBlocks_) {
const char* uniformBlockName = uniformBlock.GetName();
const char* currentIncludeOnly = includeOnly;
const char* currentExclude = exclude;
bool shouldImport = true;
if (includeOnly != nullptr) {
shouldImport = false;
while (currentIncludeOnly != nullptr && currentIncludeOnly[0] != '\0') {
if (strncmp(currentIncludeOnly, uniformBlockName, MaxUniformBlockName) == 0) {
shouldImport = true;
break;
}
currentIncludeOnly += strnlen(currentIncludeOnly, MaxUniformBlockName) + 1;
}
}
if (exclude != nullptr) {
while (currentExclude != nullptr && currentExclude[0] != '\0') {
if (strncmp(currentExclude, uniformBlockName, MaxUniformBlockName) == 0) {
shouldImport = false;
break;
}
currentExclude += strnlen(currentExclude, MaxUniformBlockName) + 1;
}
}
if (shouldImport) {
uniformBlockCaches_.emplace(uniformBlockName, &uniformBlock);
importedCount++;
}
}
if (importedCount > UniformBlockCachesHashSize) {
LOGW("More imported uniform blocks ({}) than hashmap buckets ({})", importedCount, UniformBlockCachesHashSize);
}
}
}
|