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 Sep 18, 2018
1 parent 0fe10b1 commit 0eab0a0
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 @@ -2650,7 +2650,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 @@ -1183,7 +1183,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 @@ -1204,6 +1204,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 @@ -268,7 +268,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 0eab0a0

Please sign in to comment.