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
|
#include "GLShaderUniforms.h"
#include "GLShaderProgram.h"
#include "GLUniformCache.h"
#include "../RenderResources.h"
#include "../../Base/StaticHashMapIterator.h"
#include "../../../Main.h"
namespace nCine
{
GLShaderUniforms::GLShaderUniforms()
: shaderProgram_(nullptr)
{
}
GLShaderUniforms::GLShaderUniforms(GLShaderProgram* shaderProgram)
: GLShaderUniforms(shaderProgram, nullptr, nullptr)
{
SetProgram(shaderProgram);
}
GLShaderUniforms::GLShaderUniforms(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
: GLShaderUniforms()
{
SetProgram(shaderProgram, includeOnly, exclude);
}
void GLShaderUniforms::SetProgram(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude)
{
DEATH_ASSERT(shaderProgram != nullptr);
shaderProgram_ = shaderProgram;
shaderProgram_->ProcessDeferredQueries();
uniformCaches_.clear();
if (shaderProgram_->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) {
ImportUniforms(includeOnly, exclude);
}
}
void GLShaderUniforms::SetUniformsDataPointer(GLubyte* dataPointer)
{
DEATH_ASSERT(dataPointer != nullptr);
if (shaderProgram_->GetStatus() != GLShaderProgram::Status::LinkedWithIntrospection) {
return;
}
std::uint32_t offset = 0;
for (GLUniformCache& uniformCache : uniformCaches_) {
uniformCache.SetDataPointer(dataPointer + offset);
offset += uniformCache.GetUniform()->GetMemorySize();
}
}
void GLShaderUniforms::SetDirty(bool isDirty)
{
if (shaderProgram_->GetStatus() != GLShaderProgram::Status::LinkedWithIntrospection) {
return;
}
for (auto& uniform : uniformCaches_) {
uniform.SetDirty(isDirty);
}
}
GLUniformCache* GLShaderUniforms::GetUniform(const char* name)
{
DEATH_ASSERT(name != nullptr);
GLUniformCache* uniformCache = nullptr;
if (shaderProgram_ != nullptr) {
uniformCache = uniformCaches_.find(String::nullTerminatedView(name));
} else {
LOGE("Cannot find uniform \"{}\", no shader program associated", name);
}
return uniformCache;
}
void GLShaderUniforms::CommitUniforms()
{
if (shaderProgram_ != nullptr) {
if (shaderProgram_->GetStatus() == GLShaderProgram::Status::LinkedWithIntrospection) {
shaderProgram_->Use();
for (auto& uniform : uniformCaches_) {
uniform.CommitValue();
}
}
} else {
LOGE("No shader program associated");
}
}
void GLShaderUniforms::ImportUniforms(const char* includeOnly, const char* exclude)
{
constexpr std::uint32_t MaxUniformName = 128;
std::uint32_t importedCount = 0;
for (const GLUniform& uniform : shaderProgram_->uniforms_) {
const char* uniformName = uniform.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, uniformName, MaxUniformName) == 0) {
shouldImport = true;
break;
}
currentIncludeOnly += strnlen(currentIncludeOnly, MaxUniformName) + 1;
}
}
if (exclude != nullptr) {
while (currentExclude != nullptr && currentExclude[0] != '\0') {
if (strncmp(currentExclude, uniformName, MaxUniformName) == 0) {
shouldImport = false;
break;
}
currentExclude += strnlen(currentExclude, MaxUniformName) + 1;
}
}
if (shouldImport) {
GLUniformCache uniformCache(&uniform);
uniformCaches_[uniformName] = uniformCache;
importedCount++;
}
}
if (importedCount > UniformCachesHashSize) {
LOGW("More imported uniform blocks ({}) than hashmap buckets ({})", importedCount, UniformCachesHashSize);
}
}
}
|