Commit a8d9faba by John Kessenich

Merge pull request #216 from Qining/fix-non-const-array-size-from-spec-const

SPV: Generate OpSpecConstantOp for all subtrees indicated by the AST.
parents aa0298bf 75d1d801
......@@ -66,6 +66,27 @@ namespace {
// or a different instruction sequence to do something gets used).
const int GeneratorVersion = 1;
namespace {
class SpecConstantOpModeGuard {
public:
SpecConstantOpModeGuard(spv::Builder* builder)
: builder_(builder) {
previous_flag_ = builder->isInSpecConstCodeGenMode();
}
~SpecConstantOpModeGuard() {
previous_flag_ ? builder_->setToSpecConstCodeGenMode()
: builder_->setToNormalCodeGenMode();
}
void turnOnSpecConstantOpMode() {
builder_->setToSpecConstCodeGenMode();
}
private:
spv::Builder* builder_;
bool previous_flag_;
};
}
//
// The main holder of information for translating glslang to SPIR-V.
//
......@@ -757,6 +778,10 @@ TGlslangToSpvTraverser::~TGlslangToSpvTraverser()
//
void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
{
SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
if (symbol->getType().getQualifier().isSpecConstant())
spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
// getSymbolId() will set up all the IO decorations on the first call.
// Formal function parameters were mapped during makeFunctions().
spv::Id id = getSymbolId(symbol);
......@@ -794,6 +819,10 @@ void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node)
{
SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
if (node->getType().getQualifier().isSpecConstant())
spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
// First, handle special cases
switch (node->getOp()) {
case glslang::EOpAssign:
......@@ -966,6 +995,10 @@ bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::T
bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node)
{
SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder);
if (node->getType().getQualifier().isSpecConstant())
spec_constant_op_mode_setter.turnOnSpecConstantOpMode();
spv::Id result = spv::NoResult;
// try texturing first
......@@ -1927,6 +1960,7 @@ spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arra
glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim);
if (specNode != nullptr) {
builder.clearAccessChain();
// SpecConstantOpModeGuard set_to_spec_const_mode(&builder);
specNode->traverse(this);
return accessChainLoad(specNode->getAsTyped()->getType());
}
......@@ -3868,25 +3902,6 @@ spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glsla
return builder.makeCompositeConstant(typeId, spvConsts);
}
namespace {
class SpecConstantOpModeGuard {
public:
SpecConstantOpModeGuard(spv::Builder* builder)
: builder_(builder) {
previous_flag_ = builder->isInSpecConstCodeGenMode();
builder->setToSpecConstCodeGenMode();
}
~SpecConstantOpModeGuard() {
previous_flag_ ? builder_->setToSpecConstCodeGenMode()
: builder_->setToNormalCodeGenMode();
}
private:
spv::Builder* builder_;
bool previous_flag_;
};
}
// Create constant ID from const initializer sub tree.
spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstSubTree(
glslang::TIntermTyped* subTree)
......@@ -3919,22 +3934,12 @@ spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstSubTree(
} else if (glslang::TIntermBinary* bn = subTree->getAsBinaryNode()) {
// Binary operation node, we should generate OpSpecConstantOp <binary op>
// This case should only happen when Specialization Constants are involved.
// Spec constants defined with binary operations and other constants requires
// OpSpecConstantOp instruction.
SpecConstantOpModeGuard set_to_spec_const_mode(&builder);
bn->traverse(this);
return accessChainLoad(bn->getType());
} else if (glslang::TIntermUnary* un = subTree->getAsUnaryNode()) {
// Unary operation node, similar to binary operation node, should only
// happen when specialization constants are involved.
// Spec constants defined with unary operations and other constants requires
// OpSpecConstantOp instruction.
SpecConstantOpModeGuard set_to_spec_const_mode(&builder);
un->traverse(this);
return accessChainLoad(un->getType());
......
......@@ -39,16 +39,16 @@ Linked compute stage:
15: TypeVector 6(int) 3
16: 15(ivec3) SpecConstantComposite 12 13 14
17: 6(int) Constant 0
18: 6(int) SpecConstantOp 81 16 0
19: 6(int) Constant 1
20: 6(int) SpecConstantOp 81 16 1(GLSL.std.450)
21: 6(int) SpecConstantOp 132 18 20
22: 6(int) Constant 2
23: 6(int) SpecConstantOp 81 16 2
24: 6(int) SpecConstantOp 132 21 23
25: TypePointer Uniform 6(int)
4(main): 2 Function None 3
5: Label
18: 6(int) CompositeExtract 16 0
20: 6(int) CompositeExtract 16 1
21: 6(int) IMul 18 20
23: 6(int) CompositeExtract 16 2
24: 6(int) IMul 21 23
26: 25(ptr) AccessChain 9(bi) 11
Store 26 24
Return
......
......@@ -58,17 +58,25 @@ Linked vertex stage:
30: 29(bool) SpecConstantTrue
33: TypeInt 32 0
34: 33(int) SpecConstant 2
35: 6(float) SpecConstantOp 112 34
38: TypeFloat 64
39: 38(float) SpecConstant 1413754136 1074340347
40: 6(float) SpecConstant 1078523331
41: 38(float) SpecConstantOp 115 40
42: 38(float) SpecConstantOp 136 39 41
43: 6(float) SpecConstantOp 115 42
50: 8(int) SpecConstant 12
51: TypeArray 7(fvec4) 50
52: TypePointer Input 51
53(dupUcol): 52(ptr) Variable Input
60: 29(bool) SpecConstantTrue
63: 33(int) SpecConstant 2
64: 6(float) SpecConstantOp 112 63
67: 38(float) SpecConstant 1413754136 1074340347
68: 6(float) SpecConstant 1078523331
69: 38(float) SpecConstantOp 115 68
70: 38(float) SpecConstantOp 136 67 69
71: 6(float) SpecConstantOp 115 70
75: TypePointer Function 8(int)
77: 8(int) SpecConstant 8
4(main): 2 Function None 3
......@@ -81,15 +89,11 @@ Linked vertex stage:
SelectionMerge 32 None
BranchConditional 30 31 32
31: Label
35: 6(float) ConvertUToF 34
36: 7(fvec4) Load 20(color)
37: 7(fvec4) VectorTimesScalar 36 35
Store 20(color) 37
Branch 32
32: Label
41: 38(float) FConvert 40
42: 38(float) FDiv 39 41
43: 6(float) FConvert 42
44: 7(fvec4) Load 20(color)
45: 7(fvec4) CompositeConstruct 43 43 43 43
46: 7(fvec4) FAdd 44 45
......@@ -113,15 +117,11 @@ Linked vertex stage:
SelectionMerge 62 None
BranchConditional 60 61 62
61: Label
64: 6(float) ConvertUToF 63
65: 7(fvec4) Load 20(color)
66: 7(fvec4) VectorTimesScalar 65 64
Store 20(color) 66
Branch 62
62: Label
69: 38(float) FConvert 68
70: 38(float) FDiv 67 69
71: 6(float) FConvert 70
72: 7(fvec4) Load 20(color)
73: 7(fvec4) CompositeConstruct 71 71 71 71
74: 7(fvec4) FAdd 72 73
......
......@@ -51,6 +51,7 @@ Linked vertex stage:
26: TypePointer Output 25(fvec4)
27(color): 26(ptr) Variable Output
28: 14(int) SpecConstant 3
29: 24(float) SpecConstantOp 111 28
32: 24(float) SpecConstant 1078523331
33: 25(fvec4) SpecConstantComposite 32 32 32 32
36: 24(float) Constant 1133908460
......@@ -76,6 +77,7 @@ Linked vertex stage:
70: 68 SpecConstantComposite 28 28 63 46 69
71: TypePointer Function 68
73: TypePointer Function 14(int)
79: 24(float) SpecConstantOp 111 78
87: 24(float) Constant 1106321080
88:41(flat_struct) SpecConstantComposite 69 87 43 21
89: 14(int) Constant 10
......@@ -99,7 +101,6 @@ Linked vertex stage:
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
......@@ -146,7 +147,6 @@ Linked vertex stage:
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
......
......@@ -86,5 +86,10 @@ const ivec2 iv_yx = iv.yx;
const ivec3 iv_zyx = iv.zyx;
const ivec4 iv_yzxw = iv.yzxw;
int non_const_array_size_from_spec_const() {
int array[sp_int + 2];
return array[sp_int + 1];
}
void main() {}
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