From 78a6b788103c95ef77722c1688082e3ceb384b11 Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Wed, 6 Apr 2016 13:32:44 -0600 Subject: [PATCH] Front-end: Get the right set of nodes marked as spec-const. This is according to the expected KHR_vulkan_glsl without floating point. So, floating-point spec-const operations no longer work, and that's reflected in the tests. --- Test/baseResults/spv.specConstant.vert.out | 16 +- .../spv.specConstantComposite.vert.out | 4 +- .../spv.specConstantOperations.vert.out | 155 +++++++++--------- Test/spv.specConstantOperations.vert | 8 +- glslang/MachineIndependent/Intermediate.cpp | 96 ++++++++++- .../MachineIndependent/localintermediate.h | 2 + 6 files changed, 180 insertions(+), 101 deletions(-) diff --git a/Test/baseResults/spv.specConstant.vert.out b/Test/baseResults/spv.specConstant.vert.out index 9deec593..dadab07c 100644 --- a/Test/baseResults/spv.specConstant.vert.out +++ b/Test/baseResults/spv.specConstant.vert.out @@ -58,25 +58,17 @@ 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 @@ -89,11 +81,15 @@ 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 @@ -117,11 +113,15 @@ 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 b8dfd11b..5e2dfa4a 100644 --- a/Test/baseResults/spv.specConstantComposite.vert.out +++ b/Test/baseResults/spv.specConstantComposite.vert.out @@ -51,7 +51,6 @@ 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 @@ -77,7 +76,6 @@ 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 @@ -101,6 +99,7 @@ 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 @@ -147,6 +146,7 @@ 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 a6c5993c..76853d6f 100644 --- a/Test/baseResults/spv.specConstantOperations.vert.out +++ b/Test/baseResults/spv.specConstantOperations.vert.out @@ -7,10 +7,9 @@ Linked vertex stage: // Module Version 10000 // Generated by (magic number): 80001 -// Id's are bound by 107 +// Id's are bound by 101 Capability Shader - Capability Float64 1: ExtInstImport "GLSL.std.450" MemoryModel Logical GLSL450 EntryPoint Vertex 4 "main" @@ -39,85 +38,79 @@ Linked vertex stage: 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 + 28: 6(int) SpecConstantOp 126 10 + 29: 6(int) SpecConstantOp 200 10 + 30: 6(int) SpecConstantOp 128 10 11 + 31: 6(int) SpecConstantOp 128 10 11 + 32: 6(int) Constant 3 + 33: 6(int) SpecConstantOp 130 31 32 + 34: 6(int) Constant 4 + 35: 6(int) SpecConstantOp 130 30 34 + 36: 6(int) SpecConstantOp 132 27 11 + 37: 25(int) Constant 2 + 38: 25(int) SpecConstantOp 132 26 37 + 39: 6(int) Constant 5 + 40: 6(int) SpecConstantOp 135 36 39 + 41: 25(int) Constant 5 + 42: 25(int) SpecConstantOp 134 38 41 + 43: 6(int) SpecConstantOp 139 27 34 + 44: 25(int) Constant 4 + 45: 25(int) SpecConstantOp 137 26 44 + 46: 6(int) SpecConstantOp 132 27 32 + 47: 6(int) SpecConstantOp 135 46 39 + 48: 6(int) Constant 10 + 49: 6(int) SpecConstantOp 195 27 48 + 50: 6(int) Constant 20 + 51: 25(int) SpecConstantOp 194 26 50 + 52: 6(int) SpecConstantOp 196 27 16 + 53: 25(int) SpecConstantOp 196 26 11 + 54: 6(int) Constant 256 + 55: 6(int) SpecConstantOp 197 27 54 + 56: 25(int) Constant 512 + 57: 25(int) SpecConstantOp 198 26 56 + 58: TypeBool + 59: 58(bool) SpecConstantOp 177 10 27 + 60: 58(bool) SpecConstantOp 170 26 26 + 61: 58(bool) SpecConstantOp 173 10 27 + 62: TypeVector 6(int) 4 + 63: 6(int) Constant 30 + 64: 62(ivec4) SpecConstantComposite 50 63 10 10 + 65: TypeVector 25(int) 4 + 66: 25(int) Constant 4294967295 + 67: 25(int) Constant 4294967294 + 68: 65(ivec4) SpecConstantComposite 26 26 66 67 + 69: TypeVector 23(float) 4 + 70: 23(float) Constant 1067450368 + 71: 69(fvec4) SpecConstantComposite 24 70 24 70 + 72: 62(ivec4) SpecConstantOp 200 64 + 73: 62(ivec4) SpecConstantOp 126 64 + 74: 62(ivec4) ConstantComposite 11 11 11 11 + 75: 62(ivec4) SpecConstantOp 128 64 74 + 76: 62(ivec4) SpecConstantOp 128 64 74 + 77: 62(ivec4) ConstantComposite 32 32 32 32 + 78: 62(ivec4) SpecConstantOp 130 76 77 + 79: 62(ivec4) ConstantComposite 34 34 34 34 + 80: 62(ivec4) SpecConstantOp 130 78 79 + 81: 62(ivec4) SpecConstantOp 132 64 74 + 82: 62(ivec4) ConstantComposite 39 39 39 39 + 83: 62(ivec4) SpecConstantOp 135 81 82 + 84: 62(ivec4) SpecConstantOp 139 64 79 + 85: 62(ivec4) ConstantComposite 48 48 48 48 + 86: 62(ivec4) SpecConstantOp 195 64 85 + 87: 62(ivec4) SpecConstantOp 196 64 74 + 88: 6(int) Constant 1024 + 89: 62(ivec4) ConstantComposite 88 88 88 88 + 90: 62(ivec4) SpecConstantOp 197 64 89 + 91: 25(int) Constant 2048 + 92: 65(ivec4) ConstantComposite 91 91 91 91 + 93: 65(ivec4) SpecConstantOp 198 68 92 + 94: 25(int) Constant 0 + 95: 6(int) SpecConstantOp 81 64 0 + 96: TypeVector 6(int) 2 + 97: 96(ivec2) SpecConstantOp 79 64 64 1(GLSL.std.450) 0 + 98: TypeVector 6(int) 3 + 99: 98(ivec3) SpecConstantOp 79 64 64 2 1(GLSL.std.450) 0 + 100: 62(ivec4) SpecConstantOp 79 64 64 1(GLSL.std.450) 2 0 3 4(main): 2 Function None 3 5: Label Return diff --git a/Test/spv.specConstantOperations.vert b/Test/spv.specConstantOperations.vert index 9311270f..91920cf2 100644 --- a/Test/spv.specConstantOperations.vert +++ b/Test/spv.specConstantOperations.vert @@ -11,8 +11,8 @@ layout(constant_id = 203) const int sp_sint = -10; // // Size convert -const double float_to_double = double(sp_float); -const float double_to_float = float(float_to_double); +//const double float_to_double = double(sp_float); +//const float double_to_float = float(float_to_double); // Negate and Not const int negate_int = -sp_int; @@ -55,8 +55,8 @@ const uvec4 uv = uvec4(sp_uint, sp_uint, -1, -2); const vec4 fv = vec4(sp_float, 1.25, sp_float, 1.25); // Size convert -const dvec4 fv_to_dv = dvec4(fv); -const vec4 dv_to_fv = vec4(fv_to_dv); +//const dvec4 fv_to_dv = dvec4(fv); +//const vec4 dv_to_fv = vec4(fv_to_dv); // Negate and Not const ivec4 not_iv = ~iv; diff --git a/glslang/MachineIndependent/Intermediate.cpp b/glslang/MachineIndependent/Intermediate.cpp index 777de2d6..d0fa74e5 100644 --- a/glslang/MachineIndependent/Intermediate.cpp +++ b/glslang/MachineIndependent/Intermediate.cpp @@ -145,10 +145,12 @@ TIntermTyped* TIntermediate::addBinaryMath(TOperator op, TIntermTyped* left, TIn // If either is a specialization constant, while the other is // a constant (or specialization constant), the result is still - // a specialization constant. + // a specialization constant, if the operation is an allowed + // specialization-constant operation. if (( left->getType().getQualifier().isSpecConstant() && right->getType().getQualifier().isConstant()) || (right->getType().getQualifier().isSpecConstant() && left->getType().getQualifier().isConstant())) - node->getWritableType().getQualifier().makeSpecConstant(); + if (isSpecializationOperation(*node)) + node->getWritableType().getQualifier().makeSpecConstant(); return node; } @@ -292,8 +294,9 @@ TIntermTyped* TIntermediate::addUnaryMath(TOperator op, TIntermTyped* child, TSo if (child->getAsConstantUnion()) return child->getAsConstantUnion()->fold(op, node->getType()); - // If it's a specialization constant, the result is too. - if (child->getType().getQualifier().isSpecConstant()) + // If it's a specialization constant, the result is too, + // if the operation is allowed for specialization constants. + if (child->getType().getQualifier().isSpecConstant() && isSpecializationOperation(*node)) node->getWritableType().getQualifier().makeSpecConstant(); return node; @@ -619,8 +622,8 @@ TIntermTyped* TIntermediate::addConversion(TOperator op, const TType& type, TInt // TODO: it seems that some unary folding operations should occur here, but are not - // Propagate specialization-constant-ness. - if (node->getType().getQualifier().isSpecConstant()) + // Propagate specialization-constant-ness, if allowed + if (node->getType().getQualifier().isSpecConstant() && isSpecializationOperation(*newNode)) newNode->getWritableType().getQualifier().makeSpecConstant(); return newNode; @@ -1065,6 +1068,87 @@ void TIntermediate::removeTree() RemoveAllTreeNodes(treeRoot); } +// +// Implement the part of KHR_vulkan_glsl that lists the set of operations +// that can result in a specialization constant operation. +// +// "5.x Specialization Constant Operations" +// +// ... +// +// It also needs to allow basic construction, swizzling, and indexing +// operations. +// +bool TIntermediate::isSpecializationOperation(const TIntermOperator& node) const +{ + // allow construction + if (node.isConstructor()) + return true; + + // The set for floating point is quite limited + if (node.getBasicType() == EbtFloat || + node.getBasicType() == EbtDouble) { + switch (node.getOp()) { + case EOpIndexDirect: + case EOpIndexIndirect: + case EOpIndexDirectStruct: + case EOpVectorSwizzle: + return true; + default: + return false; + } + } + + // Floating-point is out of the way. + // Now check for integer/bool-based operations + switch (node.getOp()) { + + // dereference/swizzle + case EOpIndexDirect: + case EOpIndexIndirect: + case EOpIndexDirectStruct: + case EOpVectorSwizzle: + + // conversion constructors + case EOpConvIntToBool: + case EOpConvUintToBool: + case EOpConvUintToInt: + case EOpConvBoolToInt: + case EOpConvIntToUint: + case EOpConvBoolToUint: + + // unary operations + case EOpNegative: + case EOpLogicalNot: + case EOpBitwiseNot: + + // binary operations + case EOpAdd: + case EOpSub: + case EOpMul: + case EOpVectorTimesScalar: + case EOpDiv: + case EOpMod: + case EOpRightShift: + case EOpLeftShift: + case EOpAnd: + case EOpInclusiveOr: + case EOpExclusiveOr: + case EOpLogicalOr: + case EOpLogicalXor: + case EOpLogicalAnd: + case EOpEqual: + case EOpNotEqual: + case EOpLessThan: + case EOpGreaterThan: + case EOpLessThanEqual: + case EOpGreaterThanEqual: + return true; + default: + return false; + } +} + //////////////////////////////////////////////////////////////// // // Member functions of the nodes used for building the tree. diff --git a/glslang/MachineIndependent/localintermediate.h b/glslang/MachineIndependent/localintermediate.h index 42be8b90..895bd3e2 100644 --- a/glslang/MachineIndependent/localintermediate.h +++ b/glslang/MachineIndependent/localintermediate.h @@ -342,6 +342,8 @@ protected: TIntermSequence& findLinkerObjects() const; bool userOutputUsed() const; static int getBaseAlignmentScalar(const TType&, int& size); + bool isSpecializationOperation(const TIntermOperator&) const; + const EShLanguage language; // stage, known at construction time EShSource source; // source language, known a bit later