John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2 | // Copyright (C) 2014-2016 LunarG, Inc. |
John Kessenich | b23d232 | 2018-12-14 10:47:35 -0700 | [diff] [blame] | 3 | // Copyright (C) 2015-2018 Google, Inc. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4 | // Copyright (C) 2017 ARM Limited. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 6 | // All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions |
| 10 | // are met: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 11 | // |
| 12 | // Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // |
| 15 | // Redistributions in binary form must reproduce the above |
| 16 | // copyright notice, this list of conditions and the following |
| 17 | // disclaimer in the documentation and/or other materials provided |
| 18 | // with the distribution. |
| 19 | // |
| 20 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 21 | // contributors may be used to endorse or promote products derived |
| 22 | // from this software without specific prior written permission. |
| 23 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 24 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 34 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 | // POSSIBILITY OF SUCH DAMAGE. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 36 | |
| 37 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 38 | // Visit the nodes in the glslang intermediate tree representation to |
| 39 | // translate them to SPIR-V. |
| 40 | // |
| 41 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 42 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 43 | #include "GlslangToSpv.h" |
| 44 | #include "SpvBuilder.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 45 | namespace spv { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 46 | #include "GLSL.std.450.h" |
| 47 | #include "GLSL.ext.KHR.h" |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 48 | #include "GLSL.ext.EXT.h" |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 49 | #include "GLSL.ext.AMD.h" |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 50 | #include "GLSL.ext.NV.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 51 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 52 | |
| 53 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 54 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 55 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 56 | #include "../glslang/Include/Common.h" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 57 | #include "../glslang/Include/revision.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 58 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 59 | #include <fstream> |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 60 | #include <iomanip> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 61 | #include <list> |
| 62 | #include <map> |
| 63 | #include <stack> |
| 64 | #include <string> |
| 65 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 66 | |
| 67 | namespace { |
| 68 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 69 | namespace { |
| 70 | class SpecConstantOpModeGuard { |
| 71 | public: |
| 72 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 73 | : builder_(builder) { |
| 74 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 75 | } |
| 76 | ~SpecConstantOpModeGuard() { |
| 77 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 78 | : builder_->setToNormalCodeGenMode(); |
| 79 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 80 | void turnOnSpecConstantOpMode() { |
| 81 | builder_->setToSpecConstCodeGenMode(); |
| 82 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | spv::Builder* builder_; |
| 86 | bool previous_flag_; |
| 87 | }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 88 | |
| 89 | struct OpDecorations { |
| 90 | spv::Decoration precision; |
| 91 | spv::Decoration noContraction; |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 92 | spv::Decoration nonUniform; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } // namespace |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 96 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 97 | // |
| 98 | // The main holder of information for translating glslang to SPIR-V. |
| 99 | // |
| 100 | // Derives from the AST walking base class. |
| 101 | // |
| 102 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 103 | public: |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 104 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 105 | glslang::SpvOptions& options); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 106 | virtual ~TGlslangToSpvTraverser() { } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 107 | |
| 108 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 109 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 110 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 111 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 112 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 113 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 114 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 115 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 116 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 117 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 118 | void finishSpv(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 119 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 120 | |
| 121 | protected: |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 122 | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
| 123 | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
| 124 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 125 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 126 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 127 | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 128 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 129 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 130 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 131 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 132 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 133 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 134 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 135 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 136 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 137 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 138 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 139 | spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 140 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 141 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 142 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 143 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 144 | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 145 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 146 | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 147 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 148 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 149 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 150 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 151 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 152 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 153 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 154 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 155 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 156 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 157 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 158 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 159 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 160 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 161 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 162 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 163 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 164 | bool writableParam(glslang::TStorageQualifier) const; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 165 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 166 | void makeFunctions(const glslang::TIntermSequence&); |
| 167 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 168 | void visitFunctions(const glslang::TIntermSequence&); |
| 169 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 170 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 171 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 172 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 173 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 174 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 175 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 176 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 177 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 178 | spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 179 | glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 180 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 181 | glslang::TBasicType typeProxy); |
| 182 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 183 | glslang::TBasicType typeProxy); |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 184 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 185 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 186 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 187 | spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 188 | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 189 | spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 190 | spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 191 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 192 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 193 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 194 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 195 | spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, int& nextConst, bool specConstant); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 196 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 197 | bool isTrivial(const glslang::TIntermTyped* node); |
| 198 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 199 | spv::Id getExtBuiltins(const char* name); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 200 | void addPre13Extension(const char* ext) |
| 201 | { |
| 202 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 203 | builder.addExtension(ext); |
| 204 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 205 | std::pair<spv::Id, spv::Id> getForcedType(spv::BuiltIn, const glslang::TType&); |
| 206 | spv::Id translateForcedType(spv::Id object); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 207 | spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 208 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 209 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 210 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 211 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 212 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 213 | int sequenceDepth; |
| 214 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 215 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 216 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 217 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 218 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 219 | bool inEntryPoint; |
| 220 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 221 | bool linkageOnly; // true when visiting the set of objects in the AST present only for establishing interface, whether or not they were statically used |
John Kessenich | 59420fd | 2015-12-21 11:45:34 -0700 | [diff] [blame] | 222 | std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 223 | const glslang::TIntermediate* glslangIntermediate; |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 224 | bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 225 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 226 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 227 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 228 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 229 | std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, rather than a pointer |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 230 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 231 | std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount]; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 232 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 233 | std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 234 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 235 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 236 | // Map pointee types for EbtReference to their forward pointers |
| 237 | std::map<const glslang::TType *, spv::Id> forwardPointers; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 238 | // Type forcing, for when SPIR-V wants a different type than the AST, |
| 239 | // requiring local translation to and from SPIR-V type on every access. |
| 240 | // Maps <builtin-variable-id -> AST-required-type-id> |
| 241 | std::unordered_map<spv::Id, spv::Id> forceType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | // |
| 245 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 246 | // |
| 247 | |
| 248 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 249 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 250 | { |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 251 | #ifdef GLSLANG_WEB |
| 252 | return spv::SourceLanguageESSL; |
| 253 | #endif |
| 254 | |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 255 | switch (source) { |
| 256 | case glslang::EShSourceGlsl: |
| 257 | switch (profile) { |
| 258 | case ENoProfile: |
| 259 | case ECoreProfile: |
| 260 | case ECompatibilityProfile: |
| 261 | return spv::SourceLanguageGLSL; |
| 262 | case EEsProfile: |
| 263 | return spv::SourceLanguageESSL; |
| 264 | default: |
| 265 | return spv::SourceLanguageUnknown; |
| 266 | } |
| 267 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 268 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 269 | default: |
| 270 | return spv::SourceLanguageUnknown; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Translate glslang language (stage) to SPIR-V execution model. |
| 275 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 276 | { |
| 277 | switch (stage) { |
| 278 | case EShLangVertex: return spv::ExecutionModelVertex; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 279 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 280 | #ifndef GLSLANG_WEB |
| 281 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 282 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 283 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 284 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 285 | case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV; |
| 286 | case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV; |
| 287 | case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV; |
| 288 | case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV; |
| 289 | case EShLangMissNV: return spv::ExecutionModelMissNV; |
| 290 | case EShLangCallableNV: return spv::ExecutionModelCallableNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 291 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 292 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 293 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 294 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 295 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 296 | return spv::ExecutionModelFragment; |
| 297 | } |
| 298 | } |
| 299 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 300 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 301 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 302 | { |
| 303 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 304 | case glslang::Esd1D: return spv::Dim1D; |
| 305 | case glslang::Esd2D: return spv::Dim2D; |
| 306 | case glslang::Esd3D: return spv::Dim3D; |
| 307 | case glslang::EsdCube: return spv::DimCube; |
| 308 | case glslang::EsdRect: return spv::DimRect; |
| 309 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 310 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 311 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 312 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 313 | return spv::Dim2D; |
| 314 | } |
| 315 | } |
| 316 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 317 | // Translate glslang precision to SPIR-V precision decorations. |
| 318 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 319 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 320 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 321 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 322 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 323 | default: |
| 324 | return spv::NoPrecision; |
| 325 | } |
| 326 | } |
| 327 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 328 | // Translate glslang type to SPIR-V precision decorations. |
| 329 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 330 | { |
| 331 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 332 | } |
| 333 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 334 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 335 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 336 | { |
| 337 | if (type.getBasicType() == glslang::EbtBlock) { |
| 338 | switch (type.getQualifier().storage) { |
| 339 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 340 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 341 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 342 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 343 | #ifndef GLSLANG_WEB |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 344 | case glslang::EvqPayloadNV: return spv::DecorationBlock; |
| 345 | case glslang::EvqPayloadInNV: return spv::DecorationBlock; |
| 346 | case glslang::EvqHitAttrNV: return spv::DecorationBlock; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 347 | case glslang::EvqCallableDataNV: return spv::DecorationBlock; |
| 348 | case glslang::EvqCallableDataInNV: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 349 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 350 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 351 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 356 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 357 | } |
| 358 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 359 | // Translate glslang type to SPIR-V memory decorations. |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 360 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, bool useVulkanMemoryModel) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 361 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 362 | if (!useVulkanMemoryModel) { |
| 363 | if (qualifier.coherent) |
| 364 | memory.push_back(spv::DecorationCoherent); |
| 365 | if (qualifier.volatil) { |
| 366 | memory.push_back(spv::DecorationVolatile); |
| 367 | memory.push_back(spv::DecorationCoherent); |
| 368 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 369 | } |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 370 | if (qualifier.restrict) |
| 371 | memory.push_back(spv::DecorationRestrict); |
| 372 | if (qualifier.readonly) |
| 373 | memory.push_back(spv::DecorationNonWritable); |
| 374 | if (qualifier.writeonly) |
| 375 | memory.push_back(spv::DecorationNonReadable); |
| 376 | } |
| 377 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 378 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 379 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 380 | { |
| 381 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 382 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 383 | case glslang::ElmRowMajor: |
| 384 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 385 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 386 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 387 | default: |
| 388 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 389 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 390 | } |
| 391 | } else { |
| 392 | switch (type.getBasicType()) { |
| 393 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 394 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 395 | break; |
| 396 | case glslang::EbtBlock: |
| 397 | switch (type.getQualifier().storage) { |
| 398 | case glslang::EvqUniform: |
| 399 | case glslang::EvqBuffer: |
| 400 | switch (type.getQualifier().layoutPacking) { |
| 401 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 402 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 403 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 404 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 405 | } |
| 406 | case glslang::EvqVaryingIn: |
| 407 | case glslang::EvqVaryingOut: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 408 | if (type.getQualifier().isTaskMemory()) { |
| 409 | switch (type.getQualifier().layoutPacking) { |
| 410 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 411 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 412 | default: break; |
| 413 | } |
| 414 | } else { |
| 415 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 416 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 417 | return spv::DecorationMax; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 418 | #ifndef GLSLANG_WEB |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 419 | case glslang::EvqPayloadNV: |
| 420 | case glslang::EvqPayloadInNV: |
| 421 | case glslang::EvqHitAttrNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 422 | case glslang::EvqCallableDataNV: |
| 423 | case glslang::EvqCallableDataInNV: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 424 | return spv::DecorationMax; |
| 425 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 426 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 427 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 428 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 435 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 436 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 437 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 438 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 439 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 440 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 441 | return spv::DecorationMax; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 442 | else if (qualifier.isNonPerspective()) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 443 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 444 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 445 | return spv::DecorationFlat; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 446 | else if (qualifier.isExplicitInterpolation()) { |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 447 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 448 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 449 | } |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 450 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 451 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 455 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 456 | // should be applied. |
| 457 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 458 | { |
| 459 | if (qualifier.patch) |
| 460 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 461 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 462 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 463 | else if (qualifier.sample) { |
| 464 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 465 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 466 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 467 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 468 | } |
| 469 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 470 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 471 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 472 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 473 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 474 | return spv::DecorationInvariant; |
| 475 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 476 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 477 | } |
| 478 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 479 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 480 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 481 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 482 | if (qualifier.isNoContraction()) |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 483 | return spv::DecorationNoContraction; |
| 484 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 485 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 486 | } |
| 487 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 488 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 489 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 490 | { |
| 491 | if (qualifier.isNonUniform()) { |
| 492 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 493 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 494 | return spv::DecorationNonUniformEXT; |
| 495 | } else |
| 496 | return spv::DecorationMax; |
| 497 | } |
| 498 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 499 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 500 | { |
| 501 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) { |
| 502 | return spv::MemoryAccessMaskNone; |
| 503 | } |
| 504 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
| 505 | if (coherentFlags.volatil || |
| 506 | coherentFlags.coherent || |
| 507 | coherentFlags.devicecoherent || |
| 508 | coherentFlags.queuefamilycoherent || |
| 509 | coherentFlags.workgroupcoherent || |
| 510 | coherentFlags.subgroupcoherent) { |
| 511 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 512 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 513 | } |
| 514 | if (coherentFlags.nonprivate) { |
| 515 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 516 | } |
| 517 | if (coherentFlags.volatil) { |
| 518 | mask = mask | spv::MemoryAccessVolatileMask; |
| 519 | } |
| 520 | if (mask != spv::MemoryAccessMaskNone) { |
| 521 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 522 | } |
| 523 | return mask; |
| 524 | } |
| 525 | |
| 526 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 527 | { |
| 528 | if (!glslangIntermediate->usingVulkanMemoryModel()) { |
| 529 | return spv::ImageOperandsMaskNone; |
| 530 | } |
| 531 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 532 | if (coherentFlags.volatil || |
| 533 | coherentFlags.coherent || |
| 534 | coherentFlags.devicecoherent || |
| 535 | coherentFlags.queuefamilycoherent || |
| 536 | coherentFlags.workgroupcoherent || |
| 537 | coherentFlags.subgroupcoherent) { |
| 538 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 539 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 540 | } |
| 541 | if (coherentFlags.nonprivate) { |
| 542 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 543 | } |
| 544 | if (coherentFlags.volatil) { |
| 545 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 546 | } |
| 547 | if (mask != spv::ImageOperandsMaskNone) { |
| 548 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 549 | } |
| 550 | return mask; |
| 551 | } |
| 552 | |
| 553 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 554 | { |
| 555 | spv::Builder::AccessChain::CoherentFlags flags; |
| 556 | flags.coherent = type.getQualifier().coherent; |
| 557 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 558 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 559 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 560 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 561 | type.getQualifier().storage == glslang::EvqShared; |
| 562 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 563 | flags.volatil = type.getQualifier().volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 564 | // *coherent variables are implicitly nonprivate in GLSL |
| 565 | flags.nonprivate = type.getQualifier().nonprivate || |
Jeff Bolz | ab3c965 | 2018-10-15 22:46:48 -0500 | [diff] [blame] | 566 | flags.subgroupcoherent || |
| 567 | flags.workgroupcoherent || |
| 568 | flags.queuefamilycoherent || |
| 569 | flags.devicecoherent || |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 570 | flags.coherent || |
| 571 | flags.volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 572 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
| 573 | return flags; |
| 574 | } |
| 575 | |
| 576 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 577 | { |
| 578 | spv::Scope scope; |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 579 | if (coherentFlags.volatil || coherentFlags.coherent) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 580 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 581 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 582 | } else if (coherentFlags.devicecoherent) { |
| 583 | scope = spv::ScopeDevice; |
| 584 | } else if (coherentFlags.queuefamilycoherent) { |
| 585 | scope = spv::ScopeQueueFamilyKHR; |
| 586 | } else if (coherentFlags.workgroupcoherent) { |
| 587 | scope = spv::ScopeWorkgroup; |
| 588 | } else if (coherentFlags.subgroupcoherent) { |
| 589 | scope = spv::ScopeSubgroup; |
| 590 | } else { |
| 591 | scope = spv::ScopeMax; |
| 592 | } |
| 593 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 594 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 595 | } |
| 596 | return scope; |
| 597 | } |
| 598 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 599 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 600 | // associated capabilities when required. For some built-in variables, a capability |
| 601 | // is generated only when using the variable in an executable instruction, but not when |
| 602 | // just declaring a struct member variable with it. This is true for PointSize, |
| 603 | // ClipDistance, and CullDistance. |
| 604 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 605 | { |
| 606 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 607 | case glslang::EbvPointSize: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 608 | #ifndef GLSLANG_WEB |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 609 | // Defer adding the capability until the built-in is actually used. |
| 610 | if (! memberDeclaration) { |
| 611 | switch (glslangIntermediate->getStage()) { |
| 612 | case EShLangGeometry: |
| 613 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 614 | break; |
| 615 | case EShLangTessControl: |
| 616 | case EShLangTessEvaluation: |
| 617 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 618 | break; |
| 619 | default: |
| 620 | break; |
| 621 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 622 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 623 | #endif |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 624 | return spv::BuiltInPointSize; |
| 625 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 626 | case glslang::EbvPosition: return spv::BuiltInPosition; |
| 627 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 628 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
| 629 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 630 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
| 631 | |
| 632 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 633 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 634 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
| 635 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 636 | |
| 637 | #ifndef GLSLANG_WEB |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 638 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 639 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 640 | // They are now generated when used, rather than here when declared. |
| 641 | // Potentially, the specification should be more clear what the minimum |
| 642 | // use needed is to trigger the capability. |
| 643 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 644 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 645 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 646 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 647 | return spv::BuiltInClipDistance; |
| 648 | |
| 649 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 650 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 651 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 652 | return spv::BuiltInCullDistance; |
| 653 | |
| 654 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 655 | builder.addCapability(spv::CapabilityMultiViewport); |
| 656 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 657 | glslangIntermediate->getStage() == EShLangTessControl || |
| 658 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 659 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 660 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 661 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 662 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 663 | return spv::BuiltInViewportIndex; |
| 664 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 665 | case glslang::EbvSampleId: |
| 666 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 667 | return spv::BuiltInSampleId; |
| 668 | |
| 669 | case glslang::EbvSamplePosition: |
| 670 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 671 | return spv::BuiltInSamplePosition; |
| 672 | |
| 673 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 674 | return spv::BuiltInSampleMask; |
| 675 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 676 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 677 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 678 | return spv::BuiltInLayer; |
| 679 | } |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 680 | builder.addCapability(spv::CapabilityGeometry); |
| 681 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 682 | glslangIntermediate->getStage() == EShLangTessControl || |
| 683 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 684 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 685 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 686 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 687 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 688 | return spv::BuiltInLayer; |
| 689 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 690 | case glslang::EbvBaseVertex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 691 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 692 | builder.addCapability(spv::CapabilityDrawParameters); |
| 693 | return spv::BuiltInBaseVertex; |
| 694 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 695 | case glslang::EbvBaseInstance: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 696 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 697 | builder.addCapability(spv::CapabilityDrawParameters); |
| 698 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 699 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 700 | case glslang::EbvDrawId: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 701 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 702 | builder.addCapability(spv::CapabilityDrawParameters); |
| 703 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 704 | |
| 705 | case glslang::EbvPrimitiveId: |
| 706 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 707 | builder.addCapability(spv::CapabilityGeometry); |
| 708 | return spv::BuiltInPrimitiveId; |
| 709 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 710 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 711 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 712 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 713 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 714 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 715 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 716 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 717 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 718 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 719 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 720 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 721 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 722 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 723 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 724 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 725 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 726 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 727 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 728 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 729 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 730 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 731 | return spv::BuiltInSubgroupSize; |
| 732 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 733 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 734 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 735 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 736 | return spv::BuiltInSubgroupLocalInvocationId; |
| 737 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 738 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 739 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 740 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 741 | return spv::BuiltInSubgroupEqMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 742 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 743 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 744 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 745 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 746 | return spv::BuiltInSubgroupGeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 747 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 748 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 749 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 750 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 751 | return spv::BuiltInSubgroupGtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 752 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 753 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 754 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 755 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 756 | return spv::BuiltInSubgroupLeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 757 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 758 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 759 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 760 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 761 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 762 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 763 | case glslang::EbvNumSubgroups: |
| 764 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 765 | return spv::BuiltInNumSubgroups; |
| 766 | |
| 767 | case glslang::EbvSubgroupID: |
| 768 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 769 | return spv::BuiltInSubgroupId; |
| 770 | |
| 771 | case glslang::EbvSubgroupSize2: |
| 772 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 773 | return spv::BuiltInSubgroupSize; |
| 774 | |
| 775 | case glslang::EbvSubgroupInvocation2: |
| 776 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 777 | return spv::BuiltInSubgroupLocalInvocationId; |
| 778 | |
| 779 | case glslang::EbvSubgroupEqMask2: |
| 780 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 781 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 782 | return spv::BuiltInSubgroupEqMask; |
| 783 | |
| 784 | case glslang::EbvSubgroupGeMask2: |
| 785 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 786 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 787 | return spv::BuiltInSubgroupGeMask; |
| 788 | |
| 789 | case glslang::EbvSubgroupGtMask2: |
| 790 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 791 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 792 | return spv::BuiltInSubgroupGtMask; |
| 793 | |
| 794 | case glslang::EbvSubgroupLeMask2: |
| 795 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 796 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 797 | return spv::BuiltInSubgroupLeMask; |
| 798 | |
| 799 | case glslang::EbvSubgroupLtMask2: |
| 800 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 801 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 802 | return spv::BuiltInSubgroupLtMask; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 803 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 804 | case glslang::EbvBaryCoordNoPersp: |
| 805 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 806 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 807 | |
| 808 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 809 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 810 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 811 | |
| 812 | case glslang::EbvBaryCoordNoPerspSample: |
| 813 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 814 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 815 | |
| 816 | case glslang::EbvBaryCoordSmooth: |
| 817 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 818 | return spv::BuiltInBaryCoordSmoothAMD; |
| 819 | |
| 820 | case glslang::EbvBaryCoordSmoothCentroid: |
| 821 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 822 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 823 | |
| 824 | case glslang::EbvBaryCoordSmoothSample: |
| 825 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 826 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 827 | |
| 828 | case glslang::EbvBaryCoordPullModel: |
| 829 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 830 | return spv::BuiltInBaryCoordPullModelAMD; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 831 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 832 | case glslang::EbvDeviceIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 833 | addPre13Extension(spv::E_SPV_KHR_device_group); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 834 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 835 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 836 | |
| 837 | case glslang::EbvViewIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 838 | addPre13Extension(spv::E_SPV_KHR_multiview); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 839 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 840 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 841 | |
Daniel Koch | 5154db5 | 2018-11-26 10:01:58 -0500 | [diff] [blame] | 842 | case glslang::EbvFragSizeEXT: |
| 843 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 844 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 845 | return spv::BuiltInFragSizeEXT; |
| 846 | |
| 847 | case glslang::EbvFragInvocationCountEXT: |
| 848 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 849 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 850 | return spv::BuiltInFragInvocationCountEXT; |
| 851 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 852 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 853 | if (!memberDeclaration) { |
| 854 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 855 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 856 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 857 | return spv::BuiltInViewportMaskNV; |
| 858 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 859 | if (!memberDeclaration) { |
| 860 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 861 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 862 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 863 | return spv::BuiltInSecondaryPositionNV; |
| 864 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 865 | if (!memberDeclaration) { |
| 866 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 867 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 868 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 869 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 870 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 871 | if (!memberDeclaration) { |
| 872 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 873 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 874 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 875 | return spv::BuiltInPositionPerViewNV; |
| 876 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 877 | if (!memberDeclaration) { |
| 878 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 879 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 880 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 881 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 882 | case glslang::EbvFragFullyCoveredNV: |
| 883 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 884 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 885 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 886 | case glslang::EbvFragmentSizeNV: |
| 887 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 888 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 889 | return spv::BuiltInFragmentSizeNV; |
| 890 | case glslang::EbvInvocationsPerPixelNV: |
| 891 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 892 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 893 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 894 | |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 895 | // ray tracing |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 896 | case glslang::EbvLaunchIdNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 897 | return spv::BuiltInLaunchIdNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 898 | case glslang::EbvLaunchSizeNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 899 | return spv::BuiltInLaunchSizeNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 900 | case glslang::EbvWorldRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 901 | return spv::BuiltInWorldRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 902 | case glslang::EbvWorldRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 903 | return spv::BuiltInWorldRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 904 | case glslang::EbvObjectRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 905 | return spv::BuiltInObjectRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 906 | case glslang::EbvObjectRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 907 | return spv::BuiltInObjectRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 908 | case glslang::EbvRayTminNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 909 | return spv::BuiltInRayTminNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 910 | case glslang::EbvRayTmaxNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 911 | return spv::BuiltInRayTmaxNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 912 | case glslang::EbvInstanceCustomIndexNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 913 | return spv::BuiltInInstanceCustomIndexNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 914 | case glslang::EbvHitTNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 915 | return spv::BuiltInHitTNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 916 | case glslang::EbvHitKindNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 917 | return spv::BuiltInHitKindNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 918 | case glslang::EbvObjectToWorldNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 919 | return spv::BuiltInObjectToWorldNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 920 | case glslang::EbvWorldToObjectNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 921 | return spv::BuiltInWorldToObjectNV; |
| 922 | case glslang::EbvIncomingRayFlagsNV: |
| 923 | return spv::BuiltInIncomingRayFlagsNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 924 | |
| 925 | // barycentrics |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 926 | case glslang::EbvBaryCoordNV: |
| 927 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 928 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 929 | return spv::BuiltInBaryCoordNV; |
| 930 | case glslang::EbvBaryCoordNoPerspNV: |
| 931 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 932 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 933 | return spv::BuiltInBaryCoordNoPerspNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 934 | |
| 935 | // mesh shaders |
| 936 | case glslang::EbvTaskCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 937 | return spv::BuiltInTaskCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 938 | case glslang::EbvPrimitiveCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 939 | return spv::BuiltInPrimitiveCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 940 | case glslang::EbvPrimitiveIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 941 | return spv::BuiltInPrimitiveIndicesNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 942 | case glslang::EbvClipDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 943 | return spv::BuiltInClipDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 944 | case glslang::EbvCullDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 945 | return spv::BuiltInCullDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 946 | case glslang::EbvLayerPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 947 | return spv::BuiltInLayerPerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 948 | case glslang::EbvMeshViewCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 949 | return spv::BuiltInMeshViewCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 950 | case glslang::EbvMeshViewIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 951 | return spv::BuiltInMeshViewIndicesNV; |
Daniel Koch | 2cb2f19 | 2019-06-04 08:43:32 -0400 | [diff] [blame] | 952 | |
| 953 | // sm builtins |
| 954 | case glslang::EbvWarpsPerSM: |
| 955 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 956 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 957 | return spv::BuiltInWarpsPerSMNV; |
| 958 | case glslang::EbvSMCount: |
| 959 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 960 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 961 | return spv::BuiltInSMCountNV; |
| 962 | case glslang::EbvWarpID: |
| 963 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 964 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 965 | return spv::BuiltInWarpIDNV; |
| 966 | case glslang::EbvSMID: |
| 967 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 968 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 969 | return spv::BuiltInSMIDNV; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 970 | #endif |
| 971 | |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 972 | default: |
| 973 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 977 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 978 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 979 | { |
| 980 | assert(type.getBasicType() == glslang::EbtSampler); |
| 981 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 982 | // Check for capabilities |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 983 | switch (type.getQualifier().getFormat()) { |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 984 | case glslang::ElfRg32f: |
| 985 | case glslang::ElfRg16f: |
| 986 | case glslang::ElfR11fG11fB10f: |
| 987 | case glslang::ElfR16f: |
| 988 | case glslang::ElfRgba16: |
| 989 | case glslang::ElfRgb10A2: |
| 990 | case glslang::ElfRg16: |
| 991 | case glslang::ElfRg8: |
| 992 | case glslang::ElfR16: |
| 993 | case glslang::ElfR8: |
| 994 | case glslang::ElfRgba16Snorm: |
| 995 | case glslang::ElfRg16Snorm: |
| 996 | case glslang::ElfRg8Snorm: |
| 997 | case glslang::ElfR16Snorm: |
| 998 | case glslang::ElfR8Snorm: |
| 999 | |
| 1000 | case glslang::ElfRg32i: |
| 1001 | case glslang::ElfRg16i: |
| 1002 | case glslang::ElfRg8i: |
| 1003 | case glslang::ElfR16i: |
| 1004 | case glslang::ElfR8i: |
| 1005 | |
| 1006 | case glslang::ElfRgb10a2ui: |
| 1007 | case glslang::ElfRg32ui: |
| 1008 | case glslang::ElfRg16ui: |
| 1009 | case glslang::ElfRg8ui: |
| 1010 | case glslang::ElfR16ui: |
| 1011 | case glslang::ElfR8ui: |
| 1012 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 1013 | break; |
| 1014 | |
| 1015 | default: |
| 1016 | break; |
| 1017 | } |
| 1018 | |
| 1019 | // do the translation |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1020 | switch (type.getQualifier().getFormat()) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1021 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1022 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1023 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1024 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1025 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1026 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1027 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1028 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1029 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1030 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1031 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1032 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1033 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1034 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1035 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1036 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1037 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1038 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1039 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1040 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1041 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1042 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1043 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1044 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1045 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1046 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1047 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1048 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1049 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1050 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1051 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1052 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1053 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1054 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1055 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1056 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1057 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1058 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1059 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1060 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1061 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1065 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1066 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1067 | if (selectionNode.getFlatten()) |
| 1068 | return spv::SelectionControlFlattenMask; |
| 1069 | if (selectionNode.getDontFlatten()) |
| 1070 | return spv::SelectionControlDontFlattenMask; |
| 1071 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1072 | } |
| 1073 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1074 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1075 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1076 | if (switchNode.getFlatten()) |
| 1077 | return spv::SelectionControlFlattenMask; |
| 1078 | if (switchNode.getDontFlatten()) |
| 1079 | return spv::SelectionControlDontFlattenMask; |
| 1080 | return spv::SelectionControlMaskNone; |
| 1081 | } |
| 1082 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1083 | // return a non-0 dependency if the dependency argument must be set |
| 1084 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1085 | std::vector<unsigned int>& operands) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1086 | { |
| 1087 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1088 | |
| 1089 | if (loopNode.getDontUnroll()) |
| 1090 | control = control | spv::LoopControlDontUnrollMask; |
| 1091 | if (loopNode.getUnroll()) |
| 1092 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1093 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1094 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1095 | else if (loopNode.getLoopDependency() > 0) { |
| 1096 | control = control | spv::LoopControlDependencyLengthMask; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1097 | operands.push_back((unsigned int)loopNode.getLoopDependency()); |
| 1098 | } |
| 1099 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 1100 | if (loopNode.getMinIterations() > 0) { |
| 1101 | control = control | spv::LoopControlMinIterationsMask; |
| 1102 | operands.push_back(loopNode.getMinIterations()); |
| 1103 | } |
| 1104 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
| 1105 | control = control | spv::LoopControlMaxIterationsMask; |
| 1106 | operands.push_back(loopNode.getMaxIterations()); |
| 1107 | } |
| 1108 | if (loopNode.getIterationMultiple() > 1) { |
| 1109 | control = control | spv::LoopControlIterationMultipleMask; |
| 1110 | operands.push_back(loopNode.getIterationMultiple()); |
| 1111 | } |
| 1112 | if (loopNode.getPeelCount() > 0) { |
| 1113 | control = control | spv::LoopControlPeelCountMask; |
| 1114 | operands.push_back(loopNode.getPeelCount()); |
| 1115 | } |
| 1116 | if (loopNode.getPartialCount() > 0) { |
| 1117 | control = control | spv::LoopControlPartialCountMask; |
| 1118 | operands.push_back(loopNode.getPartialCount()); |
| 1119 | } |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1120 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1121 | |
| 1122 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1123 | } |
| 1124 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1125 | // Translate glslang type to SPIR-V storage class. |
| 1126 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1127 | { |
| 1128 | if (type.getQualifier().isPipeInput()) |
| 1129 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1130 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1131 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1132 | |
| 1133 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1134 | type.getQualifier().storage == glslang::EvqUniform) { |
| 1135 | #ifndef GLSLANG_WEB |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1136 | if (type.getBasicType() == glslang::EbtAtomicUint) |
| 1137 | return spv::StorageClassAtomicCounter; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1138 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1139 | if (type.containsOpaque()) |
| 1140 | return spv::StorageClassUniformConstant; |
| 1141 | } |
| 1142 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1143 | #ifndef GLSLANG_WEB |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1144 | if (type.getQualifier().isUniformOrBuffer() && |
| 1145 | type.getQualifier().layoutShaderRecordNV) { |
| 1146 | return spv::StorageClassShaderRecordBufferNV; |
| 1147 | } |
| 1148 | #endif |
| 1149 | |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1150 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1151 | addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class); |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1152 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1156 | #ifndef GLSLANG_WEB |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1157 | if (type.getQualifier().isPushConstant()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1158 | return spv::StorageClassPushConstant; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1159 | #endif |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1160 | if (type.getBasicType() == glslang::EbtBlock) |
| 1161 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1162 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1163 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1164 | |
| 1165 | switch (type.getQualifier().storage) { |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1166 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1167 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1168 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1169 | #ifndef GLSLANG_WEB |
| 1170 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1171 | case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV; |
| 1172 | case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV; |
| 1173 | case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV; |
| 1174 | case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV; |
| 1175 | case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1176 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1177 | default: |
| 1178 | assert(0); |
| 1179 | break; |
| 1180 | } |
| 1181 | |
| 1182 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1183 | } |
| 1184 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1185 | // Add capabilities pertaining to how an array is indexed. |
| 1186 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1187 | const glslang::TType& indexType) |
| 1188 | { |
| 1189 | if (indexType.getQualifier().isNonUniform()) { |
| 1190 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1191 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1192 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1193 | if (baseType.getQualifier().hasAttachment()) |
| 1194 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1195 | else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1196 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1197 | else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1198 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1199 | else if (baseType.isImage()) |
| 1200 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1201 | else if (baseType.isTexture()) |
| 1202 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1203 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1204 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1205 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1206 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1207 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1208 | } |
| 1209 | } else { |
| 1210 | // assume a dynamically uniform index |
| 1211 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1212 | if (baseType.getQualifier().hasAttachment()) { |
| 1213 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1214 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1215 | } else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1216 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1217 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1218 | } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1219 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1220 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1221 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1226 | // Return whether or not the given type is something that should be tied to a |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1227 | // descriptor set. |
| 1228 | bool IsDescriptorResource(const glslang::TType& type) |
| 1229 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1230 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1231 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1232 | return type.getQualifier().isUniformOrBuffer() && |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1233 | ! type.getQualifier().isShaderRecordNV() && |
| 1234 | ! type.getQualifier().isPushConstant(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1235 | |
| 1236 | // non block... |
| 1237 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1238 | // if they are the global-scope-class, not the function parameter |
| 1239 | // (or local, if they ever exist) class. |
| 1240 | if (type.getBasicType() == glslang::EbtSampler) |
| 1241 | return type.getQualifier().isUniformOrBuffer(); |
| 1242 | |
| 1243 | // None of the above. |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1247 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1248 | { |
| 1249 | if (child.layoutMatrix == glslang::ElmNone) |
| 1250 | child.layoutMatrix = parent.layoutMatrix; |
| 1251 | |
| 1252 | if (parent.invariant) |
| 1253 | child.invariant = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1254 | if (parent.flat) |
| 1255 | child.flat = true; |
| 1256 | if (parent.centroid) |
| 1257 | child.centroid = true; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1258 | #ifndef GLSLANG_WEB |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1259 | if (parent.nopersp) |
| 1260 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1261 | if (parent.explicitInterp) |
| 1262 | child.explicitInterp = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1263 | if (parent.perPrimitiveNV) |
| 1264 | child.perPrimitiveNV = true; |
| 1265 | if (parent.perViewNV) |
| 1266 | child.perViewNV = true; |
| 1267 | if (parent.perTaskNV) |
| 1268 | child.perTaskNV = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1269 | if (parent.patch) |
| 1270 | child.patch = true; |
| 1271 | if (parent.sample) |
| 1272 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1273 | if (parent.coherent) |
| 1274 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1275 | if (parent.devicecoherent) |
| 1276 | child.devicecoherent = true; |
| 1277 | if (parent.queuefamilycoherent) |
| 1278 | child.queuefamilycoherent = true; |
| 1279 | if (parent.workgroupcoherent) |
| 1280 | child.workgroupcoherent = true; |
| 1281 | if (parent.subgroupcoherent) |
| 1282 | child.subgroupcoherent = true; |
| 1283 | if (parent.nonprivate) |
| 1284 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1285 | if (parent.volatil) |
| 1286 | child.volatil = true; |
| 1287 | if (parent.restrict) |
| 1288 | child.restrict = true; |
| 1289 | if (parent.readonly) |
| 1290 | child.readonly = true; |
| 1291 | if (parent.writeonly) |
| 1292 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1293 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1296 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1297 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1298 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1299 | // - struct members might inherit from a struct declaration |
| 1300 | // (note that non-block structs don't explicitly inherit, |
| 1301 | // only implicitly, meaning no decoration involved) |
| 1302 | // - affect decorations on the struct members |
| 1303 | // (note smooth does not, and expecting something like volatile |
| 1304 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1305 | // - are not part of the offset/st430/etc or row/column-major layout |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1306 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1309 | // |
| 1310 | // Implement the TGlslangToSpvTraverser class. |
| 1311 | // |
| 1312 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1313 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1314 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 1315 | : TIntermTraverser(true, false, true), |
| 1316 | options(options), |
| 1317 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1318 | sequenceDepth(0), logger(buildLogger), |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1319 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1320 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 1321 | glslangIntermediate(glslangIntermediate), |
| 1322 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1323 | { |
| 1324 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1325 | |
| 1326 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1327 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1328 | glslangIntermediate->getVersion()); |
| 1329 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1330 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1331 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1332 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1333 | |
| 1334 | // Set the source shader's text. If for SPV version 1.0, include |
| 1335 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1336 | // Otherwise, emit those as actual instructions. |
| 1337 | std::string text; |
| 1338 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1339 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1340 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1341 | text.append("// OpModuleProcessed "); |
| 1342 | text.append(processes[p]); |
| 1343 | text.append("\n"); |
| 1344 | } else |
| 1345 | builder.addModuleProcessed(processes[p]); |
| 1346 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1347 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1348 | text.append("#line 1\n"); |
| 1349 | text.append(glslangIntermediate->getSourceText()); |
| 1350 | builder.setSourceText(text); |
Greg Fischer | d445bb2 | 2018-12-06 11:13:15 -0700 | [diff] [blame] | 1351 | // Pass name and text for all included files |
| 1352 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1353 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1354 | builder.addInclude(iItr->first, iItr->second); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1355 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1356 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1357 | |
| 1358 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1359 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1360 | |
| 1361 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1362 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
| 1363 | builder.addExtension(spv::E_SPV_EXT_physical_storage_buffer); |
| 1364 | builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
| 1365 | }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1366 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1367 | memoryModel = spv::MemoryModelVulkanKHR; |
| 1368 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1369 | builder.addExtension(spv::E_SPV_KHR_vulkan_memory_model); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1370 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1371 | builder.setMemoryModel(addressingModel, memoryModel); |
| 1372 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 1373 | if (glslangIntermediate->usingVariablePointers()) { |
| 1374 | builder.addCapability(spv::CapabilityVariablePointers); |
| 1375 | } |
| 1376 | |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1377 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1378 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1379 | |
| 1380 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1381 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1382 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1383 | builder.addSourceExtension(it->c_str()); |
| 1384 | |
| 1385 | // Add the top-level modes for this shader. |
| 1386 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1387 | if (glslangIntermediate->getXfbMode()) { |
| 1388 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1389 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1390 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1391 | |
| 1392 | unsigned int mode; |
| 1393 | switch (glslangIntermediate->getStage()) { |
| 1394 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1395 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1396 | break; |
| 1397 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1398 | case EShLangFragment: |
| 1399 | builder.addCapability(spv::CapabilityShader); |
| 1400 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1401 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
| 1402 | |
| 1403 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1404 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
| 1405 | else |
| 1406 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
| 1407 | |
| 1408 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1409 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1410 | |
| 1411 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1412 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1413 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1414 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1415 | } |
| 1416 | |
| 1417 | switch(glslangIntermediate->getDepth()) { |
| 1418 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1419 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
| 1420 | default: mode = spv::ExecutionModeMax; break; |
| 1421 | } |
| 1422 | if (mode != spv::ExecutionModeMax) |
| 1423 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1424 | |
| 1425 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1426 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
| 1427 | |
| 1428 | switch (glslangIntermediate->getInterlockOrdering()) { |
| 1429 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; break; |
| 1430 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; break; |
| 1431 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; break; |
| 1432 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; break; |
| 1433 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; break; |
| 1434 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; break; |
| 1435 | default: mode = spv::ExecutionModeMax; break; |
| 1436 | } |
| 1437 | if (mode != spv::ExecutionModeMax) { |
| 1438 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1439 | if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
| 1440 | mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
| 1441 | builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
| 1442 | } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
| 1443 | mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
| 1444 | builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT); |
| 1445 | } else { |
| 1446 | builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT); |
| 1447 | } |
| 1448 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 1449 | } |
| 1450 | |
| 1451 | break; |
| 1452 | |
| 1453 | #ifndef GLSLANG_WEB |
| 1454 | case EShLangCompute: |
| 1455 | builder.addCapability(spv::CapabilityShader); |
| 1456 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1457 | glslangIntermediate->getLocalSize(1), |
| 1458 | glslangIntermediate->getLocalSize(2)); |
| 1459 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1460 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1461 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1462 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1463 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1464 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1465 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1466 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1467 | } |
| 1468 | break; |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1469 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1470 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1471 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1472 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1473 | glslang::TLayoutGeometry primitive; |
| 1474 | |
| 1475 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1476 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1477 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1478 | } else { |
| 1479 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1480 | } |
| 1481 | |
| 1482 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1483 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1484 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1485 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1486 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1487 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1488 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1489 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1490 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1491 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1492 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1493 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1494 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1495 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1496 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1497 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1498 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1499 | |
| 1500 | switch (glslangIntermediate->getVertexOrder()) { |
| 1501 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1502 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1503 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1504 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1505 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1506 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1507 | |
| 1508 | if (glslangIntermediate->getPointMode()) |
| 1509 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1510 | break; |
| 1511 | |
| 1512 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1513 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1514 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1515 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1516 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1517 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1518 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1519 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1520 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1521 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1522 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1523 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1524 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1525 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1526 | |
| 1527 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1528 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1529 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1530 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1531 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1532 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1533 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1534 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1535 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1536 | break; |
| 1537 | |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1538 | case EShLangRayGenNV: |
| 1539 | case EShLangIntersectNV: |
| 1540 | case EShLangAnyHitNV: |
| 1541 | case EShLangClosestHitNV: |
| 1542 | case EShLangMissNV: |
| 1543 | case EShLangCallableNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1544 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1545 | builder.addExtension("SPV_NV_ray_tracing"); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1546 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1547 | case EShLangTaskNV: |
| 1548 | case EShLangMeshNV: |
| 1549 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1550 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1551 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1552 | glslangIntermediate->getLocalSize(1), |
| 1553 | glslangIntermediate->getLocalSize(2)); |
| 1554 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 1555 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1556 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives()); |
| 1557 | |
| 1558 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1559 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1560 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1561 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1562 | default: mode = spv::ExecutionModeMax; break; |
| 1563 | } |
| 1564 | if (mode != spv::ExecutionModeMax) |
| 1565 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1566 | } |
| 1567 | break; |
| 1568 | #endif |
| 1569 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1570 | default: |
| 1571 | break; |
| 1572 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1573 | } |
| 1574 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1575 | // Finish creating SPV, after the traversal is complete. |
| 1576 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1577 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1578 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1579 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1580 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1581 | builder.leaveFunction(); |
| 1582 | } |
| 1583 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1584 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1585 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1586 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1587 | |
John Kessenich | 23d2775 | 2019-07-28 02:12:10 -0600 | [diff] [blame] | 1588 | #ifndef GLSLANG_WEB |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1589 | // Add capabilities, extensions, remove unneeded decorations, etc., |
| 1590 | // based on the resulting SPIR-V. |
| 1591 | builder.postProcess(); |
John Kessenich | 23d2775 | 2019-07-28 02:12:10 -0600 | [diff] [blame] | 1592 | #endif |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1595 | // Write the SPV into 'out'. |
| 1596 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1597 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1598 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | // |
| 1602 | // Implement the traversal functions. |
| 1603 | // |
| 1604 | // Return true from interior nodes to have the external traversal |
| 1605 | // continue on to children. Return false if children were |
| 1606 | // already processed. |
| 1607 | // |
| 1608 | |
| 1609 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1610 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1611 | // - uniform/input reads |
| 1612 | // - output writes |
| 1613 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1614 | // - something simple that degenerates into the last bullet |
| 1615 | // |
| 1616 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1617 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1618 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1619 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1620 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1621 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1622 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1623 | // Formal function parameters were mapped during makeFunctions(). |
| 1624 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1625 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1626 | if (builder.isPointer(id)) { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1627 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
John Kessenich | 7c7731e | 2019-01-04 16:47:06 +0700 | [diff] [blame] | 1628 | // Consider adding to the OpEntryPoint interface list. |
| 1629 | // Only looking at structures if they have at least one member. |
| 1630 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
| 1631 | spv::StorageClass sc = builder.getStorageClass(id); |
| 1632 | // Before SPIR-V 1.4, we only want to include Input and Output. |
| 1633 | // Starting with SPIR-V 1.4, we want all globals. |
| 1634 | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && sc != spv::StorageClassFunction) || |
| 1635 | (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1636 | iOSet.insert(id); |
John Kessenich | 7c7731e | 2019-01-04 16:47:06 +0700 | [diff] [blame] | 1637 | } |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1638 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1639 | |
| 1640 | // If the SPIR-V type is required to be different than the AST type, |
| 1641 | // translate now from the SPIR-V type to the AST type, for the consuming |
| 1642 | // operation. |
| 1643 | // Note this turns it from an l-value to an r-value. |
| 1644 | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
| 1645 | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
| 1646 | id = translateForcedType(id); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1650 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1651 | // Prepare to generate code for the access |
| 1652 | |
| 1653 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1654 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1655 | // followed by setting the base. |
| 1656 | builder.clearAccessChain(); |
| 1657 | |
| 1658 | // For now, we consider all user variables as being in memory, so they are pointers, |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1659 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1660 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1661 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1662 | // B) Specialization constants (normal constants don't even come in as a variable), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1663 | // These are also pure R-values. |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1664 | // C) R-Values from type translation, see above call to translateForcedType() |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1665 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1666 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
| 1667 | !builder.isPointerType(builder.getTypeId(id))) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1668 | builder.setAccessChainRValue(id); |
| 1669 | else |
| 1670 | builder.setAccessChainLValue(id); |
| 1671 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1672 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 1673 | #ifndef GLSLANG_WEB |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1674 | // Process linkage-only nodes for any special additional interface work. |
| 1675 | if (linkageOnly) { |
| 1676 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1677 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1678 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1679 | // of declaration. |
| 1680 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1681 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1682 | // Save possible originating buffers for counter buffers, keyed by |
| 1683 | // making the potential counter-buffer name. |
| 1684 | std::string keyName = symbol->getName().c_str(); |
| 1685 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1686 | counterOriginator[keyName] = symbol; |
| 1687 | } else { |
| 1688 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1689 | std::string keyName = symbol->getName().c_str(); |
| 1690 | auto it = counterOriginator.find(keyName); |
| 1691 | if (it != counterOriginator.end()) { |
| 1692 | id = getSymbolId(it->second); |
| 1693 | if (id != spv::NoResult) { |
| 1694 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1695 | if (counterId != spv::NoResult) { |
| 1696 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1697 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1698 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | } |
| 1704 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 1705 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1709 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1710 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1711 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1712 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1713 | if (node->getType().getQualifier().isSpecConstant()) |
| 1714 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1715 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1716 | // First, handle special cases |
| 1717 | switch (node->getOp()) { |
| 1718 | case glslang::EOpAssign: |
| 1719 | case glslang::EOpAddAssign: |
| 1720 | case glslang::EOpSubAssign: |
| 1721 | case glslang::EOpMulAssign: |
| 1722 | case glslang::EOpVectorTimesMatrixAssign: |
| 1723 | case glslang::EOpVectorTimesScalarAssign: |
| 1724 | case glslang::EOpMatrixTimesScalarAssign: |
| 1725 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1726 | case glslang::EOpDivAssign: |
| 1727 | case glslang::EOpModAssign: |
| 1728 | case glslang::EOpAndAssign: |
| 1729 | case glslang::EOpInclusiveOrAssign: |
| 1730 | case glslang::EOpExclusiveOrAssign: |
| 1731 | case glslang::EOpLeftShiftAssign: |
| 1732 | case glslang::EOpRightShiftAssign: |
| 1733 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1734 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1735 | // says to evaluate the left before the right. So, always, left |
| 1736 | // node then right node. |
| 1737 | { |
| 1738 | // get the left l-value, save it away |
| 1739 | builder.clearAccessChain(); |
| 1740 | node->getLeft()->traverse(this); |
| 1741 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1742 | |
| 1743 | // evaluate the right |
| 1744 | builder.clearAccessChain(); |
| 1745 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1746 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1747 | |
| 1748 | if (node->getOp() != glslang::EOpAssign) { |
| 1749 | // the left is also an r-value |
| 1750 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1751 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1752 | |
| 1753 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1754 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1755 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1756 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1757 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1758 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1759 | node->getType().getBasicType()); |
| 1760 | |
| 1761 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1762 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | // store the result |
| 1766 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1767 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1768 | |
| 1769 | // assignments are expressions having an rValue after they are evaluated... |
| 1770 | builder.clearAccessChain(); |
| 1771 | builder.setAccessChainRValue(rValue); |
| 1772 | } |
| 1773 | return false; |
| 1774 | case glslang::EOpIndexDirect: |
| 1775 | case glslang::EOpIndexDirectStruct: |
| 1776 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1777 | // Structure, array, matrix, or vector indirection with statically known index. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1778 | // Get the left part of the access chain. |
| 1779 | node->getLeft()->traverse(this); |
| 1780 | |
| 1781 | // Add the next element in the chain |
| 1782 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1783 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1784 | if (! node->getLeft()->getType().isArray() && |
| 1785 | node->getLeft()->getType().isVector() && |
| 1786 | node->getOp() == glslang::EOpIndexDirect) { |
| 1787 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1788 | // so short circuit the access-chain stuff with a swizzle. |
| 1789 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1790 | swizzle.push_back(glslangIndex); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1791 | int dummySize; |
| 1792 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1793 | TranslateCoherent(node->getLeft()->getType()), |
| 1794 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1795 | } else { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1796 | |
| 1797 | // Load through a block reference is performed with a dot operator that |
| 1798 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 1799 | // do a load and reset the access chain. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1800 | if (node->getLeft()->isReference() && |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1801 | !node->getLeft()->getType().isArray() && |
| 1802 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1803 | { |
| 1804 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 1805 | builder.clearAccessChain(); |
| 1806 | builder.setAccessChainLValue(left); |
| 1807 | } |
| 1808 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1809 | int spvIndex = glslangIndex; |
| 1810 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1811 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1812 | { |
| 1813 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1814 | // index remapping due to hidden members in anonymous blocks. |
| 1815 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1816 | assert(remapper.size() > 0); |
| 1817 | spvIndex = remapper[glslangIndex]; |
| 1818 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1819 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1820 | // normal case for indexing array or structure or block |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 1821 | builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType()), node->getLeft()->getType().getBufferReferenceAlignment()); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1822 | |
| 1823 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1824 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1825 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1826 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | return false; |
| 1830 | case glslang::EOpIndexIndirect: |
| 1831 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1832 | // Array, matrix, or vector indirection with variable index. |
| 1833 | // Will use native SPIR-V access-chain for and array indirection; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1834 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1835 | // Will use the access chain's 'component' for variable index into a vector. |
| 1836 | |
| 1837 | // This adapter is building access chains left to right. |
| 1838 | // Set up the access chain to the left. |
| 1839 | node->getLeft()->traverse(this); |
| 1840 | |
| 1841 | // save it so that computing the right side doesn't trash it |
| 1842 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1843 | |
| 1844 | // compute the next index in the chain |
| 1845 | builder.clearAccessChain(); |
| 1846 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1847 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1848 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1849 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1850 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1851 | // restore the saved access chain |
| 1852 | builder.setAccessChain(partial); |
| 1853 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1854 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 1855 | int dummySize; |
| 1856 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1857 | TranslateCoherent(node->getLeft()->getType()), |
| 1858 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
| 1859 | } else |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 1860 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), node->getLeft()->getType().getBufferReferenceAlignment()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1861 | } |
| 1862 | return false; |
| 1863 | case glslang::EOpVectorSwizzle: |
| 1864 | { |
| 1865 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1866 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1867 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1868 | int dummySize; |
| 1869 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1870 | TranslateCoherent(node->getLeft()->getType()), |
| 1871 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1872 | } |
| 1873 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1874 | case glslang::EOpMatrixSwizzle: |
| 1875 | logger->missingFunctionality("matrix swizzle"); |
| 1876 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1877 | case glslang::EOpLogicalOr: |
| 1878 | case glslang::EOpLogicalAnd: |
| 1879 | { |
| 1880 | |
| 1881 | // These may require short circuiting, but can sometimes be done as straight |
| 1882 | // binary operations. The right operand must be short circuited if it has |
| 1883 | // side effects, and should probably be if it is complex. |
| 1884 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1885 | break; // handle below as a normal binary operation |
| 1886 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1887 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1888 | builder.clearAccessChain(); |
| 1889 | builder.setAccessChainRValue(result); |
| 1890 | } |
| 1891 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1892 | default: |
| 1893 | break; |
| 1894 | } |
| 1895 | |
| 1896 | // Assume generic binary op... |
| 1897 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1898 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1899 | builder.clearAccessChain(); |
| 1900 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1901 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1902 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1903 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1904 | builder.clearAccessChain(); |
| 1905 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1906 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1907 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1908 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1909 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1910 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1911 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1912 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1913 | convertGlslangToSpvType(node->getType()), left, right, |
| 1914 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1915 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1916 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1917 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1918 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1919 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1920 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1921 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1922 | return false; |
| 1923 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1924 | } |
| 1925 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1926 | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
| 1927 | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
| 1928 | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
| 1929 | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(spv::BuiltIn builtIn, |
| 1930 | const glslang::TType& glslangType) |
| 1931 | { |
| 1932 | switch(builtIn) |
| 1933 | { |
| 1934 | case spv::BuiltInSubgroupEqMask: |
| 1935 | case spv::BuiltInSubgroupGeMask: |
| 1936 | case spv::BuiltInSubgroupGtMask: |
| 1937 | case spv::BuiltInSubgroupLeMask: |
| 1938 | case spv::BuiltInSubgroupLtMask: { |
| 1939 | // these require changing a 64-bit scaler -> a vector of 32-bit components |
| 1940 | if (glslangType.isVector()) |
| 1941 | break; |
| 1942 | std::pair<spv::Id, spv::Id> ret(builder.makeVectorType(builder.makeUintType(32), 4), |
| 1943 | builder.makeUintType(64)); |
| 1944 | return ret; |
| 1945 | } |
| 1946 | default: |
| 1947 | break; |
| 1948 | } |
| 1949 | |
| 1950 | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
| 1951 | return ret; |
| 1952 | } |
| 1953 | |
| 1954 | // For an object previously identified (see getForcedType() and forceType) |
| 1955 | // as needing type translations, do the translation needed for a load, turning |
| 1956 | // an L-value into in R-value. |
| 1957 | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
| 1958 | { |
| 1959 | const auto forceIt = forceType.find(object); |
| 1960 | if (forceIt == forceType.end()) |
| 1961 | return object; |
| 1962 | |
| 1963 | spv::Id desiredTypeId = forceIt->second; |
| 1964 | spv::Id objectTypeId = builder.getTypeId(object); |
| 1965 | assert(builder.isPointerType(objectTypeId)); |
| 1966 | objectTypeId = builder.getContainedTypeId(objectTypeId); |
| 1967 | if (builder.isVectorType(objectTypeId) && |
| 1968 | builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
| 1969 | if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
| 1970 | // handle 32-bit v.xy* -> 64-bit |
| 1971 | builder.clearAccessChain(); |
| 1972 | builder.setAccessChainLValue(object); |
| 1973 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 1974 | std::vector<spv::Id> components; |
| 1975 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
| 1976 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
| 1977 | |
| 1978 | spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
| 1979 | return builder.createUnaryOp(spv::OpBitcast, desiredTypeId, |
| 1980 | builder.createCompositeConstruct(vecType, components)); |
| 1981 | } else { |
| 1982 | logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar"); |
| 1983 | } |
| 1984 | } else { |
| 1985 | logger->missingFunctionality("forcing non 32-bit vector type"); |
| 1986 | } |
| 1987 | |
| 1988 | return object; |
| 1989 | } |
| 1990 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1991 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1992 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1993 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1994 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1995 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1996 | if (node->getType().getQualifier().isSpecConstant()) |
| 1997 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1998 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1999 | spv::Id result = spv::NoResult; |
| 2000 | |
| 2001 | // try texturing first |
| 2002 | result = createImageTextureFunctionCall(node); |
| 2003 | if (result != spv::NoResult) { |
| 2004 | builder.clearAccessChain(); |
| 2005 | builder.setAccessChainRValue(result); |
| 2006 | |
| 2007 | return false; // done with this node |
| 2008 | } |
| 2009 | |
| 2010 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2011 | |
| 2012 | if (node->getOp() == glslang::EOpArrayLength) { |
| 2013 | // Quite special; won't want to evaluate the operand. |
| 2014 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2015 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 2016 | // except for the last block membeor of an SSBO. |
| 2017 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 2018 | // size extracted. |
| 2019 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2020 | // Normal .length() would have been constant folded by the front-end. |
| 2021 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 2022 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2023 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2024 | spv::Id length; |
| 2025 | if (node->getOperand()->getType().isCoopMat()) { |
| 2026 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2027 | |
| 2028 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
| 2029 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2030 | |
| 2031 | length = builder.createCooperativeMatrixLength(typeId); |
| 2032 | } else { |
| 2033 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2034 | block->traverse(this); |
| 2035 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 2036 | length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
| 2037 | } |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2038 | |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2039 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 2040 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 2041 | // AST expectation of a signed result. |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2042 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
| 2043 | if (builder.isInSpecConstCodeGenMode()) { |
| 2044 | length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0)); |
| 2045 | } else { |
| 2046 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 2047 | } |
| 2048 | } |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2049 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2050 | builder.clearAccessChain(); |
| 2051 | builder.setAccessChainRValue(length); |
| 2052 | |
| 2053 | return false; |
| 2054 | } |
| 2055 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2056 | // Start by evaluating the operand |
| 2057 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2058 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2059 | // operate first on the swizzle base, then apply the swizzle. |
| 2060 | spv::Id invertedType = spv::NoType; |
| 2061 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 2062 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 2063 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 2064 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2065 | builder.clearAccessChain(); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2066 | TIntermNode *operandNode; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2067 | if (invertedType != spv::NoType) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2068 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2069 | else |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2070 | operandNode = node->getOperand(); |
| 2071 | |
| 2072 | operandNode->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 2073 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2074 | spv::Id operand = spv::NoResult; |
| 2075 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2076 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2077 | |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2078 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2079 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 2080 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2081 | node->getOp() == glslang::EOpAtomicCounter || |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2082 | node->getOp() == glslang::EOpInterpolateAtCentroid) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2083 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2084 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2085 | lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
| 2086 | } else |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2087 | #endif |
| 2088 | { |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2089 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2090 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2091 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2092 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2093 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2094 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2095 | |
| 2096 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2097 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2098 | result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2099 | |
| 2100 | // if not, then possibly an operation |
| 2101 | if (! result) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2102 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2103 | |
| 2104 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2105 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2106 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2107 | builder.addDecoration(result, decorations.nonUniform); |
| 2108 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2109 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2110 | builder.clearAccessChain(); |
| 2111 | builder.setAccessChainRValue(result); |
| 2112 | |
| 2113 | return false; // done with this node |
| 2114 | } |
| 2115 | |
| 2116 | // it must be a special case, check... |
| 2117 | switch (node->getOp()) { |
| 2118 | case glslang::EOpPostIncrement: |
| 2119 | case glslang::EOpPostDecrement: |
| 2120 | case glslang::EOpPreIncrement: |
| 2121 | case glslang::EOpPreDecrement: |
| 2122 | { |
| 2123 | // we need the integer value "1" or the floating point "1.0" to add/subtract |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2124 | spv::Id one = 0; |
| 2125 | if (node->getBasicType() == glslang::EbtFloat) |
| 2126 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2127 | #ifndef GLSLANG_WEB |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 2128 | else if (node->getBasicType() == glslang::EbtDouble) |
| 2129 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2130 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 2131 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2132 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 2133 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2134 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 2135 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2136 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 2137 | one = builder.makeInt64Constant(1); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2138 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2139 | else |
| 2140 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2141 | glslang::TOperator op; |
| 2142 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2143 | node->getOp() == glslang::EOpPostIncrement) |
| 2144 | op = glslang::EOpAdd; |
| 2145 | else |
| 2146 | op = glslang::EOpSub; |
| 2147 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2148 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2149 | convertGlslangToSpvType(node->getType()), operand, one, |
| 2150 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2151 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2152 | |
| 2153 | // The result of operation is always stored, but conditionally the |
| 2154 | // consumed result. The consumed result is always an r-value. |
| 2155 | builder.accessChainStore(result); |
| 2156 | builder.clearAccessChain(); |
| 2157 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2158 | node->getOp() == glslang::EOpPreDecrement) |
| 2159 | builder.setAccessChainRValue(result); |
| 2160 | else |
| 2161 | builder.setAccessChainRValue(operand); |
| 2162 | } |
| 2163 | |
| 2164 | return false; |
| 2165 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2166 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2167 | case glslang::EOpEmitStreamVertex: |
| 2168 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 2169 | return false; |
| 2170 | case glslang::EOpEndStreamPrimitive: |
| 2171 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 2172 | return false; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2173 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2174 | |
| 2175 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2176 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2177 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2178 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2179 | } |
| 2180 | |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2181 | // Construct a composite object, recursively copying members if their types don't match |
| 2182 | spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
| 2183 | { |
| 2184 | for (int c = 0; c < (int)constituents.size(); ++c) { |
| 2185 | spv::Id& constituent = constituents[c]; |
| 2186 | spv::Id lType = builder.getContainedTypeId(resultTypeId, c); |
| 2187 | spv::Id rType = builder.getTypeId(constituent); |
| 2188 | if (lType != rType) { |
| 2189 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 2190 | constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent); |
| 2191 | } else if (builder.isStructType(rType)) { |
| 2192 | std::vector<spv::Id> rTypeConstituents; |
| 2193 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2194 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2195 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, builder.getContainedTypeId(rType, i), i)); |
| 2196 | } |
| 2197 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2198 | } else { |
| 2199 | assert(builder.isArrayType(rType)); |
| 2200 | std::vector<spv::Id> rTypeConstituents; |
| 2201 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2202 | |
| 2203 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2204 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2205 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i)); |
| 2206 | } |
| 2207 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | return builder.createCompositeConstruct(resultTypeId, constituents); |
| 2212 | } |
| 2213 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2214 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2215 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 2216 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2217 | if (node->getType().getQualifier().isSpecConstant()) |
| 2218 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2219 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2220 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2221 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2222 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2223 | |
| 2224 | // try texturing |
| 2225 | result = createImageTextureFunctionCall(node); |
| 2226 | if (result != spv::NoResult) { |
| 2227 | builder.clearAccessChain(); |
| 2228 | builder.setAccessChainRValue(result); |
| 2229 | |
| 2230 | return false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2231 | } |
| 2232 | #ifndef GLSLANG_WEB |
| 2233 | else if (node->getOp() == glslang::EOpImageStore || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2234 | node->getOp() == glslang::EOpImageStoreLod || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2235 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2236 | // "imageStore" is a special case, which has no result |
| 2237 | return false; |
| 2238 | } |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2239 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2240 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2241 | glslang::TOperator binOp = glslang::EOpNull; |
| 2242 | bool reduceComparison = true; |
| 2243 | bool isMatrix = false; |
| 2244 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2245 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2246 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2247 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2248 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2249 | assert(node->getOp()); |
| 2250 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 2251 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2252 | |
| 2253 | switch (node->getOp()) { |
| 2254 | case glslang::EOpSequence: |
| 2255 | { |
| 2256 | if (preVisit) |
| 2257 | ++sequenceDepth; |
| 2258 | else |
| 2259 | --sequenceDepth; |
| 2260 | |
| 2261 | if (sequenceDepth == 1) { |
| 2262 | // If this is the parent node of all the functions, we want to see them |
| 2263 | // early, so all call points have actual SPIR-V functions to reference. |
| 2264 | // In all cases, still let the traverser visit the children for us. |
| 2265 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 2266 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2267 | // Also, we want all globals initializers to go into the beginning of the entry point, before |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2268 | // anything else gets there, so visit out of order, doing them all now. |
| 2269 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2270 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2271 | // Initializers are done, don't want to visit again, but functions and link objects need to be processed, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2272 | // so do them manually. |
| 2273 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2274 | |
| 2275 | return false; |
| 2276 | } |
| 2277 | |
| 2278 | return true; |
| 2279 | } |
| 2280 | case glslang::EOpLinkerObjects: |
| 2281 | { |
| 2282 | if (visit == glslang::EvPreVisit) |
| 2283 | linkageOnly = true; |
| 2284 | else |
| 2285 | linkageOnly = false; |
| 2286 | |
| 2287 | return true; |
| 2288 | } |
| 2289 | case glslang::EOpComma: |
| 2290 | { |
| 2291 | // processing from left to right naturally leaves the right-most |
| 2292 | // lying around in the access chain |
| 2293 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2294 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2295 | glslangOperands[i]->traverse(this); |
| 2296 | |
| 2297 | return false; |
| 2298 | } |
| 2299 | case glslang::EOpFunction: |
| 2300 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2301 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2302 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2303 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2304 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2305 | } else { |
| 2306 | handleFunctionEntry(node); |
| 2307 | } |
| 2308 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2309 | if (inEntryPoint) |
| 2310 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2311 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2312 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | return true; |
| 2316 | case glslang::EOpParameters: |
| 2317 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2318 | // the body, so we still visited the function node's children, making this |
| 2319 | // child redundant. |
| 2320 | return false; |
| 2321 | case glslang::EOpFunctionCall: |
| 2322 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2323 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2324 | if (node->isUserDefined()) |
| 2325 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2326 | // assert(result); // this can happen for bad shaders because the call graph completeness checking is not yet done |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2327 | if (result) { |
| 2328 | builder.clearAccessChain(); |
| 2329 | builder.setAccessChainRValue(result); |
| 2330 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2331 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2332 | |
| 2333 | return false; |
| 2334 | } |
| 2335 | case glslang::EOpConstructMat2x2: |
| 2336 | case glslang::EOpConstructMat2x3: |
| 2337 | case glslang::EOpConstructMat2x4: |
| 2338 | case glslang::EOpConstructMat3x2: |
| 2339 | case glslang::EOpConstructMat3x3: |
| 2340 | case glslang::EOpConstructMat3x4: |
| 2341 | case glslang::EOpConstructMat4x2: |
| 2342 | case glslang::EOpConstructMat4x3: |
| 2343 | case glslang::EOpConstructMat4x4: |
| 2344 | case glslang::EOpConstructDMat2x2: |
| 2345 | case glslang::EOpConstructDMat2x3: |
| 2346 | case glslang::EOpConstructDMat2x4: |
| 2347 | case glslang::EOpConstructDMat3x2: |
| 2348 | case glslang::EOpConstructDMat3x3: |
| 2349 | case glslang::EOpConstructDMat3x4: |
| 2350 | case glslang::EOpConstructDMat4x2: |
| 2351 | case glslang::EOpConstructDMat4x3: |
| 2352 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2353 | case glslang::EOpConstructIMat2x2: |
| 2354 | case glslang::EOpConstructIMat2x3: |
| 2355 | case glslang::EOpConstructIMat2x4: |
| 2356 | case glslang::EOpConstructIMat3x2: |
| 2357 | case glslang::EOpConstructIMat3x3: |
| 2358 | case glslang::EOpConstructIMat3x4: |
| 2359 | case glslang::EOpConstructIMat4x2: |
| 2360 | case glslang::EOpConstructIMat4x3: |
| 2361 | case glslang::EOpConstructIMat4x4: |
| 2362 | case glslang::EOpConstructUMat2x2: |
| 2363 | case glslang::EOpConstructUMat2x3: |
| 2364 | case glslang::EOpConstructUMat2x4: |
| 2365 | case glslang::EOpConstructUMat3x2: |
| 2366 | case glslang::EOpConstructUMat3x3: |
| 2367 | case glslang::EOpConstructUMat3x4: |
| 2368 | case glslang::EOpConstructUMat4x2: |
| 2369 | case glslang::EOpConstructUMat4x3: |
| 2370 | case glslang::EOpConstructUMat4x4: |
| 2371 | case glslang::EOpConstructBMat2x2: |
| 2372 | case glslang::EOpConstructBMat2x3: |
| 2373 | case glslang::EOpConstructBMat2x4: |
| 2374 | case glslang::EOpConstructBMat3x2: |
| 2375 | case glslang::EOpConstructBMat3x3: |
| 2376 | case glslang::EOpConstructBMat3x4: |
| 2377 | case glslang::EOpConstructBMat4x2: |
| 2378 | case glslang::EOpConstructBMat4x3: |
| 2379 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2380 | case glslang::EOpConstructF16Mat2x2: |
| 2381 | case glslang::EOpConstructF16Mat2x3: |
| 2382 | case glslang::EOpConstructF16Mat2x4: |
| 2383 | case glslang::EOpConstructF16Mat3x2: |
| 2384 | case glslang::EOpConstructF16Mat3x3: |
| 2385 | case glslang::EOpConstructF16Mat3x4: |
| 2386 | case glslang::EOpConstructF16Mat4x2: |
| 2387 | case glslang::EOpConstructF16Mat4x3: |
| 2388 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2389 | isMatrix = true; |
| 2390 | // fall through |
| 2391 | case glslang::EOpConstructFloat: |
| 2392 | case glslang::EOpConstructVec2: |
| 2393 | case glslang::EOpConstructVec3: |
| 2394 | case glslang::EOpConstructVec4: |
| 2395 | case glslang::EOpConstructDouble: |
| 2396 | case glslang::EOpConstructDVec2: |
| 2397 | case glslang::EOpConstructDVec3: |
| 2398 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2399 | case glslang::EOpConstructFloat16: |
| 2400 | case glslang::EOpConstructF16Vec2: |
| 2401 | case glslang::EOpConstructF16Vec3: |
| 2402 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2403 | case glslang::EOpConstructBool: |
| 2404 | case glslang::EOpConstructBVec2: |
| 2405 | case glslang::EOpConstructBVec3: |
| 2406 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2407 | case glslang::EOpConstructInt8: |
| 2408 | case glslang::EOpConstructI8Vec2: |
| 2409 | case glslang::EOpConstructI8Vec3: |
| 2410 | case glslang::EOpConstructI8Vec4: |
| 2411 | case glslang::EOpConstructUint8: |
| 2412 | case glslang::EOpConstructU8Vec2: |
| 2413 | case glslang::EOpConstructU8Vec3: |
| 2414 | case glslang::EOpConstructU8Vec4: |
| 2415 | case glslang::EOpConstructInt16: |
| 2416 | case glslang::EOpConstructI16Vec2: |
| 2417 | case glslang::EOpConstructI16Vec3: |
| 2418 | case glslang::EOpConstructI16Vec4: |
| 2419 | case glslang::EOpConstructUint16: |
| 2420 | case glslang::EOpConstructU16Vec2: |
| 2421 | case glslang::EOpConstructU16Vec3: |
| 2422 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2423 | case glslang::EOpConstructInt: |
| 2424 | case glslang::EOpConstructIVec2: |
| 2425 | case glslang::EOpConstructIVec3: |
| 2426 | case glslang::EOpConstructIVec4: |
| 2427 | case glslang::EOpConstructUint: |
| 2428 | case glslang::EOpConstructUVec2: |
| 2429 | case glslang::EOpConstructUVec3: |
| 2430 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2431 | case glslang::EOpConstructInt64: |
| 2432 | case glslang::EOpConstructI64Vec2: |
| 2433 | case glslang::EOpConstructI64Vec3: |
| 2434 | case glslang::EOpConstructI64Vec4: |
| 2435 | case glslang::EOpConstructUint64: |
| 2436 | case glslang::EOpConstructU64Vec2: |
| 2437 | case glslang::EOpConstructU64Vec3: |
| 2438 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2439 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2440 | case glslang::EOpConstructTextureSampler: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2441 | case glslang::EOpConstructReference: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2442 | case glslang::EOpConstructCooperativeMatrix: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2443 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2444 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2445 | std::vector<spv::Id> arguments; |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2446 | translateArguments(*node, arguments, lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2447 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2448 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2449 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2450 | else if (node->getOp() == glslang::EOpConstructStruct || |
| 2451 | node->getOp() == glslang::EOpConstructCooperativeMatrix || |
| 2452 | node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2453 | std::vector<spv::Id> constituents; |
| 2454 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2455 | constituents.push_back(arguments[c]); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2456 | constructed = createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2457 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2458 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2459 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2460 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2461 | |
| 2462 | builder.clearAccessChain(); |
| 2463 | builder.setAccessChainRValue(constructed); |
| 2464 | |
| 2465 | return false; |
| 2466 | } |
| 2467 | |
| 2468 | // These six are component-wise compares with component-wise results. |
| 2469 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2470 | case glslang::EOpLessThan: |
| 2471 | case glslang::EOpGreaterThan: |
| 2472 | case glslang::EOpLessThanEqual: |
| 2473 | case glslang::EOpGreaterThanEqual: |
| 2474 | case glslang::EOpVectorEqual: |
| 2475 | case glslang::EOpVectorNotEqual: |
| 2476 | { |
| 2477 | // Map the operation to a binary |
| 2478 | binOp = node->getOp(); |
| 2479 | reduceComparison = false; |
| 2480 | switch (node->getOp()) { |
| 2481 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2482 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2483 | default: binOp = node->getOp(); break; |
| 2484 | } |
| 2485 | |
| 2486 | break; |
| 2487 | } |
| 2488 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2489 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2490 | binOp = glslang::EOpMul; |
| 2491 | break; |
| 2492 | case glslang::EOpOuterProduct: |
| 2493 | // two vectors multiplied to make a matrix |
| 2494 | binOp = glslang::EOpOuterProduct; |
| 2495 | break; |
| 2496 | case glslang::EOpDot: |
| 2497 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2498 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2499 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2500 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2501 | binOp = glslang::EOpMul; |
| 2502 | break; |
| 2503 | } |
| 2504 | case glslang::EOpMod: |
| 2505 | // when an aggregate, this is the floating-point mod built-in function, |
| 2506 | // which can be emitted by the one in createBinaryOperation() |
| 2507 | binOp = glslang::EOpMod; |
| 2508 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2509 | |
| 2510 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2511 | case glslang::EOpEmitVertex: |
| 2512 | case glslang::EOpEndPrimitive: |
| 2513 | case glslang::EOpBarrier: |
| 2514 | case glslang::EOpMemoryBarrier: |
| 2515 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2516 | case glslang::EOpMemoryBarrierBuffer: |
| 2517 | case glslang::EOpMemoryBarrierImage: |
| 2518 | case glslang::EOpMemoryBarrierShared: |
| 2519 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2520 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2521 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2522 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2523 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2524 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2525 | case glslang::EOpSubgroupBarrier: |
| 2526 | case glslang::EOpSubgroupMemoryBarrier: |
| 2527 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2528 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2529 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2530 | noReturnValue = true; |
| 2531 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2532 | break; |
| 2533 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2534 | case glslang::EOpAtomicStore: |
| 2535 | noReturnValue = true; |
| 2536 | // fallthrough |
| 2537 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2538 | case glslang::EOpAtomicAdd: |
| 2539 | case glslang::EOpAtomicMin: |
| 2540 | case glslang::EOpAtomicMax: |
| 2541 | case glslang::EOpAtomicAnd: |
| 2542 | case glslang::EOpAtomicOr: |
| 2543 | case glslang::EOpAtomicXor: |
| 2544 | case glslang::EOpAtomicExchange: |
| 2545 | case glslang::EOpAtomicCompSwap: |
| 2546 | atomic = true; |
| 2547 | break; |
| 2548 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2549 | case glslang::EOpAtomicCounterAdd: |
| 2550 | case glslang::EOpAtomicCounterSubtract: |
| 2551 | case glslang::EOpAtomicCounterMin: |
| 2552 | case glslang::EOpAtomicCounterMax: |
| 2553 | case glslang::EOpAtomicCounterAnd: |
| 2554 | case glslang::EOpAtomicCounterOr: |
| 2555 | case glslang::EOpAtomicCounterXor: |
| 2556 | case glslang::EOpAtomicCounterExchange: |
| 2557 | case glslang::EOpAtomicCounterCompSwap: |
| 2558 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2559 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2560 | atomic = true; |
| 2561 | break; |
| 2562 | |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 2563 | case glslang::EOpIgnoreIntersectionNV: |
| 2564 | case glslang::EOpTerminateRayNV: |
| 2565 | case glslang::EOpTraceNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 2566 | case glslang::EOpExecuteCallableNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2567 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2568 | noReturnValue = true; |
| 2569 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2570 | case glslang::EOpCooperativeMatrixLoad: |
| 2571 | case glslang::EOpCooperativeMatrixStore: |
| 2572 | noReturnValue = true; |
| 2573 | break; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 2574 | case glslang::EOpBeginInvocationInterlock: |
| 2575 | case glslang::EOpEndInvocationInterlock: |
| 2576 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 2577 | noReturnValue = true; |
| 2578 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2579 | #endif |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2580 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2581 | default: |
| 2582 | break; |
| 2583 | } |
| 2584 | |
| 2585 | // |
| 2586 | // See if it maps to a regular operation. |
| 2587 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2588 | if (binOp != glslang::EOpNull) { |
| 2589 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2590 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2591 | assert(left && right); |
| 2592 | |
| 2593 | builder.clearAccessChain(); |
| 2594 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2595 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2596 | |
| 2597 | builder.clearAccessChain(); |
| 2598 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2599 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2600 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2601 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2602 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2603 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2604 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2605 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2606 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2607 | left->getType().getBasicType(), reduceComparison); |
| 2608 | |
| 2609 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2610 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2611 | builder.clearAccessChain(); |
| 2612 | builder.setAccessChainRValue(result); |
| 2613 | |
| 2614 | return false; |
| 2615 | } |
| 2616 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2617 | // |
| 2618 | // Create the list of operands. |
| 2619 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2620 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2621 | std::vector<spv::Id> operands; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2622 | std::vector<spv::IdImmediate> memoryAccessOperands; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2623 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2624 | // special case l-value operands; there are just a few |
| 2625 | bool lvalue = false; |
| 2626 | switch (node->getOp()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2627 | case glslang::EOpModf: |
| 2628 | if (arg == 1) |
| 2629 | lvalue = true; |
| 2630 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2631 | #ifndef GLSLANG_WEB |
| 2632 | case glslang::EOpFrexp: |
| 2633 | if (arg == 1) |
| 2634 | lvalue = true; |
| 2635 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2636 | case glslang::EOpInterpolateAtSample: |
| 2637 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2638 | case glslang::EOpInterpolateAtVertex: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2639 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2640 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2641 | |
| 2642 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2643 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2644 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2645 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2646 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 2647 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2648 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2649 | case glslang::EOpAtomicAdd: |
| 2650 | case glslang::EOpAtomicMin: |
| 2651 | case glslang::EOpAtomicMax: |
| 2652 | case glslang::EOpAtomicAnd: |
| 2653 | case glslang::EOpAtomicOr: |
| 2654 | case glslang::EOpAtomicXor: |
| 2655 | case glslang::EOpAtomicExchange: |
| 2656 | case glslang::EOpAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2657 | case glslang::EOpAtomicLoad: |
| 2658 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2659 | case glslang::EOpAtomicCounterAdd: |
| 2660 | case glslang::EOpAtomicCounterSubtract: |
| 2661 | case glslang::EOpAtomicCounterMin: |
| 2662 | case glslang::EOpAtomicCounterMax: |
| 2663 | case glslang::EOpAtomicCounterAnd: |
| 2664 | case glslang::EOpAtomicCounterOr: |
| 2665 | case glslang::EOpAtomicCounterXor: |
| 2666 | case glslang::EOpAtomicCounterExchange: |
| 2667 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2668 | if (arg == 0) |
| 2669 | lvalue = true; |
| 2670 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2671 | case glslang::EOpAddCarry: |
| 2672 | case glslang::EOpSubBorrow: |
| 2673 | if (arg == 2) |
| 2674 | lvalue = true; |
| 2675 | break; |
| 2676 | case glslang::EOpUMulExtended: |
| 2677 | case glslang::EOpIMulExtended: |
| 2678 | if (arg >= 2) |
| 2679 | lvalue = true; |
| 2680 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2681 | case glslang::EOpCooperativeMatrixLoad: |
| 2682 | if (arg == 0 || arg == 1) |
| 2683 | lvalue = true; |
| 2684 | break; |
| 2685 | case glslang::EOpCooperativeMatrixStore: |
| 2686 | if (arg == 1) |
| 2687 | lvalue = true; |
| 2688 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2689 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2690 | default: |
| 2691 | break; |
| 2692 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2693 | builder.clearAccessChain(); |
| 2694 | if (invertedType != spv::NoType && arg == 0) |
| 2695 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2696 | else |
| 2697 | glslangOperands[arg]->traverse(this); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2698 | |
| 2699 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
| 2700 | node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2701 | |
| 2702 | if (arg == 1) { |
| 2703 | // fold "element" parameter into the access chain |
| 2704 | spv::Builder::AccessChain save = builder.getAccessChain(); |
| 2705 | builder.clearAccessChain(); |
| 2706 | glslangOperands[2]->traverse(this); |
| 2707 | |
| 2708 | spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
| 2709 | |
| 2710 | builder.setAccessChain(save); |
| 2711 | |
| 2712 | // Point to the first element of the array. |
| 2713 | builder.accessChainPush(elementId, TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 2714 | glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2715 | |
| 2716 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 2717 | unsigned int alignment = builder.getAccessChain().alignment; |
| 2718 | |
| 2719 | int memoryAccess = TranslateMemoryAccess(coherentFlags); |
| 2720 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
| 2721 | memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
| 2722 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
| 2723 | memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
| 2724 | if (builder.getStorageClass(builder.getAccessChain().base) == spv::StorageClassPhysicalStorageBufferEXT) { |
| 2725 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
| 2726 | } |
| 2727 | |
| 2728 | memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
| 2729 | |
| 2730 | if (memoryAccess & spv::MemoryAccessAlignedMask) { |
| 2731 | memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
| 2732 | } |
| 2733 | |
| 2734 | if (memoryAccess & (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
| 2735 | memoryAccessOperands.push_back(spv::IdImmediate(true, builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
| 2736 | } |
| 2737 | } else if (arg == 2) { |
| 2738 | continue; |
| 2739 | } |
| 2740 | } |
| 2741 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2742 | if (lvalue) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2743 | operands.push_back(builder.accessChainGetLValue()); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2744 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2745 | lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
| 2746 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2747 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2748 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2749 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2750 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2751 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2752 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2753 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
| 2754 | std::vector<spv::IdImmediate> idImmOps; |
| 2755 | |
| 2756 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 2757 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 2758 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 2759 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 2760 | // get the pointee type |
| 2761 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
| 2762 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2763 | // do the op |
| 2764 | spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
| 2765 | // store the result to the pointer (out param 'm') |
| 2766 | builder.createStore(result, operands[0]); |
| 2767 | result = 0; |
| 2768 | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2769 | std::vector<spv::IdImmediate> idImmOps; |
| 2770 | |
| 2771 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 2772 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
| 2773 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 2774 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 2775 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 2776 | |
| 2777 | builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps); |
| 2778 | result = 0; |
| 2779 | } else if (atomic) { |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2780 | // Handle all atomics |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2781 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), lvalueCoherentFlags); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2782 | } else { |
| 2783 | // Pass through to generic operations. |
| 2784 | switch (glslangOperands.size()) { |
| 2785 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2786 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2787 | break; |
| 2788 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2789 | { |
| 2790 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2791 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2792 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2793 | result = createUnaryOperation( |
| 2794 | node->getOp(), decorations, |
| 2795 | resultType(), operands.front(), |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2796 | glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2797 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2798 | break; |
| 2799 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2800 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2801 | break; |
| 2802 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2803 | if (invertedType) |
| 2804 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | if (noReturnValue) |
| 2808 | return false; |
| 2809 | |
| 2810 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2811 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2812 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2813 | } else { |
| 2814 | builder.clearAccessChain(); |
| 2815 | builder.setAccessChainRValue(result); |
| 2816 | return false; |
| 2817 | } |
| 2818 | } |
| 2819 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2820 | // This path handles both if-then-else and ?: |
| 2821 | // The if-then-else has a node type of void, while |
| 2822 | // ?: has either a void or a non-void node type |
| 2823 | // |
| 2824 | // Leaving the result, when not void: |
| 2825 | // GLSL only has r-values as the result of a :?, but |
| 2826 | // if we have an l-value, that can be more efficient if it will |
| 2827 | // become the base of a complex r-value expression, because the |
| 2828 | // next layer copies r-values into memory to use the access-chain mechanism |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2829 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 2830 | { |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2831 | // see if OpSelect can handle it |
| 2832 | const auto isOpSelectable = [&]() { |
| 2833 | if (node->getBasicType() == glslang::EbtVoid) |
| 2834 | return false; |
| 2835 | // OpSelect can do all other types starting with SPV 1.4 |
| 2836 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
| 2837 | // pre-1.4, only scalars and vectors can be handled |
| 2838 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
| 2839 | return false; |
| 2840 | } |
| 2841 | return true; |
| 2842 | }; |
| 2843 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2844 | // See if it simple and safe, or required, to execute both sides. |
| 2845 | // Crucially, side effects must be either semantically required or avoided, |
| 2846 | // and there are performance trade-offs. |
| 2847 | // Return true if required or a good idea (and safe) to execute both sides, |
| 2848 | // false otherwise. |
| 2849 | const auto bothSidesPolicy = [&]() -> bool { |
| 2850 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2851 | if (node->getTrueBlock() == nullptr || |
| 2852 | node->getFalseBlock() == nullptr) |
| 2853 | return false; |
| 2854 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2855 | // required? (unless we write additional code to look for side effects |
| 2856 | // and make performance trade-offs if none are present) |
| 2857 | if (!node->getShortCircuit()) |
| 2858 | return true; |
| 2859 | |
| 2860 | // if not required to execute both, decide based on performance/practicality... |
| 2861 | |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2862 | if (!isOpSelectable()) |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2863 | return false; |
| 2864 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2865 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 2866 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 2867 | |
| 2868 | // return true if a single operand to ? : is okay for OpSelect |
| 2869 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2870 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2871 | }; |
| 2872 | |
| 2873 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 2874 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 2875 | }; |
| 2876 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2877 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 2878 | // emit the condition before doing anything with selection |
| 2879 | node->getCondition()->traverse(this); |
| 2880 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 2881 | |
| 2882 | // Find a way of executing both sides and selecting the right result. |
| 2883 | const auto executeBothSides = [&]() -> void { |
| 2884 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2885 | node->getTrueBlock()->traverse(this); |
| 2886 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2887 | node->getFalseBlock()->traverse(this); |
| 2888 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2889 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2890 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2891 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2892 | // done if void |
| 2893 | if (node->getBasicType() == glslang::EbtVoid) |
| 2894 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 2895 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2896 | // emit code to select between trueValue and falseValue |
| 2897 | |
| 2898 | // see if OpSelect can handle it |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2899 | if (isOpSelectable()) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2900 | // Emit OpSelect for this selection. |
| 2901 | |
| 2902 | // smear condition to vector, if necessary (AST is always scalar) |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2903 | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
| 2904 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2905 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 2906 | builder.makeVectorType(builder.makeBoolType(), |
| 2907 | builder.getNumComponents(trueValue))); |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2908 | } |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2909 | |
| 2910 | // OpSelect |
| 2911 | result = builder.createTriOp(spv::OpSelect, |
| 2912 | convertGlslangToSpvType(node->getType()), condition, |
| 2913 | trueValue, falseValue); |
| 2914 | |
| 2915 | builder.clearAccessChain(); |
| 2916 | builder.setAccessChainRValue(result); |
| 2917 | } else { |
| 2918 | // We need control flow to select the result. |
| 2919 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 2920 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2921 | |
| 2922 | // Selection control: |
| 2923 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2924 | |
| 2925 | // make an "if" based on the value created by the condition |
| 2926 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2927 | |
| 2928 | // emit the "then" statement |
| 2929 | builder.createStore(trueValue, result); |
| 2930 | ifBuilder.makeBeginElse(); |
| 2931 | // emit the "else" statement |
| 2932 | builder.createStore(falseValue, result); |
| 2933 | |
| 2934 | // finish off the control flow |
| 2935 | ifBuilder.makeEndIf(); |
| 2936 | |
| 2937 | builder.clearAccessChain(); |
| 2938 | builder.setAccessChainLValue(result); |
| 2939 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2940 | }; |
| 2941 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2942 | // Execute the one side needed, as per the condition |
| 2943 | const auto executeOneSide = [&]() { |
| 2944 | // Always emit control flow. |
| 2945 | if (node->getBasicType() != glslang::EbtVoid) |
| 2946 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2947 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2948 | // Selection control: |
| 2949 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2950 | |
| 2951 | // make an "if" based on the value created by the condition |
| 2952 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2953 | |
| 2954 | // emit the "then" statement |
| 2955 | if (node->getTrueBlock() != nullptr) { |
| 2956 | node->getTrueBlock()->traverse(this); |
| 2957 | if (result != spv::NoResult) |
| 2958 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 2959 | } |
| 2960 | |
| 2961 | if (node->getFalseBlock() != nullptr) { |
| 2962 | ifBuilder.makeBeginElse(); |
| 2963 | // emit the "else" statement |
| 2964 | node->getFalseBlock()->traverse(this); |
| 2965 | if (result != spv::NoResult) |
| 2966 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 2967 | } |
| 2968 | |
| 2969 | // finish off the control flow |
| 2970 | ifBuilder.makeEndIf(); |
| 2971 | |
| 2972 | if (result != spv::NoResult) { |
| 2973 | builder.clearAccessChain(); |
| 2974 | builder.setAccessChainLValue(result); |
| 2975 | } |
| 2976 | }; |
| 2977 | |
| 2978 | // Try for OpSelect (or a requirement to execute both sides) |
| 2979 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2980 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2981 | if (node->getType().getQualifier().isSpecConstant()) |
| 2982 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2983 | executeBothSides(); |
| 2984 | } else |
| 2985 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2986 | |
| 2987 | return false; |
| 2988 | } |
| 2989 | |
| 2990 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2991 | { |
| 2992 | // emit and get the condition before doing anything with switch |
| 2993 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2994 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2995 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2996 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 2997 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2998 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2999 | // browse the children to sort out code segments |
| 3000 | int defaultSegment = -1; |
| 3001 | std::vector<TIntermNode*> codeSegments; |
| 3002 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 3003 | std::vector<int> caseValues; |
| 3004 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 3005 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 3006 | TIntermNode* child = *c; |
| 3007 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3008 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3009 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3010 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3011 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3012 | } else |
| 3013 | codeSegments.push_back(child); |
| 3014 | } |
| 3015 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3016 | // handle the case where the last code segment is missing, due to no code |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3017 | // statements between the last case and the end of the switch statement |
| 3018 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 3019 | (int)codeSegments.size() == defaultSegment) |
| 3020 | codeSegments.push_back(nullptr); |
| 3021 | |
| 3022 | // make the switch statement |
| 3023 | std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3024 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3025 | |
| 3026 | // emit all the code in the segments |
| 3027 | breakForLoop.push(false); |
| 3028 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 3029 | builder.nextSwitchSegment(segmentBlocks, s); |
| 3030 | if (codeSegments[s]) |
| 3031 | codeSegments[s]->traverse(this); |
| 3032 | else |
| 3033 | builder.addSwitchBreak(); |
| 3034 | } |
| 3035 | breakForLoop.pop(); |
| 3036 | |
| 3037 | builder.endSwitch(segmentBlocks); |
| 3038 | |
| 3039 | return false; |
| 3040 | } |
| 3041 | |
| 3042 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 3043 | { |
| 3044 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 3045 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3046 | |
| 3047 | builder.clearAccessChain(); |
| 3048 | builder.setAccessChainRValue(constant); |
| 3049 | } |
| 3050 | |
| 3051 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 3052 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3053 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3054 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3055 | |
| 3056 | // Loop control: |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3057 | std::vector<unsigned int> operands; |
| 3058 | const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3059 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3060 | // Spec requires back edges to target header blocks, and every header block |
| 3061 | // must dominate its merge block. Make a header block first to ensure these |
| 3062 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 3063 | // by a block-ending branch. But we don't want to put any other body/test |
| 3064 | // instructions in it, since the body/test may have arbitrary instructions, |
| 3065 | // including merges of its own. |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3066 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3067 | builder.setBuildPoint(&blocks.head); |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3068 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3069 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3070 | spv::Block& test = builder.makeNewBlock(); |
| 3071 | builder.createBranch(&test); |
| 3072 | |
| 3073 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3074 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3075 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3076 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 3077 | |
| 3078 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3079 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3080 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3081 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3082 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3083 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3084 | |
| 3085 | builder.setBuildPoint(&blocks.continue_target); |
| 3086 | if (node->getTerminal()) |
| 3087 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3088 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 3089 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3090 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3091 | builder.createBranch(&blocks.body); |
| 3092 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3093 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3094 | builder.setBuildPoint(&blocks.body); |
| 3095 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3096 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3097 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3098 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3099 | |
| 3100 | builder.setBuildPoint(&blocks.continue_target); |
| 3101 | if (node->getTerminal()) |
| 3102 | node->getTerminal()->traverse(this); |
| 3103 | if (node->getTest()) { |
| 3104 | node->getTest()->traverse(this); |
| 3105 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3106 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3107 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3108 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 3109 | // TODO: unless there was a break/return/discard instruction |
| 3110 | // somewhere in the body, this is an infinite loop, so we should |
| 3111 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3112 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3113 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3114 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3115 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3116 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3117 | return false; |
| 3118 | } |
| 3119 | |
| 3120 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 3121 | { |
| 3122 | if (node->getExpression()) |
| 3123 | node->getExpression()->traverse(this); |
| 3124 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3125 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3126 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3127 | switch (node->getFlowOp()) { |
| 3128 | case glslang::EOpKill: |
| 3129 | builder.makeDiscard(); |
| 3130 | break; |
| 3131 | case glslang::EOpBreak: |
| 3132 | if (breakForLoop.top()) |
| 3133 | builder.createLoopExit(); |
| 3134 | else |
| 3135 | builder.addSwitchBreak(); |
| 3136 | break; |
| 3137 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3138 | builder.createLoopContinue(); |
| 3139 | break; |
| 3140 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3141 | if (node->getExpression()) { |
| 3142 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 3143 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 3144 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 3145 | builder.clearAccessChain(); |
| 3146 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 3147 | builder.setAccessChainLValue(copyId); |
| 3148 | multiTypeStore(glslangReturnType, returnId); |
| 3149 | returnId = builder.createLoad(copyId); |
| 3150 | } |
| 3151 | builder.makeReturn(false, returnId); |
| 3152 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 3153 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3154 | |
| 3155 | builder.clearAccessChain(); |
| 3156 | break; |
| 3157 | |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3158 | case glslang::EOpDemote: |
| 3159 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 3160 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 3161 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 3162 | break; |
| 3163 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3164 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3165 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3166 | break; |
| 3167 | } |
| 3168 | |
| 3169 | return false; |
| 3170 | } |
| 3171 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3172 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3173 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3174 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3175 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3176 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 3177 | if (node->getQualifier().isConstant()) { |
Dan Sinclair | 12fcaa2 | 2018-11-13 09:17:44 -0500 | [diff] [blame] | 3178 | spv::Id result = createSpvConstant(*node); |
| 3179 | if (result != spv::NoResult) |
| 3180 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3184 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3185 | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
| 3186 | : forcedType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3187 | |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3188 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 3189 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 3190 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3191 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3192 | switch (storageClass) { |
| 3193 | case spv::StorageClassInput: |
| 3194 | case spv::StorageClassOutput: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3195 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3196 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3197 | break; |
| 3198 | case spv::StorageClassPushConstant: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3199 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3200 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3201 | break; |
| 3202 | case spv::StorageClassUniform: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3203 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3204 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 3205 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3206 | else |
| 3207 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 3208 | break; |
| 3209 | case spv::StorageClassStorageBuffer: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3210 | case spv::StorageClassPhysicalStorageBufferEXT: |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3211 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
| 3212 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 3213 | break; |
| 3214 | default: |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3215 | if (node->getType().containsBasicType(glslang::EbtFloat16)) |
| 3216 | builder.addCapability(spv::CapabilityFloat16); |
| 3217 | if (node->getType().containsBasicType(glslang::EbtInt16) || |
| 3218 | node->getType().containsBasicType(glslang::EbtUint16)) |
| 3219 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3220 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3221 | } |
| 3222 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3223 | |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3224 | const bool contains8BitType = node->getType().containsBasicType(glslang::EbtInt8) || |
| 3225 | node->getType().containsBasicType(glslang::EbtUint8); |
| 3226 | if (contains8BitType) { |
| 3227 | if (storageClass == spv::StorageClassPushConstant) { |
| 3228 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 3229 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 3230 | } else if (storageClass == spv::StorageClassUniform) { |
| 3231 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 3232 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3233 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
| 3234 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 3235 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3236 | } else { |
| 3237 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3241 | const char* name = node->getName().c_str(); |
| 3242 | if (glslang::IsAnonymous(name)) |
| 3243 | name = ""; |
| 3244 | |
| 3245 | return builder.createVariable(storageClass, spvType, name); |
| 3246 | } |
| 3247 | |
| 3248 | // Return type Id of the sampled type. |
| 3249 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 3250 | { |
| 3251 | switch (sampler.type) { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3252 | case glslang::EbtInt: return builder.makeIntType(32); |
| 3253 | case glslang::EbtUint: return builder.makeUintType(32); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3254 | case glslang::EbtFloat: return builder.makeFloatType(32); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3255 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3256 | case glslang::EbtFloat16: |
| 3257 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 3258 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3259 | return builder.makeFloatType(16); |
| 3260 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3261 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3262 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3263 | return builder.makeFloatType(32); |
| 3264 | } |
| 3265 | } |
| 3266 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3267 | // If node is a swizzle operation, return the type that should be used if |
| 3268 | // the swizzle base is first consumed by another operation, before the swizzle |
| 3269 | // is applied. |
| 3270 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 3271 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3272 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3273 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 3274 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 3275 | else |
| 3276 | return spv::NoType; |
| 3277 | } |
| 3278 | |
| 3279 | // When inverting a swizzle with a parent op, this function |
| 3280 | // will apply the swizzle operation to a completed parent operation. |
| 3281 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 3282 | { |
| 3283 | std::vector<unsigned> swizzle; |
| 3284 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 3285 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 3286 | } |
| 3287 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3288 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 3289 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 3290 | { |
| 3291 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 3292 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 3293 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3294 | } |
| 3295 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3296 | // Convert from a glslang type to an SPV type, by calling into a |
| 3297 | // recursive version of this function. This establishes the inherited |
| 3298 | // layout state rooted from the top-level type. |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3299 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3300 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3301 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | // Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id. |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 3305 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3306 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3307 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3308 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 3309 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3310 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3311 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3312 | |
| 3313 | switch (type.getBasicType()) { |
| 3314 | case glslang::EbtVoid: |
| 3315 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3316 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3317 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3318 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3319 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 3320 | // a 32-bit int where non-0 means true. |
| 3321 | if (explicitLayout != glslang::ElpNone) |
| 3322 | spvType = builder.makeUintType(32); |
| 3323 | else |
| 3324 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3325 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3326 | case glslang::EbtInt: |
| 3327 | spvType = builder.makeIntType(32); |
| 3328 | break; |
| 3329 | case glslang::EbtUint: |
| 3330 | spvType = builder.makeUintType(32); |
| 3331 | break; |
| 3332 | case glslang::EbtFloat: |
| 3333 | spvType = builder.makeFloatType(32); |
| 3334 | break; |
| 3335 | #ifndef GLSLANG_WEB |
| 3336 | case glslang::EbtDouble: |
| 3337 | spvType = builder.makeFloatType(64); |
| 3338 | break; |
| 3339 | case glslang::EbtFloat16: |
| 3340 | spvType = builder.makeFloatType(16); |
| 3341 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3342 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3343 | spvType = builder.makeIntType(8); |
| 3344 | break; |
| 3345 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3346 | spvType = builder.makeUintType(8); |
| 3347 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3348 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3349 | spvType = builder.makeIntType(16); |
| 3350 | break; |
| 3351 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3352 | spvType = builder.makeUintType(16); |
| 3353 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3354 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3355 | spvType = builder.makeIntType(64); |
| 3356 | break; |
| 3357 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3358 | spvType = builder.makeUintType(64); |
| 3359 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3360 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 3361 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3362 | spvType = builder.makeUintType(32); |
| 3363 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3364 | case glslang::EbtAccStructNV: |
| 3365 | spvType = builder.makeAccelerationStructureNVType(); |
| 3366 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3367 | case glslang::EbtReference: |
| 3368 | { |
| 3369 | // Make the forward pointer, then recurse to convert the structure type, then |
| 3370 | // patch up the forward pointer with a real pointer type. |
| 3371 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 3372 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 3373 | forwardPointers[type.getReferentType()] = forwardId; |
| 3374 | } |
| 3375 | spvType = forwardPointers[type.getReferentType()]; |
| 3376 | if (!forwardReferenceOnly) { |
| 3377 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 3378 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 3379 | forwardPointers[type.getReferentType()], |
| 3380 | referentType); |
| 3381 | } |
| 3382 | } |
| 3383 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3384 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3385 | case glslang::EbtSampler: |
| 3386 | { |
| 3387 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3388 | if (sampler.isPureSampler()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3389 | spvType = builder.makeSamplerType(); |
| 3390 | } else { |
| 3391 | // an image is present, make its type |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3392 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), |
| 3393 | sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), |
| 3394 | sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type)); |
| 3395 | if (sampler.isCombined()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3396 | // already has both image and sampler, make the combined type |
| 3397 | spvType = builder.makeSampledImageType(spvType); |
| 3398 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3399 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3400 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3401 | break; |
| 3402 | case glslang::EbtStruct: |
| 3403 | case glslang::EbtBlock: |
| 3404 | { |
| 3405 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3406 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3407 | |
| 3408 | // Try to share structs for different layouts, but not yet for other |
| 3409 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3410 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3411 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3412 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3413 | break; |
| 3414 | |
| 3415 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3416 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3417 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 3418 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3419 | } |
| 3420 | break; |
| 3421 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3422 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3423 | break; |
| 3424 | } |
| 3425 | |
| 3426 | if (type.isMatrix()) |
| 3427 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3428 | else { |
| 3429 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3430 | if (type.getVectorSize() > 1) |
| 3431 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3432 | } |
| 3433 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3434 | if (type.isCoopMat()) { |
| 3435 | builder.addCapability(spv::CapabilityCooperativeMatrixNV); |
| 3436 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
| 3437 | if (type.getBasicType() == glslang::EbtFloat16) |
| 3438 | builder.addCapability(spv::CapabilityFloat16); |
| 3439 | |
| 3440 | spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1); |
| 3441 | spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2); |
| 3442 | spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3); |
| 3443 | |
| 3444 | spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols); |
| 3445 | } |
| 3446 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3447 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3448 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3449 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3450 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3451 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3452 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3453 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3454 | // Use a dummy glslang type for querying internal strides of |
| 3455 | // arrays of arrays, but using just a one-dimensional array. |
| 3456 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3457 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3458 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3459 | |
| 3460 | // Will compute the higher-order strides here, rather than making a whole |
| 3461 | // pile of types and doing repetitive recursion on their contents. |
| 3462 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3463 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3464 | |
| 3465 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3466 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3467 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3468 | if (stride > 0) |
| 3469 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3470 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3471 | } |
| 3472 | } else { |
| 3473 | // single-dimensional array, and don't yet have stride |
| 3474 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3475 | // We need to decorate array strides for types needing explicit layout, except blocks. |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3476 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3477 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3478 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3479 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3480 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3481 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3482 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3483 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3484 | else { |
| 3485 | if (!lastBufferBlockMember) { |
| 3486 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 3487 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3488 | } |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3489 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3490 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3491 | if (stride > 0) |
| 3492 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | return spvType; |
| 3496 | } |
| 3497 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3498 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3499 | // |
| 3500 | // Identify interface members that don't have their required extension turned on. |
| 3501 | // |
| 3502 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3503 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3504 | #ifndef GLSLANG_WEB |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3505 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3506 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3507 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3508 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3509 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3510 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3511 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3512 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3513 | |
| 3514 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3515 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3516 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3517 | return true; |
| 3518 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3519 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3520 | return true; |
| 3521 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3522 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3523 | return true; |
| 3524 | } |
| 3525 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3526 | |
| 3527 | return false; |
| 3528 | }; |
| 3529 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3530 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3531 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3532 | // Mutually recursive with convertGlslangToSpvType(). |
| 3533 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3534 | const glslang::TTypeList* glslangMembers, |
| 3535 | glslang::TLayoutPacking explicitLayout, |
| 3536 | const glslang::TQualifier& qualifier) |
| 3537 | { |
| 3538 | // Create a vector of struct types for SPIR-V to consume |
| 3539 | std::vector<spv::Id> spvMembers; |
| 3540 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3541 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3542 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3543 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3544 | if (glslangMember.hiddenMember()) { |
| 3545 | ++memberDelta; |
| 3546 | if (type.getBasicType() == glslang::EbtBlock) |
| 3547 | memberRemapper[glslangMembers][i] = -1; |
| 3548 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3549 | if (type.getBasicType() == glslang::EbtBlock) { |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 3550 | if (filterMember(glslangMember)) { |
| 3551 | memberDelta++; |
| 3552 | memberRemapper[glslangMembers][i] = -1; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3553 | continue; |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 3554 | } |
| 3555 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3556 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3557 | // modify just this child's view of the qualifier |
| 3558 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3559 | InheritQualifiers(memberQualifier, qualifier); |
| 3560 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3561 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3562 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3563 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3564 | |
| 3565 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3566 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3567 | i == (int)glslangMembers->size() - 1; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3568 | |
| 3569 | // Make forward pointers for any pointer members, and create a list of members to |
| 3570 | // convert to spirv types after creating the struct. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 3571 | if (glslangMember.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3572 | if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) { |
| 3573 | deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier)); |
| 3574 | } |
| 3575 | spvMembers.push_back( |
| 3576 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, true)); |
| 3577 | } else { |
| 3578 | spvMembers.push_back( |
| 3579 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, false)); |
| 3580 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3581 | } |
| 3582 | } |
| 3583 | |
| 3584 | // Make the SPIR-V type |
| 3585 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3586 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3587 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3588 | |
| 3589 | // Decorate it |
| 3590 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3591 | |
John Kessenich | d72f488 | 2019-01-16 14:55:37 +0700 | [diff] [blame] | 3592 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3593 | auto it = deferredForwardPointers[i]; |
| 3594 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 3595 | } |
| 3596 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3597 | return spvType; |
| 3598 | } |
| 3599 | |
| 3600 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3601 | const glslang::TTypeList* glslangMembers, |
| 3602 | glslang::TLayoutPacking explicitLayout, |
| 3603 | const glslang::TQualifier& qualifier, |
| 3604 | spv::Id spvType) |
| 3605 | { |
| 3606 | // Name and decorate the non-hidden members |
| 3607 | int offset = -1; |
| 3608 | int locationOffset = 0; // for use within the members of this struct |
| 3609 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3610 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3611 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3612 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3613 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3614 | if (filterMember(glslangMember)) |
| 3615 | continue; |
| 3616 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3617 | |
| 3618 | // modify just this child's view of the qualifier |
| 3619 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3620 | InheritQualifiers(memberQualifier, qualifier); |
| 3621 | |
| 3622 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3623 | if (member < 0) |
| 3624 | continue; |
| 3625 | |
| 3626 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3627 | builder.addMemberDecoration(spvType, member, |
| 3628 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 3629 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 3630 | // Add interpolation and auxiliary storage decorations only to |
| 3631 | // top-level members of Input and Output storage classes |
| 3632 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 3633 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 3634 | if (type.getBasicType() == glslang::EbtBlock || |
| 3635 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 3636 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 3637 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3638 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3639 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 3640 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3641 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3642 | } |
| 3643 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3644 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3645 | if (type.getBasicType() == glslang::EbtBlock && |
| 3646 | qualifier.storage == glslang::EvqBuffer) { |
| 3647 | // Add memory decorations only to top-level members of shader storage block |
| 3648 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3649 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3650 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3651 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 3652 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3653 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3654 | // Location assignment was already completed correctly by the front end, |
| 3655 | // just track whether a member needs to be decorated. |
| 3656 | // Ignore member locations if the container is an array, as that's |
| 3657 | // ill-specified and decisions have been made to not allow this. |
| 3658 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 3659 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3660 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3661 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 3662 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 3663 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 3664 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3665 | // component, XFB, others |
| 3666 | if (glslangMember.getQualifier().hasComponent()) |
| 3667 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 3668 | glslangMember.getQualifier().layoutComponent); |
| 3669 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 3670 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 3671 | glslangMember.getQualifier().layoutXfbOffset); |
| 3672 | else if (explicitLayout != glslang::ElpNone) { |
| 3673 | // figure out what to do with offset, which is accumulating |
| 3674 | int nextOffset; |
| 3675 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 3676 | if (offset >= 0) |
| 3677 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 3678 | offset = nextOffset; |
| 3679 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3680 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3681 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 3682 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 3683 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3684 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3685 | // built-in variable decorations |
| 3686 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 3687 | if (builtIn != spv::BuiltInMax) |
| 3688 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3689 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3690 | // nonuniform |
| 3691 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 3692 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3693 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 3694 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 3695 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 3696 | memberQualifier.semanticName); |
| 3697 | } |
| 3698 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3699 | #ifndef GLSLANG_WEB |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3700 | if (builtIn == spv::BuiltInLayer) { |
| 3701 | // SPV_NV_viewport_array2 extension |
| 3702 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 3703 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 3704 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 3705 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3706 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3707 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 3708 | builder.addMemberDecoration(spvType, member, |
| 3709 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 3710 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 3711 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 3712 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3713 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3714 | } |
| 3715 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 3716 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 3717 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 3718 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 3719 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3720 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3724 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 3725 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3726 | } |
| 3727 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3728 | // Turn the expression forming the array size into an id. |
| 3729 | // This is not quite trivial, because of specialization constants. |
| 3730 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 3731 | // a specialization constant expression is. |
| 3732 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 3733 | { |
| 3734 | // First, see if this is sized with a node, meaning a specialization constant: |
| 3735 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 3736 | if (specNode != nullptr) { |
| 3737 | builder.clearAccessChain(); |
| 3738 | specNode->traverse(this); |
| 3739 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 3740 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3741 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3742 | // Otherwise, need a compile-time (front end) size, get it: |
| 3743 | int size = arraySizes.getDimSize(dim); |
| 3744 | assert(size > 0); |
| 3745 | return builder.makeUintConstant(size); |
| 3746 | } |
| 3747 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3748 | // Wrap the builder's accessChainLoad to: |
| 3749 | // - localize handling of RelaxedPrecision |
| 3750 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 3751 | // (avoids unnecessary work and possible type punning for structures) |
| 3752 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3753 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 3754 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3755 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3756 | |
| 3757 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3758 | coherentFlags |= TranslateCoherent(type); |
| 3759 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3760 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3761 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3762 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3763 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3764 | TranslateNonUniformDecoration(type.getQualifier()), |
| 3765 | nominalTypeId, |
| 3766 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3767 | TranslateMemoryScope(coherentFlags), |
| 3768 | alignment); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3769 | |
| 3770 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3771 | if (type.getBasicType() == glslang::EbtBool) { |
| 3772 | if (builder.isScalarType(nominalTypeId)) { |
| 3773 | // Conversion for bool |
| 3774 | spv::Id boolType = builder.makeBoolType(); |
| 3775 | if (nominalTypeId != boolType) |
| 3776 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 3777 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3778 | // Conversion for bvec |
| 3779 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3780 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 3781 | if (nominalTypeId != bvecType) |
| 3782 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 3783 | } |
| 3784 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3785 | |
| 3786 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3787 | } |
| 3788 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3789 | // Wrap the builder's accessChainStore to: |
| 3790 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3791 | // |
| 3792 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3793 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 3794 | { |
| 3795 | // Need to convert to abstract types when necessary |
| 3796 | if (type.getBasicType() == glslang::EbtBool) { |
| 3797 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 3798 | |
| 3799 | if (builder.isScalarType(nominalTypeId)) { |
| 3800 | // Conversion for bool |
| 3801 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3802 | if (nominalTypeId != boolType) { |
| 3803 | // keep these outside arguments, for determinant order-of-evaluation |
| 3804 | spv::Id one = builder.makeUintConstant(1); |
| 3805 | spv::Id zero = builder.makeUintConstant(0); |
| 3806 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 3807 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3808 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3809 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3810 | // Conversion for bvec |
| 3811 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3812 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3813 | if (nominalTypeId != bvecType) { |
| 3814 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 3815 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 3816 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 3817 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3818 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3819 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 3820 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3821 | } |
| 3822 | } |
| 3823 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3824 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3825 | coherentFlags |= TranslateCoherent(type); |
| 3826 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3827 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3828 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3829 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3830 | builder.accessChainStore(rvalue, |
| 3831 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3832 | TranslateMemoryScope(coherentFlags), alignment); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3833 | } |
| 3834 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3835 | // For storing when types match at the glslang level, but not might match at the |
| 3836 | // SPIR-V level. |
| 3837 | // |
| 3838 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3839 | // SPIR-V types, like a struct that is used in a member-undecorated way as well |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3840 | // as in a member-decorated way. |
| 3841 | // |
| 3842 | // NOTE: This function can handle any store request; if it's not special it |
| 3843 | // simplifies to a simple OpStore. |
| 3844 | // |
| 3845 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 3846 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 3847 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3848 | // we only do the complex path here if it's an aggregate |
| 3849 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3850 | accessChainStore(type, rValue); |
| 3851 | return; |
| 3852 | } |
| 3853 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3854 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3855 | spv::Id rType = builder.getTypeId(rValue); |
| 3856 | spv::Id lValue = builder.accessChainGetLValue(); |
| 3857 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 3858 | if (lType == rType) { |
| 3859 | accessChainStore(type, rValue); |
| 3860 | return; |
| 3861 | } |
| 3862 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3863 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3864 | // where the two types were the same type in GLSL. This requires member |
| 3865 | // by member copy, recursively. |
| 3866 | |
John Kessenich | fbb6bdf | 2019-01-15 21:48:27 +0700 | [diff] [blame] | 3867 | // SPIR-V 1.4 added an instruction to do help do this. |
| 3868 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 3869 | // However, bool in uniform space is changed to int, so |
| 3870 | // OpCopyLogical does not work for that. |
| 3871 | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
| 3872 | bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0); |
| 3873 | bool lBool = builder.containsType(lType, spv::OpTypeBool, 0); |
| 3874 | if (lBool == rBool) { |
| 3875 | spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue); |
| 3876 | accessChainStore(type, logicalCopy); |
| 3877 | return; |
| 3878 | } |
| 3879 | } |
| 3880 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3881 | // If an array, copy element by element. |
| 3882 | if (type.isArray()) { |
| 3883 | glslang::TType glslangElementType(type, 0); |
| 3884 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 3885 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 3886 | // get the source member |
| 3887 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3888 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3889 | // set up the target storage |
| 3890 | builder.clearAccessChain(); |
| 3891 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3892 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), type.getBufferReferenceAlignment()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3893 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3894 | // store the member |
| 3895 | multiTypeStore(glslangElementType, elementRValue); |
| 3896 | } |
| 3897 | } else { |
| 3898 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3899 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3900 | // loop over structure members |
| 3901 | const glslang::TTypeList& members = *type.getStruct(); |
| 3902 | for (int m = 0; m < (int)members.size(); ++m) { |
| 3903 | const glslang::TType& glslangMemberType = *members[m].type; |
| 3904 | |
| 3905 | // get the source member |
| 3906 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 3907 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 3908 | |
| 3909 | // set up the target storage |
| 3910 | builder.clearAccessChain(); |
| 3911 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3912 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), type.getBufferReferenceAlignment()); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3913 | |
| 3914 | // store the member |
| 3915 | multiTypeStore(glslangMemberType, memberRValue); |
| 3916 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3917 | } |
| 3918 | } |
| 3919 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3920 | // Decide whether or not this type should be |
| 3921 | // decorated with offsets and strides, and if so |
| 3922 | // whether std140 or std430 rules should be applied. |
| 3923 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3924 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3925 | // has to be a block |
| 3926 | if (type.getBasicType() != glslang::EbtBlock) |
| 3927 | return glslang::ElpNone; |
| 3928 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3929 | // has to be a uniform or buffer block or task in/out blocks |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3930 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3931 | type.getQualifier().storage != glslang::EvqBuffer && |
| 3932 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3933 | return glslang::ElpNone; |
| 3934 | |
| 3935 | // return the layout to use |
| 3936 | switch (type.getQualifier().layoutPacking) { |
| 3937 | case glslang::ElpStd140: |
| 3938 | case glslang::ElpStd430: |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3939 | case glslang::ElpScalar: |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3940 | return type.getQualifier().layoutPacking; |
| 3941 | default: |
| 3942 | return glslang::ElpNone; |
| 3943 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3944 | } |
| 3945 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3946 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3947 | int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3948 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3949 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3950 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3951 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 3952 | |
| 3953 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3954 | } |
| 3955 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3956 | // Given a matrix type, or array (of array) of matrixes type, returns the integer stride required for that matrix |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3957 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3958 | int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3959 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3960 | glslang::TType elementType; |
| 3961 | elementType.shallowCopy(matrixType); |
| 3962 | elementType.clearArraySizes(); |
| 3963 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3964 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3965 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3966 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3967 | |
| 3968 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3969 | } |
| 3970 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3971 | // Given a member type of a struct, realign the current offset for it, and compute |
| 3972 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 3973 | // on the next call. |
| 3974 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 3975 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 3976 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3977 | void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, int& nextOffset, |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3978 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3979 | { |
| 3980 | // this will get a positive value when deemed necessary |
| 3981 | nextOffset = -1; |
| 3982 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3983 | // override anything in currentOffset with user-set offset |
| 3984 | if (memberType.getQualifier().hasOffset()) |
| 3985 | currentOffset = memberType.getQualifier().layoutOffset; |
| 3986 | |
| 3987 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 3988 | // in which case the following code does not matter. But, that's not quite right |
| 3989 | // once cross-compilation unit GLSL validation is done, as the original user |
| 3990 | // settings are needed in layoutOffset, and then the following will come into play. |
| 3991 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3992 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3993 | if (! memberType.getQualifier().hasOffset()) |
| 3994 | currentOffset = -1; |
| 3995 | |
| 3996 | return; |
| 3997 | } |
| 3998 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3999 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4000 | if (currentOffset < 0) |
| 4001 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4002 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4003 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 4004 | // but possibly not yet correctly aligned. |
| 4005 | |
| 4006 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4007 | int dummyStride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4008 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4009 | |
| 4010 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4011 | // TODO: make this consistent in early phases of code: |
| 4012 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 4013 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 4014 | // which is the most likely to rely on reflection, and least likely to rely implicit layouts |
John Kessenich | e7df8e0 | 2018-08-22 17:12:46 -0600 | [diff] [blame] | 4015 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4016 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4017 | int dummySize; |
| 4018 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 4019 | if (componentAlignment <= 4) |
| 4020 | memberAlignment = componentAlignment; |
| 4021 | } |
| 4022 | |
| 4023 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4024 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4025 | |
| 4026 | // Bump up to vec4 if there is a bad straddle |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4027 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4028 | glslang::RoundToPow2(currentOffset, 16); |
| 4029 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4030 | nextOffset = currentOffset + memberSize; |
| 4031 | } |
| 4032 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4033 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4034 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4035 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 4036 | switch (glslangBuiltIn) |
| 4037 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4038 | case glslang::EbvPointSize: |
| 4039 | #ifndef GLSLANG_WEB |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4040 | case glslang::EbvClipDistance: |
| 4041 | case glslang::EbvCullDistance: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4042 | case glslang::EbvViewportMaskNV: |
| 4043 | case glslang::EbvSecondaryPositionNV: |
| 4044 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 4045 | case glslang::EbvPositionPerViewNV: |
| 4046 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4047 | case glslang::EbvTaskCountNV: |
| 4048 | case glslang::EbvPrimitiveCountNV: |
| 4049 | case glslang::EbvPrimitiveIndicesNV: |
| 4050 | case glslang::EbvClipDistancePerViewNV: |
| 4051 | case glslang::EbvCullDistancePerViewNV: |
| 4052 | case glslang::EbvLayerPerViewNV: |
| 4053 | case glslang::EbvMeshViewCountNV: |
| 4054 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4055 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4056 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 4057 | // Alternately, we could just call this for any glslang built-in, since the |
| 4058 | // capability already guards against duplicates. |
| 4059 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 4060 | break; |
| 4061 | default: |
| 4062 | // Capabilities were already generated when the struct was declared. |
| 4063 | break; |
| 4064 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4065 | } |
| 4066 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4067 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4068 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 4069 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4070 | } |
| 4071 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4072 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4073 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 4074 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4075 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4076 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4077 | assert(qualifier == glslang::EvqIn || |
| 4078 | qualifier == glslang::EvqOut || |
| 4079 | qualifier == glslang::EvqInOut || |
| 4080 | qualifier == glslang::EvqConstReadOnly); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4081 | return qualifier != glslang::EvqConstReadOnly; |
| 4082 | } |
| 4083 | |
| 4084 | // Is parameter pass-by-original? |
| 4085 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 4086 | bool implicitThisParam) |
| 4087 | { |
| 4088 | if (implicitThisParam) // implicit this |
| 4089 | return true; |
| 4090 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4091 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4092 | return paramType.containsOpaque() || // sampler, etc. |
| 4093 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 4094 | } |
| 4095 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4096 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 4097 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4098 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4099 | const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, bool useVulkanMemoryModel) { |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4100 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 4101 | if (paramPrecision != spv::NoPrecision) |
| 4102 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4103 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 4104 | if (type.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4105 | // Original and non-writable params pass the pointer directly and |
| 4106 | // use restrict/aliased, others are stored to a pointer in Function |
| 4107 | // memory and use RestrictPointer/AliasedPointer. |
| 4108 | if (originalParam(type.getQualifier().storage, type, false) || |
| 4109 | !writableParam(type.getQualifier().storage)) { |
| 4110 | decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrict : spv::DecorationAliased); |
| 4111 | } else { |
| 4112 | decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 4113 | } |
| 4114 | } |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4115 | }; |
| 4116 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4117 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4118 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4119 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4120 | continue; |
| 4121 | |
| 4122 | // We're on a user function. Set up the basic interface for the function now, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4123 | // so that it's available to call. Translating the body will happen later. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4124 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4125 | // Typically (except for a "const in" parameter), an address will be passed to the |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4126 | // function. What it is an address of varies: |
| 4127 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4128 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 4129 | // argument so that write needs to be to a copy, hence the address of a copy works. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4130 | // |
| 4131 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 4132 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4133 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 4134 | // GLSL has copy-in/copy-out semantics. They can be handled though with a pointer to a copy. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4135 | |
| 4136 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4137 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4138 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 4139 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4140 | #ifdef ENABLE_HLSL |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4141 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 4142 | glslangIntermediate->implicitThisName; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4143 | #else |
| 4144 | bool implicitThis = false; |
| 4145 | #endif |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4146 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4147 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4148 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4149 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4150 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4151 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 4152 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4153 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4154 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 4155 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4156 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4157 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4158 | paramTypes.push_back(typeId); |
| 4159 | } |
| 4160 | |
| 4161 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4162 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 4163 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4164 | glslFunction->getName().c_str(), paramTypes, |
| 4165 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4166 | if (implicitThis) |
| 4167 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4168 | |
| 4169 | // Track function to emit/call later |
| 4170 | functionMap[glslFunction->getName().c_str()] = function; |
| 4171 | |
| 4172 | // Set the parameter id's |
| 4173 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4174 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 4175 | // give a name too |
| 4176 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4177 | |
| 4178 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4179 | if (paramType.containsBasicType(glslang::EbtInt8) || |
| 4180 | paramType.containsBasicType(glslang::EbtUint8)) |
| 4181 | builder.addCapability(spv::CapabilityInt8); |
| 4182 | if (paramType.containsBasicType(glslang::EbtInt16) || |
| 4183 | paramType.containsBasicType(glslang::EbtUint16)) |
| 4184 | builder.addCapability(spv::CapabilityInt16); |
| 4185 | if (paramType.containsBasicType(glslang::EbtFloat16)) |
| 4186 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4187 | } |
| 4188 | } |
| 4189 | } |
| 4190 | |
| 4191 | // Process all the initializers, while skipping the functions and link objects |
| 4192 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 4193 | { |
| 4194 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 4195 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 4196 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 4197 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 4198 | |
| 4199 | // We're on a top-level node that's not a function. Treat as an initializer, whose |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4200 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4201 | initializer->traverse(this); |
| 4202 | } |
| 4203 | } |
| 4204 | } |
| 4205 | |
| 4206 | // Process all the functions, while skipping initializers. |
| 4207 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4208 | { |
| 4209 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4210 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 4211 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4212 | node->traverse(this); |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 4217 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4218 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4219 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4220 | currentFunction = functionMap[node->getName().c_str()]; |
| 4221 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4222 | builder.setBuildPoint(functionBlock); |
| 4223 | } |
| 4224 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4225 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4226 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4227 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4228 | |
| 4229 | glslang::TSampler sampler = {}; |
| 4230 | bool cubeCompare = false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4231 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4232 | bool f16ShadowCompare = false; |
| 4233 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4234 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4235 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 4236 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4237 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4238 | f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 4239 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4240 | } |
| 4241 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4242 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 4243 | builder.clearAccessChain(); |
| 4244 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4245 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4246 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4247 | // Special case l-value operands |
| 4248 | bool lvalue = false; |
| 4249 | switch (node.getOp()) { |
| 4250 | case glslang::EOpImageAtomicAdd: |
| 4251 | case glslang::EOpImageAtomicMin: |
| 4252 | case glslang::EOpImageAtomicMax: |
| 4253 | case glslang::EOpImageAtomicAnd: |
| 4254 | case glslang::EOpImageAtomicOr: |
| 4255 | case glslang::EOpImageAtomicXor: |
| 4256 | case glslang::EOpImageAtomicExchange: |
| 4257 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4258 | case glslang::EOpImageAtomicLoad: |
| 4259 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4260 | if (i == 0) |
| 4261 | lvalue = true; |
| 4262 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4263 | case glslang::EOpSparseImageLoad: |
| 4264 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 4265 | lvalue = true; |
| 4266 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4267 | case glslang::EOpSparseTexture: |
| 4268 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 4269 | lvalue = true; |
| 4270 | break; |
| 4271 | case glslang::EOpSparseTextureClamp: |
| 4272 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 4273 | lvalue = true; |
| 4274 | break; |
| 4275 | case glslang::EOpSparseTextureLod: |
| 4276 | case glslang::EOpSparseTextureOffset: |
| 4277 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 4278 | lvalue = true; |
| 4279 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4280 | case glslang::EOpSparseTextureFetch: |
| 4281 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 4282 | lvalue = true; |
| 4283 | break; |
| 4284 | case glslang::EOpSparseTextureFetchOffset: |
| 4285 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 4286 | lvalue = true; |
| 4287 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4288 | case glslang::EOpSparseTextureLodOffset: |
| 4289 | case glslang::EOpSparseTextureGrad: |
| 4290 | case glslang::EOpSparseTextureOffsetClamp: |
| 4291 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 4292 | lvalue = true; |
| 4293 | break; |
| 4294 | case glslang::EOpSparseTextureGradOffset: |
| 4295 | case glslang::EOpSparseTextureGradClamp: |
| 4296 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 4297 | lvalue = true; |
| 4298 | break; |
| 4299 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 4300 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 4301 | lvalue = true; |
| 4302 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4303 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4304 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 4305 | lvalue = true; |
| 4306 | break; |
| 4307 | case glslang::EOpSparseTextureGatherOffset: |
| 4308 | case glslang::EOpSparseTextureGatherOffsets: |
| 4309 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 4310 | lvalue = true; |
| 4311 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4312 | case glslang::EOpSparseTextureGatherLod: |
| 4313 | if (i == 3) |
| 4314 | lvalue = true; |
| 4315 | break; |
| 4316 | case glslang::EOpSparseTextureGatherLodOffset: |
| 4317 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 4318 | if (i == 4) |
| 4319 | lvalue = true; |
| 4320 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4321 | case glslang::EOpSparseImageLoadLod: |
| 4322 | if (i == 3) |
| 4323 | lvalue = true; |
| 4324 | break; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4325 | case glslang::EOpImageSampleFootprintNV: |
| 4326 | if (i == 4) |
| 4327 | lvalue = true; |
| 4328 | break; |
| 4329 | case glslang::EOpImageSampleFootprintClampNV: |
| 4330 | case glslang::EOpImageSampleFootprintLodNV: |
| 4331 | if (i == 5) |
| 4332 | lvalue = true; |
| 4333 | break; |
| 4334 | case glslang::EOpImageSampleFootprintGradNV: |
| 4335 | if (i == 6) |
| 4336 | lvalue = true; |
| 4337 | break; |
| 4338 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 4339 | if (i == 7) |
| 4340 | lvalue = true; |
| 4341 | break; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4342 | default: |
| 4343 | break; |
| 4344 | } |
| 4345 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4346 | if (lvalue) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4347 | arguments.push_back(builder.accessChainGetLValue()); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4348 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 4349 | lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
| 4350 | } else |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4351 | #endif |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4352 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4353 | } |
| 4354 | } |
| 4355 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4356 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4357 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4358 | builder.clearAccessChain(); |
| 4359 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4360 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4361 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4362 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4363 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 4364 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4365 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4366 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4367 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 4368 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4369 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4370 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4371 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4372 | const glslang::TType &imageType = node->getAsAggregate() |
| 4373 | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 4374 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4375 | const glslang::TSampler sampler = imageType.getSampler(); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4376 | #ifdef GLSLANG_WEB |
| 4377 | const bool f16ShadowCompare = false; |
| 4378 | #else |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4379 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4380 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 4381 | : false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4382 | #endif |
| 4383 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4384 | const auto signExtensionMask = [&]() { |
| 4385 | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
| 4386 | if (sampler.type == glslang::EbtUint) |
| 4387 | return spv::ImageOperandsZeroExtendMask; |
| 4388 | else if (sampler.type == glslang::EbtInt) |
| 4389 | return spv::ImageOperandsSignExtendMask; |
| 4390 | } |
| 4391 | return spv::ImageOperandsMaskNone; |
| 4392 | }; |
| 4393 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4394 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 4395 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4396 | std::vector<spv::Id> arguments; |
| 4397 | if (node->getAsAggregate()) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4398 | translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4399 | else |
| 4400 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 4401 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4402 | |
| 4403 | spv::Builder::TextureParameters params = { }; |
| 4404 | params.sampler = arguments[0]; |
| 4405 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4406 | glslang::TCrackedTextureOp cracked; |
| 4407 | node->crackTexture(sampler, cracked); |
| 4408 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4409 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4410 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4411 | // Check for queries |
| 4412 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4413 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 4414 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 4415 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4416 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4417 | switch (node->getOp()) { |
| 4418 | case glslang::EOpImageQuerySize: |
| 4419 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4420 | if (arguments.size() > 1) { |
| 4421 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4422 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4423 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4424 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4425 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4426 | case glslang::EOpImageQuerySamples: |
| 4427 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4428 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4429 | case glslang::EOpTextureQueryLod: |
| 4430 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4431 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4432 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4433 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4434 | case glslang::EOpSparseTexelsResident: |
| 4435 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4436 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4437 | default: |
| 4438 | assert(0); |
| 4439 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4440 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4441 | } |
| 4442 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4443 | int components = node->getType().getVectorSize(); |
| 4444 | |
| 4445 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 4446 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 4447 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 4448 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 4449 | // here around e.g. which ones return scalars or other types. |
| 4450 | components = 4; |
| 4451 | } |
| 4452 | |
| 4453 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 4454 | |
| 4455 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 4456 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4457 | // Check for image functions other than queries |
| 4458 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4459 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4460 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4461 | spv::IdImmediate image = { true, *(opIt++) }; |
| 4462 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4463 | |
| 4464 | // Handle subpass operations |
| 4465 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 4466 | // built-in function. |
| 4467 | if (cracked.subpass) { |
| 4468 | // add on the (0,0) coordinate |
| 4469 | spv::Id zero = builder.makeIntConstant(0); |
| 4470 | std::vector<spv::Id> comps; |
| 4471 | comps.push_back(zero); |
| 4472 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4473 | spv::IdImmediate coord = { true, |
| 4474 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 4475 | operands.push_back(coord); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4476 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
| 4477 | imageOperands.word = imageOperands.word | signExtensionMask(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4478 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4479 | imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
| 4480 | } |
| 4481 | if (imageOperands.word != spv::ImageOperandsMaskNone) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4482 | operands.push_back(imageOperands); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4483 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4484 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 4485 | operands.push_back(imageOperand); |
| 4486 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4487 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4488 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 4489 | builder.setPrecision(result, precision); |
| 4490 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4491 | } |
| 4492 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4493 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 4494 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4495 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4496 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4497 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4498 | mask = mask | spv::ImageOperandsSampleMask; |
| 4499 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4500 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4501 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4502 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4503 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4504 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4505 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4506 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4507 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4508 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4509 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4510 | operands.push_back(imageOperands); |
| 4511 | } |
| 4512 | if (mask & spv::ImageOperandsSampleMask) { |
| 4513 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4514 | operands.push_back(imageOperand); |
| 4515 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4516 | if (mask & spv::ImageOperandsLodMask) { |
| 4517 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4518 | operands.push_back(imageOperand); |
| 4519 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4520 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4521 | spv::IdImmediate imageOperand = { true, |
| 4522 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4523 | operands.push_back(imageOperand); |
| 4524 | } |
| 4525 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4526 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4527 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4528 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4529 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4530 | builder.setPrecision(result[0], precision); |
| 4531 | |
| 4532 | // If needed, add a conversion constructor to the proper size. |
| 4533 | if (components != node->getType().getVectorSize()) |
| 4534 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4535 | |
| 4536 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4537 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4538 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4539 | // Push the texel value before the operands |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4540 | if (sampler.isMultiSample() || cracked.lod) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4541 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4542 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4543 | } else { |
| 4544 | spv::IdImmediate texel = { true, *opIt }; |
| 4545 | operands.push_back(texel); |
| 4546 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4547 | |
| 4548 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4549 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4550 | mask = mask | spv::ImageOperandsSampleMask; |
| 4551 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4552 | if (cracked.lod) { |
| 4553 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4554 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4555 | mask = mask | spv::ImageOperandsLodMask; |
| 4556 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4557 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4558 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4559 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4560 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4561 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4562 | operands.push_back(imageOperands); |
| 4563 | } |
| 4564 | if (mask & spv::ImageOperandsSampleMask) { |
| 4565 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4566 | operands.push_back(imageOperand); |
| 4567 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4568 | if (mask & spv::ImageOperandsLodMask) { |
| 4569 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4570 | operands.push_back(imageOperand); |
| 4571 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4572 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4573 | spv::IdImmediate imageOperand = { true, |
| 4574 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4575 | operands.push_back(imageOperand); |
| 4576 | } |
| 4577 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4578 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4579 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4580 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4581 | return spv::NoResult; |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4582 | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
| 4583 | node->getOp() == glslang::EOpSparseImageLoadLod) { |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4584 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4585 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4586 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4587 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4588 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4589 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4590 | mask = mask | spv::ImageOperandsSampleMask; |
| 4591 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4592 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4593 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4594 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4595 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4596 | mask = mask | spv::ImageOperandsLodMask; |
| 4597 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4598 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4599 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4600 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4601 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4602 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4603 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4604 | } |
| 4605 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4606 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4607 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4608 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4609 | if (mask & spv::ImageOperandsLodMask) { |
| 4610 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4611 | operands.push_back(imageOperand); |
| 4612 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4613 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4614 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4615 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | // Create the return type that was a special structure |
| 4619 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4620 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4621 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 4622 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 4623 | |
| 4624 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 4625 | |
| 4626 | // Decode the return type |
| 4627 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 4628 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 4629 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 4630 | // Process image atomic operations |
| 4631 | |
| 4632 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 4633 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4634 | // For non-MS, the sample value should be 0 |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4635 | spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4636 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4637 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4638 | spv::Id resultTypeId; |
| 4639 | // imageAtomicStore has a void return type so base the pointer type on |
| 4640 | // the type of the value operand. |
| 4641 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 4642 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word)); |
| 4643 | } else { |
| 4644 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 4645 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4646 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4647 | |
| 4648 | std::vector<spv::Id> operands; |
| 4649 | operands.push_back(pointer); |
| 4650 | for (; opIt != arguments.end(); ++opIt) |
| 4651 | operands.push_back(*opIt); |
| 4652 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4653 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), lvalueCoherentFlags); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4654 | } |
| 4655 | } |
| 4656 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4657 | #ifndef GLSLANG_WEB |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4658 | // Check for fragment mask functions other than queries |
| 4659 | if (cracked.fragMask) { |
| 4660 | assert(sampler.ms); |
| 4661 | |
| 4662 | auto opIt = arguments.begin(); |
| 4663 | std::vector<spv::Id> operands; |
| 4664 | |
| 4665 | // Extract the image if necessary |
| 4666 | if (builder.isSampledImage(params.sampler)) |
| 4667 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4668 | |
| 4669 | operands.push_back(params.sampler); |
| 4670 | ++opIt; |
| 4671 | |
| 4672 | if (sampler.isSubpass()) { |
| 4673 | // add on the (0,0) coordinate |
| 4674 | spv::Id zero = builder.makeIntConstant(0); |
| 4675 | std::vector<spv::Id> comps; |
| 4676 | comps.push_back(zero); |
| 4677 | comps.push_back(zero); |
| 4678 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 4679 | } |
| 4680 | |
| 4681 | for (; opIt != arguments.end(); ++opIt) |
| 4682 | operands.push_back(*opIt); |
| 4683 | |
| 4684 | spv::Op fragMaskOp = spv::OpNop; |
| 4685 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 4686 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 4687 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 4688 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 4689 | |
| 4690 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 4691 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 4692 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 4693 | } |
| 4694 | #endif |
| 4695 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4696 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4697 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4698 | bool imageFootprint = node->isImageFootprint(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4699 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4700 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4701 | // check for bias argument |
| 4702 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4703 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4704 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4705 | if (cracked.gather) |
| 4706 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4707 | |
| 4708 | if (f16ShadowCompare) |
| 4709 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4710 | if (cracked.offset) |
| 4711 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4712 | else if (cracked.offsets) |
| 4713 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4714 | if (cracked.grad) |
| 4715 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4716 | if (cracked.lodClamp) |
| 4717 | ++nonBiasArgCount; |
| 4718 | if (sparse) |
| 4719 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4720 | if (imageFootprint) |
| 4721 | //Following three extra arguments |
| 4722 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4723 | nonBiasArgCount += 3; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4724 | if ((int)arguments.size() > nonBiasArgCount) |
| 4725 | bias = true; |
| 4726 | } |
| 4727 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 4728 | // See if the sampler param should really be just the SPV image part |
| 4729 | if (cracked.fetch) { |
| 4730 | // a fetch needs to have the image extracted first |
| 4731 | if (builder.isSampledImage(params.sampler)) |
| 4732 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4733 | } |
| 4734 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4735 | #ifndef GLSLANG_WEB |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4736 | if (cracked.gather) { |
| 4737 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 4738 | if (bias || cracked.lod || |
| 4739 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 4740 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 4741 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4742 | } |
| 4743 | } |
| 4744 | #endif |
| 4745 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4746 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4747 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4748 | params.coords = arguments[1]; |
| 4749 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4750 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4751 | |
| 4752 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4753 | if (cubeCompare || f16ShadowCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4754 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4755 | ++extraArgs; |
| 4756 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4757 | params.Dref = arguments[2]; |
| 4758 | ++extraArgs; |
| 4759 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4760 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4761 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4762 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4763 | dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref" |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4764 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4765 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 4766 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4767 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 4768 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4769 | |
| 4770 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4771 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4772 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4773 | ++extraArgs; |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 4774 | } else if (glslangIntermediate->getStage() != EShLangFragment |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4775 | #ifndef GLSLANG_WEB |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 4776 | // NV_compute_shader_derivatives layout qualifiers allow for implicit LODs |
| 4777 | && !(glslangIntermediate->getStage() == EShLangCompute && |
| 4778 | (glslangIntermediate->getLayoutDerivativeModeNone() != glslang::LayoutDerivativeNone)) |
| 4779 | #endif |
| 4780 | ) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4781 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 4782 | noImplicitLod = true; |
| 4783 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4784 | |
| 4785 | // multisample |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4786 | if (sampler.isMultiSample()) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4787 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4788 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4789 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4790 | |
| 4791 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4792 | if (cracked.grad) { |
| 4793 | params.gradX = arguments[2 + extraArgs]; |
| 4794 | params.gradY = arguments[3 + extraArgs]; |
| 4795 | extraArgs += 2; |
| 4796 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4797 | |
| 4798 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4799 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4800 | params.offset = arguments[2 + extraArgs]; |
| 4801 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4802 | } else if (cracked.offsets) { |
| 4803 | params.offsets = arguments[2 + extraArgs]; |
| 4804 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4805 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4806 | |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4807 | #ifndef GLSLANG_WEB |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4808 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4809 | if (cracked.lodClamp) { |
| 4810 | params.lodClamp = arguments[2 + extraArgs]; |
| 4811 | ++extraArgs; |
| 4812 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4813 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4814 | if (sparse) { |
| 4815 | params.texelOut = arguments[2 + extraArgs]; |
| 4816 | ++extraArgs; |
| 4817 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4818 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4819 | if (cracked.gather && ! sampler.shadow) { |
| 4820 | // default component is 0, if missing, otherwise an argument |
| 4821 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4822 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4823 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4824 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4825 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4826 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4827 | spv::Id resultStruct = spv::NoResult; |
| 4828 | if (imageFootprint) { |
| 4829 | //Following three extra arguments |
| 4830 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4831 | params.granularity = arguments[2 + extraArgs]; |
| 4832 | params.coarse = arguments[3 + extraArgs]; |
| 4833 | resultStruct = arguments[4 + extraArgs]; |
| 4834 | extraArgs += 3; |
| 4835 | } |
| 4836 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4837 | // bias |
| 4838 | if (bias) { |
| 4839 | params.bias = arguments[2 + extraArgs]; |
| 4840 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4841 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4842 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4843 | #ifndef GLSLANG_WEB |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4844 | if (imageFootprint) { |
| 4845 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 4846 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 4847 | |
| 4848 | |
| 4849 | //resultStructType(OpenGL type) contains 5 elements: |
| 4850 | //struct gl_TextureFootprint2DNV { |
| 4851 | // uvec2 anchor; |
| 4852 | // uvec2 offset; |
| 4853 | // uvec2 mask; |
| 4854 | // uint lod; |
| 4855 | // uint granularity; |
| 4856 | //}; |
| 4857 | //or |
| 4858 | //struct gl_TextureFootprint3DNV { |
| 4859 | // uvec3 anchor; |
| 4860 | // uvec3 offset; |
| 4861 | // uvec2 mask; |
| 4862 | // uint lod; |
| 4863 | // uint granularity; |
| 4864 | //}; |
| 4865 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 4866 | assert(builder.isStructType(resultStructType)); |
| 4867 | |
| 4868 | //resType (SPIR-V type) contains 6 elements: |
| 4869 | //Member 0 must be a Boolean type scalar(LOD), |
| 4870 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 4871 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 4872 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 4873 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 4874 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 4875 | std::vector<spv::Id> members; |
| 4876 | members.push_back(resultType()); |
| 4877 | for (int i = 0; i < 5; i++) { |
| 4878 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 4879 | } |
| 4880 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 4881 | |
| 4882 | //call ImageFootprintNV |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4883 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
| 4884 | cracked.gather, noImplicitLod, params, signExtensionMask()); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4885 | |
| 4886 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 4887 | for (int i = 0; i < 5; i++) { |
| 4888 | builder.clearAccessChain(); |
| 4889 | builder.setAccessChainLValue(resultStruct); |
| 4890 | |
| 4891 | //Accessing to a struct we created, no coherent flag is set |
| 4892 | spv::Builder::AccessChain::CoherentFlags flags; |
| 4893 | flags.clear(); |
| 4894 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4895 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4896 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1)); |
| 4897 | } |
| 4898 | return builder.createCompositeExtract(res, resultType(), 0); |
| 4899 | } |
| 4900 | #endif |
| 4901 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4902 | // projective component (might not to move) |
| 4903 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 4904 | // are divided by the last component of P." |
| 4905 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 4906 | // unused components will appear after all used components." |
| 4907 | if (cracked.proj) { |
| 4908 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 4909 | int projTargetComp; |
| 4910 | switch (sampler.dim) { |
| 4911 | case glslang::Esd1D: projTargetComp = 1; break; |
| 4912 | case glslang::Esd2D: projTargetComp = 2; break; |
| 4913 | case glslang::EsdRect: projTargetComp = 2; break; |
| 4914 | default: projTargetComp = projSourceComp; break; |
| 4915 | } |
| 4916 | // copy the projective coordinate if we have to |
| 4917 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 4918 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4919 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 4920 | projSourceComp); |
| 4921 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 4922 | builder.getTypeId(params.coords), projTargetComp); |
| 4923 | } |
| 4924 | } |
| 4925 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4926 | // nonprivate |
| 4927 | if (imageType.getQualifier().nonprivate) { |
| 4928 | params.nonprivate = true; |
| 4929 | } |
| 4930 | |
| 4931 | // volatile |
| 4932 | if (imageType.getQualifier().volatil) { |
| 4933 | params.volatil = true; |
| 4934 | } |
| 4935 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4936 | std::vector<spv::Id> result( 1, |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4937 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
| 4938 | noImplicitLod, params, signExtensionMask()) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4939 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4940 | |
| 4941 | if (components != node->getType().getVectorSize()) |
| 4942 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4943 | |
| 4944 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 4948 | { |
| 4949 | // Grab the function's pointer from the previously created function |
| 4950 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 4951 | if (! function) |
| 4952 | return 0; |
| 4953 | |
| 4954 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 4955 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 4956 | |
| 4957 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 4958 | // |
| 4959 | // These imply we need a four step process: |
| 4960 | // 1. Evaluate the arguments |
| 4961 | // 2. Allocate and make copies of in, out, and inout arguments |
| 4962 | // 3. Make the call |
| 4963 | // 4. Copy back the results |
| 4964 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4965 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4966 | std::vector<spv::Builder::AccessChain> lValues; |
| 4967 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4968 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4969 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4970 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4971 | // build l-value |
| 4972 | builder.clearAccessChain(); |
| 4973 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4974 | // keep outputs and pass-by-originals as l-values, evaluate others as r-values |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4975 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4976 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4977 | // save l-value |
| 4978 | lValues.push_back(builder.getAccessChain()); |
| 4979 | } else { |
| 4980 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4981 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 4986 | // copy the original into that space. |
| 4987 | // |
| 4988 | // Also, build up the list of actual arguments to pass in for the call |
| 4989 | int lValueCount = 0; |
| 4990 | int rValueCount = 0; |
| 4991 | std::vector<spv::Id> spvArgs; |
| 4992 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 4993 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4994 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 4995 | builder.setAccessChain(lValues[lValueCount]); |
| 4996 | arg = builder.accessChainGetLValue(); |
| 4997 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4998 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4999 | // need space to hold the copy |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5000 | arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5001 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 5002 | // need to copy the input into output space |
| 5003 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5004 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 5005 | builder.clearAccessChain(); |
| 5006 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5007 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5008 | } |
| 5009 | ++lValueCount; |
| 5010 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5011 | // process r-value, which involves a copy for a type mismatch |
| 5012 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) { |
| 5013 | spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg"); |
| 5014 | builder.clearAccessChain(); |
| 5015 | builder.setAccessChainLValue(argCopy); |
| 5016 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 5017 | arg = builder.createLoad(argCopy); |
| 5018 | } else |
| 5019 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5020 | ++rValueCount; |
| 5021 | } |
| 5022 | spvArgs.push_back(arg); |
| 5023 | } |
| 5024 | |
| 5025 | // 3. Make the call. |
| 5026 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5027 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5028 | |
| 5029 | // 4. Copy back out an "out" arguments. |
| 5030 | lValueCount = 0; |
| 5031 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5032 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5033 | ++lValueCount; |
| 5034 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5035 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 5036 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 5037 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5038 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5039 | } |
| 5040 | ++lValueCount; |
| 5041 | } |
| 5042 | } |
| 5043 | |
| 5044 | return result; |
| 5045 | } |
| 5046 | |
| 5047 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5048 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5049 | spv::Id typeId, spv::Id left, spv::Id right, |
| 5050 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 5051 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5052 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5053 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5054 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5055 | |
| 5056 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5057 | bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector? |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5058 | bool comparison = false; |
| 5059 | |
| 5060 | switch (op) { |
| 5061 | case glslang::EOpAdd: |
| 5062 | case glslang::EOpAddAssign: |
| 5063 | if (isFloat) |
| 5064 | binOp = spv::OpFAdd; |
| 5065 | else |
| 5066 | binOp = spv::OpIAdd; |
| 5067 | break; |
| 5068 | case glslang::EOpSub: |
| 5069 | case glslang::EOpSubAssign: |
| 5070 | if (isFloat) |
| 5071 | binOp = spv::OpFSub; |
| 5072 | else |
| 5073 | binOp = spv::OpISub; |
| 5074 | break; |
| 5075 | case glslang::EOpMul: |
| 5076 | case glslang::EOpMulAssign: |
| 5077 | if (isFloat) |
| 5078 | binOp = spv::OpFMul; |
| 5079 | else |
| 5080 | binOp = spv::OpIMul; |
| 5081 | break; |
| 5082 | case glslang::EOpVectorTimesScalar: |
| 5083 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5084 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5085 | if (builder.isVector(right)) |
| 5086 | std::swap(left, right); |
| 5087 | assert(builder.isScalar(right)); |
| 5088 | needMatchingVectors = false; |
| 5089 | binOp = spv::OpVectorTimesScalar; |
t.jung | 697fdf0 | 2018-11-14 13:04:39 +0100 | [diff] [blame] | 5090 | } else if (isFloat) |
| 5091 | binOp = spv::OpFMul; |
| 5092 | else |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5093 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5094 | break; |
| 5095 | case glslang::EOpVectorTimesMatrix: |
| 5096 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5097 | binOp = spv::OpVectorTimesMatrix; |
| 5098 | break; |
| 5099 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5100 | binOp = spv::OpMatrixTimesVector; |
| 5101 | break; |
| 5102 | case glslang::EOpMatrixTimesScalar: |
| 5103 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5104 | binOp = spv::OpMatrixTimesScalar; |
| 5105 | break; |
| 5106 | case glslang::EOpMatrixTimesMatrix: |
| 5107 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5108 | binOp = spv::OpMatrixTimesMatrix; |
| 5109 | break; |
| 5110 | case glslang::EOpOuterProduct: |
| 5111 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5112 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5113 | break; |
| 5114 | |
| 5115 | case glslang::EOpDiv: |
| 5116 | case glslang::EOpDivAssign: |
| 5117 | if (isFloat) |
| 5118 | binOp = spv::OpFDiv; |
| 5119 | else if (isUnsigned) |
| 5120 | binOp = spv::OpUDiv; |
| 5121 | else |
| 5122 | binOp = spv::OpSDiv; |
| 5123 | break; |
| 5124 | case glslang::EOpMod: |
| 5125 | case glslang::EOpModAssign: |
| 5126 | if (isFloat) |
| 5127 | binOp = spv::OpFMod; |
| 5128 | else if (isUnsigned) |
| 5129 | binOp = spv::OpUMod; |
| 5130 | else |
| 5131 | binOp = spv::OpSMod; |
| 5132 | break; |
| 5133 | case glslang::EOpRightShift: |
| 5134 | case glslang::EOpRightShiftAssign: |
| 5135 | if (isUnsigned) |
| 5136 | binOp = spv::OpShiftRightLogical; |
| 5137 | else |
| 5138 | binOp = spv::OpShiftRightArithmetic; |
| 5139 | break; |
| 5140 | case glslang::EOpLeftShift: |
| 5141 | case glslang::EOpLeftShiftAssign: |
| 5142 | binOp = spv::OpShiftLeftLogical; |
| 5143 | break; |
| 5144 | case glslang::EOpAnd: |
| 5145 | case glslang::EOpAndAssign: |
| 5146 | binOp = spv::OpBitwiseAnd; |
| 5147 | break; |
| 5148 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5149 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5150 | binOp = spv::OpLogicalAnd; |
| 5151 | break; |
| 5152 | case glslang::EOpInclusiveOr: |
| 5153 | case glslang::EOpInclusiveOrAssign: |
| 5154 | binOp = spv::OpBitwiseOr; |
| 5155 | break; |
| 5156 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5157 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5158 | binOp = spv::OpLogicalOr; |
| 5159 | break; |
| 5160 | case glslang::EOpExclusiveOr: |
| 5161 | case glslang::EOpExclusiveOrAssign: |
| 5162 | binOp = spv::OpBitwiseXor; |
| 5163 | break; |
| 5164 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5165 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5166 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5167 | break; |
| 5168 | |
| 5169 | case glslang::EOpLessThan: |
| 5170 | case glslang::EOpGreaterThan: |
| 5171 | case glslang::EOpLessThanEqual: |
| 5172 | case glslang::EOpGreaterThanEqual: |
| 5173 | case glslang::EOpEqual: |
| 5174 | case glslang::EOpNotEqual: |
| 5175 | case glslang::EOpVectorEqual: |
| 5176 | case glslang::EOpVectorNotEqual: |
| 5177 | comparison = true; |
| 5178 | break; |
| 5179 | default: |
| 5180 | break; |
| 5181 | } |
| 5182 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5183 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5184 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5185 | assert(comparison == false); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5186 | if (builder.isMatrix(left) || builder.isMatrix(right) || |
| 5187 | builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5188 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5189 | |
| 5190 | // No matrix involved; make both operands be the same number of components, if needed |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5191 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5192 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5193 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5194 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5195 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5196 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5197 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | if (! comparison) |
| 5201 | return 0; |
| 5202 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5203 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5204 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 5205 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5206 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 5207 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5208 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5209 | return result; |
| 5210 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5211 | |
| 5212 | switch (op) { |
| 5213 | case glslang::EOpLessThan: |
| 5214 | if (isFloat) |
| 5215 | binOp = spv::OpFOrdLessThan; |
| 5216 | else if (isUnsigned) |
| 5217 | binOp = spv::OpULessThan; |
| 5218 | else |
| 5219 | binOp = spv::OpSLessThan; |
| 5220 | break; |
| 5221 | case glslang::EOpGreaterThan: |
| 5222 | if (isFloat) |
| 5223 | binOp = spv::OpFOrdGreaterThan; |
| 5224 | else if (isUnsigned) |
| 5225 | binOp = spv::OpUGreaterThan; |
| 5226 | else |
| 5227 | binOp = spv::OpSGreaterThan; |
| 5228 | break; |
| 5229 | case glslang::EOpLessThanEqual: |
| 5230 | if (isFloat) |
| 5231 | binOp = spv::OpFOrdLessThanEqual; |
| 5232 | else if (isUnsigned) |
| 5233 | binOp = spv::OpULessThanEqual; |
| 5234 | else |
| 5235 | binOp = spv::OpSLessThanEqual; |
| 5236 | break; |
| 5237 | case glslang::EOpGreaterThanEqual: |
| 5238 | if (isFloat) |
| 5239 | binOp = spv::OpFOrdGreaterThanEqual; |
| 5240 | else if (isUnsigned) |
| 5241 | binOp = spv::OpUGreaterThanEqual; |
| 5242 | else |
| 5243 | binOp = spv::OpSGreaterThanEqual; |
| 5244 | break; |
| 5245 | case glslang::EOpEqual: |
| 5246 | case glslang::EOpVectorEqual: |
| 5247 | if (isFloat) |
| 5248 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5249 | else if (isBool) |
| 5250 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5251 | else |
| 5252 | binOp = spv::OpIEqual; |
| 5253 | break; |
| 5254 | case glslang::EOpNotEqual: |
| 5255 | case glslang::EOpVectorNotEqual: |
| 5256 | if (isFloat) |
| 5257 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5258 | else if (isBool) |
| 5259 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5260 | else |
| 5261 | binOp = spv::OpINotEqual; |
| 5262 | break; |
| 5263 | default: |
| 5264 | break; |
| 5265 | } |
| 5266 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5267 | if (binOp != spv::OpNop) { |
| 5268 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5269 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5270 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5271 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5272 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5273 | |
| 5274 | return 0; |
| 5275 | } |
| 5276 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5277 | // |
| 5278 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 5279 | // These can be any of: |
| 5280 | // |
| 5281 | // matrix * scalar |
| 5282 | // scalar * matrix |
| 5283 | // matrix * matrix linear algebraic |
| 5284 | // matrix * vector |
| 5285 | // vector * matrix |
| 5286 | // matrix * matrix componentwise |
| 5287 | // matrix op matrix op in {+, -, /} |
| 5288 | // matrix op scalar op in {+, -, /} |
| 5289 | // scalar op matrix op in {+, -, /} |
| 5290 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5291 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5292 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5293 | { |
| 5294 | bool firstClass = true; |
| 5295 | |
| 5296 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 5297 | switch (op) { |
| 5298 | case spv::OpFDiv: |
| 5299 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 5300 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 5301 | spv::Id resultType = builder.getTypeId(right); |
| 5302 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5303 | op = spv::OpMatrixTimesScalar; |
| 5304 | } else |
| 5305 | firstClass = false; |
| 5306 | break; |
| 5307 | case spv::OpMatrixTimesScalar: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5308 | if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5309 | std::swap(left, right); |
| 5310 | assert(builder.isScalar(right)); |
| 5311 | break; |
| 5312 | case spv::OpVectorTimesMatrix: |
| 5313 | assert(builder.isVector(left)); |
| 5314 | assert(builder.isMatrix(right)); |
| 5315 | break; |
| 5316 | case spv::OpMatrixTimesVector: |
| 5317 | assert(builder.isMatrix(left)); |
| 5318 | assert(builder.isVector(right)); |
| 5319 | break; |
| 5320 | case spv::OpMatrixTimesMatrix: |
| 5321 | assert(builder.isMatrix(left)); |
| 5322 | assert(builder.isMatrix(right)); |
| 5323 | break; |
| 5324 | default: |
| 5325 | firstClass = false; |
| 5326 | break; |
| 5327 | } |
| 5328 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5329 | if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 5330 | firstClass = true; |
| 5331 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5332 | if (firstClass) { |
| 5333 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5334 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5335 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5336 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5337 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5338 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5339 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5340 | // The result type of all of them is the same type as the (a) matrix operand. |
| 5341 | // The algorithm is to: |
| 5342 | // - break the matrix(es) into vectors |
| 5343 | // - smear any scalar to a vector |
| 5344 | // - do vector operations |
| 5345 | // - make a matrix out the vector results |
| 5346 | switch (op) { |
| 5347 | case spv::OpFAdd: |
| 5348 | case spv::OpFSub: |
| 5349 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5350 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5351 | case spv::OpFMul: |
| 5352 | { |
| 5353 | // one time set up... |
| 5354 | bool leftMat = builder.isMatrix(left); |
| 5355 | bool rightMat = builder.isMatrix(right); |
| 5356 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 5357 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 5358 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 5359 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 5360 | std::vector<spv::Id> results; |
| 5361 | spv::Id smearVec = spv::NoResult; |
| 5362 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5363 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5364 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5365 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5366 | |
| 5367 | // do each vector op |
| 5368 | for (unsigned int c = 0; c < numCols; ++c) { |
| 5369 | std::vector<unsigned int> indexes; |
| 5370 | indexes.push_back(c); |
| 5371 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 5372 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5373 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5374 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5375 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5376 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5380 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5381 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5382 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5383 | } |
| 5384 | default: |
| 5385 | assert(0); |
| 5386 | return spv::NoResult; |
| 5387 | } |
| 5388 | } |
| 5389 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5390 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 5391 | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5392 | { |
| 5393 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5394 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5395 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5396 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5397 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5398 | |
| 5399 | switch (op) { |
| 5400 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5401 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5402 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5403 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5404 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5405 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5406 | unaryOp = spv::OpSNegate; |
| 5407 | break; |
| 5408 | |
| 5409 | case glslang::EOpLogicalNot: |
| 5410 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5411 | unaryOp = spv::OpLogicalNot; |
| 5412 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5413 | case glslang::EOpBitwiseNot: |
| 5414 | unaryOp = spv::OpNot; |
| 5415 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5416 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5417 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5418 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5419 | break; |
| 5420 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5421 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5422 | break; |
| 5423 | case glslang::EOpTranspose: |
| 5424 | unaryOp = spv::OpTranspose; |
| 5425 | break; |
| 5426 | |
| 5427 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5428 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5429 | break; |
| 5430 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5431 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5432 | break; |
| 5433 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5434 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5435 | break; |
| 5436 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5437 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5438 | break; |
| 5439 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5440 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5441 | break; |
| 5442 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5443 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5444 | break; |
| 5445 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5446 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5447 | break; |
| 5448 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5449 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5450 | break; |
| 5451 | |
| 5452 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5453 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5454 | break; |
| 5455 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5456 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5457 | break; |
| 5458 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5459 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5460 | break; |
| 5461 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5462 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5463 | break; |
| 5464 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5465 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5466 | break; |
| 5467 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5468 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5469 | break; |
| 5470 | |
| 5471 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5472 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5473 | break; |
| 5474 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5475 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5476 | break; |
| 5477 | |
| 5478 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5479 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5480 | break; |
| 5481 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5482 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5483 | break; |
| 5484 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5485 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5486 | break; |
| 5487 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5488 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5489 | break; |
| 5490 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5491 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5492 | break; |
| 5493 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5494 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5495 | break; |
| 5496 | |
| 5497 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5498 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5499 | break; |
| 5500 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5501 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5502 | break; |
| 5503 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5504 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5505 | break; |
| 5506 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5507 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5508 | break; |
| 5509 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5510 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5511 | break; |
| 5512 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5513 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5514 | break; |
| 5515 | |
| 5516 | case glslang::EOpIsNan: |
| 5517 | unaryOp = spv::OpIsNan; |
| 5518 | break; |
| 5519 | case glslang::EOpIsInf: |
| 5520 | unaryOp = spv::OpIsInf; |
| 5521 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5522 | case glslang::EOpIsFinite: |
| 5523 | unaryOp = spv::OpIsFinite; |
| 5524 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5525 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5526 | case glslang::EOpFloatBitsToInt: |
| 5527 | case glslang::EOpFloatBitsToUint: |
| 5528 | case glslang::EOpIntBitsToFloat: |
| 5529 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5530 | case glslang::EOpDoubleBitsToInt64: |
| 5531 | case glslang::EOpDoubleBitsToUint64: |
| 5532 | case glslang::EOpInt64BitsToDouble: |
| 5533 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5534 | case glslang::EOpFloat16BitsToInt16: |
| 5535 | case glslang::EOpFloat16BitsToUint16: |
| 5536 | case glslang::EOpInt16BitsToFloat16: |
| 5537 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5538 | unaryOp = spv::OpBitcast; |
| 5539 | break; |
| 5540 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5541 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5542 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5543 | break; |
| 5544 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5545 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5546 | break; |
| 5547 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5548 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5549 | break; |
| 5550 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5551 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5552 | break; |
| 5553 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5554 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5555 | break; |
| 5556 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5557 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5558 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5559 | case glslang::EOpPackSnorm4x8: |
| 5560 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 5561 | break; |
| 5562 | case glslang::EOpUnpackSnorm4x8: |
| 5563 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5564 | break; |
| 5565 | case glslang::EOpPackUnorm4x8: |
| 5566 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 5567 | break; |
| 5568 | case glslang::EOpUnpackUnorm4x8: |
| 5569 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5570 | break; |
| 5571 | case glslang::EOpPackDouble2x32: |
| 5572 | libCall = spv::GLSLstd450PackDouble2x32; |
| 5573 | break; |
| 5574 | case glslang::EOpUnpackDouble2x32: |
| 5575 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5576 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5577 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5578 | case glslang::EOpPackInt2x32: |
| 5579 | case glslang::EOpUnpackInt2x32: |
| 5580 | case glslang::EOpPackUint2x32: |
| 5581 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5582 | case glslang::EOpPack16: |
| 5583 | case glslang::EOpPack32: |
| 5584 | case glslang::EOpPack64: |
| 5585 | case glslang::EOpUnpack32: |
| 5586 | case glslang::EOpUnpack16: |
| 5587 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5588 | case glslang::EOpPackInt2x16: |
| 5589 | case glslang::EOpUnpackInt2x16: |
| 5590 | case glslang::EOpPackUint2x16: |
| 5591 | case glslang::EOpUnpackUint2x16: |
| 5592 | case glslang::EOpPackInt4x16: |
| 5593 | case glslang::EOpUnpackInt4x16: |
| 5594 | case glslang::EOpPackUint4x16: |
| 5595 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5596 | case glslang::EOpPackFloat2x16: |
| 5597 | case glslang::EOpUnpackFloat2x16: |
| 5598 | unaryOp = spv::OpBitcast; |
| 5599 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5600 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5601 | case glslang::EOpDPdx: |
| 5602 | unaryOp = spv::OpDPdx; |
| 5603 | break; |
| 5604 | case glslang::EOpDPdy: |
| 5605 | unaryOp = spv::OpDPdy; |
| 5606 | break; |
| 5607 | case glslang::EOpFwidth: |
| 5608 | unaryOp = spv::OpFwidth; |
| 5609 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5610 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5611 | case glslang::EOpAny: |
| 5612 | unaryOp = spv::OpAny; |
| 5613 | break; |
| 5614 | case glslang::EOpAll: |
| 5615 | unaryOp = spv::OpAll; |
| 5616 | break; |
| 5617 | |
| 5618 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5619 | if (isFloat) |
| 5620 | libCall = spv::GLSLstd450FAbs; |
| 5621 | else |
| 5622 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5623 | break; |
| 5624 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5625 | if (isFloat) |
| 5626 | libCall = spv::GLSLstd450FSign; |
| 5627 | else |
| 5628 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5629 | break; |
| 5630 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5631 | #ifndef GLSLANG_WEB |
| 5632 | case glslang::EOpDPdxFine: |
| 5633 | unaryOp = spv::OpDPdxFine; |
| 5634 | break; |
| 5635 | case glslang::EOpDPdyFine: |
| 5636 | unaryOp = spv::OpDPdyFine; |
| 5637 | break; |
| 5638 | case glslang::EOpFwidthFine: |
| 5639 | unaryOp = spv::OpFwidthFine; |
| 5640 | break; |
| 5641 | case glslang::EOpDPdxCoarse: |
| 5642 | unaryOp = spv::OpDPdxCoarse; |
| 5643 | break; |
| 5644 | case glslang::EOpDPdyCoarse: |
| 5645 | unaryOp = spv::OpDPdyCoarse; |
| 5646 | break; |
| 5647 | case glslang::EOpFwidthCoarse: |
| 5648 | unaryOp = spv::OpFwidthCoarse; |
| 5649 | break; |
| 5650 | case glslang::EOpInterpolateAtCentroid: |
| 5651 | if (typeProxy == glslang::EbtFloat16) |
| 5652 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 5653 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 5654 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5655 | case glslang::EOpAtomicCounterIncrement: |
| 5656 | case glslang::EOpAtomicCounterDecrement: |
| 5657 | case glslang::EOpAtomicCounter: |
| 5658 | { |
| 5659 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 5660 | std::vector<spv::Id> operands; |
| 5661 | operands.push_back(operand); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 5662 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5663 | } |
| 5664 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5665 | case glslang::EOpBitFieldReverse: |
| 5666 | unaryOp = spv::OpBitReverse; |
| 5667 | break; |
| 5668 | case glslang::EOpBitCount: |
| 5669 | unaryOp = spv::OpBitCount; |
| 5670 | break; |
| 5671 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5672 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5673 | break; |
| 5674 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5675 | if (isUnsigned) |
| 5676 | libCall = spv::GLSLstd450FindUMsb; |
| 5677 | else |
| 5678 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5679 | break; |
| 5680 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5681 | case glslang::EOpBallot: |
| 5682 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5683 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5684 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5685 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5686 | case glslang::EOpMinInvocations: |
| 5687 | case glslang::EOpMaxInvocations: |
| 5688 | case glslang::EOpAddInvocations: |
| 5689 | case glslang::EOpMinInvocationsNonUniform: |
| 5690 | case glslang::EOpMaxInvocationsNonUniform: |
| 5691 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5692 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5693 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5694 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5695 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5696 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5697 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5698 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5699 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5700 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5701 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5702 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5703 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5704 | { |
| 5705 | std::vector<spv::Id> operands; |
| 5706 | operands.push_back(operand); |
| 5707 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 5708 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5709 | case glslang::EOpSubgroupAll: |
| 5710 | case glslang::EOpSubgroupAny: |
| 5711 | case glslang::EOpSubgroupAllEqual: |
| 5712 | case glslang::EOpSubgroupBroadcastFirst: |
| 5713 | case glslang::EOpSubgroupBallot: |
| 5714 | case glslang::EOpSubgroupInverseBallot: |
| 5715 | case glslang::EOpSubgroupBallotBitCount: |
| 5716 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5717 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5718 | case glslang::EOpSubgroupBallotFindLSB: |
| 5719 | case glslang::EOpSubgroupBallotFindMSB: |
| 5720 | case glslang::EOpSubgroupAdd: |
| 5721 | case glslang::EOpSubgroupMul: |
| 5722 | case glslang::EOpSubgroupMin: |
| 5723 | case glslang::EOpSubgroupMax: |
| 5724 | case glslang::EOpSubgroupAnd: |
| 5725 | case glslang::EOpSubgroupOr: |
| 5726 | case glslang::EOpSubgroupXor: |
| 5727 | case glslang::EOpSubgroupInclusiveAdd: |
| 5728 | case glslang::EOpSubgroupInclusiveMul: |
| 5729 | case glslang::EOpSubgroupInclusiveMin: |
| 5730 | case glslang::EOpSubgroupInclusiveMax: |
| 5731 | case glslang::EOpSubgroupInclusiveAnd: |
| 5732 | case glslang::EOpSubgroupInclusiveOr: |
| 5733 | case glslang::EOpSubgroupInclusiveXor: |
| 5734 | case glslang::EOpSubgroupExclusiveAdd: |
| 5735 | case glslang::EOpSubgroupExclusiveMul: |
| 5736 | case glslang::EOpSubgroupExclusiveMin: |
| 5737 | case glslang::EOpSubgroupExclusiveMax: |
| 5738 | case glslang::EOpSubgroupExclusiveAnd: |
| 5739 | case glslang::EOpSubgroupExclusiveOr: |
| 5740 | case glslang::EOpSubgroupExclusiveXor: |
| 5741 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5742 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5743 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 5744 | std::vector<spv::Id> operands; |
| 5745 | operands.push_back(operand); |
| 5746 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 5747 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5748 | case glslang::EOpMbcnt: |
| 5749 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5750 | libCall = spv::MbcntAMD; |
| 5751 | break; |
| 5752 | |
| 5753 | case glslang::EOpCubeFaceIndex: |
| 5754 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5755 | libCall = spv::CubeFaceIndexAMD; |
| 5756 | break; |
| 5757 | |
| 5758 | case glslang::EOpCubeFaceCoord: |
| 5759 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5760 | libCall = spv::CubeFaceCoordAMD; |
| 5761 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5762 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5763 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 5764 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 5765 | case glslang::EOpConstructReference: |
| 5766 | unaryOp = spv::OpBitcast; |
| 5767 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5768 | #endif |
Jeff Bolz | 88220d5 | 2019-05-08 10:24:46 -0500 | [diff] [blame] | 5769 | |
| 5770 | case glslang::EOpCopyObject: |
| 5771 | unaryOp = spv::OpCopyObject; |
| 5772 | break; |
| 5773 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5774 | default: |
| 5775 | return 0; |
| 5776 | } |
| 5777 | |
| 5778 | spv::Id id; |
| 5779 | if (libCall >= 0) { |
| 5780 | std::vector<spv::Id> args; |
| 5781 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5782 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5783 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5784 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5785 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5786 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5787 | builder.addDecoration(id, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5788 | builder.addDecoration(id, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5789 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5790 | } |
| 5791 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5792 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5793 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5794 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5795 | { |
| 5796 | // Handle unary operations vector by vector. |
| 5797 | // The result type is the same type as the original type. |
| 5798 | // The algorithm is to: |
| 5799 | // - break the matrix into vectors |
| 5800 | // - apply the operation to each vector |
| 5801 | // - make a matrix out the vector results |
| 5802 | |
| 5803 | // get the types sorted out |
| 5804 | int numCols = builder.getNumColumns(operand); |
| 5805 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5806 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 5807 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5808 | std::vector<spv::Id> results; |
| 5809 | |
| 5810 | // do each vector op |
| 5811 | for (int c = 0; c < numCols; ++c) { |
| 5812 | std::vector<unsigned int> indexes; |
| 5813 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5814 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 5815 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5816 | builder.addDecoration(destVec, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5817 | builder.addDecoration(destVec, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5818 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5819 | } |
| 5820 | |
| 5821 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5822 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5823 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5824 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5825 | } |
| 5826 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5827 | // For converting integers where both the bitwidth and the signedness could |
| 5828 | // change, but only do the width change here. The caller is still responsible |
| 5829 | // for the signedness conversion. |
| 5830 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5831 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5832 | // Get the result type width, based on the type to convert to. |
| 5833 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5834 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5835 | case glslang::EOpConvInt16ToUint8: |
| 5836 | case glslang::EOpConvIntToUint8: |
| 5837 | case glslang::EOpConvInt64ToUint8: |
| 5838 | case glslang::EOpConvUint16ToInt8: |
| 5839 | case glslang::EOpConvUintToInt8: |
| 5840 | case glslang::EOpConvUint64ToInt8: |
| 5841 | width = 8; |
| 5842 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5843 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5844 | case glslang::EOpConvIntToUint16: |
| 5845 | case glslang::EOpConvInt64ToUint16: |
| 5846 | case glslang::EOpConvUint8ToInt16: |
| 5847 | case glslang::EOpConvUintToInt16: |
| 5848 | case glslang::EOpConvUint64ToInt16: |
| 5849 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5850 | break; |
| 5851 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5852 | case glslang::EOpConvInt16ToUint: |
| 5853 | case glslang::EOpConvInt64ToUint: |
| 5854 | case glslang::EOpConvUint8ToInt: |
| 5855 | case glslang::EOpConvUint16ToInt: |
| 5856 | case glslang::EOpConvUint64ToInt: |
| 5857 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5858 | break; |
| 5859 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5860 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5861 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5862 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5863 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5864 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5865 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5866 | break; |
| 5867 | |
| 5868 | default: |
| 5869 | assert(false && "Default missing"); |
| 5870 | break; |
| 5871 | } |
| 5872 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5873 | // Get the conversion operation and result type, |
| 5874 | // based on the target width, but the source type. |
| 5875 | spv::Id type = spv::NoType; |
| 5876 | spv::Op convOp = spv::OpNop; |
| 5877 | switch(op) { |
| 5878 | case glslang::EOpConvInt8ToUint16: |
| 5879 | case glslang::EOpConvInt8ToUint: |
| 5880 | case glslang::EOpConvInt8ToUint64: |
| 5881 | case glslang::EOpConvInt16ToUint8: |
| 5882 | case glslang::EOpConvInt16ToUint: |
| 5883 | case glslang::EOpConvInt16ToUint64: |
| 5884 | case glslang::EOpConvIntToUint8: |
| 5885 | case glslang::EOpConvIntToUint16: |
| 5886 | case glslang::EOpConvIntToUint64: |
| 5887 | case glslang::EOpConvInt64ToUint8: |
| 5888 | case glslang::EOpConvInt64ToUint16: |
| 5889 | case glslang::EOpConvInt64ToUint: |
| 5890 | convOp = spv::OpSConvert; |
| 5891 | type = builder.makeIntType(width); |
| 5892 | break; |
| 5893 | default: |
| 5894 | convOp = spv::OpUConvert; |
| 5895 | type = builder.makeUintType(width); |
| 5896 | break; |
| 5897 | } |
| 5898 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5899 | if (vectorSize > 0) |
| 5900 | type = builder.makeVectorType(type, vectorSize); |
| 5901 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5902 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5903 | } |
| 5904 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5905 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 5906 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5907 | { |
| 5908 | spv::Op convOp = spv::OpNop; |
| 5909 | spv::Id zero = 0; |
| 5910 | spv::Id one = 0; |
| 5911 | |
| 5912 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 5913 | |
| 5914 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5915 | case glslang::EOpConvIntToBool: |
| 5916 | case glslang::EOpConvUintToBool: |
| 5917 | zero = builder.makeUintConstant(0); |
| 5918 | zero = makeSmearedConstant(zero, vectorSize); |
| 5919 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5920 | case glslang::EOpConvFloatToBool: |
| 5921 | zero = builder.makeFloatConstant(0.0F); |
| 5922 | zero = makeSmearedConstant(zero, vectorSize); |
| 5923 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5924 | case glslang::EOpConvBoolToFloat: |
| 5925 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5926 | zero = builder.makeFloatConstant(0.0F); |
| 5927 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5928 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5929 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5930 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5931 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5932 | if (op == glslang::EOpConvBoolToInt64) |
| 5933 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5934 | else |
| 5935 | zero = builder.makeIntConstant(0); |
| 5936 | |
| 5937 | if (op == glslang::EOpConvBoolToInt64) |
| 5938 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5939 | else |
| 5940 | one = builder.makeIntConstant(1); |
| 5941 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5942 | convOp = spv::OpSelect; |
| 5943 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5944 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5945 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5946 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5947 | if (op == glslang::EOpConvBoolToUint64) |
| 5948 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5949 | else |
| 5950 | zero = builder.makeUintConstant(0); |
| 5951 | |
| 5952 | if (op == glslang::EOpConvBoolToUint64) |
| 5953 | one = builder.makeUint64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5954 | else |
| 5955 | one = builder.makeUintConstant(1); |
| 5956 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5957 | convOp = spv::OpSelect; |
| 5958 | break; |
| 5959 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5960 | case glslang::EOpConvInt8ToFloat16: |
| 5961 | case glslang::EOpConvInt8ToFloat: |
| 5962 | case glslang::EOpConvInt8ToDouble: |
| 5963 | case glslang::EOpConvInt16ToFloat16: |
| 5964 | case glslang::EOpConvInt16ToFloat: |
| 5965 | case glslang::EOpConvInt16ToDouble: |
| 5966 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5967 | case glslang::EOpConvIntToFloat: |
| 5968 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5969 | case glslang::EOpConvInt64ToFloat: |
| 5970 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5971 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5972 | convOp = spv::OpConvertSToF; |
| 5973 | break; |
| 5974 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5975 | case glslang::EOpConvUint8ToFloat16: |
| 5976 | case glslang::EOpConvUint8ToFloat: |
| 5977 | case glslang::EOpConvUint8ToDouble: |
| 5978 | case glslang::EOpConvUint16ToFloat16: |
| 5979 | case glslang::EOpConvUint16ToFloat: |
| 5980 | case glslang::EOpConvUint16ToDouble: |
| 5981 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5982 | case glslang::EOpConvUintToFloat: |
| 5983 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5984 | case glslang::EOpConvUint64ToFloat: |
| 5985 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5986 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5987 | convOp = spv::OpConvertUToF; |
| 5988 | break; |
| 5989 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5990 | case glslang::EOpConvFloat16ToInt8: |
| 5991 | case glslang::EOpConvFloatToInt8: |
| 5992 | case glslang::EOpConvDoubleToInt8: |
| 5993 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5994 | case glslang::EOpConvFloatToInt16: |
| 5995 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5996 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5997 | case glslang::EOpConvFloatToInt: |
| 5998 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5999 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6000 | case glslang::EOpConvFloatToInt64: |
| 6001 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6002 | convOp = spv::OpConvertFToS; |
| 6003 | break; |
| 6004 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6005 | case glslang::EOpConvUint8ToInt8: |
| 6006 | case glslang::EOpConvInt8ToUint8: |
| 6007 | case glslang::EOpConvUint16ToInt16: |
| 6008 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6009 | case glslang::EOpConvUintToInt: |
| 6010 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6011 | case glslang::EOpConvUint64ToInt64: |
| 6012 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6013 | if (builder.isInSpecConstCodeGenMode()) { |
| 6014 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6015 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6016 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 6017 | zero = builder.makeUint8Constant(0); |
| 6018 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6019 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6020 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 6021 | zero = builder.makeUint64Constant(0); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6022 | } else |
| 6023 | #endif |
| 6024 | { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6025 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6026 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 6027 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6028 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6029 | // generating for OpSpecConstantOp instruction. |
| 6030 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6031 | } |
| 6032 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6033 | convOp = spv::OpBitcast; |
| 6034 | break; |
| 6035 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6036 | case glslang::EOpConvFloat16ToUint8: |
| 6037 | case glslang::EOpConvFloatToUint8: |
| 6038 | case glslang::EOpConvDoubleToUint8: |
| 6039 | case glslang::EOpConvFloat16ToUint16: |
| 6040 | case glslang::EOpConvFloatToUint16: |
| 6041 | case glslang::EOpConvDoubleToUint16: |
| 6042 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6043 | case glslang::EOpConvFloatToUint: |
| 6044 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6045 | case glslang::EOpConvFloatToUint64: |
| 6046 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6047 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6048 | convOp = spv::OpConvertFToU; |
| 6049 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6050 | |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6051 | #ifndef GLSLANG_WEB |
| 6052 | case glslang::EOpConvInt8ToBool: |
| 6053 | case glslang::EOpConvUint8ToBool: |
| 6054 | zero = builder.makeUint8Constant(0); |
| 6055 | zero = makeSmearedConstant(zero, vectorSize); |
| 6056 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6057 | case glslang::EOpConvInt16ToBool: |
| 6058 | case glslang::EOpConvUint16ToBool: |
| 6059 | zero = builder.makeUint16Constant(0); |
| 6060 | zero = makeSmearedConstant(zero, vectorSize); |
| 6061 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6062 | case glslang::EOpConvInt64ToBool: |
| 6063 | case glslang::EOpConvUint64ToBool: |
| 6064 | zero = builder.makeUint64Constant(0); |
| 6065 | zero = makeSmearedConstant(zero, vectorSize); |
| 6066 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6067 | case glslang::EOpConvDoubleToBool: |
| 6068 | zero = builder.makeDoubleConstant(0.0); |
| 6069 | zero = makeSmearedConstant(zero, vectorSize); |
| 6070 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 6071 | case glslang::EOpConvFloat16ToBool: |
| 6072 | zero = builder.makeFloat16Constant(0.0F); |
| 6073 | zero = makeSmearedConstant(zero, vectorSize); |
| 6074 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 6075 | case glslang::EOpConvBoolToDouble: |
| 6076 | convOp = spv::OpSelect; |
| 6077 | zero = builder.makeDoubleConstant(0.0); |
| 6078 | one = builder.makeDoubleConstant(1.0); |
| 6079 | break; |
| 6080 | case glslang::EOpConvBoolToFloat16: |
| 6081 | convOp = spv::OpSelect; |
| 6082 | zero = builder.makeFloat16Constant(0.0F); |
| 6083 | one = builder.makeFloat16Constant(1.0F); |
| 6084 | break; |
| 6085 | case glslang::EOpConvBoolToInt8: |
| 6086 | zero = builder.makeInt8Constant(0); |
| 6087 | one = builder.makeInt8Constant(1); |
| 6088 | convOp = spv::OpSelect; |
| 6089 | break; |
| 6090 | case glslang::EOpConvBoolToUint8: |
| 6091 | zero = builder.makeUint8Constant(0); |
| 6092 | one = builder.makeUint8Constant(1); |
| 6093 | convOp = spv::OpSelect; |
| 6094 | break; |
| 6095 | case glslang::EOpConvBoolToInt16: |
| 6096 | zero = builder.makeInt16Constant(0); |
| 6097 | one = builder.makeInt16Constant(1); |
| 6098 | convOp = spv::OpSelect; |
| 6099 | break; |
| 6100 | case glslang::EOpConvBoolToUint16: |
| 6101 | zero = builder.makeUint16Constant(0); |
| 6102 | one = builder.makeUint16Constant(1); |
| 6103 | convOp = spv::OpSelect; |
| 6104 | break; |
| 6105 | case glslang::EOpConvDoubleToFloat: |
| 6106 | case glslang::EOpConvFloatToDouble: |
| 6107 | case glslang::EOpConvDoubleToFloat16: |
| 6108 | case glslang::EOpConvFloat16ToDouble: |
| 6109 | case glslang::EOpConvFloatToFloat16: |
| 6110 | case glslang::EOpConvFloat16ToFloat: |
| 6111 | convOp = spv::OpFConvert; |
| 6112 | if (builder.isMatrixType(destType)) |
| 6113 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
| 6114 | break; |
| 6115 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6116 | case glslang::EOpConvInt8ToInt16: |
| 6117 | case glslang::EOpConvInt8ToInt: |
| 6118 | case glslang::EOpConvInt8ToInt64: |
| 6119 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6120 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6121 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6122 | case glslang::EOpConvIntToInt8: |
| 6123 | case glslang::EOpConvIntToInt16: |
| 6124 | case glslang::EOpConvIntToInt64: |
| 6125 | case glslang::EOpConvInt64ToInt8: |
| 6126 | case glslang::EOpConvInt64ToInt16: |
| 6127 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6128 | convOp = spv::OpSConvert; |
| 6129 | break; |
| 6130 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6131 | case glslang::EOpConvUint8ToUint16: |
| 6132 | case glslang::EOpConvUint8ToUint: |
| 6133 | case glslang::EOpConvUint8ToUint64: |
| 6134 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6135 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6136 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6137 | case glslang::EOpConvUintToUint8: |
| 6138 | case glslang::EOpConvUintToUint16: |
| 6139 | case glslang::EOpConvUintToUint64: |
| 6140 | case glslang::EOpConvUint64ToUint8: |
| 6141 | case glslang::EOpConvUint64ToUint16: |
| 6142 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6143 | convOp = spv::OpUConvert; |
| 6144 | break; |
| 6145 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6146 | case glslang::EOpConvInt8ToUint16: |
| 6147 | case glslang::EOpConvInt8ToUint: |
| 6148 | case glslang::EOpConvInt8ToUint64: |
| 6149 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6150 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6151 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6152 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6153 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6154 | case glslang::EOpConvIntToUint64: |
| 6155 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6156 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6157 | case glslang::EOpConvInt64ToUint: |
| 6158 | case glslang::EOpConvUint8ToInt16: |
| 6159 | case glslang::EOpConvUint8ToInt: |
| 6160 | case glslang::EOpConvUint8ToInt64: |
| 6161 | case glslang::EOpConvUint16ToInt8: |
| 6162 | case glslang::EOpConvUint16ToInt: |
| 6163 | case glslang::EOpConvUint16ToInt64: |
| 6164 | case glslang::EOpConvUintToInt8: |
| 6165 | case glslang::EOpConvUintToInt16: |
| 6166 | case glslang::EOpConvUintToInt64: |
| 6167 | case glslang::EOpConvUint64ToInt8: |
| 6168 | case glslang::EOpConvUint64ToInt16: |
| 6169 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6170 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6171 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6172 | |
| 6173 | if (builder.isInSpecConstCodeGenMode()) { |
| 6174 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6175 | switch(op) { |
| 6176 | case glslang::EOpConvInt16ToUint8: |
| 6177 | case glslang::EOpConvIntToUint8: |
| 6178 | case glslang::EOpConvInt64ToUint8: |
| 6179 | case glslang::EOpConvUint16ToInt8: |
| 6180 | case glslang::EOpConvUintToInt8: |
| 6181 | case glslang::EOpConvUint64ToInt8: |
| 6182 | zero = builder.makeUint8Constant(0); |
| 6183 | break; |
| 6184 | case glslang::EOpConvInt8ToUint16: |
| 6185 | case glslang::EOpConvIntToUint16: |
| 6186 | case glslang::EOpConvInt64ToUint16: |
| 6187 | case glslang::EOpConvUint8ToInt16: |
| 6188 | case glslang::EOpConvUintToInt16: |
| 6189 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6190 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6191 | break; |
| 6192 | case glslang::EOpConvInt8ToUint: |
| 6193 | case glslang::EOpConvInt16ToUint: |
| 6194 | case glslang::EOpConvInt64ToUint: |
| 6195 | case glslang::EOpConvUint8ToInt: |
| 6196 | case glslang::EOpConvUint16ToInt: |
| 6197 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6198 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6199 | break; |
| 6200 | case glslang::EOpConvInt8ToUint64: |
| 6201 | case glslang::EOpConvInt16ToUint64: |
| 6202 | case glslang::EOpConvIntToUint64: |
| 6203 | case glslang::EOpConvUint8ToInt64: |
| 6204 | case glslang::EOpConvUint16ToInt64: |
| 6205 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6206 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6207 | break; |
| 6208 | default: |
| 6209 | assert(false && "Default missing"); |
| 6210 | break; |
| 6211 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6212 | zero = makeSmearedConstant(zero, vectorSize); |
| 6213 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6214 | // generating for OpSpecConstantOp instruction. |
| 6215 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6216 | } |
| 6217 | // For normal run-time conversion instruction, use OpBitcast. |
| 6218 | convOp = spv::OpBitcast; |
| 6219 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6220 | case glslang::EOpConvUint64ToPtr: |
| 6221 | convOp = spv::OpConvertUToPtr; |
| 6222 | break; |
| 6223 | case glslang::EOpConvPtrToUint64: |
| 6224 | convOp = spv::OpConvertPtrToU; |
| 6225 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6226 | #endif |
| 6227 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6228 | default: |
| 6229 | break; |
| 6230 | } |
| 6231 | |
| 6232 | spv::Id result = 0; |
| 6233 | if (convOp == spv::OpNop) |
| 6234 | return result; |
| 6235 | |
| 6236 | if (convOp == spv::OpSelect) { |
| 6237 | zero = makeSmearedConstant(zero, vectorSize); |
| 6238 | one = makeSmearedConstant(one, vectorSize); |
| 6239 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 6240 | } else |
| 6241 | result = builder.createUnaryOp(convOp, destType, operand); |
| 6242 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6243 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 6244 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6245 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6246 | } |
| 6247 | |
| 6248 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 6249 | { |
| 6250 | if (vectorSize == 0) |
| 6251 | return constant; |
| 6252 | |
| 6253 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 6254 | std::vector<spv::Id> components; |
| 6255 | for (int c = 0; c < vectorSize; ++c) |
| 6256 | components.push_back(constant); |
| 6257 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 6258 | } |
| 6259 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6260 | // For glslang ops that map to SPV atomic opCodes |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6261 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6262 | { |
| 6263 | spv::Op opCode = spv::OpNop; |
| 6264 | |
| 6265 | switch (op) { |
| 6266 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6267 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6268 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6269 | opCode = spv::OpAtomicIAdd; |
| 6270 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6271 | case glslang::EOpAtomicCounterSubtract: |
| 6272 | opCode = spv::OpAtomicISub; |
| 6273 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6274 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6275 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6276 | case glslang::EOpAtomicCounterMin: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6277 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6278 | break; |
| 6279 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6280 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6281 | case glslang::EOpAtomicCounterMax: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6282 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6283 | break; |
| 6284 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6285 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6286 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6287 | opCode = spv::OpAtomicAnd; |
| 6288 | break; |
| 6289 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6290 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6291 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6292 | opCode = spv::OpAtomicOr; |
| 6293 | break; |
| 6294 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6295 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6296 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6297 | opCode = spv::OpAtomicXor; |
| 6298 | break; |
| 6299 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6300 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6301 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6302 | opCode = spv::OpAtomicExchange; |
| 6303 | break; |
| 6304 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6305 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6306 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6307 | opCode = spv::OpAtomicCompareExchange; |
| 6308 | break; |
| 6309 | case glslang::EOpAtomicCounterIncrement: |
| 6310 | opCode = spv::OpAtomicIIncrement; |
| 6311 | break; |
| 6312 | case glslang::EOpAtomicCounterDecrement: |
| 6313 | opCode = spv::OpAtomicIDecrement; |
| 6314 | break; |
| 6315 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6316 | case glslang::EOpImageAtomicLoad: |
| 6317 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6318 | opCode = spv::OpAtomicLoad; |
| 6319 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6320 | case glslang::EOpAtomicStore: |
| 6321 | case glslang::EOpImageAtomicStore: |
| 6322 | opCode = spv::OpAtomicStore; |
| 6323 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6324 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6325 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6326 | break; |
| 6327 | } |
| 6328 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6329 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 6330 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 6331 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6332 | // Sort out the operands |
| 6333 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6334 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 6335 | // - compare-exchange swaps the value and comparator |
| 6336 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6337 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6338 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 6339 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 6340 | spv::Id scopeId; |
| 6341 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6342 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 6343 | } else { |
| 6344 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 6345 | } |
| 6346 | // semantics default to relaxed |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6347 | spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.volatil ? spv::MemorySemanticsVolatileMask : spv::MemorySemanticsMaskNone); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6348 | spv::Id semanticsId2 = semanticsId; |
| 6349 | |
| 6350 | pointerId = operands[0]; |
| 6351 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 6352 | // no additional operands |
| 6353 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 6354 | compareId = operands[1]; |
| 6355 | valueId = operands[2]; |
| 6356 | if (operands.size() > 3) { |
| 6357 | scopeId = operands[3]; |
| 6358 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 6359 | semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 6360 | } |
| 6361 | } else if (opCode == spv::OpAtomicLoad) { |
| 6362 | if (operands.size() > 1) { |
| 6363 | scopeId = operands[1]; |
| 6364 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 6365 | } |
| 6366 | } else { |
| 6367 | // atomic store or RMW |
| 6368 | valueId = operands[1]; |
| 6369 | if (operands.size() > 2) { |
| 6370 | scopeId = operands[2]; |
| 6371 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 6372 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 6373 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6374 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6375 | // Check for capabilities |
| 6376 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6377 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 6378 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 6379 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 6380 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6381 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6382 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6383 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6384 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 6385 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6386 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6387 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6388 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 6389 | spvAtomicOperands.push_back(pointerId); |
| 6390 | spvAtomicOperands.push_back(scopeId); |
| 6391 | spvAtomicOperands.push_back(semanticsId); |
| 6392 | if (opCode == spv::OpAtomicCompareExchange) { |
| 6393 | spvAtomicOperands.push_back(semanticsId2); |
| 6394 | spvAtomicOperands.push_back(valueId); |
| 6395 | spvAtomicOperands.push_back(compareId); |
| 6396 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 6397 | spvAtomicOperands.push_back(valueId); |
| 6398 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6399 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6400 | if (opCode == spv::OpAtomicStore) { |
| 6401 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 6402 | return 0; |
| 6403 | } else { |
| 6404 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 6405 | |
| 6406 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 6407 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 6408 | if (op == glslang::EOpAtomicCounterDecrement) |
| 6409 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 6410 | |
| 6411 | return resultId; |
| 6412 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6413 | } |
| 6414 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6415 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6416 | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6417 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6418 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6419 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6420 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6421 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6422 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6423 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 6424 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6425 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 6426 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6427 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 6428 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6429 | } else if (op == glslang::EOpAnyInvocation || |
| 6430 | op == glslang::EOpAllInvocations || |
| 6431 | op == glslang::EOpAllInvocationsEqual) { |
| 6432 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 6433 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6434 | } else { |
| 6435 | builder.addCapability(spv::CapabilityGroups); |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6436 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6437 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6438 | op == glslang::EOpAddInvocationsNonUniform || |
| 6439 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6440 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6441 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 6442 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 6443 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 6444 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6445 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6446 | |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6447 | switch (op) { |
| 6448 | case glslang::EOpMinInvocations: |
| 6449 | case glslang::EOpMaxInvocations: |
| 6450 | case glslang::EOpAddInvocations: |
| 6451 | case glslang::EOpMinInvocationsNonUniform: |
| 6452 | case glslang::EOpMaxInvocationsNonUniform: |
| 6453 | case glslang::EOpAddInvocationsNonUniform: |
| 6454 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6455 | break; |
| 6456 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6457 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6458 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6459 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6460 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6461 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6462 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6463 | break; |
| 6464 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6465 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6466 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6467 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6468 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6469 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6470 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6471 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 6472 | default: |
| 6473 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6474 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6475 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6476 | spvGroupOperands.push_back(scope); |
| 6477 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6478 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6479 | spvGroupOperands.push_back(groupOp); |
| 6480 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6481 | } |
| 6482 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6483 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6484 | spv::IdImmediate op = { true, *opIt }; |
| 6485 | spvGroupOperands.push_back(op); |
| 6486 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6487 | |
| 6488 | switch (op) { |
| 6489 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6490 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6491 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6492 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6493 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6494 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6495 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6496 | opCode = spv::OpSubgroupAllEqualKHR; |
| 6497 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6498 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6499 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6500 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6501 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6502 | break; |
| 6503 | case glslang::EOpReadFirstInvocation: |
| 6504 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6505 | break; |
| 6506 | case glslang::EOpBallot: |
| 6507 | { |
| 6508 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6509 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6510 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6511 | // |
| 6512 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6513 | // |
| 6514 | spv::Id uintType = builder.makeUintType(32); |
| 6515 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6516 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6517 | |
| 6518 | std::vector<spv::Id> components; |
| 6519 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6520 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6521 | |
| 6522 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6523 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6524 | builder.createCompositeConstruct(uvec2Type, components)); |
| 6525 | } |
| 6526 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6527 | case glslang::EOpMinInvocations: |
| 6528 | case glslang::EOpMaxInvocations: |
| 6529 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6530 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6531 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6532 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6533 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6534 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6535 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6536 | if (op == glslang::EOpMinInvocations || |
| 6537 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 6538 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6539 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6540 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6541 | else { |
| 6542 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6543 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6544 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6545 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6546 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6547 | } else if (op == glslang::EOpMaxInvocations || |
| 6548 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 6549 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6550 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6551 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6552 | else { |
| 6553 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6554 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6555 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6556 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6557 | } |
| 6558 | } else { |
| 6559 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6560 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6561 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6562 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6563 | } |
| 6564 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6565 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6566 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6567 | |
| 6568 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6569 | case glslang::EOpMinInvocationsNonUniform: |
| 6570 | case glslang::EOpMaxInvocationsNonUniform: |
| 6571 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6572 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6573 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6574 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6575 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6576 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6577 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6578 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6579 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6580 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6581 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6582 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6583 | else { |
| 6584 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6585 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6586 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6587 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6588 | } |
| 6589 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6590 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 6591 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6592 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6593 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6594 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6595 | else { |
| 6596 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6597 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6598 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6599 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6600 | } |
| 6601 | } |
| 6602 | else { |
| 6603 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6604 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6605 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6606 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6607 | } |
| 6608 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6609 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6610 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6611 | |
| 6612 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6613 | default: |
| 6614 | logger->missingFunctionality("invocation operation"); |
| 6615 | return spv::NoResult; |
| 6616 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6617 | |
| 6618 | assert(opCode != spv::OpNop); |
| 6619 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6620 | } |
| 6621 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6622 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6623 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 6624 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6625 | { |
| 6626 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6627 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6628 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6629 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6630 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 6631 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 6632 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
| 6633 | |
| 6634 | // Handle group invocation operations scalar by scalar. |
| 6635 | // The result type is the same type as the original type. |
| 6636 | // The algorithm is to: |
| 6637 | // - break the vector into scalars |
| 6638 | // - apply the operation to each scalar |
| 6639 | // - make a vector out the scalar results |
| 6640 | |
| 6641 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6642 | int numComponents = builder.getNumComponents(operands[0]); |
| 6643 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6644 | std::vector<spv::Id> results; |
| 6645 | |
| 6646 | // do each scalar op |
| 6647 | for (int comp = 0; comp < numComponents; ++comp) { |
| 6648 | std::vector<unsigned int> indexes; |
| 6649 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6650 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 6651 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6652 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 6653 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6654 | spv::IdImmediate operand = { true, operands[1] }; |
| 6655 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6656 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6657 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6658 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6659 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6660 | spv::IdImmediate operand = { true, operands[1] }; |
| 6661 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6662 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6663 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6664 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6665 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6666 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6667 | spvGroupOperands.push_back(scalar); |
| 6668 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6669 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6670 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6671 | } |
| 6672 | |
| 6673 | // put the pieces together |
| 6674 | return builder.createCompositeConstruct(typeId, results); |
| 6675 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6676 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6677 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6678 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 6679 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6680 | { |
| 6681 | // Add the required capabilities. |
| 6682 | switch (op) { |
| 6683 | case glslang::EOpSubgroupElect: |
| 6684 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6685 | break; |
| 6686 | case glslang::EOpSubgroupAll: |
| 6687 | case glslang::EOpSubgroupAny: |
| 6688 | case glslang::EOpSubgroupAllEqual: |
| 6689 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6690 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 6691 | break; |
| 6692 | case glslang::EOpSubgroupBroadcast: |
| 6693 | case glslang::EOpSubgroupBroadcastFirst: |
| 6694 | case glslang::EOpSubgroupBallot: |
| 6695 | case glslang::EOpSubgroupInverseBallot: |
| 6696 | case glslang::EOpSubgroupBallotBitExtract: |
| 6697 | case glslang::EOpSubgroupBallotBitCount: |
| 6698 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6699 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6700 | case glslang::EOpSubgroupBallotFindLSB: |
| 6701 | case glslang::EOpSubgroupBallotFindMSB: |
| 6702 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6703 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 6704 | break; |
| 6705 | case glslang::EOpSubgroupShuffle: |
| 6706 | case glslang::EOpSubgroupShuffleXor: |
| 6707 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6708 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 6709 | break; |
| 6710 | case glslang::EOpSubgroupShuffleUp: |
| 6711 | case glslang::EOpSubgroupShuffleDown: |
| 6712 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6713 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 6714 | break; |
| 6715 | case glslang::EOpSubgroupAdd: |
| 6716 | case glslang::EOpSubgroupMul: |
| 6717 | case glslang::EOpSubgroupMin: |
| 6718 | case glslang::EOpSubgroupMax: |
| 6719 | case glslang::EOpSubgroupAnd: |
| 6720 | case glslang::EOpSubgroupOr: |
| 6721 | case glslang::EOpSubgroupXor: |
| 6722 | case glslang::EOpSubgroupInclusiveAdd: |
| 6723 | case glslang::EOpSubgroupInclusiveMul: |
| 6724 | case glslang::EOpSubgroupInclusiveMin: |
| 6725 | case glslang::EOpSubgroupInclusiveMax: |
| 6726 | case glslang::EOpSubgroupInclusiveAnd: |
| 6727 | case glslang::EOpSubgroupInclusiveOr: |
| 6728 | case glslang::EOpSubgroupInclusiveXor: |
| 6729 | case glslang::EOpSubgroupExclusiveAdd: |
| 6730 | case glslang::EOpSubgroupExclusiveMul: |
| 6731 | case glslang::EOpSubgroupExclusiveMin: |
| 6732 | case glslang::EOpSubgroupExclusiveMax: |
| 6733 | case glslang::EOpSubgroupExclusiveAnd: |
| 6734 | case glslang::EOpSubgroupExclusiveOr: |
| 6735 | case glslang::EOpSubgroupExclusiveXor: |
| 6736 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6737 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 6738 | break; |
| 6739 | case glslang::EOpSubgroupClusteredAdd: |
| 6740 | case glslang::EOpSubgroupClusteredMul: |
| 6741 | case glslang::EOpSubgroupClusteredMin: |
| 6742 | case glslang::EOpSubgroupClusteredMax: |
| 6743 | case glslang::EOpSubgroupClusteredAnd: |
| 6744 | case glslang::EOpSubgroupClusteredOr: |
| 6745 | case glslang::EOpSubgroupClusteredXor: |
| 6746 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6747 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 6748 | break; |
| 6749 | case glslang::EOpSubgroupQuadBroadcast: |
| 6750 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6751 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6752 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 6753 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6754 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 6755 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6756 | case glslang::EOpSubgroupPartitionedAdd: |
| 6757 | case glslang::EOpSubgroupPartitionedMul: |
| 6758 | case glslang::EOpSubgroupPartitionedMin: |
| 6759 | case glslang::EOpSubgroupPartitionedMax: |
| 6760 | case glslang::EOpSubgroupPartitionedAnd: |
| 6761 | case glslang::EOpSubgroupPartitionedOr: |
| 6762 | case glslang::EOpSubgroupPartitionedXor: |
| 6763 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6764 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6765 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6766 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6767 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6768 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6769 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6770 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6771 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6772 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6773 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6774 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6775 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6776 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6777 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 6778 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 6779 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6780 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6781 | } |
| 6782 | |
| 6783 | const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
| 6784 | const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
| 6785 | const bool isBool = typeProxy == glslang::EbtBool; |
| 6786 | |
| 6787 | spv::Op opCode = spv::OpNop; |
| 6788 | |
| 6789 | // Figure out which opcode to use. |
| 6790 | switch (op) { |
| 6791 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 6792 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 6793 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 6794 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 6795 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 6796 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 6797 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 6798 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 6799 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 6800 | case glslang::EOpSubgroupBallotBitCount: |
| 6801 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6802 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 6803 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 6804 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 6805 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 6806 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 6807 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 6808 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 6809 | case glslang::EOpSubgroupAdd: |
| 6810 | case glslang::EOpSubgroupInclusiveAdd: |
| 6811 | case glslang::EOpSubgroupExclusiveAdd: |
| 6812 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6813 | case glslang::EOpSubgroupPartitionedAdd: |
| 6814 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6815 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6816 | if (isFloat) { |
| 6817 | opCode = spv::OpGroupNonUniformFAdd; |
| 6818 | } else { |
| 6819 | opCode = spv::OpGroupNonUniformIAdd; |
| 6820 | } |
| 6821 | break; |
| 6822 | case glslang::EOpSubgroupMul: |
| 6823 | case glslang::EOpSubgroupInclusiveMul: |
| 6824 | case glslang::EOpSubgroupExclusiveMul: |
| 6825 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6826 | case glslang::EOpSubgroupPartitionedMul: |
| 6827 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6828 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6829 | if (isFloat) { |
| 6830 | opCode = spv::OpGroupNonUniformFMul; |
| 6831 | } else { |
| 6832 | opCode = spv::OpGroupNonUniformIMul; |
| 6833 | } |
| 6834 | break; |
| 6835 | case glslang::EOpSubgroupMin: |
| 6836 | case glslang::EOpSubgroupInclusiveMin: |
| 6837 | case glslang::EOpSubgroupExclusiveMin: |
| 6838 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6839 | case glslang::EOpSubgroupPartitionedMin: |
| 6840 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6841 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6842 | if (isFloat) { |
| 6843 | opCode = spv::OpGroupNonUniformFMin; |
| 6844 | } else if (isUnsigned) { |
| 6845 | opCode = spv::OpGroupNonUniformUMin; |
| 6846 | } else { |
| 6847 | opCode = spv::OpGroupNonUniformSMin; |
| 6848 | } |
| 6849 | break; |
| 6850 | case glslang::EOpSubgroupMax: |
| 6851 | case glslang::EOpSubgroupInclusiveMax: |
| 6852 | case glslang::EOpSubgroupExclusiveMax: |
| 6853 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6854 | case glslang::EOpSubgroupPartitionedMax: |
| 6855 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6856 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6857 | if (isFloat) { |
| 6858 | opCode = spv::OpGroupNonUniformFMax; |
| 6859 | } else if (isUnsigned) { |
| 6860 | opCode = spv::OpGroupNonUniformUMax; |
| 6861 | } else { |
| 6862 | opCode = spv::OpGroupNonUniformSMax; |
| 6863 | } |
| 6864 | break; |
| 6865 | case glslang::EOpSubgroupAnd: |
| 6866 | case glslang::EOpSubgroupInclusiveAnd: |
| 6867 | case glslang::EOpSubgroupExclusiveAnd: |
| 6868 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6869 | case glslang::EOpSubgroupPartitionedAnd: |
| 6870 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6871 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6872 | if (isBool) { |
| 6873 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 6874 | } else { |
| 6875 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 6876 | } |
| 6877 | break; |
| 6878 | case glslang::EOpSubgroupOr: |
| 6879 | case glslang::EOpSubgroupInclusiveOr: |
| 6880 | case glslang::EOpSubgroupExclusiveOr: |
| 6881 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6882 | case glslang::EOpSubgroupPartitionedOr: |
| 6883 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6884 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6885 | if (isBool) { |
| 6886 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 6887 | } else { |
| 6888 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 6889 | } |
| 6890 | break; |
| 6891 | case glslang::EOpSubgroupXor: |
| 6892 | case glslang::EOpSubgroupInclusiveXor: |
| 6893 | case glslang::EOpSubgroupExclusiveXor: |
| 6894 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6895 | case glslang::EOpSubgroupPartitionedXor: |
| 6896 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6897 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6898 | if (isBool) { |
| 6899 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 6900 | } else { |
| 6901 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 6902 | } |
| 6903 | break; |
| 6904 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 6905 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6906 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6907 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 6908 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6909 | } |
| 6910 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6911 | // get the right Group Operation |
| 6912 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6913 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6914 | default: |
| 6915 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6916 | case glslang::EOpSubgroupBallotBitCount: |
| 6917 | case glslang::EOpSubgroupAdd: |
| 6918 | case glslang::EOpSubgroupMul: |
| 6919 | case glslang::EOpSubgroupMin: |
| 6920 | case glslang::EOpSubgroupMax: |
| 6921 | case glslang::EOpSubgroupAnd: |
| 6922 | case glslang::EOpSubgroupOr: |
| 6923 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6924 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6925 | break; |
| 6926 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6927 | case glslang::EOpSubgroupInclusiveAdd: |
| 6928 | case glslang::EOpSubgroupInclusiveMul: |
| 6929 | case glslang::EOpSubgroupInclusiveMin: |
| 6930 | case glslang::EOpSubgroupInclusiveMax: |
| 6931 | case glslang::EOpSubgroupInclusiveAnd: |
| 6932 | case glslang::EOpSubgroupInclusiveOr: |
| 6933 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6934 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6935 | break; |
| 6936 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6937 | case glslang::EOpSubgroupExclusiveAdd: |
| 6938 | case glslang::EOpSubgroupExclusiveMul: |
| 6939 | case glslang::EOpSubgroupExclusiveMin: |
| 6940 | case glslang::EOpSubgroupExclusiveMax: |
| 6941 | case glslang::EOpSubgroupExclusiveAnd: |
| 6942 | case glslang::EOpSubgroupExclusiveOr: |
| 6943 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6944 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6945 | break; |
| 6946 | case glslang::EOpSubgroupClusteredAdd: |
| 6947 | case glslang::EOpSubgroupClusteredMul: |
| 6948 | case glslang::EOpSubgroupClusteredMin: |
| 6949 | case glslang::EOpSubgroupClusteredMax: |
| 6950 | case glslang::EOpSubgroupClusteredAnd: |
| 6951 | case glslang::EOpSubgroupClusteredOr: |
| 6952 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6953 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6954 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6955 | case glslang::EOpSubgroupPartitionedAdd: |
| 6956 | case glslang::EOpSubgroupPartitionedMul: |
| 6957 | case glslang::EOpSubgroupPartitionedMin: |
| 6958 | case glslang::EOpSubgroupPartitionedMax: |
| 6959 | case glslang::EOpSubgroupPartitionedAnd: |
| 6960 | case glslang::EOpSubgroupPartitionedOr: |
| 6961 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6962 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6963 | break; |
| 6964 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6965 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6966 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6967 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6968 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6969 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6970 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6971 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6972 | break; |
| 6973 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6974 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6975 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6976 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6977 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6978 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6979 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6980 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6981 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6982 | } |
| 6983 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6984 | // build the instruction |
| 6985 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 6986 | |
| 6987 | // Every operation begins with the Execution Scope operand. |
| 6988 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6989 | spvGroupOperands.push_back(executionScope); |
| 6990 | |
| 6991 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 6992 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6993 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6994 | spvGroupOperands.push_back(groupOperand); |
| 6995 | } |
| 6996 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6997 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6998 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 6999 | spv::IdImmediate operand = { true, *opIt }; |
| 7000 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7001 | } |
| 7002 | |
| 7003 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7004 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7005 | switch (op) { |
| 7006 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7007 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 7008 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 7009 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 7010 | } |
| 7011 | if (directionId != spv::NoResult) { |
| 7012 | spv::IdImmediate direction = { true, directionId }; |
| 7013 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7014 | } |
| 7015 | |
| 7016 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 7017 | } |
| 7018 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7019 | spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7020 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7021 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7022 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7023 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7024 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7025 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7026 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 7027 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7028 | spv::Id typeId0 = 0; |
| 7029 | if (consumedOperands > 0) |
| 7030 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7031 | spv::Id typeId1 = 0; |
| 7032 | if (consumedOperands > 1) |
| 7033 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7034 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7035 | |
| 7036 | switch (op) { |
| 7037 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7038 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7039 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7040 | else if (isUnsigned) |
| 7041 | libCall = spv::GLSLstd450UMin; |
| 7042 | else |
| 7043 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7044 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7045 | break; |
| 7046 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7047 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7048 | break; |
| 7049 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7050 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7051 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7052 | else if (isUnsigned) |
| 7053 | libCall = spv::GLSLstd450UMax; |
| 7054 | else |
| 7055 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7056 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7057 | break; |
| 7058 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7059 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7060 | break; |
| 7061 | case glslang::EOpDot: |
| 7062 | opCode = spv::OpDot; |
| 7063 | break; |
| 7064 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7065 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7066 | break; |
| 7067 | |
| 7068 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7069 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7070 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7071 | else if (isUnsigned) |
| 7072 | libCall = spv::GLSLstd450UClamp; |
| 7073 | else |
| 7074 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7075 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 7076 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7077 | break; |
| 7078 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7079 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 7080 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7081 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7082 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7083 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7084 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7085 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7086 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7087 | break; |
| 7088 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7089 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7090 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7091 | break; |
| 7092 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7093 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7094 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 7095 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7096 | break; |
| 7097 | |
| 7098 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7099 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7100 | break; |
| 7101 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7102 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7103 | break; |
| 7104 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7105 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7106 | break; |
| 7107 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7108 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7109 | break; |
| 7110 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7111 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7112 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7113 | #ifndef GLSLANG_WEB |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7114 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7115 | if (typeProxy == glslang::EbtFloat16) |
| 7116 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7117 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 7118 | break; |
| 7119 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7120 | if (typeProxy == glslang::EbtFloat16) |
| 7121 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7122 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 7123 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7124 | case glslang::EOpAddCarry: |
| 7125 | opCode = spv::OpIAddCarry; |
| 7126 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7127 | consumedOperands = 2; |
| 7128 | break; |
| 7129 | case glslang::EOpSubBorrow: |
| 7130 | opCode = spv::OpISubBorrow; |
| 7131 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7132 | consumedOperands = 2; |
| 7133 | break; |
| 7134 | case glslang::EOpUMulExtended: |
| 7135 | opCode = spv::OpUMulExtended; |
| 7136 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7137 | consumedOperands = 2; |
| 7138 | break; |
| 7139 | case glslang::EOpIMulExtended: |
| 7140 | opCode = spv::OpSMulExtended; |
| 7141 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7142 | consumedOperands = 2; |
| 7143 | break; |
| 7144 | case glslang::EOpBitfieldExtract: |
| 7145 | if (isUnsigned) |
| 7146 | opCode = spv::OpBitFieldUExtract; |
| 7147 | else |
| 7148 | opCode = spv::OpBitFieldSExtract; |
| 7149 | break; |
| 7150 | case glslang::EOpBitfieldInsert: |
| 7151 | opCode = spv::OpBitFieldInsert; |
| 7152 | break; |
| 7153 | |
| 7154 | case glslang::EOpFma: |
| 7155 | libCall = spv::GLSLstd450Fma; |
| 7156 | break; |
| 7157 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7158 | { |
| 7159 | libCall = spv::GLSLstd450FrexpStruct; |
| 7160 | assert(builder.isPointerType(typeId1)); |
| 7161 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7162 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 7163 | if (width == 16) |
| 7164 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 7165 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7166 | if (builder.getNumComponents(operands[0]) == 1) |
| 7167 | frexpIntType = builder.makeIntegerType(width, true); |
| 7168 | else |
| 7169 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 7170 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 7171 | consumedOperands = 1; |
| 7172 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7173 | break; |
| 7174 | case glslang::EOpLdexp: |
| 7175 | libCall = spv::GLSLstd450Ldexp; |
| 7176 | break; |
| 7177 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7178 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7179 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7180 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7181 | case glslang::EOpSubgroupBroadcast: |
| 7182 | case glslang::EOpSubgroupBallotBitExtract: |
| 7183 | case glslang::EOpSubgroupShuffle: |
| 7184 | case glslang::EOpSubgroupShuffleXor: |
| 7185 | case glslang::EOpSubgroupShuffleUp: |
| 7186 | case glslang::EOpSubgroupShuffleDown: |
| 7187 | case glslang::EOpSubgroupClusteredAdd: |
| 7188 | case glslang::EOpSubgroupClusteredMul: |
| 7189 | case glslang::EOpSubgroupClusteredMin: |
| 7190 | case glslang::EOpSubgroupClusteredMax: |
| 7191 | case glslang::EOpSubgroupClusteredAnd: |
| 7192 | case glslang::EOpSubgroupClusteredOr: |
| 7193 | case glslang::EOpSubgroupClusteredXor: |
| 7194 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7195 | case glslang::EOpSubgroupPartitionedAdd: |
| 7196 | case glslang::EOpSubgroupPartitionedMul: |
| 7197 | case glslang::EOpSubgroupPartitionedMin: |
| 7198 | case glslang::EOpSubgroupPartitionedMax: |
| 7199 | case glslang::EOpSubgroupPartitionedAnd: |
| 7200 | case glslang::EOpSubgroupPartitionedOr: |
| 7201 | case glslang::EOpSubgroupPartitionedXor: |
| 7202 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7203 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7204 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7205 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7206 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7207 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7208 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7209 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7210 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7211 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7212 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7213 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7214 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7215 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7216 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 7217 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7218 | case glslang::EOpSwizzleInvocations: |
| 7219 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7220 | libCall = spv::SwizzleInvocationsAMD; |
| 7221 | break; |
| 7222 | case glslang::EOpSwizzleInvocationsMasked: |
| 7223 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7224 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 7225 | break; |
| 7226 | case glslang::EOpWriteInvocation: |
| 7227 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7228 | libCall = spv::WriteInvocationAMD; |
| 7229 | break; |
| 7230 | |
| 7231 | case glslang::EOpMin3: |
| 7232 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7233 | if (isFloat) |
| 7234 | libCall = spv::FMin3AMD; |
| 7235 | else { |
| 7236 | if (isUnsigned) |
| 7237 | libCall = spv::UMin3AMD; |
| 7238 | else |
| 7239 | libCall = spv::SMin3AMD; |
| 7240 | } |
| 7241 | break; |
| 7242 | case glslang::EOpMax3: |
| 7243 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7244 | if (isFloat) |
| 7245 | libCall = spv::FMax3AMD; |
| 7246 | else { |
| 7247 | if (isUnsigned) |
| 7248 | libCall = spv::UMax3AMD; |
| 7249 | else |
| 7250 | libCall = spv::SMax3AMD; |
| 7251 | } |
| 7252 | break; |
| 7253 | case glslang::EOpMid3: |
| 7254 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7255 | if (isFloat) |
| 7256 | libCall = spv::FMid3AMD; |
| 7257 | else { |
| 7258 | if (isUnsigned) |
| 7259 | libCall = spv::UMid3AMD; |
| 7260 | else |
| 7261 | libCall = spv::SMid3AMD; |
| 7262 | } |
| 7263 | break; |
| 7264 | |
| 7265 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7266 | if (typeProxy == glslang::EbtFloat16) |
| 7267 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7268 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 7269 | libCall = spv::InterpolateAtVertexAMD; |
| 7270 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7271 | case glslang::EOpBarrier: |
| 7272 | { |
| 7273 | // This is for the extended controlBarrier function, with four operands. |
| 7274 | // The unextended barrier() goes through createNoArgOperation. |
| 7275 | assert(operands.size() == 4); |
| 7276 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 7277 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 7278 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 7279 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 7280 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7281 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7282 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7283 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7284 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7285 | } |
| 7286 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) { |
| 7287 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7288 | } |
| 7289 | return 0; |
| 7290 | } |
| 7291 | break; |
| 7292 | case glslang::EOpMemoryBarrier: |
| 7293 | { |
| 7294 | // This is for the extended memoryBarrier function, with three operands. |
| 7295 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 7296 | assert(operands.size() == 3); |
| 7297 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 7298 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 7299 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 7300 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7301 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7302 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7303 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7304 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7305 | } |
| 7306 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 7307 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7308 | } |
| 7309 | return 0; |
| 7310 | } |
| 7311 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7312 | |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7313 | case glslang::EOpReportIntersectionNV: |
| 7314 | { |
| 7315 | typeId = builder.makeBoolType(); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7316 | opCode = spv::OpReportIntersectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7317 | } |
| 7318 | break; |
| 7319 | case glslang::EOpTraceNV: |
| 7320 | { |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7321 | builder.createNoResultOp(spv::OpTraceNV, operands); |
| 7322 | return 0; |
| 7323 | } |
| 7324 | break; |
| 7325 | case glslang::EOpExecuteCallableNV: |
| 7326 | { |
| 7327 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7328 | return 0; |
| 7329 | } |
| 7330 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7331 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 7332 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 7333 | return 0; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 7334 | case glslang::EOpCooperativeMatrixMulAdd: |
| 7335 | opCode = spv::OpCooperativeMatrixMulAddNV; |
| 7336 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7337 | #endif // GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7338 | default: |
| 7339 | return 0; |
| 7340 | } |
| 7341 | |
| 7342 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7343 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 7344 | // Use an extended instruction from the standard library. |
| 7345 | // Construct the call arguments, without modifying the original operands vector. |
| 7346 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 7347 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7348 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 7349 | } else if (opCode == spv::OpDot && !isFloat) { |
| 7350 | // int dot(int, int) |
| 7351 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 7352 | const int componentCount = builder.getNumComponents(operands[0]); |
| 7353 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 7354 | builder.setPrecision(mulOp, precision); |
| 7355 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 7356 | for (int i = 1; i < componentCount; ++i) { |
| 7357 | builder.setPrecision(id, precision); |
| 7358 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(operands[0], typeId, i)); |
| 7359 | } |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7360 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7361 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7362 | case 0: |
| 7363 | // should all be handled by visitAggregate and createNoArgOperation |
| 7364 | assert(0); |
| 7365 | return 0; |
| 7366 | case 1: |
| 7367 | // should all be handled by createUnaryOperation |
| 7368 | assert(0); |
| 7369 | return 0; |
| 7370 | case 2: |
| 7371 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 7372 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7373 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7374 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 7375 | assert(consumedOperands == operands.size()); |
| 7376 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7377 | break; |
| 7378 | } |
| 7379 | } |
| 7380 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7381 | // Decode the return types that were structures |
| 7382 | switch (op) { |
| 7383 | case glslang::EOpAddCarry: |
| 7384 | case glslang::EOpSubBorrow: |
| 7385 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7386 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7387 | break; |
| 7388 | case glslang::EOpUMulExtended: |
| 7389 | case glslang::EOpIMulExtended: |
| 7390 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 7391 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7392 | break; |
| 7393 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7394 | { |
| 7395 | assert(operands.size() == 2); |
| 7396 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 7397 | // "exp" is floating-point type (from HLSL intrinsic) |
| 7398 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 7399 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 7400 | builder.createStore(member1, operands[1]); |
| 7401 | } else |
| 7402 | // "exp" is integer type (from GLSL built-in function) |
| 7403 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 7404 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7405 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7406 | break; |
| 7407 | default: |
| 7408 | break; |
| 7409 | } |
| 7410 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 7411 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7412 | } |
| 7413 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7414 | // Intrinsics with no arguments (or no return value, and no precision). |
| 7415 | spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7416 | { |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7417 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7418 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 7419 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7420 | |
| 7421 | switch (op) { |
| 7422 | case glslang::EOpEmitVertex: |
| 7423 | builder.createNoResultOp(spv::OpEmitVertex); |
| 7424 | return 0; |
| 7425 | case glslang::EOpEndPrimitive: |
| 7426 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 7427 | return 0; |
| 7428 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7429 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7430 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7431 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7432 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7433 | spv::MemorySemanticsAcquireReleaseMask); |
| 7434 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7435 | } else { |
| 7436 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 7437 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7438 | } else { |
| 7439 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7440 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7441 | spv::MemorySemanticsAcquireReleaseMask); |
| 7442 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7443 | return 0; |
| 7444 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7445 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 7446 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7447 | return 0; |
| 7448 | case glslang::EOpMemoryBarrierAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7449 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 7450 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7451 | return 0; |
| 7452 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7453 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 7454 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7455 | return 0; |
| 7456 | case glslang::EOpMemoryBarrierImage: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7457 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 7458 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7459 | return 0; |
| 7460 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7461 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7462 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7463 | return 0; |
| 7464 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7465 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 7466 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7467 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7468 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7469 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7470 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7471 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7472 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7473 | case glslang::EOpDeviceMemoryBarrier: |
| 7474 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7475 | spv::MemorySemanticsImageMemoryMask | |
| 7476 | spv::MemorySemanticsAcquireReleaseMask); |
| 7477 | return 0; |
| 7478 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 7479 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7480 | spv::MemorySemanticsImageMemoryMask | |
| 7481 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7482 | return 0; |
| 7483 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7484 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7485 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7486 | return 0; |
| 7487 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7488 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7489 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7490 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7491 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7492 | case glslang::EOpSubgroupBarrier: |
| 7493 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7494 | spv::MemorySemanticsAcquireReleaseMask); |
| 7495 | return spv::NoResult; |
| 7496 | case glslang::EOpSubgroupMemoryBarrier: |
| 7497 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7498 | spv::MemorySemanticsAcquireReleaseMask); |
| 7499 | return spv::NoResult; |
| 7500 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 7501 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 7502 | spv::MemorySemanticsAcquireReleaseMask); |
| 7503 | return spv::NoResult; |
| 7504 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 7505 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 7506 | spv::MemorySemanticsAcquireReleaseMask); |
| 7507 | return spv::NoResult; |
| 7508 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 7509 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7510 | spv::MemorySemanticsAcquireReleaseMask); |
| 7511 | return spv::NoResult; |
| 7512 | case glslang::EOpSubgroupElect: { |
| 7513 | std::vector<spv::Id> operands; |
| 7514 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 7515 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7516 | case glslang::EOpTime: |
| 7517 | { |
| 7518 | std::vector<spv::Id> args; // Dummy arguments |
| 7519 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 7520 | return builder.setPrecision(id, precision); |
| 7521 | } |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7522 | case glslang::EOpIgnoreIntersectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7523 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7524 | return 0; |
| 7525 | case glslang::EOpTerminateRayNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7526 | builder.createNoResultOp(spv::OpTerminateRayNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7527 | return 0; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 7528 | |
| 7529 | case glslang::EOpBeginInvocationInterlock: |
| 7530 | builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
| 7531 | return 0; |
| 7532 | case glslang::EOpEndInvocationInterlock: |
| 7533 | builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
| 7534 | return 0; |
| 7535 | |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 7536 | case glslang::EOpIsHelperInvocation: |
| 7537 | { |
| 7538 | std::vector<spv::Id> args; // Dummy arguments |
Rex Xu | bb7307b | 2019-07-15 14:57:20 +0800 | [diff] [blame] | 7539 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 7540 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 7541 | return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args); |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 7542 | } |
| 7543 | |
amhagan | 91fb009 | 2019-07-10 21:14:38 -0400 | [diff] [blame] | 7544 | case glslang::EOpReadClockSubgroupKHR: { |
| 7545 | std::vector<spv::Id> args; |
| 7546 | args.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 7547 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 7548 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 7549 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 7550 | } |
| 7551 | |
| 7552 | case glslang::EOpReadClockDeviceKHR: { |
| 7553 | std::vector<spv::Id> args; |
| 7554 | args.push_back(builder.makeUintConstant(spv::ScopeDevice)); |
| 7555 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 7556 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 7557 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 7558 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7559 | default: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7560 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7561 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7562 | #endif |
| 7563 | |
| 7564 | logger->missingFunctionality("unknown operation with no arguments"); |
| 7565 | |
| 7566 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7567 | } |
| 7568 | |
| 7569 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 7570 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 7571 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7572 | spv::Id id; |
| 7573 | if (symbolValues.end() != iter) { |
| 7574 | id = iter->second; |
| 7575 | return id; |
| 7576 | } |
| 7577 | |
| 7578 | // it was not found, create it |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7579 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
| 7580 | auto forcedType = getForcedType(builtIn, symbol->getType()); |
| 7581 | id = createSpvVariable(symbol, forcedType.first); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7582 | symbolValues[symbol->getId()] = id; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7583 | if (forcedType.second != spv::NoType) |
| 7584 | forceType[id] = forcedType.second; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7585 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 7586 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7587 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 7588 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 7589 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7590 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7591 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
| 7592 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7593 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7594 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7595 | if (symbol->getQualifier().hasIndex()) |
| 7596 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 7597 | if (symbol->getQualifier().hasComponent()) |
| 7598 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 7599 | // atomic counters use this: |
| 7600 | if (symbol->getQualifier().hasOffset()) |
| 7601 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7602 | } |
| 7603 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 7604 | if (symbol->getQualifier().hasLocation()) |
| 7605 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7606 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 7607 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7608 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7609 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7610 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7611 | if (symbol->getQualifier().hasSet()) |
| 7612 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7613 | else if (IsDescriptorResource(symbol->getType())) { |
| 7614 | // default to 0 |
| 7615 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 7616 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7617 | if (symbol->getQualifier().hasBinding()) |
| 7618 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
Jeff Bolz | 0a93cfb | 2018-12-11 20:53:59 -0600 | [diff] [blame] | 7619 | else if (IsDescriptorResource(symbol->getType())) { |
| 7620 | // default to 0 |
| 7621 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 7622 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7623 | if (symbol->getQualifier().hasAttachment()) |
| 7624 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 7625 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7626 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7627 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7628 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7629 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7630 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 7631 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 7632 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 7633 | } |
| 7634 | if (symbol->getQualifier().hasXfbOffset()) |
| 7635 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7636 | } |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 7637 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7638 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7639 | if (symbol->getType().isImage()) { |
| 7640 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7641 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7642 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7643 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7644 | } |
| 7645 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7646 | // add built-in variable decoration |
| 7647 | if (builtIn != spv::BuiltInMax) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7648 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7649 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7650 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 7651 | // nonuniform |
| 7652 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 7653 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7654 | #ifndef GLSLANG_WEB |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7655 | if (builtIn == spv::BuiltInSampleMask) { |
| 7656 | spv::Decoration decoration; |
| 7657 | // GL_NV_sample_mask_override_coverage extension |
| 7658 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7659 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7660 | else |
| 7661 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7662 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7663 | if (decoration != spv::DecorationMax) { |
Jason Macnak | dbd4c3c | 2019-07-12 14:33:02 -0700 | [diff] [blame] | 7664 | builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7665 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 7666 | } |
| 7667 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7668 | else if (builtIn == spv::BuiltInLayer) { |
| 7669 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7670 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7671 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7672 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 7673 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 7674 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7675 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7676 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 7677 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7678 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 7679 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 7680 | } |
| 7681 | } |
| 7682 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7683 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7684 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7685 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7686 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 7687 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 7688 | if (symbol->getQualifier().pervertexNV) { |
| 7689 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 7690 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 7691 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 7692 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7693 | #endif |
| 7694 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7695 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 7696 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 7697 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 7698 | symbol->getType().getQualifier().semanticName); |
| 7699 | } |
| 7700 | |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 7701 | if (symbol->isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7702 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 7703 | } |
| 7704 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7705 | return id; |
| 7706 | } |
| 7707 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7708 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7709 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 7710 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 7711 | { |
| 7712 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7713 | if (qualifier.perPrimitiveNV) { |
| 7714 | // Need to add capability/extension for fragment shader. |
| 7715 | // Mesh shader already adds this by default. |
| 7716 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7717 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7718 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7719 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7720 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7721 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7722 | if (qualifier.perViewNV) |
| 7723 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 7724 | if (qualifier.perTaskNV) |
| 7725 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 7726 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7727 | if (qualifier.perPrimitiveNV) { |
| 7728 | // Need to add capability/extension for fragment shader. |
| 7729 | // Mesh shader already adds this by default. |
| 7730 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7731 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7732 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7733 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7734 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7735 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7736 | if (qualifier.perViewNV) |
| 7737 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 7738 | if (qualifier.perTaskNV) |
| 7739 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 7740 | } |
| 7741 | } |
| 7742 | #endif |
| 7743 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7744 | // Make a full tree of instructions to build a SPIR-V specialization constant, |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7745 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7746 | // |
| 7747 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 7748 | // |
| 7749 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 7750 | // regular constants, which themselves are trees that createSpvConstant() |
| 7751 | // recursively walks. So, this function walks the "top" of the tree: |
| 7752 | // - emit specialization constant-building instructions for specConstant |
| 7753 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7754 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7755 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 7756 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7757 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7758 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7759 | if (! node.getQualifier().specConstant) { |
| 7760 | // hand off to the non-spec-constant path |
| 7761 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 7762 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7763 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7764 | nextConst, false); |
| 7765 | } |
| 7766 | |
| 7767 | // We now know we have a specialization constant to build |
| 7768 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7769 | #ifndef GLSLANG_WEB |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 7770 | // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants, |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7771 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 7772 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 7773 | std::vector<spv::Id> dimConstId; |
| 7774 | for (int dim = 0; dim < 3; ++dim) { |
| 7775 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 7776 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7777 | if (specConst) { |
| 7778 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 7779 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 7780 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7781 | } |
| 7782 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 7783 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7784 | #endif |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7785 | |
| 7786 | // An AST node labelled as specialization constant should be a symbol node. |
| 7787 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 7788 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7789 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7790 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 7791 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 7792 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 7793 | // will set the builder into spec constant op instruction generating mode. |
| 7794 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7795 | result = accessChainLoad(sub_tree->getType()); |
| 7796 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7797 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7798 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7799 | } else { |
| 7800 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7801 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7802 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7803 | builder.addName(result, sn->getName().c_str()); |
| 7804 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7805 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7806 | |
| 7807 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 7808 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7809 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7810 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7811 | } |
| 7812 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7813 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 7814 | // build the aggregate SPIR-V constant. |
| 7815 | // |
| 7816 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 7817 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 7818 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7819 | spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7820 | { |
| 7821 | // vector of constants for SPIR-V |
| 7822 | std::vector<spv::Id> spvConsts; |
| 7823 | |
| 7824 | // Type is used for struct and array constants |
| 7825 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 7826 | |
| 7827 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7828 | glslang::TType elementType(glslangType, 0); |
| 7829 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7830 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7831 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7832 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7833 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7834 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 7835 | } else if (glslangType.isCoopMat()) { |
| 7836 | glslang::TType componentType(glslangType.getBasicType()); |
| 7837 | spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7838 | } else if (glslangType.isStruct()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7839 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 7840 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7841 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 7842 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7843 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 7844 | bool zero = nextConst >= consts.size(); |
| 7845 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 7846 | case glslang::EbtInt: |
| 7847 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 7848 | break; |
| 7849 | case glslang::EbtUint: |
| 7850 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 7851 | break; |
| 7852 | case glslang::EbtFloat: |
| 7853 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7854 | break; |
| 7855 | case glslang::EbtBool: |
| 7856 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 7857 | break; |
| 7858 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7859 | case glslang::EbtInt8: |
| 7860 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 7861 | break; |
| 7862 | case glslang::EbtUint8: |
| 7863 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 7864 | break; |
| 7865 | case glslang::EbtInt16: |
| 7866 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 7867 | break; |
| 7868 | case glslang::EbtUint16: |
| 7869 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 7870 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7871 | case glslang::EbtInt64: |
| 7872 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 7873 | break; |
| 7874 | case glslang::EbtUint64: |
| 7875 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 7876 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7877 | case glslang::EbtDouble: |
| 7878 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 7879 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7880 | case glslang::EbtFloat16: |
| 7881 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7882 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 7883 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7884 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7885 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7886 | break; |
| 7887 | } |
| 7888 | ++nextConst; |
| 7889 | } |
| 7890 | } else { |
| 7891 | // we have a non-aggregate (scalar) constant |
| 7892 | bool zero = nextConst >= consts.size(); |
| 7893 | spv::Id scalar = 0; |
| 7894 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 7895 | case glslang::EbtInt: |
| 7896 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
| 7897 | break; |
| 7898 | case glslang::EbtUint: |
| 7899 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
| 7900 | break; |
| 7901 | case glslang::EbtFloat: |
| 7902 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 7903 | break; |
| 7904 | case glslang::EbtBool: |
| 7905 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
| 7906 | break; |
| 7907 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7908 | case glslang::EbtInt8: |
| 7909 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 7910 | break; |
| 7911 | case glslang::EbtUint8: |
| 7912 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 7913 | break; |
| 7914 | case glslang::EbtInt16: |
| 7915 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 7916 | break; |
| 7917 | case glslang::EbtUint16: |
| 7918 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 7919 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7920 | case glslang::EbtInt64: |
| 7921 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 7922 | break; |
| 7923 | case glslang::EbtUint64: |
| 7924 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7925 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7926 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7927 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7928 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7929 | case glslang::EbtFloat16: |
| 7930 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 7931 | break; |
Jeff Bolz | 3fd1232 | 2019-03-05 23:27:09 -0600 | [diff] [blame] | 7932 | case glslang::EbtReference: |
| 7933 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7934 | scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar); |
| 7935 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 7936 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7937 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7938 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7939 | break; |
| 7940 | } |
| 7941 | ++nextConst; |
| 7942 | return scalar; |
| 7943 | } |
| 7944 | |
| 7945 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 7946 | } |
| 7947 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7948 | // Return true if the node is a constant or symbol whose reading has no |
| 7949 | // non-trivial observable cost or effect. |
| 7950 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 7951 | { |
| 7952 | // don't know what this is |
| 7953 | if (node == nullptr) |
| 7954 | return false; |
| 7955 | |
| 7956 | // a constant is safe |
| 7957 | if (node->getAsConstantUnion() != nullptr) |
| 7958 | return true; |
| 7959 | |
| 7960 | // not a symbol means non-trivial |
| 7961 | if (node->getAsSymbolNode() == nullptr) |
| 7962 | return false; |
| 7963 | |
| 7964 | // a symbol, depends on what's being read |
| 7965 | switch (node->getType().getQualifier().storage) { |
| 7966 | case glslang::EvqTemporary: |
| 7967 | case glslang::EvqGlobal: |
| 7968 | case glslang::EvqIn: |
| 7969 | case glslang::EvqInOut: |
| 7970 | case glslang::EvqConst: |
| 7971 | case glslang::EvqConstReadOnly: |
| 7972 | case glslang::EvqUniform: |
| 7973 | return true; |
| 7974 | default: |
| 7975 | return false; |
| 7976 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 7977 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7978 | |
| 7979 | // A node is trivial if it is a single operation with no side effects. |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 7980 | // HLSL (and/or vectors) are always trivial, as it does not short circuit. |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 7981 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7982 | // Return true if trivial. |
| 7983 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 7984 | { |
| 7985 | if (node == nullptr) |
| 7986 | return false; |
| 7987 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 7988 | // count non scalars as trivial, as well as anything coming from HLSL |
| 7989 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 7990 | return true; |
| 7991 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7992 | // symbols and constants are trivial |
| 7993 | if (isTrivialLeaf(node)) |
| 7994 | return true; |
| 7995 | |
| 7996 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 7997 | |
| 7998 | // not a simple operation |
| 7999 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 8000 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 8001 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 8002 | return false; |
| 8003 | |
| 8004 | // not on leaf nodes |
| 8005 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 8006 | return false; |
| 8007 | |
| 8008 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 8009 | return false; |
| 8010 | } |
| 8011 | |
| 8012 | switch (node->getAsOperator()->getOp()) { |
| 8013 | case glslang::EOpLogicalNot: |
| 8014 | case glslang::EOpConvIntToBool: |
| 8015 | case glslang::EOpConvUintToBool: |
| 8016 | case glslang::EOpConvFloatToBool: |
| 8017 | case glslang::EOpConvDoubleToBool: |
| 8018 | case glslang::EOpEqual: |
| 8019 | case glslang::EOpNotEqual: |
| 8020 | case glslang::EOpLessThan: |
| 8021 | case glslang::EOpGreaterThan: |
| 8022 | case glslang::EOpLessThanEqual: |
| 8023 | case glslang::EOpGreaterThanEqual: |
| 8024 | case glslang::EOpIndexDirect: |
| 8025 | case glslang::EOpIndexDirectStruct: |
| 8026 | case glslang::EOpLogicalXor: |
| 8027 | case glslang::EOpAny: |
| 8028 | case glslang::EOpAll: |
| 8029 | return true; |
| 8030 | default: |
| 8031 | return false; |
| 8032 | } |
| 8033 | } |
| 8034 | |
| 8035 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 8036 | // the left side is true (for &&) or false (for ||). |
| 8037 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 8038 | { |
| 8039 | spv::Id boolTypeId = builder.makeBoolType(); |
| 8040 | |
| 8041 | // emit left operand |
| 8042 | builder.clearAccessChain(); |
| 8043 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8044 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8045 | |
| 8046 | // Operands to accumulate OpPhi operands |
| 8047 | std::vector<spv::Id> phiOperands; |
| 8048 | // accumulate left operand's phi information |
| 8049 | phiOperands.push_back(leftId); |
| 8050 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8051 | |
| 8052 | // Make the two kinds of operation symmetric with a "!" |
| 8053 | // || => emit "if (! left) result = right" |
| 8054 | // && => emit "if ( left) result = right" |
| 8055 | // |
| 8056 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 8057 | // to 'builder' to have an "else" without an "then" |
| 8058 | if (op == glslang::EOpLogicalOr) |
| 8059 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 8060 | |
| 8061 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 8062 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8063 | |
| 8064 | // emit right operand as the "then" part of the "if" |
| 8065 | builder.clearAccessChain(); |
| 8066 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8067 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8068 | |
| 8069 | // accumulate left operand's phi information |
| 8070 | phiOperands.push_back(rightId); |
| 8071 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8072 | |
| 8073 | // finish the "if" |
| 8074 | ifBuilder.makeEndIf(); |
| 8075 | |
| 8076 | // phi together the two results |
| 8077 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 8078 | } |
| 8079 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8080 | #ifndef GLSLANG_WEB |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8081 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 8082 | // Import this set if it has not been imported yet. |
| 8083 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 8084 | { |
| 8085 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 8086 | return extBuiltinMap[name]; |
| 8087 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 8088 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8089 | spv::Id extBuiltins = builder.import(name); |
| 8090 | extBuiltinMap[name] = extBuiltins; |
| 8091 | return extBuiltins; |
| 8092 | } |
| 8093 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 8094 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8095 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8096 | }; // end anonymous namespace |
| 8097 | |
| 8098 | namespace glslang { |
| 8099 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8100 | void GetSpirvVersion(std::string& version) |
| 8101 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 8102 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 8103 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8104 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8105 | version = buf; |
| 8106 | } |
| 8107 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8108 | // For low-order part of the generator's magic number. Bump up |
| 8109 | // when there is a change in the style (e.g., if SSA form changes, |
| 8110 | // or a different instruction sequence to do something gets used). |
| 8111 | int GetSpirvGeneratorVersion() |
| 8112 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 8113 | // return 1; // start |
| 8114 | // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V |
John Kessenich | 71b5da6 | 2018-02-06 08:06:36 -0700 | [diff] [blame] | 8115 | // return 3; // change/correct barrier-instruction operands, to match memory model group decisions |
John Kessenich | 0216f24 | 2018-03-03 11:47:07 -0700 | [diff] [blame] | 8116 | // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component |
John Kessenich | ac37079 | 2018-03-07 11:24:50 -0700 | [diff] [blame] | 8117 | // return 5; // make OpArrayLength result type be an int with signedness of 0 |
John Kessenich | d6c9755 | 2018-06-04 15:33:31 -0600 | [diff] [blame] | 8118 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 8119 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
| 8120 | return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8121 | } |
| 8122 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8123 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8124 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8125 | { |
| 8126 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8127 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8128 | if (out.fail()) |
| 8129 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8130 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 8131 | unsigned int word = spirv[i]; |
| 8132 | out.write((const char*)&word, 4); |
| 8133 | } |
| 8134 | out.close(); |
| 8135 | } |
| 8136 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8137 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 8138 | void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName) |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8139 | { |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8140 | #ifndef GLSLANG_WEB |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8141 | std::ofstream out; |
| 8142 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8143 | if (out.fail()) |
| 8144 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 8145 | out << "\t// " << |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8146 | GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL << |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 8147 | std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8148 | if (varName != nullptr) { |
| 8149 | out << "\t #pragma once" << std::endl; |
| 8150 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 8151 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8152 | const int WORDS_PER_LINE = 8; |
| 8153 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 8154 | out << "\t"; |
| 8155 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 8156 | const unsigned int word = spirv[i + j]; |
| 8157 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 8158 | if (i + j + 1 < (int)spirv.size()) { |
| 8159 | out << ","; |
| 8160 | } |
| 8161 | } |
| 8162 | out << std::endl; |
| 8163 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8164 | if (varName != nullptr) { |
| 8165 | out << "};"; |
| 8166 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8167 | out.close(); |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8168 | #endif |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8169 | } |
| 8170 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8171 | // |
| 8172 | // Set up the glslang traversal |
| 8173 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8174 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8175 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8176 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8177 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8178 | } |
| 8179 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8180 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8181 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8182 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8183 | TIntermNode* root = intermediate.getTreeRoot(); |
| 8184 | |
| 8185 | if (root == 0) |
| 8186 | return; |
| 8187 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8188 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8189 | if (options == nullptr) |
| 8190 | options = &defaultOptions; |
| 8191 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8192 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8193 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 8194 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8195 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 8196 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8197 | it.dumpSpv(spirv); |
| 8198 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 8199 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8200 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 8201 | // eg. forward and remove memory writes of opaque types. |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8202 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
| 8203 | if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer) { |
John Kessenich | e7df8e0 | 2018-08-22 17:12:46 -0600 | [diff] [blame] | 8204 | SpirvToolsLegalize(intermediate, spirv, logger, options); |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8205 | prelegalization = false; |
| 8206 | } |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8207 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8208 | if (options->validate) |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8209 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8210 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8211 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8212 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8213 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8214 | #endif |
| 8215 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8216 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8217 | } |
| 8218 | |
| 8219 | }; // end namespace glslang |