diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp index 81486ca5..6a86e21d 100755 --- a/SPIRV/GlslangToSpv.cpp +++ b/SPIRV/GlslangToSpv.cpp @@ -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 // 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()); diff --git a/Test/baseResults/spv.specConstant.comp.out b/Test/baseResults/spv.specConstant.comp.out index d1c9b0a6..2f16f04d 100644 --- a/Test/baseResults/spv.specConstant.comp.out +++ b/Test/baseResults/spv.specConstant.comp.out @@ -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 diff --git a/Test/baseResults/spv.specConstant.vert.out b/Test/baseResults/spv.specConstant.vert.out index dadab07c..9deec593 100644 --- a/Test/baseResults/spv.specConstant.vert.out +++ b/Test/baseResults/spv.specConstant.vert.out @@ -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 diff --git a/Test/baseResults/spv.specConstantComposite.vert.out b/Test/baseResults/spv.specConstantComposite.vert.out index 5e2dfa4a..b8dfd11b 100644 --- a/Test/baseResults/spv.specConstantComposite.vert.out +++ b/Test/baseResults/spv.specConstantComposite.vert.out @@ -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 diff --git a/Test/baseResults/spv.specConstantOperations.vert.out b/Test/baseResults/spv.specConstantOperations.vert.out index 476efc90..a6c5993c 100644 --- a/Test/baseResults/spv.specConstantOperations.vert.out +++ b/Test/baseResults/spv.specConstantOperations.vert.out @@ -7,7 +7,7 @@ Linked vertex stage: // Module Version 10000 // Generated by (magic number): 80001 -// Id's are bound by 94 +// Id's are bound by 107 Capability Shader Capability Float64 @@ -16,101 +16,116 @@ Linked vertex stage: EntryPoint Vertex 4 "main" Source GLSL 450 Name 4 "main" - Decorate 7 SpecId 200 - Decorate 9 SpecId 201 - Decorate 11 SpecId 202 - Decorate 12 SpecId 203 + Name 8 "non_const_array_size_from_spec_const(" + Name 15 "array" + Decorate 10 SpecId 201 + Decorate 24 SpecId 200 + Decorate 26 SpecId 202 + Decorate 27 SpecId 203 2: TypeVoid 3: TypeFunction 2 - 6: TypeFloat 32 - 7: 6(float) SpecConstant 1078530010 - 8: TypeInt 32 1 - 9: 8(int) SpecConstant 10 - 10: TypeInt 32 0 - 11: 10(int) SpecConstant 100 - 12: 8(int) SpecConstant 4294967286 - 13: TypeFloat 64 - 14: 13(float) SpecConstantOp 115 7 - 15: 6(float) SpecConstantOp 115 14 - 16: 8(int) SpecConstantOp 126 9 - 17: 8(int) SpecConstantOp 200 9 - 18: 8(int) Constant 2 - 19: 8(int) SpecConstantOp 128 9 18 - 20: 8(int) SpecConstantOp 128 9 18 - 21: 8(int) Constant 3 - 22: 8(int) SpecConstantOp 130 20 21 - 23: 8(int) Constant 4 - 24: 8(int) SpecConstantOp 130 19 23 - 25: 8(int) SpecConstantOp 132 12 18 - 26: 10(int) Constant 2 - 27: 10(int) SpecConstantOp 132 11 26 - 28: 8(int) Constant 5 - 29: 8(int) SpecConstantOp 135 25 28 - 30: 10(int) Constant 5 - 31: 10(int) SpecConstantOp 134 27 30 - 32: 8(int) SpecConstantOp 139 12 23 - 33: 10(int) Constant 4 - 34: 10(int) SpecConstantOp 137 11 33 - 35: 8(int) SpecConstantOp 132 12 21 - 36: 8(int) SpecConstantOp 135 35 28 - 37: 8(int) Constant 10 - 38: 8(int) SpecConstantOp 195 12 37 - 39: 8(int) Constant 20 - 40: 10(int) SpecConstantOp 194 11 39 - 41: 8(int) Constant 1 - 42: 8(int) SpecConstantOp 196 12 41 - 43: 10(int) SpecConstantOp 196 11 18 - 44: 8(int) Constant 256 - 45: 8(int) SpecConstantOp 197 12 44 - 46: 10(int) Constant 512 - 47: 10(int) SpecConstantOp 198 11 46 - 48: TypeBool - 49: 48(bool) SpecConstantOp 177 9 12 - 50: 48(bool) SpecConstantOp 170 11 11 - 51: 48(bool) SpecConstantOp 173 9 12 - 52: TypeVector 8(int) 4 - 53: 8(int) Constant 30 - 54: 52(ivec4) SpecConstantComposite 39 53 9 9 - 55: TypeVector 10(int) 4 - 56: 10(int) Constant 4294967295 - 57: 10(int) Constant 4294967294 - 58: 55(ivec4) SpecConstantComposite 11 11 56 57 - 59: TypeVector 6(float) 4 - 60: 6(float) Constant 1067450368 - 61: 59(fvec4) SpecConstantComposite 7 60 7 60 - 62: TypeVector 13(float) 4 - 63: 62(fvec4) SpecConstantOp 115 61 - 64: 59(fvec4) SpecConstantOp 115 63 - 65: 52(ivec4) SpecConstantOp 200 54 - 66: 52(ivec4) SpecConstantOp 126 54 - 67: 52(ivec4) ConstantComposite 18 18 18 18 - 68: 52(ivec4) SpecConstantOp 128 54 67 - 69: 52(ivec4) SpecConstantOp 128 54 67 - 70: 52(ivec4) ConstantComposite 21 21 21 21 - 71: 52(ivec4) SpecConstantOp 130 69 70 - 72: 52(ivec4) ConstantComposite 23 23 23 23 - 73: 52(ivec4) SpecConstantOp 130 71 72 - 74: 52(ivec4) SpecConstantOp 132 54 67 - 75: 52(ivec4) ConstantComposite 28 28 28 28 - 76: 52(ivec4) SpecConstantOp 135 74 75 - 77: 52(ivec4) SpecConstantOp 139 54 72 - 78: 52(ivec4) ConstantComposite 37 37 37 37 - 79: 52(ivec4) SpecConstantOp 195 54 78 - 80: 52(ivec4) SpecConstantOp 196 54 67 - 81: 8(int) Constant 1024 - 82: 52(ivec4) ConstantComposite 81 81 81 81 - 83: 52(ivec4) SpecConstantOp 197 54 82 - 84: 10(int) Constant 2048 - 85: 55(ivec4) ConstantComposite 84 84 84 84 - 86: 55(ivec4) SpecConstantOp 198 58 85 - 87: 10(int) Constant 0 - 88: 8(int) SpecConstantOp 81 54 0 - 89: TypeVector 8(int) 2 - 90: 89(ivec2) SpecConstantOp 79 54 54 1(GLSL.std.450) 0 - 91: TypeVector 8(int) 3 - 92: 91(ivec3) SpecConstantOp 79 54 54 2 1(GLSL.std.450) 0 - 93: 52(ivec4) SpecConstantOp 79 54 54 1(GLSL.std.450) 2 0 3 + 6: TypeInt 32 1 + 7: TypeFunction 6(int) + 10: 6(int) SpecConstant 10 + 11: 6(int) Constant 2 + 12: 6(int) SpecConstantOp 128 10 11 + 13: TypeArray 6(int) 12 + 14: TypePointer Function 13 + 16: 6(int) Constant 1 + 17: 6(int) SpecConstantOp 128 10 16 + 18: TypePointer Function 6(int) + 23: TypeFloat 32 + 24: 23(float) SpecConstant 1078530010 + 25: TypeInt 32 0 + 26: 25(int) SpecConstant 100 + 27: 6(int) SpecConstant 4294967286 + 28: TypeFloat 64 + 29: 28(float) SpecConstantOp 115 24 + 30: 23(float) SpecConstantOp 115 29 + 31: 6(int) SpecConstantOp 126 10 + 32: 6(int) SpecConstantOp 200 10 + 33: 6(int) SpecConstantOp 128 10 11 + 34: 6(int) SpecConstantOp 128 10 11 + 35: 6(int) Constant 3 + 36: 6(int) SpecConstantOp 130 34 35 + 37: 6(int) Constant 4 + 38: 6(int) SpecConstantOp 130 33 37 + 39: 6(int) SpecConstantOp 132 27 11 + 40: 25(int) Constant 2 + 41: 25(int) SpecConstantOp 132 26 40 + 42: 6(int) Constant 5 + 43: 6(int) SpecConstantOp 135 39 42 + 44: 25(int) Constant 5 + 45: 25(int) SpecConstantOp 134 41 44 + 46: 6(int) SpecConstantOp 139 27 37 + 47: 25(int) Constant 4 + 48: 25(int) SpecConstantOp 137 26 47 + 49: 6(int) SpecConstantOp 132 27 35 + 50: 6(int) SpecConstantOp 135 49 42 + 51: 6(int) Constant 10 + 52: 6(int) SpecConstantOp 195 27 51 + 53: 6(int) Constant 20 + 54: 25(int) SpecConstantOp 194 26 53 + 55: 6(int) SpecConstantOp 196 27 16 + 56: 25(int) SpecConstantOp 196 26 11 + 57: 6(int) Constant 256 + 58: 6(int) SpecConstantOp 197 27 57 + 59: 25(int) Constant 512 + 60: 25(int) SpecConstantOp 198 26 59 + 61: TypeBool + 62: 61(bool) SpecConstantOp 177 10 27 + 63: 61(bool) SpecConstantOp 170 26 26 + 64: 61(bool) SpecConstantOp 173 10 27 + 65: TypeVector 6(int) 4 + 66: 6(int) Constant 30 + 67: 65(ivec4) SpecConstantComposite 53 66 10 10 + 68: TypeVector 25(int) 4 + 69: 25(int) Constant 4294967295 + 70: 25(int) Constant 4294967294 + 71: 68(ivec4) SpecConstantComposite 26 26 69 70 + 72: TypeVector 23(float) 4 + 73: 23(float) Constant 1067450368 + 74: 72(fvec4) SpecConstantComposite 24 73 24 73 + 75: TypeVector 28(float) 4 + 76: 75(fvec4) SpecConstantOp 115 74 + 77: 72(fvec4) SpecConstantOp 115 76 + 78: 65(ivec4) SpecConstantOp 200 67 + 79: 65(ivec4) SpecConstantOp 126 67 + 80: 65(ivec4) ConstantComposite 11 11 11 11 + 81: 65(ivec4) SpecConstantOp 128 67 80 + 82: 65(ivec4) SpecConstantOp 128 67 80 + 83: 65(ivec4) ConstantComposite 35 35 35 35 + 84: 65(ivec4) SpecConstantOp 130 82 83 + 85: 65(ivec4) ConstantComposite 37 37 37 37 + 86: 65(ivec4) SpecConstantOp 130 84 85 + 87: 65(ivec4) SpecConstantOp 132 67 80 + 88: 65(ivec4) ConstantComposite 42 42 42 42 + 89: 65(ivec4) SpecConstantOp 135 87 88 + 90: 65(ivec4) SpecConstantOp 139 67 85 + 91: 65(ivec4) ConstantComposite 51 51 51 51 + 92: 65(ivec4) SpecConstantOp 195 67 91 + 93: 65(ivec4) SpecConstantOp 196 67 80 + 94: 6(int) Constant 1024 + 95: 65(ivec4) ConstantComposite 94 94 94 94 + 96: 65(ivec4) SpecConstantOp 197 67 95 + 97: 25(int) Constant 2048 + 98: 68(ivec4) ConstantComposite 97 97 97 97 + 99: 68(ivec4) SpecConstantOp 198 71 98 + 100: 25(int) Constant 0 + 101: 6(int) SpecConstantOp 81 67 0 + 102: TypeVector 6(int) 2 + 103: 102(ivec2) SpecConstantOp 79 67 67 1(GLSL.std.450) 0 + 104: TypeVector 6(int) 3 + 105: 104(ivec3) SpecConstantOp 79 67 67 2 1(GLSL.std.450) 0 + 106: 65(ivec4) SpecConstantOp 79 67 67 1(GLSL.std.450) 2 0 3 4(main): 2 Function None 3 5: Label Return FunctionEnd +8(non_const_array_size_from_spec_const(): 6(int) Function None 7 + 9: Label + 15(array): 14(ptr) Variable Function + 19: 18(ptr) AccessChain 15(array) 17 + 20: 6(int) Load 19 + ReturnValue 20 + FunctionEnd diff --git a/Test/spv.specConstantOperations.vert b/Test/spv.specConstantOperations.vert index 9de96aee..9311270f 100644 --- a/Test/spv.specConstantOperations.vert +++ b/Test/spv.specConstantOperations.vert @@ -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() {}