Add names for composite constants in SPIR-V

Consider the following code:

    layout(constant_id=0) const int Y = 1;
    layout(constant_id=1) const int Z = 2;
    layout(constant_id=3) const int X = Y + Z;

Previously, it would produce SPIR-V decorations like this:

    Decorate 21(Y) SpecId 1
    Decorate 22 SpecId 3
    Decorate 33(Z) SpecId 0

This seems inaccurate, since the spec constant `X` that is dependent on
the two others did not get a name in the SPIR-V decorations. This behavior
may potentially negatively affect shader introspection capabilities.

This change alters the behavior to always add a name, which results in the code
above producing the following decorations:

    Decorate 21(Y) SpecId 1
    Decorate 22(X) SpecId 3
    Decorate 33(Z) SpecId 0
This commit is contained in:
Grigory Dzhavadyan
2018-10-29 22:56:44 -07:00
committed by nicebyte
parent b2b3d81e9b
commit 4c9876b34c
8 changed files with 232 additions and 117 deletions

View File

@@ -7277,18 +7277,19 @@ spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& n
// An AST node labelled as specialization constant should be a symbol node.
// Its initializer should either be a sub tree with constant nodes, or a constant union array.
if (auto* sn = node.getAsSymbolNode()) {
spv::Id result;
if (auto* sub_tree = sn->getConstSubtree()) {
// Traverse the constant constructor sub tree like generating normal run-time instructions.
// During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard
// will set the builder into spec constant op instruction generating mode.
sub_tree->traverse(this);
return accessChainLoad(sub_tree->getType());
} else if (auto* const_union_array = &sn->getConstArray()){
result = accessChainLoad(sub_tree->getType());
} else if (auto* const_union_array = &sn->getConstArray()) {
int nextConst = 0;
spv::Id id = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true);
builder.addName(id, sn->getName().c_str());
return id;
result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true);
}
builder.addName(result, sn->getName().c_str());
return result;
}
// Neither a front-end constant node, nor a specialization constant node with constant union array or