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 | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 49 | #ifdef AMD_EXTENSIONS |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 50 | #include "GLSL.ext.AMD.h" |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 51 | #endif |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 52 | #include "GLSL.ext.NV.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 53 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 54 | |
| 55 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 56 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 57 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 58 | #include "../glslang/Include/Common.h" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 59 | #include "../glslang/Include/revision.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 60 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 61 | #include <fstream> |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 62 | #include <iomanip> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 63 | #include <list> |
| 64 | #include <map> |
| 65 | #include <stack> |
| 66 | #include <string> |
| 67 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 68 | |
| 69 | namespace { |
| 70 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 71 | namespace { |
| 72 | class SpecConstantOpModeGuard { |
| 73 | public: |
| 74 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 75 | : builder_(builder) { |
| 76 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 77 | } |
| 78 | ~SpecConstantOpModeGuard() { |
| 79 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 80 | : builder_->setToNormalCodeGenMode(); |
| 81 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 82 | void turnOnSpecConstantOpMode() { |
| 83 | builder_->setToSpecConstCodeGenMode(); |
| 84 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | spv::Builder* builder_; |
| 88 | bool previous_flag_; |
| 89 | }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 90 | |
| 91 | struct OpDecorations { |
| 92 | spv::Decoration precision; |
| 93 | spv::Decoration noContraction; |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 94 | spv::Decoration nonUniform; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | } // namespace |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 98 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 99 | // |
| 100 | // The main holder of information for translating glslang to SPIR-V. |
| 101 | // |
| 102 | // Derives from the AST walking base class. |
| 103 | // |
| 104 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 105 | public: |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 106 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 107 | glslang::SpvOptions& options); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 108 | virtual ~TGlslangToSpvTraverser() { } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 109 | |
| 110 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 111 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 112 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 113 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 114 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 115 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 116 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 117 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 118 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 119 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 120 | void finishSpv(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 121 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 122 | |
| 123 | protected: |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 124 | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
| 125 | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
| 126 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 127 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 128 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 129 | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 130 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 131 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 132 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 133 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 134 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 135 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 136 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 137 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 138 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 139 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 140 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 141 | spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 142 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 143 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 144 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 145 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 146 | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 147 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 148 | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 149 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 150 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 151 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 152 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 153 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 154 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 155 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 156 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 157 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 158 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 159 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 160 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 161 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 162 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 163 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 164 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 165 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 166 | bool writableParam(glslang::TStorageQualifier) const; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 167 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 168 | void makeFunctions(const glslang::TIntermSequence&); |
| 169 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 170 | void visitFunctions(const glslang::TIntermSequence&); |
| 171 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 172 | 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] | 173 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 174 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 175 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 176 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 177 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 178 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 179 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 180 | 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] | 181 | glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 182 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 183 | glslang::TBasicType typeProxy); |
| 184 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 185 | glslang::TBasicType typeProxy); |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 186 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 187 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 188 | 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] | 189 | 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] | 190 | 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] | 191 | 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] | 192 | 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] | 193 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 194 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 195 | #ifdef NV_EXTENSIONS |
| 196 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
| 197 | #endif |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 198 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 199 | 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] | 200 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 201 | bool isTrivial(const glslang::TIntermTyped* node); |
| 202 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 203 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 204 | spv::Id getExtBuiltins(const char* name); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 205 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 206 | void addPre13Extension(const char* ext) |
| 207 | { |
| 208 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 209 | builder.addExtension(ext); |
| 210 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 211 | std::pair<spv::Id, spv::Id> getForcedType(spv::BuiltIn, const glslang::TType&); |
| 212 | spv::Id translateForcedType(spv::Id object); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 213 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 214 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 215 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 216 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 217 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 218 | int sequenceDepth; |
| 219 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 220 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 221 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 222 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 223 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 224 | bool inEntryPoint; |
| 225 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 226 | 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] | 227 | 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] | 228 | const glslang::TIntermediate* glslangIntermediate; |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 229 | 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] | 230 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 231 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 232 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 233 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 234 | 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] | 235 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 236 | 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] | 237 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 238 | std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 239 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 240 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 241 | // Map pointee types for EbtReference to their forward pointers |
| 242 | std::map<const glslang::TType *, spv::Id> forwardPointers; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 243 | // Type forcing, for when SPIR-V wants a different type than the AST, |
| 244 | // requiring local translation to and from SPIR-V type on every access. |
| 245 | // Maps <builtin-variable-id -> AST-required-type-id> |
| 246 | std::unordered_map<spv::Id, spv::Id> forceType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | // |
| 250 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 251 | // |
| 252 | |
| 253 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 254 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 255 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 256 | switch (source) { |
| 257 | case glslang::EShSourceGlsl: |
| 258 | switch (profile) { |
| 259 | case ENoProfile: |
| 260 | case ECoreProfile: |
| 261 | case ECompatibilityProfile: |
| 262 | return spv::SourceLanguageGLSL; |
| 263 | case EEsProfile: |
| 264 | return spv::SourceLanguageESSL; |
| 265 | default: |
| 266 | return spv::SourceLanguageUnknown; |
| 267 | } |
| 268 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 269 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 270 | default: |
| 271 | return spv::SourceLanguageUnknown; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // Translate glslang language (stage) to SPIR-V execution model. |
| 276 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 277 | { |
| 278 | switch (stage) { |
| 279 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 280 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 281 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 282 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 283 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 284 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 285 | #ifdef NV_EXTENSIONS |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 286 | case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV; |
| 287 | case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV; |
| 288 | case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV; |
| 289 | case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV; |
| 290 | case EShLangMissNV: return spv::ExecutionModelMissNV; |
| 291 | case EShLangCallableNV: return spv::ExecutionModelCallableNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 292 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 293 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 294 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 295 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 296 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 297 | return spv::ExecutionModelFragment; |
| 298 | } |
| 299 | } |
| 300 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 301 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 302 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 303 | { |
| 304 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 305 | case glslang::Esd1D: return spv::Dim1D; |
| 306 | case glslang::Esd2D: return spv::Dim2D; |
| 307 | case glslang::Esd3D: return spv::Dim3D; |
| 308 | case glslang::EsdCube: return spv::DimCube; |
| 309 | case glslang::EsdRect: return spv::DimRect; |
| 310 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 311 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 312 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 313 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 314 | return spv::Dim2D; |
| 315 | } |
| 316 | } |
| 317 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 318 | // Translate glslang precision to SPIR-V precision decorations. |
| 319 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 320 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 321 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 322 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 323 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 324 | default: |
| 325 | return spv::NoPrecision; |
| 326 | } |
| 327 | } |
| 328 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 329 | // Translate glslang type to SPIR-V precision decorations. |
| 330 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 331 | { |
| 332 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 333 | } |
| 334 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 335 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 336 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 337 | { |
| 338 | if (type.getBasicType() == glslang::EbtBlock) { |
| 339 | switch (type.getQualifier().storage) { |
| 340 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 341 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 342 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 343 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 344 | #ifdef NV_EXTENSIONS |
| 345 | case glslang::EvqPayloadNV: return spv::DecorationBlock; |
| 346 | case glslang::EvqPayloadInNV: return spv::DecorationBlock; |
| 347 | case glslang::EvqHitAttrNV: return spv::DecorationBlock; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 348 | case glslang::EvqCallableDataNV: return spv::DecorationBlock; |
| 349 | case glslang::EvqCallableDataInNV: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 350 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 351 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 352 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 357 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 358 | } |
| 359 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 360 | // Translate glslang type to SPIR-V memory decorations. |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 361 | 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] | 362 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 363 | if (!useVulkanMemoryModel) { |
| 364 | if (qualifier.coherent) |
| 365 | memory.push_back(spv::DecorationCoherent); |
| 366 | if (qualifier.volatil) { |
| 367 | memory.push_back(spv::DecorationVolatile); |
| 368 | memory.push_back(spv::DecorationCoherent); |
| 369 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 370 | } |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 371 | if (qualifier.restrict) |
| 372 | memory.push_back(spv::DecorationRestrict); |
| 373 | if (qualifier.readonly) |
| 374 | memory.push_back(spv::DecorationNonWritable); |
| 375 | if (qualifier.writeonly) |
| 376 | memory.push_back(spv::DecorationNonReadable); |
| 377 | } |
| 378 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 379 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 380 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 381 | { |
| 382 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 383 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 384 | case glslang::ElmRowMajor: |
| 385 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 386 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 387 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 388 | default: |
| 389 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 390 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 391 | } |
| 392 | } else { |
| 393 | switch (type.getBasicType()) { |
| 394 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 395 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 396 | break; |
| 397 | case glslang::EbtBlock: |
| 398 | switch (type.getQualifier().storage) { |
| 399 | case glslang::EvqUniform: |
| 400 | case glslang::EvqBuffer: |
| 401 | switch (type.getQualifier().layoutPacking) { |
| 402 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 403 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 404 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 405 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 406 | } |
| 407 | case glslang::EvqVaryingIn: |
| 408 | case glslang::EvqVaryingOut: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 409 | if (type.getQualifier().isTaskMemory()) { |
| 410 | switch (type.getQualifier().layoutPacking) { |
| 411 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 412 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 413 | default: break; |
| 414 | } |
| 415 | } else { |
| 416 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 417 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 418 | return spv::DecorationMax; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 419 | #ifdef NV_EXTENSIONS |
| 420 | case glslang::EvqPayloadNV: |
| 421 | case glslang::EvqPayloadInNV: |
| 422 | case glslang::EvqHitAttrNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 423 | case glslang::EvqCallableDataNV: |
| 424 | case glslang::EvqCallableDataInNV: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 425 | return spv::DecorationMax; |
| 426 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 427 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 428 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 429 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 436 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 437 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 438 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 439 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 440 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 441 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 442 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 443 | else if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 444 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 445 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 446 | return spv::DecorationFlat; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 447 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 448 | else if (qualifier.explicitInterp) { |
| 449 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 450 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 451 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 452 | #endif |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 453 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 454 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 458 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 459 | // should be applied. |
| 460 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 461 | { |
| 462 | if (qualifier.patch) |
| 463 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 464 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 465 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 466 | else if (qualifier.sample) { |
| 467 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 468 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 469 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 470 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 471 | } |
| 472 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 473 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 474 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 475 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 476 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 477 | return spv::DecorationInvariant; |
| 478 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 479 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 480 | } |
| 481 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 482 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 483 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 484 | { |
| 485 | if (qualifier.noContraction) |
| 486 | return spv::DecorationNoContraction; |
| 487 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 488 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 489 | } |
| 490 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 491 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 492 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 493 | { |
| 494 | if (qualifier.isNonUniform()) { |
| 495 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 496 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 497 | return spv::DecorationNonUniformEXT; |
| 498 | } else |
| 499 | return spv::DecorationMax; |
| 500 | } |
| 501 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 502 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 503 | { |
| 504 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) { |
| 505 | return spv::MemoryAccessMaskNone; |
| 506 | } |
| 507 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
| 508 | if (coherentFlags.volatil || |
| 509 | coherentFlags.coherent || |
| 510 | coherentFlags.devicecoherent || |
| 511 | coherentFlags.queuefamilycoherent || |
| 512 | coherentFlags.workgroupcoherent || |
| 513 | coherentFlags.subgroupcoherent) { |
| 514 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 515 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 516 | } |
| 517 | if (coherentFlags.nonprivate) { |
| 518 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 519 | } |
| 520 | if (coherentFlags.volatil) { |
| 521 | mask = mask | spv::MemoryAccessVolatileMask; |
| 522 | } |
| 523 | if (mask != spv::MemoryAccessMaskNone) { |
| 524 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 525 | } |
| 526 | return mask; |
| 527 | } |
| 528 | |
| 529 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 530 | { |
| 531 | if (!glslangIntermediate->usingVulkanMemoryModel()) { |
| 532 | return spv::ImageOperandsMaskNone; |
| 533 | } |
| 534 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 535 | if (coherentFlags.volatil || |
| 536 | coherentFlags.coherent || |
| 537 | coherentFlags.devicecoherent || |
| 538 | coherentFlags.queuefamilycoherent || |
| 539 | coherentFlags.workgroupcoherent || |
| 540 | coherentFlags.subgroupcoherent) { |
| 541 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 542 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 543 | } |
| 544 | if (coherentFlags.nonprivate) { |
| 545 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 546 | } |
| 547 | if (coherentFlags.volatil) { |
| 548 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 549 | } |
| 550 | if (mask != spv::ImageOperandsMaskNone) { |
| 551 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 552 | } |
| 553 | return mask; |
| 554 | } |
| 555 | |
| 556 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 557 | { |
| 558 | spv::Builder::AccessChain::CoherentFlags flags; |
| 559 | flags.coherent = type.getQualifier().coherent; |
| 560 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 561 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 562 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 563 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 564 | type.getQualifier().storage == glslang::EvqShared; |
| 565 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 566 | flags.volatil = type.getQualifier().volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 567 | // *coherent variables are implicitly nonprivate in GLSL |
| 568 | flags.nonprivate = type.getQualifier().nonprivate || |
Jeff Bolz | ab3c965 | 2018-10-15 22:46:48 -0500 | [diff] [blame] | 569 | flags.subgroupcoherent || |
| 570 | flags.workgroupcoherent || |
| 571 | flags.queuefamilycoherent || |
| 572 | flags.devicecoherent || |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 573 | flags.coherent || |
| 574 | flags.volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 575 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
| 576 | return flags; |
| 577 | } |
| 578 | |
| 579 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 580 | { |
| 581 | spv::Scope scope; |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 582 | if (coherentFlags.volatil || coherentFlags.coherent) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 583 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 584 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 585 | } else if (coherentFlags.devicecoherent) { |
| 586 | scope = spv::ScopeDevice; |
| 587 | } else if (coherentFlags.queuefamilycoherent) { |
| 588 | scope = spv::ScopeQueueFamilyKHR; |
| 589 | } else if (coherentFlags.workgroupcoherent) { |
| 590 | scope = spv::ScopeWorkgroup; |
| 591 | } else if (coherentFlags.subgroupcoherent) { |
| 592 | scope = spv::ScopeSubgroup; |
| 593 | } else { |
| 594 | scope = spv::ScopeMax; |
| 595 | } |
| 596 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 597 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 598 | } |
| 599 | return scope; |
| 600 | } |
| 601 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 602 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 603 | // associated capabilities when required. For some built-in variables, a capability |
| 604 | // is generated only when using the variable in an executable instruction, but not when |
| 605 | // just declaring a struct member variable with it. This is true for PointSize, |
| 606 | // ClipDistance, and CullDistance. |
| 607 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 608 | { |
| 609 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 610 | case glslang::EbvPointSize: |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 611 | // Defer adding the capability until the built-in is actually used. |
| 612 | if (! memberDeclaration) { |
| 613 | switch (glslangIntermediate->getStage()) { |
| 614 | case EShLangGeometry: |
| 615 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 616 | break; |
| 617 | case EShLangTessControl: |
| 618 | case EShLangTessEvaluation: |
| 619 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 620 | break; |
| 621 | default: |
| 622 | break; |
| 623 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 624 | } |
| 625 | return spv::BuiltInPointSize; |
| 626 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 627 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 628 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 629 | // They are now generated when used, rather than here when declared. |
| 630 | // Potentially, the specification should be more clear what the minimum |
| 631 | // use needed is to trigger the capability. |
| 632 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 633 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 634 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 635 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 636 | return spv::BuiltInClipDistance; |
| 637 | |
| 638 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 639 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 640 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 641 | return spv::BuiltInCullDistance; |
| 642 | |
| 643 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 644 | builder.addCapability(spv::CapabilityMultiViewport); |
| 645 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 646 | glslangIntermediate->getStage() == EShLangTessControl || |
| 647 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 648 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 649 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 650 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 651 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 652 | return spv::BuiltInViewportIndex; |
| 653 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 654 | case glslang::EbvSampleId: |
| 655 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 656 | return spv::BuiltInSampleId; |
| 657 | |
| 658 | case glslang::EbvSamplePosition: |
| 659 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 660 | return spv::BuiltInSamplePosition; |
| 661 | |
| 662 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 663 | return spv::BuiltInSampleMask; |
| 664 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 665 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 666 | #ifdef NV_EXTENSIONS |
| 667 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 668 | return spv::BuiltInLayer; |
| 669 | } |
| 670 | #endif |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 671 | builder.addCapability(spv::CapabilityGeometry); |
| 672 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 673 | glslangIntermediate->getStage() == EShLangTessControl || |
| 674 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 675 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 676 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 677 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 678 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 679 | return spv::BuiltInLayer; |
| 680 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 681 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 682 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 683 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 684 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 685 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 686 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 687 | case glslang::EbvBaseVertex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 688 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 689 | builder.addCapability(spv::CapabilityDrawParameters); |
| 690 | return spv::BuiltInBaseVertex; |
| 691 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 692 | case glslang::EbvBaseInstance: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 693 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 694 | builder.addCapability(spv::CapabilityDrawParameters); |
| 695 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 696 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 697 | case glslang::EbvDrawId: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 698 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 699 | builder.addCapability(spv::CapabilityDrawParameters); |
| 700 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 701 | |
| 702 | case glslang::EbvPrimitiveId: |
| 703 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 704 | builder.addCapability(spv::CapabilityGeometry); |
| 705 | return spv::BuiltInPrimitiveId; |
| 706 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 707 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 708 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 709 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 710 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 711 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 712 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 713 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 714 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 715 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 716 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 717 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 718 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 719 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 720 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 721 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 722 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 723 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 724 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 725 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 726 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 727 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 728 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 729 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 730 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 731 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 732 | return spv::BuiltInSubgroupSize; |
| 733 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 734 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 735 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 736 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 737 | return spv::BuiltInSubgroupLocalInvocationId; |
| 738 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 739 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 740 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 741 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 742 | return spv::BuiltInSubgroupEqMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 743 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 744 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 745 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 746 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 747 | return spv::BuiltInSubgroupGeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 748 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 749 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 750 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 751 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 752 | return spv::BuiltInSubgroupGtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 753 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 754 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 755 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 756 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 757 | return spv::BuiltInSubgroupLeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 758 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 759 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 760 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 761 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 762 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 763 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 764 | case glslang::EbvNumSubgroups: |
| 765 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 766 | return spv::BuiltInNumSubgroups; |
| 767 | |
| 768 | case glslang::EbvSubgroupID: |
| 769 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 770 | return spv::BuiltInSubgroupId; |
| 771 | |
| 772 | case glslang::EbvSubgroupSize2: |
| 773 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 774 | return spv::BuiltInSubgroupSize; |
| 775 | |
| 776 | case glslang::EbvSubgroupInvocation2: |
| 777 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 778 | return spv::BuiltInSubgroupLocalInvocationId; |
| 779 | |
| 780 | case glslang::EbvSubgroupEqMask2: |
| 781 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 782 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 783 | return spv::BuiltInSubgroupEqMask; |
| 784 | |
| 785 | case glslang::EbvSubgroupGeMask2: |
| 786 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 787 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 788 | return spv::BuiltInSubgroupGeMask; |
| 789 | |
| 790 | case glslang::EbvSubgroupGtMask2: |
| 791 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 792 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 793 | return spv::BuiltInSubgroupGtMask; |
| 794 | |
| 795 | case glslang::EbvSubgroupLeMask2: |
| 796 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 797 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 798 | return spv::BuiltInSubgroupLeMask; |
| 799 | |
| 800 | case glslang::EbvSubgroupLtMask2: |
| 801 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 802 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 803 | return spv::BuiltInSubgroupLtMask; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 804 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 805 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 806 | case glslang::EbvBaryCoordNoPersp: |
| 807 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 808 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 809 | |
| 810 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 811 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 812 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 813 | |
| 814 | case glslang::EbvBaryCoordNoPerspSample: |
| 815 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 816 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 817 | |
| 818 | case glslang::EbvBaryCoordSmooth: |
| 819 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 820 | return spv::BuiltInBaryCoordSmoothAMD; |
| 821 | |
| 822 | case glslang::EbvBaryCoordSmoothCentroid: |
| 823 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 824 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 825 | |
| 826 | case glslang::EbvBaryCoordSmoothSample: |
| 827 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 828 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 829 | |
| 830 | case glslang::EbvBaryCoordPullModel: |
| 831 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 832 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 833 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 834 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 835 | case glslang::EbvDeviceIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 836 | addPre13Extension(spv::E_SPV_KHR_device_group); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 837 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 838 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 839 | |
| 840 | case glslang::EbvViewIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 841 | addPre13Extension(spv::E_SPV_KHR_multiview); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 842 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 843 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 844 | |
Daniel Koch | 5154db5 | 2018-11-26 10:01:58 -0500 | [diff] [blame] | 845 | case glslang::EbvFragSizeEXT: |
| 846 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 847 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 848 | return spv::BuiltInFragSizeEXT; |
| 849 | |
| 850 | case glslang::EbvFragInvocationCountEXT: |
| 851 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 852 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 853 | return spv::BuiltInFragInvocationCountEXT; |
| 854 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 855 | #ifdef NV_EXTENSIONS |
| 856 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 857 | if (!memberDeclaration) { |
| 858 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 859 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 860 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 861 | return spv::BuiltInViewportMaskNV; |
| 862 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 863 | if (!memberDeclaration) { |
| 864 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 865 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 866 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 867 | return spv::BuiltInSecondaryPositionNV; |
| 868 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 869 | if (!memberDeclaration) { |
| 870 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 871 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 872 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 873 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 874 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 875 | if (!memberDeclaration) { |
| 876 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 877 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 878 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 879 | return spv::BuiltInPositionPerViewNV; |
| 880 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 881 | if (!memberDeclaration) { |
| 882 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 883 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 884 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 885 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 886 | case glslang::EbvFragFullyCoveredNV: |
| 887 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 888 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 889 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 890 | case glslang::EbvFragmentSizeNV: |
| 891 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 892 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 893 | return spv::BuiltInFragmentSizeNV; |
| 894 | case glslang::EbvInvocationsPerPixelNV: |
| 895 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 896 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 897 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 898 | |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 899 | // ray tracing |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 900 | case glslang::EbvLaunchIdNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 901 | return spv::BuiltInLaunchIdNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 902 | case glslang::EbvLaunchSizeNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 903 | return spv::BuiltInLaunchSizeNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 904 | case glslang::EbvWorldRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 905 | return spv::BuiltInWorldRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 906 | case glslang::EbvWorldRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 907 | return spv::BuiltInWorldRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 908 | case glslang::EbvObjectRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 909 | return spv::BuiltInObjectRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 910 | case glslang::EbvObjectRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 911 | return spv::BuiltInObjectRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 912 | case glslang::EbvRayTminNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 913 | return spv::BuiltInRayTminNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 914 | case glslang::EbvRayTmaxNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 915 | return spv::BuiltInRayTmaxNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 916 | case glslang::EbvInstanceCustomIndexNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 917 | return spv::BuiltInInstanceCustomIndexNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 918 | case glslang::EbvHitTNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 919 | return spv::BuiltInHitTNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 920 | case glslang::EbvHitKindNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 921 | return spv::BuiltInHitKindNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 922 | case glslang::EbvObjectToWorldNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 923 | return spv::BuiltInObjectToWorldNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 924 | case glslang::EbvWorldToObjectNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 925 | return spv::BuiltInWorldToObjectNV; |
| 926 | case glslang::EbvIncomingRayFlagsNV: |
| 927 | return spv::BuiltInIncomingRayFlagsNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 928 | |
| 929 | // barycentrics |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 930 | case glslang::EbvBaryCoordNV: |
| 931 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 932 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 933 | return spv::BuiltInBaryCoordNV; |
| 934 | case glslang::EbvBaryCoordNoPerspNV: |
| 935 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 936 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 937 | return spv::BuiltInBaryCoordNoPerspNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 938 | |
| 939 | // mesh shaders |
| 940 | case glslang::EbvTaskCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 941 | return spv::BuiltInTaskCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 942 | case glslang::EbvPrimitiveCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 943 | return spv::BuiltInPrimitiveCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 944 | case glslang::EbvPrimitiveIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 945 | return spv::BuiltInPrimitiveIndicesNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 946 | case glslang::EbvClipDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 947 | return spv::BuiltInClipDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 948 | case glslang::EbvCullDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 949 | return spv::BuiltInCullDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 950 | case glslang::EbvLayerPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 951 | return spv::BuiltInLayerPerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 952 | case glslang::EbvMeshViewCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 953 | return spv::BuiltInMeshViewCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 954 | case glslang::EbvMeshViewIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 955 | return spv::BuiltInMeshViewIndicesNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 956 | #endif |
Daniel Koch | 2cb2f19 | 2019-06-04 08:43:32 -0400 | [diff] [blame] | 957 | |
| 958 | // sm builtins |
| 959 | case glslang::EbvWarpsPerSM: |
| 960 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 961 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 962 | return spv::BuiltInWarpsPerSMNV; |
| 963 | case glslang::EbvSMCount: |
| 964 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 965 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 966 | return spv::BuiltInSMCountNV; |
| 967 | case glslang::EbvWarpID: |
| 968 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 969 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 970 | return spv::BuiltInWarpIDNV; |
| 971 | case glslang::EbvSMID: |
| 972 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 973 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 974 | return spv::BuiltInSMIDNV; |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 975 | default: |
| 976 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 980 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 981 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 982 | { |
| 983 | assert(type.getBasicType() == glslang::EbtSampler); |
| 984 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 985 | // Check for capabilities |
| 986 | switch (type.getQualifier().layoutFormat) { |
| 987 | case glslang::ElfRg32f: |
| 988 | case glslang::ElfRg16f: |
| 989 | case glslang::ElfR11fG11fB10f: |
| 990 | case glslang::ElfR16f: |
| 991 | case glslang::ElfRgba16: |
| 992 | case glslang::ElfRgb10A2: |
| 993 | case glslang::ElfRg16: |
| 994 | case glslang::ElfRg8: |
| 995 | case glslang::ElfR16: |
| 996 | case glslang::ElfR8: |
| 997 | case glslang::ElfRgba16Snorm: |
| 998 | case glslang::ElfRg16Snorm: |
| 999 | case glslang::ElfRg8Snorm: |
| 1000 | case glslang::ElfR16Snorm: |
| 1001 | case glslang::ElfR8Snorm: |
| 1002 | |
| 1003 | case glslang::ElfRg32i: |
| 1004 | case glslang::ElfRg16i: |
| 1005 | case glslang::ElfRg8i: |
| 1006 | case glslang::ElfR16i: |
| 1007 | case glslang::ElfR8i: |
| 1008 | |
| 1009 | case glslang::ElfRgb10a2ui: |
| 1010 | case glslang::ElfRg32ui: |
| 1011 | case glslang::ElfRg16ui: |
| 1012 | case glslang::ElfRg8ui: |
| 1013 | case glslang::ElfR16ui: |
| 1014 | case glslang::ElfR8ui: |
| 1015 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 1016 | break; |
| 1017 | |
| 1018 | default: |
| 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1023 | switch (type.getQualifier().layoutFormat) { |
| 1024 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1025 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1026 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1027 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1028 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1029 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1030 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1031 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1032 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1033 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1034 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1035 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1036 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1037 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1038 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1039 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1040 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1041 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1042 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1043 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1044 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1045 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1046 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1047 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1048 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1049 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1050 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1051 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1052 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1053 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1054 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1055 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1056 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1057 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1058 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1059 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1060 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1061 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1062 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1063 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1064 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1068 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1069 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1070 | if (selectionNode.getFlatten()) |
| 1071 | return spv::SelectionControlFlattenMask; |
| 1072 | if (selectionNode.getDontFlatten()) |
| 1073 | return spv::SelectionControlDontFlattenMask; |
| 1074 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1075 | } |
| 1076 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1077 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1078 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1079 | if (switchNode.getFlatten()) |
| 1080 | return spv::SelectionControlFlattenMask; |
| 1081 | if (switchNode.getDontFlatten()) |
| 1082 | return spv::SelectionControlDontFlattenMask; |
| 1083 | return spv::SelectionControlMaskNone; |
| 1084 | } |
| 1085 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1086 | // return a non-0 dependency if the dependency argument must be set |
| 1087 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1088 | std::vector<unsigned int>& operands) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1089 | { |
| 1090 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1091 | |
| 1092 | if (loopNode.getDontUnroll()) |
| 1093 | control = control | spv::LoopControlDontUnrollMask; |
| 1094 | if (loopNode.getUnroll()) |
| 1095 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1096 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1097 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1098 | else if (loopNode.getLoopDependency() > 0) { |
| 1099 | control = control | spv::LoopControlDependencyLengthMask; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1100 | operands.push_back((unsigned int)loopNode.getLoopDependency()); |
| 1101 | } |
| 1102 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 1103 | if (loopNode.getMinIterations() > 0) { |
| 1104 | control = control | spv::LoopControlMinIterationsMask; |
| 1105 | operands.push_back(loopNode.getMinIterations()); |
| 1106 | } |
| 1107 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
| 1108 | control = control | spv::LoopControlMaxIterationsMask; |
| 1109 | operands.push_back(loopNode.getMaxIterations()); |
| 1110 | } |
| 1111 | if (loopNode.getIterationMultiple() > 1) { |
| 1112 | control = control | spv::LoopControlIterationMultipleMask; |
| 1113 | operands.push_back(loopNode.getIterationMultiple()); |
| 1114 | } |
| 1115 | if (loopNode.getPeelCount() > 0) { |
| 1116 | control = control | spv::LoopControlPeelCountMask; |
| 1117 | operands.push_back(loopNode.getPeelCount()); |
| 1118 | } |
| 1119 | if (loopNode.getPartialCount() > 0) { |
| 1120 | control = control | spv::LoopControlPartialCountMask; |
| 1121 | operands.push_back(loopNode.getPartialCount()); |
| 1122 | } |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1123 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1124 | |
| 1125 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1126 | } |
| 1127 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1128 | // Translate glslang type to SPIR-V storage class. |
| 1129 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1130 | { |
| 1131 | if (type.getQualifier().isPipeInput()) |
| 1132 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1133 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1134 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1135 | |
| 1136 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
| 1137 | type.getQualifier().storage == glslang::EvqUniform) { |
| 1138 | if (type.getBasicType() == glslang::EbtAtomicUint) |
| 1139 | return spv::StorageClassAtomicCounter; |
| 1140 | if (type.containsOpaque()) |
| 1141 | return spv::StorageClassUniformConstant; |
| 1142 | } |
| 1143 | |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1144 | #ifdef NV_EXTENSIONS |
| 1145 | if (type.getQualifier().isUniformOrBuffer() && |
| 1146 | type.getQualifier().layoutShaderRecordNV) { |
| 1147 | return spv::StorageClassShaderRecordBufferNV; |
| 1148 | } |
| 1149 | #endif |
| 1150 | |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1151 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1152 | addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class); |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1153 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1157 | if (type.getQualifier().layoutPushConstant) |
| 1158 | return spv::StorageClassPushConstant; |
| 1159 | if (type.getBasicType() == glslang::EbtBlock) |
| 1160 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1161 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1162 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1163 | |
| 1164 | switch (type.getQualifier().storage) { |
| 1165 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
| 1166 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1167 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1168 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1169 | #ifdef NV_EXTENSIONS |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1170 | case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV; |
| 1171 | case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV; |
| 1172 | case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV; |
| 1173 | case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV; |
| 1174 | case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1175 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1176 | default: |
| 1177 | assert(0); |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1182 | } |
| 1183 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1184 | // Add capabilities pertaining to how an array is indexed. |
| 1185 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1186 | const glslang::TType& indexType) |
| 1187 | { |
| 1188 | if (indexType.getQualifier().isNonUniform()) { |
| 1189 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1190 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1191 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1192 | if (baseType.getQualifier().hasAttachment()) |
| 1193 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
| 1194 | else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1195 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
| 1196 | else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1197 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1198 | else if (baseType.isImage()) |
| 1199 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1200 | else if (baseType.isTexture()) |
| 1201 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1202 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1203 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1204 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1205 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1206 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1207 | } |
| 1208 | } else { |
| 1209 | // assume a dynamically uniform index |
| 1210 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1211 | if (baseType.getQualifier().hasAttachment()) { |
| 1212 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1213 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1214 | } else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1215 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1216 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1217 | } else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1218 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1219 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1220 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1225 | // 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] | 1226 | // descriptor set. |
| 1227 | bool IsDescriptorResource(const glslang::TType& type) |
| 1228 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1229 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1230 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1231 | return type.getQualifier().isUniformOrBuffer() && |
| 1232 | #ifdef NV_EXTENSIONS |
| 1233 | ! type.getQualifier().layoutShaderRecordNV && |
| 1234 | #endif |
| 1235 | ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1236 | |
| 1237 | // non block... |
| 1238 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1239 | // if they are the global-scope-class, not the function parameter |
| 1240 | // (or local, if they ever exist) class. |
| 1241 | if (type.getBasicType() == glslang::EbtSampler) |
| 1242 | return type.getQualifier().isUniformOrBuffer(); |
| 1243 | |
| 1244 | // None of the above. |
| 1245 | return false; |
| 1246 | } |
| 1247 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1248 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1249 | { |
| 1250 | if (child.layoutMatrix == glslang::ElmNone) |
| 1251 | child.layoutMatrix = parent.layoutMatrix; |
| 1252 | |
| 1253 | if (parent.invariant) |
| 1254 | child.invariant = true; |
| 1255 | if (parent.nopersp) |
| 1256 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1257 | #ifdef AMD_EXTENSIONS |
| 1258 | if (parent.explicitInterp) |
| 1259 | child.explicitInterp = true; |
| 1260 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1261 | if (parent.flat) |
| 1262 | child.flat = true; |
| 1263 | if (parent.centroid) |
| 1264 | child.centroid = true; |
| 1265 | if (parent.patch) |
| 1266 | child.patch = true; |
| 1267 | if (parent.sample) |
| 1268 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1269 | if (parent.coherent) |
| 1270 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1271 | if (parent.devicecoherent) |
| 1272 | child.devicecoherent = true; |
| 1273 | if (parent.queuefamilycoherent) |
| 1274 | child.queuefamilycoherent = true; |
| 1275 | if (parent.workgroupcoherent) |
| 1276 | child.workgroupcoherent = true; |
| 1277 | if (parent.subgroupcoherent) |
| 1278 | child.subgroupcoherent = true; |
| 1279 | if (parent.nonprivate) |
| 1280 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1281 | if (parent.volatil) |
| 1282 | child.volatil = true; |
| 1283 | if (parent.restrict) |
| 1284 | child.restrict = true; |
| 1285 | if (parent.readonly) |
| 1286 | child.readonly = true; |
| 1287 | if (parent.writeonly) |
| 1288 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1289 | #ifdef NV_EXTENSIONS |
| 1290 | if (parent.perPrimitiveNV) |
| 1291 | child.perPrimitiveNV = true; |
| 1292 | if (parent.perViewNV) |
| 1293 | child.perViewNV = true; |
| 1294 | if (parent.perTaskNV) |
| 1295 | child.perTaskNV = true; |
| 1296 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1299 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1300 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1301 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1302 | // - struct members might inherit from a struct declaration |
| 1303 | // (note that non-block structs don't explicitly inherit, |
| 1304 | // only implicitly, meaning no decoration involved) |
| 1305 | // - affect decorations on the struct members |
| 1306 | // (note smooth does not, and expecting something like volatile |
| 1307 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1308 | // - 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] | 1309 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1312 | // |
| 1313 | // Implement the TGlslangToSpvTraverser class. |
| 1314 | // |
| 1315 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1316 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1317 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 1318 | : TIntermTraverser(true, false, true), |
| 1319 | options(options), |
| 1320 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1321 | sequenceDepth(0), logger(buildLogger), |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1322 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1323 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 1324 | glslangIntermediate(glslangIntermediate), |
| 1325 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1326 | { |
| 1327 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1328 | |
| 1329 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1330 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1331 | glslangIntermediate->getVersion()); |
| 1332 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1333 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1334 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1335 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1336 | |
| 1337 | // Set the source shader's text. If for SPV version 1.0, include |
| 1338 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1339 | // Otherwise, emit those as actual instructions. |
| 1340 | std::string text; |
| 1341 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1342 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1343 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1344 | text.append("// OpModuleProcessed "); |
| 1345 | text.append(processes[p]); |
| 1346 | text.append("\n"); |
| 1347 | } else |
| 1348 | builder.addModuleProcessed(processes[p]); |
| 1349 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1350 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1351 | text.append("#line 1\n"); |
| 1352 | text.append(glslangIntermediate->getSourceText()); |
| 1353 | builder.setSourceText(text); |
Greg Fischer | d445bb2 | 2018-12-06 11:13:15 -0700 | [diff] [blame] | 1354 | // Pass name and text for all included files |
| 1355 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1356 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1357 | builder.addInclude(iItr->first, iItr->second); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1358 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1359 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1360 | |
| 1361 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1362 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1363 | |
| 1364 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1365 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
| 1366 | builder.addExtension(spv::E_SPV_EXT_physical_storage_buffer); |
| 1367 | builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
| 1368 | }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1369 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1370 | memoryModel = spv::MemoryModelVulkanKHR; |
| 1371 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1372 | builder.addExtension(spv::E_SPV_KHR_vulkan_memory_model); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1373 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1374 | builder.setMemoryModel(addressingModel, memoryModel); |
| 1375 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 1376 | if (glslangIntermediate->usingVariablePointers()) { |
| 1377 | builder.addCapability(spv::CapabilityVariablePointers); |
| 1378 | } |
| 1379 | |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1380 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1381 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1382 | |
| 1383 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1384 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1385 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1386 | builder.addSourceExtension(it->c_str()); |
| 1387 | |
| 1388 | // Add the top-level modes for this shader. |
| 1389 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1390 | if (glslangIntermediate->getXfbMode()) { |
| 1391 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1392 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1393 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1394 | |
| 1395 | unsigned int mode; |
| 1396 | switch (glslangIntermediate->getStage()) { |
| 1397 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1398 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1399 | break; |
| 1400 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1401 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1402 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1403 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1404 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1405 | glslang::TLayoutGeometry primitive; |
| 1406 | |
| 1407 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1408 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1409 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1410 | } else { |
| 1411 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1412 | } |
| 1413 | |
| 1414 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1415 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1416 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1417 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1418 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1419 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1420 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1421 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1422 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1423 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1424 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1425 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1426 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1427 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1428 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1429 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1430 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1431 | |
| 1432 | switch (glslangIntermediate->getVertexOrder()) { |
| 1433 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1434 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1435 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1436 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1437 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1438 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1439 | |
| 1440 | if (glslangIntermediate->getPointMode()) |
| 1441 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1442 | break; |
| 1443 | |
| 1444 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1445 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1446 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1447 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1448 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1449 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1450 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1451 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1452 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1453 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1454 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1455 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1456 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1457 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1458 | |
| 1459 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1460 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1461 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1462 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1463 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1464 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1465 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1466 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1467 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1468 | break; |
| 1469 | |
| 1470 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1471 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1472 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1473 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1474 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1475 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1476 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1477 | else |
| 1478 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1479 | |
| 1480 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1481 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1482 | |
chaoc | c120452 | 2017-06-30 17:14:30 -0700 | [diff] [blame] | 1483 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1484 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1485 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1486 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1487 | } |
| 1488 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1489 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1490 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1491 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1492 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1493 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1494 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1495 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1496 | |
| 1497 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1498 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 1499 | |
| 1500 | switch (glslangIntermediate->getInterlockOrdering()) { |
| 1501 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; break; |
| 1502 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; break; |
| 1503 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; break; |
| 1504 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; break; |
| 1505 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; break; |
| 1506 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; break; |
| 1507 | default: mode = spv::ExecutionModeMax; break; |
| 1508 | } |
| 1509 | if (mode != spv::ExecutionModeMax) { |
| 1510 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1511 | if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
| 1512 | mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
| 1513 | builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
| 1514 | } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
| 1515 | mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
| 1516 | builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT); |
| 1517 | } else { |
| 1518 | builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT); |
| 1519 | } |
| 1520 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 1521 | } |
| 1522 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1523 | break; |
| 1524 | |
| 1525 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1526 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 1527 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1528 | glslangIntermediate->getLocalSize(1), |
| 1529 | glslangIntermediate->getLocalSize(2)); |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 1530 | #ifdef NV_EXTENSIONS |
| 1531 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1532 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1533 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1534 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1535 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1536 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1537 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1538 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1539 | } |
| 1540 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1541 | break; |
| 1542 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1543 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1544 | case EShLangRayGenNV: |
| 1545 | case EShLangIntersectNV: |
| 1546 | case EShLangAnyHitNV: |
| 1547 | case EShLangClosestHitNV: |
| 1548 | case EShLangMissNV: |
| 1549 | case EShLangCallableNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1550 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1551 | builder.addExtension("SPV_NV_ray_tracing"); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1552 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1553 | case EShLangTaskNV: |
| 1554 | case EShLangMeshNV: |
| 1555 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1556 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1557 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1558 | glslangIntermediate->getLocalSize(1), |
| 1559 | glslangIntermediate->getLocalSize(2)); |
| 1560 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 1561 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1562 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives()); |
| 1563 | |
| 1564 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1565 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1566 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1567 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1568 | default: mode = spv::ExecutionModeMax; break; |
| 1569 | } |
| 1570 | if (mode != spv::ExecutionModeMax) |
| 1571 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1572 | } |
| 1573 | break; |
| 1574 | #endif |
| 1575 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1576 | default: |
| 1577 | break; |
| 1578 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1579 | } |
| 1580 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1581 | // Finish creating SPV, after the traversal is complete. |
| 1582 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1583 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1584 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1585 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1586 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1587 | builder.leaveFunction(); |
| 1588 | } |
| 1589 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1590 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1591 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1592 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1593 | |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1594 | // Add capabilities, extensions, remove unneeded decorations, etc., |
| 1595 | // based on the resulting SPIR-V. |
| 1596 | builder.postProcess(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1599 | // Write the SPV into 'out'. |
| 1600 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1601 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1602 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // |
| 1606 | // Implement the traversal functions. |
| 1607 | // |
| 1608 | // Return true from interior nodes to have the external traversal |
| 1609 | // continue on to children. Return false if children were |
| 1610 | // already processed. |
| 1611 | // |
| 1612 | |
| 1613 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1614 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1615 | // - uniform/input reads |
| 1616 | // - output writes |
| 1617 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1618 | // - something simple that degenerates into the last bullet |
| 1619 | // |
| 1620 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1621 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1622 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1623 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1624 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1625 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1626 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1627 | // Formal function parameters were mapped during makeFunctions(). |
| 1628 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1629 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1630 | if (builder.isPointer(id)) { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1631 | // 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] | 1632 | // Consider adding to the OpEntryPoint interface list. |
| 1633 | // Only looking at structures if they have at least one member. |
| 1634 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
| 1635 | spv::StorageClass sc = builder.getStorageClass(id); |
| 1636 | // Before SPIR-V 1.4, we only want to include Input and Output. |
| 1637 | // Starting with SPIR-V 1.4, we want all globals. |
| 1638 | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && sc != spv::StorageClassFunction) || |
| 1639 | (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1640 | iOSet.insert(id); |
John Kessenich | 7c7731e | 2019-01-04 16:47:06 +0700 | [diff] [blame] | 1641 | } |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1642 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1643 | |
| 1644 | // If the SPIR-V type is required to be different than the AST type, |
| 1645 | // translate now from the SPIR-V type to the AST type, for the consuming |
| 1646 | // operation. |
| 1647 | // Note this turns it from an l-value to an r-value. |
| 1648 | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
| 1649 | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
| 1650 | id = translateForcedType(id); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1654 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1655 | // Prepare to generate code for the access |
| 1656 | |
| 1657 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1658 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1659 | // followed by setting the base. |
| 1660 | builder.clearAccessChain(); |
| 1661 | |
| 1662 | // 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] | 1663 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1664 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1665 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1666 | // 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] | 1667 | // These are also pure R-values. |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1668 | // C) R-Values from type translation, see above call to translateForcedType() |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1669 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1670 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
| 1671 | !builder.isPointerType(builder.getTypeId(id))) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1672 | builder.setAccessChainRValue(id); |
| 1673 | else |
| 1674 | builder.setAccessChainLValue(id); |
| 1675 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1676 | |
| 1677 | // Process linkage-only nodes for any special additional interface work. |
| 1678 | if (linkageOnly) { |
| 1679 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1680 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1681 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1682 | // of declaration. |
| 1683 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1684 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1685 | // Save possible originating buffers for counter buffers, keyed by |
| 1686 | // making the potential counter-buffer name. |
| 1687 | std::string keyName = symbol->getName().c_str(); |
| 1688 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1689 | counterOriginator[keyName] = symbol; |
| 1690 | } else { |
| 1691 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1692 | std::string keyName = symbol->getName().c_str(); |
| 1693 | auto it = counterOriginator.find(keyName); |
| 1694 | if (it != counterOriginator.end()) { |
| 1695 | id = getSymbolId(it->second); |
| 1696 | if (id != spv::NoResult) { |
| 1697 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1698 | if (counterId != spv::NoResult) { |
| 1699 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1700 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1701 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1711 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1712 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1713 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1714 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1715 | if (node->getType().getQualifier().isSpecConstant()) |
| 1716 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1717 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1718 | // First, handle special cases |
| 1719 | switch (node->getOp()) { |
| 1720 | case glslang::EOpAssign: |
| 1721 | case glslang::EOpAddAssign: |
| 1722 | case glslang::EOpSubAssign: |
| 1723 | case glslang::EOpMulAssign: |
| 1724 | case glslang::EOpVectorTimesMatrixAssign: |
| 1725 | case glslang::EOpVectorTimesScalarAssign: |
| 1726 | case glslang::EOpMatrixTimesScalarAssign: |
| 1727 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1728 | case glslang::EOpDivAssign: |
| 1729 | case glslang::EOpModAssign: |
| 1730 | case glslang::EOpAndAssign: |
| 1731 | case glslang::EOpInclusiveOrAssign: |
| 1732 | case glslang::EOpExclusiveOrAssign: |
| 1733 | case glslang::EOpLeftShiftAssign: |
| 1734 | case glslang::EOpRightShiftAssign: |
| 1735 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1736 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1737 | // says to evaluate the left before the right. So, always, left |
| 1738 | // node then right node. |
| 1739 | { |
| 1740 | // get the left l-value, save it away |
| 1741 | builder.clearAccessChain(); |
| 1742 | node->getLeft()->traverse(this); |
| 1743 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1744 | |
| 1745 | // evaluate the right |
| 1746 | builder.clearAccessChain(); |
| 1747 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1748 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1749 | |
| 1750 | if (node->getOp() != glslang::EOpAssign) { |
| 1751 | // the left is also an r-value |
| 1752 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1753 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1754 | |
| 1755 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1756 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1757 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1758 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1759 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1760 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1761 | node->getType().getBasicType()); |
| 1762 | |
| 1763 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1764 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | // store the result |
| 1768 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1769 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1770 | |
| 1771 | // assignments are expressions having an rValue after they are evaluated... |
| 1772 | builder.clearAccessChain(); |
| 1773 | builder.setAccessChainRValue(rValue); |
| 1774 | } |
| 1775 | return false; |
| 1776 | case glslang::EOpIndexDirect: |
| 1777 | case glslang::EOpIndexDirectStruct: |
| 1778 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1779 | // Structure, array, matrix, or vector indirection with statically known index. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1780 | // Get the left part of the access chain. |
| 1781 | node->getLeft()->traverse(this); |
| 1782 | |
| 1783 | // Add the next element in the chain |
| 1784 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1785 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1786 | if (! node->getLeft()->getType().isArray() && |
| 1787 | node->getLeft()->getType().isVector() && |
| 1788 | node->getOp() == glslang::EOpIndexDirect) { |
| 1789 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1790 | // so short circuit the access-chain stuff with a swizzle. |
| 1791 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1792 | swizzle.push_back(glslangIndex); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1793 | int dummySize; |
| 1794 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1795 | TranslateCoherent(node->getLeft()->getType()), |
| 1796 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1797 | } else { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1798 | |
| 1799 | // Load through a block reference is performed with a dot operator that |
| 1800 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 1801 | // do a load and reset the access chain. |
| 1802 | if (node->getLeft()->getBasicType() == glslang::EbtReference && |
| 1803 | !node->getLeft()->getType().isArray() && |
| 1804 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1805 | { |
| 1806 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 1807 | builder.clearAccessChain(); |
| 1808 | builder.setAccessChainLValue(left); |
| 1809 | } |
| 1810 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1811 | int spvIndex = glslangIndex; |
| 1812 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1813 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1814 | { |
| 1815 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1816 | // index remapping due to hidden members in anonymous blocks. |
| 1817 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1818 | assert(remapper.size() > 0); |
| 1819 | spvIndex = remapper[glslangIndex]; |
| 1820 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1821 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1822 | // normal case for indexing array or structure or block |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 1823 | 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] | 1824 | |
| 1825 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1826 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1827 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1828 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | return false; |
| 1832 | case glslang::EOpIndexIndirect: |
| 1833 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1834 | // Array, matrix, or vector indirection with variable index. |
| 1835 | // Will use native SPIR-V access-chain for and array indirection; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1836 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1837 | // Will use the access chain's 'component' for variable index into a vector. |
| 1838 | |
| 1839 | // This adapter is building access chains left to right. |
| 1840 | // Set up the access chain to the left. |
| 1841 | node->getLeft()->traverse(this); |
| 1842 | |
| 1843 | // save it so that computing the right side doesn't trash it |
| 1844 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1845 | |
| 1846 | // compute the next index in the chain |
| 1847 | builder.clearAccessChain(); |
| 1848 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1849 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1850 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1851 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1852 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1853 | // restore the saved access chain |
| 1854 | builder.setAccessChain(partial); |
| 1855 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1856 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 1857 | int dummySize; |
| 1858 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1859 | TranslateCoherent(node->getLeft()->getType()), |
| 1860 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
| 1861 | } else |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 1862 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), node->getLeft()->getType().getBufferReferenceAlignment()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1863 | } |
| 1864 | return false; |
| 1865 | case glslang::EOpVectorSwizzle: |
| 1866 | { |
| 1867 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1868 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1869 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1870 | int dummySize; |
| 1871 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1872 | TranslateCoherent(node->getLeft()->getType()), |
| 1873 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1874 | } |
| 1875 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1876 | case glslang::EOpMatrixSwizzle: |
| 1877 | logger->missingFunctionality("matrix swizzle"); |
| 1878 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1879 | case glslang::EOpLogicalOr: |
| 1880 | case glslang::EOpLogicalAnd: |
| 1881 | { |
| 1882 | |
| 1883 | // These may require short circuiting, but can sometimes be done as straight |
| 1884 | // binary operations. The right operand must be short circuited if it has |
| 1885 | // side effects, and should probably be if it is complex. |
| 1886 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1887 | break; // handle below as a normal binary operation |
| 1888 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1889 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1890 | builder.clearAccessChain(); |
| 1891 | builder.setAccessChainRValue(result); |
| 1892 | } |
| 1893 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1894 | default: |
| 1895 | break; |
| 1896 | } |
| 1897 | |
| 1898 | // Assume generic binary op... |
| 1899 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1900 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1901 | builder.clearAccessChain(); |
| 1902 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1903 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1904 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1905 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1906 | builder.clearAccessChain(); |
| 1907 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1908 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1909 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1910 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1911 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1912 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1913 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1914 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1915 | convertGlslangToSpvType(node->getType()), left, right, |
| 1916 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1917 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1918 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1919 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1920 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1921 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1922 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1923 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1924 | return false; |
| 1925 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1926 | } |
| 1927 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1928 | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
| 1929 | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
| 1930 | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
| 1931 | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(spv::BuiltIn builtIn, |
| 1932 | const glslang::TType& glslangType) |
| 1933 | { |
| 1934 | switch(builtIn) |
| 1935 | { |
| 1936 | case spv::BuiltInSubgroupEqMask: |
| 1937 | case spv::BuiltInSubgroupGeMask: |
| 1938 | case spv::BuiltInSubgroupGtMask: |
| 1939 | case spv::BuiltInSubgroupLeMask: |
| 1940 | case spv::BuiltInSubgroupLtMask: { |
| 1941 | // these require changing a 64-bit scaler -> a vector of 32-bit components |
| 1942 | if (glslangType.isVector()) |
| 1943 | break; |
| 1944 | std::pair<spv::Id, spv::Id> ret(builder.makeVectorType(builder.makeUintType(32), 4), |
| 1945 | builder.makeUintType(64)); |
| 1946 | return ret; |
| 1947 | } |
| 1948 | default: |
| 1949 | break; |
| 1950 | } |
| 1951 | |
| 1952 | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
| 1953 | return ret; |
| 1954 | } |
| 1955 | |
| 1956 | // For an object previously identified (see getForcedType() and forceType) |
| 1957 | // as needing type translations, do the translation needed for a load, turning |
| 1958 | // an L-value into in R-value. |
| 1959 | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
| 1960 | { |
| 1961 | const auto forceIt = forceType.find(object); |
| 1962 | if (forceIt == forceType.end()) |
| 1963 | return object; |
| 1964 | |
| 1965 | spv::Id desiredTypeId = forceIt->second; |
| 1966 | spv::Id objectTypeId = builder.getTypeId(object); |
| 1967 | assert(builder.isPointerType(objectTypeId)); |
| 1968 | objectTypeId = builder.getContainedTypeId(objectTypeId); |
| 1969 | if (builder.isVectorType(objectTypeId) && |
| 1970 | builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
| 1971 | if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
| 1972 | // handle 32-bit v.xy* -> 64-bit |
| 1973 | builder.clearAccessChain(); |
| 1974 | builder.setAccessChainLValue(object); |
| 1975 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 1976 | std::vector<spv::Id> components; |
| 1977 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
| 1978 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
| 1979 | |
| 1980 | spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
| 1981 | return builder.createUnaryOp(spv::OpBitcast, desiredTypeId, |
| 1982 | builder.createCompositeConstruct(vecType, components)); |
| 1983 | } else { |
| 1984 | logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar"); |
| 1985 | } |
| 1986 | } else { |
| 1987 | logger->missingFunctionality("forcing non 32-bit vector type"); |
| 1988 | } |
| 1989 | |
| 1990 | return object; |
| 1991 | } |
| 1992 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1993 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1994 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1995 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1996 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1997 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1998 | if (node->getType().getQualifier().isSpecConstant()) |
| 1999 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2000 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2001 | spv::Id result = spv::NoResult; |
| 2002 | |
| 2003 | // try texturing first |
| 2004 | result = createImageTextureFunctionCall(node); |
| 2005 | if (result != spv::NoResult) { |
| 2006 | builder.clearAccessChain(); |
| 2007 | builder.setAccessChainRValue(result); |
| 2008 | |
| 2009 | return false; // done with this node |
| 2010 | } |
| 2011 | |
| 2012 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2013 | |
| 2014 | if (node->getOp() == glslang::EOpArrayLength) { |
| 2015 | // Quite special; won't want to evaluate the operand. |
| 2016 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2017 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 2018 | // except for the last block membeor of an SSBO. |
| 2019 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 2020 | // size extracted. |
| 2021 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2022 | // Normal .length() would have been constant folded by the front-end. |
| 2023 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 2024 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2025 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2026 | spv::Id length; |
| 2027 | if (node->getOperand()->getType().isCoopMat()) { |
| 2028 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2029 | |
| 2030 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
| 2031 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2032 | |
| 2033 | length = builder.createCooperativeMatrixLength(typeId); |
| 2034 | } else { |
| 2035 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2036 | block->traverse(this); |
| 2037 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 2038 | length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
| 2039 | } |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2040 | |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2041 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 2042 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 2043 | // AST expectation of a signed result. |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2044 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
| 2045 | if (builder.isInSpecConstCodeGenMode()) { |
| 2046 | length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0)); |
| 2047 | } else { |
| 2048 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 2049 | } |
| 2050 | } |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2051 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2052 | builder.clearAccessChain(); |
| 2053 | builder.setAccessChainRValue(length); |
| 2054 | |
| 2055 | return false; |
| 2056 | } |
| 2057 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2058 | // Start by evaluating the operand |
| 2059 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2060 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2061 | // operate first on the swizzle base, then apply the swizzle. |
| 2062 | spv::Id invertedType = spv::NoType; |
| 2063 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 2064 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 2065 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 2066 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2067 | builder.clearAccessChain(); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2068 | TIntermNode *operandNode; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2069 | if (invertedType != spv::NoType) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2070 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2071 | else |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2072 | operandNode = node->getOperand(); |
| 2073 | |
| 2074 | operandNode->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 2075 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2076 | spv::Id operand = spv::NoResult; |
| 2077 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2078 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2079 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2080 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 2081 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2082 | node->getOp() == glslang::EOpAtomicCounter || |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2083 | node->getOp() == glslang::EOpInterpolateAtCentroid) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2084 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2085 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2086 | lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
| 2087 | } else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2088 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2089 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2090 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2091 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2092 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2093 | |
| 2094 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2095 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2096 | result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2097 | |
| 2098 | // if not, then possibly an operation |
| 2099 | if (! result) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2100 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2101 | |
| 2102 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2103 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2104 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2105 | builder.addDecoration(result, decorations.nonUniform); |
| 2106 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2107 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2108 | builder.clearAccessChain(); |
| 2109 | builder.setAccessChainRValue(result); |
| 2110 | |
| 2111 | return false; // done with this node |
| 2112 | } |
| 2113 | |
| 2114 | // it must be a special case, check... |
| 2115 | switch (node->getOp()) { |
| 2116 | case glslang::EOpPostIncrement: |
| 2117 | case glslang::EOpPostDecrement: |
| 2118 | case glslang::EOpPreIncrement: |
| 2119 | case glslang::EOpPreDecrement: |
| 2120 | { |
| 2121 | // 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] | 2122 | spv::Id one = 0; |
| 2123 | if (node->getBasicType() == glslang::EbtFloat) |
| 2124 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 2125 | else if (node->getBasicType() == glslang::EbtDouble) |
| 2126 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2127 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 2128 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2129 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 2130 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2131 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 2132 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2133 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 2134 | one = builder.makeInt64Constant(1); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2135 | else |
| 2136 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2137 | glslang::TOperator op; |
| 2138 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2139 | node->getOp() == glslang::EOpPostIncrement) |
| 2140 | op = glslang::EOpAdd; |
| 2141 | else |
| 2142 | op = glslang::EOpSub; |
| 2143 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2144 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2145 | convertGlslangToSpvType(node->getType()), operand, one, |
| 2146 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2147 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2148 | |
| 2149 | // The result of operation is always stored, but conditionally the |
| 2150 | // consumed result. The consumed result is always an r-value. |
| 2151 | builder.accessChainStore(result); |
| 2152 | builder.clearAccessChain(); |
| 2153 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2154 | node->getOp() == glslang::EOpPreDecrement) |
| 2155 | builder.setAccessChainRValue(result); |
| 2156 | else |
| 2157 | builder.setAccessChainRValue(operand); |
| 2158 | } |
| 2159 | |
| 2160 | return false; |
| 2161 | |
| 2162 | case glslang::EOpEmitStreamVertex: |
| 2163 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 2164 | return false; |
| 2165 | case glslang::EOpEndStreamPrimitive: |
| 2166 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 2167 | return false; |
| 2168 | |
| 2169 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2170 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2171 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2172 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2176 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 2177 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2178 | if (node->getType().getQualifier().isSpecConstant()) |
| 2179 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2180 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2181 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2182 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2183 | 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] | 2184 | |
| 2185 | // try texturing |
| 2186 | result = createImageTextureFunctionCall(node); |
| 2187 | if (result != spv::NoResult) { |
| 2188 | builder.clearAccessChain(); |
| 2189 | builder.setAccessChainRValue(result); |
| 2190 | |
| 2191 | return false; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2192 | } else if (node->getOp() == glslang::EOpImageStore || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 2193 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2194 | node->getOp() == glslang::EOpImageStoreLod || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 2195 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2196 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2197 | // "imageStore" is a special case, which has no result |
| 2198 | return false; |
| 2199 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2200 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2201 | glslang::TOperator binOp = glslang::EOpNull; |
| 2202 | bool reduceComparison = true; |
| 2203 | bool isMatrix = false; |
| 2204 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2205 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2206 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2207 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2208 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2209 | assert(node->getOp()); |
| 2210 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 2211 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2212 | |
| 2213 | switch (node->getOp()) { |
| 2214 | case glslang::EOpSequence: |
| 2215 | { |
| 2216 | if (preVisit) |
| 2217 | ++sequenceDepth; |
| 2218 | else |
| 2219 | --sequenceDepth; |
| 2220 | |
| 2221 | if (sequenceDepth == 1) { |
| 2222 | // If this is the parent node of all the functions, we want to see them |
| 2223 | // early, so all call points have actual SPIR-V functions to reference. |
| 2224 | // In all cases, still let the traverser visit the children for us. |
| 2225 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 2226 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2227 | // 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] | 2228 | // anything else gets there, so visit out of order, doing them all now. |
| 2229 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2230 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2231 | // 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] | 2232 | // so do them manually. |
| 2233 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2234 | |
| 2235 | return false; |
| 2236 | } |
| 2237 | |
| 2238 | return true; |
| 2239 | } |
| 2240 | case glslang::EOpLinkerObjects: |
| 2241 | { |
| 2242 | if (visit == glslang::EvPreVisit) |
| 2243 | linkageOnly = true; |
| 2244 | else |
| 2245 | linkageOnly = false; |
| 2246 | |
| 2247 | return true; |
| 2248 | } |
| 2249 | case glslang::EOpComma: |
| 2250 | { |
| 2251 | // processing from left to right naturally leaves the right-most |
| 2252 | // lying around in the access chain |
| 2253 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2254 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2255 | glslangOperands[i]->traverse(this); |
| 2256 | |
| 2257 | return false; |
| 2258 | } |
| 2259 | case glslang::EOpFunction: |
| 2260 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2261 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2262 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2263 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2264 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2265 | } else { |
| 2266 | handleFunctionEntry(node); |
| 2267 | } |
| 2268 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2269 | if (inEntryPoint) |
| 2270 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2271 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2272 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | return true; |
| 2276 | case glslang::EOpParameters: |
| 2277 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2278 | // the body, so we still visited the function node's children, making this |
| 2279 | // child redundant. |
| 2280 | return false; |
| 2281 | case glslang::EOpFunctionCall: |
| 2282 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2283 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2284 | if (node->isUserDefined()) |
| 2285 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2286 | // 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] | 2287 | if (result) { |
| 2288 | builder.clearAccessChain(); |
| 2289 | builder.setAccessChainRValue(result); |
| 2290 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2291 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2292 | |
| 2293 | return false; |
| 2294 | } |
| 2295 | case glslang::EOpConstructMat2x2: |
| 2296 | case glslang::EOpConstructMat2x3: |
| 2297 | case glslang::EOpConstructMat2x4: |
| 2298 | case glslang::EOpConstructMat3x2: |
| 2299 | case glslang::EOpConstructMat3x3: |
| 2300 | case glslang::EOpConstructMat3x4: |
| 2301 | case glslang::EOpConstructMat4x2: |
| 2302 | case glslang::EOpConstructMat4x3: |
| 2303 | case glslang::EOpConstructMat4x4: |
| 2304 | case glslang::EOpConstructDMat2x2: |
| 2305 | case glslang::EOpConstructDMat2x3: |
| 2306 | case glslang::EOpConstructDMat2x4: |
| 2307 | case glslang::EOpConstructDMat3x2: |
| 2308 | case glslang::EOpConstructDMat3x3: |
| 2309 | case glslang::EOpConstructDMat3x4: |
| 2310 | case glslang::EOpConstructDMat4x2: |
| 2311 | case glslang::EOpConstructDMat4x3: |
| 2312 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2313 | case glslang::EOpConstructIMat2x2: |
| 2314 | case glslang::EOpConstructIMat2x3: |
| 2315 | case glslang::EOpConstructIMat2x4: |
| 2316 | case glslang::EOpConstructIMat3x2: |
| 2317 | case glslang::EOpConstructIMat3x3: |
| 2318 | case glslang::EOpConstructIMat3x4: |
| 2319 | case glslang::EOpConstructIMat4x2: |
| 2320 | case glslang::EOpConstructIMat4x3: |
| 2321 | case glslang::EOpConstructIMat4x4: |
| 2322 | case glslang::EOpConstructUMat2x2: |
| 2323 | case glslang::EOpConstructUMat2x3: |
| 2324 | case glslang::EOpConstructUMat2x4: |
| 2325 | case glslang::EOpConstructUMat3x2: |
| 2326 | case glslang::EOpConstructUMat3x3: |
| 2327 | case glslang::EOpConstructUMat3x4: |
| 2328 | case glslang::EOpConstructUMat4x2: |
| 2329 | case glslang::EOpConstructUMat4x3: |
| 2330 | case glslang::EOpConstructUMat4x4: |
| 2331 | case glslang::EOpConstructBMat2x2: |
| 2332 | case glslang::EOpConstructBMat2x3: |
| 2333 | case glslang::EOpConstructBMat2x4: |
| 2334 | case glslang::EOpConstructBMat3x2: |
| 2335 | case glslang::EOpConstructBMat3x3: |
| 2336 | case glslang::EOpConstructBMat3x4: |
| 2337 | case glslang::EOpConstructBMat4x2: |
| 2338 | case glslang::EOpConstructBMat4x3: |
| 2339 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2340 | case glslang::EOpConstructF16Mat2x2: |
| 2341 | case glslang::EOpConstructF16Mat2x3: |
| 2342 | case glslang::EOpConstructF16Mat2x4: |
| 2343 | case glslang::EOpConstructF16Mat3x2: |
| 2344 | case glslang::EOpConstructF16Mat3x3: |
| 2345 | case glslang::EOpConstructF16Mat3x4: |
| 2346 | case glslang::EOpConstructF16Mat4x2: |
| 2347 | case glslang::EOpConstructF16Mat4x3: |
| 2348 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2349 | isMatrix = true; |
| 2350 | // fall through |
| 2351 | case glslang::EOpConstructFloat: |
| 2352 | case glslang::EOpConstructVec2: |
| 2353 | case glslang::EOpConstructVec3: |
| 2354 | case glslang::EOpConstructVec4: |
| 2355 | case glslang::EOpConstructDouble: |
| 2356 | case glslang::EOpConstructDVec2: |
| 2357 | case glslang::EOpConstructDVec3: |
| 2358 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2359 | case glslang::EOpConstructFloat16: |
| 2360 | case glslang::EOpConstructF16Vec2: |
| 2361 | case glslang::EOpConstructF16Vec3: |
| 2362 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2363 | case glslang::EOpConstructBool: |
| 2364 | case glslang::EOpConstructBVec2: |
| 2365 | case glslang::EOpConstructBVec3: |
| 2366 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2367 | case glslang::EOpConstructInt8: |
| 2368 | case glslang::EOpConstructI8Vec2: |
| 2369 | case glslang::EOpConstructI8Vec3: |
| 2370 | case glslang::EOpConstructI8Vec4: |
| 2371 | case glslang::EOpConstructUint8: |
| 2372 | case glslang::EOpConstructU8Vec2: |
| 2373 | case glslang::EOpConstructU8Vec3: |
| 2374 | case glslang::EOpConstructU8Vec4: |
| 2375 | case glslang::EOpConstructInt16: |
| 2376 | case glslang::EOpConstructI16Vec2: |
| 2377 | case glslang::EOpConstructI16Vec3: |
| 2378 | case glslang::EOpConstructI16Vec4: |
| 2379 | case glslang::EOpConstructUint16: |
| 2380 | case glslang::EOpConstructU16Vec2: |
| 2381 | case glslang::EOpConstructU16Vec3: |
| 2382 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2383 | case glslang::EOpConstructInt: |
| 2384 | case glslang::EOpConstructIVec2: |
| 2385 | case glslang::EOpConstructIVec3: |
| 2386 | case glslang::EOpConstructIVec4: |
| 2387 | case glslang::EOpConstructUint: |
| 2388 | case glslang::EOpConstructUVec2: |
| 2389 | case glslang::EOpConstructUVec3: |
| 2390 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2391 | case glslang::EOpConstructInt64: |
| 2392 | case glslang::EOpConstructI64Vec2: |
| 2393 | case glslang::EOpConstructI64Vec3: |
| 2394 | case glslang::EOpConstructI64Vec4: |
| 2395 | case glslang::EOpConstructUint64: |
| 2396 | case glslang::EOpConstructU64Vec2: |
| 2397 | case glslang::EOpConstructU64Vec3: |
| 2398 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2399 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2400 | case glslang::EOpConstructTextureSampler: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2401 | case glslang::EOpConstructReference: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2402 | case glslang::EOpConstructCooperativeMatrix: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2403 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2404 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2405 | std::vector<spv::Id> arguments; |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2406 | translateArguments(*node, arguments, lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2407 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2408 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2409 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2410 | else if (node->getOp() == glslang::EOpConstructStruct || |
| 2411 | node->getOp() == glslang::EOpConstructCooperativeMatrix || |
| 2412 | node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2413 | std::vector<spv::Id> constituents; |
| 2414 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2415 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2416 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2417 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2418 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2419 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2420 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2421 | |
| 2422 | builder.clearAccessChain(); |
| 2423 | builder.setAccessChainRValue(constructed); |
| 2424 | |
| 2425 | return false; |
| 2426 | } |
| 2427 | |
| 2428 | // These six are component-wise compares with component-wise results. |
| 2429 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2430 | case glslang::EOpLessThan: |
| 2431 | case glslang::EOpGreaterThan: |
| 2432 | case glslang::EOpLessThanEqual: |
| 2433 | case glslang::EOpGreaterThanEqual: |
| 2434 | case glslang::EOpVectorEqual: |
| 2435 | case glslang::EOpVectorNotEqual: |
| 2436 | { |
| 2437 | // Map the operation to a binary |
| 2438 | binOp = node->getOp(); |
| 2439 | reduceComparison = false; |
| 2440 | switch (node->getOp()) { |
| 2441 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2442 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2443 | default: binOp = node->getOp(); break; |
| 2444 | } |
| 2445 | |
| 2446 | break; |
| 2447 | } |
| 2448 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2449 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2450 | binOp = glslang::EOpMul; |
| 2451 | break; |
| 2452 | case glslang::EOpOuterProduct: |
| 2453 | // two vectors multiplied to make a matrix |
| 2454 | binOp = glslang::EOpOuterProduct; |
| 2455 | break; |
| 2456 | case glslang::EOpDot: |
| 2457 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2458 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2459 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2460 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2461 | binOp = glslang::EOpMul; |
| 2462 | break; |
| 2463 | } |
| 2464 | case glslang::EOpMod: |
| 2465 | // when an aggregate, this is the floating-point mod built-in function, |
| 2466 | // which can be emitted by the one in createBinaryOperation() |
| 2467 | binOp = glslang::EOpMod; |
| 2468 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2469 | case glslang::EOpEmitVertex: |
| 2470 | case glslang::EOpEndPrimitive: |
| 2471 | case glslang::EOpBarrier: |
| 2472 | case glslang::EOpMemoryBarrier: |
| 2473 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2474 | case glslang::EOpMemoryBarrierBuffer: |
| 2475 | case glslang::EOpMemoryBarrierImage: |
| 2476 | case glslang::EOpMemoryBarrierShared: |
| 2477 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2478 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2479 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2480 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2481 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2482 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2483 | case glslang::EOpSubgroupBarrier: |
| 2484 | case glslang::EOpSubgroupMemoryBarrier: |
| 2485 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2486 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2487 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2488 | noReturnValue = true; |
| 2489 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2490 | break; |
| 2491 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2492 | case glslang::EOpAtomicStore: |
| 2493 | noReturnValue = true; |
| 2494 | // fallthrough |
| 2495 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2496 | case glslang::EOpAtomicAdd: |
| 2497 | case glslang::EOpAtomicMin: |
| 2498 | case glslang::EOpAtomicMax: |
| 2499 | case glslang::EOpAtomicAnd: |
| 2500 | case glslang::EOpAtomicOr: |
| 2501 | case glslang::EOpAtomicXor: |
| 2502 | case glslang::EOpAtomicExchange: |
| 2503 | case glslang::EOpAtomicCompSwap: |
| 2504 | atomic = true; |
| 2505 | break; |
| 2506 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2507 | case glslang::EOpAtomicCounterAdd: |
| 2508 | case glslang::EOpAtomicCounterSubtract: |
| 2509 | case glslang::EOpAtomicCounterMin: |
| 2510 | case glslang::EOpAtomicCounterMax: |
| 2511 | case glslang::EOpAtomicCounterAnd: |
| 2512 | case glslang::EOpAtomicCounterOr: |
| 2513 | case glslang::EOpAtomicCounterXor: |
| 2514 | case glslang::EOpAtomicCounterExchange: |
| 2515 | case glslang::EOpAtomicCounterCompSwap: |
| 2516 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2517 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2518 | atomic = true; |
| 2519 | break; |
| 2520 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2521 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 2522 | case glslang::EOpIgnoreIntersectionNV: |
| 2523 | case glslang::EOpTerminateRayNV: |
| 2524 | case glslang::EOpTraceNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 2525 | case glslang::EOpExecuteCallableNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2526 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2527 | noReturnValue = true; |
| 2528 | break; |
| 2529 | #endif |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2530 | case glslang::EOpCooperativeMatrixLoad: |
| 2531 | case glslang::EOpCooperativeMatrixStore: |
| 2532 | noReturnValue = true; |
| 2533 | break; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 2534 | case glslang::EOpBeginInvocationInterlock: |
| 2535 | case glslang::EOpEndInvocationInterlock: |
| 2536 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 2537 | noReturnValue = true; |
| 2538 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2539 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2540 | default: |
| 2541 | break; |
| 2542 | } |
| 2543 | |
| 2544 | // |
| 2545 | // See if it maps to a regular operation. |
| 2546 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2547 | if (binOp != glslang::EOpNull) { |
| 2548 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2549 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2550 | assert(left && right); |
| 2551 | |
| 2552 | builder.clearAccessChain(); |
| 2553 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2554 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2555 | |
| 2556 | builder.clearAccessChain(); |
| 2557 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2558 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2559 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2560 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2561 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2562 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2563 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2564 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2565 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2566 | left->getType().getBasicType(), reduceComparison); |
| 2567 | |
| 2568 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2569 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2570 | builder.clearAccessChain(); |
| 2571 | builder.setAccessChainRValue(result); |
| 2572 | |
| 2573 | return false; |
| 2574 | } |
| 2575 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2576 | // |
| 2577 | // Create the list of operands. |
| 2578 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2579 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2580 | std::vector<spv::Id> operands; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2581 | std::vector<spv::IdImmediate> memoryAccessOperands; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2582 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2583 | // special case l-value operands; there are just a few |
| 2584 | bool lvalue = false; |
| 2585 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2586 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2587 | case glslang::EOpModf: |
| 2588 | if (arg == 1) |
| 2589 | lvalue = true; |
| 2590 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2591 | case glslang::EOpInterpolateAtSample: |
| 2592 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2593 | #ifdef AMD_EXTENSIONS |
| 2594 | case glslang::EOpInterpolateAtVertex: |
| 2595 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2596 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2597 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2598 | |
| 2599 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2600 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2601 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2602 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2603 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 2604 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2605 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2606 | case glslang::EOpAtomicAdd: |
| 2607 | case glslang::EOpAtomicMin: |
| 2608 | case glslang::EOpAtomicMax: |
| 2609 | case glslang::EOpAtomicAnd: |
| 2610 | case glslang::EOpAtomicOr: |
| 2611 | case glslang::EOpAtomicXor: |
| 2612 | case glslang::EOpAtomicExchange: |
| 2613 | case glslang::EOpAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2614 | case glslang::EOpAtomicLoad: |
| 2615 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2616 | case glslang::EOpAtomicCounterAdd: |
| 2617 | case glslang::EOpAtomicCounterSubtract: |
| 2618 | case glslang::EOpAtomicCounterMin: |
| 2619 | case glslang::EOpAtomicCounterMax: |
| 2620 | case glslang::EOpAtomicCounterAnd: |
| 2621 | case glslang::EOpAtomicCounterOr: |
| 2622 | case glslang::EOpAtomicCounterXor: |
| 2623 | case glslang::EOpAtomicCounterExchange: |
| 2624 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2625 | if (arg == 0) |
| 2626 | lvalue = true; |
| 2627 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2628 | case glslang::EOpAddCarry: |
| 2629 | case glslang::EOpSubBorrow: |
| 2630 | if (arg == 2) |
| 2631 | lvalue = true; |
| 2632 | break; |
| 2633 | case glslang::EOpUMulExtended: |
| 2634 | case glslang::EOpIMulExtended: |
| 2635 | if (arg >= 2) |
| 2636 | lvalue = true; |
| 2637 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2638 | case glslang::EOpCooperativeMatrixLoad: |
| 2639 | if (arg == 0 || arg == 1) |
| 2640 | lvalue = true; |
| 2641 | break; |
| 2642 | case glslang::EOpCooperativeMatrixStore: |
| 2643 | if (arg == 1) |
| 2644 | lvalue = true; |
| 2645 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2646 | default: |
| 2647 | break; |
| 2648 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2649 | builder.clearAccessChain(); |
| 2650 | if (invertedType != spv::NoType && arg == 0) |
| 2651 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2652 | else |
| 2653 | glslangOperands[arg]->traverse(this); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2654 | |
| 2655 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
| 2656 | node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2657 | |
| 2658 | if (arg == 1) { |
| 2659 | // fold "element" parameter into the access chain |
| 2660 | spv::Builder::AccessChain save = builder.getAccessChain(); |
| 2661 | builder.clearAccessChain(); |
| 2662 | glslangOperands[2]->traverse(this); |
| 2663 | |
| 2664 | spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
| 2665 | |
| 2666 | builder.setAccessChain(save); |
| 2667 | |
| 2668 | // Point to the first element of the array. |
| 2669 | builder.accessChainPush(elementId, TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 2670 | glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2671 | |
| 2672 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 2673 | unsigned int alignment = builder.getAccessChain().alignment; |
| 2674 | |
| 2675 | int memoryAccess = TranslateMemoryAccess(coherentFlags); |
| 2676 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
| 2677 | memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
| 2678 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
| 2679 | memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
| 2680 | if (builder.getStorageClass(builder.getAccessChain().base) == spv::StorageClassPhysicalStorageBufferEXT) { |
| 2681 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
| 2682 | } |
| 2683 | |
| 2684 | memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
| 2685 | |
| 2686 | if (memoryAccess & spv::MemoryAccessAlignedMask) { |
| 2687 | memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
| 2688 | } |
| 2689 | |
| 2690 | if (memoryAccess & (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
| 2691 | memoryAccessOperands.push_back(spv::IdImmediate(true, builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
| 2692 | } |
| 2693 | } else if (arg == 2) { |
| 2694 | continue; |
| 2695 | } |
| 2696 | } |
| 2697 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2698 | if (lvalue) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2699 | operands.push_back(builder.accessChainGetLValue()); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2700 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2701 | lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
| 2702 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2703 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2704 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2705 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2706 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2707 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2708 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2709 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
| 2710 | std::vector<spv::IdImmediate> idImmOps; |
| 2711 | |
| 2712 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 2713 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 2714 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 2715 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 2716 | // get the pointee type |
| 2717 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
| 2718 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2719 | // do the op |
| 2720 | spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
| 2721 | // store the result to the pointer (out param 'm') |
| 2722 | builder.createStore(result, operands[0]); |
| 2723 | result = 0; |
| 2724 | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2725 | std::vector<spv::IdImmediate> idImmOps; |
| 2726 | |
| 2727 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 2728 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
| 2729 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 2730 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 2731 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 2732 | |
| 2733 | builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps); |
| 2734 | result = 0; |
| 2735 | } else if (atomic) { |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2736 | // Handle all atomics |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2737 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), lvalueCoherentFlags); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2738 | } else { |
| 2739 | // Pass through to generic operations. |
| 2740 | switch (glslangOperands.size()) { |
| 2741 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2742 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2743 | break; |
| 2744 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2745 | { |
| 2746 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2747 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2748 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2749 | result = createUnaryOperation( |
| 2750 | node->getOp(), decorations, |
| 2751 | resultType(), operands.front(), |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2752 | glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2753 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2754 | break; |
| 2755 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2756 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2757 | break; |
| 2758 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2759 | if (invertedType) |
| 2760 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | if (noReturnValue) |
| 2764 | return false; |
| 2765 | |
| 2766 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2767 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2768 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2769 | } else { |
| 2770 | builder.clearAccessChain(); |
| 2771 | builder.setAccessChainRValue(result); |
| 2772 | return false; |
| 2773 | } |
| 2774 | } |
| 2775 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2776 | // This path handles both if-then-else and ?: |
| 2777 | // The if-then-else has a node type of void, while |
| 2778 | // ?: has either a void or a non-void node type |
| 2779 | // |
| 2780 | // Leaving the result, when not void: |
| 2781 | // GLSL only has r-values as the result of a :?, but |
| 2782 | // if we have an l-value, that can be more efficient if it will |
| 2783 | // become the base of a complex r-value expression, because the |
| 2784 | // 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] | 2785 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 2786 | { |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2787 | // see if OpSelect can handle it |
| 2788 | const auto isOpSelectable = [&]() { |
| 2789 | if (node->getBasicType() == glslang::EbtVoid) |
| 2790 | return false; |
| 2791 | // OpSelect can do all other types starting with SPV 1.4 |
| 2792 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
| 2793 | // pre-1.4, only scalars and vectors can be handled |
| 2794 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
| 2795 | return false; |
| 2796 | } |
| 2797 | return true; |
| 2798 | }; |
| 2799 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2800 | // See if it simple and safe, or required, to execute both sides. |
| 2801 | // Crucially, side effects must be either semantically required or avoided, |
| 2802 | // and there are performance trade-offs. |
| 2803 | // Return true if required or a good idea (and safe) to execute both sides, |
| 2804 | // false otherwise. |
| 2805 | const auto bothSidesPolicy = [&]() -> bool { |
| 2806 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2807 | if (node->getTrueBlock() == nullptr || |
| 2808 | node->getFalseBlock() == nullptr) |
| 2809 | return false; |
| 2810 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2811 | // required? (unless we write additional code to look for side effects |
| 2812 | // and make performance trade-offs if none are present) |
| 2813 | if (!node->getShortCircuit()) |
| 2814 | return true; |
| 2815 | |
| 2816 | // if not required to execute both, decide based on performance/practicality... |
| 2817 | |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2818 | if (!isOpSelectable()) |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2819 | return false; |
| 2820 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2821 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 2822 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 2823 | |
| 2824 | // return true if a single operand to ? : is okay for OpSelect |
| 2825 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2826 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2827 | }; |
| 2828 | |
| 2829 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 2830 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 2831 | }; |
| 2832 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2833 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 2834 | // emit the condition before doing anything with selection |
| 2835 | node->getCondition()->traverse(this); |
| 2836 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 2837 | |
| 2838 | // Find a way of executing both sides and selecting the right result. |
| 2839 | const auto executeBothSides = [&]() -> void { |
| 2840 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2841 | node->getTrueBlock()->traverse(this); |
| 2842 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2843 | node->getFalseBlock()->traverse(this); |
| 2844 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2845 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2846 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2847 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2848 | // done if void |
| 2849 | if (node->getBasicType() == glslang::EbtVoid) |
| 2850 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 2851 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2852 | // emit code to select between trueValue and falseValue |
| 2853 | |
| 2854 | // see if OpSelect can handle it |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2855 | if (isOpSelectable()) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2856 | // Emit OpSelect for this selection. |
| 2857 | |
| 2858 | // smear condition to vector, if necessary (AST is always scalar) |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2859 | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
| 2860 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2861 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 2862 | builder.makeVectorType(builder.makeBoolType(), |
| 2863 | builder.getNumComponents(trueValue))); |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2864 | } |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2865 | |
| 2866 | // OpSelect |
| 2867 | result = builder.createTriOp(spv::OpSelect, |
| 2868 | convertGlslangToSpvType(node->getType()), condition, |
| 2869 | trueValue, falseValue); |
| 2870 | |
| 2871 | builder.clearAccessChain(); |
| 2872 | builder.setAccessChainRValue(result); |
| 2873 | } else { |
| 2874 | // We need control flow to select the result. |
| 2875 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 2876 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2877 | |
| 2878 | // Selection control: |
| 2879 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2880 | |
| 2881 | // make an "if" based on the value created by the condition |
| 2882 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2883 | |
| 2884 | // emit the "then" statement |
| 2885 | builder.createStore(trueValue, result); |
| 2886 | ifBuilder.makeBeginElse(); |
| 2887 | // emit the "else" statement |
| 2888 | builder.createStore(falseValue, result); |
| 2889 | |
| 2890 | // finish off the control flow |
| 2891 | ifBuilder.makeEndIf(); |
| 2892 | |
| 2893 | builder.clearAccessChain(); |
| 2894 | builder.setAccessChainLValue(result); |
| 2895 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2896 | }; |
| 2897 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2898 | // Execute the one side needed, as per the condition |
| 2899 | const auto executeOneSide = [&]() { |
| 2900 | // Always emit control flow. |
| 2901 | if (node->getBasicType() != glslang::EbtVoid) |
| 2902 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2903 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2904 | // Selection control: |
| 2905 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2906 | |
| 2907 | // make an "if" based on the value created by the condition |
| 2908 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2909 | |
| 2910 | // emit the "then" statement |
| 2911 | if (node->getTrueBlock() != nullptr) { |
| 2912 | node->getTrueBlock()->traverse(this); |
| 2913 | if (result != spv::NoResult) |
| 2914 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 2915 | } |
| 2916 | |
| 2917 | if (node->getFalseBlock() != nullptr) { |
| 2918 | ifBuilder.makeBeginElse(); |
| 2919 | // emit the "else" statement |
| 2920 | node->getFalseBlock()->traverse(this); |
| 2921 | if (result != spv::NoResult) |
| 2922 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 2923 | } |
| 2924 | |
| 2925 | // finish off the control flow |
| 2926 | ifBuilder.makeEndIf(); |
| 2927 | |
| 2928 | if (result != spv::NoResult) { |
| 2929 | builder.clearAccessChain(); |
| 2930 | builder.setAccessChainLValue(result); |
| 2931 | } |
| 2932 | }; |
| 2933 | |
| 2934 | // Try for OpSelect (or a requirement to execute both sides) |
| 2935 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2936 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2937 | if (node->getType().getQualifier().isSpecConstant()) |
| 2938 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2939 | executeBothSides(); |
| 2940 | } else |
| 2941 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2942 | |
| 2943 | return false; |
| 2944 | } |
| 2945 | |
| 2946 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2947 | { |
| 2948 | // emit and get the condition before doing anything with switch |
| 2949 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2950 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2951 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2952 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 2953 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2954 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2955 | // browse the children to sort out code segments |
| 2956 | int defaultSegment = -1; |
| 2957 | std::vector<TIntermNode*> codeSegments; |
| 2958 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 2959 | std::vector<int> caseValues; |
| 2960 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 2961 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 2962 | TIntermNode* child = *c; |
| 2963 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2964 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2965 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2966 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2967 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2968 | } else |
| 2969 | codeSegments.push_back(child); |
| 2970 | } |
| 2971 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2972 | // 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] | 2973 | // statements between the last case and the end of the switch statement |
| 2974 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 2975 | (int)codeSegments.size() == defaultSegment) |
| 2976 | codeSegments.push_back(nullptr); |
| 2977 | |
| 2978 | // make the switch statement |
| 2979 | 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] | 2980 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2981 | |
| 2982 | // emit all the code in the segments |
| 2983 | breakForLoop.push(false); |
| 2984 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 2985 | builder.nextSwitchSegment(segmentBlocks, s); |
| 2986 | if (codeSegments[s]) |
| 2987 | codeSegments[s]->traverse(this); |
| 2988 | else |
| 2989 | builder.addSwitchBreak(); |
| 2990 | } |
| 2991 | breakForLoop.pop(); |
| 2992 | |
| 2993 | builder.endSwitch(segmentBlocks); |
| 2994 | |
| 2995 | return false; |
| 2996 | } |
| 2997 | |
| 2998 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 2999 | { |
| 3000 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 3001 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3002 | |
| 3003 | builder.clearAccessChain(); |
| 3004 | builder.setAccessChainRValue(constant); |
| 3005 | } |
| 3006 | |
| 3007 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 3008 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3009 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3010 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3011 | |
| 3012 | // Loop control: |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3013 | std::vector<unsigned int> operands; |
| 3014 | const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3015 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3016 | // Spec requires back edges to target header blocks, and every header block |
| 3017 | // must dominate its merge block. Make a header block first to ensure these |
| 3018 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 3019 | // by a block-ending branch. But we don't want to put any other body/test |
| 3020 | // instructions in it, since the body/test may have arbitrary instructions, |
| 3021 | // including merges of its own. |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3022 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3023 | builder.setBuildPoint(&blocks.head); |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3024 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3025 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3026 | spv::Block& test = builder.makeNewBlock(); |
| 3027 | builder.createBranch(&test); |
| 3028 | |
| 3029 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3030 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3031 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3032 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 3033 | |
| 3034 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3035 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3036 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3037 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3038 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3039 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3040 | |
| 3041 | builder.setBuildPoint(&blocks.continue_target); |
| 3042 | if (node->getTerminal()) |
| 3043 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3044 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 3045 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3046 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3047 | builder.createBranch(&blocks.body); |
| 3048 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3049 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3050 | builder.setBuildPoint(&blocks.body); |
| 3051 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3052 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3053 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3054 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3055 | |
| 3056 | builder.setBuildPoint(&blocks.continue_target); |
| 3057 | if (node->getTerminal()) |
| 3058 | node->getTerminal()->traverse(this); |
| 3059 | if (node->getTest()) { |
| 3060 | node->getTest()->traverse(this); |
| 3061 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3062 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3063 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3064 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 3065 | // TODO: unless there was a break/return/discard instruction |
| 3066 | // somewhere in the body, this is an infinite loop, so we should |
| 3067 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3068 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3069 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3070 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3071 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3072 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3073 | return false; |
| 3074 | } |
| 3075 | |
| 3076 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 3077 | { |
| 3078 | if (node->getExpression()) |
| 3079 | node->getExpression()->traverse(this); |
| 3080 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3081 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3082 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3083 | switch (node->getFlowOp()) { |
| 3084 | case glslang::EOpKill: |
| 3085 | builder.makeDiscard(); |
| 3086 | break; |
| 3087 | case glslang::EOpBreak: |
| 3088 | if (breakForLoop.top()) |
| 3089 | builder.createLoopExit(); |
| 3090 | else |
| 3091 | builder.addSwitchBreak(); |
| 3092 | break; |
| 3093 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3094 | builder.createLoopContinue(); |
| 3095 | break; |
| 3096 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3097 | if (node->getExpression()) { |
| 3098 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 3099 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 3100 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 3101 | builder.clearAccessChain(); |
| 3102 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 3103 | builder.setAccessChainLValue(copyId); |
| 3104 | multiTypeStore(glslangReturnType, returnId); |
| 3105 | returnId = builder.createLoad(copyId); |
| 3106 | } |
| 3107 | builder.makeReturn(false, returnId); |
| 3108 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 3109 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3110 | |
| 3111 | builder.clearAccessChain(); |
| 3112 | break; |
| 3113 | |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3114 | case glslang::EOpDemote: |
| 3115 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 3116 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 3117 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 3118 | break; |
| 3119 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3120 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3121 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3122 | break; |
| 3123 | } |
| 3124 | |
| 3125 | return false; |
| 3126 | } |
| 3127 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3128 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3129 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3130 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3131 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3132 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 3133 | if (node->getQualifier().isConstant()) { |
Dan Sinclair | 12fcaa2 | 2018-11-13 09:17:44 -0500 | [diff] [blame] | 3134 | spv::Id result = createSpvConstant(*node); |
| 3135 | if (result != spv::NoResult) |
| 3136 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3140 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3141 | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
| 3142 | : forcedType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3143 | |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3144 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 3145 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 3146 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3147 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3148 | switch (storageClass) { |
| 3149 | case spv::StorageClassInput: |
| 3150 | case spv::StorageClassOutput: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3151 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3152 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3153 | break; |
| 3154 | case spv::StorageClassPushConstant: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3155 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3156 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3157 | break; |
| 3158 | case spv::StorageClassUniform: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3159 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3160 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 3161 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3162 | else |
| 3163 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 3164 | break; |
| 3165 | case spv::StorageClassStorageBuffer: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3166 | case spv::StorageClassPhysicalStorageBufferEXT: |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3167 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
| 3168 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 3169 | break; |
| 3170 | default: |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3171 | if (node->getType().containsBasicType(glslang::EbtFloat16)) |
| 3172 | builder.addCapability(spv::CapabilityFloat16); |
| 3173 | if (node->getType().containsBasicType(glslang::EbtInt16) || |
| 3174 | node->getType().containsBasicType(glslang::EbtUint16)) |
| 3175 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3176 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3177 | } |
| 3178 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3179 | |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3180 | const bool contains8BitType = node->getType().containsBasicType(glslang::EbtInt8) || |
| 3181 | node->getType().containsBasicType(glslang::EbtUint8); |
| 3182 | if (contains8BitType) { |
| 3183 | if (storageClass == spv::StorageClassPushConstant) { |
| 3184 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 3185 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 3186 | } else if (storageClass == spv::StorageClassUniform) { |
| 3187 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 3188 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3189 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
| 3190 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 3191 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3192 | } else { |
| 3193 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3197 | const char* name = node->getName().c_str(); |
| 3198 | if (glslang::IsAnonymous(name)) |
| 3199 | name = ""; |
| 3200 | |
| 3201 | return builder.createVariable(storageClass, spvType, name); |
| 3202 | } |
| 3203 | |
| 3204 | // Return type Id of the sampled type. |
| 3205 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 3206 | { |
| 3207 | switch (sampler.type) { |
| 3208 | case glslang::EbtFloat: return builder.makeFloatType(32); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3209 | #ifdef AMD_EXTENSIONS |
| 3210 | case glslang::EbtFloat16: |
| 3211 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 3212 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3213 | return builder.makeFloatType(16); |
| 3214 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3215 | case glslang::EbtInt: return builder.makeIntType(32); |
| 3216 | case glslang::EbtUint: return builder.makeUintType(32); |
| 3217 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3218 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3219 | return builder.makeFloatType(32); |
| 3220 | } |
| 3221 | } |
| 3222 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3223 | // If node is a swizzle operation, return the type that should be used if |
| 3224 | // the swizzle base is first consumed by another operation, before the swizzle |
| 3225 | // is applied. |
| 3226 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 3227 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3228 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3229 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 3230 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 3231 | else |
| 3232 | return spv::NoType; |
| 3233 | } |
| 3234 | |
| 3235 | // When inverting a swizzle with a parent op, this function |
| 3236 | // will apply the swizzle operation to a completed parent operation. |
| 3237 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 3238 | { |
| 3239 | std::vector<unsigned> swizzle; |
| 3240 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 3241 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 3242 | } |
| 3243 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3244 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 3245 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 3246 | { |
| 3247 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 3248 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 3249 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3250 | } |
| 3251 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3252 | // Convert from a glslang type to an SPV type, by calling into a |
| 3253 | // recursive version of this function. This establishes the inherited |
| 3254 | // layout state rooted from the top-level type. |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3255 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3256 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3257 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | // 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] | 3261 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3262 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3263 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3264 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 3265 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3266 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3267 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3268 | |
| 3269 | switch (type.getBasicType()) { |
| 3270 | case glslang::EbtVoid: |
| 3271 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3272 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3273 | break; |
| 3274 | case glslang::EbtFloat: |
| 3275 | spvType = builder.makeFloatType(32); |
| 3276 | break; |
| 3277 | case glslang::EbtDouble: |
| 3278 | spvType = builder.makeFloatType(64); |
| 3279 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3280 | case glslang::EbtFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3281 | spvType = builder.makeFloatType(16); |
| 3282 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3283 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3284 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 3285 | // a 32-bit int where non-0 means true. |
| 3286 | if (explicitLayout != glslang::ElpNone) |
| 3287 | spvType = builder.makeUintType(32); |
| 3288 | else |
| 3289 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3290 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3291 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3292 | spvType = builder.makeIntType(8); |
| 3293 | break; |
| 3294 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3295 | spvType = builder.makeUintType(8); |
| 3296 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3297 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3298 | spvType = builder.makeIntType(16); |
| 3299 | break; |
| 3300 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3301 | spvType = builder.makeUintType(16); |
| 3302 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3303 | case glslang::EbtInt: |
| 3304 | spvType = builder.makeIntType(32); |
| 3305 | break; |
| 3306 | case glslang::EbtUint: |
| 3307 | spvType = builder.makeUintType(32); |
| 3308 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3309 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3310 | spvType = builder.makeIntType(64); |
| 3311 | break; |
| 3312 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3313 | spvType = builder.makeUintType(64); |
| 3314 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3315 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 3316 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3317 | spvType = builder.makeUintType(32); |
| 3318 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3319 | #ifdef NV_EXTENSIONS |
| 3320 | case glslang::EbtAccStructNV: |
| 3321 | spvType = builder.makeAccelerationStructureNVType(); |
| 3322 | break; |
| 3323 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3324 | case glslang::EbtSampler: |
| 3325 | { |
| 3326 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3327 | if (sampler.sampler) { |
| 3328 | // pure sampler |
| 3329 | spvType = builder.makeSamplerType(); |
| 3330 | } else { |
| 3331 | // an image is present, make its type |
| 3332 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 3333 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 3334 | if (sampler.combined) { |
| 3335 | // already has both image and sampler, make the combined type |
| 3336 | spvType = builder.makeSampledImageType(spvType); |
| 3337 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3338 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3339 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3340 | break; |
| 3341 | case glslang::EbtStruct: |
| 3342 | case glslang::EbtBlock: |
| 3343 | { |
| 3344 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3345 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3346 | |
| 3347 | // Try to share structs for different layouts, but not yet for other |
| 3348 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3349 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3350 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3351 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3352 | break; |
| 3353 | |
| 3354 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3355 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3356 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 3357 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3358 | } |
| 3359 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3360 | case glslang::EbtReference: |
| 3361 | { |
| 3362 | // Make the forward pointer, then recurse to convert the structure type, then |
| 3363 | // patch up the forward pointer with a real pointer type. |
| 3364 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 3365 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 3366 | forwardPointers[type.getReferentType()] = forwardId; |
| 3367 | } |
| 3368 | spvType = forwardPointers[type.getReferentType()]; |
| 3369 | if (!forwardReferenceOnly) { |
| 3370 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 3371 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 3372 | forwardPointers[type.getReferentType()], |
| 3373 | referentType); |
| 3374 | } |
| 3375 | } |
| 3376 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3377 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3378 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3379 | break; |
| 3380 | } |
| 3381 | |
| 3382 | if (type.isMatrix()) |
| 3383 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3384 | else { |
| 3385 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3386 | if (type.getVectorSize() > 1) |
| 3387 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3388 | } |
| 3389 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3390 | if (type.isCoopMat()) { |
| 3391 | builder.addCapability(spv::CapabilityCooperativeMatrixNV); |
| 3392 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
| 3393 | if (type.getBasicType() == glslang::EbtFloat16) |
| 3394 | builder.addCapability(spv::CapabilityFloat16); |
| 3395 | |
| 3396 | spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1); |
| 3397 | spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2); |
| 3398 | spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3); |
| 3399 | |
| 3400 | spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols); |
| 3401 | } |
| 3402 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3403 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3404 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3405 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3406 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3407 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3408 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3409 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3410 | // Use a dummy glslang type for querying internal strides of |
| 3411 | // arrays of arrays, but using just a one-dimensional array. |
| 3412 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3413 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3414 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3415 | |
| 3416 | // Will compute the higher-order strides here, rather than making a whole |
| 3417 | // pile of types and doing repetitive recursion on their contents. |
| 3418 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3419 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3420 | |
| 3421 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3422 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3423 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3424 | if (stride > 0) |
| 3425 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3426 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3427 | } |
| 3428 | } else { |
| 3429 | // single-dimensional array, and don't yet have stride |
| 3430 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3431 | // 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] | 3432 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3433 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3434 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3435 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3436 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3437 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3438 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3439 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3440 | else { |
| 3441 | if (!lastBufferBlockMember) { |
| 3442 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 3443 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3444 | } |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3445 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3446 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3447 | if (stride > 0) |
| 3448 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | return spvType; |
| 3452 | } |
| 3453 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3454 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3455 | // |
| 3456 | // Identify interface members that don't have their required extension turned on. |
| 3457 | // |
| 3458 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3459 | { |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3460 | #ifdef NV_EXTENSIONS |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3461 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3462 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3463 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3464 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3465 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3466 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3467 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3468 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3469 | |
| 3470 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3471 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3472 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3473 | return true; |
| 3474 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3475 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3476 | return true; |
| 3477 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3478 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3479 | return true; |
| 3480 | } |
| 3481 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3482 | |
| 3483 | return false; |
| 3484 | }; |
| 3485 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3486 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3487 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3488 | // Mutually recursive with convertGlslangToSpvType(). |
| 3489 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3490 | const glslang::TTypeList* glslangMembers, |
| 3491 | glslang::TLayoutPacking explicitLayout, |
| 3492 | const glslang::TQualifier& qualifier) |
| 3493 | { |
| 3494 | // Create a vector of struct types for SPIR-V to consume |
| 3495 | std::vector<spv::Id> spvMembers; |
| 3496 | 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] | 3497 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3498 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3499 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3500 | if (glslangMember.hiddenMember()) { |
| 3501 | ++memberDelta; |
| 3502 | if (type.getBasicType() == glslang::EbtBlock) |
| 3503 | memberRemapper[glslangMembers][i] = -1; |
| 3504 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3505 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3506 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3507 | if (filterMember(glslangMember)) |
| 3508 | continue; |
| 3509 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3510 | // modify just this child's view of the qualifier |
| 3511 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3512 | InheritQualifiers(memberQualifier, qualifier); |
| 3513 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3514 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3515 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3516 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3517 | |
| 3518 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3519 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3520 | i == (int)glslangMembers->size() - 1; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3521 | |
| 3522 | // Make forward pointers for any pointer members, and create a list of members to |
| 3523 | // convert to spirv types after creating the struct. |
| 3524 | if (glslangMember.getBasicType() == glslang::EbtReference) { |
| 3525 | if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) { |
| 3526 | deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier)); |
| 3527 | } |
| 3528 | spvMembers.push_back( |
| 3529 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, true)); |
| 3530 | } else { |
| 3531 | spvMembers.push_back( |
| 3532 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, false)); |
| 3533 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | // Make the SPIR-V type |
| 3538 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3539 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3540 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3541 | |
| 3542 | // Decorate it |
| 3543 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3544 | |
John Kessenich | d72f488 | 2019-01-16 14:55:37 +0700 | [diff] [blame] | 3545 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3546 | auto it = deferredForwardPointers[i]; |
| 3547 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 3548 | } |
| 3549 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3550 | return spvType; |
| 3551 | } |
| 3552 | |
| 3553 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3554 | const glslang::TTypeList* glslangMembers, |
| 3555 | glslang::TLayoutPacking explicitLayout, |
| 3556 | const glslang::TQualifier& qualifier, |
| 3557 | spv::Id spvType) |
| 3558 | { |
| 3559 | // Name and decorate the non-hidden members |
| 3560 | int offset = -1; |
| 3561 | int locationOffset = 0; // for use within the members of this struct |
| 3562 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3563 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3564 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3565 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3566 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3567 | if (filterMember(glslangMember)) |
| 3568 | continue; |
| 3569 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3570 | |
| 3571 | // modify just this child's view of the qualifier |
| 3572 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3573 | InheritQualifiers(memberQualifier, qualifier); |
| 3574 | |
| 3575 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3576 | if (member < 0) |
| 3577 | continue; |
| 3578 | |
| 3579 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3580 | builder.addMemberDecoration(spvType, member, |
| 3581 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 3582 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 3583 | // Add interpolation and auxiliary storage decorations only to |
| 3584 | // top-level members of Input and Output storage classes |
| 3585 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 3586 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 3587 | if (type.getBasicType() == glslang::EbtBlock || |
| 3588 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 3589 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 3590 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3591 | #ifdef NV_EXTENSIONS |
| 3592 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 3593 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3594 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3595 | } |
| 3596 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3597 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3598 | if (type.getBasicType() == glslang::EbtBlock && |
| 3599 | qualifier.storage == glslang::EvqBuffer) { |
| 3600 | // Add memory decorations only to top-level members of shader storage block |
| 3601 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3602 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3603 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3604 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 3605 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3606 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3607 | // Location assignment was already completed correctly by the front end, |
| 3608 | // just track whether a member needs to be decorated. |
| 3609 | // Ignore member locations if the container is an array, as that's |
| 3610 | // ill-specified and decisions have been made to not allow this. |
| 3611 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 3612 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3613 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3614 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 3615 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 3616 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 3617 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3618 | // component, XFB, others |
| 3619 | if (glslangMember.getQualifier().hasComponent()) |
| 3620 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 3621 | glslangMember.getQualifier().layoutComponent); |
| 3622 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 3623 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 3624 | glslangMember.getQualifier().layoutXfbOffset); |
| 3625 | else if (explicitLayout != glslang::ElpNone) { |
| 3626 | // figure out what to do with offset, which is accumulating |
| 3627 | int nextOffset; |
| 3628 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 3629 | if (offset >= 0) |
| 3630 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 3631 | offset = nextOffset; |
| 3632 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3633 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3634 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 3635 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 3636 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3637 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3638 | // built-in variable decorations |
| 3639 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 3640 | if (builtIn != spv::BuiltInMax) |
| 3641 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3642 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3643 | // nonuniform |
| 3644 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 3645 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3646 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 3647 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 3648 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 3649 | memberQualifier.semanticName); |
| 3650 | } |
| 3651 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3652 | #ifdef NV_EXTENSIONS |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3653 | if (builtIn == spv::BuiltInLayer) { |
| 3654 | // SPV_NV_viewport_array2 extension |
| 3655 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 3656 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 3657 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 3658 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3659 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3660 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 3661 | builder.addMemberDecoration(spvType, member, |
| 3662 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 3663 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 3664 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 3665 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3666 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3667 | } |
| 3668 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 3669 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 3670 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 3671 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 3672 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3673 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3677 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 3678 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3679 | } |
| 3680 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3681 | // Turn the expression forming the array size into an id. |
| 3682 | // This is not quite trivial, because of specialization constants. |
| 3683 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 3684 | // a specialization constant expression is. |
| 3685 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 3686 | { |
| 3687 | // First, see if this is sized with a node, meaning a specialization constant: |
| 3688 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 3689 | if (specNode != nullptr) { |
| 3690 | builder.clearAccessChain(); |
| 3691 | specNode->traverse(this); |
| 3692 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 3693 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3694 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3695 | // Otherwise, need a compile-time (front end) size, get it: |
| 3696 | int size = arraySizes.getDimSize(dim); |
| 3697 | assert(size > 0); |
| 3698 | return builder.makeUintConstant(size); |
| 3699 | } |
| 3700 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3701 | // Wrap the builder's accessChainLoad to: |
| 3702 | // - localize handling of RelaxedPrecision |
| 3703 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 3704 | // (avoids unnecessary work and possible type punning for structures) |
| 3705 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3706 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 3707 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3708 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3709 | |
| 3710 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3711 | coherentFlags |= TranslateCoherent(type); |
| 3712 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3713 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3714 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3715 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3716 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3717 | TranslateNonUniformDecoration(type.getQualifier()), |
| 3718 | nominalTypeId, |
| 3719 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3720 | TranslateMemoryScope(coherentFlags), |
| 3721 | alignment); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3722 | |
| 3723 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3724 | if (type.getBasicType() == glslang::EbtBool) { |
| 3725 | if (builder.isScalarType(nominalTypeId)) { |
| 3726 | // Conversion for bool |
| 3727 | spv::Id boolType = builder.makeBoolType(); |
| 3728 | if (nominalTypeId != boolType) |
| 3729 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 3730 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3731 | // Conversion for bvec |
| 3732 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3733 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 3734 | if (nominalTypeId != bvecType) |
| 3735 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 3736 | } |
| 3737 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3738 | |
| 3739 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3740 | } |
| 3741 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3742 | // Wrap the builder's accessChainStore to: |
| 3743 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3744 | // |
| 3745 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3746 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 3747 | { |
| 3748 | // Need to convert to abstract types when necessary |
| 3749 | if (type.getBasicType() == glslang::EbtBool) { |
| 3750 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 3751 | |
| 3752 | if (builder.isScalarType(nominalTypeId)) { |
| 3753 | // Conversion for bool |
| 3754 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3755 | if (nominalTypeId != boolType) { |
| 3756 | // keep these outside arguments, for determinant order-of-evaluation |
| 3757 | spv::Id one = builder.makeUintConstant(1); |
| 3758 | spv::Id zero = builder.makeUintConstant(0); |
| 3759 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 3760 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3761 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3762 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3763 | // Conversion for bvec |
| 3764 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3765 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3766 | if (nominalTypeId != bvecType) { |
| 3767 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 3768 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 3769 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 3770 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3771 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3772 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 3773 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3777 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3778 | coherentFlags |= TranslateCoherent(type); |
| 3779 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3780 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3781 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3782 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3783 | builder.accessChainStore(rvalue, |
| 3784 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3785 | TranslateMemoryScope(coherentFlags), alignment); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3786 | } |
| 3787 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3788 | // For storing when types match at the glslang level, but not might match at the |
| 3789 | // SPIR-V level. |
| 3790 | // |
| 3791 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3792 | // 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] | 3793 | // as in a member-decorated way. |
| 3794 | // |
| 3795 | // NOTE: This function can handle any store request; if it's not special it |
| 3796 | // simplifies to a simple OpStore. |
| 3797 | // |
| 3798 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 3799 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 3800 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3801 | // we only do the complex path here if it's an aggregate |
| 3802 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3803 | accessChainStore(type, rValue); |
| 3804 | return; |
| 3805 | } |
| 3806 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3807 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3808 | spv::Id rType = builder.getTypeId(rValue); |
| 3809 | spv::Id lValue = builder.accessChainGetLValue(); |
| 3810 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 3811 | if (lType == rType) { |
| 3812 | accessChainStore(type, rValue); |
| 3813 | return; |
| 3814 | } |
| 3815 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3816 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3817 | // where the two types were the same type in GLSL. This requires member |
| 3818 | // by member copy, recursively. |
| 3819 | |
John Kessenich | fbb6bdf | 2019-01-15 21:48:27 +0700 | [diff] [blame] | 3820 | // SPIR-V 1.4 added an instruction to do help do this. |
| 3821 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 3822 | // However, bool in uniform space is changed to int, so |
| 3823 | // OpCopyLogical does not work for that. |
| 3824 | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
| 3825 | bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0); |
| 3826 | bool lBool = builder.containsType(lType, spv::OpTypeBool, 0); |
| 3827 | if (lBool == rBool) { |
| 3828 | spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue); |
| 3829 | accessChainStore(type, logicalCopy); |
| 3830 | return; |
| 3831 | } |
| 3832 | } |
| 3833 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3834 | // If an array, copy element by element. |
| 3835 | if (type.isArray()) { |
| 3836 | glslang::TType glslangElementType(type, 0); |
| 3837 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 3838 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 3839 | // get the source member |
| 3840 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3841 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3842 | // set up the target storage |
| 3843 | builder.clearAccessChain(); |
| 3844 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3845 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), type.getBufferReferenceAlignment()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3846 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3847 | // store the member |
| 3848 | multiTypeStore(glslangElementType, elementRValue); |
| 3849 | } |
| 3850 | } else { |
| 3851 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3852 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3853 | // loop over structure members |
| 3854 | const glslang::TTypeList& members = *type.getStruct(); |
| 3855 | for (int m = 0; m < (int)members.size(); ++m) { |
| 3856 | const glslang::TType& glslangMemberType = *members[m].type; |
| 3857 | |
| 3858 | // get the source member |
| 3859 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 3860 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 3861 | |
| 3862 | // set up the target storage |
| 3863 | builder.clearAccessChain(); |
| 3864 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3865 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), type.getBufferReferenceAlignment()); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3866 | |
| 3867 | // store the member |
| 3868 | multiTypeStore(glslangMemberType, memberRValue); |
| 3869 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3870 | } |
| 3871 | } |
| 3872 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3873 | // Decide whether or not this type should be |
| 3874 | // decorated with offsets and strides, and if so |
| 3875 | // whether std140 or std430 rules should be applied. |
| 3876 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3877 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3878 | // has to be a block |
| 3879 | if (type.getBasicType() != glslang::EbtBlock) |
| 3880 | return glslang::ElpNone; |
| 3881 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3882 | // 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] | 3883 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3884 | type.getQualifier().storage != glslang::EvqBuffer && |
| 3885 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3886 | return glslang::ElpNone; |
| 3887 | |
| 3888 | // return the layout to use |
| 3889 | switch (type.getQualifier().layoutPacking) { |
| 3890 | case glslang::ElpStd140: |
| 3891 | case glslang::ElpStd430: |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3892 | case glslang::ElpScalar: |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3893 | return type.getQualifier().layoutPacking; |
| 3894 | default: |
| 3895 | return glslang::ElpNone; |
| 3896 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3897 | } |
| 3898 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3899 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3900 | 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] | 3901 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3902 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3903 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3904 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 3905 | |
| 3906 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3907 | } |
| 3908 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3909 | // 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] | 3910 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3911 | 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] | 3912 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3913 | glslang::TType elementType; |
| 3914 | elementType.shallowCopy(matrixType); |
| 3915 | elementType.clearArraySizes(); |
| 3916 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3917 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3918 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3919 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3920 | |
| 3921 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3922 | } |
| 3923 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3924 | // Given a member type of a struct, realign the current offset for it, and compute |
| 3925 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 3926 | // on the next call. |
| 3927 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 3928 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 3929 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3930 | 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] | 3931 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3932 | { |
| 3933 | // this will get a positive value when deemed necessary |
| 3934 | nextOffset = -1; |
| 3935 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3936 | // override anything in currentOffset with user-set offset |
| 3937 | if (memberType.getQualifier().hasOffset()) |
| 3938 | currentOffset = memberType.getQualifier().layoutOffset; |
| 3939 | |
| 3940 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 3941 | // in which case the following code does not matter. But, that's not quite right |
| 3942 | // once cross-compilation unit GLSL validation is done, as the original user |
| 3943 | // settings are needed in layoutOffset, and then the following will come into play. |
| 3944 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3945 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3946 | if (! memberType.getQualifier().hasOffset()) |
| 3947 | currentOffset = -1; |
| 3948 | |
| 3949 | return; |
| 3950 | } |
| 3951 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3952 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3953 | if (currentOffset < 0) |
| 3954 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3955 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3956 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 3957 | // but possibly not yet correctly aligned. |
| 3958 | |
| 3959 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3960 | int dummyStride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3961 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3962 | |
| 3963 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3964 | // TODO: make this consistent in early phases of code: |
| 3965 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 3966 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 3967 | // 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] | 3968 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3969 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3970 | int dummySize; |
| 3971 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 3972 | if (componentAlignment <= 4) |
| 3973 | memberAlignment = componentAlignment; |
| 3974 | } |
| 3975 | |
| 3976 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3977 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3978 | |
| 3979 | // Bump up to vec4 if there is a bad straddle |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3980 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3981 | glslang::RoundToPow2(currentOffset, 16); |
| 3982 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3983 | nextOffset = currentOffset + memberSize; |
| 3984 | } |
| 3985 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3986 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3987 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3988 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 3989 | switch (glslangBuiltIn) |
| 3990 | { |
| 3991 | case glslang::EbvClipDistance: |
| 3992 | case glslang::EbvCullDistance: |
| 3993 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3994 | #ifdef NV_EXTENSIONS |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3995 | case glslang::EbvViewportMaskNV: |
| 3996 | case glslang::EbvSecondaryPositionNV: |
| 3997 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3998 | case glslang::EbvPositionPerViewNV: |
| 3999 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4000 | case glslang::EbvTaskCountNV: |
| 4001 | case glslang::EbvPrimitiveCountNV: |
| 4002 | case glslang::EbvPrimitiveIndicesNV: |
| 4003 | case glslang::EbvClipDistancePerViewNV: |
| 4004 | case glslang::EbvCullDistancePerViewNV: |
| 4005 | case glslang::EbvLayerPerViewNV: |
| 4006 | case glslang::EbvMeshViewCountNV: |
| 4007 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4008 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4009 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 4010 | // Alternately, we could just call this for any glslang built-in, since the |
| 4011 | // capability already guards against duplicates. |
| 4012 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 4013 | break; |
| 4014 | default: |
| 4015 | // Capabilities were already generated when the struct was declared. |
| 4016 | break; |
| 4017 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4018 | } |
| 4019 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4020 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4021 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 4022 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4023 | } |
| 4024 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4025 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4026 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 4027 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4028 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4029 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4030 | assert(qualifier == glslang::EvqIn || |
| 4031 | qualifier == glslang::EvqOut || |
| 4032 | qualifier == glslang::EvqInOut || |
| 4033 | qualifier == glslang::EvqConstReadOnly); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4034 | return qualifier != glslang::EvqConstReadOnly; |
| 4035 | } |
| 4036 | |
| 4037 | // Is parameter pass-by-original? |
| 4038 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 4039 | bool implicitThisParam) |
| 4040 | { |
| 4041 | if (implicitThisParam) // implicit this |
| 4042 | return true; |
| 4043 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4044 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4045 | return paramType.containsOpaque() || // sampler, etc. |
| 4046 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 4047 | } |
| 4048 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4049 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 4050 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4051 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4052 | 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] | 4053 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 4054 | if (paramPrecision != spv::NoPrecision) |
| 4055 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4056 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4057 | if (type.getBasicType() == glslang::EbtReference) { |
| 4058 | // Original and non-writable params pass the pointer directly and |
| 4059 | // use restrict/aliased, others are stored to a pointer in Function |
| 4060 | // memory and use RestrictPointer/AliasedPointer. |
| 4061 | if (originalParam(type.getQualifier().storage, type, false) || |
| 4062 | !writableParam(type.getQualifier().storage)) { |
| 4063 | decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrict : spv::DecorationAliased); |
| 4064 | } else { |
| 4065 | decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 4066 | } |
| 4067 | } |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4068 | }; |
| 4069 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4070 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4071 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4072 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4073 | continue; |
| 4074 | |
| 4075 | // 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] | 4076 | // 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] | 4077 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4078 | // 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] | 4079 | // function. What it is an address of varies: |
| 4080 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4081 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 4082 | // 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] | 4083 | // |
| 4084 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 4085 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4086 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 4087 | // 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] | 4088 | |
| 4089 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4090 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4091 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 4092 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4093 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 4094 | glslangIntermediate->implicitThisName; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4095 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4096 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4097 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4098 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4099 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4100 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 4101 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4102 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4103 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 4104 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4105 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4106 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4107 | paramTypes.push_back(typeId); |
| 4108 | } |
| 4109 | |
| 4110 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4111 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 4112 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4113 | glslFunction->getName().c_str(), paramTypes, |
| 4114 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4115 | if (implicitThis) |
| 4116 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4117 | |
| 4118 | // Track function to emit/call later |
| 4119 | functionMap[glslFunction->getName().c_str()] = function; |
| 4120 | |
| 4121 | // Set the parameter id's |
| 4122 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4123 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 4124 | // give a name too |
| 4125 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4126 | |
| 4127 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4128 | if (paramType.containsBasicType(glslang::EbtInt8) || |
| 4129 | paramType.containsBasicType(glslang::EbtUint8)) |
| 4130 | builder.addCapability(spv::CapabilityInt8); |
| 4131 | if (paramType.containsBasicType(glslang::EbtInt16) || |
| 4132 | paramType.containsBasicType(glslang::EbtUint16)) |
| 4133 | builder.addCapability(spv::CapabilityInt16); |
| 4134 | if (paramType.containsBasicType(glslang::EbtFloat16)) |
| 4135 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4136 | } |
| 4137 | } |
| 4138 | } |
| 4139 | |
| 4140 | // Process all the initializers, while skipping the functions and link objects |
| 4141 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 4142 | { |
| 4143 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 4144 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 4145 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 4146 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 4147 | |
| 4148 | // 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] | 4149 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4150 | initializer->traverse(this); |
| 4151 | } |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | // Process all the functions, while skipping initializers. |
| 4156 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4157 | { |
| 4158 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4159 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 4160 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4161 | node->traverse(this); |
| 4162 | } |
| 4163 | } |
| 4164 | |
| 4165 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 4166 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4167 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4168 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4169 | currentFunction = functionMap[node->getName().c_str()]; |
| 4170 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4171 | builder.setBuildPoint(functionBlock); |
| 4172 | } |
| 4173 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4174 | 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] | 4175 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4176 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4177 | |
| 4178 | glslang::TSampler sampler = {}; |
| 4179 | bool cubeCompare = false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4180 | #ifdef AMD_EXTENSIONS |
| 4181 | bool f16ShadowCompare = false; |
| 4182 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4183 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4184 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 4185 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4186 | #ifdef AMD_EXTENSIONS |
| 4187 | f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 4188 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4189 | } |
| 4190 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4191 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 4192 | builder.clearAccessChain(); |
| 4193 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4194 | |
| 4195 | // Special case l-value operands |
| 4196 | bool lvalue = false; |
| 4197 | switch (node.getOp()) { |
| 4198 | case glslang::EOpImageAtomicAdd: |
| 4199 | case glslang::EOpImageAtomicMin: |
| 4200 | case glslang::EOpImageAtomicMax: |
| 4201 | case glslang::EOpImageAtomicAnd: |
| 4202 | case glslang::EOpImageAtomicOr: |
| 4203 | case glslang::EOpImageAtomicXor: |
| 4204 | case glslang::EOpImageAtomicExchange: |
| 4205 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4206 | case glslang::EOpImageAtomicLoad: |
| 4207 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4208 | if (i == 0) |
| 4209 | lvalue = true; |
| 4210 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4211 | case glslang::EOpSparseImageLoad: |
| 4212 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 4213 | lvalue = true; |
| 4214 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4215 | #ifdef AMD_EXTENSIONS |
| 4216 | case glslang::EOpSparseTexture: |
| 4217 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 4218 | lvalue = true; |
| 4219 | break; |
| 4220 | case glslang::EOpSparseTextureClamp: |
| 4221 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 4222 | lvalue = true; |
| 4223 | break; |
| 4224 | case glslang::EOpSparseTextureLod: |
| 4225 | case glslang::EOpSparseTextureOffset: |
| 4226 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 4227 | lvalue = true; |
| 4228 | break; |
| 4229 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4230 | case glslang::EOpSparseTexture: |
| 4231 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 4232 | lvalue = true; |
| 4233 | break; |
| 4234 | case glslang::EOpSparseTextureClamp: |
| 4235 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 4236 | lvalue = true; |
| 4237 | break; |
| 4238 | case glslang::EOpSparseTextureLod: |
| 4239 | case glslang::EOpSparseTextureOffset: |
| 4240 | if (i == 3) |
| 4241 | lvalue = true; |
| 4242 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4243 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4244 | case glslang::EOpSparseTextureFetch: |
| 4245 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 4246 | lvalue = true; |
| 4247 | break; |
| 4248 | case glslang::EOpSparseTextureFetchOffset: |
| 4249 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 4250 | lvalue = true; |
| 4251 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4252 | #ifdef AMD_EXTENSIONS |
| 4253 | case glslang::EOpSparseTextureLodOffset: |
| 4254 | case glslang::EOpSparseTextureGrad: |
| 4255 | case glslang::EOpSparseTextureOffsetClamp: |
| 4256 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 4257 | lvalue = true; |
| 4258 | break; |
| 4259 | case glslang::EOpSparseTextureGradOffset: |
| 4260 | case glslang::EOpSparseTextureGradClamp: |
| 4261 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 4262 | lvalue = true; |
| 4263 | break; |
| 4264 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 4265 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 4266 | lvalue = true; |
| 4267 | break; |
| 4268 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4269 | case glslang::EOpSparseTextureLodOffset: |
| 4270 | case glslang::EOpSparseTextureGrad: |
| 4271 | case glslang::EOpSparseTextureOffsetClamp: |
| 4272 | if (i == 4) |
| 4273 | lvalue = true; |
| 4274 | break; |
| 4275 | case glslang::EOpSparseTextureGradOffset: |
| 4276 | case glslang::EOpSparseTextureGradClamp: |
| 4277 | if (i == 5) |
| 4278 | lvalue = true; |
| 4279 | break; |
| 4280 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 4281 | if (i == 6) |
| 4282 | lvalue = true; |
| 4283 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4284 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4285 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4286 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 4287 | lvalue = true; |
| 4288 | break; |
| 4289 | case glslang::EOpSparseTextureGatherOffset: |
| 4290 | case glslang::EOpSparseTextureGatherOffsets: |
| 4291 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 4292 | lvalue = true; |
| 4293 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4294 | #ifdef AMD_EXTENSIONS |
| 4295 | case glslang::EOpSparseTextureGatherLod: |
| 4296 | if (i == 3) |
| 4297 | lvalue = true; |
| 4298 | break; |
| 4299 | case glslang::EOpSparseTextureGatherLodOffset: |
| 4300 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 4301 | if (i == 4) |
| 4302 | lvalue = true; |
| 4303 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4304 | case glslang::EOpSparseImageLoadLod: |
| 4305 | if (i == 3) |
| 4306 | lvalue = true; |
| 4307 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4308 | #endif |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4309 | #ifdef NV_EXTENSIONS |
| 4310 | case glslang::EOpImageSampleFootprintNV: |
| 4311 | if (i == 4) |
| 4312 | lvalue = true; |
| 4313 | break; |
| 4314 | case glslang::EOpImageSampleFootprintClampNV: |
| 4315 | case glslang::EOpImageSampleFootprintLodNV: |
| 4316 | if (i == 5) |
| 4317 | lvalue = true; |
| 4318 | break; |
| 4319 | case glslang::EOpImageSampleFootprintGradNV: |
| 4320 | if (i == 6) |
| 4321 | lvalue = true; |
| 4322 | break; |
| 4323 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 4324 | if (i == 7) |
| 4325 | lvalue = true; |
| 4326 | break; |
| 4327 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4328 | default: |
| 4329 | break; |
| 4330 | } |
| 4331 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4332 | if (lvalue) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4333 | arguments.push_back(builder.accessChainGetLValue()); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4334 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 4335 | lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
| 4336 | } else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4337 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4338 | } |
| 4339 | } |
| 4340 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4341 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4342 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4343 | builder.clearAccessChain(); |
| 4344 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4345 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4346 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4347 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4348 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 4349 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4350 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4351 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4352 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 4353 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4354 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4355 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4356 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4357 | const glslang::TType &imageType = node->getAsAggregate() |
| 4358 | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 4359 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4360 | const glslang::TSampler sampler = imageType.getSampler(); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4361 | #ifdef AMD_EXTENSIONS |
| 4362 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4363 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 4364 | : false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4365 | #endif |
| 4366 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4367 | const auto signExtensionMask = [&]() { |
| 4368 | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
| 4369 | if (sampler.type == glslang::EbtUint) |
| 4370 | return spv::ImageOperandsZeroExtendMask; |
| 4371 | else if (sampler.type == glslang::EbtInt) |
| 4372 | return spv::ImageOperandsSignExtendMask; |
| 4373 | } |
| 4374 | return spv::ImageOperandsMaskNone; |
| 4375 | }; |
| 4376 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4377 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 4378 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4379 | std::vector<spv::Id> arguments; |
| 4380 | if (node->getAsAggregate()) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4381 | translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4382 | else |
| 4383 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 4384 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4385 | |
| 4386 | spv::Builder::TextureParameters params = { }; |
| 4387 | params.sampler = arguments[0]; |
| 4388 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4389 | glslang::TCrackedTextureOp cracked; |
| 4390 | node->crackTexture(sampler, cracked); |
| 4391 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4392 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4393 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4394 | // Check for queries |
| 4395 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4396 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 4397 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 4398 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4399 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4400 | switch (node->getOp()) { |
| 4401 | case glslang::EOpImageQuerySize: |
| 4402 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4403 | if (arguments.size() > 1) { |
| 4404 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4405 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4406 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4407 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4408 | case glslang::EOpImageQuerySamples: |
| 4409 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4410 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4411 | case glslang::EOpTextureQueryLod: |
| 4412 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4413 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4414 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4415 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4416 | case glslang::EOpSparseTexelsResident: |
| 4417 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4418 | default: |
| 4419 | assert(0); |
| 4420 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4421 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4422 | } |
| 4423 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4424 | int components = node->getType().getVectorSize(); |
| 4425 | |
| 4426 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 4427 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 4428 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 4429 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 4430 | // here around e.g. which ones return scalars or other types. |
| 4431 | components = 4; |
| 4432 | } |
| 4433 | |
| 4434 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 4435 | |
| 4436 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 4437 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4438 | // Check for image functions other than queries |
| 4439 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4440 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4441 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4442 | spv::IdImmediate image = { true, *(opIt++) }; |
| 4443 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4444 | |
| 4445 | // Handle subpass operations |
| 4446 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 4447 | // built-in function. |
| 4448 | if (cracked.subpass) { |
| 4449 | // add on the (0,0) coordinate |
| 4450 | spv::Id zero = builder.makeIntConstant(0); |
| 4451 | std::vector<spv::Id> comps; |
| 4452 | comps.push_back(zero); |
| 4453 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4454 | spv::IdImmediate coord = { true, |
| 4455 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 4456 | operands.push_back(coord); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4457 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
| 4458 | imageOperands.word = imageOperands.word | signExtensionMask(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4459 | if (sampler.ms) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4460 | imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
| 4461 | } |
| 4462 | if (imageOperands.word != spv::ImageOperandsMaskNone) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4463 | operands.push_back(imageOperands); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4464 | if (sampler.ms) { |
| 4465 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 4466 | operands.push_back(imageOperand); |
| 4467 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4468 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4469 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 4470 | builder.setPrecision(result, precision); |
| 4471 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4474 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 4475 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4476 | #ifdef AMD_EXTENSIONS |
| 4477 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 4478 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4479 | if (node->getOp() == glslang::EOpImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4480 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4481 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4482 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4483 | mask = mask | spv::ImageOperandsSampleMask; |
| 4484 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4485 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4486 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4487 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4488 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4489 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4490 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4491 | #endif |
| 4492 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4493 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4494 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4495 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4496 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4497 | operands.push_back(imageOperands); |
| 4498 | } |
| 4499 | if (mask & spv::ImageOperandsSampleMask) { |
| 4500 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4501 | operands.push_back(imageOperand); |
| 4502 | } |
| 4503 | #ifdef AMD_EXTENSIONS |
| 4504 | if (mask & spv::ImageOperandsLodMask) { |
| 4505 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4506 | operands.push_back(imageOperand); |
| 4507 | } |
| 4508 | #endif |
| 4509 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4510 | spv::IdImmediate imageOperand = { true, |
| 4511 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4512 | operands.push_back(imageOperand); |
| 4513 | } |
| 4514 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4515 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4516 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4517 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4518 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4519 | builder.setPrecision(result[0], precision); |
| 4520 | |
| 4521 | // If needed, add a conversion constructor to the proper size. |
| 4522 | if (components != node->getType().getVectorSize()) |
| 4523 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4524 | |
| 4525 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4526 | #ifdef AMD_EXTENSIONS |
| 4527 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 4528 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4529 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4530 | #endif |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4531 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4532 | // Push the texel value before the operands |
| 4533 | #ifdef AMD_EXTENSIONS |
| 4534 | if (sampler.ms || cracked.lod) { |
| 4535 | #else |
| 4536 | if (sampler.ms) { |
| 4537 | #endif |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4538 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4539 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4540 | } else { |
| 4541 | spv::IdImmediate texel = { true, *opIt }; |
| 4542 | operands.push_back(texel); |
| 4543 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4544 | |
| 4545 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 4546 | if (sampler.ms) { |
| 4547 | mask = mask | spv::ImageOperandsSampleMask; |
| 4548 | } |
| 4549 | #ifdef AMD_EXTENSIONS |
| 4550 | if (cracked.lod) { |
| 4551 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4552 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4553 | mask = mask | spv::ImageOperandsLodMask; |
| 4554 | } |
| 4555 | #endif |
| 4556 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4557 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4558 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4559 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4560 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4561 | operands.push_back(imageOperands); |
| 4562 | } |
| 4563 | if (mask & spv::ImageOperandsSampleMask) { |
| 4564 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4565 | operands.push_back(imageOperand); |
| 4566 | } |
| 4567 | #ifdef AMD_EXTENSIONS |
| 4568 | if (mask & spv::ImageOperandsLodMask) { |
| 4569 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4570 | operands.push_back(imageOperand); |
| 4571 | } |
| 4572 | #endif |
| 4573 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4574 | spv::IdImmediate imageOperand = { true, |
| 4575 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4576 | operands.push_back(imageOperand); |
| 4577 | } |
| 4578 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4579 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4580 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4581 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4582 | return spv::NoResult; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4583 | #ifdef AMD_EXTENSIONS |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4584 | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
| 4585 | node->getOp() == glslang::EOpSparseImageLoadLod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4586 | #else |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4587 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4588 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4589 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4590 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4591 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4592 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4593 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4594 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4595 | mask = mask | spv::ImageOperandsSampleMask; |
| 4596 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4597 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4598 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4599 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4600 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4601 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4602 | mask = mask | spv::ImageOperandsLodMask; |
| 4603 | } |
| 4604 | #endif |
| 4605 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4606 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4607 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4608 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4609 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4610 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4611 | } |
| 4612 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4613 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4614 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4615 | } |
| 4616 | #ifdef AMD_EXTENSIONS |
| 4617 | if (mask & spv::ImageOperandsLodMask) { |
| 4618 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4619 | operands.push_back(imageOperand); |
| 4620 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4621 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4622 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4623 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4624 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | // Create the return type that was a special structure |
| 4628 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4629 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4630 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 4631 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 4632 | |
| 4633 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 4634 | |
| 4635 | // Decode the return type |
| 4636 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 4637 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 4638 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 4639 | // Process image atomic operations |
| 4640 | |
| 4641 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 4642 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4643 | // For non-MS, the sample value should be 0 |
| 4644 | spv::IdImmediate sample = { true, sampler.ms ? *(opIt++) : builder.makeUintConstant(0) }; |
| 4645 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4646 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4647 | spv::Id resultTypeId; |
| 4648 | // imageAtomicStore has a void return type so base the pointer type on |
| 4649 | // the type of the value operand. |
| 4650 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 4651 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word)); |
| 4652 | } else { |
| 4653 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 4654 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4655 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4656 | |
| 4657 | std::vector<spv::Id> operands; |
| 4658 | operands.push_back(pointer); |
| 4659 | for (; opIt != arguments.end(); ++opIt) |
| 4660 | operands.push_back(*opIt); |
| 4661 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4662 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), lvalueCoherentFlags); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4663 | } |
| 4664 | } |
| 4665 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4666 | #ifdef AMD_EXTENSIONS |
| 4667 | // Check for fragment mask functions other than queries |
| 4668 | if (cracked.fragMask) { |
| 4669 | assert(sampler.ms); |
| 4670 | |
| 4671 | auto opIt = arguments.begin(); |
| 4672 | std::vector<spv::Id> operands; |
| 4673 | |
| 4674 | // Extract the image if necessary |
| 4675 | if (builder.isSampledImage(params.sampler)) |
| 4676 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4677 | |
| 4678 | operands.push_back(params.sampler); |
| 4679 | ++opIt; |
| 4680 | |
| 4681 | if (sampler.isSubpass()) { |
| 4682 | // add on the (0,0) coordinate |
| 4683 | spv::Id zero = builder.makeIntConstant(0); |
| 4684 | std::vector<spv::Id> comps; |
| 4685 | comps.push_back(zero); |
| 4686 | comps.push_back(zero); |
| 4687 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 4688 | } |
| 4689 | |
| 4690 | for (; opIt != arguments.end(); ++opIt) |
| 4691 | operands.push_back(*opIt); |
| 4692 | |
| 4693 | spv::Op fragMaskOp = spv::OpNop; |
| 4694 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 4695 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 4696 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 4697 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 4698 | |
| 4699 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 4700 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 4701 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 4702 | } |
| 4703 | #endif |
| 4704 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4705 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4706 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4707 | #ifdef NV_EXTENSIONS |
| 4708 | bool imageFootprint = node->isImageFootprint(); |
| 4709 | #endif |
| 4710 | |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4711 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 4712 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4713 | // check for bias argument |
| 4714 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4715 | #ifdef AMD_EXTENSIONS |
| 4716 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 4717 | #else |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4718 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4719 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4720 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4721 | #ifdef AMD_EXTENSIONS |
| 4722 | if (cracked.gather) |
| 4723 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4724 | |
| 4725 | if (f16ShadowCompare) |
| 4726 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4727 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4728 | if (cracked.offset) |
| 4729 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4730 | #ifdef AMD_EXTENSIONS |
| 4731 | else if (cracked.offsets) |
| 4732 | ++nonBiasArgCount; |
| 4733 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4734 | if (cracked.grad) |
| 4735 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4736 | if (cracked.lodClamp) |
| 4737 | ++nonBiasArgCount; |
| 4738 | if (sparse) |
| 4739 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4740 | #ifdef NV_EXTENSIONS |
| 4741 | if (imageFootprint) |
| 4742 | //Following three extra arguments |
| 4743 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4744 | nonBiasArgCount += 3; |
| 4745 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4746 | if ((int)arguments.size() > nonBiasArgCount) |
| 4747 | bias = true; |
| 4748 | } |
| 4749 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 4750 | // See if the sampler param should really be just the SPV image part |
| 4751 | if (cracked.fetch) { |
| 4752 | // a fetch needs to have the image extracted first |
| 4753 | if (builder.isSampledImage(params.sampler)) |
| 4754 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4755 | } |
| 4756 | |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4757 | #ifdef AMD_EXTENSIONS |
| 4758 | if (cracked.gather) { |
| 4759 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 4760 | if (bias || cracked.lod || |
| 4761 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 4762 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 4763 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | #endif |
| 4767 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4768 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4769 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4770 | params.coords = arguments[1]; |
| 4771 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4772 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4773 | |
| 4774 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4775 | #ifdef AMD_EXTENSIONS |
| 4776 | if (cubeCompare || f16ShadowCompare) { |
| 4777 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4778 | if (cubeCompare) { |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4779 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4780 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4781 | ++extraArgs; |
| 4782 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4783 | params.Dref = arguments[2]; |
| 4784 | ++extraArgs; |
| 4785 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4786 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4787 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4788 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4789 | 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] | 4790 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4791 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 4792 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4793 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 4794 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4795 | |
| 4796 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4797 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4798 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4799 | ++extraArgs; |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 4800 | } else if (glslangIntermediate->getStage() != EShLangFragment |
| 4801 | #ifdef NV_EXTENSIONS |
| 4802 | // NV_compute_shader_derivatives layout qualifiers allow for implicit LODs |
| 4803 | && !(glslangIntermediate->getStage() == EShLangCompute && |
| 4804 | (glslangIntermediate->getLayoutDerivativeModeNone() != glslang::LayoutDerivativeNone)) |
| 4805 | #endif |
| 4806 | ) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4807 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 4808 | noImplicitLod = true; |
| 4809 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4810 | |
| 4811 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4812 | if (sampler.ms) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4813 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4814 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4815 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4816 | |
| 4817 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4818 | if (cracked.grad) { |
| 4819 | params.gradX = arguments[2 + extraArgs]; |
| 4820 | params.gradY = arguments[3 + extraArgs]; |
| 4821 | extraArgs += 2; |
| 4822 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4823 | |
| 4824 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4825 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4826 | params.offset = arguments[2 + extraArgs]; |
| 4827 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4828 | } else if (cracked.offsets) { |
| 4829 | params.offsets = arguments[2 + extraArgs]; |
| 4830 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4831 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4832 | |
| 4833 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4834 | if (cracked.lodClamp) { |
| 4835 | params.lodClamp = arguments[2 + extraArgs]; |
| 4836 | ++extraArgs; |
| 4837 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4838 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4839 | if (sparse) { |
| 4840 | params.texelOut = arguments[2 + extraArgs]; |
| 4841 | ++extraArgs; |
| 4842 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4843 | |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4844 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4845 | if (cracked.gather && ! sampler.shadow) { |
| 4846 | // default component is 0, if missing, otherwise an argument |
| 4847 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4848 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4849 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4850 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4851 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4852 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4853 | #ifdef NV_EXTENSIONS |
| 4854 | spv::Id resultStruct = spv::NoResult; |
| 4855 | if (imageFootprint) { |
| 4856 | //Following three extra arguments |
| 4857 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4858 | params.granularity = arguments[2 + extraArgs]; |
| 4859 | params.coarse = arguments[3 + extraArgs]; |
| 4860 | resultStruct = arguments[4 + extraArgs]; |
| 4861 | extraArgs += 3; |
| 4862 | } |
| 4863 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4864 | // bias |
| 4865 | if (bias) { |
| 4866 | params.bias = arguments[2 + extraArgs]; |
| 4867 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4868 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4869 | |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4870 | #ifdef NV_EXTENSIONS |
| 4871 | if (imageFootprint) { |
| 4872 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 4873 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 4874 | |
| 4875 | |
| 4876 | //resultStructType(OpenGL type) contains 5 elements: |
| 4877 | //struct gl_TextureFootprint2DNV { |
| 4878 | // uvec2 anchor; |
| 4879 | // uvec2 offset; |
| 4880 | // uvec2 mask; |
| 4881 | // uint lod; |
| 4882 | // uint granularity; |
| 4883 | //}; |
| 4884 | //or |
| 4885 | //struct gl_TextureFootprint3DNV { |
| 4886 | // uvec3 anchor; |
| 4887 | // uvec3 offset; |
| 4888 | // uvec2 mask; |
| 4889 | // uint lod; |
| 4890 | // uint granularity; |
| 4891 | //}; |
| 4892 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 4893 | assert(builder.isStructType(resultStructType)); |
| 4894 | |
| 4895 | //resType (SPIR-V type) contains 6 elements: |
| 4896 | //Member 0 must be a Boolean type scalar(LOD), |
| 4897 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 4898 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 4899 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 4900 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 4901 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 4902 | std::vector<spv::Id> members; |
| 4903 | members.push_back(resultType()); |
| 4904 | for (int i = 0; i < 5; i++) { |
| 4905 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 4906 | } |
| 4907 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 4908 | |
| 4909 | //call ImageFootprintNV |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4910 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
| 4911 | cracked.gather, noImplicitLod, params, signExtensionMask()); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4912 | |
| 4913 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 4914 | for (int i = 0; i < 5; i++) { |
| 4915 | builder.clearAccessChain(); |
| 4916 | builder.setAccessChainLValue(resultStruct); |
| 4917 | |
| 4918 | //Accessing to a struct we created, no coherent flag is set |
| 4919 | spv::Builder::AccessChain::CoherentFlags flags; |
| 4920 | flags.clear(); |
| 4921 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4922 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4923 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1)); |
| 4924 | } |
| 4925 | return builder.createCompositeExtract(res, resultType(), 0); |
| 4926 | } |
| 4927 | #endif |
| 4928 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4929 | // projective component (might not to move) |
| 4930 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 4931 | // are divided by the last component of P." |
| 4932 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 4933 | // unused components will appear after all used components." |
| 4934 | if (cracked.proj) { |
| 4935 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 4936 | int projTargetComp; |
| 4937 | switch (sampler.dim) { |
| 4938 | case glslang::Esd1D: projTargetComp = 1; break; |
| 4939 | case glslang::Esd2D: projTargetComp = 2; break; |
| 4940 | case glslang::EsdRect: projTargetComp = 2; break; |
| 4941 | default: projTargetComp = projSourceComp; break; |
| 4942 | } |
| 4943 | // copy the projective coordinate if we have to |
| 4944 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 4945 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4946 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 4947 | projSourceComp); |
| 4948 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 4949 | builder.getTypeId(params.coords), projTargetComp); |
| 4950 | } |
| 4951 | } |
| 4952 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4953 | // nonprivate |
| 4954 | if (imageType.getQualifier().nonprivate) { |
| 4955 | params.nonprivate = true; |
| 4956 | } |
| 4957 | |
| 4958 | // volatile |
| 4959 | if (imageType.getQualifier().volatil) { |
| 4960 | params.volatil = true; |
| 4961 | } |
| 4962 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4963 | std::vector<spv::Id> result( 1, |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4964 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
| 4965 | noImplicitLod, params, signExtensionMask()) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4966 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4967 | |
| 4968 | if (components != node->getType().getVectorSize()) |
| 4969 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4970 | |
| 4971 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4972 | } |
| 4973 | |
| 4974 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 4975 | { |
| 4976 | // Grab the function's pointer from the previously created function |
| 4977 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 4978 | if (! function) |
| 4979 | return 0; |
| 4980 | |
| 4981 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 4982 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 4983 | |
| 4984 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 4985 | // |
| 4986 | // These imply we need a four step process: |
| 4987 | // 1. Evaluate the arguments |
| 4988 | // 2. Allocate and make copies of in, out, and inout arguments |
| 4989 | // 3. Make the call |
| 4990 | // 4. Copy back the results |
| 4991 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4992 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4993 | std::vector<spv::Builder::AccessChain> lValues; |
| 4994 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4995 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4996 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4997 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4998 | // build l-value |
| 4999 | builder.clearAccessChain(); |
| 5000 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5001 | // 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] | 5002 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 5003 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5004 | // save l-value |
| 5005 | lValues.push_back(builder.getAccessChain()); |
| 5006 | } else { |
| 5007 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5008 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5009 | } |
| 5010 | } |
| 5011 | |
| 5012 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 5013 | // copy the original into that space. |
| 5014 | // |
| 5015 | // Also, build up the list of actual arguments to pass in for the call |
| 5016 | int lValueCount = 0; |
| 5017 | int rValueCount = 0; |
| 5018 | std::vector<spv::Id> spvArgs; |
| 5019 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 5020 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5021 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 5022 | builder.setAccessChain(lValues[lValueCount]); |
| 5023 | arg = builder.accessChainGetLValue(); |
| 5024 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5025 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5026 | // need space to hold the copy |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5027 | arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5028 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 5029 | // need to copy the input into output space |
| 5030 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5031 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 5032 | builder.clearAccessChain(); |
| 5033 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5034 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5035 | } |
| 5036 | ++lValueCount; |
| 5037 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5038 | // process r-value, which involves a copy for a type mismatch |
| 5039 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) { |
| 5040 | spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg"); |
| 5041 | builder.clearAccessChain(); |
| 5042 | builder.setAccessChainLValue(argCopy); |
| 5043 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 5044 | arg = builder.createLoad(argCopy); |
| 5045 | } else |
| 5046 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5047 | ++rValueCount; |
| 5048 | } |
| 5049 | spvArgs.push_back(arg); |
| 5050 | } |
| 5051 | |
| 5052 | // 3. Make the call. |
| 5053 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5054 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5055 | |
| 5056 | // 4. Copy back out an "out" arguments. |
| 5057 | lValueCount = 0; |
| 5058 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5059 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5060 | ++lValueCount; |
| 5061 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5062 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 5063 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 5064 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5065 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5066 | } |
| 5067 | ++lValueCount; |
| 5068 | } |
| 5069 | } |
| 5070 | |
| 5071 | return result; |
| 5072 | } |
| 5073 | |
| 5074 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5075 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5076 | spv::Id typeId, spv::Id left, spv::Id right, |
| 5077 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 5078 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5079 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5080 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5081 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5082 | |
| 5083 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5084 | 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] | 5085 | bool comparison = false; |
| 5086 | |
| 5087 | switch (op) { |
| 5088 | case glslang::EOpAdd: |
| 5089 | case glslang::EOpAddAssign: |
| 5090 | if (isFloat) |
| 5091 | binOp = spv::OpFAdd; |
| 5092 | else |
| 5093 | binOp = spv::OpIAdd; |
| 5094 | break; |
| 5095 | case glslang::EOpSub: |
| 5096 | case glslang::EOpSubAssign: |
| 5097 | if (isFloat) |
| 5098 | binOp = spv::OpFSub; |
| 5099 | else |
| 5100 | binOp = spv::OpISub; |
| 5101 | break; |
| 5102 | case glslang::EOpMul: |
| 5103 | case glslang::EOpMulAssign: |
| 5104 | if (isFloat) |
| 5105 | binOp = spv::OpFMul; |
| 5106 | else |
| 5107 | binOp = spv::OpIMul; |
| 5108 | break; |
| 5109 | case glslang::EOpVectorTimesScalar: |
| 5110 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5111 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5112 | if (builder.isVector(right)) |
| 5113 | std::swap(left, right); |
| 5114 | assert(builder.isScalar(right)); |
| 5115 | needMatchingVectors = false; |
| 5116 | binOp = spv::OpVectorTimesScalar; |
t.jung | 697fdf0 | 2018-11-14 13:04:39 +0100 | [diff] [blame] | 5117 | } else if (isFloat) |
| 5118 | binOp = spv::OpFMul; |
| 5119 | else |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5120 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5121 | break; |
| 5122 | case glslang::EOpVectorTimesMatrix: |
| 5123 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5124 | binOp = spv::OpVectorTimesMatrix; |
| 5125 | break; |
| 5126 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5127 | binOp = spv::OpMatrixTimesVector; |
| 5128 | break; |
| 5129 | case glslang::EOpMatrixTimesScalar: |
| 5130 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5131 | binOp = spv::OpMatrixTimesScalar; |
| 5132 | break; |
| 5133 | case glslang::EOpMatrixTimesMatrix: |
| 5134 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5135 | binOp = spv::OpMatrixTimesMatrix; |
| 5136 | break; |
| 5137 | case glslang::EOpOuterProduct: |
| 5138 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5139 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5140 | break; |
| 5141 | |
| 5142 | case glslang::EOpDiv: |
| 5143 | case glslang::EOpDivAssign: |
| 5144 | if (isFloat) |
| 5145 | binOp = spv::OpFDiv; |
| 5146 | else if (isUnsigned) |
| 5147 | binOp = spv::OpUDiv; |
| 5148 | else |
| 5149 | binOp = spv::OpSDiv; |
| 5150 | break; |
| 5151 | case glslang::EOpMod: |
| 5152 | case glslang::EOpModAssign: |
| 5153 | if (isFloat) |
| 5154 | binOp = spv::OpFMod; |
| 5155 | else if (isUnsigned) |
| 5156 | binOp = spv::OpUMod; |
| 5157 | else |
| 5158 | binOp = spv::OpSMod; |
| 5159 | break; |
| 5160 | case glslang::EOpRightShift: |
| 5161 | case glslang::EOpRightShiftAssign: |
| 5162 | if (isUnsigned) |
| 5163 | binOp = spv::OpShiftRightLogical; |
| 5164 | else |
| 5165 | binOp = spv::OpShiftRightArithmetic; |
| 5166 | break; |
| 5167 | case glslang::EOpLeftShift: |
| 5168 | case glslang::EOpLeftShiftAssign: |
| 5169 | binOp = spv::OpShiftLeftLogical; |
| 5170 | break; |
| 5171 | case glslang::EOpAnd: |
| 5172 | case glslang::EOpAndAssign: |
| 5173 | binOp = spv::OpBitwiseAnd; |
| 5174 | break; |
| 5175 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5176 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5177 | binOp = spv::OpLogicalAnd; |
| 5178 | break; |
| 5179 | case glslang::EOpInclusiveOr: |
| 5180 | case glslang::EOpInclusiveOrAssign: |
| 5181 | binOp = spv::OpBitwiseOr; |
| 5182 | break; |
| 5183 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5184 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5185 | binOp = spv::OpLogicalOr; |
| 5186 | break; |
| 5187 | case glslang::EOpExclusiveOr: |
| 5188 | case glslang::EOpExclusiveOrAssign: |
| 5189 | binOp = spv::OpBitwiseXor; |
| 5190 | break; |
| 5191 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5192 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5193 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5194 | break; |
| 5195 | |
| 5196 | case glslang::EOpLessThan: |
| 5197 | case glslang::EOpGreaterThan: |
| 5198 | case glslang::EOpLessThanEqual: |
| 5199 | case glslang::EOpGreaterThanEqual: |
| 5200 | case glslang::EOpEqual: |
| 5201 | case glslang::EOpNotEqual: |
| 5202 | case glslang::EOpVectorEqual: |
| 5203 | case glslang::EOpVectorNotEqual: |
| 5204 | comparison = true; |
| 5205 | break; |
| 5206 | default: |
| 5207 | break; |
| 5208 | } |
| 5209 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5210 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5211 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5212 | assert(comparison == false); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5213 | if (builder.isMatrix(left) || builder.isMatrix(right) || |
| 5214 | builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5215 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5216 | |
| 5217 | // 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] | 5218 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5219 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5220 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5221 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5222 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5223 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5224 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | if (! comparison) |
| 5228 | return 0; |
| 5229 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5230 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5231 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 5232 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5233 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 5234 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5235 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5236 | return result; |
| 5237 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5238 | |
| 5239 | switch (op) { |
| 5240 | case glslang::EOpLessThan: |
| 5241 | if (isFloat) |
| 5242 | binOp = spv::OpFOrdLessThan; |
| 5243 | else if (isUnsigned) |
| 5244 | binOp = spv::OpULessThan; |
| 5245 | else |
| 5246 | binOp = spv::OpSLessThan; |
| 5247 | break; |
| 5248 | case glslang::EOpGreaterThan: |
| 5249 | if (isFloat) |
| 5250 | binOp = spv::OpFOrdGreaterThan; |
| 5251 | else if (isUnsigned) |
| 5252 | binOp = spv::OpUGreaterThan; |
| 5253 | else |
| 5254 | binOp = spv::OpSGreaterThan; |
| 5255 | break; |
| 5256 | case glslang::EOpLessThanEqual: |
| 5257 | if (isFloat) |
| 5258 | binOp = spv::OpFOrdLessThanEqual; |
| 5259 | else if (isUnsigned) |
| 5260 | binOp = spv::OpULessThanEqual; |
| 5261 | else |
| 5262 | binOp = spv::OpSLessThanEqual; |
| 5263 | break; |
| 5264 | case glslang::EOpGreaterThanEqual: |
| 5265 | if (isFloat) |
| 5266 | binOp = spv::OpFOrdGreaterThanEqual; |
| 5267 | else if (isUnsigned) |
| 5268 | binOp = spv::OpUGreaterThanEqual; |
| 5269 | else |
| 5270 | binOp = spv::OpSGreaterThanEqual; |
| 5271 | break; |
| 5272 | case glslang::EOpEqual: |
| 5273 | case glslang::EOpVectorEqual: |
| 5274 | if (isFloat) |
| 5275 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5276 | else if (isBool) |
| 5277 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5278 | else |
| 5279 | binOp = spv::OpIEqual; |
| 5280 | break; |
| 5281 | case glslang::EOpNotEqual: |
| 5282 | case glslang::EOpVectorNotEqual: |
| 5283 | if (isFloat) |
| 5284 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5285 | else if (isBool) |
| 5286 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5287 | else |
| 5288 | binOp = spv::OpINotEqual; |
| 5289 | break; |
| 5290 | default: |
| 5291 | break; |
| 5292 | } |
| 5293 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5294 | if (binOp != spv::OpNop) { |
| 5295 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5296 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5297 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5298 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5299 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5300 | |
| 5301 | return 0; |
| 5302 | } |
| 5303 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5304 | // |
| 5305 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 5306 | // These can be any of: |
| 5307 | // |
| 5308 | // matrix * scalar |
| 5309 | // scalar * matrix |
| 5310 | // matrix * matrix linear algebraic |
| 5311 | // matrix * vector |
| 5312 | // vector * matrix |
| 5313 | // matrix * matrix componentwise |
| 5314 | // matrix op matrix op in {+, -, /} |
| 5315 | // matrix op scalar op in {+, -, /} |
| 5316 | // scalar op matrix op in {+, -, /} |
| 5317 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5318 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5319 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5320 | { |
| 5321 | bool firstClass = true; |
| 5322 | |
| 5323 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 5324 | switch (op) { |
| 5325 | case spv::OpFDiv: |
| 5326 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 5327 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 5328 | spv::Id resultType = builder.getTypeId(right); |
| 5329 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5330 | op = spv::OpMatrixTimesScalar; |
| 5331 | } else |
| 5332 | firstClass = false; |
| 5333 | break; |
| 5334 | case spv::OpMatrixTimesScalar: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5335 | if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5336 | std::swap(left, right); |
| 5337 | assert(builder.isScalar(right)); |
| 5338 | break; |
| 5339 | case spv::OpVectorTimesMatrix: |
| 5340 | assert(builder.isVector(left)); |
| 5341 | assert(builder.isMatrix(right)); |
| 5342 | break; |
| 5343 | case spv::OpMatrixTimesVector: |
| 5344 | assert(builder.isMatrix(left)); |
| 5345 | assert(builder.isVector(right)); |
| 5346 | break; |
| 5347 | case spv::OpMatrixTimesMatrix: |
| 5348 | assert(builder.isMatrix(left)); |
| 5349 | assert(builder.isMatrix(right)); |
| 5350 | break; |
| 5351 | default: |
| 5352 | firstClass = false; |
| 5353 | break; |
| 5354 | } |
| 5355 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5356 | if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 5357 | firstClass = true; |
| 5358 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5359 | if (firstClass) { |
| 5360 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5361 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5362 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5363 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5364 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5365 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5366 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5367 | // The result type of all of them is the same type as the (a) matrix operand. |
| 5368 | // The algorithm is to: |
| 5369 | // - break the matrix(es) into vectors |
| 5370 | // - smear any scalar to a vector |
| 5371 | // - do vector operations |
| 5372 | // - make a matrix out the vector results |
| 5373 | switch (op) { |
| 5374 | case spv::OpFAdd: |
| 5375 | case spv::OpFSub: |
| 5376 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5377 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5378 | case spv::OpFMul: |
| 5379 | { |
| 5380 | // one time set up... |
| 5381 | bool leftMat = builder.isMatrix(left); |
| 5382 | bool rightMat = builder.isMatrix(right); |
| 5383 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 5384 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 5385 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 5386 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 5387 | std::vector<spv::Id> results; |
| 5388 | spv::Id smearVec = spv::NoResult; |
| 5389 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5390 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5391 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5392 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5393 | |
| 5394 | // do each vector op |
| 5395 | for (unsigned int c = 0; c < numCols; ++c) { |
| 5396 | std::vector<unsigned int> indexes; |
| 5397 | indexes.push_back(c); |
| 5398 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 5399 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5400 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5401 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5402 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5403 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5407 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5408 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5409 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5410 | } |
| 5411 | default: |
| 5412 | assert(0); |
| 5413 | return spv::NoResult; |
| 5414 | } |
| 5415 | } |
| 5416 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5417 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 5418 | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5419 | { |
| 5420 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5421 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5422 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5423 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5424 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5425 | |
| 5426 | switch (op) { |
| 5427 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5428 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5429 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5430 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5431 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5432 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5433 | unaryOp = spv::OpSNegate; |
| 5434 | break; |
| 5435 | |
| 5436 | case glslang::EOpLogicalNot: |
| 5437 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5438 | unaryOp = spv::OpLogicalNot; |
| 5439 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5440 | case glslang::EOpBitwiseNot: |
| 5441 | unaryOp = spv::OpNot; |
| 5442 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5443 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5444 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5445 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5446 | break; |
| 5447 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5448 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5449 | break; |
| 5450 | case glslang::EOpTranspose: |
| 5451 | unaryOp = spv::OpTranspose; |
| 5452 | break; |
| 5453 | |
| 5454 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5455 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5456 | break; |
| 5457 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5458 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5459 | break; |
| 5460 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5461 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5462 | break; |
| 5463 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5464 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5465 | break; |
| 5466 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5467 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5468 | break; |
| 5469 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5470 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5471 | break; |
| 5472 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5473 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5474 | break; |
| 5475 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5476 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5477 | break; |
| 5478 | |
| 5479 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5480 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5481 | break; |
| 5482 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5483 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5484 | break; |
| 5485 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5486 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5487 | break; |
| 5488 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5489 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5490 | break; |
| 5491 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5492 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5493 | break; |
| 5494 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5495 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5496 | break; |
| 5497 | |
| 5498 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5499 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5500 | break; |
| 5501 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5502 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5503 | break; |
| 5504 | |
| 5505 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5506 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5507 | break; |
| 5508 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5509 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5510 | break; |
| 5511 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5512 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5513 | break; |
| 5514 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5515 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5516 | break; |
| 5517 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5518 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5519 | break; |
| 5520 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5521 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5522 | break; |
| 5523 | |
| 5524 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5525 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5526 | break; |
| 5527 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5528 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5529 | break; |
| 5530 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5531 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5532 | break; |
| 5533 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5534 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5535 | break; |
| 5536 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5537 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5538 | break; |
| 5539 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5540 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5541 | break; |
| 5542 | |
| 5543 | case glslang::EOpIsNan: |
| 5544 | unaryOp = spv::OpIsNan; |
| 5545 | break; |
| 5546 | case glslang::EOpIsInf: |
| 5547 | unaryOp = spv::OpIsInf; |
| 5548 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5549 | case glslang::EOpIsFinite: |
| 5550 | unaryOp = spv::OpIsFinite; |
| 5551 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5552 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5553 | case glslang::EOpFloatBitsToInt: |
| 5554 | case glslang::EOpFloatBitsToUint: |
| 5555 | case glslang::EOpIntBitsToFloat: |
| 5556 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5557 | case glslang::EOpDoubleBitsToInt64: |
| 5558 | case glslang::EOpDoubleBitsToUint64: |
| 5559 | case glslang::EOpInt64BitsToDouble: |
| 5560 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5561 | case glslang::EOpFloat16BitsToInt16: |
| 5562 | case glslang::EOpFloat16BitsToUint16: |
| 5563 | case glslang::EOpInt16BitsToFloat16: |
| 5564 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5565 | unaryOp = spv::OpBitcast; |
| 5566 | break; |
| 5567 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5568 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5569 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5570 | break; |
| 5571 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5572 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5573 | break; |
| 5574 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5575 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5576 | break; |
| 5577 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5578 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5579 | break; |
| 5580 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5581 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5582 | break; |
| 5583 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5584 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5585 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5586 | case glslang::EOpPackSnorm4x8: |
| 5587 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 5588 | break; |
| 5589 | case glslang::EOpUnpackSnorm4x8: |
| 5590 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5591 | break; |
| 5592 | case glslang::EOpPackUnorm4x8: |
| 5593 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 5594 | break; |
| 5595 | case glslang::EOpUnpackUnorm4x8: |
| 5596 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5597 | break; |
| 5598 | case glslang::EOpPackDouble2x32: |
| 5599 | libCall = spv::GLSLstd450PackDouble2x32; |
| 5600 | break; |
| 5601 | case glslang::EOpUnpackDouble2x32: |
| 5602 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5603 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5604 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5605 | case glslang::EOpPackInt2x32: |
| 5606 | case glslang::EOpUnpackInt2x32: |
| 5607 | case glslang::EOpPackUint2x32: |
| 5608 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5609 | case glslang::EOpPack16: |
| 5610 | case glslang::EOpPack32: |
| 5611 | case glslang::EOpPack64: |
| 5612 | case glslang::EOpUnpack32: |
| 5613 | case glslang::EOpUnpack16: |
| 5614 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5615 | case glslang::EOpPackInt2x16: |
| 5616 | case glslang::EOpUnpackInt2x16: |
| 5617 | case glslang::EOpPackUint2x16: |
| 5618 | case glslang::EOpUnpackUint2x16: |
| 5619 | case glslang::EOpPackInt4x16: |
| 5620 | case glslang::EOpUnpackInt4x16: |
| 5621 | case glslang::EOpPackUint4x16: |
| 5622 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5623 | case glslang::EOpPackFloat2x16: |
| 5624 | case glslang::EOpUnpackFloat2x16: |
| 5625 | unaryOp = spv::OpBitcast; |
| 5626 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5627 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5628 | case glslang::EOpDPdx: |
| 5629 | unaryOp = spv::OpDPdx; |
| 5630 | break; |
| 5631 | case glslang::EOpDPdy: |
| 5632 | unaryOp = spv::OpDPdy; |
| 5633 | break; |
| 5634 | case glslang::EOpFwidth: |
| 5635 | unaryOp = spv::OpFwidth; |
| 5636 | break; |
| 5637 | case glslang::EOpDPdxFine: |
| 5638 | unaryOp = spv::OpDPdxFine; |
| 5639 | break; |
| 5640 | case glslang::EOpDPdyFine: |
| 5641 | unaryOp = spv::OpDPdyFine; |
| 5642 | break; |
| 5643 | case glslang::EOpFwidthFine: |
| 5644 | unaryOp = spv::OpFwidthFine; |
| 5645 | break; |
| 5646 | case glslang::EOpDPdxCoarse: |
| 5647 | unaryOp = spv::OpDPdxCoarse; |
| 5648 | break; |
| 5649 | case glslang::EOpDPdyCoarse: |
| 5650 | unaryOp = spv::OpDPdyCoarse; |
| 5651 | break; |
| 5652 | case glslang::EOpFwidthCoarse: |
| 5653 | unaryOp = spv::OpFwidthCoarse; |
| 5654 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5655 | case glslang::EOpInterpolateAtCentroid: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 5656 | #ifdef AMD_EXTENSIONS |
| 5657 | if (typeProxy == glslang::EbtFloat16) |
| 5658 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 5659 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5660 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 5661 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5662 | case glslang::EOpAny: |
| 5663 | unaryOp = spv::OpAny; |
| 5664 | break; |
| 5665 | case glslang::EOpAll: |
| 5666 | unaryOp = spv::OpAll; |
| 5667 | break; |
| 5668 | |
| 5669 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5670 | if (isFloat) |
| 5671 | libCall = spv::GLSLstd450FAbs; |
| 5672 | else |
| 5673 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5674 | break; |
| 5675 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5676 | if (isFloat) |
| 5677 | libCall = spv::GLSLstd450FSign; |
| 5678 | else |
| 5679 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5680 | break; |
| 5681 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5682 | case glslang::EOpAtomicCounterIncrement: |
| 5683 | case glslang::EOpAtomicCounterDecrement: |
| 5684 | case glslang::EOpAtomicCounter: |
| 5685 | { |
| 5686 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 5687 | std::vector<spv::Id> operands; |
| 5688 | operands.push_back(operand); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 5689 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5690 | } |
| 5691 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5692 | case glslang::EOpBitFieldReverse: |
| 5693 | unaryOp = spv::OpBitReverse; |
| 5694 | break; |
| 5695 | case glslang::EOpBitCount: |
| 5696 | unaryOp = spv::OpBitCount; |
| 5697 | break; |
| 5698 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5699 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5700 | break; |
| 5701 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5702 | if (isUnsigned) |
| 5703 | libCall = spv::GLSLstd450FindUMsb; |
| 5704 | else |
| 5705 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5706 | break; |
| 5707 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5708 | case glslang::EOpBallot: |
| 5709 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5710 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5711 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5712 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5713 | #ifdef AMD_EXTENSIONS |
| 5714 | case glslang::EOpMinInvocations: |
| 5715 | case glslang::EOpMaxInvocations: |
| 5716 | case glslang::EOpAddInvocations: |
| 5717 | case glslang::EOpMinInvocationsNonUniform: |
| 5718 | case glslang::EOpMaxInvocationsNonUniform: |
| 5719 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5720 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5721 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5722 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5723 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5724 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5725 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5726 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5727 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5728 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5729 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5730 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5731 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5732 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5733 | { |
| 5734 | std::vector<spv::Id> operands; |
| 5735 | operands.push_back(operand); |
| 5736 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 5737 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5738 | case glslang::EOpSubgroupAll: |
| 5739 | case glslang::EOpSubgroupAny: |
| 5740 | case glslang::EOpSubgroupAllEqual: |
| 5741 | case glslang::EOpSubgroupBroadcastFirst: |
| 5742 | case glslang::EOpSubgroupBallot: |
| 5743 | case glslang::EOpSubgroupInverseBallot: |
| 5744 | case glslang::EOpSubgroupBallotBitCount: |
| 5745 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5746 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5747 | case glslang::EOpSubgroupBallotFindLSB: |
| 5748 | case glslang::EOpSubgroupBallotFindMSB: |
| 5749 | case glslang::EOpSubgroupAdd: |
| 5750 | case glslang::EOpSubgroupMul: |
| 5751 | case glslang::EOpSubgroupMin: |
| 5752 | case glslang::EOpSubgroupMax: |
| 5753 | case glslang::EOpSubgroupAnd: |
| 5754 | case glslang::EOpSubgroupOr: |
| 5755 | case glslang::EOpSubgroupXor: |
| 5756 | case glslang::EOpSubgroupInclusiveAdd: |
| 5757 | case glslang::EOpSubgroupInclusiveMul: |
| 5758 | case glslang::EOpSubgroupInclusiveMin: |
| 5759 | case glslang::EOpSubgroupInclusiveMax: |
| 5760 | case glslang::EOpSubgroupInclusiveAnd: |
| 5761 | case glslang::EOpSubgroupInclusiveOr: |
| 5762 | case glslang::EOpSubgroupInclusiveXor: |
| 5763 | case glslang::EOpSubgroupExclusiveAdd: |
| 5764 | case glslang::EOpSubgroupExclusiveMul: |
| 5765 | case glslang::EOpSubgroupExclusiveMin: |
| 5766 | case glslang::EOpSubgroupExclusiveMax: |
| 5767 | case glslang::EOpSubgroupExclusiveAnd: |
| 5768 | case glslang::EOpSubgroupExclusiveOr: |
| 5769 | case glslang::EOpSubgroupExclusiveXor: |
| 5770 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5771 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5772 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 5773 | std::vector<spv::Id> operands; |
| 5774 | operands.push_back(operand); |
| 5775 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 5776 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5777 | #ifdef AMD_EXTENSIONS |
| 5778 | case glslang::EOpMbcnt: |
| 5779 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5780 | libCall = spv::MbcntAMD; |
| 5781 | break; |
| 5782 | |
| 5783 | case glslang::EOpCubeFaceIndex: |
| 5784 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5785 | libCall = spv::CubeFaceIndexAMD; |
| 5786 | break; |
| 5787 | |
| 5788 | case glslang::EOpCubeFaceCoord: |
| 5789 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5790 | libCall = spv::CubeFaceCoordAMD; |
| 5791 | break; |
| 5792 | #endif |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5793 | #ifdef NV_EXTENSIONS |
| 5794 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5795 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 5796 | break; |
| 5797 | #endif |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 5798 | case glslang::EOpConstructReference: |
| 5799 | unaryOp = spv::OpBitcast; |
| 5800 | break; |
Jeff Bolz | 88220d5 | 2019-05-08 10:24:46 -0500 | [diff] [blame] | 5801 | |
| 5802 | case glslang::EOpCopyObject: |
| 5803 | unaryOp = spv::OpCopyObject; |
| 5804 | break; |
| 5805 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5806 | default: |
| 5807 | return 0; |
| 5808 | } |
| 5809 | |
| 5810 | spv::Id id; |
| 5811 | if (libCall >= 0) { |
| 5812 | std::vector<spv::Id> args; |
| 5813 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5814 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5815 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5816 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5817 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5818 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5819 | builder.addDecoration(id, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5820 | builder.addDecoration(id, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5821 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5822 | } |
| 5823 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5824 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5825 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5826 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5827 | { |
| 5828 | // Handle unary operations vector by vector. |
| 5829 | // The result type is the same type as the original type. |
| 5830 | // The algorithm is to: |
| 5831 | // - break the matrix into vectors |
| 5832 | // - apply the operation to each vector |
| 5833 | // - make a matrix out the vector results |
| 5834 | |
| 5835 | // get the types sorted out |
| 5836 | int numCols = builder.getNumColumns(operand); |
| 5837 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5838 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 5839 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5840 | std::vector<spv::Id> results; |
| 5841 | |
| 5842 | // do each vector op |
| 5843 | for (int c = 0; c < numCols; ++c) { |
| 5844 | std::vector<unsigned int> indexes; |
| 5845 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5846 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 5847 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5848 | builder.addDecoration(destVec, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5849 | builder.addDecoration(destVec, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5850 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5854 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5855 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5856 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5857 | } |
| 5858 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5859 | // For converting integers where both the bitwidth and the signedness could |
| 5860 | // change, but only do the width change here. The caller is still responsible |
| 5861 | // for the signedness conversion. |
| 5862 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5863 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5864 | // Get the result type width, based on the type to convert to. |
| 5865 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5866 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5867 | case glslang::EOpConvInt16ToUint8: |
| 5868 | case glslang::EOpConvIntToUint8: |
| 5869 | case glslang::EOpConvInt64ToUint8: |
| 5870 | case glslang::EOpConvUint16ToInt8: |
| 5871 | case glslang::EOpConvUintToInt8: |
| 5872 | case glslang::EOpConvUint64ToInt8: |
| 5873 | width = 8; |
| 5874 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5875 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5876 | case glslang::EOpConvIntToUint16: |
| 5877 | case glslang::EOpConvInt64ToUint16: |
| 5878 | case glslang::EOpConvUint8ToInt16: |
| 5879 | case glslang::EOpConvUintToInt16: |
| 5880 | case glslang::EOpConvUint64ToInt16: |
| 5881 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5882 | break; |
| 5883 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5884 | case glslang::EOpConvInt16ToUint: |
| 5885 | case glslang::EOpConvInt64ToUint: |
| 5886 | case glslang::EOpConvUint8ToInt: |
| 5887 | case glslang::EOpConvUint16ToInt: |
| 5888 | case glslang::EOpConvUint64ToInt: |
| 5889 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5890 | break; |
| 5891 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5892 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5893 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5894 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5895 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5896 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5897 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5898 | break; |
| 5899 | |
| 5900 | default: |
| 5901 | assert(false && "Default missing"); |
| 5902 | break; |
| 5903 | } |
| 5904 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5905 | // Get the conversion operation and result type, |
| 5906 | // based on the target width, but the source type. |
| 5907 | spv::Id type = spv::NoType; |
| 5908 | spv::Op convOp = spv::OpNop; |
| 5909 | switch(op) { |
| 5910 | case glslang::EOpConvInt8ToUint16: |
| 5911 | case glslang::EOpConvInt8ToUint: |
| 5912 | case glslang::EOpConvInt8ToUint64: |
| 5913 | case glslang::EOpConvInt16ToUint8: |
| 5914 | case glslang::EOpConvInt16ToUint: |
| 5915 | case glslang::EOpConvInt16ToUint64: |
| 5916 | case glslang::EOpConvIntToUint8: |
| 5917 | case glslang::EOpConvIntToUint16: |
| 5918 | case glslang::EOpConvIntToUint64: |
| 5919 | case glslang::EOpConvInt64ToUint8: |
| 5920 | case glslang::EOpConvInt64ToUint16: |
| 5921 | case glslang::EOpConvInt64ToUint: |
| 5922 | convOp = spv::OpSConvert; |
| 5923 | type = builder.makeIntType(width); |
| 5924 | break; |
| 5925 | default: |
| 5926 | convOp = spv::OpUConvert; |
| 5927 | type = builder.makeUintType(width); |
| 5928 | break; |
| 5929 | } |
| 5930 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5931 | if (vectorSize > 0) |
| 5932 | type = builder.makeVectorType(type, vectorSize); |
| 5933 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5934 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5935 | } |
| 5936 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5937 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 5938 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5939 | { |
| 5940 | spv::Op convOp = spv::OpNop; |
| 5941 | spv::Id zero = 0; |
| 5942 | spv::Id one = 0; |
| 5943 | |
| 5944 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 5945 | |
| 5946 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5947 | case glslang::EOpConvInt8ToBool: |
| 5948 | case glslang::EOpConvUint8ToBool: |
| 5949 | zero = builder.makeUint8Constant(0); |
| 5950 | zero = makeSmearedConstant(zero, vectorSize); |
| 5951 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5952 | case glslang::EOpConvInt16ToBool: |
| 5953 | case glslang::EOpConvUint16ToBool: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5954 | zero = builder.makeUint16Constant(0); |
| 5955 | zero = makeSmearedConstant(zero, vectorSize); |
| 5956 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5957 | case glslang::EOpConvIntToBool: |
| 5958 | case glslang::EOpConvUintToBool: |
| 5959 | zero = builder.makeUintConstant(0); |
| 5960 | zero = makeSmearedConstant(zero, vectorSize); |
| 5961 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5962 | case glslang::EOpConvInt64ToBool: |
| 5963 | case glslang::EOpConvUint64ToBool: |
| 5964 | zero = builder.makeUint64Constant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5965 | zero = makeSmearedConstant(zero, vectorSize); |
| 5966 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5967 | |
| 5968 | case glslang::EOpConvFloatToBool: |
| 5969 | zero = builder.makeFloatConstant(0.0F); |
| 5970 | zero = makeSmearedConstant(zero, vectorSize); |
| 5971 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5972 | |
| 5973 | case glslang::EOpConvDoubleToBool: |
| 5974 | zero = builder.makeDoubleConstant(0.0); |
| 5975 | zero = makeSmearedConstant(zero, vectorSize); |
| 5976 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5977 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5978 | case glslang::EOpConvFloat16ToBool: |
| 5979 | zero = builder.makeFloat16Constant(0.0F); |
| 5980 | zero = makeSmearedConstant(zero, vectorSize); |
| 5981 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5982 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5983 | case glslang::EOpConvBoolToFloat: |
| 5984 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5985 | zero = builder.makeFloatConstant(0.0F); |
| 5986 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5987 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5988 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5989 | case glslang::EOpConvBoolToDouble: |
| 5990 | convOp = spv::OpSelect; |
| 5991 | zero = builder.makeDoubleConstant(0.0); |
| 5992 | one = builder.makeDoubleConstant(1.0); |
| 5993 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5994 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5995 | case glslang::EOpConvBoolToFloat16: |
| 5996 | convOp = spv::OpSelect; |
| 5997 | zero = builder.makeFloat16Constant(0.0F); |
| 5998 | one = builder.makeFloat16Constant(1.0F); |
| 5999 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6000 | |
| 6001 | case glslang::EOpConvBoolToInt8: |
| 6002 | zero = builder.makeInt8Constant(0); |
| 6003 | one = builder.makeInt8Constant(1); |
| 6004 | convOp = spv::OpSelect; |
| 6005 | break; |
| 6006 | |
| 6007 | case glslang::EOpConvBoolToUint8: |
| 6008 | zero = builder.makeUint8Constant(0); |
| 6009 | one = builder.makeUint8Constant(1); |
| 6010 | convOp = spv::OpSelect; |
| 6011 | break; |
| 6012 | |
| 6013 | case glslang::EOpConvBoolToInt16: |
| 6014 | zero = builder.makeInt16Constant(0); |
| 6015 | one = builder.makeInt16Constant(1); |
| 6016 | convOp = spv::OpSelect; |
| 6017 | break; |
| 6018 | |
| 6019 | case glslang::EOpConvBoolToUint16: |
| 6020 | zero = builder.makeUint16Constant(0); |
| 6021 | one = builder.makeUint16Constant(1); |
| 6022 | convOp = spv::OpSelect; |
| 6023 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6024 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6025 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6026 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6027 | if (op == glslang::EOpConvBoolToInt64) |
| 6028 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6029 | else |
| 6030 | zero = builder.makeIntConstant(0); |
| 6031 | |
| 6032 | if (op == glslang::EOpConvBoolToInt64) |
| 6033 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6034 | else |
| 6035 | one = builder.makeIntConstant(1); |
| 6036 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6037 | convOp = spv::OpSelect; |
| 6038 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6039 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6040 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6041 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6042 | if (op == glslang::EOpConvBoolToUint64) |
| 6043 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6044 | else |
| 6045 | zero = builder.makeUintConstant(0); |
| 6046 | |
| 6047 | if (op == glslang::EOpConvBoolToUint64) |
| 6048 | one = builder.makeUint64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6049 | else |
| 6050 | one = builder.makeUintConstant(1); |
| 6051 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6052 | convOp = spv::OpSelect; |
| 6053 | break; |
| 6054 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6055 | case glslang::EOpConvInt8ToFloat16: |
| 6056 | case glslang::EOpConvInt8ToFloat: |
| 6057 | case glslang::EOpConvInt8ToDouble: |
| 6058 | case glslang::EOpConvInt16ToFloat16: |
| 6059 | case glslang::EOpConvInt16ToFloat: |
| 6060 | case glslang::EOpConvInt16ToDouble: |
| 6061 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6062 | case glslang::EOpConvIntToFloat: |
| 6063 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6064 | case glslang::EOpConvInt64ToFloat: |
| 6065 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6066 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6067 | convOp = spv::OpConvertSToF; |
| 6068 | break; |
| 6069 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6070 | case glslang::EOpConvUint8ToFloat16: |
| 6071 | case glslang::EOpConvUint8ToFloat: |
| 6072 | case glslang::EOpConvUint8ToDouble: |
| 6073 | case glslang::EOpConvUint16ToFloat16: |
| 6074 | case glslang::EOpConvUint16ToFloat: |
| 6075 | case glslang::EOpConvUint16ToDouble: |
| 6076 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6077 | case glslang::EOpConvUintToFloat: |
| 6078 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6079 | case glslang::EOpConvUint64ToFloat: |
| 6080 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6081 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6082 | convOp = spv::OpConvertUToF; |
| 6083 | break; |
| 6084 | |
| 6085 | case glslang::EOpConvDoubleToFloat: |
| 6086 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6087 | case glslang::EOpConvDoubleToFloat16: |
| 6088 | case glslang::EOpConvFloat16ToDouble: |
| 6089 | case glslang::EOpConvFloatToFloat16: |
| 6090 | case glslang::EOpConvFloat16ToFloat: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6091 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 6092 | if (builder.isMatrixType(destType)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6093 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6094 | break; |
| 6095 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6096 | case glslang::EOpConvFloat16ToInt8: |
| 6097 | case glslang::EOpConvFloatToInt8: |
| 6098 | case glslang::EOpConvDoubleToInt8: |
| 6099 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6100 | case glslang::EOpConvFloatToInt16: |
| 6101 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6102 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6103 | case glslang::EOpConvFloatToInt: |
| 6104 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6105 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6106 | case glslang::EOpConvFloatToInt64: |
| 6107 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6108 | convOp = spv::OpConvertFToS; |
| 6109 | break; |
| 6110 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6111 | case glslang::EOpConvUint8ToInt8: |
| 6112 | case glslang::EOpConvInt8ToUint8: |
| 6113 | case glslang::EOpConvUint16ToInt16: |
| 6114 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6115 | case glslang::EOpConvUintToInt: |
| 6116 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6117 | case glslang::EOpConvUint64ToInt64: |
| 6118 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6119 | if (builder.isInSpecConstCodeGenMode()) { |
| 6120 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6121 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 6122 | zero = builder.makeUint8Constant(0); |
| 6123 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6124 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6125 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 6126 | zero = builder.makeUint64Constant(0); |
| 6127 | } else { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6128 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6129 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 6130 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6131 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6132 | // generating for OpSpecConstantOp instruction. |
| 6133 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6134 | } |
| 6135 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6136 | convOp = spv::OpBitcast; |
| 6137 | break; |
| 6138 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6139 | case glslang::EOpConvFloat16ToUint8: |
| 6140 | case glslang::EOpConvFloatToUint8: |
| 6141 | case glslang::EOpConvDoubleToUint8: |
| 6142 | case glslang::EOpConvFloat16ToUint16: |
| 6143 | case glslang::EOpConvFloatToUint16: |
| 6144 | case glslang::EOpConvDoubleToUint16: |
| 6145 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6146 | case glslang::EOpConvFloatToUint: |
| 6147 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6148 | case glslang::EOpConvFloatToUint64: |
| 6149 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6150 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6151 | convOp = spv::OpConvertFToU; |
| 6152 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6153 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6154 | case glslang::EOpConvInt8ToInt16: |
| 6155 | case glslang::EOpConvInt8ToInt: |
| 6156 | case glslang::EOpConvInt8ToInt64: |
| 6157 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6158 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6159 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6160 | case glslang::EOpConvIntToInt8: |
| 6161 | case glslang::EOpConvIntToInt16: |
| 6162 | case glslang::EOpConvIntToInt64: |
| 6163 | case glslang::EOpConvInt64ToInt8: |
| 6164 | case glslang::EOpConvInt64ToInt16: |
| 6165 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6166 | convOp = spv::OpSConvert; |
| 6167 | break; |
| 6168 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6169 | case glslang::EOpConvUint8ToUint16: |
| 6170 | case glslang::EOpConvUint8ToUint: |
| 6171 | case glslang::EOpConvUint8ToUint64: |
| 6172 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6173 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6174 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6175 | case glslang::EOpConvUintToUint8: |
| 6176 | case glslang::EOpConvUintToUint16: |
| 6177 | case glslang::EOpConvUintToUint64: |
| 6178 | case glslang::EOpConvUint64ToUint8: |
| 6179 | case glslang::EOpConvUint64ToUint16: |
| 6180 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6181 | convOp = spv::OpUConvert; |
| 6182 | break; |
| 6183 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6184 | case glslang::EOpConvInt8ToUint16: |
| 6185 | case glslang::EOpConvInt8ToUint: |
| 6186 | case glslang::EOpConvInt8ToUint64: |
| 6187 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6188 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6189 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6190 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6191 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6192 | case glslang::EOpConvIntToUint64: |
| 6193 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6194 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6195 | case glslang::EOpConvInt64ToUint: |
| 6196 | case glslang::EOpConvUint8ToInt16: |
| 6197 | case glslang::EOpConvUint8ToInt: |
| 6198 | case glslang::EOpConvUint8ToInt64: |
| 6199 | case glslang::EOpConvUint16ToInt8: |
| 6200 | case glslang::EOpConvUint16ToInt: |
| 6201 | case glslang::EOpConvUint16ToInt64: |
| 6202 | case glslang::EOpConvUintToInt8: |
| 6203 | case glslang::EOpConvUintToInt16: |
| 6204 | case glslang::EOpConvUintToInt64: |
| 6205 | case glslang::EOpConvUint64ToInt8: |
| 6206 | case glslang::EOpConvUint64ToInt16: |
| 6207 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6208 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6209 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6210 | |
| 6211 | if (builder.isInSpecConstCodeGenMode()) { |
| 6212 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6213 | switch(op) { |
| 6214 | case glslang::EOpConvInt16ToUint8: |
| 6215 | case glslang::EOpConvIntToUint8: |
| 6216 | case glslang::EOpConvInt64ToUint8: |
| 6217 | case glslang::EOpConvUint16ToInt8: |
| 6218 | case glslang::EOpConvUintToInt8: |
| 6219 | case glslang::EOpConvUint64ToInt8: |
| 6220 | zero = builder.makeUint8Constant(0); |
| 6221 | break; |
| 6222 | case glslang::EOpConvInt8ToUint16: |
| 6223 | case glslang::EOpConvIntToUint16: |
| 6224 | case glslang::EOpConvInt64ToUint16: |
| 6225 | case glslang::EOpConvUint8ToInt16: |
| 6226 | case glslang::EOpConvUintToInt16: |
| 6227 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6228 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6229 | break; |
| 6230 | case glslang::EOpConvInt8ToUint: |
| 6231 | case glslang::EOpConvInt16ToUint: |
| 6232 | case glslang::EOpConvInt64ToUint: |
| 6233 | case glslang::EOpConvUint8ToInt: |
| 6234 | case glslang::EOpConvUint16ToInt: |
| 6235 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6236 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6237 | break; |
| 6238 | case glslang::EOpConvInt8ToUint64: |
| 6239 | case glslang::EOpConvInt16ToUint64: |
| 6240 | case glslang::EOpConvIntToUint64: |
| 6241 | case glslang::EOpConvUint8ToInt64: |
| 6242 | case glslang::EOpConvUint16ToInt64: |
| 6243 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6244 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6245 | break; |
| 6246 | default: |
| 6247 | assert(false && "Default missing"); |
| 6248 | break; |
| 6249 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6250 | zero = makeSmearedConstant(zero, vectorSize); |
| 6251 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6252 | // generating for OpSpecConstantOp instruction. |
| 6253 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6254 | } |
| 6255 | // For normal run-time conversion instruction, use OpBitcast. |
| 6256 | convOp = spv::OpBitcast; |
| 6257 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6258 | case glslang::EOpConvUint64ToPtr: |
| 6259 | convOp = spv::OpConvertUToPtr; |
| 6260 | break; |
| 6261 | case glslang::EOpConvPtrToUint64: |
| 6262 | convOp = spv::OpConvertPtrToU; |
| 6263 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6264 | default: |
| 6265 | break; |
| 6266 | } |
| 6267 | |
| 6268 | spv::Id result = 0; |
| 6269 | if (convOp == spv::OpNop) |
| 6270 | return result; |
| 6271 | |
| 6272 | if (convOp == spv::OpSelect) { |
| 6273 | zero = makeSmearedConstant(zero, vectorSize); |
| 6274 | one = makeSmearedConstant(one, vectorSize); |
| 6275 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 6276 | } else |
| 6277 | result = builder.createUnaryOp(convOp, destType, operand); |
| 6278 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6279 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 6280 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6281 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 6285 | { |
| 6286 | if (vectorSize == 0) |
| 6287 | return constant; |
| 6288 | |
| 6289 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 6290 | std::vector<spv::Id> components; |
| 6291 | for (int c = 0; c < vectorSize; ++c) |
| 6292 | components.push_back(constant); |
| 6293 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 6294 | } |
| 6295 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6296 | // For glslang ops that map to SPV atomic opCodes |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6297 | 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] | 6298 | { |
| 6299 | spv::Op opCode = spv::OpNop; |
| 6300 | |
| 6301 | switch (op) { |
| 6302 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6303 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6304 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6305 | opCode = spv::OpAtomicIAdd; |
| 6306 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6307 | case glslang::EOpAtomicCounterSubtract: |
| 6308 | opCode = spv::OpAtomicISub; |
| 6309 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6310 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6311 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6312 | case glslang::EOpAtomicCounterMin: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6313 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6314 | break; |
| 6315 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6316 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6317 | case glslang::EOpAtomicCounterMax: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6318 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6319 | break; |
| 6320 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6321 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6322 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6323 | opCode = spv::OpAtomicAnd; |
| 6324 | break; |
| 6325 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6326 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6327 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6328 | opCode = spv::OpAtomicOr; |
| 6329 | break; |
| 6330 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6331 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6332 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6333 | opCode = spv::OpAtomicXor; |
| 6334 | break; |
| 6335 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6336 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6337 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6338 | opCode = spv::OpAtomicExchange; |
| 6339 | break; |
| 6340 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6341 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6342 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6343 | opCode = spv::OpAtomicCompareExchange; |
| 6344 | break; |
| 6345 | case glslang::EOpAtomicCounterIncrement: |
| 6346 | opCode = spv::OpAtomicIIncrement; |
| 6347 | break; |
| 6348 | case glslang::EOpAtomicCounterDecrement: |
| 6349 | opCode = spv::OpAtomicIDecrement; |
| 6350 | break; |
| 6351 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6352 | case glslang::EOpImageAtomicLoad: |
| 6353 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6354 | opCode = spv::OpAtomicLoad; |
| 6355 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6356 | case glslang::EOpAtomicStore: |
| 6357 | case glslang::EOpImageAtomicStore: |
| 6358 | opCode = spv::OpAtomicStore; |
| 6359 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6360 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6361 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6362 | break; |
| 6363 | } |
| 6364 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6365 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 6366 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 6367 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6368 | // Sort out the operands |
| 6369 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6370 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 6371 | // - compare-exchange swaps the value and comparator |
| 6372 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6373 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6374 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 6375 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 6376 | spv::Id scopeId; |
| 6377 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6378 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 6379 | } else { |
| 6380 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 6381 | } |
| 6382 | // semantics default to relaxed |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6383 | spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.volatil ? spv::MemorySemanticsVolatileMask : spv::MemorySemanticsMaskNone); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6384 | spv::Id semanticsId2 = semanticsId; |
| 6385 | |
| 6386 | pointerId = operands[0]; |
| 6387 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 6388 | // no additional operands |
| 6389 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 6390 | compareId = operands[1]; |
| 6391 | valueId = operands[2]; |
| 6392 | if (operands.size() > 3) { |
| 6393 | scopeId = operands[3]; |
| 6394 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 6395 | semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 6396 | } |
| 6397 | } else if (opCode == spv::OpAtomicLoad) { |
| 6398 | if (operands.size() > 1) { |
| 6399 | scopeId = operands[1]; |
| 6400 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 6401 | } |
| 6402 | } else { |
| 6403 | // atomic store or RMW |
| 6404 | valueId = operands[1]; |
| 6405 | if (operands.size() > 2) { |
| 6406 | scopeId = operands[2]; |
| 6407 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 6408 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 6409 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6410 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6411 | // Check for capabilities |
| 6412 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6413 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 6414 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 6415 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 6416 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6417 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6418 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6419 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6420 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 6421 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6422 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6423 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6424 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 6425 | spvAtomicOperands.push_back(pointerId); |
| 6426 | spvAtomicOperands.push_back(scopeId); |
| 6427 | spvAtomicOperands.push_back(semanticsId); |
| 6428 | if (opCode == spv::OpAtomicCompareExchange) { |
| 6429 | spvAtomicOperands.push_back(semanticsId2); |
| 6430 | spvAtomicOperands.push_back(valueId); |
| 6431 | spvAtomicOperands.push_back(compareId); |
| 6432 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 6433 | spvAtomicOperands.push_back(valueId); |
| 6434 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6435 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6436 | if (opCode == spv::OpAtomicStore) { |
| 6437 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 6438 | return 0; |
| 6439 | } else { |
| 6440 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 6441 | |
| 6442 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 6443 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 6444 | if (op == glslang::EOpAtomicCounterDecrement) |
| 6445 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 6446 | |
| 6447 | return resultId; |
| 6448 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6449 | } |
| 6450 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6451 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6452 | 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] | 6453 | { |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 6454 | #ifdef AMD_EXTENSIONS |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6455 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6456 | bool isFloat = isTypeFloat(typeProxy); |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 6457 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6458 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6459 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6460 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6461 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 6462 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6463 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 6464 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6465 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 6466 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6467 | } else if (op == glslang::EOpAnyInvocation || |
| 6468 | op == glslang::EOpAllInvocations || |
| 6469 | op == glslang::EOpAllInvocationsEqual) { |
| 6470 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 6471 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6472 | } else { |
| 6473 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 6474 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6475 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6476 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6477 | op == glslang::EOpAddInvocationsNonUniform || |
| 6478 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6479 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6480 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 6481 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 6482 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 6483 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6484 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 6485 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6486 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6487 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6488 | switch (op) { |
| 6489 | case glslang::EOpMinInvocations: |
| 6490 | case glslang::EOpMaxInvocations: |
| 6491 | case glslang::EOpAddInvocations: |
| 6492 | case glslang::EOpMinInvocationsNonUniform: |
| 6493 | case glslang::EOpMaxInvocationsNonUniform: |
| 6494 | case glslang::EOpAddInvocationsNonUniform: |
| 6495 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6496 | break; |
| 6497 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6498 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6499 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6500 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6501 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6502 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6503 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6504 | break; |
| 6505 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6506 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6507 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6508 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6509 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6510 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6511 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6512 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 6513 | default: |
| 6514 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6515 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6516 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6517 | spvGroupOperands.push_back(scope); |
| 6518 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6519 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6520 | spvGroupOperands.push_back(groupOp); |
| 6521 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6522 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6523 | } |
| 6524 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6525 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6526 | spv::IdImmediate op = { true, *opIt }; |
| 6527 | spvGroupOperands.push_back(op); |
| 6528 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6529 | |
| 6530 | switch (op) { |
| 6531 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6532 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6533 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6534 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6535 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6536 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6537 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6538 | opCode = spv::OpSubgroupAllEqualKHR; |
| 6539 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6540 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6541 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6542 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6543 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6544 | break; |
| 6545 | case glslang::EOpReadFirstInvocation: |
| 6546 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6547 | break; |
| 6548 | case glslang::EOpBallot: |
| 6549 | { |
| 6550 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6551 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6552 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6553 | // |
| 6554 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6555 | // |
| 6556 | spv::Id uintType = builder.makeUintType(32); |
| 6557 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6558 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6559 | |
| 6560 | std::vector<spv::Id> components; |
| 6561 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6562 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6563 | |
| 6564 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6565 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6566 | builder.createCompositeConstruct(uvec2Type, components)); |
| 6567 | } |
| 6568 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6569 | #ifdef AMD_EXTENSIONS |
| 6570 | case glslang::EOpMinInvocations: |
| 6571 | case glslang::EOpMaxInvocations: |
| 6572 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6573 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6574 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6575 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6576 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6577 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6578 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6579 | if (op == glslang::EOpMinInvocations || |
| 6580 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 6581 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6582 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6583 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6584 | else { |
| 6585 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6586 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6587 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6588 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6589 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6590 | } else if (op == glslang::EOpMaxInvocations || |
| 6591 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 6592 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
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::OpGroupFMax; |
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::OpGroupUMax; |
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::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6600 | } |
| 6601 | } else { |
| 6602 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6603 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6604 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6605 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6606 | } |
| 6607 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6608 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6609 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6610 | |
| 6611 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6612 | case glslang::EOpMinInvocationsNonUniform: |
| 6613 | case glslang::EOpMaxInvocationsNonUniform: |
| 6614 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6615 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6616 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6617 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6618 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6619 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6620 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6621 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6622 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6623 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6624 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6625 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6626 | else { |
| 6627 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6628 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6629 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6630 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6631 | } |
| 6632 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6633 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 6634 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6635 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6636 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6637 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6638 | else { |
| 6639 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6640 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6641 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6642 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6643 | } |
| 6644 | } |
| 6645 | else { |
| 6646 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6647 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6648 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6649 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6650 | } |
| 6651 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6652 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6653 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6654 | |
| 6655 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6656 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6657 | default: |
| 6658 | logger->missingFunctionality("invocation operation"); |
| 6659 | return spv::NoResult; |
| 6660 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6661 | |
| 6662 | assert(opCode != spv::OpNop); |
| 6663 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6664 | } |
| 6665 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6666 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6667 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 6668 | spv::Id typeId, std::vector<spv::Id>& operands) |
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 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6671 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6672 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6673 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6674 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6675 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 6676 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 6677 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6678 | #else |
| 6679 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6680 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6681 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 6682 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6683 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6684 | |
| 6685 | // Handle group invocation operations scalar by scalar. |
| 6686 | // The result type is the same type as the original type. |
| 6687 | // The algorithm is to: |
| 6688 | // - break the vector into scalars |
| 6689 | // - apply the operation to each scalar |
| 6690 | // - make a vector out the scalar results |
| 6691 | |
| 6692 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6693 | int numComponents = builder.getNumComponents(operands[0]); |
| 6694 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6695 | std::vector<spv::Id> results; |
| 6696 | |
| 6697 | // do each scalar op |
| 6698 | for (int comp = 0; comp < numComponents; ++comp) { |
| 6699 | std::vector<unsigned int> indexes; |
| 6700 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6701 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 6702 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6703 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 6704 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6705 | spv::IdImmediate operand = { true, operands[1] }; |
| 6706 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6707 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6708 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6709 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6710 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6711 | spv::IdImmediate operand = { true, operands[1] }; |
| 6712 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6713 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6714 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6715 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6716 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6717 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6718 | spvGroupOperands.push_back(scalar); |
| 6719 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6720 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6721 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6722 | } |
| 6723 | |
| 6724 | // put the pieces together |
| 6725 | return builder.createCompositeConstruct(typeId, results); |
| 6726 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6727 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6728 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6729 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 6730 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6731 | { |
| 6732 | // Add the required capabilities. |
| 6733 | switch (op) { |
| 6734 | case glslang::EOpSubgroupElect: |
| 6735 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6736 | break; |
| 6737 | case glslang::EOpSubgroupAll: |
| 6738 | case glslang::EOpSubgroupAny: |
| 6739 | case glslang::EOpSubgroupAllEqual: |
| 6740 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6741 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 6742 | break; |
| 6743 | case glslang::EOpSubgroupBroadcast: |
| 6744 | case glslang::EOpSubgroupBroadcastFirst: |
| 6745 | case glslang::EOpSubgroupBallot: |
| 6746 | case glslang::EOpSubgroupInverseBallot: |
| 6747 | case glslang::EOpSubgroupBallotBitExtract: |
| 6748 | case glslang::EOpSubgroupBallotBitCount: |
| 6749 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6750 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6751 | case glslang::EOpSubgroupBallotFindLSB: |
| 6752 | case glslang::EOpSubgroupBallotFindMSB: |
| 6753 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6754 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 6755 | break; |
| 6756 | case glslang::EOpSubgroupShuffle: |
| 6757 | case glslang::EOpSubgroupShuffleXor: |
| 6758 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6759 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 6760 | break; |
| 6761 | case glslang::EOpSubgroupShuffleUp: |
| 6762 | case glslang::EOpSubgroupShuffleDown: |
| 6763 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6764 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 6765 | break; |
| 6766 | case glslang::EOpSubgroupAdd: |
| 6767 | case glslang::EOpSubgroupMul: |
| 6768 | case glslang::EOpSubgroupMin: |
| 6769 | case glslang::EOpSubgroupMax: |
| 6770 | case glslang::EOpSubgroupAnd: |
| 6771 | case glslang::EOpSubgroupOr: |
| 6772 | case glslang::EOpSubgroupXor: |
| 6773 | case glslang::EOpSubgroupInclusiveAdd: |
| 6774 | case glslang::EOpSubgroupInclusiveMul: |
| 6775 | case glslang::EOpSubgroupInclusiveMin: |
| 6776 | case glslang::EOpSubgroupInclusiveMax: |
| 6777 | case glslang::EOpSubgroupInclusiveAnd: |
| 6778 | case glslang::EOpSubgroupInclusiveOr: |
| 6779 | case glslang::EOpSubgroupInclusiveXor: |
| 6780 | case glslang::EOpSubgroupExclusiveAdd: |
| 6781 | case glslang::EOpSubgroupExclusiveMul: |
| 6782 | case glslang::EOpSubgroupExclusiveMin: |
| 6783 | case glslang::EOpSubgroupExclusiveMax: |
| 6784 | case glslang::EOpSubgroupExclusiveAnd: |
| 6785 | case glslang::EOpSubgroupExclusiveOr: |
| 6786 | case glslang::EOpSubgroupExclusiveXor: |
| 6787 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6788 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 6789 | break; |
| 6790 | case glslang::EOpSubgroupClusteredAdd: |
| 6791 | case glslang::EOpSubgroupClusteredMul: |
| 6792 | case glslang::EOpSubgroupClusteredMin: |
| 6793 | case glslang::EOpSubgroupClusteredMax: |
| 6794 | case glslang::EOpSubgroupClusteredAnd: |
| 6795 | case glslang::EOpSubgroupClusteredOr: |
| 6796 | case glslang::EOpSubgroupClusteredXor: |
| 6797 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6798 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 6799 | break; |
| 6800 | case glslang::EOpSubgroupQuadBroadcast: |
| 6801 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6802 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6803 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 6804 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6805 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 6806 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6807 | #ifdef NV_EXTENSIONS |
| 6808 | case glslang::EOpSubgroupPartitionedAdd: |
| 6809 | case glslang::EOpSubgroupPartitionedMul: |
| 6810 | case glslang::EOpSubgroupPartitionedMin: |
| 6811 | case glslang::EOpSubgroupPartitionedMax: |
| 6812 | case glslang::EOpSubgroupPartitionedAnd: |
| 6813 | case glslang::EOpSubgroupPartitionedOr: |
| 6814 | case glslang::EOpSubgroupPartitionedXor: |
| 6815 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6816 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6817 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6818 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6819 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6820 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6821 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6822 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6823 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6824 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6825 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6826 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6827 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6828 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6829 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 6830 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 6831 | break; |
| 6832 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6833 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6834 | } |
| 6835 | |
| 6836 | const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
| 6837 | const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
| 6838 | const bool isBool = typeProxy == glslang::EbtBool; |
| 6839 | |
| 6840 | spv::Op opCode = spv::OpNop; |
| 6841 | |
| 6842 | // Figure out which opcode to use. |
| 6843 | switch (op) { |
| 6844 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 6845 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 6846 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 6847 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 6848 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 6849 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 6850 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 6851 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 6852 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 6853 | case glslang::EOpSubgroupBallotBitCount: |
| 6854 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6855 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 6856 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 6857 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 6858 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 6859 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 6860 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 6861 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 6862 | case glslang::EOpSubgroupAdd: |
| 6863 | case glslang::EOpSubgroupInclusiveAdd: |
| 6864 | case glslang::EOpSubgroupExclusiveAdd: |
| 6865 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6866 | #ifdef NV_EXTENSIONS |
| 6867 | case glslang::EOpSubgroupPartitionedAdd: |
| 6868 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6869 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6870 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6871 | if (isFloat) { |
| 6872 | opCode = spv::OpGroupNonUniformFAdd; |
| 6873 | } else { |
| 6874 | opCode = spv::OpGroupNonUniformIAdd; |
| 6875 | } |
| 6876 | break; |
| 6877 | case glslang::EOpSubgroupMul: |
| 6878 | case glslang::EOpSubgroupInclusiveMul: |
| 6879 | case glslang::EOpSubgroupExclusiveMul: |
| 6880 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6881 | #ifdef NV_EXTENSIONS |
| 6882 | case glslang::EOpSubgroupPartitionedMul: |
| 6883 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6884 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6885 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6886 | if (isFloat) { |
| 6887 | opCode = spv::OpGroupNonUniformFMul; |
| 6888 | } else { |
| 6889 | opCode = spv::OpGroupNonUniformIMul; |
| 6890 | } |
| 6891 | break; |
| 6892 | case glslang::EOpSubgroupMin: |
| 6893 | case glslang::EOpSubgroupInclusiveMin: |
| 6894 | case glslang::EOpSubgroupExclusiveMin: |
| 6895 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6896 | #ifdef NV_EXTENSIONS |
| 6897 | case glslang::EOpSubgroupPartitionedMin: |
| 6898 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6899 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6900 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6901 | if (isFloat) { |
| 6902 | opCode = spv::OpGroupNonUniformFMin; |
| 6903 | } else if (isUnsigned) { |
| 6904 | opCode = spv::OpGroupNonUniformUMin; |
| 6905 | } else { |
| 6906 | opCode = spv::OpGroupNonUniformSMin; |
| 6907 | } |
| 6908 | break; |
| 6909 | case glslang::EOpSubgroupMax: |
| 6910 | case glslang::EOpSubgroupInclusiveMax: |
| 6911 | case glslang::EOpSubgroupExclusiveMax: |
| 6912 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6913 | #ifdef NV_EXTENSIONS |
| 6914 | case glslang::EOpSubgroupPartitionedMax: |
| 6915 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6916 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6917 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6918 | if (isFloat) { |
| 6919 | opCode = spv::OpGroupNonUniformFMax; |
| 6920 | } else if (isUnsigned) { |
| 6921 | opCode = spv::OpGroupNonUniformUMax; |
| 6922 | } else { |
| 6923 | opCode = spv::OpGroupNonUniformSMax; |
| 6924 | } |
| 6925 | break; |
| 6926 | case glslang::EOpSubgroupAnd: |
| 6927 | case glslang::EOpSubgroupInclusiveAnd: |
| 6928 | case glslang::EOpSubgroupExclusiveAnd: |
| 6929 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6930 | #ifdef NV_EXTENSIONS |
| 6931 | case glslang::EOpSubgroupPartitionedAnd: |
| 6932 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6933 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6934 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6935 | if (isBool) { |
| 6936 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 6937 | } else { |
| 6938 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 6939 | } |
| 6940 | break; |
| 6941 | case glslang::EOpSubgroupOr: |
| 6942 | case glslang::EOpSubgroupInclusiveOr: |
| 6943 | case glslang::EOpSubgroupExclusiveOr: |
| 6944 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6945 | #ifdef NV_EXTENSIONS |
| 6946 | case glslang::EOpSubgroupPartitionedOr: |
| 6947 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6948 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6949 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6950 | if (isBool) { |
| 6951 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 6952 | } else { |
| 6953 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 6954 | } |
| 6955 | break; |
| 6956 | case glslang::EOpSubgroupXor: |
| 6957 | case glslang::EOpSubgroupInclusiveXor: |
| 6958 | case glslang::EOpSubgroupExclusiveXor: |
| 6959 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6960 | #ifdef NV_EXTENSIONS |
| 6961 | case glslang::EOpSubgroupPartitionedXor: |
| 6962 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6963 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6964 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6965 | if (isBool) { |
| 6966 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 6967 | } else { |
| 6968 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 6969 | } |
| 6970 | break; |
| 6971 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 6972 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6973 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6974 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 6975 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6976 | } |
| 6977 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6978 | // get the right Group Operation |
| 6979 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6980 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6981 | default: |
| 6982 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6983 | case glslang::EOpSubgroupBallotBitCount: |
| 6984 | case glslang::EOpSubgroupAdd: |
| 6985 | case glslang::EOpSubgroupMul: |
| 6986 | case glslang::EOpSubgroupMin: |
| 6987 | case glslang::EOpSubgroupMax: |
| 6988 | case glslang::EOpSubgroupAnd: |
| 6989 | case glslang::EOpSubgroupOr: |
| 6990 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6991 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6992 | break; |
| 6993 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6994 | case glslang::EOpSubgroupInclusiveAdd: |
| 6995 | case glslang::EOpSubgroupInclusiveMul: |
| 6996 | case glslang::EOpSubgroupInclusiveMin: |
| 6997 | case glslang::EOpSubgroupInclusiveMax: |
| 6998 | case glslang::EOpSubgroupInclusiveAnd: |
| 6999 | case glslang::EOpSubgroupInclusiveOr: |
| 7000 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7001 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7002 | break; |
| 7003 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7004 | case glslang::EOpSubgroupExclusiveAdd: |
| 7005 | case glslang::EOpSubgroupExclusiveMul: |
| 7006 | case glslang::EOpSubgroupExclusiveMin: |
| 7007 | case glslang::EOpSubgroupExclusiveMax: |
| 7008 | case glslang::EOpSubgroupExclusiveAnd: |
| 7009 | case glslang::EOpSubgroupExclusiveOr: |
| 7010 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7011 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7012 | break; |
| 7013 | case glslang::EOpSubgroupClusteredAdd: |
| 7014 | case glslang::EOpSubgroupClusteredMul: |
| 7015 | case glslang::EOpSubgroupClusteredMin: |
| 7016 | case glslang::EOpSubgroupClusteredMax: |
| 7017 | case glslang::EOpSubgroupClusteredAnd: |
| 7018 | case glslang::EOpSubgroupClusteredOr: |
| 7019 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7020 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7021 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7022 | #ifdef NV_EXTENSIONS |
| 7023 | case glslang::EOpSubgroupPartitionedAdd: |
| 7024 | case glslang::EOpSubgroupPartitionedMul: |
| 7025 | case glslang::EOpSubgroupPartitionedMin: |
| 7026 | case glslang::EOpSubgroupPartitionedMax: |
| 7027 | case glslang::EOpSubgroupPartitionedAnd: |
| 7028 | case glslang::EOpSubgroupPartitionedOr: |
| 7029 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7030 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7031 | break; |
| 7032 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7033 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7034 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7035 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7036 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7037 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7038 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7039 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7040 | break; |
| 7041 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7042 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7043 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7044 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7045 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7046 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7047 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7048 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7049 | break; |
| 7050 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7051 | } |
| 7052 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7053 | // build the instruction |
| 7054 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 7055 | |
| 7056 | // Every operation begins with the Execution Scope operand. |
| 7057 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7058 | spvGroupOperands.push_back(executionScope); |
| 7059 | |
| 7060 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 7061 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7062 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7063 | spvGroupOperands.push_back(groupOperand); |
| 7064 | } |
| 7065 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7066 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7067 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 7068 | spv::IdImmediate operand = { true, *opIt }; |
| 7069 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7070 | } |
| 7071 | |
| 7072 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7073 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7074 | switch (op) { |
| 7075 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7076 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 7077 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 7078 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 7079 | } |
| 7080 | if (directionId != spv::NoResult) { |
| 7081 | spv::IdImmediate direction = { true, directionId }; |
| 7082 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7083 | } |
| 7084 | |
| 7085 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 7086 | } |
| 7087 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7088 | 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] | 7089 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7090 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7091 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7092 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7093 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7094 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7095 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 7096 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7097 | spv::Id typeId0 = 0; |
| 7098 | if (consumedOperands > 0) |
| 7099 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7100 | spv::Id typeId1 = 0; |
| 7101 | if (consumedOperands > 1) |
| 7102 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7103 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7104 | |
| 7105 | switch (op) { |
| 7106 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7107 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7108 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7109 | else if (isUnsigned) |
| 7110 | libCall = spv::GLSLstd450UMin; |
| 7111 | else |
| 7112 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7113 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7114 | break; |
| 7115 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7116 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7117 | break; |
| 7118 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7119 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7120 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7121 | else if (isUnsigned) |
| 7122 | libCall = spv::GLSLstd450UMax; |
| 7123 | else |
| 7124 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7125 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7126 | break; |
| 7127 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7128 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7129 | break; |
| 7130 | case glslang::EOpDot: |
| 7131 | opCode = spv::OpDot; |
| 7132 | break; |
| 7133 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7134 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7135 | break; |
| 7136 | |
| 7137 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7138 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7139 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7140 | else if (isUnsigned) |
| 7141 | libCall = spv::GLSLstd450UClamp; |
| 7142 | else |
| 7143 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7144 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 7145 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7146 | break; |
| 7147 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7148 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 7149 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7150 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7151 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7152 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7153 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7154 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7155 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7156 | break; |
| 7157 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7158 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7159 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7160 | break; |
| 7161 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7162 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7163 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 7164 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7165 | break; |
| 7166 | |
| 7167 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7168 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7169 | break; |
| 7170 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7171 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7172 | break; |
| 7173 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7174 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7175 | break; |
| 7176 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7177 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7178 | break; |
| 7179 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7180 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7181 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7182 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7183 | #ifdef AMD_EXTENSIONS |
| 7184 | if (typeProxy == glslang::EbtFloat16) |
| 7185 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 7186 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7187 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 7188 | break; |
| 7189 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7190 | #ifdef AMD_EXTENSIONS |
| 7191 | if (typeProxy == glslang::EbtFloat16) |
| 7192 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 7193 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7194 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 7195 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7196 | case glslang::EOpAddCarry: |
| 7197 | opCode = spv::OpIAddCarry; |
| 7198 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7199 | consumedOperands = 2; |
| 7200 | break; |
| 7201 | case glslang::EOpSubBorrow: |
| 7202 | opCode = spv::OpISubBorrow; |
| 7203 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7204 | consumedOperands = 2; |
| 7205 | break; |
| 7206 | case glslang::EOpUMulExtended: |
| 7207 | opCode = spv::OpUMulExtended; |
| 7208 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7209 | consumedOperands = 2; |
| 7210 | break; |
| 7211 | case glslang::EOpIMulExtended: |
| 7212 | opCode = spv::OpSMulExtended; |
| 7213 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7214 | consumedOperands = 2; |
| 7215 | break; |
| 7216 | case glslang::EOpBitfieldExtract: |
| 7217 | if (isUnsigned) |
| 7218 | opCode = spv::OpBitFieldUExtract; |
| 7219 | else |
| 7220 | opCode = spv::OpBitFieldSExtract; |
| 7221 | break; |
| 7222 | case glslang::EOpBitfieldInsert: |
| 7223 | opCode = spv::OpBitFieldInsert; |
| 7224 | break; |
| 7225 | |
| 7226 | case glslang::EOpFma: |
| 7227 | libCall = spv::GLSLstd450Fma; |
| 7228 | break; |
| 7229 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7230 | { |
| 7231 | libCall = spv::GLSLstd450FrexpStruct; |
| 7232 | assert(builder.isPointerType(typeId1)); |
| 7233 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7234 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 7235 | #ifdef AMD_EXTENSIONS |
| 7236 | if (width == 16) |
| 7237 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 7238 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 7239 | #endif |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7240 | if (builder.getNumComponents(operands[0]) == 1) |
| 7241 | frexpIntType = builder.makeIntegerType(width, true); |
| 7242 | else |
| 7243 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 7244 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 7245 | consumedOperands = 1; |
| 7246 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7247 | break; |
| 7248 | case glslang::EOpLdexp: |
| 7249 | libCall = spv::GLSLstd450Ldexp; |
| 7250 | break; |
| 7251 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7252 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7253 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7254 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7255 | case glslang::EOpSubgroupBroadcast: |
| 7256 | case glslang::EOpSubgroupBallotBitExtract: |
| 7257 | case glslang::EOpSubgroupShuffle: |
| 7258 | case glslang::EOpSubgroupShuffleXor: |
| 7259 | case glslang::EOpSubgroupShuffleUp: |
| 7260 | case glslang::EOpSubgroupShuffleDown: |
| 7261 | case glslang::EOpSubgroupClusteredAdd: |
| 7262 | case glslang::EOpSubgroupClusteredMul: |
| 7263 | case glslang::EOpSubgroupClusteredMin: |
| 7264 | case glslang::EOpSubgroupClusteredMax: |
| 7265 | case glslang::EOpSubgroupClusteredAnd: |
| 7266 | case glslang::EOpSubgroupClusteredOr: |
| 7267 | case glslang::EOpSubgroupClusteredXor: |
| 7268 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7269 | #ifdef NV_EXTENSIONS |
| 7270 | case glslang::EOpSubgroupPartitionedAdd: |
| 7271 | case glslang::EOpSubgroupPartitionedMul: |
| 7272 | case glslang::EOpSubgroupPartitionedMin: |
| 7273 | case glslang::EOpSubgroupPartitionedMax: |
| 7274 | case glslang::EOpSubgroupPartitionedAnd: |
| 7275 | case glslang::EOpSubgroupPartitionedOr: |
| 7276 | case glslang::EOpSubgroupPartitionedXor: |
| 7277 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7278 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7279 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7280 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7281 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7282 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7283 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7284 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7285 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7286 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7287 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7288 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7289 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7290 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 7291 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7292 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 7293 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7294 | #ifdef AMD_EXTENSIONS |
| 7295 | case glslang::EOpSwizzleInvocations: |
| 7296 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7297 | libCall = spv::SwizzleInvocationsAMD; |
| 7298 | break; |
| 7299 | case glslang::EOpSwizzleInvocationsMasked: |
| 7300 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7301 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 7302 | break; |
| 7303 | case glslang::EOpWriteInvocation: |
| 7304 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7305 | libCall = spv::WriteInvocationAMD; |
| 7306 | break; |
| 7307 | |
| 7308 | case glslang::EOpMin3: |
| 7309 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7310 | if (isFloat) |
| 7311 | libCall = spv::FMin3AMD; |
| 7312 | else { |
| 7313 | if (isUnsigned) |
| 7314 | libCall = spv::UMin3AMD; |
| 7315 | else |
| 7316 | libCall = spv::SMin3AMD; |
| 7317 | } |
| 7318 | break; |
| 7319 | case glslang::EOpMax3: |
| 7320 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7321 | if (isFloat) |
| 7322 | libCall = spv::FMax3AMD; |
| 7323 | else { |
| 7324 | if (isUnsigned) |
| 7325 | libCall = spv::UMax3AMD; |
| 7326 | else |
| 7327 | libCall = spv::SMax3AMD; |
| 7328 | } |
| 7329 | break; |
| 7330 | case glslang::EOpMid3: |
| 7331 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7332 | if (isFloat) |
| 7333 | libCall = spv::FMid3AMD; |
| 7334 | else { |
| 7335 | if (isUnsigned) |
| 7336 | libCall = spv::UMid3AMD; |
| 7337 | else |
| 7338 | libCall = spv::SMid3AMD; |
| 7339 | } |
| 7340 | break; |
| 7341 | |
| 7342 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7343 | if (typeProxy == glslang::EbtFloat16) |
| 7344 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7345 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 7346 | libCall = spv::InterpolateAtVertexAMD; |
| 7347 | break; |
| 7348 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7349 | case glslang::EOpBarrier: |
| 7350 | { |
| 7351 | // This is for the extended controlBarrier function, with four operands. |
| 7352 | // The unextended barrier() goes through createNoArgOperation. |
| 7353 | assert(operands.size() == 4); |
| 7354 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 7355 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 7356 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 7357 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 7358 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7359 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7360 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7361 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7362 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7363 | } |
| 7364 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) { |
| 7365 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7366 | } |
| 7367 | return 0; |
| 7368 | } |
| 7369 | break; |
| 7370 | case glslang::EOpMemoryBarrier: |
| 7371 | { |
| 7372 | // This is for the extended memoryBarrier function, with three operands. |
| 7373 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 7374 | assert(operands.size() == 3); |
| 7375 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 7376 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 7377 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 7378 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7379 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7380 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7381 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7382 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7383 | } |
| 7384 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 7385 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7386 | } |
| 7387 | return 0; |
| 7388 | } |
| 7389 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7390 | |
| 7391 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7392 | case glslang::EOpReportIntersectionNV: |
| 7393 | { |
| 7394 | typeId = builder.makeBoolType(); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7395 | opCode = spv::OpReportIntersectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7396 | } |
| 7397 | break; |
| 7398 | case glslang::EOpTraceNV: |
| 7399 | { |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7400 | builder.createNoResultOp(spv::OpTraceNV, operands); |
| 7401 | return 0; |
| 7402 | } |
| 7403 | break; |
| 7404 | case glslang::EOpExecuteCallableNV: |
| 7405 | { |
| 7406 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7407 | return 0; |
| 7408 | } |
| 7409 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7410 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 7411 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 7412 | return 0; |
| 7413 | #endif |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 7414 | case glslang::EOpCooperativeMatrixMulAdd: |
| 7415 | opCode = spv::OpCooperativeMatrixMulAddNV; |
| 7416 | break; |
| 7417 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7418 | default: |
| 7419 | return 0; |
| 7420 | } |
| 7421 | |
| 7422 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7423 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 7424 | // Use an extended instruction from the standard library. |
| 7425 | // Construct the call arguments, without modifying the original operands vector. |
| 7426 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 7427 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7428 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 7429 | } else if (opCode == spv::OpDot && !isFloat) { |
| 7430 | // int dot(int, int) |
| 7431 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 7432 | const int componentCount = builder.getNumComponents(operands[0]); |
| 7433 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 7434 | builder.setPrecision(mulOp, precision); |
| 7435 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 7436 | for (int i = 1; i < componentCount; ++i) { |
| 7437 | builder.setPrecision(id, precision); |
| 7438 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(operands[0], typeId, i)); |
| 7439 | } |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7440 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7441 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7442 | case 0: |
| 7443 | // should all be handled by visitAggregate and createNoArgOperation |
| 7444 | assert(0); |
| 7445 | return 0; |
| 7446 | case 1: |
| 7447 | // should all be handled by createUnaryOperation |
| 7448 | assert(0); |
| 7449 | return 0; |
| 7450 | case 2: |
| 7451 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 7452 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7453 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7454 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 7455 | assert(consumedOperands == operands.size()); |
| 7456 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7457 | break; |
| 7458 | } |
| 7459 | } |
| 7460 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7461 | // Decode the return types that were structures |
| 7462 | switch (op) { |
| 7463 | case glslang::EOpAddCarry: |
| 7464 | case glslang::EOpSubBorrow: |
| 7465 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7466 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7467 | break; |
| 7468 | case glslang::EOpUMulExtended: |
| 7469 | case glslang::EOpIMulExtended: |
| 7470 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 7471 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7472 | break; |
| 7473 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7474 | { |
| 7475 | assert(operands.size() == 2); |
| 7476 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 7477 | // "exp" is floating-point type (from HLSL intrinsic) |
| 7478 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 7479 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 7480 | builder.createStore(member1, operands[1]); |
| 7481 | } else |
| 7482 | // "exp" is integer type (from GLSL built-in function) |
| 7483 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 7484 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7485 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7486 | break; |
| 7487 | default: |
| 7488 | break; |
| 7489 | } |
| 7490 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 7491 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7492 | } |
| 7493 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7494 | // Intrinsics with no arguments (or no return value, and no precision). |
| 7495 | 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] | 7496 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7497 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 7498 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7499 | |
| 7500 | switch (op) { |
| 7501 | case glslang::EOpEmitVertex: |
| 7502 | builder.createNoResultOp(spv::OpEmitVertex); |
| 7503 | return 0; |
| 7504 | case glslang::EOpEndPrimitive: |
| 7505 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 7506 | return 0; |
| 7507 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7508 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7509 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7510 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7511 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7512 | spv::MemorySemanticsAcquireReleaseMask); |
| 7513 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7514 | } else { |
| 7515 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 7516 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7517 | } else { |
| 7518 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7519 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7520 | spv::MemorySemanticsAcquireReleaseMask); |
| 7521 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7522 | return 0; |
| 7523 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7524 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 7525 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7526 | return 0; |
| 7527 | case glslang::EOpMemoryBarrierAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7528 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 7529 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7530 | return 0; |
| 7531 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7532 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 7533 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7534 | return 0; |
| 7535 | case glslang::EOpMemoryBarrierImage: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7536 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 7537 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7538 | return 0; |
| 7539 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7540 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7541 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7542 | return 0; |
| 7543 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7544 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 7545 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7546 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7547 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7548 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7549 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7550 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7551 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7552 | case glslang::EOpDeviceMemoryBarrier: |
| 7553 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7554 | spv::MemorySemanticsImageMemoryMask | |
| 7555 | spv::MemorySemanticsAcquireReleaseMask); |
| 7556 | return 0; |
| 7557 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 7558 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7559 | spv::MemorySemanticsImageMemoryMask | |
| 7560 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7561 | return 0; |
| 7562 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7563 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7564 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7565 | return 0; |
| 7566 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7567 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7568 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7569 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7570 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7571 | case glslang::EOpSubgroupBarrier: |
| 7572 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7573 | spv::MemorySemanticsAcquireReleaseMask); |
| 7574 | return spv::NoResult; |
| 7575 | case glslang::EOpSubgroupMemoryBarrier: |
| 7576 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7577 | spv::MemorySemanticsAcquireReleaseMask); |
| 7578 | return spv::NoResult; |
| 7579 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 7580 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 7581 | spv::MemorySemanticsAcquireReleaseMask); |
| 7582 | return spv::NoResult; |
| 7583 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 7584 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 7585 | spv::MemorySemanticsAcquireReleaseMask); |
| 7586 | return spv::NoResult; |
| 7587 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 7588 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7589 | spv::MemorySemanticsAcquireReleaseMask); |
| 7590 | return spv::NoResult; |
| 7591 | case glslang::EOpSubgroupElect: { |
| 7592 | std::vector<spv::Id> operands; |
| 7593 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 7594 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7595 | #ifdef AMD_EXTENSIONS |
| 7596 | case glslang::EOpTime: |
| 7597 | { |
| 7598 | std::vector<spv::Id> args; // Dummy arguments |
| 7599 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 7600 | return builder.setPrecision(id, precision); |
| 7601 | } |
| 7602 | #endif |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7603 | #ifdef NV_EXTENSIONS |
| 7604 | case glslang::EOpIgnoreIntersectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7605 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7606 | return 0; |
| 7607 | case glslang::EOpTerminateRayNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7608 | builder.createNoResultOp(spv::OpTerminateRayNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7609 | return 0; |
| 7610 | #endif |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 7611 | |
| 7612 | case glslang::EOpBeginInvocationInterlock: |
| 7613 | builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
| 7614 | return 0; |
| 7615 | case glslang::EOpEndInvocationInterlock: |
| 7616 | builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
| 7617 | return 0; |
| 7618 | |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 7619 | case glslang::EOpIsHelperInvocation: |
| 7620 | { |
| 7621 | std::vector<spv::Id> args; // Dummy arguments |
| 7622 | spv::Id id = builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args); |
| 7623 | return id; |
| 7624 | } |
| 7625 | |
amhagan | 91fb009 | 2019-07-10 21:14:38 -0400 | [diff] [blame] | 7626 | case glslang::EOpReadClockSubgroupKHR: { |
| 7627 | std::vector<spv::Id> args; |
| 7628 | args.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 7629 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 7630 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 7631 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 7632 | } |
| 7633 | |
| 7634 | case glslang::EOpReadClockDeviceKHR: { |
| 7635 | std::vector<spv::Id> args; |
| 7636 | args.push_back(builder.makeUintConstant(spv::ScopeDevice)); |
| 7637 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 7638 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 7639 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 7640 | } |
| 7641 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7642 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7643 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7644 | return 0; |
| 7645 | } |
| 7646 | } |
| 7647 | |
| 7648 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 7649 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 7650 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7651 | spv::Id id; |
| 7652 | if (symbolValues.end() != iter) { |
| 7653 | id = iter->second; |
| 7654 | return id; |
| 7655 | } |
| 7656 | |
| 7657 | // it was not found, create it |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7658 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
| 7659 | auto forcedType = getForcedType(builtIn, symbol->getType()); |
| 7660 | id = createSpvVariable(symbol, forcedType.first); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7661 | symbolValues[symbol->getId()] = id; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7662 | if (forcedType.second != spv::NoType) |
| 7663 | forceType[id] = forcedType.second; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7664 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 7665 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7666 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 7667 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 7668 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7669 | #ifdef NV_EXTENSIONS |
| 7670 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
| 7671 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7672 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7673 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7674 | if (symbol->getQualifier().hasIndex()) |
| 7675 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 7676 | if (symbol->getQualifier().hasComponent()) |
| 7677 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 7678 | // atomic counters use this: |
| 7679 | if (symbol->getQualifier().hasOffset()) |
| 7680 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7681 | } |
| 7682 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 7683 | if (symbol->getQualifier().hasLocation()) |
| 7684 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7685 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 7686 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7687 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7688 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7689 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7690 | if (symbol->getQualifier().hasSet()) |
| 7691 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7692 | else if (IsDescriptorResource(symbol->getType())) { |
| 7693 | // default to 0 |
| 7694 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 7695 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7696 | if (symbol->getQualifier().hasBinding()) |
| 7697 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
Jeff Bolz | 0a93cfb | 2018-12-11 20:53:59 -0600 | [diff] [blame] | 7698 | else if (IsDescriptorResource(symbol->getType())) { |
| 7699 | // default to 0 |
| 7700 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 7701 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7702 | if (symbol->getQualifier().hasAttachment()) |
| 7703 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7704 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7705 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7706 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7707 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7708 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 7709 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 7710 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 7711 | } |
| 7712 | if (symbol->getQualifier().hasXfbOffset()) |
| 7713 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7714 | } |
| 7715 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7716 | if (symbol->getType().isImage()) { |
| 7717 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7718 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7719 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7720 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7721 | } |
| 7722 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7723 | // add built-in variable decoration |
| 7724 | if (builtIn != spv::BuiltInMax) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7725 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7726 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7727 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 7728 | // nonuniform |
| 7729 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 7730 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 7731 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7732 | if (builtIn == spv::BuiltInSampleMask) { |
| 7733 | spv::Decoration decoration; |
| 7734 | // GL_NV_sample_mask_override_coverage extension |
| 7735 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7736 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7737 | else |
| 7738 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7739 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7740 | if (decoration != spv::DecorationMax) { |
Jason Macnak | dbd4c3c | 2019-07-12 14:33:02 -0700 | [diff] [blame] | 7741 | builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7742 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 7743 | } |
| 7744 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7745 | else if (builtIn == spv::BuiltInLayer) { |
| 7746 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7747 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7748 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7749 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 7750 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 7751 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7752 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7753 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 7754 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7755 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 7756 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 7757 | } |
| 7758 | } |
| 7759 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7760 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7761 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7762 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7763 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 7764 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 7765 | if (symbol->getQualifier().pervertexNV) { |
| 7766 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 7767 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 7768 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 7769 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7770 | #endif |
| 7771 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7772 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 7773 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 7774 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 7775 | symbol->getType().getQualifier().semanticName); |
| 7776 | } |
| 7777 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7778 | if (symbol->getBasicType() == glslang::EbtReference) { |
| 7779 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 7780 | } |
| 7781 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7782 | return id; |
| 7783 | } |
| 7784 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7785 | #ifdef NV_EXTENSIONS |
| 7786 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 7787 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 7788 | { |
| 7789 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7790 | if (qualifier.perPrimitiveNV) { |
| 7791 | // Need to add capability/extension for fragment shader. |
| 7792 | // Mesh shader already adds this by default. |
| 7793 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7794 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7795 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7796 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7797 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7798 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7799 | if (qualifier.perViewNV) |
| 7800 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 7801 | if (qualifier.perTaskNV) |
| 7802 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 7803 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7804 | if (qualifier.perPrimitiveNV) { |
| 7805 | // Need to add capability/extension for fragment shader. |
| 7806 | // Mesh shader already adds this by default. |
| 7807 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7808 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7809 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7810 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7811 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7812 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7813 | if (qualifier.perViewNV) |
| 7814 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 7815 | if (qualifier.perTaskNV) |
| 7816 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 7817 | } |
| 7818 | } |
| 7819 | #endif |
| 7820 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7821 | // 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] | 7822 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7823 | // |
| 7824 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 7825 | // |
| 7826 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 7827 | // regular constants, which themselves are trees that createSpvConstant() |
| 7828 | // recursively walks. So, this function walks the "top" of the tree: |
| 7829 | // - emit specialization constant-building instructions for specConstant |
| 7830 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7831 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7832 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 7833 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7834 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7835 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7836 | if (! node.getQualifier().specConstant) { |
| 7837 | // hand off to the non-spec-constant path |
| 7838 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 7839 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7840 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7841 | nextConst, false); |
| 7842 | } |
| 7843 | |
| 7844 | // We now know we have a specialization constant to build |
| 7845 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 7846 | // 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] | 7847 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 7848 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 7849 | std::vector<spv::Id> dimConstId; |
| 7850 | for (int dim = 0; dim < 3; ++dim) { |
| 7851 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 7852 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7853 | if (specConst) { |
| 7854 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 7855 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 7856 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7857 | } |
| 7858 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 7859 | } |
| 7860 | |
| 7861 | // An AST node labelled as specialization constant should be a symbol node. |
| 7862 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 7863 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7864 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7865 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 7866 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 7867 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 7868 | // will set the builder into spec constant op instruction generating mode. |
| 7869 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7870 | result = accessChainLoad(sub_tree->getType()); |
| 7871 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7872 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7873 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7874 | } else { |
| 7875 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7876 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7877 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7878 | builder.addName(result, sn->getName().c_str()); |
| 7879 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7880 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7881 | |
| 7882 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 7883 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7884 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7885 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7886 | } |
| 7887 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7888 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 7889 | // build the aggregate SPIR-V constant. |
| 7890 | // |
| 7891 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 7892 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 7893 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7894 | 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] | 7895 | { |
| 7896 | // vector of constants for SPIR-V |
| 7897 | std::vector<spv::Id> spvConsts; |
| 7898 | |
| 7899 | // Type is used for struct and array constants |
| 7900 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 7901 | |
| 7902 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7903 | glslang::TType elementType(glslangType, 0); |
| 7904 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7905 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7906 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7907 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7908 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7909 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 7910 | } else if (glslangType.isCoopMat()) { |
| 7911 | glslang::TType componentType(glslangType.getBasicType()); |
| 7912 | spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7913 | } else if (glslangType.isStruct()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7914 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 7915 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7916 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 7917 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7918 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 7919 | bool zero = nextConst >= consts.size(); |
| 7920 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7921 | case glslang::EbtInt8: |
| 7922 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 7923 | break; |
| 7924 | case glslang::EbtUint8: |
| 7925 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 7926 | break; |
| 7927 | case glslang::EbtInt16: |
| 7928 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 7929 | break; |
| 7930 | case glslang::EbtUint16: |
| 7931 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 7932 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7933 | case glslang::EbtInt: |
| 7934 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 7935 | break; |
| 7936 | case glslang::EbtUint: |
| 7937 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 7938 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7939 | case glslang::EbtInt64: |
| 7940 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 7941 | break; |
| 7942 | case glslang::EbtUint64: |
| 7943 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 7944 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7945 | case glslang::EbtFloat: |
| 7946 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7947 | break; |
| 7948 | case glslang::EbtDouble: |
| 7949 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 7950 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7951 | case glslang::EbtFloat16: |
| 7952 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7953 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7954 | case glslang::EbtBool: |
| 7955 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 7956 | break; |
| 7957 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7958 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7959 | break; |
| 7960 | } |
| 7961 | ++nextConst; |
| 7962 | } |
| 7963 | } else { |
| 7964 | // we have a non-aggregate (scalar) constant |
| 7965 | bool zero = nextConst >= consts.size(); |
| 7966 | spv::Id scalar = 0; |
| 7967 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7968 | case glslang::EbtInt8: |
| 7969 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 7970 | break; |
| 7971 | case glslang::EbtUint8: |
| 7972 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 7973 | break; |
| 7974 | case glslang::EbtInt16: |
| 7975 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 7976 | break; |
| 7977 | case glslang::EbtUint16: |
| 7978 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 7979 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7980 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7981 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7982 | break; |
| 7983 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7984 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7985 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7986 | case glslang::EbtInt64: |
| 7987 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 7988 | break; |
| 7989 | case glslang::EbtUint64: |
| 7990 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7991 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7992 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7993 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7994 | break; |
| 7995 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7996 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7997 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7998 | case glslang::EbtFloat16: |
| 7999 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8000 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8001 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8002 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8003 | break; |
Jeff Bolz | 3fd1232 | 2019-03-05 23:27:09 -0600 | [diff] [blame] | 8004 | case glslang::EbtReference: |
| 8005 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8006 | scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar); |
| 8007 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8008 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8009 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8010 | break; |
| 8011 | } |
| 8012 | ++nextConst; |
| 8013 | return scalar; |
| 8014 | } |
| 8015 | |
| 8016 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 8017 | } |
| 8018 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8019 | // Return true if the node is a constant or symbol whose reading has no |
| 8020 | // non-trivial observable cost or effect. |
| 8021 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 8022 | { |
| 8023 | // don't know what this is |
| 8024 | if (node == nullptr) |
| 8025 | return false; |
| 8026 | |
| 8027 | // a constant is safe |
| 8028 | if (node->getAsConstantUnion() != nullptr) |
| 8029 | return true; |
| 8030 | |
| 8031 | // not a symbol means non-trivial |
| 8032 | if (node->getAsSymbolNode() == nullptr) |
| 8033 | return false; |
| 8034 | |
| 8035 | // a symbol, depends on what's being read |
| 8036 | switch (node->getType().getQualifier().storage) { |
| 8037 | case glslang::EvqTemporary: |
| 8038 | case glslang::EvqGlobal: |
| 8039 | case glslang::EvqIn: |
| 8040 | case glslang::EvqInOut: |
| 8041 | case glslang::EvqConst: |
| 8042 | case glslang::EvqConstReadOnly: |
| 8043 | case glslang::EvqUniform: |
| 8044 | return true; |
| 8045 | default: |
| 8046 | return false; |
| 8047 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 8048 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8049 | |
| 8050 | // 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] | 8051 | // 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] | 8052 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8053 | // Return true if trivial. |
| 8054 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 8055 | { |
| 8056 | if (node == nullptr) |
| 8057 | return false; |
| 8058 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 8059 | // count non scalars as trivial, as well as anything coming from HLSL |
| 8060 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 8061 | return true; |
| 8062 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8063 | // symbols and constants are trivial |
| 8064 | if (isTrivialLeaf(node)) |
| 8065 | return true; |
| 8066 | |
| 8067 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 8068 | |
| 8069 | // not a simple operation |
| 8070 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 8071 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 8072 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 8073 | return false; |
| 8074 | |
| 8075 | // not on leaf nodes |
| 8076 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 8077 | return false; |
| 8078 | |
| 8079 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 8080 | return false; |
| 8081 | } |
| 8082 | |
| 8083 | switch (node->getAsOperator()->getOp()) { |
| 8084 | case glslang::EOpLogicalNot: |
| 8085 | case glslang::EOpConvIntToBool: |
| 8086 | case glslang::EOpConvUintToBool: |
| 8087 | case glslang::EOpConvFloatToBool: |
| 8088 | case glslang::EOpConvDoubleToBool: |
| 8089 | case glslang::EOpEqual: |
| 8090 | case glslang::EOpNotEqual: |
| 8091 | case glslang::EOpLessThan: |
| 8092 | case glslang::EOpGreaterThan: |
| 8093 | case glslang::EOpLessThanEqual: |
| 8094 | case glslang::EOpGreaterThanEqual: |
| 8095 | case glslang::EOpIndexDirect: |
| 8096 | case glslang::EOpIndexDirectStruct: |
| 8097 | case glslang::EOpLogicalXor: |
| 8098 | case glslang::EOpAny: |
| 8099 | case glslang::EOpAll: |
| 8100 | return true; |
| 8101 | default: |
| 8102 | return false; |
| 8103 | } |
| 8104 | } |
| 8105 | |
| 8106 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 8107 | // the left side is true (for &&) or false (for ||). |
| 8108 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 8109 | { |
| 8110 | spv::Id boolTypeId = builder.makeBoolType(); |
| 8111 | |
| 8112 | // emit left operand |
| 8113 | builder.clearAccessChain(); |
| 8114 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8115 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8116 | |
| 8117 | // Operands to accumulate OpPhi operands |
| 8118 | std::vector<spv::Id> phiOperands; |
| 8119 | // accumulate left operand's phi information |
| 8120 | phiOperands.push_back(leftId); |
| 8121 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8122 | |
| 8123 | // Make the two kinds of operation symmetric with a "!" |
| 8124 | // || => emit "if (! left) result = right" |
| 8125 | // && => emit "if ( left) result = right" |
| 8126 | // |
| 8127 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 8128 | // to 'builder' to have an "else" without an "then" |
| 8129 | if (op == glslang::EOpLogicalOr) |
| 8130 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 8131 | |
| 8132 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 8133 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8134 | |
| 8135 | // emit right operand as the "then" part of the "if" |
| 8136 | builder.clearAccessChain(); |
| 8137 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8138 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8139 | |
| 8140 | // accumulate left operand's phi information |
| 8141 | phiOperands.push_back(rightId); |
| 8142 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8143 | |
| 8144 | // finish the "if" |
| 8145 | ifBuilder.makeEndIf(); |
| 8146 | |
| 8147 | // phi together the two results |
| 8148 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 8149 | } |
| 8150 | |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 8151 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8152 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 8153 | // Import this set if it has not been imported yet. |
| 8154 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 8155 | { |
| 8156 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 8157 | return extBuiltinMap[name]; |
| 8158 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 8159 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8160 | spv::Id extBuiltins = builder.import(name); |
| 8161 | extBuiltinMap[name] = extBuiltins; |
| 8162 | return extBuiltins; |
| 8163 | } |
| 8164 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 8165 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8166 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8167 | }; // end anonymous namespace |
| 8168 | |
| 8169 | namespace glslang { |
| 8170 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8171 | void GetSpirvVersion(std::string& version) |
| 8172 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 8173 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 8174 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8175 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8176 | version = buf; |
| 8177 | } |
| 8178 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8179 | // For low-order part of the generator's magic number. Bump up |
| 8180 | // when there is a change in the style (e.g., if SSA form changes, |
| 8181 | // or a different instruction sequence to do something gets used). |
| 8182 | int GetSpirvGeneratorVersion() |
| 8183 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 8184 | // return 1; // start |
| 8185 | // 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] | 8186 | // 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] | 8187 | // 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] | 8188 | // 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] | 8189 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 8190 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
| 8191 | 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] | 8192 | } |
| 8193 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8194 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8195 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8196 | { |
| 8197 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8198 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8199 | if (out.fail()) |
| 8200 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8201 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 8202 | unsigned int word = spirv[i]; |
| 8203 | out.write((const char*)&word, 4); |
| 8204 | } |
| 8205 | out.close(); |
| 8206 | } |
| 8207 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8208 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 8209 | 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] | 8210 | { |
| 8211 | std::ofstream out; |
| 8212 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8213 | if (out.fail()) |
| 8214 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 8215 | out << "\t// " << |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8216 | GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL << |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 8217 | std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8218 | if (varName != nullptr) { |
| 8219 | out << "\t #pragma once" << std::endl; |
| 8220 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 8221 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8222 | const int WORDS_PER_LINE = 8; |
| 8223 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 8224 | out << "\t"; |
| 8225 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 8226 | const unsigned int word = spirv[i + j]; |
| 8227 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 8228 | if (i + j + 1 < (int)spirv.size()) { |
| 8229 | out << ","; |
| 8230 | } |
| 8231 | } |
| 8232 | out << std::endl; |
| 8233 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8234 | if (varName != nullptr) { |
| 8235 | out << "};"; |
| 8236 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8237 | out.close(); |
| 8238 | } |
| 8239 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8240 | // |
| 8241 | // Set up the glslang traversal |
| 8242 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8243 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8244 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8245 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8246 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8247 | } |
| 8248 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8249 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8250 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8251 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8252 | TIntermNode* root = intermediate.getTreeRoot(); |
| 8253 | |
| 8254 | if (root == 0) |
| 8255 | return; |
| 8256 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8257 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8258 | if (options == nullptr) |
| 8259 | options = &defaultOptions; |
| 8260 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8261 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8262 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 8263 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8264 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 8265 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8266 | it.dumpSpv(spirv); |
| 8267 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 8268 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8269 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 8270 | // eg. forward and remove memory writes of opaque types. |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8271 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
| 8272 | if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer) { |
John Kessenich | e7df8e0 | 2018-08-22 17:12:46 -0600 | [diff] [blame] | 8273 | SpirvToolsLegalize(intermediate, spirv, logger, options); |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8274 | prelegalization = false; |
| 8275 | } |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8276 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8277 | if (options->validate) |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8278 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8279 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8280 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8281 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8282 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8283 | #endif |
| 8284 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8285 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8286 | } |
| 8287 | |
| 8288 | }; // end namespace glslang |