Support specialization composite constants
Fix issue #163, support creation and reference of composite type specialization constants. e.g.: ``` layout(constant_id = 200) const float myfloat = 1.25; layout(constant_id = 201) const int myint = 14; struct structtype { float f; int i; }; const structtype outer_struct_var = {myfloat, myint}; void main(){} ``` generated code (use glslangValidator): ``` // Module Version 10000 // Generated by (magic number): 80001 // Id's are bound by 12 Capability Shader 1: ExtInstImport "GLSL.std.450" MemoryModel Logical GLSL450 EntryPoint Vertex 4 "main" Source GLSL 450 Name 4 "main" Name 10 "structtype" MemberName 10(structtype) 0 "f" MemberName 10(structtype) 1 "i" Decorate 7 SpecId 200 Decorate 9 SpecId 201 2: TypeVoid 3: TypeFunction 2 6: TypeFloat 32 7: 6(float) SpecConstant 1067450368 8: TypeInt 32 1 9: 8(int) SpecConstant 14 10(structtype): TypeStruct 6(float) 8(int) 11:10(structtype) SpecConstantComposite 7 9 4(main): 2 Function None 3 5: Label Return FunctionEnd ``` Rname two function names to match their functionalities. 1) Rename `GlslangToSpvTraverser::createSpvSpecConstant()` to `createSpvConstant()`; 2) Rename `GlslangToSpvTraverser::createSpvConstant()` to `createSpvConstantFromConstUnionArray()` Add function `GlslangToSpvTraverser::createSpvConstantFromSubTree()` to handle constant creation from sub trees (e.g.: specialization constants). Related PR: #208
This commit is contained in:
172
Test/baseResults/spv.specConstantComposite.vert.out
Normal file
172
Test/baseResults/spv.specConstantComposite.vert.out
Normal file
@@ -0,0 +1,172 @@
|
||||
spv.specConstantComposite.vert
|
||||
Warning, version 450 is not yet complete; most version-specific features are present, but some are missing.
|
||||
|
||||
|
||||
Linked vertex stage:
|
||||
|
||||
|
||||
// Module Version 10000
|
||||
// Generated by (magic number): 80001
|
||||
// Id's are bound by 106
|
||||
|
||||
Capability Shader
|
||||
Capability Float64
|
||||
1: ExtInstImport "GLSL.std.450"
|
||||
MemoryModel Logical GLSL450
|
||||
EntryPoint Vertex 4 "main" 27 105
|
||||
Source GLSL 450
|
||||
Name 4 "main"
|
||||
Name 6 "refer_primary_spec_const("
|
||||
Name 8 "refer_composite_spec_const("
|
||||
Name 10 "refer_copmosite_dot_dereference("
|
||||
Name 12 "refer_composite_bracket_dereference("
|
||||
Name 16 "refer_spec_const_array_length("
|
||||
Name 18 "declare_spec_const_in_func("
|
||||
Name 27 "color"
|
||||
Name 41 "flat_struct"
|
||||
MemberName 41(flat_struct) 0 "i"
|
||||
MemberName 41(flat_struct) 1 "f"
|
||||
MemberName 41(flat_struct) 2 "d"
|
||||
MemberName 41(flat_struct) 3 "b"
|
||||
Name 42 "nesting_struct"
|
||||
MemberName 42(nesting_struct) 0 "nested"
|
||||
MemberName 42(nesting_struct) 1 "v"
|
||||
MemberName 42(nesting_struct) 2 "i"
|
||||
Name 72 "indexable"
|
||||
Name 76 "indexable"
|
||||
Name 83 "len"
|
||||
Name 105 "global_vec4_array_with_spec_length"
|
||||
Decorate 21 SpecId 203
|
||||
Decorate 28 SpecId 200
|
||||
Decorate 32 SpecId 201
|
||||
Decorate 43 SpecId 202
|
||||
2: TypeVoid
|
||||
3: TypeFunction 2
|
||||
14: TypeInt 32 1
|
||||
15: TypeFunction 14(int)
|
||||
20: TypeBool
|
||||
21: 20(bool) SpecConstantTrue
|
||||
24: TypeFloat 32
|
||||
25: TypeVector 24(float) 4
|
||||
26: TypePointer Output 25(fvec4)
|
||||
27(color): 26(ptr) Variable Output
|
||||
28: 14(int) SpecConstant 3
|
||||
32: 24(float) SpecConstant 1078523331
|
||||
33: 25(fvec4) SpecConstantComposite 32 32 32 32
|
||||
36: 24(float) Constant 1133908460
|
||||
37: 25(fvec4) SpecConstantComposite 32 32 36 36
|
||||
40: TypeFloat 64
|
||||
41(flat_struct): TypeStruct 14(int) 24(float) 40(float) 20(bool)
|
||||
42(nesting_struct): TypeStruct 41(flat_struct) 25(fvec4) 14(int)
|
||||
43: 40(float) SpecConstant 1413754136 1074340347
|
||||
44:41(flat_struct) SpecConstantComposite 28 32 43 21
|
||||
45:42(nesting_struct) SpecConstantComposite 44 33 28
|
||||
46: 14(int) Constant 2
|
||||
51: TypeInt 32 0
|
||||
52: 51(int) Constant 0
|
||||
57: 51(int) Constant 5
|
||||
58: TypeArray 24(float) 57
|
||||
59: 24(float) Constant 1065353216
|
||||
60: 24(float) Constant 1073741824
|
||||
61: 24(float) Constant 1077936128
|
||||
62: 58 SpecConstantComposite 32 32 59 60 61
|
||||
63: 14(int) Constant 1
|
||||
68: TypeArray 14(int) 57
|
||||
69: 14(int) Constant 30
|
||||
70: 68 SpecConstantComposite 28 28 63 46 69
|
||||
71: TypePointer Function 68
|
||||
73: TypePointer Function 14(int)
|
||||
87: 24(float) Constant 1106321080
|
||||
88:41(flat_struct) SpecConstantComposite 69 87 43 21
|
||||
89: 14(int) Constant 10
|
||||
90:42(nesting_struct) SpecConstantComposite 88 37 89
|
||||
96: 20(bool) ConstantFalse
|
||||
97:41(flat_struct) SpecConstantComposite 28 32 43 96
|
||||
98: 24(float) Constant 1036831949
|
||||
99: 25(fvec4) ConstantComposite 98 98 98 98
|
||||
100:42(nesting_struct) SpecConstantComposite 97 99 28
|
||||
101: 14(int) Constant 3000
|
||||
102:42(nesting_struct) SpecConstantComposite 88 37 101
|
||||
103: TypeArray 25(fvec4) 28
|
||||
104: TypePointer Input 103
|
||||
105(global_vec4_array_with_spec_length): 104(ptr) Variable Input
|
||||
4(main): 2 Function None 3
|
||||
5: Label
|
||||
Return
|
||||
FunctionEnd
|
||||
6(refer_primary_spec_const(): 2 Function None 3
|
||||
7: Label
|
||||
SelectionMerge 23 None
|
||||
BranchConditional 21 22 23
|
||||
22: Label
|
||||
29: 24(float) ConvertSToF 28
|
||||
30: 25(fvec4) Load 27(color)
|
||||
31: 25(fvec4) VectorTimesScalar 30 29
|
||||
Store 27(color) 31
|
||||
Branch 23
|
||||
23: Label
|
||||
Return
|
||||
FunctionEnd
|
||||
8(refer_composite_spec_const(): 2 Function None 3
|
||||
9: Label
|
||||
34: 25(fvec4) Load 27(color)
|
||||
35: 25(fvec4) FAdd 34 33
|
||||
Store 27(color) 35
|
||||
38: 25(fvec4) Load 27(color)
|
||||
39: 25(fvec4) FSub 38 37
|
||||
Store 27(color) 39
|
||||
Return
|
||||
FunctionEnd
|
||||
10(refer_copmosite_dot_dereference(): 2 Function None 3
|
||||
11: Label
|
||||
47: 14(int) CompositeExtract 45 2
|
||||
48: 24(float) ConvertSToF 47
|
||||
49: 25(fvec4) Load 27(color)
|
||||
50: 25(fvec4) VectorTimesScalar 49 48
|
||||
Store 27(color) 50
|
||||
53: 24(float) CompositeExtract 33 0
|
||||
54: 25(fvec4) Load 27(color)
|
||||
55: 25(fvec4) CompositeConstruct 53 53 53 53
|
||||
56: 25(fvec4) FAdd 54 55
|
||||
Store 27(color) 56
|
||||
Return
|
||||
FunctionEnd
|
||||
12(refer_composite_bracket_dereference(): 2 Function None 3
|
||||
13: Label
|
||||
72(indexable): 71(ptr) Variable Function
|
||||
76(indexable): 71(ptr) Variable Function
|
||||
64: 24(float) CompositeExtract 62 1
|
||||
65: 25(fvec4) Load 27(color)
|
||||
66: 25(fvec4) CompositeConstruct 64 64 64 64
|
||||
67: 25(fvec4) FSub 65 66
|
||||
Store 27(color) 67
|
||||
Store 72(indexable) 70
|
||||
74: 73(ptr) AccessChain 72(indexable) 28
|
||||
75: 14(int) Load 74
|
||||
Store 76(indexable) 70
|
||||
77: 73(ptr) AccessChain 76(indexable) 75
|
||||
78: 14(int) Load 77
|
||||
79: 24(float) ConvertSToF 78
|
||||
80: 25(fvec4) Load 27(color)
|
||||
81: 25(fvec4) CompositeConstruct 79 79 79 79
|
||||
82: 25(fvec4) FDiv 80 81
|
||||
Store 27(color) 82
|
||||
Return
|
||||
FunctionEnd
|
||||
16(refer_spec_const_array_length(): 14(int) Function None 15
|
||||
17: Label
|
||||
83(len): 73(ptr) Variable Function
|
||||
Store 83(len) 28
|
||||
84: 14(int) Load 83(len)
|
||||
ReturnValue 84
|
||||
FunctionEnd
|
||||
18(declare_spec_const_in_func(): 2 Function None 3
|
||||
19: Label
|
||||
91: 14(int) CompositeExtract 90 2
|
||||
92: 24(float) ConvertSToF 91
|
||||
93: 25(fvec4) Load 27(color)
|
||||
94: 25(fvec4) CompositeConstruct 92 92 92 92
|
||||
95: 25(fvec4) FDiv 93 94
|
||||
Store 27(color) 95
|
||||
Return
|
||||
FunctionEnd
|
||||
93
Test/spv.specConstantComposite.vert
Normal file
93
Test/spv.specConstantComposite.vert
Normal file
@@ -0,0 +1,93 @@
|
||||
#version 450
|
||||
|
||||
// constant_id specified scalar spec constants
|
||||
layout(constant_id = 200) const int spec_int = 3;
|
||||
layout(constant_id = 201) const float spec_float = 3.14;
|
||||
layout(constant_id = 202) const
|
||||
double spec_double = 3.1415926535897932384626433832795;
|
||||
layout(constant_id = 203) const bool spec_bool = true;
|
||||
|
||||
const float cast_spec_float = float(spec_float);
|
||||
|
||||
// Flat struct
|
||||
struct flat_struct {
|
||||
int i;
|
||||
float f;
|
||||
double d;
|
||||
bool b;
|
||||
};
|
||||
|
||||
// Nesting struct
|
||||
struct nesting_struct {
|
||||
flat_struct nested;
|
||||
vec4 v;
|
||||
int i;
|
||||
};
|
||||
|
||||
// Expect OpSpecConstantComposite
|
||||
// Flat struct initializer
|
||||
const flat_struct spec_flat_struct_all_spec = {spec_int, spec_float,
|
||||
spec_double, spec_bool};
|
||||
const flat_struct spec_flat_struct_partial_spec = {30, 30.14, spec_double,
|
||||
spec_bool};
|
||||
|
||||
// Nesting struct initializer
|
||||
const nesting_struct nesting_struct_ctor = {
|
||||
{spec_int, spec_float, spec_double, false},
|
||||
vec4(0.1, 0.1, 0.1, 0.1),
|
||||
spec_int};
|
||||
|
||||
// Vector constructor
|
||||
const vec4 spec_vec4_all_spec =
|
||||
vec4(spec_float, spec_float, spec_float, spec_float);
|
||||
const vec4 spec_vec4_partial_spec =
|
||||
vec4(spec_float, spec_float, 300.14, 300.14);
|
||||
|
||||
// Struct nesting constructor
|
||||
const nesting_struct spec_nesting_struct_all_spec = {
|
||||
spec_flat_struct_all_spec, spec_vec4_all_spec, spec_int};
|
||||
const nesting_struct spec_nesting_struct_partial_spec = {
|
||||
spec_flat_struct_partial_spec, spec_vec4_partial_spec, 3000};
|
||||
|
||||
const float spec_float_array[5] = {spec_float, spec_float, 1.0, 2.0, 3.0};
|
||||
const int spec_int_array[5] = {spec_int, spec_int, 1, 2, 30};
|
||||
|
||||
// global_vec4_array_with_spec_length is not a spec constant, but its array
|
||||
// size is. When calling global_vec4_array_with_spec_length.length(), A
|
||||
// TIntermSymbol Node shoule be returned, instead of a TIntermConstantUnion
|
||||
// node which represents a known constant value.
|
||||
in vec4 global_vec4_array_with_spec_length[spec_int];
|
||||
|
||||
out vec4 color;
|
||||
|
||||
void refer_primary_spec_const() {
|
||||
if (spec_bool) color *= spec_int;
|
||||
}
|
||||
|
||||
void refer_composite_spec_const() {
|
||||
color += spec_vec4_all_spec;
|
||||
color -= spec_vec4_partial_spec;
|
||||
}
|
||||
|
||||
void refer_copmosite_dot_dereference() {
|
||||
color *= spec_nesting_struct_all_spec.i;
|
||||
color += spec_vec4_all_spec.x;
|
||||
}
|
||||
|
||||
void refer_composite_bracket_dereference() {
|
||||
color -= spec_float_array[1];
|
||||
color /= spec_int_array[spec_int_array[spec_int]];
|
||||
}
|
||||
|
||||
int refer_spec_const_array_length() {
|
||||
int len = global_vec4_array_with_spec_length.length();
|
||||
return len;
|
||||
}
|
||||
|
||||
void declare_spec_const_in_func() {
|
||||
const nesting_struct spec_const_declared_in_func = {
|
||||
spec_flat_struct_partial_spec, spec_vec4_partial_spec, 10};
|
||||
color /= spec_const_declared_in_func.i;
|
||||
}
|
||||
|
||||
void main() {}
|
||||
@@ -102,6 +102,7 @@ spv.pushConstant.vert
|
||||
spv.subpass.frag
|
||||
spv.specConstant.vert
|
||||
spv.specConstant.comp
|
||||
spv.specConstantComposite.vert
|
||||
# GLSL-level semantics
|
||||
vulkan.frag
|
||||
vulkan.vert
|
||||
|
||||
Reference in New Issue
Block a user