Skip to content

Commit

Permalink
spirv: Support initializers on uniforms
Browse files Browse the repository at this point in the history
If a uniform has an initializer it will now be given as the optional
initializer operand to the OpVariable instruction.

Fixes: KhronosGroup#1259
  • Loading branch information
bpeel authored and infapi00 committed Mar 30, 2018
1 parent b89c3fc commit 8a25755
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
13 changes: 12 additions & 1 deletion SPIRV/GlslangToSpv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,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.
Expand Down
5 changes: 4 additions & 1 deletion SPIRV/SpvBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,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);
Expand All @@ -1182,6 +1182,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();
}

Expand Down
2 changes: 1 addition & 1 deletion SPIRV/SpvBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,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);
Expand Down

0 comments on commit 8a25755

Please sign in to comment.