HLSL: allow trailing commas in initializer lists & scalar initialization

This commit is contained in:
steve-lunarg 2016-07-30 10:36:09 -06:00
parent b38f071605
commit fe5a3ff2f3
5 changed files with 199 additions and 5 deletions

View File

@ -0,0 +1,176 @@
hlsl.init2.frag
Shader version: 450
gl_FragCoord origin is upper left
0:? Sequence
0:23 Function Definition: Test1( (global void)
0:3 Function Parameters:
0:? Sequence
0:5 Sequence
0:5 move second child to first child (temp structure{temp 2-component vector of float a})
0:5 'test1' (temp structure{temp 2-component vector of float a})
0:5 Constant:
0:5 1.000000
0:5 2.000000
0:9 Sequence
0:9 move second child to first child (temp structure{temp 2-component vector of float a})
0:9 'test2' (temp structure{temp 2-component vector of float a})
0:9 Constant:
0:9 3.000000
0:9 4.000000
0:17 Sequence
0:17 move second child to first child (temp float)
0:17 'test4' (temp float)
0:17 Constant:
0:17 7.000000
0:20 Sequence
0:20 move second child to first child (temp structure{temp float a, temp float b, temp float c})
0:20 'test5' (temp structure{temp float a, temp float b, temp float c})
0:20 Constant:
0:20 8.000000
0:20 9.000000
0:20 10.000000
0:33 Function Definition: main( (global structure{temp 4-component vector of float color})
0:26 Function Parameters:
0:? Sequence
0:27 Function Call: Test1( (global void)
0:30 move second child to first child (temp 4-component vector of float)
0:30 color: direct index for structure (temp 4-component vector of float)
0:30 'ps_output' (temp structure{temp 4-component vector of float color})
0:30 Constant:
0:30 0 (const int)
0:30 Constant:
0:30 1.000000
0:30 1.000000
0:30 1.000000
0:30 1.000000
0:31 Branch: Return with expression
0:31 'ps_output' (temp structure{temp 4-component vector of float color})
0:? Linker Objects
Linked fragment stage:
Shader version: 450
gl_FragCoord origin is upper left
0:? Sequence
0:23 Function Definition: Test1( (global void)
0:3 Function Parameters:
0:? Sequence
0:5 Sequence
0:5 move second child to first child (temp structure{temp 2-component vector of float a})
0:5 'test1' (temp structure{temp 2-component vector of float a})
0:5 Constant:
0:5 1.000000
0:5 2.000000
0:9 Sequence
0:9 move second child to first child (temp structure{temp 2-component vector of float a})
0:9 'test2' (temp structure{temp 2-component vector of float a})
0:9 Constant:
0:9 3.000000
0:9 4.000000
0:17 Sequence
0:17 move second child to first child (temp float)
0:17 'test4' (temp float)
0:17 Constant:
0:17 7.000000
0:20 Sequence
0:20 move second child to first child (temp structure{temp float a, temp float b, temp float c})
0:20 'test5' (temp structure{temp float a, temp float b, temp float c})
0:20 Constant:
0:20 8.000000
0:20 9.000000
0:20 10.000000
0:33 Function Definition: main( (global structure{temp 4-component vector of float color})
0:26 Function Parameters:
0:? Sequence
0:27 Function Call: Test1( (global void)
0:30 move second child to first child (temp 4-component vector of float)
0:30 color: direct index for structure (temp 4-component vector of float)
0:30 'ps_output' (temp structure{temp 4-component vector of float color})
0:30 Constant:
0:30 0 (const int)
0:30 Constant:
0:30 1.000000
0:30 1.000000
0:30 1.000000
0:30 1.000000
0:31 Branch: Return with expression
0:31 'ps_output' (temp structure{temp 4-component vector of float color})
0:? Linker Objects
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 44
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "main"
ExecutionMode 4 OriginUpperLeft
Source HLSL 450
Name 4 "main"
Name 6 "Test1("
Name 10 "mystruct"
MemberName 10(mystruct) 0 "a"
Name 12 "test1"
Name 17 "test2"
Name 23 "test4"
Name 25 "mystruct2"
MemberName 25(mystruct2) 0 "a"
MemberName 25(mystruct2) 1 "b"
MemberName 25(mystruct2) 2 "c"
Name 27 "test5"
Name 34 "PS_OUTPUT"
MemberName 34(PS_OUTPUT) 0 "color"
Name 36 "ps_output"
2: TypeVoid
3: TypeFunction 2
8: TypeFloat 32
9: TypeVector 8(float) 2
10(mystruct): TypeStruct 9(fvec2)
11: TypePointer Function 10(mystruct)
13: 8(float) Constant 1065353216
14: 8(float) Constant 1073741824
15: 9(fvec2) ConstantComposite 13 14
16:10(mystruct) ConstantComposite 15
18: 8(float) Constant 1077936128
19: 8(float) Constant 1082130432
20: 9(fvec2) ConstantComposite 18 19
21:10(mystruct) ConstantComposite 20
22: TypePointer Function 8(float)
24: 8(float) Constant 1088421888
25(mystruct2): TypeStruct 8(float) 8(float) 8(float)
26: TypePointer Function 25(mystruct2)
28: 8(float) Constant 1090519040
29: 8(float) Constant 1091567616
30: 8(float) Constant 1092616192
31:25(mystruct2) ConstantComposite 28 29 30
33: TypeVector 8(float) 4
34(PS_OUTPUT): TypeStruct 33(fvec4)
35: TypePointer Function 34(PS_OUTPUT)
37: TypeInt 32 1
38: 37(int) Constant 0
39: 33(fvec4) ConstantComposite 13 13 13 13
40: TypePointer Function 33(fvec4)
4(main): 2 Function None 3
5: Label
36(ps_output): 35(ptr) Variable Function
32: 2 FunctionCall 6(Test1()
41: 40(ptr) AccessChain 36(ps_output) 38
Store 41 39
42:34(PS_OUTPUT) Load 36(ps_output)
ReturnValue 42
FunctionEnd
6(Test1(): 2 Function None 3
7: Label
12(test1): 11(ptr) Variable Function
17(test2): 11(ptr) Variable Function
23(test4): 22(ptr) Variable Function
27(test5): 26(ptr) Variable Function
Store 12(test1) 16
Store 17(test2) 21
Store 23(test4) 24
Store 27(test5) 31
Return
FunctionEnd

View File

@ -3,20 +3,29 @@ void Test1()
{
struct mystruct { float2 a; };
mystruct test1 = {
{ 1, 2, }, // test trailing commas
{ 1, 2, }, // test trailing commas in list
};
mystruct test2 = {
{ { 1, 2, } }, // test unneeded levels
float2(3, 4),
};
float test3 = { 1 } ; // test scalar initialization
// mystruct test3 = {
// { { 5, 6, } }, // TODO: test unneeded levels
// };
float test4 = { 7, } ; // test scalar initialization
struct mystruct2 { float a; float b; float c; };
mystruct2 test5 = { {8,}, {9,}, {10}, };
}
struct PS_OUTPUT { float4 color : SV_Target0; };
PS_OUTPUT main()
{
Test1();
PS_OUTPUT ps_output;
ps_output.color = 1.0;
return ps_output;

View File

@ -97,6 +97,7 @@ INSTANTIATE_TEST_CASE_P(
{"hlsl.if.frag", "PixelShaderFunction"},
{"hlsl.inoutquals.frag", "main"},
{"hlsl.init.frag", "ShaderFunction"},
{"hlsl.init2.frag", "main"},
{"hlsl.intrinsics.barriers.comp", "ComputeShaderFunction"},
{"hlsl.intrinsics.comp", "ComputeShaderFunction"},
{"hlsl.intrinsics.evalfns.frag", "main"},

View File

@ -1549,8 +1549,11 @@ bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
node = intermediate.growAggregate(node, expr, loc);
// COMMA
if (acceptTokenClass(EHTokComma))
if (acceptTokenClass(EHTokComma)) {
if (acceptTokenClass(EHTokRightBrace)) // allow trailing comma
return true;
continue;
}
// RIGHT_BRACE
if (acceptTokenClass(EHTokRightBrace))

View File

@ -3727,7 +3727,12 @@ TIntermTyped* HlslParseContext::convertInitializerList(const TSourceLoc& loc, co
error(loc, "wrong vector size (or rows in a matrix column):", "initializer list", type.getCompleteString().c_str());
return nullptr;
}
} else {
} else if (type.isScalar()) {
if ((int)initList->getSequence().size() != 1) {
error(loc, "scalar expected one element:", "initializer list", type.getCompleteString().c_str());
return nullptr;
}
} else {
error(loc, "unexpected initializer-list type:", "initializer list", type.getCompleteString().c_str());
return nullptr;
}