From b9af4826ebe22c09237a1cbd5b0ed57fa6546dcf Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Wed, 14 Mar 2018 19:06:22 +0100 Subject: [PATCH] spirv: Support initializers on uniforms If a uniform has an initializer it will now be given as the optional initializer operand to the OpVariable instruction. Fixes: https://github.com/KhronosGroup/glslang/issues/1259 --- SPIRV/GlslangToSpv.cpp | 13 ++++++++++++- SPIRV/SpvBuilder.cpp | 5 ++++- SPIRV/SpvBuilder.h | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp index 5c00024c69..ad8c854ff4 100755 --- a/SPIRV/GlslangToSpv.cpp +++ b/SPIRV/GlslangToSpv.cpp @@ -2843,7 +2843,18 @@ spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* if (glslang::IsAnonymous(name)) name = ""; - return builder.createVariable(storageClass, spvType, name); + spv::Id initializer = spv::NoResult; + + if (node->getType().getQualifier().storage == glslang::EvqUniform && + !node->getConstArray().empty()) { + int nextConst = 0; + initializer = createSpvConstantFromConstUnionArray(node->getType(), + node->getConstArray(), + nextConst, + false /* specConst */); + } + + return builder.createVariable(storageClass, spvType, name, initializer); } // Return type Id of the sampled type. diff --git a/SPIRV/SpvBuilder.cpp b/SPIRV/SpvBuilder.cpp index 8755d9eacf..d726372c12 100644 --- a/SPIRV/SpvBuilder.cpp +++ b/SPIRV/SpvBuilder.cpp @@ -1198,7 +1198,7 @@ void Builder::makeDiscard() } // Comments in header -Id Builder::createVariable(StorageClass storageClass, Id type, const char* name) +Id Builder::createVariable(StorageClass storageClass, Id type, const char* name, Id initializer) { Id pointerType = makePointer(storageClass, type); Instruction* inst = new Instruction(getUniqueId(), pointerType, OpVariable); @@ -1219,6 +1219,9 @@ Id Builder::createVariable(StorageClass storageClass, Id type, const char* name) if (name) addName(inst->getResultId(), name); + if (initializer != NoResult) + inst->addIdOperand(initializer); + return inst->getResultId(); } diff --git a/SPIRV/SpvBuilder.h b/SPIRV/SpvBuilder.h index 7c1d421b37..77a14a7bea 100644 --- a/SPIRV/SpvBuilder.h +++ b/SPIRV/SpvBuilder.h @@ -271,7 +271,7 @@ class Builder { void makeDiscard(); // Create a global or function local or IO variable. - Id createVariable(StorageClass, Id type, const char* name = 0); + Id createVariable(StorageClass, Id type, const char* name = 0, Id initializer = NoResult); // Create an intermediate with an undefined value. Id createUndefined(Id type);