Commit a2b01a0d by steve-lunarg

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.
parent b56f4ac7
hlsl.structarray.flatten.geom
ERROR: 0:10: 'vin' : recursive type not yet supported in GS input
ERROR: 1 compilation errors. No code generated.
Shader version: 450
invocations = -1
max_vertices = 4
input primitive = lines
output primitive = triangle_strip
ERROR: node is still EOpNull!
0:10 Function Definition: main(struct-VertexData-vf4-vf4-vf21[2];struct-VertexData-vf4-vf4-vf21; (temp void)
0:10 Function Parameters:
0:10 'vin' (in 2-element array of structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:10 'outStream' (out structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:? Sequence
0:13 move second child to first child (temp 4-component vector of float)
0:13 color: direct index for structure (temp 4-component vector of float)
0:13 'vout' (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:13 Constant:
0:13 1 (const int)
0:? 'vin[0].color' (layout(location=1 ) in 4-component vector of float)
0:14 move second child to first child (temp 2-component vector of float)
0:14 uv: direct index for structure (temp 2-component vector of float)
0:14 'vout' (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:14 Constant:
0:14 2 (const int)
0:? 'vin[0].uv' (layout(location=2 ) in 2-component vector of float)
0:15 move second child to first child (temp 4-component vector of float)
0:15 position: direct index for structure (temp 4-component vector of float)
0:15 'vout' (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:15 Constant:
0:15 0 (const int)
0:? 'vin[0].position' (layout(location=0 ) in 4-component vector of float)
0:16 Sequence
0:16 move second child to first child (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:16 'outStream' (out structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:16 'vout' (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:16 EmitVertex (temp void)
0:? Linker Objects
0:? 'vin[0].position' (layout(location=0 ) in 4-component vector of float)
0:? 'vin[0].color' (layout(location=1 ) in 4-component vector of float)
0:? 'vin[0].uv' (layout(location=2 ) in 2-component vector of float)
0:? 'vin[1].position' (layout(location=3 ) in 4-component vector of float)
0:? 'vin[1].color' (layout(location=4 ) in 4-component vector of float)
0:? 'vin[1].uv' (layout(location=5 ) in 2-component vector of float)
0:? 'position' (layout(location=0 ) out 4-component vector of float)
0:? 'color' (layout(location=1 ) out 4-component vector of float)
0:? 'uv' (layout(location=2 ) out 2-component vector of float)
Linked geometry stage:
Shader version: 450
invocations = 1
max_vertices = 4
input primitive = lines
output primitive = triangle_strip
ERROR: node is still EOpNull!
0:10 Function Definition: main(struct-VertexData-vf4-vf4-vf21[2];struct-VertexData-vf4-vf4-vf21; (temp void)
0:10 Function Parameters:
0:10 'vin' (in 2-element array of structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:10 'outStream' (out structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:? Sequence
0:13 move second child to first child (temp 4-component vector of float)
0:13 color: direct index for structure (temp 4-component vector of float)
0:13 'vout' (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:13 Constant:
0:13 1 (const int)
0:? 'vin[0].color' (layout(location=1 ) in 4-component vector of float)
0:14 move second child to first child (temp 2-component vector of float)
0:14 uv: direct index for structure (temp 2-component vector of float)
0:14 'vout' (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:14 Constant:
0:14 2 (const int)
0:? 'vin[0].uv' (layout(location=2 ) in 2-component vector of float)
0:15 move second child to first child (temp 4-component vector of float)
0:15 position: direct index for structure (temp 4-component vector of float)
0:15 'vout' (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:15 Constant:
0:15 0 (const int)
0:? 'vin[0].position' (layout(location=0 ) in 4-component vector of float)
0:16 Sequence
0:16 move second child to first child (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:16 'outStream' (out structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:16 'vout' (temp structure{temp 4-component vector of float position, temp 4-component vector of float color, temp 2-component vector of float uv})
0:16 EmitVertex (temp void)
0:? Linker Objects
0:? 'vin[0].position' (layout(location=0 ) in 4-component vector of float)
0:? 'vin[0].color' (layout(location=1 ) in 4-component vector of float)
0:? 'vin[0].uv' (layout(location=2 ) in 2-component vector of float)
0:? 'vin[1].position' (layout(location=3 ) in 4-component vector of float)
0:? 'vin[1].color' (layout(location=4 ) in 4-component vector of float)
0:? 'vin[1].uv' (layout(location=5 ) in 2-component vector of float)
0:? 'position' (layout(location=0 ) out 4-component vector of float)
0:? 'color' (layout(location=1 ) out 4-component vector of float)
0:? 'uv' (layout(location=2 ) out 2-component vector of float)
SPIR-V is not generated for failed compile or link
SamplerState g_samp;
Texture1D g_tex;
struct tex_t {
SamplerState samp;
Texture1D tex;
int nonopaque_thing;
};
struct tex_with_arrays_t {
SamplerState samp[2];
Texture1D tex[2];
int nonopaque_thing;
};
uniform tex_t g_texdata;
uniform tex_t g_texdata_array[3];
uniform tex_with_arrays_t g_texdata_array2[3];
struct PS_OUTPUT { float4 color : SV_Target0; };
void main(out PS_OUTPUT ps_output)
{
ps_output.color =
g_texdata.tex.Sample(g_texdata.samp, 0.5) +
g_texdata_array[1].tex.Sample(g_texdata_array[1].samp, 0.4) +
g_texdata_array2[1].tex[0].Sample(g_texdata_array2[1].samp[0], 0.3);
}
struct VertexData {
float4 position : POSITION;
float4 color : COLOR0;
float2 uv : TEXCOORD0;
};
[maxvertexcount(4)]
void main(line VertexData vin[2], inout TriangleStream<VertexData> outStream)
{
VertexData vout;
vout.color = vin[0].color;
vout.uv = vin[0].uv;
vout.position = vin[0].position;
outStream.Append(vout);
}
...@@ -11,4 +11,4 @@ VI main(float4 d, VI vi, float4 e) : SV_POSITION ...@@ -11,4 +11,4 @@ VI main(float4 d, VI vi, float4 e) : SV_POSITION
local.b = vi.m[1] + vi.m[0] + float4(vi.coord.x) + d + e; local.b = vi.m[1] + vi.m[0] + float4(vi.coord.x) + d + e;
return local; return local;
} }
\ No newline at end of file
...@@ -203,6 +203,8 @@ INSTANTIATE_TEST_CASE_P( ...@@ -203,6 +203,8 @@ INSTANTIATE_TEST_CASE_P(
{"hlsl.shapeConvRet.frag", "main"}, {"hlsl.shapeConvRet.frag", "main"},
{"hlsl.stringtoken.frag", "main"}, {"hlsl.stringtoken.frag", "main"},
{"hlsl.string.frag", "main"}, {"hlsl.string.frag", "main"},
{"hlsl.structarray.flatten.frag", "main"},
{"hlsl.structarray.flatten.geom", "main"},
{"hlsl.structin.vert", "main"}, {"hlsl.structin.vert", "main"},
{"hlsl.intrinsics.vert", "VertexShaderFunction"}, {"hlsl.intrinsics.vert", "VertexShaderFunction"},
{"hlsl.matType.frag", "PixelShaderFunction"}, {"hlsl.matType.frag", "PixelShaderFunction"},
......
...@@ -389,7 +389,7 @@ bool HlslGrammar::acceptDeclaration(TIntermNode*& node) ...@@ -389,7 +389,7 @@ bool HlslGrammar::acceptDeclaration(TIntermNode*& node)
else if (variableType.getBasicType() == EbtBlock) else if (variableType.getBasicType() == EbtBlock)
parseContext.declareBlock(idToken.loc, variableType, idToken.string); parseContext.declareBlock(idToken.loc, variableType, idToken.string);
else { 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 // this isn't really an individual variable, but a member of the $Global buffer
parseContext.growGlobalUniformBlock(idToken.loc, variableType, *idToken.string); parseContext.growGlobalUniformBlock(idToken.loc, variableType, *idToken.string);
} else { } else {
...@@ -2215,6 +2215,20 @@ bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node) ...@@ -2215,6 +2215,20 @@ bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
return false; 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. // Something was found, chain as many postfix operations as exist.
do { do {
TSourceLoc loc = token.loc; TSourceLoc loc = token.loc;
...@@ -2248,7 +2262,7 @@ bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node) ...@@ -2248,7 +2262,7 @@ bool HlslGrammar::acceptPostfixExpression(TIntermTyped*& node)
node = parseContext.handleDotDereference(field.loc, node, *field.string); 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. // 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)) { if (! acceptFunctionCall(field, node, base)) {
expected("function parameters"); expected("function parameters");
return false; return false;
......
...@@ -169,10 +169,23 @@ public: ...@@ -169,10 +169,23 @@ public:
// Potentially rename shader entry point function // Potentially rename shader entry point function
void renameShaderFunction(TString*& name) const; void renameShaderFunction(TString*& name) const;
// Reset data for incrementally built referencing of flattened composite structures
void initFlattening() { flattenLevel.push_back(0); flattenOffset.push_back(0); }
void finalizeFlattening() { flattenLevel.pop_back(); flattenOffset.pop_back(); }
protected: protected:
struct TFlattenData {
TFlattenData() : nextBinding(TQualifier::layoutBindingEnd) { }
TFlattenData(int nb) : nextBinding(nb) { }
TVector<TVariable*> members; // individual flattened variables
TVector<int> offsets; // offset to next tree level
int nextBinding; // next binding to use.
};
void inheritGlobalDefaults(TQualifier& dst) const; void inheritGlobalDefaults(TQualifier& dst) const;
TVariable* makeInternalVariable(const char* name, const TType&) const; TVariable* makeInternalVariable(const char* name, const TType&) const;
TVariable* declareNonArray(const TSourceLoc&, TString& identifier, TType&); TVariable* declareNonArray(const TSourceLoc&, TString& identifier, TType&, bool track);
void declareArray(const TSourceLoc&, TString& identifier, const TType&, TSymbol*&, bool track); void declareArray(const TSourceLoc&, TString& identifier, const TType&, TSymbol*&, bool track);
TIntermNode* executeInitializer(const TSourceLoc&, TIntermTyped* initializer, TVariable* variable); TIntermNode* executeInitializer(const TSourceLoc&, TIntermTyped* initializer, TVariable* variable);
TIntermTyped* convertInitializerList(const TSourceLoc&, const TType&, TIntermTyped* initializer); TIntermTyped* convertInitializerList(const TSourceLoc&, const TType&, TIntermTyped* initializer);
...@@ -183,13 +196,19 @@ protected: ...@@ -183,13 +196,19 @@ protected:
bool shouldConvertLValue(const TIntermNode*) const; bool shouldConvertLValue(const TIntermNode*) const;
// Array and struct flattening // Array and struct flattening
bool shouldFlatten(const TType& type) const { return shouldFlattenIO(type) || shouldFlattenUniform(type); } bool shouldFlatten(const TType& type) const;
TIntermTyped* flattenAccess(TIntermTyped* base, int member); TIntermTyped* flattenAccess(const TSourceLoc&, TIntermTyped* base, int member);
bool shouldFlattenIO(const TType&) const; bool shouldFlattenIO(const TType&) const;
bool shouldFlattenUniform(const TType&) const; bool shouldFlattenUniform(const TType&) const;
bool wasFlattened(const TIntermTyped* node) const;
bool wasFlattened(int id) const { return flattenMap.find(id) != flattenMap.end(); }
int addFlattenedMember(const TSourceLoc& loc, const TVariable&, const TType&, TFlattenData&, const TString& name, bool track);
bool isFinalFlattening(const TType& type) const { return !(type.isStruct() || type.isArray()); }
void flatten(const TSourceLoc& loc, const TVariable& variable); void flatten(const TSourceLoc& loc, const TVariable& variable);
void flattenStruct(const TVariable& variable); int flatten(const TSourceLoc& loc, const TVariable& variable, const TType&, TFlattenData&, TString name);
void flattenArray(const TSourceLoc& loc, const TVariable& variable); int flattenStruct(const TSourceLoc& loc, const TVariable& variable, const TType&, TFlattenData&, TString name);
int flattenArray(const TSourceLoc& loc, const TVariable& variable, const TType&, TFlattenData&, TString name);
// Current state of parsing // Current state of parsing
struct TPragma contextPragma; struct TPragma contextPragma;
...@@ -252,7 +271,10 @@ protected: ...@@ -252,7 +271,10 @@ protected:
// //
TVector<TSymbol*> ioArraySymbolResizeList; TVector<TSymbol*> ioArraySymbolResizeList;
TMap<int, TVector<TVariable*>> flattenMap; TMap<int, TFlattenData> flattenMap;
TVector<int> flattenLevel; // nested postfix operator level for flattening
TVector<int> flattenOffset; // cumulative offset for flattening
unsigned int nextInLocation; unsigned int nextInLocation;
unsigned int nextOutLocation; unsigned int nextOutLocation;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment