HLSL: Recursive composite flattening
This PR implements recursive type flattening. For example, an array of structs of other structs can be flattened to individual member variables at the shader interface. This is sufficient for many purposes, e.g, uniforms containing opaque types, but is not sufficient for geometry shader arrayed inputs. That will be handled separately with structure splitting, which is not implemented by this PR. In the meantime, that case is detected and triggers an error. The recursive flattening extends the following three aspects of single-level flattening: - Flattening of structures to individual members with names such as "foo[0].samp[1]"; - Turning constant references to the nested composite type into a reference to a particular flattened member. - Shadow copies between arrays of flattened members and the nested composite type. Previous single-level flattening only flattened at the shader interface, and that is unchanged by this PR. Internally, shadow copies are, such as if the type is passed to a function. Also, the reasons for flattening are unchanged. Uniforms containing opaque types, and interface struct types are flattened. (The latter will change with structure splitting). One existing test changes: hlsl.structin.vert, which did in fact contain a nested composite type to be flattened. Two new tests are added: hlsl.structarray.flatten.frag, and hlsl.structarray.flatten.geom (currently issues an error until type splitting is online). The process of arriving at the individual member from chained postfix expressions is more complex than it was with one level. See large-ish comment above HlslParseContext::flatten() for details.
This commit is contained in:
@@ -389,7 +389,7 @@ bool HlslGrammar::acceptDeclaration(TIntermNode*& node)
|
||||
else if (variableType.getBasicType() == EbtBlock)
|
||||
parseContext.declareBlock(idToken.loc, variableType, idToken.string);
|
||||
else {
|
||||
if (variableType.getQualifier().storage == EvqUniform && ! variableType.isOpaque()) {
|
||||
if (variableType.getQualifier().storage == EvqUniform && ! variableType.containsOpaque()) {
|
||||
// this isn't really an individual variable, but a member of the $Global buffer
|
||||
parseContext.growGlobalUniformBlock(idToken.loc, variableType, *idToken.string);
|
||||
} else {
|
||||
@@ -2215,6 +2215,20 @@ bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
|
||||
return false;
|
||||
}
|
||||
|
||||
// This is to guarantee we do this no matter how we get out of the stack frame.
|
||||
// This way there's no bug if an early return forgets to do it.
|
||||
struct tFinalize {
|
||||
tFinalize(HlslParseContext& p) : parseContext(p) { }
|
||||
~tFinalize() { parseContext.finalizeFlattening(); }
|
||||
HlslParseContext& parseContext;
|
||||
} finalize(parseContext);
|
||||
|
||||
// Initialize the flattening accumulation data, so we can track data across multiple bracket or
|
||||
// dot operators. This can also be nested, e.g, for [], so we have to track each nesting
|
||||
// level: hence the init and finalize. Even though in practice these must be
|
||||
// constants, they are parsed no matter what.
|
||||
parseContext.initFlattening();
|
||||
|
||||
// Something was found, chain as many postfix operations as exist.
|
||||
do {
|
||||
TSourceLoc loc = token.loc;
|
||||
@@ -2248,7 +2262,7 @@ bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
|
||||
node = parseContext.handleDotDereference(field.loc, node, *field.string);
|
||||
|
||||
// In the event of a method node, we look for an open paren and accept the function call.
|
||||
if (node->getAsMethodNode() != nullptr && peekTokenClass(EHTokLeftParen)) {
|
||||
if (node != nullptr && node->getAsMethodNode() != nullptr && peekTokenClass(EHTokLeftParen)) {
|
||||
if (! acceptFunctionCall(field, node, base)) {
|
||||
expected("function parameters");
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user