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 | #ifdef NV_EXTENSIONS |
| 53 | #include "GLSL.ext.NV.h" |
| 54 | #endif |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 55 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 56 | |
| 57 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 58 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 59 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 60 | #include "../glslang/Include/Common.h" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 61 | #include "../glslang/Include/revision.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 62 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 63 | #include <fstream> |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 64 | #include <iomanip> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 65 | #include <list> |
| 66 | #include <map> |
| 67 | #include <stack> |
| 68 | #include <string> |
| 69 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 70 | |
| 71 | namespace { |
| 72 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 73 | namespace { |
| 74 | class SpecConstantOpModeGuard { |
| 75 | public: |
| 76 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 77 | : builder_(builder) { |
| 78 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 79 | } |
| 80 | ~SpecConstantOpModeGuard() { |
| 81 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 82 | : builder_->setToNormalCodeGenMode(); |
| 83 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 84 | void turnOnSpecConstantOpMode() { |
| 85 | builder_->setToSpecConstCodeGenMode(); |
| 86 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | spv::Builder* builder_; |
| 90 | bool previous_flag_; |
| 91 | }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 92 | |
| 93 | struct OpDecorations { |
| 94 | spv::Decoration precision; |
| 95 | spv::Decoration noContraction; |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 96 | spv::Decoration nonUniform; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 100 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 101 | // |
| 102 | // The main holder of information for translating glslang to SPIR-V. |
| 103 | // |
| 104 | // Derives from the AST walking base class. |
| 105 | // |
| 106 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 107 | public: |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 108 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 109 | glslang::SpvOptions& options); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 110 | virtual ~TGlslangToSpvTraverser() { } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 111 | |
| 112 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 113 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 114 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 115 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 116 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 117 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 118 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 119 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 120 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 121 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 122 | void finishSpv(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 123 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 124 | |
| 125 | protected: |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 126 | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
| 127 | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
| 128 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 129 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 130 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 131 | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 132 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 133 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 134 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 135 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 136 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 137 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 138 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 139 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 140 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, unsigned int& dependencyLength) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 141 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 142 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 143 | spv::Id createSpvVariable(const glslang::TIntermSymbol*); |
| 144 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 145 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 146 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 147 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 148 | spv::Id convertGlslangToSpvType(const glslang::TType& type); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 149 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
| 150 | bool lastBufferBlockMember); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 151 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 152 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 153 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 154 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 155 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 156 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 157 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 158 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 159 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 160 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 161 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 162 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 163 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 164 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 165 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 166 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 167 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 168 | bool writableParam(glslang::TStorageQualifier) const; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 169 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 170 | void makeFunctions(const glslang::TIntermSequence&); |
| 171 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 172 | void visitFunctions(const glslang::TIntermSequence&); |
| 173 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 174 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 175 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 176 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 177 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 178 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 179 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 180 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 181 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 182 | spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 183 | glslang::TBasicType typeProxy); |
| 184 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 185 | glslang::TBasicType typeProxy); |
| 186 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 187 | glslang::TBasicType typeProxy); |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 188 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 189 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 190 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 191 | 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] | 192 | 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] | 193 | 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] | 194 | 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] | 195 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 196 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 197 | #ifdef NV_EXTENSIONS |
| 198 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
| 199 | #endif |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 200 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 201 | 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] | 202 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 203 | bool isTrivial(const glslang::TIntermTyped* node); |
| 204 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 205 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 206 | spv::Id getExtBuiltins(const char* name); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 207 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 208 | void addPre13Extension(const char* ext) |
| 209 | { |
| 210 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 211 | builder.addExtension(ext); |
| 212 | } |
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; |
| 229 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 230 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 231 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 232 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 233 | 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] | 234 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 235 | 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] | 236 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 237 | std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 238 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 239 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | // |
| 243 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 244 | // |
| 245 | |
| 246 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 247 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 248 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 249 | switch (source) { |
| 250 | case glslang::EShSourceGlsl: |
| 251 | switch (profile) { |
| 252 | case ENoProfile: |
| 253 | case ECoreProfile: |
| 254 | case ECompatibilityProfile: |
| 255 | return spv::SourceLanguageGLSL; |
| 256 | case EEsProfile: |
| 257 | return spv::SourceLanguageESSL; |
| 258 | default: |
| 259 | return spv::SourceLanguageUnknown; |
| 260 | } |
| 261 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 262 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 263 | default: |
| 264 | return spv::SourceLanguageUnknown; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Translate glslang language (stage) to SPIR-V execution model. |
| 269 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 270 | { |
| 271 | switch (stage) { |
| 272 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 273 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 274 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 275 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 276 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 277 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 278 | #ifdef NV_EXTENSIONS |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 279 | case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV; |
| 280 | case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV; |
| 281 | case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV; |
| 282 | case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV; |
| 283 | case EShLangMissNV: return spv::ExecutionModelMissNV; |
| 284 | case EShLangCallableNV: return spv::ExecutionModelCallableNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 285 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 286 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 287 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 288 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 289 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 290 | return spv::ExecutionModelFragment; |
| 291 | } |
| 292 | } |
| 293 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 294 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 295 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 296 | { |
| 297 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 298 | case glslang::Esd1D: return spv::Dim1D; |
| 299 | case glslang::Esd2D: return spv::Dim2D; |
| 300 | case glslang::Esd3D: return spv::Dim3D; |
| 301 | case glslang::EsdCube: return spv::DimCube; |
| 302 | case glslang::EsdRect: return spv::DimRect; |
| 303 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 304 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 305 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 306 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 307 | return spv::Dim2D; |
| 308 | } |
| 309 | } |
| 310 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 311 | // Translate glslang precision to SPIR-V precision decorations. |
| 312 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 313 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 314 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 315 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 316 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 317 | default: |
| 318 | return spv::NoPrecision; |
| 319 | } |
| 320 | } |
| 321 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 322 | // Translate glslang type to SPIR-V precision decorations. |
| 323 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 324 | { |
| 325 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 326 | } |
| 327 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 328 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 329 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 330 | { |
| 331 | if (type.getBasicType() == glslang::EbtBlock) { |
| 332 | switch (type.getQualifier().storage) { |
| 333 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 334 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 335 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 336 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 337 | #ifdef NV_EXTENSIONS |
| 338 | case glslang::EvqPayloadNV: return spv::DecorationBlock; |
| 339 | case glslang::EvqPayloadInNV: return spv::DecorationBlock; |
| 340 | case glslang::EvqHitAttrNV: return spv::DecorationBlock; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 341 | case glslang::EvqCallableDataNV: return spv::DecorationBlock; |
| 342 | case glslang::EvqCallableDataInNV: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 343 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 344 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 345 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 350 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 351 | } |
| 352 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 353 | // Translate glslang type to SPIR-V memory decorations. |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 354 | 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] | 355 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 356 | if (!useVulkanMemoryModel) { |
| 357 | if (qualifier.coherent) |
| 358 | memory.push_back(spv::DecorationCoherent); |
| 359 | if (qualifier.volatil) { |
| 360 | memory.push_back(spv::DecorationVolatile); |
| 361 | memory.push_back(spv::DecorationCoherent); |
| 362 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 363 | } |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 364 | if (qualifier.restrict) |
| 365 | memory.push_back(spv::DecorationRestrict); |
| 366 | if (qualifier.readonly) |
| 367 | memory.push_back(spv::DecorationNonWritable); |
| 368 | if (qualifier.writeonly) |
| 369 | memory.push_back(spv::DecorationNonReadable); |
| 370 | } |
| 371 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 372 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 373 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 374 | { |
| 375 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 376 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 377 | case glslang::ElmRowMajor: |
| 378 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 379 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 380 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 381 | default: |
| 382 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 383 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 384 | } |
| 385 | } else { |
| 386 | switch (type.getBasicType()) { |
| 387 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 388 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 389 | break; |
| 390 | case glslang::EbtBlock: |
| 391 | switch (type.getQualifier().storage) { |
| 392 | case glslang::EvqUniform: |
| 393 | case glslang::EvqBuffer: |
| 394 | switch (type.getQualifier().layoutPacking) { |
| 395 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 396 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 397 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 398 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 399 | } |
| 400 | case glslang::EvqVaryingIn: |
| 401 | case glslang::EvqVaryingOut: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 402 | if (type.getQualifier().isTaskMemory()) { |
| 403 | switch (type.getQualifier().layoutPacking) { |
| 404 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 405 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 406 | default: break; |
| 407 | } |
| 408 | } else { |
| 409 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 410 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 411 | return spv::DecorationMax; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 412 | #ifdef NV_EXTENSIONS |
| 413 | case glslang::EvqPayloadNV: |
| 414 | case glslang::EvqPayloadInNV: |
| 415 | case glslang::EvqHitAttrNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 416 | case glslang::EvqCallableDataNV: |
| 417 | case glslang::EvqCallableDataInNV: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 418 | return spv::DecorationMax; |
| 419 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 420 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 421 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 422 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 429 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 430 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 431 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 432 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 433 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 434 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 435 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 436 | else if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 437 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 438 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 439 | return spv::DecorationFlat; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 440 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 441 | else if (qualifier.explicitInterp) { |
| 442 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 443 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 444 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 445 | #endif |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 446 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 447 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 451 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 452 | // should be applied. |
| 453 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 454 | { |
| 455 | if (qualifier.patch) |
| 456 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 457 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 458 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 459 | else if (qualifier.sample) { |
| 460 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 461 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 462 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 463 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 464 | } |
| 465 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 466 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 467 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 468 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 469 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 470 | return spv::DecorationInvariant; |
| 471 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 472 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 473 | } |
| 474 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 475 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 476 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 477 | { |
| 478 | if (qualifier.noContraction) |
| 479 | return spv::DecorationNoContraction; |
| 480 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 481 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 482 | } |
| 483 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 484 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 485 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 486 | { |
| 487 | if (qualifier.isNonUniform()) { |
| 488 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 489 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 490 | return spv::DecorationNonUniformEXT; |
| 491 | } else |
| 492 | return spv::DecorationMax; |
| 493 | } |
| 494 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 495 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 496 | { |
| 497 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) { |
| 498 | return spv::MemoryAccessMaskNone; |
| 499 | } |
| 500 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
| 501 | if (coherentFlags.volatil || |
| 502 | coherentFlags.coherent || |
| 503 | coherentFlags.devicecoherent || |
| 504 | coherentFlags.queuefamilycoherent || |
| 505 | coherentFlags.workgroupcoherent || |
| 506 | coherentFlags.subgroupcoherent) { |
| 507 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 508 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 509 | } |
| 510 | if (coherentFlags.nonprivate) { |
| 511 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 512 | } |
| 513 | if (coherentFlags.volatil) { |
| 514 | mask = mask | spv::MemoryAccessVolatileMask; |
| 515 | } |
| 516 | if (mask != spv::MemoryAccessMaskNone) { |
| 517 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 518 | } |
| 519 | return mask; |
| 520 | } |
| 521 | |
| 522 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 523 | { |
| 524 | if (!glslangIntermediate->usingVulkanMemoryModel()) { |
| 525 | return spv::ImageOperandsMaskNone; |
| 526 | } |
| 527 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 528 | if (coherentFlags.volatil || |
| 529 | coherentFlags.coherent || |
| 530 | coherentFlags.devicecoherent || |
| 531 | coherentFlags.queuefamilycoherent || |
| 532 | coherentFlags.workgroupcoherent || |
| 533 | coherentFlags.subgroupcoherent) { |
| 534 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 535 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 536 | } |
| 537 | if (coherentFlags.nonprivate) { |
| 538 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 539 | } |
| 540 | if (coherentFlags.volatil) { |
| 541 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 542 | } |
| 543 | if (mask != spv::ImageOperandsMaskNone) { |
| 544 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 545 | } |
| 546 | return mask; |
| 547 | } |
| 548 | |
| 549 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 550 | { |
| 551 | spv::Builder::AccessChain::CoherentFlags flags; |
| 552 | flags.coherent = type.getQualifier().coherent; |
| 553 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 554 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 555 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 556 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 557 | type.getQualifier().storage == glslang::EvqShared; |
| 558 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
| 559 | // *coherent variables are implicitly nonprivate in GLSL |
| 560 | flags.nonprivate = type.getQualifier().nonprivate || |
Jeff Bolz | ab3c965 | 2018-10-15 22:46:48 -0500 | [diff] [blame] | 561 | flags.subgroupcoherent || |
| 562 | flags.workgroupcoherent || |
| 563 | flags.queuefamilycoherent || |
| 564 | flags.devicecoherent || |
| 565 | flags.coherent; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 566 | flags.volatil = type.getQualifier().volatil; |
| 567 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
| 568 | return flags; |
| 569 | } |
| 570 | |
| 571 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 572 | { |
| 573 | spv::Scope scope; |
| 574 | if (coherentFlags.coherent) { |
| 575 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 576 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 577 | } else if (coherentFlags.devicecoherent) { |
| 578 | scope = spv::ScopeDevice; |
| 579 | } else if (coherentFlags.queuefamilycoherent) { |
| 580 | scope = spv::ScopeQueueFamilyKHR; |
| 581 | } else if (coherentFlags.workgroupcoherent) { |
| 582 | scope = spv::ScopeWorkgroup; |
| 583 | } else if (coherentFlags.subgroupcoherent) { |
| 584 | scope = spv::ScopeSubgroup; |
| 585 | } else { |
| 586 | scope = spv::ScopeMax; |
| 587 | } |
| 588 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 589 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 590 | } |
| 591 | return scope; |
| 592 | } |
| 593 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 594 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 595 | // associated capabilities when required. For some built-in variables, a capability |
| 596 | // is generated only when using the variable in an executable instruction, but not when |
| 597 | // just declaring a struct member variable with it. This is true for PointSize, |
| 598 | // ClipDistance, and CullDistance. |
| 599 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 600 | { |
| 601 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 602 | case glslang::EbvPointSize: |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 603 | // Defer adding the capability until the built-in is actually used. |
| 604 | if (! memberDeclaration) { |
| 605 | switch (glslangIntermediate->getStage()) { |
| 606 | case EShLangGeometry: |
| 607 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 608 | break; |
| 609 | case EShLangTessControl: |
| 610 | case EShLangTessEvaluation: |
| 611 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 612 | break; |
| 613 | default: |
| 614 | break; |
| 615 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 616 | } |
| 617 | return spv::BuiltInPointSize; |
| 618 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 619 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 620 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 621 | // They are now generated when used, rather than here when declared. |
| 622 | // Potentially, the specification should be more clear what the minimum |
| 623 | // use needed is to trigger the capability. |
| 624 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 625 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 626 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 627 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 628 | return spv::BuiltInClipDistance; |
| 629 | |
| 630 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 631 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 632 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 633 | return spv::BuiltInCullDistance; |
| 634 | |
| 635 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 636 | builder.addCapability(spv::CapabilityMultiViewport); |
| 637 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 638 | glslangIntermediate->getStage() == EShLangTessControl || |
| 639 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 640 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 641 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 642 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 643 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 644 | return spv::BuiltInViewportIndex; |
| 645 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 646 | case glslang::EbvSampleId: |
| 647 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 648 | return spv::BuiltInSampleId; |
| 649 | |
| 650 | case glslang::EbvSamplePosition: |
| 651 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 652 | return spv::BuiltInSamplePosition; |
| 653 | |
| 654 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 655 | return spv::BuiltInSampleMask; |
| 656 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 657 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 658 | #ifdef NV_EXTENSIONS |
| 659 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 660 | return spv::BuiltInLayer; |
| 661 | } |
| 662 | #endif |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 663 | builder.addCapability(spv::CapabilityGeometry); |
| 664 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 665 | glslangIntermediate->getStage() == EShLangTessControl || |
| 666 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 667 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 668 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 669 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 670 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 671 | return spv::BuiltInLayer; |
| 672 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 673 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 674 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 675 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 676 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 677 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 678 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 679 | case glslang::EbvBaseVertex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 680 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 681 | builder.addCapability(spv::CapabilityDrawParameters); |
| 682 | return spv::BuiltInBaseVertex; |
| 683 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 684 | case glslang::EbvBaseInstance: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 685 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 686 | builder.addCapability(spv::CapabilityDrawParameters); |
| 687 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 688 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 689 | case glslang::EbvDrawId: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 690 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 691 | builder.addCapability(spv::CapabilityDrawParameters); |
| 692 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 693 | |
| 694 | case glslang::EbvPrimitiveId: |
| 695 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 696 | builder.addCapability(spv::CapabilityGeometry); |
| 697 | return spv::BuiltInPrimitiveId; |
| 698 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 699 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 700 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 701 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 702 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 703 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 704 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 705 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 706 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 707 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 708 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 709 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 710 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 711 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 712 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 713 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 714 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 715 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 716 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 717 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 718 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 719 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 720 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 721 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 722 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 723 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 724 | return spv::BuiltInSubgroupSize; |
| 725 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 726 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 727 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 728 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 729 | return spv::BuiltInSubgroupLocalInvocationId; |
| 730 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 731 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 732 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 733 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 734 | return spv::BuiltInSubgroupEqMaskKHR; |
| 735 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 736 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 737 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 738 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 739 | return spv::BuiltInSubgroupGeMaskKHR; |
| 740 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 741 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 742 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 743 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 744 | return spv::BuiltInSubgroupGtMaskKHR; |
| 745 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 746 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 747 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 748 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 749 | return spv::BuiltInSubgroupLeMaskKHR; |
| 750 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 751 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 752 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 753 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 754 | return spv::BuiltInSubgroupLtMaskKHR; |
| 755 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 756 | case glslang::EbvNumSubgroups: |
| 757 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 758 | return spv::BuiltInNumSubgroups; |
| 759 | |
| 760 | case glslang::EbvSubgroupID: |
| 761 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 762 | return spv::BuiltInSubgroupId; |
| 763 | |
| 764 | case glslang::EbvSubgroupSize2: |
| 765 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 766 | return spv::BuiltInSubgroupSize; |
| 767 | |
| 768 | case glslang::EbvSubgroupInvocation2: |
| 769 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 770 | return spv::BuiltInSubgroupLocalInvocationId; |
| 771 | |
| 772 | case glslang::EbvSubgroupEqMask2: |
| 773 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 774 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 775 | return spv::BuiltInSubgroupEqMask; |
| 776 | |
| 777 | case glslang::EbvSubgroupGeMask2: |
| 778 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 779 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 780 | return spv::BuiltInSubgroupGeMask; |
| 781 | |
| 782 | case glslang::EbvSubgroupGtMask2: |
| 783 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 784 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 785 | return spv::BuiltInSubgroupGtMask; |
| 786 | |
| 787 | case glslang::EbvSubgroupLeMask2: |
| 788 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 789 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 790 | return spv::BuiltInSubgroupLeMask; |
| 791 | |
| 792 | case glslang::EbvSubgroupLtMask2: |
| 793 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 794 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 795 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 796 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 797 | case glslang::EbvBaryCoordNoPersp: |
| 798 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 799 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 800 | |
| 801 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 802 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 803 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 804 | |
| 805 | case glslang::EbvBaryCoordNoPerspSample: |
| 806 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 807 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 808 | |
| 809 | case glslang::EbvBaryCoordSmooth: |
| 810 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 811 | return spv::BuiltInBaryCoordSmoothAMD; |
| 812 | |
| 813 | case glslang::EbvBaryCoordSmoothCentroid: |
| 814 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 815 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 816 | |
| 817 | case glslang::EbvBaryCoordSmoothSample: |
| 818 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 819 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 820 | |
| 821 | case glslang::EbvBaryCoordPullModel: |
| 822 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 823 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 824 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 825 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 826 | case glslang::EbvDeviceIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 827 | addPre13Extension(spv::E_SPV_KHR_device_group); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 828 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 829 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 830 | |
| 831 | case glslang::EbvViewIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 832 | addPre13Extension(spv::E_SPV_KHR_multiview); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 833 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 834 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 835 | |
Daniel Koch | 5154db5 | 2018-11-26 10:01:58 -0500 | [diff] [blame] | 836 | case glslang::EbvFragSizeEXT: |
| 837 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 838 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 839 | return spv::BuiltInFragSizeEXT; |
| 840 | |
| 841 | case glslang::EbvFragInvocationCountEXT: |
| 842 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 843 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 844 | return spv::BuiltInFragInvocationCountEXT; |
| 845 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 846 | #ifdef NV_EXTENSIONS |
| 847 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 848 | if (!memberDeclaration) { |
| 849 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 850 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 851 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 852 | return spv::BuiltInViewportMaskNV; |
| 853 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 854 | if (!memberDeclaration) { |
| 855 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 856 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 857 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 858 | return spv::BuiltInSecondaryPositionNV; |
| 859 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 860 | if (!memberDeclaration) { |
| 861 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 862 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 863 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 864 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 865 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 866 | if (!memberDeclaration) { |
| 867 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 868 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 869 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 870 | return spv::BuiltInPositionPerViewNV; |
| 871 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 872 | if (!memberDeclaration) { |
| 873 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 874 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 875 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 876 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 877 | case glslang::EbvFragFullyCoveredNV: |
| 878 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 879 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 880 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 881 | case glslang::EbvFragmentSizeNV: |
| 882 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 883 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 884 | return spv::BuiltInFragmentSizeNV; |
| 885 | case glslang::EbvInvocationsPerPixelNV: |
| 886 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 887 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 888 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 889 | |
| 890 | // raytracing |
| 891 | case glslang::EbvLaunchIdNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 892 | return spv::BuiltInLaunchIdNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 893 | case glslang::EbvLaunchSizeNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 894 | return spv::BuiltInLaunchSizeNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 895 | case glslang::EbvWorldRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 896 | return spv::BuiltInWorldRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 897 | case glslang::EbvWorldRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 898 | return spv::BuiltInWorldRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 899 | case glslang::EbvObjectRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 900 | return spv::BuiltInObjectRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 901 | case glslang::EbvObjectRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 902 | return spv::BuiltInObjectRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 903 | case glslang::EbvRayTminNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 904 | return spv::BuiltInRayTminNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 905 | case glslang::EbvRayTmaxNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 906 | return spv::BuiltInRayTmaxNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 907 | case glslang::EbvInstanceCustomIndexNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 908 | return spv::BuiltInInstanceCustomIndexNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 909 | case glslang::EbvHitTNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 910 | return spv::BuiltInHitTNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 911 | case glslang::EbvHitKindNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 912 | return spv::BuiltInHitKindNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 913 | case glslang::EbvObjectToWorldNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 914 | return spv::BuiltInObjectToWorldNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 915 | case glslang::EbvWorldToObjectNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 916 | return spv::BuiltInWorldToObjectNV; |
| 917 | case glslang::EbvIncomingRayFlagsNV: |
| 918 | return spv::BuiltInIncomingRayFlagsNV; |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 919 | case glslang::EbvBaryCoordNV: |
| 920 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 921 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 922 | return spv::BuiltInBaryCoordNV; |
| 923 | case glslang::EbvBaryCoordNoPerspNV: |
| 924 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 925 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 926 | return spv::BuiltInBaryCoordNoPerspNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 927 | case glslang::EbvTaskCountNV: |
| 928 | return spv::BuiltInTaskCountNV; |
| 929 | case glslang::EbvPrimitiveCountNV: |
| 930 | return spv::BuiltInPrimitiveCountNV; |
| 931 | case glslang::EbvPrimitiveIndicesNV: |
| 932 | return spv::BuiltInPrimitiveIndicesNV; |
| 933 | case glslang::EbvClipDistancePerViewNV: |
| 934 | return spv::BuiltInClipDistancePerViewNV; |
| 935 | case glslang::EbvCullDistancePerViewNV: |
| 936 | return spv::BuiltInCullDistancePerViewNV; |
| 937 | case glslang::EbvLayerPerViewNV: |
| 938 | return spv::BuiltInLayerPerViewNV; |
| 939 | case glslang::EbvMeshViewCountNV: |
| 940 | return spv::BuiltInMeshViewCountNV; |
| 941 | case glslang::EbvMeshViewIndicesNV: |
| 942 | return spv::BuiltInMeshViewIndicesNV; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 943 | #endif |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 944 | default: |
| 945 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 949 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 950 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 951 | { |
| 952 | assert(type.getBasicType() == glslang::EbtSampler); |
| 953 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 954 | // Check for capabilities |
| 955 | switch (type.getQualifier().layoutFormat) { |
| 956 | case glslang::ElfRg32f: |
| 957 | case glslang::ElfRg16f: |
| 958 | case glslang::ElfR11fG11fB10f: |
| 959 | case glslang::ElfR16f: |
| 960 | case glslang::ElfRgba16: |
| 961 | case glslang::ElfRgb10A2: |
| 962 | case glslang::ElfRg16: |
| 963 | case glslang::ElfRg8: |
| 964 | case glslang::ElfR16: |
| 965 | case glslang::ElfR8: |
| 966 | case glslang::ElfRgba16Snorm: |
| 967 | case glslang::ElfRg16Snorm: |
| 968 | case glslang::ElfRg8Snorm: |
| 969 | case glslang::ElfR16Snorm: |
| 970 | case glslang::ElfR8Snorm: |
| 971 | |
| 972 | case glslang::ElfRg32i: |
| 973 | case glslang::ElfRg16i: |
| 974 | case glslang::ElfRg8i: |
| 975 | case glslang::ElfR16i: |
| 976 | case glslang::ElfR8i: |
| 977 | |
| 978 | case glslang::ElfRgb10a2ui: |
| 979 | case glslang::ElfRg32ui: |
| 980 | case glslang::ElfRg16ui: |
| 981 | case glslang::ElfRg8ui: |
| 982 | case glslang::ElfR16ui: |
| 983 | case glslang::ElfR8ui: |
| 984 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 985 | break; |
| 986 | |
| 987 | default: |
| 988 | break; |
| 989 | } |
| 990 | |
| 991 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 992 | switch (type.getQualifier().layoutFormat) { |
| 993 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 994 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 995 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 996 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 997 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 998 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 999 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1000 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1001 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1002 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1003 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1004 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1005 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1006 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1007 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1008 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1009 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1010 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1011 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1012 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1013 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1014 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1015 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1016 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1017 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1018 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1019 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1020 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1021 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1022 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1023 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1024 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1025 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1026 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1027 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1028 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1029 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1030 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1031 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1032 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1033 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1037 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1038 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1039 | if (selectionNode.getFlatten()) |
| 1040 | return spv::SelectionControlFlattenMask; |
| 1041 | if (selectionNode.getDontFlatten()) |
| 1042 | return spv::SelectionControlDontFlattenMask; |
| 1043 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1044 | } |
| 1045 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1046 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1047 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1048 | if (switchNode.getFlatten()) |
| 1049 | return spv::SelectionControlFlattenMask; |
| 1050 | if (switchNode.getDontFlatten()) |
| 1051 | return spv::SelectionControlDontFlattenMask; |
| 1052 | return spv::SelectionControlMaskNone; |
| 1053 | } |
| 1054 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1055 | // return a non-0 dependency if the dependency argument must be set |
| 1056 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
| 1057 | unsigned int& dependencyLength) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1058 | { |
| 1059 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1060 | |
| 1061 | if (loopNode.getDontUnroll()) |
| 1062 | control = control | spv::LoopControlDontUnrollMask; |
| 1063 | if (loopNode.getUnroll()) |
| 1064 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1065 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1066 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1067 | else if (loopNode.getLoopDependency() > 0) { |
| 1068 | control = control | spv::LoopControlDependencyLengthMask; |
| 1069 | dependencyLength = loopNode.getLoopDependency(); |
| 1070 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1071 | |
| 1072 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1073 | } |
| 1074 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1075 | // Translate glslang type to SPIR-V storage class. |
| 1076 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1077 | { |
| 1078 | if (type.getQualifier().isPipeInput()) |
| 1079 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1080 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1081 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1082 | |
| 1083 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
| 1084 | type.getQualifier().storage == glslang::EvqUniform) { |
| 1085 | if (type.getBasicType() == glslang::EbtAtomicUint) |
| 1086 | return spv::StorageClassAtomicCounter; |
| 1087 | if (type.containsOpaque()) |
| 1088 | return spv::StorageClassUniformConstant; |
| 1089 | } |
| 1090 | |
| 1091 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1092 | addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class); |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1093 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1097 | if (type.getQualifier().layoutPushConstant) |
| 1098 | return spv::StorageClassPushConstant; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1099 | #ifdef NV_EXTENSIONS |
| 1100 | if (type.getQualifier().layoutShaderRecordNV) |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1101 | return spv::StorageClassShaderRecordBufferNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1102 | #endif |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1103 | if (type.getBasicType() == glslang::EbtBlock) |
| 1104 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1105 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1106 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1107 | |
| 1108 | switch (type.getQualifier().storage) { |
| 1109 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
| 1110 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1111 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1112 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1113 | #ifdef NV_EXTENSIONS |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1114 | case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV; |
| 1115 | case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV; |
| 1116 | case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV; |
| 1117 | case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV; |
| 1118 | case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1119 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1120 | default: |
| 1121 | assert(0); |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1126 | } |
| 1127 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1128 | // Add capabilities pertaining to how an array is indexed. |
| 1129 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1130 | const glslang::TType& indexType) |
| 1131 | { |
| 1132 | if (indexType.getQualifier().isNonUniform()) { |
| 1133 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1134 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1135 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1136 | if (baseType.getQualifier().hasAttachment()) |
| 1137 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
| 1138 | else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1139 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
| 1140 | else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1141 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1142 | else if (baseType.isImage()) |
| 1143 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1144 | else if (baseType.isTexture()) |
| 1145 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1146 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1147 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1148 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1149 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1150 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1151 | } |
| 1152 | } else { |
| 1153 | // assume a dynamically uniform index |
| 1154 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1155 | if (baseType.getQualifier().hasAttachment()) { |
| 1156 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1157 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1158 | } else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1159 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1160 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1161 | } else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1162 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1163 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1164 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1169 | // 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] | 1170 | // descriptor set. |
| 1171 | bool IsDescriptorResource(const glslang::TType& type) |
| 1172 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1173 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1174 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1175 | return type.getQualifier().isUniformOrBuffer() && |
| 1176 | #ifdef NV_EXTENSIONS |
| 1177 | ! type.getQualifier().layoutShaderRecordNV && |
| 1178 | #endif |
| 1179 | ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1180 | |
| 1181 | // non block... |
| 1182 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1183 | // if they are the global-scope-class, not the function parameter |
| 1184 | // (or local, if they ever exist) class. |
| 1185 | if (type.getBasicType() == glslang::EbtSampler) |
| 1186 | return type.getQualifier().isUniformOrBuffer(); |
| 1187 | |
| 1188 | // None of the above. |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1192 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1193 | { |
| 1194 | if (child.layoutMatrix == glslang::ElmNone) |
| 1195 | child.layoutMatrix = parent.layoutMatrix; |
| 1196 | |
| 1197 | if (parent.invariant) |
| 1198 | child.invariant = true; |
| 1199 | if (parent.nopersp) |
| 1200 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1201 | #ifdef AMD_EXTENSIONS |
| 1202 | if (parent.explicitInterp) |
| 1203 | child.explicitInterp = true; |
| 1204 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1205 | if (parent.flat) |
| 1206 | child.flat = true; |
| 1207 | if (parent.centroid) |
| 1208 | child.centroid = true; |
| 1209 | if (parent.patch) |
| 1210 | child.patch = true; |
| 1211 | if (parent.sample) |
| 1212 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1213 | if (parent.coherent) |
| 1214 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1215 | if (parent.devicecoherent) |
| 1216 | child.devicecoherent = true; |
| 1217 | if (parent.queuefamilycoherent) |
| 1218 | child.queuefamilycoherent = true; |
| 1219 | if (parent.workgroupcoherent) |
| 1220 | child.workgroupcoherent = true; |
| 1221 | if (parent.subgroupcoherent) |
| 1222 | child.subgroupcoherent = true; |
| 1223 | if (parent.nonprivate) |
| 1224 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1225 | if (parent.volatil) |
| 1226 | child.volatil = true; |
| 1227 | if (parent.restrict) |
| 1228 | child.restrict = true; |
| 1229 | if (parent.readonly) |
| 1230 | child.readonly = true; |
| 1231 | if (parent.writeonly) |
| 1232 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1233 | #ifdef NV_EXTENSIONS |
| 1234 | if (parent.perPrimitiveNV) |
| 1235 | child.perPrimitiveNV = true; |
| 1236 | if (parent.perViewNV) |
| 1237 | child.perViewNV = true; |
| 1238 | if (parent.perTaskNV) |
| 1239 | child.perTaskNV = true; |
| 1240 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1243 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1244 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1245 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1246 | // - struct members might inherit from a struct declaration |
| 1247 | // (note that non-block structs don't explicitly inherit, |
| 1248 | // only implicitly, meaning no decoration involved) |
| 1249 | // - affect decorations on the struct members |
| 1250 | // (note smooth does not, and expecting something like volatile |
| 1251 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1252 | // - 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] | 1253 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1256 | // |
| 1257 | // Implement the TGlslangToSpvTraverser class. |
| 1258 | // |
| 1259 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1260 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1261 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 1262 | : TIntermTraverser(true, false, true), |
| 1263 | options(options), |
| 1264 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1265 | sequenceDepth(0), logger(buildLogger), |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1266 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1267 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1268 | glslangIntermediate(glslangIntermediate) |
| 1269 | { |
| 1270 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1271 | |
| 1272 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1273 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1274 | glslangIntermediate->getVersion()); |
| 1275 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1276 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1277 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1278 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1279 | |
| 1280 | // Set the source shader's text. If for SPV version 1.0, include |
| 1281 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1282 | // Otherwise, emit those as actual instructions. |
| 1283 | std::string text; |
| 1284 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1285 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1286 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1287 | text.append("// OpModuleProcessed "); |
| 1288 | text.append(processes[p]); |
| 1289 | text.append("\n"); |
| 1290 | } else |
| 1291 | builder.addModuleProcessed(processes[p]); |
| 1292 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1293 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1294 | text.append("#line 1\n"); |
| 1295 | text.append(glslangIntermediate->getSourceText()); |
| 1296 | builder.setSourceText(text); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1297 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1298 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1299 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 1300 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelVulkanKHR); |
| 1301 | builder.addExtension(spv::E_SPV_KHR_vulkan_memory_model); |
| 1302 | } else { |
| 1303 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); |
| 1304 | } |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1305 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1306 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1307 | |
| 1308 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1309 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1310 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1311 | builder.addSourceExtension(it->c_str()); |
| 1312 | |
| 1313 | // Add the top-level modes for this shader. |
| 1314 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1315 | if (glslangIntermediate->getXfbMode()) { |
| 1316 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1317 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1318 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1319 | |
| 1320 | unsigned int mode; |
| 1321 | switch (glslangIntermediate->getStage()) { |
| 1322 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1323 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1324 | break; |
| 1325 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1326 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1327 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1328 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1329 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1330 | glslang::TLayoutGeometry primitive; |
| 1331 | |
| 1332 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1333 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1334 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1335 | } else { |
| 1336 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1337 | } |
| 1338 | |
| 1339 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1340 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1341 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1342 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1343 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1344 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1345 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1346 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1347 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1348 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1349 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1350 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1351 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1352 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1353 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1354 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1355 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1356 | |
| 1357 | switch (glslangIntermediate->getVertexOrder()) { |
| 1358 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1359 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1360 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1361 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1362 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1363 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1364 | |
| 1365 | if (glslangIntermediate->getPointMode()) |
| 1366 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1367 | break; |
| 1368 | |
| 1369 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1370 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1371 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1372 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1373 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1374 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1375 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1376 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1377 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1378 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1379 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1380 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1381 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1382 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1383 | |
| 1384 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1385 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1386 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1387 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1388 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1389 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1390 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1391 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1392 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1393 | break; |
| 1394 | |
| 1395 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1396 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1397 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1398 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1399 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1400 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1401 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1402 | else |
| 1403 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1404 | |
| 1405 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1406 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1407 | |
chaoc | c120452 | 2017-06-30 17:14:30 -0700 | [diff] [blame] | 1408 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1409 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1410 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1411 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1412 | } |
| 1413 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1414 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1415 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1416 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1417 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1418 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1419 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1420 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1421 | |
| 1422 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1423 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1424 | break; |
| 1425 | |
| 1426 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1427 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 1428 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1429 | glslangIntermediate->getLocalSize(1), |
| 1430 | glslangIntermediate->getLocalSize(2)); |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 1431 | #ifdef NV_EXTENSIONS |
| 1432 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1433 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1434 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1435 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1436 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1437 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1438 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1439 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1440 | } |
| 1441 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1442 | break; |
| 1443 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1444 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1445 | case EShLangRayGenNV: |
| 1446 | case EShLangIntersectNV: |
| 1447 | case EShLangAnyHitNV: |
| 1448 | case EShLangClosestHitNV: |
| 1449 | case EShLangMissNV: |
| 1450 | case EShLangCallableNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1451 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1452 | builder.addExtension("SPV_NV_ray_tracing"); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1453 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1454 | case EShLangTaskNV: |
| 1455 | case EShLangMeshNV: |
| 1456 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1457 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1458 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1459 | glslangIntermediate->getLocalSize(1), |
| 1460 | glslangIntermediate->getLocalSize(2)); |
| 1461 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 1462 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1463 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives()); |
| 1464 | |
| 1465 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1466 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1467 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1468 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1469 | default: mode = spv::ExecutionModeMax; break; |
| 1470 | } |
| 1471 | if (mode != spv::ExecutionModeMax) |
| 1472 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1473 | } |
| 1474 | break; |
| 1475 | #endif |
| 1476 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1477 | default: |
| 1478 | break; |
| 1479 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1480 | } |
| 1481 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1482 | // Finish creating SPV, after the traversal is complete. |
| 1483 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1484 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1485 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1486 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1487 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1488 | builder.leaveFunction(); |
| 1489 | } |
| 1490 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1491 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1492 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1493 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1494 | |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1495 | // Add capabilities, extensions, remove unneeded decorations, etc., |
| 1496 | // based on the resulting SPIR-V. |
| 1497 | builder.postProcess(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1500 | // Write the SPV into 'out'. |
| 1501 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1502 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1503 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | // |
| 1507 | // Implement the traversal functions. |
| 1508 | // |
| 1509 | // Return true from interior nodes to have the external traversal |
| 1510 | // continue on to children. Return false if children were |
| 1511 | // already processed. |
| 1512 | // |
| 1513 | |
| 1514 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1515 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1516 | // - uniform/input reads |
| 1517 | // - output writes |
| 1518 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1519 | // - something simple that degenerates into the last bullet |
| 1520 | // |
| 1521 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1522 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1523 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1524 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1525 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1526 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1527 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1528 | // Formal function parameters were mapped during makeFunctions(). |
| 1529 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1530 | |
| 1531 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1532 | if (builder.isPointer(id)) { |
| 1533 | spv::StorageClass sc = builder.getStorageClass(id); |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1534 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) { |
| 1535 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) |
| 1536 | iOSet.insert(id); |
| 1537 | } |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1541 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1542 | // Prepare to generate code for the access |
| 1543 | |
| 1544 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1545 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1546 | // followed by setting the base. |
| 1547 | builder.clearAccessChain(); |
| 1548 | |
| 1549 | // 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] | 1550 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1551 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1552 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1553 | // 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] | 1554 | // These are also pure R-values. |
| 1555 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1556 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1557 | builder.setAccessChainRValue(id); |
| 1558 | else |
| 1559 | builder.setAccessChainLValue(id); |
| 1560 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1561 | |
| 1562 | // Process linkage-only nodes for any special additional interface work. |
| 1563 | if (linkageOnly) { |
| 1564 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1565 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1566 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1567 | // of declaration. |
| 1568 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1569 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1570 | // Save possible originating buffers for counter buffers, keyed by |
| 1571 | // making the potential counter-buffer name. |
| 1572 | std::string keyName = symbol->getName().c_str(); |
| 1573 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1574 | counterOriginator[keyName] = symbol; |
| 1575 | } else { |
| 1576 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1577 | std::string keyName = symbol->getName().c_str(); |
| 1578 | auto it = counterOriginator.find(keyName); |
| 1579 | if (it != counterOriginator.end()) { |
| 1580 | id = getSymbolId(it->second); |
| 1581 | if (id != spv::NoResult) { |
| 1582 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1583 | if (counterId != spv::NoResult) { |
| 1584 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1585 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1586 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1596 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1597 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1598 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1599 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1600 | if (node->getType().getQualifier().isSpecConstant()) |
| 1601 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1602 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1603 | // First, handle special cases |
| 1604 | switch (node->getOp()) { |
| 1605 | case glslang::EOpAssign: |
| 1606 | case glslang::EOpAddAssign: |
| 1607 | case glslang::EOpSubAssign: |
| 1608 | case glslang::EOpMulAssign: |
| 1609 | case glslang::EOpVectorTimesMatrixAssign: |
| 1610 | case glslang::EOpVectorTimesScalarAssign: |
| 1611 | case glslang::EOpMatrixTimesScalarAssign: |
| 1612 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1613 | case glslang::EOpDivAssign: |
| 1614 | case glslang::EOpModAssign: |
| 1615 | case glslang::EOpAndAssign: |
| 1616 | case glslang::EOpInclusiveOrAssign: |
| 1617 | case glslang::EOpExclusiveOrAssign: |
| 1618 | case glslang::EOpLeftShiftAssign: |
| 1619 | case glslang::EOpRightShiftAssign: |
| 1620 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1621 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1622 | // says to evaluate the left before the right. So, always, left |
| 1623 | // node then right node. |
| 1624 | { |
| 1625 | // get the left l-value, save it away |
| 1626 | builder.clearAccessChain(); |
| 1627 | node->getLeft()->traverse(this); |
| 1628 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1629 | |
| 1630 | // evaluate the right |
| 1631 | builder.clearAccessChain(); |
| 1632 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1633 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1634 | |
| 1635 | if (node->getOp() != glslang::EOpAssign) { |
| 1636 | // the left is also an r-value |
| 1637 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1638 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1639 | |
| 1640 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1641 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1642 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1643 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1644 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1645 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1646 | node->getType().getBasicType()); |
| 1647 | |
| 1648 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1649 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | // store the result |
| 1653 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1654 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1655 | |
| 1656 | // assignments are expressions having an rValue after they are evaluated... |
| 1657 | builder.clearAccessChain(); |
| 1658 | builder.setAccessChainRValue(rValue); |
| 1659 | } |
| 1660 | return false; |
| 1661 | case glslang::EOpIndexDirect: |
| 1662 | case glslang::EOpIndexDirectStruct: |
| 1663 | { |
| 1664 | // Get the left part of the access chain. |
| 1665 | node->getLeft()->traverse(this); |
| 1666 | |
| 1667 | // Add the next element in the chain |
| 1668 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1669 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1670 | if (! node->getLeft()->getType().isArray() && |
| 1671 | node->getLeft()->getType().isVector() && |
| 1672 | node->getOp() == glslang::EOpIndexDirect) { |
| 1673 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1674 | // so short circuit the access-chain stuff with a swizzle. |
| 1675 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1676 | swizzle.push_back(glslangIndex); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1677 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1678 | } else { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1679 | int spvIndex = glslangIndex; |
| 1680 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1681 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1682 | { |
| 1683 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1684 | // index remapping due to hidden members in anonymous blocks. |
| 1685 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1686 | assert(remapper.size() > 0); |
| 1687 | spvIndex = remapper[glslangIndex]; |
| 1688 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1689 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1690 | // normal case for indexing array or structure or block |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1691 | builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType())); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1692 | |
| 1693 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1694 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1695 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1696 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | return false; |
| 1700 | case glslang::EOpIndexIndirect: |
| 1701 | { |
| 1702 | // Structure or array or vector indirection. |
| 1703 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 1704 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1705 | // Will use the access chain's 'component' for variable index into a vector. |
| 1706 | |
| 1707 | // This adapter is building access chains left to right. |
| 1708 | // Set up the access chain to the left. |
| 1709 | node->getLeft()->traverse(this); |
| 1710 | |
| 1711 | // save it so that computing the right side doesn't trash it |
| 1712 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1713 | |
| 1714 | // compute the next index in the chain |
| 1715 | builder.clearAccessChain(); |
| 1716 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1717 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1718 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1719 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1720 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1721 | // restore the saved access chain |
| 1722 | builder.setAccessChain(partial); |
| 1723 | |
| 1724 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1725 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1726 | else |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1727 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1728 | } |
| 1729 | return false; |
| 1730 | case glslang::EOpVectorSwizzle: |
| 1731 | { |
| 1732 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1733 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1734 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1735 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1736 | } |
| 1737 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1738 | case glslang::EOpMatrixSwizzle: |
| 1739 | logger->missingFunctionality("matrix swizzle"); |
| 1740 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1741 | case glslang::EOpLogicalOr: |
| 1742 | case glslang::EOpLogicalAnd: |
| 1743 | { |
| 1744 | |
| 1745 | // These may require short circuiting, but can sometimes be done as straight |
| 1746 | // binary operations. The right operand must be short circuited if it has |
| 1747 | // side effects, and should probably be if it is complex. |
| 1748 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1749 | break; // handle below as a normal binary operation |
| 1750 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1751 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1752 | builder.clearAccessChain(); |
| 1753 | builder.setAccessChainRValue(result); |
| 1754 | } |
| 1755 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1756 | default: |
| 1757 | break; |
| 1758 | } |
| 1759 | |
| 1760 | // Assume generic binary op... |
| 1761 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1762 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1763 | builder.clearAccessChain(); |
| 1764 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1765 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1766 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1767 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1768 | builder.clearAccessChain(); |
| 1769 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1770 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1771 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1772 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1773 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1774 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1775 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1776 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1777 | convertGlslangToSpvType(node->getType()), left, right, |
| 1778 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1779 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1780 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1781 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1782 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1783 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1784 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1785 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1786 | return false; |
| 1787 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1791 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1792 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1793 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1794 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1795 | if (node->getType().getQualifier().isSpecConstant()) |
| 1796 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1797 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1798 | spv::Id result = spv::NoResult; |
| 1799 | |
| 1800 | // try texturing first |
| 1801 | result = createImageTextureFunctionCall(node); |
| 1802 | if (result != spv::NoResult) { |
| 1803 | builder.clearAccessChain(); |
| 1804 | builder.setAccessChainRValue(result); |
| 1805 | |
| 1806 | return false; // done with this node |
| 1807 | } |
| 1808 | |
| 1809 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1810 | |
| 1811 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1812 | // Quite special; won't want to evaluate the operand. |
| 1813 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1814 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 1815 | // except for the last block membeor of an SSBO. |
| 1816 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 1817 | // size extracted. |
| 1818 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1819 | // Normal .length() would have been constant folded by the front-end. |
| 1820 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1821 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1822 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1823 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1824 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1825 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1826 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1827 | |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 1828 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 1829 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 1830 | // AST expectation of a signed result. |
| 1831 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) |
| 1832 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 1833 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1834 | builder.clearAccessChain(); |
| 1835 | builder.setAccessChainRValue(length); |
| 1836 | |
| 1837 | return false; |
| 1838 | } |
| 1839 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1840 | // Start by evaluating the operand |
| 1841 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1842 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1843 | // operate first on the swizzle base, then apply the swizzle. |
| 1844 | spv::Id invertedType = spv::NoType; |
| 1845 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 1846 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 1847 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 1848 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1849 | builder.clearAccessChain(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1850 | if (invertedType != spv::NoType) |
| 1851 | node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this); |
| 1852 | else |
| 1853 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1854 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1855 | spv::Id operand = spv::NoResult; |
| 1856 | |
| 1857 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1858 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1859 | node->getOp() == glslang::EOpAtomicCounter || |
| 1860 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1861 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1862 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1863 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1864 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1865 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1866 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1867 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1868 | |
| 1869 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1870 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1871 | result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1872 | |
| 1873 | // if not, then possibly an operation |
| 1874 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1875 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1876 | |
| 1877 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1878 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1879 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1880 | builder.addDecoration(result, decorations.nonUniform); |
| 1881 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1882 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1883 | builder.clearAccessChain(); |
| 1884 | builder.setAccessChainRValue(result); |
| 1885 | |
| 1886 | return false; // done with this node |
| 1887 | } |
| 1888 | |
| 1889 | // it must be a special case, check... |
| 1890 | switch (node->getOp()) { |
| 1891 | case glslang::EOpPostIncrement: |
| 1892 | case glslang::EOpPostDecrement: |
| 1893 | case glslang::EOpPreIncrement: |
| 1894 | case glslang::EOpPreDecrement: |
| 1895 | { |
| 1896 | // 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] | 1897 | spv::Id one = 0; |
| 1898 | if (node->getBasicType() == glslang::EbtFloat) |
| 1899 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 1900 | else if (node->getBasicType() == glslang::EbtDouble) |
| 1901 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1902 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 1903 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1904 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 1905 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1906 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 1907 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1908 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1909 | one = builder.makeInt64Constant(1); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1910 | else |
| 1911 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1912 | glslang::TOperator op; |
| 1913 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1914 | node->getOp() == glslang::EOpPostIncrement) |
| 1915 | op = glslang::EOpAdd; |
| 1916 | else |
| 1917 | op = glslang::EOpSub; |
| 1918 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1919 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1920 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1921 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1922 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1923 | |
| 1924 | // The result of operation is always stored, but conditionally the |
| 1925 | // consumed result. The consumed result is always an r-value. |
| 1926 | builder.accessChainStore(result); |
| 1927 | builder.clearAccessChain(); |
| 1928 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1929 | node->getOp() == glslang::EOpPreDecrement) |
| 1930 | builder.setAccessChainRValue(result); |
| 1931 | else |
| 1932 | builder.setAccessChainRValue(operand); |
| 1933 | } |
| 1934 | |
| 1935 | return false; |
| 1936 | |
| 1937 | case glslang::EOpEmitStreamVertex: |
| 1938 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1939 | return false; |
| 1940 | case glslang::EOpEndStreamPrimitive: |
| 1941 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1942 | return false; |
| 1943 | |
| 1944 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1945 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1946 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1947 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 1951 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 1952 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1953 | if (node->getType().getQualifier().isSpecConstant()) |
| 1954 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1955 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1956 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1957 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 1958 | 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] | 1959 | |
| 1960 | // try texturing |
| 1961 | result = createImageTextureFunctionCall(node); |
| 1962 | if (result != spv::NoResult) { |
| 1963 | builder.clearAccessChain(); |
| 1964 | builder.setAccessChainRValue(result); |
| 1965 | |
| 1966 | return false; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1967 | } else if (node->getOp() == glslang::EOpImageStore || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1968 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1969 | node->getOp() == glslang::EOpImageStoreLod || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1970 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1971 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1972 | // "imageStore" is a special case, which has no result |
| 1973 | return false; |
| 1974 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1975 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1976 | glslang::TOperator binOp = glslang::EOpNull; |
| 1977 | bool reduceComparison = true; |
| 1978 | bool isMatrix = false; |
| 1979 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1980 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1981 | |
| 1982 | assert(node->getOp()); |
| 1983 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1984 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1985 | |
| 1986 | switch (node->getOp()) { |
| 1987 | case glslang::EOpSequence: |
| 1988 | { |
| 1989 | if (preVisit) |
| 1990 | ++sequenceDepth; |
| 1991 | else |
| 1992 | --sequenceDepth; |
| 1993 | |
| 1994 | if (sequenceDepth == 1) { |
| 1995 | // If this is the parent node of all the functions, we want to see them |
| 1996 | // early, so all call points have actual SPIR-V functions to reference. |
| 1997 | // In all cases, still let the traverser visit the children for us. |
| 1998 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 1999 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2000 | // 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] | 2001 | // anything else gets there, so visit out of order, doing them all now. |
| 2002 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2003 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2004 | // 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] | 2005 | // so do them manually. |
| 2006 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2007 | |
| 2008 | return false; |
| 2009 | } |
| 2010 | |
| 2011 | return true; |
| 2012 | } |
| 2013 | case glslang::EOpLinkerObjects: |
| 2014 | { |
| 2015 | if (visit == glslang::EvPreVisit) |
| 2016 | linkageOnly = true; |
| 2017 | else |
| 2018 | linkageOnly = false; |
| 2019 | |
| 2020 | return true; |
| 2021 | } |
| 2022 | case glslang::EOpComma: |
| 2023 | { |
| 2024 | // processing from left to right naturally leaves the right-most |
| 2025 | // lying around in the access chain |
| 2026 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2027 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2028 | glslangOperands[i]->traverse(this); |
| 2029 | |
| 2030 | return false; |
| 2031 | } |
| 2032 | case glslang::EOpFunction: |
| 2033 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2034 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2035 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2036 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2037 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2038 | } else { |
| 2039 | handleFunctionEntry(node); |
| 2040 | } |
| 2041 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2042 | if (inEntryPoint) |
| 2043 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2044 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2045 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | return true; |
| 2049 | case glslang::EOpParameters: |
| 2050 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2051 | // the body, so we still visited the function node's children, making this |
| 2052 | // child redundant. |
| 2053 | return false; |
| 2054 | case glslang::EOpFunctionCall: |
| 2055 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2056 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2057 | if (node->isUserDefined()) |
| 2058 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2059 | // 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] | 2060 | if (result) { |
| 2061 | builder.clearAccessChain(); |
| 2062 | builder.setAccessChainRValue(result); |
| 2063 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2064 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2065 | |
| 2066 | return false; |
| 2067 | } |
| 2068 | case glslang::EOpConstructMat2x2: |
| 2069 | case glslang::EOpConstructMat2x3: |
| 2070 | case glslang::EOpConstructMat2x4: |
| 2071 | case glslang::EOpConstructMat3x2: |
| 2072 | case glslang::EOpConstructMat3x3: |
| 2073 | case glslang::EOpConstructMat3x4: |
| 2074 | case glslang::EOpConstructMat4x2: |
| 2075 | case glslang::EOpConstructMat4x3: |
| 2076 | case glslang::EOpConstructMat4x4: |
| 2077 | case glslang::EOpConstructDMat2x2: |
| 2078 | case glslang::EOpConstructDMat2x3: |
| 2079 | case glslang::EOpConstructDMat2x4: |
| 2080 | case glslang::EOpConstructDMat3x2: |
| 2081 | case glslang::EOpConstructDMat3x3: |
| 2082 | case glslang::EOpConstructDMat3x4: |
| 2083 | case glslang::EOpConstructDMat4x2: |
| 2084 | case glslang::EOpConstructDMat4x3: |
| 2085 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2086 | case glslang::EOpConstructIMat2x2: |
| 2087 | case glslang::EOpConstructIMat2x3: |
| 2088 | case glslang::EOpConstructIMat2x4: |
| 2089 | case glslang::EOpConstructIMat3x2: |
| 2090 | case glslang::EOpConstructIMat3x3: |
| 2091 | case glslang::EOpConstructIMat3x4: |
| 2092 | case glslang::EOpConstructIMat4x2: |
| 2093 | case glslang::EOpConstructIMat4x3: |
| 2094 | case glslang::EOpConstructIMat4x4: |
| 2095 | case glslang::EOpConstructUMat2x2: |
| 2096 | case glslang::EOpConstructUMat2x3: |
| 2097 | case glslang::EOpConstructUMat2x4: |
| 2098 | case glslang::EOpConstructUMat3x2: |
| 2099 | case glslang::EOpConstructUMat3x3: |
| 2100 | case glslang::EOpConstructUMat3x4: |
| 2101 | case glslang::EOpConstructUMat4x2: |
| 2102 | case glslang::EOpConstructUMat4x3: |
| 2103 | case glslang::EOpConstructUMat4x4: |
| 2104 | case glslang::EOpConstructBMat2x2: |
| 2105 | case glslang::EOpConstructBMat2x3: |
| 2106 | case glslang::EOpConstructBMat2x4: |
| 2107 | case glslang::EOpConstructBMat3x2: |
| 2108 | case glslang::EOpConstructBMat3x3: |
| 2109 | case glslang::EOpConstructBMat3x4: |
| 2110 | case glslang::EOpConstructBMat4x2: |
| 2111 | case glslang::EOpConstructBMat4x3: |
| 2112 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2113 | case glslang::EOpConstructF16Mat2x2: |
| 2114 | case glslang::EOpConstructF16Mat2x3: |
| 2115 | case glslang::EOpConstructF16Mat2x4: |
| 2116 | case glslang::EOpConstructF16Mat3x2: |
| 2117 | case glslang::EOpConstructF16Mat3x3: |
| 2118 | case glslang::EOpConstructF16Mat3x4: |
| 2119 | case glslang::EOpConstructF16Mat4x2: |
| 2120 | case glslang::EOpConstructF16Mat4x3: |
| 2121 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2122 | isMatrix = true; |
| 2123 | // fall through |
| 2124 | case glslang::EOpConstructFloat: |
| 2125 | case glslang::EOpConstructVec2: |
| 2126 | case glslang::EOpConstructVec3: |
| 2127 | case glslang::EOpConstructVec4: |
| 2128 | case glslang::EOpConstructDouble: |
| 2129 | case glslang::EOpConstructDVec2: |
| 2130 | case glslang::EOpConstructDVec3: |
| 2131 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2132 | case glslang::EOpConstructFloat16: |
| 2133 | case glslang::EOpConstructF16Vec2: |
| 2134 | case glslang::EOpConstructF16Vec3: |
| 2135 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2136 | case glslang::EOpConstructBool: |
| 2137 | case glslang::EOpConstructBVec2: |
| 2138 | case glslang::EOpConstructBVec3: |
| 2139 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2140 | case glslang::EOpConstructInt8: |
| 2141 | case glslang::EOpConstructI8Vec2: |
| 2142 | case glslang::EOpConstructI8Vec3: |
| 2143 | case glslang::EOpConstructI8Vec4: |
| 2144 | case glslang::EOpConstructUint8: |
| 2145 | case glslang::EOpConstructU8Vec2: |
| 2146 | case glslang::EOpConstructU8Vec3: |
| 2147 | case glslang::EOpConstructU8Vec4: |
| 2148 | case glslang::EOpConstructInt16: |
| 2149 | case glslang::EOpConstructI16Vec2: |
| 2150 | case glslang::EOpConstructI16Vec3: |
| 2151 | case glslang::EOpConstructI16Vec4: |
| 2152 | case glslang::EOpConstructUint16: |
| 2153 | case glslang::EOpConstructU16Vec2: |
| 2154 | case glslang::EOpConstructU16Vec3: |
| 2155 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2156 | case glslang::EOpConstructInt: |
| 2157 | case glslang::EOpConstructIVec2: |
| 2158 | case glslang::EOpConstructIVec3: |
| 2159 | case glslang::EOpConstructIVec4: |
| 2160 | case glslang::EOpConstructUint: |
| 2161 | case glslang::EOpConstructUVec2: |
| 2162 | case glslang::EOpConstructUVec3: |
| 2163 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2164 | case glslang::EOpConstructInt64: |
| 2165 | case glslang::EOpConstructI64Vec2: |
| 2166 | case glslang::EOpConstructI64Vec3: |
| 2167 | case glslang::EOpConstructI64Vec4: |
| 2168 | case glslang::EOpConstructUint64: |
| 2169 | case glslang::EOpConstructU64Vec2: |
| 2170 | case glslang::EOpConstructU64Vec3: |
| 2171 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2172 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2173 | case glslang::EOpConstructTextureSampler: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2174 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2175 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2176 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2177 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2178 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2179 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2180 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2181 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2182 | std::vector<spv::Id> constituents; |
| 2183 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2184 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2185 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2186 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2187 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2188 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2189 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2190 | |
| 2191 | builder.clearAccessChain(); |
| 2192 | builder.setAccessChainRValue(constructed); |
| 2193 | |
| 2194 | return false; |
| 2195 | } |
| 2196 | |
| 2197 | // These six are component-wise compares with component-wise results. |
| 2198 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2199 | case glslang::EOpLessThan: |
| 2200 | case glslang::EOpGreaterThan: |
| 2201 | case glslang::EOpLessThanEqual: |
| 2202 | case glslang::EOpGreaterThanEqual: |
| 2203 | case glslang::EOpVectorEqual: |
| 2204 | case glslang::EOpVectorNotEqual: |
| 2205 | { |
| 2206 | // Map the operation to a binary |
| 2207 | binOp = node->getOp(); |
| 2208 | reduceComparison = false; |
| 2209 | switch (node->getOp()) { |
| 2210 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2211 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2212 | default: binOp = node->getOp(); break; |
| 2213 | } |
| 2214 | |
| 2215 | break; |
| 2216 | } |
| 2217 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2218 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2219 | binOp = glslang::EOpMul; |
| 2220 | break; |
| 2221 | case glslang::EOpOuterProduct: |
| 2222 | // two vectors multiplied to make a matrix |
| 2223 | binOp = glslang::EOpOuterProduct; |
| 2224 | break; |
| 2225 | case glslang::EOpDot: |
| 2226 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2227 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2228 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2229 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2230 | binOp = glslang::EOpMul; |
| 2231 | break; |
| 2232 | } |
| 2233 | case glslang::EOpMod: |
| 2234 | // when an aggregate, this is the floating-point mod built-in function, |
| 2235 | // which can be emitted by the one in createBinaryOperation() |
| 2236 | binOp = glslang::EOpMod; |
| 2237 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2238 | case glslang::EOpEmitVertex: |
| 2239 | case glslang::EOpEndPrimitive: |
| 2240 | case glslang::EOpBarrier: |
| 2241 | case glslang::EOpMemoryBarrier: |
| 2242 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2243 | case glslang::EOpMemoryBarrierBuffer: |
| 2244 | case glslang::EOpMemoryBarrierImage: |
| 2245 | case glslang::EOpMemoryBarrierShared: |
| 2246 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2247 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2248 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2249 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2250 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2251 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2252 | case glslang::EOpSubgroupBarrier: |
| 2253 | case glslang::EOpSubgroupMemoryBarrier: |
| 2254 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2255 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2256 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2257 | noReturnValue = true; |
| 2258 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2259 | break; |
| 2260 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2261 | case glslang::EOpAtomicStore: |
| 2262 | noReturnValue = true; |
| 2263 | // fallthrough |
| 2264 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2265 | case glslang::EOpAtomicAdd: |
| 2266 | case glslang::EOpAtomicMin: |
| 2267 | case glslang::EOpAtomicMax: |
| 2268 | case glslang::EOpAtomicAnd: |
| 2269 | case glslang::EOpAtomicOr: |
| 2270 | case glslang::EOpAtomicXor: |
| 2271 | case glslang::EOpAtomicExchange: |
| 2272 | case glslang::EOpAtomicCompSwap: |
| 2273 | atomic = true; |
| 2274 | break; |
| 2275 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2276 | case glslang::EOpAtomicCounterAdd: |
| 2277 | case glslang::EOpAtomicCounterSubtract: |
| 2278 | case glslang::EOpAtomicCounterMin: |
| 2279 | case glslang::EOpAtomicCounterMax: |
| 2280 | case glslang::EOpAtomicCounterAnd: |
| 2281 | case glslang::EOpAtomicCounterOr: |
| 2282 | case glslang::EOpAtomicCounterXor: |
| 2283 | case glslang::EOpAtomicCounterExchange: |
| 2284 | case glslang::EOpAtomicCounterCompSwap: |
| 2285 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2286 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2287 | atomic = true; |
| 2288 | break; |
| 2289 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2290 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 2291 | case glslang::EOpIgnoreIntersectionNV: |
| 2292 | case glslang::EOpTerminateRayNV: |
| 2293 | case glslang::EOpTraceNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 2294 | case glslang::EOpExecuteCallableNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2295 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2296 | noReturnValue = true; |
| 2297 | break; |
| 2298 | #endif |
| 2299 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2300 | default: |
| 2301 | break; |
| 2302 | } |
| 2303 | |
| 2304 | // |
| 2305 | // See if it maps to a regular operation. |
| 2306 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2307 | if (binOp != glslang::EOpNull) { |
| 2308 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2309 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2310 | assert(left && right); |
| 2311 | |
| 2312 | builder.clearAccessChain(); |
| 2313 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2314 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2315 | |
| 2316 | builder.clearAccessChain(); |
| 2317 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2318 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2319 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2320 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2321 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2322 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2323 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2324 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2325 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2326 | left->getType().getBasicType(), reduceComparison); |
| 2327 | |
| 2328 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2329 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2330 | builder.clearAccessChain(); |
| 2331 | builder.setAccessChainRValue(result); |
| 2332 | |
| 2333 | return false; |
| 2334 | } |
| 2335 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2336 | // |
| 2337 | // Create the list of operands. |
| 2338 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2339 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2340 | std::vector<spv::Id> operands; |
| 2341 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2342 | // special case l-value operands; there are just a few |
| 2343 | bool lvalue = false; |
| 2344 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2345 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2346 | case glslang::EOpModf: |
| 2347 | if (arg == 1) |
| 2348 | lvalue = true; |
| 2349 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2350 | case glslang::EOpInterpolateAtSample: |
| 2351 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2352 | #ifdef AMD_EXTENSIONS |
| 2353 | case glslang::EOpInterpolateAtVertex: |
| 2354 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2355 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2356 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2357 | |
| 2358 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2359 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2360 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2361 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2362 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 2363 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2364 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2365 | case glslang::EOpAtomicAdd: |
| 2366 | case glslang::EOpAtomicMin: |
| 2367 | case glslang::EOpAtomicMax: |
| 2368 | case glslang::EOpAtomicAnd: |
| 2369 | case glslang::EOpAtomicOr: |
| 2370 | case glslang::EOpAtomicXor: |
| 2371 | case glslang::EOpAtomicExchange: |
| 2372 | case glslang::EOpAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2373 | case glslang::EOpAtomicLoad: |
| 2374 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2375 | case glslang::EOpAtomicCounterAdd: |
| 2376 | case glslang::EOpAtomicCounterSubtract: |
| 2377 | case glslang::EOpAtomicCounterMin: |
| 2378 | case glslang::EOpAtomicCounterMax: |
| 2379 | case glslang::EOpAtomicCounterAnd: |
| 2380 | case glslang::EOpAtomicCounterOr: |
| 2381 | case glslang::EOpAtomicCounterXor: |
| 2382 | case glslang::EOpAtomicCounterExchange: |
| 2383 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2384 | if (arg == 0) |
| 2385 | lvalue = true; |
| 2386 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2387 | case glslang::EOpAddCarry: |
| 2388 | case glslang::EOpSubBorrow: |
| 2389 | if (arg == 2) |
| 2390 | lvalue = true; |
| 2391 | break; |
| 2392 | case glslang::EOpUMulExtended: |
| 2393 | case glslang::EOpIMulExtended: |
| 2394 | if (arg >= 2) |
| 2395 | lvalue = true; |
| 2396 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2397 | default: |
| 2398 | break; |
| 2399 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2400 | builder.clearAccessChain(); |
| 2401 | if (invertedType != spv::NoType && arg == 0) |
| 2402 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2403 | else |
| 2404 | glslangOperands[arg]->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2405 | if (lvalue) |
| 2406 | operands.push_back(builder.accessChainGetLValue()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2407 | else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2408 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2409 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2410 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2411 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2412 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2413 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2414 | if (atomic) { |
| 2415 | // Handle all atomics |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2416 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2417 | } else { |
| 2418 | // Pass through to generic operations. |
| 2419 | switch (glslangOperands.size()) { |
| 2420 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2421 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2422 | break; |
| 2423 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2424 | { |
| 2425 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2426 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2427 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2428 | result = createUnaryOperation( |
| 2429 | node->getOp(), decorations, |
| 2430 | resultType(), operands.front(), |
| 2431 | glslangOperands[0]->getAsTyped()->getBasicType()); |
| 2432 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2433 | break; |
| 2434 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2435 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2436 | break; |
| 2437 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2438 | if (invertedType) |
| 2439 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | if (noReturnValue) |
| 2443 | return false; |
| 2444 | |
| 2445 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2446 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2447 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2448 | } else { |
| 2449 | builder.clearAccessChain(); |
| 2450 | builder.setAccessChainRValue(result); |
| 2451 | return false; |
| 2452 | } |
| 2453 | } |
| 2454 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2455 | // This path handles both if-then-else and ?: |
| 2456 | // The if-then-else has a node type of void, while |
| 2457 | // ?: has either a void or a non-void node type |
| 2458 | // |
| 2459 | // Leaving the result, when not void: |
| 2460 | // GLSL only has r-values as the result of a :?, but |
| 2461 | // if we have an l-value, that can be more efficient if it will |
| 2462 | // become the base of a complex r-value expression, because the |
| 2463 | // 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] | 2464 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 2465 | { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2466 | // See if it simple and safe, or required, to execute both sides. |
| 2467 | // Crucially, side effects must be either semantically required or avoided, |
| 2468 | // and there are performance trade-offs. |
| 2469 | // Return true if required or a good idea (and safe) to execute both sides, |
| 2470 | // false otherwise. |
| 2471 | const auto bothSidesPolicy = [&]() -> bool { |
| 2472 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2473 | if (node->getTrueBlock() == nullptr || |
| 2474 | node->getFalseBlock() == nullptr) |
| 2475 | return false; |
| 2476 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2477 | // required? (unless we write additional code to look for side effects |
| 2478 | // and make performance trade-offs if none are present) |
| 2479 | if (!node->getShortCircuit()) |
| 2480 | return true; |
| 2481 | |
| 2482 | // if not required to execute both, decide based on performance/practicality... |
| 2483 | |
| 2484 | // see if OpSelect can handle it |
| 2485 | if ((!node->getType().isScalar() && !node->getType().isVector()) || |
| 2486 | node->getBasicType() == glslang::EbtVoid) |
| 2487 | return false; |
| 2488 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2489 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 2490 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 2491 | |
| 2492 | // return true if a single operand to ? : is okay for OpSelect |
| 2493 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2494 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2495 | }; |
| 2496 | |
| 2497 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 2498 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 2499 | }; |
| 2500 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2501 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 2502 | // emit the condition before doing anything with selection |
| 2503 | node->getCondition()->traverse(this); |
| 2504 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 2505 | |
| 2506 | // Find a way of executing both sides and selecting the right result. |
| 2507 | const auto executeBothSides = [&]() -> void { |
| 2508 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2509 | node->getTrueBlock()->traverse(this); |
| 2510 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2511 | node->getFalseBlock()->traverse(this); |
| 2512 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2513 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2514 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2515 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2516 | // done if void |
| 2517 | if (node->getBasicType() == glslang::EbtVoid) |
| 2518 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 2519 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2520 | // emit code to select between trueValue and falseValue |
| 2521 | |
| 2522 | // see if OpSelect can handle it |
| 2523 | if (node->getType().isScalar() || node->getType().isVector()) { |
| 2524 | // Emit OpSelect for this selection. |
| 2525 | |
| 2526 | // smear condition to vector, if necessary (AST is always scalar) |
| 2527 | if (builder.isVector(trueValue)) |
| 2528 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 2529 | builder.makeVectorType(builder.makeBoolType(), |
| 2530 | builder.getNumComponents(trueValue))); |
| 2531 | |
| 2532 | // OpSelect |
| 2533 | result = builder.createTriOp(spv::OpSelect, |
| 2534 | convertGlslangToSpvType(node->getType()), condition, |
| 2535 | trueValue, falseValue); |
| 2536 | |
| 2537 | builder.clearAccessChain(); |
| 2538 | builder.setAccessChainRValue(result); |
| 2539 | } else { |
| 2540 | // We need control flow to select the result. |
| 2541 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 2542 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2543 | |
| 2544 | // Selection control: |
| 2545 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2546 | |
| 2547 | // make an "if" based on the value created by the condition |
| 2548 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2549 | |
| 2550 | // emit the "then" statement |
| 2551 | builder.createStore(trueValue, result); |
| 2552 | ifBuilder.makeBeginElse(); |
| 2553 | // emit the "else" statement |
| 2554 | builder.createStore(falseValue, result); |
| 2555 | |
| 2556 | // finish off the control flow |
| 2557 | ifBuilder.makeEndIf(); |
| 2558 | |
| 2559 | builder.clearAccessChain(); |
| 2560 | builder.setAccessChainLValue(result); |
| 2561 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2562 | }; |
| 2563 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2564 | // Execute the one side needed, as per the condition |
| 2565 | const auto executeOneSide = [&]() { |
| 2566 | // Always emit control flow. |
| 2567 | if (node->getBasicType() != glslang::EbtVoid) |
| 2568 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2569 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2570 | // Selection control: |
| 2571 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2572 | |
| 2573 | // make an "if" based on the value created by the condition |
| 2574 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2575 | |
| 2576 | // emit the "then" statement |
| 2577 | if (node->getTrueBlock() != nullptr) { |
| 2578 | node->getTrueBlock()->traverse(this); |
| 2579 | if (result != spv::NoResult) |
| 2580 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 2581 | } |
| 2582 | |
| 2583 | if (node->getFalseBlock() != nullptr) { |
| 2584 | ifBuilder.makeBeginElse(); |
| 2585 | // emit the "else" statement |
| 2586 | node->getFalseBlock()->traverse(this); |
| 2587 | if (result != spv::NoResult) |
| 2588 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 2589 | } |
| 2590 | |
| 2591 | // finish off the control flow |
| 2592 | ifBuilder.makeEndIf(); |
| 2593 | |
| 2594 | if (result != spv::NoResult) { |
| 2595 | builder.clearAccessChain(); |
| 2596 | builder.setAccessChainLValue(result); |
| 2597 | } |
| 2598 | }; |
| 2599 | |
| 2600 | // Try for OpSelect (or a requirement to execute both sides) |
| 2601 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2602 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2603 | if (node->getType().getQualifier().isSpecConstant()) |
| 2604 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2605 | executeBothSides(); |
| 2606 | } else |
| 2607 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2608 | |
| 2609 | return false; |
| 2610 | } |
| 2611 | |
| 2612 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2613 | { |
| 2614 | // emit and get the condition before doing anything with switch |
| 2615 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2616 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2617 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2618 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 2619 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2620 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2621 | // browse the children to sort out code segments |
| 2622 | int defaultSegment = -1; |
| 2623 | std::vector<TIntermNode*> codeSegments; |
| 2624 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 2625 | std::vector<int> caseValues; |
| 2626 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 2627 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 2628 | TIntermNode* child = *c; |
| 2629 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2630 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2631 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2632 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2633 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2634 | } else |
| 2635 | codeSegments.push_back(child); |
| 2636 | } |
| 2637 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2638 | // 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] | 2639 | // statements between the last case and the end of the switch statement |
| 2640 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 2641 | (int)codeSegments.size() == defaultSegment) |
| 2642 | codeSegments.push_back(nullptr); |
| 2643 | |
| 2644 | // make the switch statement |
| 2645 | 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] | 2646 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2647 | |
| 2648 | // emit all the code in the segments |
| 2649 | breakForLoop.push(false); |
| 2650 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 2651 | builder.nextSwitchSegment(segmentBlocks, s); |
| 2652 | if (codeSegments[s]) |
| 2653 | codeSegments[s]->traverse(this); |
| 2654 | else |
| 2655 | builder.addSwitchBreak(); |
| 2656 | } |
| 2657 | breakForLoop.pop(); |
| 2658 | |
| 2659 | builder.endSwitch(segmentBlocks); |
| 2660 | |
| 2661 | return false; |
| 2662 | } |
| 2663 | |
| 2664 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 2665 | { |
| 2666 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2667 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2668 | |
| 2669 | builder.clearAccessChain(); |
| 2670 | builder.setAccessChainRValue(constant); |
| 2671 | } |
| 2672 | |
| 2673 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 2674 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2675 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2676 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2677 | |
| 2678 | // Loop control: |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2679 | unsigned int dependencyLength = glslang::TIntermLoop::dependencyInfinite; |
| 2680 | const spv::LoopControlMask control = TranslateLoopControl(*node, dependencyLength); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2681 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2682 | // Spec requires back edges to target header blocks, and every header block |
| 2683 | // must dominate its merge block. Make a header block first to ensure these |
| 2684 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 2685 | // by a block-ending branch. But we don't want to put any other body/test |
| 2686 | // instructions in it, since the body/test may have arbitrary instructions, |
| 2687 | // including merges of its own. |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2688 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2689 | builder.setBuildPoint(&blocks.head); |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2690 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, dependencyLength); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2691 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2692 | spv::Block& test = builder.makeNewBlock(); |
| 2693 | builder.createBranch(&test); |
| 2694 | |
| 2695 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2696 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2697 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2698 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 2699 | |
| 2700 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2701 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2702 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2703 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2704 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2705 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2706 | |
| 2707 | builder.setBuildPoint(&blocks.continue_target); |
| 2708 | if (node->getTerminal()) |
| 2709 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2710 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 2711 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2712 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2713 | builder.createBranch(&blocks.body); |
| 2714 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2715 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2716 | builder.setBuildPoint(&blocks.body); |
| 2717 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2718 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2719 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2720 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2721 | |
| 2722 | builder.setBuildPoint(&blocks.continue_target); |
| 2723 | if (node->getTerminal()) |
| 2724 | node->getTerminal()->traverse(this); |
| 2725 | if (node->getTest()) { |
| 2726 | node->getTest()->traverse(this); |
| 2727 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2728 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2729 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2730 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 2731 | // TODO: unless there was a break/return/discard instruction |
| 2732 | // somewhere in the body, this is an infinite loop, so we should |
| 2733 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2734 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2735 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2736 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2737 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2738 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2739 | return false; |
| 2740 | } |
| 2741 | |
| 2742 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 2743 | { |
| 2744 | if (node->getExpression()) |
| 2745 | node->getExpression()->traverse(this); |
| 2746 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2747 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2748 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2749 | switch (node->getFlowOp()) { |
| 2750 | case glslang::EOpKill: |
| 2751 | builder.makeDiscard(); |
| 2752 | break; |
| 2753 | case glslang::EOpBreak: |
| 2754 | if (breakForLoop.top()) |
| 2755 | builder.createLoopExit(); |
| 2756 | else |
| 2757 | builder.addSwitchBreak(); |
| 2758 | break; |
| 2759 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2760 | builder.createLoopContinue(); |
| 2761 | break; |
| 2762 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2763 | if (node->getExpression()) { |
| 2764 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 2765 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 2766 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 2767 | builder.clearAccessChain(); |
| 2768 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 2769 | builder.setAccessChainLValue(copyId); |
| 2770 | multiTypeStore(glslangReturnType, returnId); |
| 2771 | returnId = builder.createLoad(copyId); |
| 2772 | } |
| 2773 | builder.makeReturn(false, returnId); |
| 2774 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2775 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2776 | |
| 2777 | builder.clearAccessChain(); |
| 2778 | break; |
| 2779 | |
| 2780 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2781 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2782 | break; |
| 2783 | } |
| 2784 | |
| 2785 | return false; |
| 2786 | } |
| 2787 | |
| 2788 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 2789 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2790 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2791 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2792 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 2793 | if (node->getQualifier().isConstant()) { |
Dan Sinclair | 12fcaa2 | 2018-11-13 09:17:44 -0500 | [diff] [blame] | 2794 | spv::Id result = createSpvConstant(*node); |
| 2795 | if (result != spv::NoResult) |
| 2796 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2800 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2801 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 2802 | |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2803 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 2804 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 2805 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2806 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2807 | switch (storageClass) { |
| 2808 | case spv::StorageClassInput: |
| 2809 | case spv::StorageClassOutput: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2810 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2811 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2812 | break; |
| 2813 | case spv::StorageClassPushConstant: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2814 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2815 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2816 | break; |
| 2817 | case spv::StorageClassUniform: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2818 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2819 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2820 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2821 | else |
| 2822 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 2823 | break; |
| 2824 | case spv::StorageClassStorageBuffer: |
| 2825 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
| 2826 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 2827 | break; |
| 2828 | default: |
| 2829 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2830 | } |
| 2831 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2832 | |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 2833 | const bool contains8BitType = node->getType().containsBasicType(glslang::EbtInt8) || |
| 2834 | node->getType().containsBasicType(glslang::EbtUint8); |
| 2835 | if (contains8BitType) { |
| 2836 | if (storageClass == spv::StorageClassPushConstant) { |
| 2837 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2838 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 2839 | } else if (storageClass == spv::StorageClassUniform) { |
| 2840 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2841 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 2842 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
| 2843 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2844 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2848 | const char* name = node->getName().c_str(); |
| 2849 | if (glslang::IsAnonymous(name)) |
| 2850 | name = ""; |
| 2851 | |
| 2852 | return builder.createVariable(storageClass, spvType, name); |
| 2853 | } |
| 2854 | |
| 2855 | // Return type Id of the sampled type. |
| 2856 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 2857 | { |
| 2858 | switch (sampler.type) { |
| 2859 | case glslang::EbtFloat: return builder.makeFloatType(32); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 2860 | #ifdef AMD_EXTENSIONS |
| 2861 | case glslang::EbtFloat16: |
| 2862 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 2863 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 2864 | return builder.makeFloatType(16); |
| 2865 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2866 | case glslang::EbtInt: return builder.makeIntType(32); |
| 2867 | case glslang::EbtUint: return builder.makeUintType(32); |
| 2868 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2869 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2870 | return builder.makeFloatType(32); |
| 2871 | } |
| 2872 | } |
| 2873 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2874 | // If node is a swizzle operation, return the type that should be used if |
| 2875 | // the swizzle base is first consumed by another operation, before the swizzle |
| 2876 | // is applied. |
| 2877 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 2878 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2879 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2880 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2881 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 2882 | else |
| 2883 | return spv::NoType; |
| 2884 | } |
| 2885 | |
| 2886 | // When inverting a swizzle with a parent op, this function |
| 2887 | // will apply the swizzle operation to a completed parent operation. |
| 2888 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 2889 | { |
| 2890 | std::vector<unsigned> swizzle; |
| 2891 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 2892 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 2893 | } |
| 2894 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2895 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 2896 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 2897 | { |
| 2898 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 2899 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 2900 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2901 | } |
| 2902 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2903 | // Convert from a glslang type to an SPV type, by calling into a |
| 2904 | // recursive version of this function. This establishes the inherited |
| 2905 | // layout state rooted from the top-level type. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2906 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type) |
| 2907 | { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2908 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | // 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] | 2912 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2913 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2914 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
| 2915 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, bool lastBufferBlockMember) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2916 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2917 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2918 | |
| 2919 | switch (type.getBasicType()) { |
| 2920 | case glslang::EbtVoid: |
| 2921 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2922 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2923 | break; |
| 2924 | case glslang::EbtFloat: |
| 2925 | spvType = builder.makeFloatType(32); |
| 2926 | break; |
| 2927 | case glslang::EbtDouble: |
| 2928 | spvType = builder.makeFloatType(64); |
| 2929 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2930 | case glslang::EbtFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2931 | spvType = builder.makeFloatType(16); |
| 2932 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2933 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2934 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 2935 | // a 32-bit int where non-0 means true. |
| 2936 | if (explicitLayout != glslang::ElpNone) |
| 2937 | spvType = builder.makeUintType(32); |
| 2938 | else |
| 2939 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2940 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 2941 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2942 | spvType = builder.makeIntType(8); |
| 2943 | break; |
| 2944 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2945 | spvType = builder.makeUintType(8); |
| 2946 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 2947 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2948 | spvType = builder.makeIntType(16); |
| 2949 | break; |
| 2950 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2951 | spvType = builder.makeUintType(16); |
| 2952 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2953 | case glslang::EbtInt: |
| 2954 | spvType = builder.makeIntType(32); |
| 2955 | break; |
| 2956 | case glslang::EbtUint: |
| 2957 | spvType = builder.makeUintType(32); |
| 2958 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2959 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2960 | spvType = builder.makeIntType(64); |
| 2961 | break; |
| 2962 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2963 | spvType = builder.makeUintType(64); |
| 2964 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2965 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 2966 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2967 | spvType = builder.makeUintType(32); |
| 2968 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 2969 | #ifdef NV_EXTENSIONS |
| 2970 | case glslang::EbtAccStructNV: |
| 2971 | spvType = builder.makeAccelerationStructureNVType(); |
| 2972 | break; |
| 2973 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2974 | case glslang::EbtSampler: |
| 2975 | { |
| 2976 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2977 | if (sampler.sampler) { |
| 2978 | // pure sampler |
| 2979 | spvType = builder.makeSamplerType(); |
| 2980 | } else { |
| 2981 | // an image is present, make its type |
| 2982 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 2983 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 2984 | if (sampler.combined) { |
| 2985 | // already has both image and sampler, make the combined type |
| 2986 | spvType = builder.makeSampledImageType(spvType); |
| 2987 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2988 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2989 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2990 | break; |
| 2991 | case glslang::EbtStruct: |
| 2992 | case glslang::EbtBlock: |
| 2993 | { |
| 2994 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2995 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2996 | |
| 2997 | // Try to share structs for different layouts, but not yet for other |
| 2998 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2999 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3000 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3001 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3002 | break; |
| 3003 | |
| 3004 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3005 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3006 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 3007 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3008 | } |
| 3009 | break; |
| 3010 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3011 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3012 | break; |
| 3013 | } |
| 3014 | |
| 3015 | if (type.isMatrix()) |
| 3016 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3017 | else { |
| 3018 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3019 | if (type.getVectorSize() > 1) |
| 3020 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3021 | } |
| 3022 | |
| 3023 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3024 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3025 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3026 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3027 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3028 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3029 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3030 | // Use a dummy glslang type for querying internal strides of |
| 3031 | // arrays of arrays, but using just a one-dimensional array. |
| 3032 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3033 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3034 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3035 | |
| 3036 | // Will compute the higher-order strides here, rather than making a whole |
| 3037 | // pile of types and doing repetitive recursion on their contents. |
| 3038 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3039 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3040 | |
| 3041 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3042 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3043 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3044 | if (stride > 0) |
| 3045 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3046 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3047 | } |
| 3048 | } else { |
| 3049 | // single-dimensional array, and don't yet have stride |
| 3050 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3051 | // 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] | 3052 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3053 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3054 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3055 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3056 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3057 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3058 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3059 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3060 | else { |
| 3061 | if (!lastBufferBlockMember) { |
| 3062 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 3063 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3064 | } |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3065 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3066 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3067 | if (stride > 0) |
| 3068 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | return spvType; |
| 3072 | } |
| 3073 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3074 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3075 | // |
| 3076 | // Identify interface members that don't have their required extension turned on. |
| 3077 | // |
| 3078 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3079 | { |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3080 | #ifdef NV_EXTENSIONS |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3081 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3082 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3083 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3084 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3085 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3086 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3087 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3088 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3089 | |
| 3090 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3091 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3092 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3093 | return true; |
| 3094 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3095 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3096 | return true; |
| 3097 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3098 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3099 | return true; |
| 3100 | } |
| 3101 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3102 | |
| 3103 | return false; |
| 3104 | }; |
| 3105 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3106 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3107 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3108 | // Mutually recursive with convertGlslangToSpvType(). |
| 3109 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3110 | const glslang::TTypeList* glslangMembers, |
| 3111 | glslang::TLayoutPacking explicitLayout, |
| 3112 | const glslang::TQualifier& qualifier) |
| 3113 | { |
| 3114 | // Create a vector of struct types for SPIR-V to consume |
| 3115 | std::vector<spv::Id> spvMembers; |
| 3116 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3117 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3118 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3119 | if (glslangMember.hiddenMember()) { |
| 3120 | ++memberDelta; |
| 3121 | if (type.getBasicType() == glslang::EbtBlock) |
| 3122 | memberRemapper[glslangMembers][i] = -1; |
| 3123 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3124 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3125 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3126 | if (filterMember(glslangMember)) |
| 3127 | continue; |
| 3128 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3129 | // modify just this child's view of the qualifier |
| 3130 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3131 | InheritQualifiers(memberQualifier, qualifier); |
| 3132 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3133 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3134 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3135 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3136 | |
| 3137 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3138 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3139 | i == (int)glslangMembers->size() - 1; |
| 3140 | spvMembers.push_back( |
| 3141 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | // Make the SPIR-V type |
| 3146 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3147 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3148 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3149 | |
| 3150 | // Decorate it |
| 3151 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3152 | |
| 3153 | return spvType; |
| 3154 | } |
| 3155 | |
| 3156 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3157 | const glslang::TTypeList* glslangMembers, |
| 3158 | glslang::TLayoutPacking explicitLayout, |
| 3159 | const glslang::TQualifier& qualifier, |
| 3160 | spv::Id spvType) |
| 3161 | { |
| 3162 | // Name and decorate the non-hidden members |
| 3163 | int offset = -1; |
| 3164 | int locationOffset = 0; // for use within the members of this struct |
| 3165 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3166 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3167 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3168 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3169 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3170 | if (filterMember(glslangMember)) |
| 3171 | continue; |
| 3172 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3173 | |
| 3174 | // modify just this child's view of the qualifier |
| 3175 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3176 | InheritQualifiers(memberQualifier, qualifier); |
| 3177 | |
| 3178 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3179 | if (member < 0) |
| 3180 | continue; |
| 3181 | |
| 3182 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3183 | builder.addMemberDecoration(spvType, member, |
| 3184 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 3185 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 3186 | // Add interpolation and auxiliary storage decorations only to |
| 3187 | // top-level members of Input and Output storage classes |
| 3188 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 3189 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 3190 | if (type.getBasicType() == glslang::EbtBlock || |
| 3191 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 3192 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 3193 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3194 | #ifdef NV_EXTENSIONS |
| 3195 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 3196 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3197 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3198 | } |
| 3199 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3200 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3201 | if (type.getBasicType() == glslang::EbtBlock && |
| 3202 | qualifier.storage == glslang::EvqBuffer) { |
| 3203 | // Add memory decorations only to top-level members of shader storage block |
| 3204 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3205 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3206 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3207 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 3208 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3209 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3210 | // Location assignment was already completed correctly by the front end, |
| 3211 | // just track whether a member needs to be decorated. |
| 3212 | // Ignore member locations if the container is an array, as that's |
| 3213 | // ill-specified and decisions have been made to not allow this. |
| 3214 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 3215 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3216 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3217 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 3218 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 3219 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 3220 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3221 | // component, XFB, others |
| 3222 | if (glslangMember.getQualifier().hasComponent()) |
| 3223 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 3224 | glslangMember.getQualifier().layoutComponent); |
| 3225 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 3226 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 3227 | glslangMember.getQualifier().layoutXfbOffset); |
| 3228 | else if (explicitLayout != glslang::ElpNone) { |
| 3229 | // figure out what to do with offset, which is accumulating |
| 3230 | int nextOffset; |
| 3231 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 3232 | if (offset >= 0) |
| 3233 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 3234 | offset = nextOffset; |
| 3235 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3236 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3237 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 3238 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 3239 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3240 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3241 | // built-in variable decorations |
| 3242 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 3243 | if (builtIn != spv::BuiltInMax) |
| 3244 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3245 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3246 | // nonuniform |
| 3247 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 3248 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3249 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 3250 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 3251 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 3252 | memberQualifier.semanticName); |
| 3253 | } |
| 3254 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3255 | #ifdef NV_EXTENSIONS |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3256 | if (builtIn == spv::BuiltInLayer) { |
| 3257 | // SPV_NV_viewport_array2 extension |
| 3258 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 3259 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 3260 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 3261 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3262 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3263 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 3264 | builder.addMemberDecoration(spvType, member, |
| 3265 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 3266 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 3267 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 3268 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3269 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3270 | } |
| 3271 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 3272 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 3273 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 3274 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 3275 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3276 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3280 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 3281 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3282 | } |
| 3283 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3284 | // Turn the expression forming the array size into an id. |
| 3285 | // This is not quite trivial, because of specialization constants. |
| 3286 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 3287 | // a specialization constant expression is. |
| 3288 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 3289 | { |
| 3290 | // First, see if this is sized with a node, meaning a specialization constant: |
| 3291 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 3292 | if (specNode != nullptr) { |
| 3293 | builder.clearAccessChain(); |
| 3294 | specNode->traverse(this); |
| 3295 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 3296 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3297 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3298 | // Otherwise, need a compile-time (front end) size, get it: |
| 3299 | int size = arraySizes.getDimSize(dim); |
| 3300 | assert(size > 0); |
| 3301 | return builder.makeUintConstant(size); |
| 3302 | } |
| 3303 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3304 | // Wrap the builder's accessChainLoad to: |
| 3305 | // - localize handling of RelaxedPrecision |
| 3306 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 3307 | // (avoids unnecessary work and possible type punning for structures) |
| 3308 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3309 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 3310 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3311 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3312 | |
| 3313 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3314 | coherentFlags |= TranslateCoherent(type); |
| 3315 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3316 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3317 | TranslateNonUniformDecoration(type.getQualifier()), |
| 3318 | nominalTypeId, |
| 3319 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
| 3320 | TranslateMemoryScope(coherentFlags)); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3321 | |
| 3322 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3323 | if (type.getBasicType() == glslang::EbtBool) { |
| 3324 | if (builder.isScalarType(nominalTypeId)) { |
| 3325 | // Conversion for bool |
| 3326 | spv::Id boolType = builder.makeBoolType(); |
| 3327 | if (nominalTypeId != boolType) |
| 3328 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 3329 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3330 | // Conversion for bvec |
| 3331 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3332 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 3333 | if (nominalTypeId != bvecType) |
| 3334 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 3335 | } |
| 3336 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3337 | |
| 3338 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3339 | } |
| 3340 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3341 | // Wrap the builder's accessChainStore to: |
| 3342 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3343 | // |
| 3344 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3345 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 3346 | { |
| 3347 | // Need to convert to abstract types when necessary |
| 3348 | if (type.getBasicType() == glslang::EbtBool) { |
| 3349 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 3350 | |
| 3351 | if (builder.isScalarType(nominalTypeId)) { |
| 3352 | // Conversion for bool |
| 3353 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3354 | if (nominalTypeId != boolType) { |
| 3355 | // keep these outside arguments, for determinant order-of-evaluation |
| 3356 | spv::Id one = builder.makeUintConstant(1); |
| 3357 | spv::Id zero = builder.makeUintConstant(0); |
| 3358 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 3359 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3360 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3361 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3362 | // Conversion for bvec |
| 3363 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3364 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3365 | if (nominalTypeId != bvecType) { |
| 3366 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 3367 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 3368 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 3369 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3370 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3371 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 3372 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3373 | } |
| 3374 | } |
| 3375 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3376 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3377 | coherentFlags |= TranslateCoherent(type); |
| 3378 | |
| 3379 | builder.accessChainStore(rvalue, |
| 3380 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask), |
| 3381 | TranslateMemoryScope(coherentFlags)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3382 | } |
| 3383 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3384 | // For storing when types match at the glslang level, but not might match at the |
| 3385 | // SPIR-V level. |
| 3386 | // |
| 3387 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3388 | // 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] | 3389 | // as in a member-decorated way. |
| 3390 | // |
| 3391 | // NOTE: This function can handle any store request; if it's not special it |
| 3392 | // simplifies to a simple OpStore. |
| 3393 | // |
| 3394 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 3395 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 3396 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3397 | // we only do the complex path here if it's an aggregate |
| 3398 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3399 | accessChainStore(type, rValue); |
| 3400 | return; |
| 3401 | } |
| 3402 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3403 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3404 | spv::Id rType = builder.getTypeId(rValue); |
| 3405 | spv::Id lValue = builder.accessChainGetLValue(); |
| 3406 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 3407 | if (lType == rType) { |
| 3408 | accessChainStore(type, rValue); |
| 3409 | return; |
| 3410 | } |
| 3411 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3412 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3413 | // where the two types were the same type in GLSL. This requires member |
| 3414 | // by member copy, recursively. |
| 3415 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3416 | // If an array, copy element by element. |
| 3417 | if (type.isArray()) { |
| 3418 | glslang::TType glslangElementType(type, 0); |
| 3419 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 3420 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 3421 | // get the source member |
| 3422 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3423 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3424 | // set up the target storage |
| 3425 | builder.clearAccessChain(); |
| 3426 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3427 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type)); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3428 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3429 | // store the member |
| 3430 | multiTypeStore(glslangElementType, elementRValue); |
| 3431 | } |
| 3432 | } else { |
| 3433 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3434 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3435 | // loop over structure members |
| 3436 | const glslang::TTypeList& members = *type.getStruct(); |
| 3437 | for (int m = 0; m < (int)members.size(); ++m) { |
| 3438 | const glslang::TType& glslangMemberType = *members[m].type; |
| 3439 | |
| 3440 | // get the source member |
| 3441 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 3442 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 3443 | |
| 3444 | // set up the target storage |
| 3445 | builder.clearAccessChain(); |
| 3446 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3447 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type)); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3448 | |
| 3449 | // store the member |
| 3450 | multiTypeStore(glslangMemberType, memberRValue); |
| 3451 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3452 | } |
| 3453 | } |
| 3454 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3455 | // Decide whether or not this type should be |
| 3456 | // decorated with offsets and strides, and if so |
| 3457 | // whether std140 or std430 rules should be applied. |
| 3458 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3459 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3460 | // has to be a block |
| 3461 | if (type.getBasicType() != glslang::EbtBlock) |
| 3462 | return glslang::ElpNone; |
| 3463 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3464 | // 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] | 3465 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3466 | type.getQualifier().storage != glslang::EvqBuffer && |
| 3467 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3468 | return glslang::ElpNone; |
| 3469 | |
| 3470 | // return the layout to use |
| 3471 | switch (type.getQualifier().layoutPacking) { |
| 3472 | case glslang::ElpStd140: |
| 3473 | case glslang::ElpStd430: |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3474 | case glslang::ElpScalar: |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3475 | return type.getQualifier().layoutPacking; |
| 3476 | default: |
| 3477 | return glslang::ElpNone; |
| 3478 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3479 | } |
| 3480 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3481 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3482 | 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] | 3483 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3484 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3485 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3486 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 3487 | |
| 3488 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3489 | } |
| 3490 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3491 | // 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] | 3492 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3493 | 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] | 3494 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3495 | glslang::TType elementType; |
| 3496 | elementType.shallowCopy(matrixType); |
| 3497 | elementType.clearArraySizes(); |
| 3498 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3499 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3500 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3501 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3502 | |
| 3503 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3504 | } |
| 3505 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3506 | // Given a member type of a struct, realign the current offset for it, and compute |
| 3507 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 3508 | // on the next call. |
| 3509 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 3510 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 3511 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3512 | 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] | 3513 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3514 | { |
| 3515 | // this will get a positive value when deemed necessary |
| 3516 | nextOffset = -1; |
| 3517 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3518 | // override anything in currentOffset with user-set offset |
| 3519 | if (memberType.getQualifier().hasOffset()) |
| 3520 | currentOffset = memberType.getQualifier().layoutOffset; |
| 3521 | |
| 3522 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 3523 | // in which case the following code does not matter. But, that's not quite right |
| 3524 | // once cross-compilation unit GLSL validation is done, as the original user |
| 3525 | // settings are needed in layoutOffset, and then the following will come into play. |
| 3526 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3527 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3528 | if (! memberType.getQualifier().hasOffset()) |
| 3529 | currentOffset = -1; |
| 3530 | |
| 3531 | return; |
| 3532 | } |
| 3533 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3534 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3535 | if (currentOffset < 0) |
| 3536 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3537 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3538 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 3539 | // but possibly not yet correctly aligned. |
| 3540 | |
| 3541 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3542 | int dummyStride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3543 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3544 | |
| 3545 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3546 | // TODO: make this consistent in early phases of code: |
| 3547 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 3548 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 3549 | // 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] | 3550 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3551 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3552 | int dummySize; |
| 3553 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 3554 | if (componentAlignment <= 4) |
| 3555 | memberAlignment = componentAlignment; |
| 3556 | } |
| 3557 | |
| 3558 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3559 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3560 | |
| 3561 | // Bump up to vec4 if there is a bad straddle |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3562 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3563 | glslang::RoundToPow2(currentOffset, 16); |
| 3564 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3565 | nextOffset = currentOffset + memberSize; |
| 3566 | } |
| 3567 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3568 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3569 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3570 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 3571 | switch (glslangBuiltIn) |
| 3572 | { |
| 3573 | case glslang::EbvClipDistance: |
| 3574 | case glslang::EbvCullDistance: |
| 3575 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3576 | #ifdef NV_EXTENSIONS |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3577 | case glslang::EbvViewportMaskNV: |
| 3578 | case glslang::EbvSecondaryPositionNV: |
| 3579 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3580 | case glslang::EbvPositionPerViewNV: |
| 3581 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3582 | case glslang::EbvTaskCountNV: |
| 3583 | case glslang::EbvPrimitiveCountNV: |
| 3584 | case glslang::EbvPrimitiveIndicesNV: |
| 3585 | case glslang::EbvClipDistancePerViewNV: |
| 3586 | case glslang::EbvCullDistancePerViewNV: |
| 3587 | case glslang::EbvLayerPerViewNV: |
| 3588 | case glslang::EbvMeshViewCountNV: |
| 3589 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3590 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3591 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 3592 | // Alternately, we could just call this for any glslang built-in, since the |
| 3593 | // capability already guards against duplicates. |
| 3594 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 3595 | break; |
| 3596 | default: |
| 3597 | // Capabilities were already generated when the struct was declared. |
| 3598 | break; |
| 3599 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3600 | } |
| 3601 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3602 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3603 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 3604 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3605 | } |
| 3606 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3607 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3608 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 3609 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 3610 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3611 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3612 | assert(qualifier == glslang::EvqIn || |
| 3613 | qualifier == glslang::EvqOut || |
| 3614 | qualifier == glslang::EvqInOut || |
| 3615 | qualifier == glslang::EvqConstReadOnly); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3616 | return qualifier != glslang::EvqConstReadOnly; |
| 3617 | } |
| 3618 | |
| 3619 | // Is parameter pass-by-original? |
| 3620 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 3621 | bool implicitThisParam) |
| 3622 | { |
| 3623 | if (implicitThisParam) // implicit this |
| 3624 | return true; |
| 3625 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3626 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3627 | return paramType.containsOpaque() || // sampler, etc. |
| 3628 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 3629 | } |
| 3630 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3631 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 3632 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3633 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3634 | 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] | 3635 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 3636 | if (paramPrecision != spv::NoPrecision) |
| 3637 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3638 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3639 | }; |
| 3640 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3641 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3642 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3643 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3644 | continue; |
| 3645 | |
| 3646 | // 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] | 3647 | // 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] | 3648 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3649 | // 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] | 3650 | // function. What it is an address of varies: |
| 3651 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3652 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 3653 | // 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] | 3654 | // |
| 3655 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 3656 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3657 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 3658 | // 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] | 3659 | |
| 3660 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3661 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3662 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 3663 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3664 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 3665 | glslangIntermediate->implicitThisName; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3666 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3667 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3668 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3669 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 3670 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3671 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3672 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3673 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3674 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 3675 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3676 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3677 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3678 | paramTypes.push_back(typeId); |
| 3679 | } |
| 3680 | |
| 3681 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3682 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 3683 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3684 | glslFunction->getName().c_str(), paramTypes, |
| 3685 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3686 | if (implicitThis) |
| 3687 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3688 | |
| 3689 | // Track function to emit/call later |
| 3690 | functionMap[glslFunction->getName().c_str()] = function; |
| 3691 | |
| 3692 | // Set the parameter id's |
| 3693 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3694 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 3695 | // give a name too |
| 3696 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 3697 | } |
| 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | // Process all the initializers, while skipping the functions and link objects |
| 3702 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 3703 | { |
| 3704 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 3705 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 3706 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 3707 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 3708 | |
| 3709 | // 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] | 3710 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3711 | initializer->traverse(this); |
| 3712 | } |
| 3713 | } |
| 3714 | } |
| 3715 | |
| 3716 | // Process all the functions, while skipping initializers. |
| 3717 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3718 | { |
| 3719 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3720 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 3721 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3722 | node->traverse(this); |
| 3723 | } |
| 3724 | } |
| 3725 | |
| 3726 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 3727 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3728 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3729 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3730 | currentFunction = functionMap[node->getName().c_str()]; |
| 3731 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3732 | builder.setBuildPoint(functionBlock); |
| 3733 | } |
| 3734 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3735 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3736 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3737 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3738 | |
| 3739 | glslang::TSampler sampler = {}; |
| 3740 | bool cubeCompare = false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3741 | #ifdef AMD_EXTENSIONS |
| 3742 | bool f16ShadowCompare = false; |
| 3743 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3744 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3745 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 3746 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3747 | #ifdef AMD_EXTENSIONS |
| 3748 | f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 3749 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3750 | } |
| 3751 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3752 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 3753 | builder.clearAccessChain(); |
| 3754 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3755 | |
| 3756 | // Special case l-value operands |
| 3757 | bool lvalue = false; |
| 3758 | switch (node.getOp()) { |
| 3759 | case glslang::EOpImageAtomicAdd: |
| 3760 | case glslang::EOpImageAtomicMin: |
| 3761 | case glslang::EOpImageAtomicMax: |
| 3762 | case glslang::EOpImageAtomicAnd: |
| 3763 | case glslang::EOpImageAtomicOr: |
| 3764 | case glslang::EOpImageAtomicXor: |
| 3765 | case glslang::EOpImageAtomicExchange: |
| 3766 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3767 | case glslang::EOpImageAtomicLoad: |
| 3768 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3769 | if (i == 0) |
| 3770 | lvalue = true; |
| 3771 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3772 | case glslang::EOpSparseImageLoad: |
| 3773 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 3774 | lvalue = true; |
| 3775 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3776 | #ifdef AMD_EXTENSIONS |
| 3777 | case glslang::EOpSparseTexture: |
| 3778 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 3779 | lvalue = true; |
| 3780 | break; |
| 3781 | case glslang::EOpSparseTextureClamp: |
| 3782 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 3783 | lvalue = true; |
| 3784 | break; |
| 3785 | case glslang::EOpSparseTextureLod: |
| 3786 | case glslang::EOpSparseTextureOffset: |
| 3787 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 3788 | lvalue = true; |
| 3789 | break; |
| 3790 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3791 | case glslang::EOpSparseTexture: |
| 3792 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 3793 | lvalue = true; |
| 3794 | break; |
| 3795 | case glslang::EOpSparseTextureClamp: |
| 3796 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 3797 | lvalue = true; |
| 3798 | break; |
| 3799 | case glslang::EOpSparseTextureLod: |
| 3800 | case glslang::EOpSparseTextureOffset: |
| 3801 | if (i == 3) |
| 3802 | lvalue = true; |
| 3803 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3804 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3805 | case glslang::EOpSparseTextureFetch: |
| 3806 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 3807 | lvalue = true; |
| 3808 | break; |
| 3809 | case glslang::EOpSparseTextureFetchOffset: |
| 3810 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 3811 | lvalue = true; |
| 3812 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3813 | #ifdef AMD_EXTENSIONS |
| 3814 | case glslang::EOpSparseTextureLodOffset: |
| 3815 | case glslang::EOpSparseTextureGrad: |
| 3816 | case glslang::EOpSparseTextureOffsetClamp: |
| 3817 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 3818 | lvalue = true; |
| 3819 | break; |
| 3820 | case glslang::EOpSparseTextureGradOffset: |
| 3821 | case glslang::EOpSparseTextureGradClamp: |
| 3822 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 3823 | lvalue = true; |
| 3824 | break; |
| 3825 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3826 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 3827 | lvalue = true; |
| 3828 | break; |
| 3829 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3830 | case glslang::EOpSparseTextureLodOffset: |
| 3831 | case glslang::EOpSparseTextureGrad: |
| 3832 | case glslang::EOpSparseTextureOffsetClamp: |
| 3833 | if (i == 4) |
| 3834 | lvalue = true; |
| 3835 | break; |
| 3836 | case glslang::EOpSparseTextureGradOffset: |
| 3837 | case glslang::EOpSparseTextureGradClamp: |
| 3838 | if (i == 5) |
| 3839 | lvalue = true; |
| 3840 | break; |
| 3841 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3842 | if (i == 6) |
| 3843 | lvalue = true; |
| 3844 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3845 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3846 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3847 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 3848 | lvalue = true; |
| 3849 | break; |
| 3850 | case glslang::EOpSparseTextureGatherOffset: |
| 3851 | case glslang::EOpSparseTextureGatherOffsets: |
| 3852 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 3853 | lvalue = true; |
| 3854 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3855 | #ifdef AMD_EXTENSIONS |
| 3856 | case glslang::EOpSparseTextureGatherLod: |
| 3857 | if (i == 3) |
| 3858 | lvalue = true; |
| 3859 | break; |
| 3860 | case glslang::EOpSparseTextureGatherLodOffset: |
| 3861 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 3862 | if (i == 4) |
| 3863 | lvalue = true; |
| 3864 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3865 | case glslang::EOpSparseImageLoadLod: |
| 3866 | if (i == 3) |
| 3867 | lvalue = true; |
| 3868 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3869 | #endif |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 3870 | #ifdef NV_EXTENSIONS |
| 3871 | case glslang::EOpImageSampleFootprintNV: |
| 3872 | if (i == 4) |
| 3873 | lvalue = true; |
| 3874 | break; |
| 3875 | case glslang::EOpImageSampleFootprintClampNV: |
| 3876 | case glslang::EOpImageSampleFootprintLodNV: |
| 3877 | if (i == 5) |
| 3878 | lvalue = true; |
| 3879 | break; |
| 3880 | case glslang::EOpImageSampleFootprintGradNV: |
| 3881 | if (i == 6) |
| 3882 | lvalue = true; |
| 3883 | break; |
| 3884 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 3885 | if (i == 7) |
| 3886 | lvalue = true; |
| 3887 | break; |
| 3888 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3889 | default: |
| 3890 | break; |
| 3891 | } |
| 3892 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3893 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3894 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3895 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3896 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3897 | } |
| 3898 | } |
| 3899 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3900 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3901 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3902 | builder.clearAccessChain(); |
| 3903 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3904 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3905 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3906 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3907 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 3908 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3909 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3910 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3911 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3912 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3913 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3914 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3915 | |
| 3916 | const glslang::TType &imageType = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 3917 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
| 3918 | const glslang::TSampler sampler = imageType.getSampler(); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3919 | #ifdef AMD_EXTENSIONS |
| 3920 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
| 3921 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 3922 | : false; |
| 3923 | #endif |
| 3924 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3925 | std::vector<spv::Id> arguments; |
| 3926 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3927 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3928 | else |
| 3929 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 3930 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3931 | |
| 3932 | spv::Builder::TextureParameters params = { }; |
| 3933 | params.sampler = arguments[0]; |
| 3934 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3935 | glslang::TCrackedTextureOp cracked; |
| 3936 | node->crackTexture(sampler, cracked); |
| 3937 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 3938 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3939 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3940 | // Check for queries |
| 3941 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3942 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 3943 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 3944 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3945 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3946 | switch (node->getOp()) { |
| 3947 | case glslang::EOpImageQuerySize: |
| 3948 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3949 | if (arguments.size() > 1) { |
| 3950 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3951 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3952 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3953 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3954 | case glslang::EOpImageQuerySamples: |
| 3955 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3956 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3957 | case glslang::EOpTextureQueryLod: |
| 3958 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3959 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3960 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3961 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3962 | case glslang::EOpSparseTexelsResident: |
| 3963 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3964 | default: |
| 3965 | assert(0); |
| 3966 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3967 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3968 | } |
| 3969 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 3970 | int components = node->getType().getVectorSize(); |
| 3971 | |
| 3972 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 3973 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 3974 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 3975 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 3976 | // here around e.g. which ones return scalars or other types. |
| 3977 | components = 4; |
| 3978 | } |
| 3979 | |
| 3980 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 3981 | |
| 3982 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 3983 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3984 | // Check for image functions other than queries |
| 3985 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3986 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3987 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3988 | spv::IdImmediate image = { true, *(opIt++) }; |
| 3989 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3990 | |
| 3991 | // Handle subpass operations |
| 3992 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 3993 | // built-in function. |
| 3994 | if (cracked.subpass) { |
| 3995 | // add on the (0,0) coordinate |
| 3996 | spv::Id zero = builder.makeIntConstant(0); |
| 3997 | std::vector<spv::Id> comps; |
| 3998 | comps.push_back(zero); |
| 3999 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4000 | spv::IdImmediate coord = { true, |
| 4001 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 4002 | operands.push_back(coord); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4003 | if (sampler.ms) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4004 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsSampleMask }; |
| 4005 | operands.push_back(imageOperands); |
| 4006 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 4007 | operands.push_back(imageOperand); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4008 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4009 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 4010 | builder.setPrecision(result, precision); |
| 4011 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4012 | } |
| 4013 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4014 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 4015 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4016 | #ifdef AMD_EXTENSIONS |
| 4017 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 4018 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4019 | if (node->getOp() == glslang::EOpImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4020 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4021 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4022 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4023 | mask = mask | spv::ImageOperandsSampleMask; |
| 4024 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4025 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4026 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4027 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4028 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4029 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4030 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4031 | #endif |
| 4032 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4033 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 4034 | if (mask) { |
| 4035 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4036 | operands.push_back(imageOperands); |
| 4037 | } |
| 4038 | if (mask & spv::ImageOperandsSampleMask) { |
| 4039 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4040 | operands.push_back(imageOperand); |
| 4041 | } |
| 4042 | #ifdef AMD_EXTENSIONS |
| 4043 | if (mask & spv::ImageOperandsLodMask) { |
| 4044 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4045 | operands.push_back(imageOperand); |
| 4046 | } |
| 4047 | #endif |
| 4048 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4049 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4050 | operands.push_back(imageOperand); |
| 4051 | } |
| 4052 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4053 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4054 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4055 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4056 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4057 | builder.setPrecision(result[0], precision); |
| 4058 | |
| 4059 | // If needed, add a conversion constructor to the proper size. |
| 4060 | if (components != node->getType().getVectorSize()) |
| 4061 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4062 | |
| 4063 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4064 | #ifdef AMD_EXTENSIONS |
| 4065 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 4066 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4067 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4068 | #endif |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4069 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4070 | // Push the texel value before the operands |
| 4071 | #ifdef AMD_EXTENSIONS |
| 4072 | if (sampler.ms || cracked.lod) { |
| 4073 | #else |
| 4074 | if (sampler.ms) { |
| 4075 | #endif |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4076 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4077 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4078 | } else { |
| 4079 | spv::IdImmediate texel = { true, *opIt }; |
| 4080 | operands.push_back(texel); |
| 4081 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4082 | |
| 4083 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 4084 | if (sampler.ms) { |
| 4085 | mask = mask | spv::ImageOperandsSampleMask; |
| 4086 | } |
| 4087 | #ifdef AMD_EXTENSIONS |
| 4088 | if (cracked.lod) { |
| 4089 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4090 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4091 | mask = mask | spv::ImageOperandsLodMask; |
| 4092 | } |
| 4093 | #endif |
| 4094 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4095 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
| 4096 | if (mask) { |
| 4097 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4098 | operands.push_back(imageOperands); |
| 4099 | } |
| 4100 | if (mask & spv::ImageOperandsSampleMask) { |
| 4101 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4102 | operands.push_back(imageOperand); |
| 4103 | } |
| 4104 | #ifdef AMD_EXTENSIONS |
| 4105 | if (mask & spv::ImageOperandsLodMask) { |
| 4106 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4107 | operands.push_back(imageOperand); |
| 4108 | } |
| 4109 | #endif |
| 4110 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
| 4111 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4112 | operands.push_back(imageOperand); |
| 4113 | } |
| 4114 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4115 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4116 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4117 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4118 | return spv::NoResult; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4119 | #ifdef AMD_EXTENSIONS |
| 4120 | } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 4121 | #else |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4122 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4123 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4124 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4125 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4126 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4127 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4128 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4129 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4130 | mask = mask | spv::ImageOperandsSampleMask; |
| 4131 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4132 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4133 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4134 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4135 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4136 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4137 | mask = mask | spv::ImageOperandsLodMask; |
| 4138 | } |
| 4139 | #endif |
| 4140 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4141 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 4142 | if (mask) { |
| 4143 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4144 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4145 | } |
| 4146 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4147 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4148 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4149 | } |
| 4150 | #ifdef AMD_EXTENSIONS |
| 4151 | if (mask & spv::ImageOperandsLodMask) { |
| 4152 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4153 | operands.push_back(imageOperand); |
| 4154 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4155 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4156 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4157 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4158 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | // Create the return type that was a special structure |
| 4162 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4163 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4164 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 4165 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 4166 | |
| 4167 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 4168 | |
| 4169 | // Decode the return type |
| 4170 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 4171 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 4172 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 4173 | // Process image atomic operations |
| 4174 | |
| 4175 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 4176 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4177 | // For non-MS, the sample value should be 0 |
| 4178 | spv::IdImmediate sample = { true, sampler.ms ? *(opIt++) : builder.makeUintConstant(0) }; |
| 4179 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4180 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4181 | spv::Id resultTypeId; |
| 4182 | // imageAtomicStore has a void return type so base the pointer type on |
| 4183 | // the type of the value operand. |
| 4184 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 4185 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word)); |
| 4186 | } else { |
| 4187 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 4188 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4189 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4190 | |
| 4191 | std::vector<spv::Id> operands; |
| 4192 | operands.push_back(pointer); |
| 4193 | for (; opIt != arguments.end(); ++opIt) |
| 4194 | operands.push_back(*opIt); |
| 4195 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4196 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4197 | } |
| 4198 | } |
| 4199 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4200 | #ifdef AMD_EXTENSIONS |
| 4201 | // Check for fragment mask functions other than queries |
| 4202 | if (cracked.fragMask) { |
| 4203 | assert(sampler.ms); |
| 4204 | |
| 4205 | auto opIt = arguments.begin(); |
| 4206 | std::vector<spv::Id> operands; |
| 4207 | |
| 4208 | // Extract the image if necessary |
| 4209 | if (builder.isSampledImage(params.sampler)) |
| 4210 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4211 | |
| 4212 | operands.push_back(params.sampler); |
| 4213 | ++opIt; |
| 4214 | |
| 4215 | if (sampler.isSubpass()) { |
| 4216 | // add on the (0,0) coordinate |
| 4217 | spv::Id zero = builder.makeIntConstant(0); |
| 4218 | std::vector<spv::Id> comps; |
| 4219 | comps.push_back(zero); |
| 4220 | comps.push_back(zero); |
| 4221 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 4222 | } |
| 4223 | |
| 4224 | for (; opIt != arguments.end(); ++opIt) |
| 4225 | operands.push_back(*opIt); |
| 4226 | |
| 4227 | spv::Op fragMaskOp = spv::OpNop; |
| 4228 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 4229 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 4230 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 4231 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 4232 | |
| 4233 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 4234 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 4235 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 4236 | } |
| 4237 | #endif |
| 4238 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4239 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4240 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4241 | #ifdef NV_EXTENSIONS |
| 4242 | bool imageFootprint = node->isImageFootprint(); |
| 4243 | #endif |
| 4244 | |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4245 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 4246 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4247 | // check for bias argument |
| 4248 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4249 | #ifdef AMD_EXTENSIONS |
| 4250 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 4251 | #else |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4252 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4253 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4254 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4255 | #ifdef AMD_EXTENSIONS |
| 4256 | if (cracked.gather) |
| 4257 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4258 | |
| 4259 | if (f16ShadowCompare) |
| 4260 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4261 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4262 | if (cracked.offset) |
| 4263 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4264 | #ifdef AMD_EXTENSIONS |
| 4265 | else if (cracked.offsets) |
| 4266 | ++nonBiasArgCount; |
| 4267 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4268 | if (cracked.grad) |
| 4269 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4270 | if (cracked.lodClamp) |
| 4271 | ++nonBiasArgCount; |
| 4272 | if (sparse) |
| 4273 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4274 | #ifdef NV_EXTENSIONS |
| 4275 | if (imageFootprint) |
| 4276 | //Following three extra arguments |
| 4277 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4278 | nonBiasArgCount += 3; |
| 4279 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4280 | if ((int)arguments.size() > nonBiasArgCount) |
| 4281 | bias = true; |
| 4282 | } |
| 4283 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 4284 | // See if the sampler param should really be just the SPV image part |
| 4285 | if (cracked.fetch) { |
| 4286 | // a fetch needs to have the image extracted first |
| 4287 | if (builder.isSampledImage(params.sampler)) |
| 4288 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4289 | } |
| 4290 | |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4291 | #ifdef AMD_EXTENSIONS |
| 4292 | if (cracked.gather) { |
| 4293 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 4294 | if (bias || cracked.lod || |
| 4295 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 4296 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 4297 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4298 | } |
| 4299 | } |
| 4300 | #endif |
| 4301 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4302 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4303 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4304 | params.coords = arguments[1]; |
| 4305 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4306 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4307 | |
| 4308 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4309 | #ifdef AMD_EXTENSIONS |
| 4310 | if (cubeCompare || f16ShadowCompare) { |
| 4311 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4312 | if (cubeCompare) { |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4313 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4314 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4315 | ++extraArgs; |
| 4316 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4317 | params.Dref = arguments[2]; |
| 4318 | ++extraArgs; |
| 4319 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4320 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4321 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4322 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4323 | 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] | 4324 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4325 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 4326 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4327 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 4328 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4329 | |
| 4330 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4331 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4332 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4333 | ++extraArgs; |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 4334 | } else if (glslangIntermediate->getStage() != EShLangFragment |
| 4335 | #ifdef NV_EXTENSIONS |
| 4336 | // NV_compute_shader_derivatives layout qualifiers allow for implicit LODs |
| 4337 | && !(glslangIntermediate->getStage() == EShLangCompute && |
| 4338 | (glslangIntermediate->getLayoutDerivativeModeNone() != glslang::LayoutDerivativeNone)) |
| 4339 | #endif |
| 4340 | ) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4341 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 4342 | noImplicitLod = true; |
| 4343 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4344 | |
| 4345 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4346 | if (sampler.ms) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4347 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4348 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4349 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4350 | |
| 4351 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4352 | if (cracked.grad) { |
| 4353 | params.gradX = arguments[2 + extraArgs]; |
| 4354 | params.gradY = arguments[3 + extraArgs]; |
| 4355 | extraArgs += 2; |
| 4356 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4357 | |
| 4358 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4359 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4360 | params.offset = arguments[2 + extraArgs]; |
| 4361 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4362 | } else if (cracked.offsets) { |
| 4363 | params.offsets = arguments[2 + extraArgs]; |
| 4364 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4365 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4366 | |
| 4367 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4368 | if (cracked.lodClamp) { |
| 4369 | params.lodClamp = arguments[2 + extraArgs]; |
| 4370 | ++extraArgs; |
| 4371 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4372 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4373 | if (sparse) { |
| 4374 | params.texelOut = arguments[2 + extraArgs]; |
| 4375 | ++extraArgs; |
| 4376 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4377 | |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4378 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4379 | if (cracked.gather && ! sampler.shadow) { |
| 4380 | // default component is 0, if missing, otherwise an argument |
| 4381 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4382 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4383 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4384 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4385 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4386 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4387 | #ifdef NV_EXTENSIONS |
| 4388 | spv::Id resultStruct = spv::NoResult; |
| 4389 | if (imageFootprint) { |
| 4390 | //Following three extra arguments |
| 4391 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4392 | params.granularity = arguments[2 + extraArgs]; |
| 4393 | params.coarse = arguments[3 + extraArgs]; |
| 4394 | resultStruct = arguments[4 + extraArgs]; |
| 4395 | extraArgs += 3; |
| 4396 | } |
| 4397 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4398 | // bias |
| 4399 | if (bias) { |
| 4400 | params.bias = arguments[2 + extraArgs]; |
| 4401 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4402 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4403 | |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4404 | #ifdef NV_EXTENSIONS |
| 4405 | if (imageFootprint) { |
| 4406 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 4407 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 4408 | |
| 4409 | |
| 4410 | //resultStructType(OpenGL type) contains 5 elements: |
| 4411 | //struct gl_TextureFootprint2DNV { |
| 4412 | // uvec2 anchor; |
| 4413 | // uvec2 offset; |
| 4414 | // uvec2 mask; |
| 4415 | // uint lod; |
| 4416 | // uint granularity; |
| 4417 | //}; |
| 4418 | //or |
| 4419 | //struct gl_TextureFootprint3DNV { |
| 4420 | // uvec3 anchor; |
| 4421 | // uvec3 offset; |
| 4422 | // uvec2 mask; |
| 4423 | // uint lod; |
| 4424 | // uint granularity; |
| 4425 | //}; |
| 4426 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 4427 | assert(builder.isStructType(resultStructType)); |
| 4428 | |
| 4429 | //resType (SPIR-V type) contains 6 elements: |
| 4430 | //Member 0 must be a Boolean type scalar(LOD), |
| 4431 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 4432 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 4433 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 4434 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 4435 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 4436 | std::vector<spv::Id> members; |
| 4437 | members.push_back(resultType()); |
| 4438 | for (int i = 0; i < 5; i++) { |
| 4439 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 4440 | } |
| 4441 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 4442 | |
| 4443 | //call ImageFootprintNV |
| 4444 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params); |
| 4445 | |
| 4446 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 4447 | for (int i = 0; i < 5; i++) { |
| 4448 | builder.clearAccessChain(); |
| 4449 | builder.setAccessChainLValue(resultStruct); |
| 4450 | |
| 4451 | //Accessing to a struct we created, no coherent flag is set |
| 4452 | spv::Builder::AccessChain::CoherentFlags flags; |
| 4453 | flags.clear(); |
| 4454 | |
| 4455 | builder.accessChainPush(builder.makeIntConstant(i), flags); |
| 4456 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1)); |
| 4457 | } |
| 4458 | return builder.createCompositeExtract(res, resultType(), 0); |
| 4459 | } |
| 4460 | #endif |
| 4461 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4462 | // projective component (might not to move) |
| 4463 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 4464 | // are divided by the last component of P." |
| 4465 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 4466 | // unused components will appear after all used components." |
| 4467 | if (cracked.proj) { |
| 4468 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 4469 | int projTargetComp; |
| 4470 | switch (sampler.dim) { |
| 4471 | case glslang::Esd1D: projTargetComp = 1; break; |
| 4472 | case glslang::Esd2D: projTargetComp = 2; break; |
| 4473 | case glslang::EsdRect: projTargetComp = 2; break; |
| 4474 | default: projTargetComp = projSourceComp; break; |
| 4475 | } |
| 4476 | // copy the projective coordinate if we have to |
| 4477 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 4478 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4479 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 4480 | projSourceComp); |
| 4481 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 4482 | builder.getTypeId(params.coords), projTargetComp); |
| 4483 | } |
| 4484 | } |
| 4485 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4486 | // nonprivate |
| 4487 | if (imageType.getQualifier().nonprivate) { |
| 4488 | params.nonprivate = true; |
| 4489 | } |
| 4490 | |
| 4491 | // volatile |
| 4492 | if (imageType.getQualifier().volatil) { |
| 4493 | params.volatil = true; |
| 4494 | } |
| 4495 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4496 | std::vector<spv::Id> result( 1, |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4497 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4498 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4499 | |
| 4500 | if (components != node->getType().getVectorSize()) |
| 4501 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4502 | |
| 4503 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 4507 | { |
| 4508 | // Grab the function's pointer from the previously created function |
| 4509 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 4510 | if (! function) |
| 4511 | return 0; |
| 4512 | |
| 4513 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 4514 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 4515 | |
| 4516 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 4517 | // |
| 4518 | // These imply we need a four step process: |
| 4519 | // 1. Evaluate the arguments |
| 4520 | // 2. Allocate and make copies of in, out, and inout arguments |
| 4521 | // 3. Make the call |
| 4522 | // 4. Copy back the results |
| 4523 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4524 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4525 | std::vector<spv::Builder::AccessChain> lValues; |
| 4526 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4527 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4528 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4529 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4530 | // build l-value |
| 4531 | builder.clearAccessChain(); |
| 4532 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4533 | // 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] | 4534 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4535 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4536 | // save l-value |
| 4537 | lValues.push_back(builder.getAccessChain()); |
| 4538 | } else { |
| 4539 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4540 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4541 | } |
| 4542 | } |
| 4543 | |
| 4544 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 4545 | // copy the original into that space. |
| 4546 | // |
| 4547 | // Also, build up the list of actual arguments to pass in for the call |
| 4548 | int lValueCount = 0; |
| 4549 | int rValueCount = 0; |
| 4550 | std::vector<spv::Id> spvArgs; |
| 4551 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 4552 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4553 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 4554 | builder.setAccessChain(lValues[lValueCount]); |
| 4555 | arg = builder.accessChainGetLValue(); |
| 4556 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4557 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4558 | // need space to hold the copy |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4559 | arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4560 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 4561 | // need to copy the input into output space |
| 4562 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4563 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4564 | builder.clearAccessChain(); |
| 4565 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4566 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4567 | } |
| 4568 | ++lValueCount; |
| 4569 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4570 | // process r-value, which involves a copy for a type mismatch |
| 4571 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) { |
| 4572 | spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg"); |
| 4573 | builder.clearAccessChain(); |
| 4574 | builder.setAccessChainLValue(argCopy); |
| 4575 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 4576 | arg = builder.createLoad(argCopy); |
| 4577 | } else |
| 4578 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4579 | ++rValueCount; |
| 4580 | } |
| 4581 | spvArgs.push_back(arg); |
| 4582 | } |
| 4583 | |
| 4584 | // 3. Make the call. |
| 4585 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4586 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4587 | |
| 4588 | // 4. Copy back out an "out" arguments. |
| 4589 | lValueCount = 0; |
| 4590 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4591 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4592 | ++lValueCount; |
| 4593 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4594 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 4595 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 4596 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4597 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4598 | } |
| 4599 | ++lValueCount; |
| 4600 | } |
| 4601 | } |
| 4602 | |
| 4603 | return result; |
| 4604 | } |
| 4605 | |
| 4606 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4607 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4608 | spv::Id typeId, spv::Id left, spv::Id right, |
| 4609 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 4610 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4611 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 4612 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4613 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4614 | |
| 4615 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4616 | 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] | 4617 | bool comparison = false; |
| 4618 | |
| 4619 | switch (op) { |
| 4620 | case glslang::EOpAdd: |
| 4621 | case glslang::EOpAddAssign: |
| 4622 | if (isFloat) |
| 4623 | binOp = spv::OpFAdd; |
| 4624 | else |
| 4625 | binOp = spv::OpIAdd; |
| 4626 | break; |
| 4627 | case glslang::EOpSub: |
| 4628 | case glslang::EOpSubAssign: |
| 4629 | if (isFloat) |
| 4630 | binOp = spv::OpFSub; |
| 4631 | else |
| 4632 | binOp = spv::OpISub; |
| 4633 | break; |
| 4634 | case glslang::EOpMul: |
| 4635 | case glslang::EOpMulAssign: |
| 4636 | if (isFloat) |
| 4637 | binOp = spv::OpFMul; |
| 4638 | else |
| 4639 | binOp = spv::OpIMul; |
| 4640 | break; |
| 4641 | case glslang::EOpVectorTimesScalar: |
| 4642 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 4643 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4644 | if (builder.isVector(right)) |
| 4645 | std::swap(left, right); |
| 4646 | assert(builder.isScalar(right)); |
| 4647 | needMatchingVectors = false; |
| 4648 | binOp = spv::OpVectorTimesScalar; |
t.jung | 697fdf0 | 2018-11-14 13:04:39 +0100 | [diff] [blame] | 4649 | } else if (isFloat) |
| 4650 | binOp = spv::OpFMul; |
| 4651 | else |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4652 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4653 | break; |
| 4654 | case glslang::EOpVectorTimesMatrix: |
| 4655 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4656 | binOp = spv::OpVectorTimesMatrix; |
| 4657 | break; |
| 4658 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4659 | binOp = spv::OpMatrixTimesVector; |
| 4660 | break; |
| 4661 | case glslang::EOpMatrixTimesScalar: |
| 4662 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4663 | binOp = spv::OpMatrixTimesScalar; |
| 4664 | break; |
| 4665 | case glslang::EOpMatrixTimesMatrix: |
| 4666 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4667 | binOp = spv::OpMatrixTimesMatrix; |
| 4668 | break; |
| 4669 | case glslang::EOpOuterProduct: |
| 4670 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4671 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4672 | break; |
| 4673 | |
| 4674 | case glslang::EOpDiv: |
| 4675 | case glslang::EOpDivAssign: |
| 4676 | if (isFloat) |
| 4677 | binOp = spv::OpFDiv; |
| 4678 | else if (isUnsigned) |
| 4679 | binOp = spv::OpUDiv; |
| 4680 | else |
| 4681 | binOp = spv::OpSDiv; |
| 4682 | break; |
| 4683 | case glslang::EOpMod: |
| 4684 | case glslang::EOpModAssign: |
| 4685 | if (isFloat) |
| 4686 | binOp = spv::OpFMod; |
| 4687 | else if (isUnsigned) |
| 4688 | binOp = spv::OpUMod; |
| 4689 | else |
| 4690 | binOp = spv::OpSMod; |
| 4691 | break; |
| 4692 | case glslang::EOpRightShift: |
| 4693 | case glslang::EOpRightShiftAssign: |
| 4694 | if (isUnsigned) |
| 4695 | binOp = spv::OpShiftRightLogical; |
| 4696 | else |
| 4697 | binOp = spv::OpShiftRightArithmetic; |
| 4698 | break; |
| 4699 | case glslang::EOpLeftShift: |
| 4700 | case glslang::EOpLeftShiftAssign: |
| 4701 | binOp = spv::OpShiftLeftLogical; |
| 4702 | break; |
| 4703 | case glslang::EOpAnd: |
| 4704 | case glslang::EOpAndAssign: |
| 4705 | binOp = spv::OpBitwiseAnd; |
| 4706 | break; |
| 4707 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4708 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4709 | binOp = spv::OpLogicalAnd; |
| 4710 | break; |
| 4711 | case glslang::EOpInclusiveOr: |
| 4712 | case glslang::EOpInclusiveOrAssign: |
| 4713 | binOp = spv::OpBitwiseOr; |
| 4714 | break; |
| 4715 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4716 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4717 | binOp = spv::OpLogicalOr; |
| 4718 | break; |
| 4719 | case glslang::EOpExclusiveOr: |
| 4720 | case glslang::EOpExclusiveOrAssign: |
| 4721 | binOp = spv::OpBitwiseXor; |
| 4722 | break; |
| 4723 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4724 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4725 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4726 | break; |
| 4727 | |
| 4728 | case glslang::EOpLessThan: |
| 4729 | case glslang::EOpGreaterThan: |
| 4730 | case glslang::EOpLessThanEqual: |
| 4731 | case glslang::EOpGreaterThanEqual: |
| 4732 | case glslang::EOpEqual: |
| 4733 | case glslang::EOpNotEqual: |
| 4734 | case glslang::EOpVectorEqual: |
| 4735 | case glslang::EOpVectorNotEqual: |
| 4736 | comparison = true; |
| 4737 | break; |
| 4738 | default: |
| 4739 | break; |
| 4740 | } |
| 4741 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4742 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4743 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4744 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4745 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4746 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4747 | |
| 4748 | // 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] | 4749 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4750 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4751 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4752 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4753 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4754 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4755 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | if (! comparison) |
| 4759 | return 0; |
| 4760 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4761 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4762 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 4763 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4764 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 4765 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4766 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4767 | return result; |
| 4768 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4769 | |
| 4770 | switch (op) { |
| 4771 | case glslang::EOpLessThan: |
| 4772 | if (isFloat) |
| 4773 | binOp = spv::OpFOrdLessThan; |
| 4774 | else if (isUnsigned) |
| 4775 | binOp = spv::OpULessThan; |
| 4776 | else |
| 4777 | binOp = spv::OpSLessThan; |
| 4778 | break; |
| 4779 | case glslang::EOpGreaterThan: |
| 4780 | if (isFloat) |
| 4781 | binOp = spv::OpFOrdGreaterThan; |
| 4782 | else if (isUnsigned) |
| 4783 | binOp = spv::OpUGreaterThan; |
| 4784 | else |
| 4785 | binOp = spv::OpSGreaterThan; |
| 4786 | break; |
| 4787 | case glslang::EOpLessThanEqual: |
| 4788 | if (isFloat) |
| 4789 | binOp = spv::OpFOrdLessThanEqual; |
| 4790 | else if (isUnsigned) |
| 4791 | binOp = spv::OpULessThanEqual; |
| 4792 | else |
| 4793 | binOp = spv::OpSLessThanEqual; |
| 4794 | break; |
| 4795 | case glslang::EOpGreaterThanEqual: |
| 4796 | if (isFloat) |
| 4797 | binOp = spv::OpFOrdGreaterThanEqual; |
| 4798 | else if (isUnsigned) |
| 4799 | binOp = spv::OpUGreaterThanEqual; |
| 4800 | else |
| 4801 | binOp = spv::OpSGreaterThanEqual; |
| 4802 | break; |
| 4803 | case glslang::EOpEqual: |
| 4804 | case glslang::EOpVectorEqual: |
| 4805 | if (isFloat) |
| 4806 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4807 | else if (isBool) |
| 4808 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4809 | else |
| 4810 | binOp = spv::OpIEqual; |
| 4811 | break; |
| 4812 | case glslang::EOpNotEqual: |
| 4813 | case glslang::EOpVectorNotEqual: |
| 4814 | if (isFloat) |
| 4815 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4816 | else if (isBool) |
| 4817 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4818 | else |
| 4819 | binOp = spv::OpINotEqual; |
| 4820 | break; |
| 4821 | default: |
| 4822 | break; |
| 4823 | } |
| 4824 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4825 | if (binOp != spv::OpNop) { |
| 4826 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4827 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4828 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4829 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4830 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4831 | |
| 4832 | return 0; |
| 4833 | } |
| 4834 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4835 | // |
| 4836 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 4837 | // These can be any of: |
| 4838 | // |
| 4839 | // matrix * scalar |
| 4840 | // scalar * matrix |
| 4841 | // matrix * matrix linear algebraic |
| 4842 | // matrix * vector |
| 4843 | // vector * matrix |
| 4844 | // matrix * matrix componentwise |
| 4845 | // matrix op matrix op in {+, -, /} |
| 4846 | // matrix op scalar op in {+, -, /} |
| 4847 | // scalar op matrix op in {+, -, /} |
| 4848 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4849 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 4850 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4851 | { |
| 4852 | bool firstClass = true; |
| 4853 | |
| 4854 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 4855 | switch (op) { |
| 4856 | case spv::OpFDiv: |
| 4857 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 4858 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 4859 | spv::Id resultType = builder.getTypeId(right); |
| 4860 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4861 | op = spv::OpMatrixTimesScalar; |
| 4862 | } else |
| 4863 | firstClass = false; |
| 4864 | break; |
| 4865 | case spv::OpMatrixTimesScalar: |
| 4866 | if (builder.isMatrix(right)) |
| 4867 | std::swap(left, right); |
| 4868 | assert(builder.isScalar(right)); |
| 4869 | break; |
| 4870 | case spv::OpVectorTimesMatrix: |
| 4871 | assert(builder.isVector(left)); |
| 4872 | assert(builder.isMatrix(right)); |
| 4873 | break; |
| 4874 | case spv::OpMatrixTimesVector: |
| 4875 | assert(builder.isMatrix(left)); |
| 4876 | assert(builder.isVector(right)); |
| 4877 | break; |
| 4878 | case spv::OpMatrixTimesMatrix: |
| 4879 | assert(builder.isMatrix(left)); |
| 4880 | assert(builder.isMatrix(right)); |
| 4881 | break; |
| 4882 | default: |
| 4883 | firstClass = false; |
| 4884 | break; |
| 4885 | } |
| 4886 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4887 | if (firstClass) { |
| 4888 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4889 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4890 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4891 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4892 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4893 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4894 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4895 | // The result type of all of them is the same type as the (a) matrix operand. |
| 4896 | // The algorithm is to: |
| 4897 | // - break the matrix(es) into vectors |
| 4898 | // - smear any scalar to a vector |
| 4899 | // - do vector operations |
| 4900 | // - make a matrix out the vector results |
| 4901 | switch (op) { |
| 4902 | case spv::OpFAdd: |
| 4903 | case spv::OpFSub: |
| 4904 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4905 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4906 | case spv::OpFMul: |
| 4907 | { |
| 4908 | // one time set up... |
| 4909 | bool leftMat = builder.isMatrix(left); |
| 4910 | bool rightMat = builder.isMatrix(right); |
| 4911 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 4912 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 4913 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 4914 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 4915 | std::vector<spv::Id> results; |
| 4916 | spv::Id smearVec = spv::NoResult; |
| 4917 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4918 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4919 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4920 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4921 | |
| 4922 | // do each vector op |
| 4923 | for (unsigned int c = 0; c < numCols; ++c) { |
| 4924 | std::vector<unsigned int> indexes; |
| 4925 | indexes.push_back(c); |
| 4926 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 4927 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4928 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4929 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4930 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4931 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4935 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4936 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4937 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4938 | } |
| 4939 | default: |
| 4940 | assert(0); |
| 4941 | return spv::NoResult; |
| 4942 | } |
| 4943 | } |
| 4944 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4945 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
| 4946 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4947 | { |
| 4948 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4949 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4950 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4951 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 4952 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4953 | |
| 4954 | switch (op) { |
| 4955 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4956 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4957 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4958 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4959 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4960 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4961 | unaryOp = spv::OpSNegate; |
| 4962 | break; |
| 4963 | |
| 4964 | case glslang::EOpLogicalNot: |
| 4965 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4966 | unaryOp = spv::OpLogicalNot; |
| 4967 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4968 | case glslang::EOpBitwiseNot: |
| 4969 | unaryOp = spv::OpNot; |
| 4970 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4971 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4972 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4973 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4974 | break; |
| 4975 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4976 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4977 | break; |
| 4978 | case glslang::EOpTranspose: |
| 4979 | unaryOp = spv::OpTranspose; |
| 4980 | break; |
| 4981 | |
| 4982 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4983 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4984 | break; |
| 4985 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4986 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4987 | break; |
| 4988 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4989 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4990 | break; |
| 4991 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4992 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4993 | break; |
| 4994 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4995 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4996 | break; |
| 4997 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4998 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4999 | break; |
| 5000 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5001 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5002 | break; |
| 5003 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5004 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5005 | break; |
| 5006 | |
| 5007 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5008 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5009 | break; |
| 5010 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5011 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5012 | break; |
| 5013 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5014 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5015 | break; |
| 5016 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5017 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5018 | break; |
| 5019 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5020 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5021 | break; |
| 5022 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5023 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5024 | break; |
| 5025 | |
| 5026 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5027 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5028 | break; |
| 5029 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5030 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5031 | break; |
| 5032 | |
| 5033 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5034 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5035 | break; |
| 5036 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5037 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5038 | break; |
| 5039 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5040 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5041 | break; |
| 5042 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5043 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5044 | break; |
| 5045 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5046 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5047 | break; |
| 5048 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5049 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5050 | break; |
| 5051 | |
| 5052 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5053 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5054 | break; |
| 5055 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5056 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5057 | break; |
| 5058 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5059 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5060 | break; |
| 5061 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5062 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5063 | break; |
| 5064 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5065 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5066 | break; |
| 5067 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5068 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5069 | break; |
| 5070 | |
| 5071 | case glslang::EOpIsNan: |
| 5072 | unaryOp = spv::OpIsNan; |
| 5073 | break; |
| 5074 | case glslang::EOpIsInf: |
| 5075 | unaryOp = spv::OpIsInf; |
| 5076 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5077 | case glslang::EOpIsFinite: |
| 5078 | unaryOp = spv::OpIsFinite; |
| 5079 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5080 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5081 | case glslang::EOpFloatBitsToInt: |
| 5082 | case glslang::EOpFloatBitsToUint: |
| 5083 | case glslang::EOpIntBitsToFloat: |
| 5084 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5085 | case glslang::EOpDoubleBitsToInt64: |
| 5086 | case glslang::EOpDoubleBitsToUint64: |
| 5087 | case glslang::EOpInt64BitsToDouble: |
| 5088 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5089 | case glslang::EOpFloat16BitsToInt16: |
| 5090 | case glslang::EOpFloat16BitsToUint16: |
| 5091 | case glslang::EOpInt16BitsToFloat16: |
| 5092 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5093 | unaryOp = spv::OpBitcast; |
| 5094 | break; |
| 5095 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5096 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5097 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5098 | break; |
| 5099 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5100 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5101 | break; |
| 5102 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5103 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5104 | break; |
| 5105 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5106 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5107 | break; |
| 5108 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5109 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5110 | break; |
| 5111 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5112 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5113 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5114 | case glslang::EOpPackSnorm4x8: |
| 5115 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 5116 | break; |
| 5117 | case glslang::EOpUnpackSnorm4x8: |
| 5118 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5119 | break; |
| 5120 | case glslang::EOpPackUnorm4x8: |
| 5121 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 5122 | break; |
| 5123 | case glslang::EOpUnpackUnorm4x8: |
| 5124 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5125 | break; |
| 5126 | case glslang::EOpPackDouble2x32: |
| 5127 | libCall = spv::GLSLstd450PackDouble2x32; |
| 5128 | break; |
| 5129 | case glslang::EOpUnpackDouble2x32: |
| 5130 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5131 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5132 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5133 | case glslang::EOpPackInt2x32: |
| 5134 | case glslang::EOpUnpackInt2x32: |
| 5135 | case glslang::EOpPackUint2x32: |
| 5136 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5137 | case glslang::EOpPack16: |
| 5138 | case glslang::EOpPack32: |
| 5139 | case glslang::EOpPack64: |
| 5140 | case glslang::EOpUnpack32: |
| 5141 | case glslang::EOpUnpack16: |
| 5142 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5143 | case glslang::EOpPackInt2x16: |
| 5144 | case glslang::EOpUnpackInt2x16: |
| 5145 | case glslang::EOpPackUint2x16: |
| 5146 | case glslang::EOpUnpackUint2x16: |
| 5147 | case glslang::EOpPackInt4x16: |
| 5148 | case glslang::EOpUnpackInt4x16: |
| 5149 | case glslang::EOpPackUint4x16: |
| 5150 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5151 | case glslang::EOpPackFloat2x16: |
| 5152 | case glslang::EOpUnpackFloat2x16: |
| 5153 | unaryOp = spv::OpBitcast; |
| 5154 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5155 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5156 | case glslang::EOpDPdx: |
| 5157 | unaryOp = spv::OpDPdx; |
| 5158 | break; |
| 5159 | case glslang::EOpDPdy: |
| 5160 | unaryOp = spv::OpDPdy; |
| 5161 | break; |
| 5162 | case glslang::EOpFwidth: |
| 5163 | unaryOp = spv::OpFwidth; |
| 5164 | break; |
| 5165 | case glslang::EOpDPdxFine: |
| 5166 | unaryOp = spv::OpDPdxFine; |
| 5167 | break; |
| 5168 | case glslang::EOpDPdyFine: |
| 5169 | unaryOp = spv::OpDPdyFine; |
| 5170 | break; |
| 5171 | case glslang::EOpFwidthFine: |
| 5172 | unaryOp = spv::OpFwidthFine; |
| 5173 | break; |
| 5174 | case glslang::EOpDPdxCoarse: |
| 5175 | unaryOp = spv::OpDPdxCoarse; |
| 5176 | break; |
| 5177 | case glslang::EOpDPdyCoarse: |
| 5178 | unaryOp = spv::OpDPdyCoarse; |
| 5179 | break; |
| 5180 | case glslang::EOpFwidthCoarse: |
| 5181 | unaryOp = spv::OpFwidthCoarse; |
| 5182 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5183 | case glslang::EOpInterpolateAtCentroid: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 5184 | #ifdef AMD_EXTENSIONS |
| 5185 | if (typeProxy == glslang::EbtFloat16) |
| 5186 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 5187 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5188 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 5189 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5190 | case glslang::EOpAny: |
| 5191 | unaryOp = spv::OpAny; |
| 5192 | break; |
| 5193 | case glslang::EOpAll: |
| 5194 | unaryOp = spv::OpAll; |
| 5195 | break; |
| 5196 | |
| 5197 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5198 | if (isFloat) |
| 5199 | libCall = spv::GLSLstd450FAbs; |
| 5200 | else |
| 5201 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5202 | break; |
| 5203 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5204 | if (isFloat) |
| 5205 | libCall = spv::GLSLstd450FSign; |
| 5206 | else |
| 5207 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5208 | break; |
| 5209 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5210 | case glslang::EOpAtomicCounterIncrement: |
| 5211 | case glslang::EOpAtomicCounterDecrement: |
| 5212 | case glslang::EOpAtomicCounter: |
| 5213 | { |
| 5214 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 5215 | std::vector<spv::Id> operands; |
| 5216 | operands.push_back(operand); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5217 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5218 | } |
| 5219 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5220 | case glslang::EOpBitFieldReverse: |
| 5221 | unaryOp = spv::OpBitReverse; |
| 5222 | break; |
| 5223 | case glslang::EOpBitCount: |
| 5224 | unaryOp = spv::OpBitCount; |
| 5225 | break; |
| 5226 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5227 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5228 | break; |
| 5229 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5230 | if (isUnsigned) |
| 5231 | libCall = spv::GLSLstd450FindUMsb; |
| 5232 | else |
| 5233 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5234 | break; |
| 5235 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5236 | case glslang::EOpBallot: |
| 5237 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5238 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5239 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5240 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5241 | #ifdef AMD_EXTENSIONS |
| 5242 | case glslang::EOpMinInvocations: |
| 5243 | case glslang::EOpMaxInvocations: |
| 5244 | case glslang::EOpAddInvocations: |
| 5245 | case glslang::EOpMinInvocationsNonUniform: |
| 5246 | case glslang::EOpMaxInvocationsNonUniform: |
| 5247 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5248 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5249 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5250 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5251 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5252 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5253 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5254 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5255 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5256 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5257 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5258 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5259 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5260 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5261 | { |
| 5262 | std::vector<spv::Id> operands; |
| 5263 | operands.push_back(operand); |
| 5264 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 5265 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5266 | case glslang::EOpSubgroupAll: |
| 5267 | case glslang::EOpSubgroupAny: |
| 5268 | case glslang::EOpSubgroupAllEqual: |
| 5269 | case glslang::EOpSubgroupBroadcastFirst: |
| 5270 | case glslang::EOpSubgroupBallot: |
| 5271 | case glslang::EOpSubgroupInverseBallot: |
| 5272 | case glslang::EOpSubgroupBallotBitCount: |
| 5273 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5274 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5275 | case glslang::EOpSubgroupBallotFindLSB: |
| 5276 | case glslang::EOpSubgroupBallotFindMSB: |
| 5277 | case glslang::EOpSubgroupAdd: |
| 5278 | case glslang::EOpSubgroupMul: |
| 5279 | case glslang::EOpSubgroupMin: |
| 5280 | case glslang::EOpSubgroupMax: |
| 5281 | case glslang::EOpSubgroupAnd: |
| 5282 | case glslang::EOpSubgroupOr: |
| 5283 | case glslang::EOpSubgroupXor: |
| 5284 | case glslang::EOpSubgroupInclusiveAdd: |
| 5285 | case glslang::EOpSubgroupInclusiveMul: |
| 5286 | case glslang::EOpSubgroupInclusiveMin: |
| 5287 | case glslang::EOpSubgroupInclusiveMax: |
| 5288 | case glslang::EOpSubgroupInclusiveAnd: |
| 5289 | case glslang::EOpSubgroupInclusiveOr: |
| 5290 | case glslang::EOpSubgroupInclusiveXor: |
| 5291 | case glslang::EOpSubgroupExclusiveAdd: |
| 5292 | case glslang::EOpSubgroupExclusiveMul: |
| 5293 | case glslang::EOpSubgroupExclusiveMin: |
| 5294 | case glslang::EOpSubgroupExclusiveMax: |
| 5295 | case glslang::EOpSubgroupExclusiveAnd: |
| 5296 | case glslang::EOpSubgroupExclusiveOr: |
| 5297 | case glslang::EOpSubgroupExclusiveXor: |
| 5298 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5299 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5300 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 5301 | std::vector<spv::Id> operands; |
| 5302 | operands.push_back(operand); |
| 5303 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 5304 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5305 | #ifdef AMD_EXTENSIONS |
| 5306 | case glslang::EOpMbcnt: |
| 5307 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5308 | libCall = spv::MbcntAMD; |
| 5309 | break; |
| 5310 | |
| 5311 | case glslang::EOpCubeFaceIndex: |
| 5312 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5313 | libCall = spv::CubeFaceIndexAMD; |
| 5314 | break; |
| 5315 | |
| 5316 | case glslang::EOpCubeFaceCoord: |
| 5317 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5318 | libCall = spv::CubeFaceCoordAMD; |
| 5319 | break; |
| 5320 | #endif |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5321 | #ifdef NV_EXTENSIONS |
| 5322 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5323 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 5324 | break; |
| 5325 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5326 | default: |
| 5327 | return 0; |
| 5328 | } |
| 5329 | |
| 5330 | spv::Id id; |
| 5331 | if (libCall >= 0) { |
| 5332 | std::vector<spv::Id> args; |
| 5333 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5334 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5335 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5336 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5337 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5338 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5339 | builder.addDecoration(id, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5340 | builder.addDecoration(id, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5341 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5342 | } |
| 5343 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5344 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5345 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5346 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5347 | { |
| 5348 | // Handle unary operations vector by vector. |
| 5349 | // The result type is the same type as the original type. |
| 5350 | // The algorithm is to: |
| 5351 | // - break the matrix into vectors |
| 5352 | // - apply the operation to each vector |
| 5353 | // - make a matrix out the vector results |
| 5354 | |
| 5355 | // get the types sorted out |
| 5356 | int numCols = builder.getNumColumns(operand); |
| 5357 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5358 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 5359 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5360 | std::vector<spv::Id> results; |
| 5361 | |
| 5362 | // do each vector op |
| 5363 | for (int c = 0; c < numCols; ++c) { |
| 5364 | std::vector<unsigned int> indexes; |
| 5365 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5366 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 5367 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5368 | builder.addDecoration(destVec, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5369 | builder.addDecoration(destVec, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5370 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5374 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5375 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5376 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5377 | } |
| 5378 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5379 | // For converting integers where both the bitwidth and the signedness could |
| 5380 | // change, but only do the width change here. The caller is still responsible |
| 5381 | // for the signedness conversion. |
| 5382 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5383 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5384 | // Get the result type width, based on the type to convert to. |
| 5385 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5386 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5387 | case glslang::EOpConvInt16ToUint8: |
| 5388 | case glslang::EOpConvIntToUint8: |
| 5389 | case glslang::EOpConvInt64ToUint8: |
| 5390 | case glslang::EOpConvUint16ToInt8: |
| 5391 | case glslang::EOpConvUintToInt8: |
| 5392 | case glslang::EOpConvUint64ToInt8: |
| 5393 | width = 8; |
| 5394 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5395 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5396 | case glslang::EOpConvIntToUint16: |
| 5397 | case glslang::EOpConvInt64ToUint16: |
| 5398 | case glslang::EOpConvUint8ToInt16: |
| 5399 | case glslang::EOpConvUintToInt16: |
| 5400 | case glslang::EOpConvUint64ToInt16: |
| 5401 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5402 | break; |
| 5403 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5404 | case glslang::EOpConvInt16ToUint: |
| 5405 | case glslang::EOpConvInt64ToUint: |
| 5406 | case glslang::EOpConvUint8ToInt: |
| 5407 | case glslang::EOpConvUint16ToInt: |
| 5408 | case glslang::EOpConvUint64ToInt: |
| 5409 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5410 | break; |
| 5411 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5412 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5413 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5414 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5415 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5416 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5417 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5418 | break; |
| 5419 | |
| 5420 | default: |
| 5421 | assert(false && "Default missing"); |
| 5422 | break; |
| 5423 | } |
| 5424 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5425 | // Get the conversion operation and result type, |
| 5426 | // based on the target width, but the source type. |
| 5427 | spv::Id type = spv::NoType; |
| 5428 | spv::Op convOp = spv::OpNop; |
| 5429 | switch(op) { |
| 5430 | case glslang::EOpConvInt8ToUint16: |
| 5431 | case glslang::EOpConvInt8ToUint: |
| 5432 | case glslang::EOpConvInt8ToUint64: |
| 5433 | case glslang::EOpConvInt16ToUint8: |
| 5434 | case glslang::EOpConvInt16ToUint: |
| 5435 | case glslang::EOpConvInt16ToUint64: |
| 5436 | case glslang::EOpConvIntToUint8: |
| 5437 | case glslang::EOpConvIntToUint16: |
| 5438 | case glslang::EOpConvIntToUint64: |
| 5439 | case glslang::EOpConvInt64ToUint8: |
| 5440 | case glslang::EOpConvInt64ToUint16: |
| 5441 | case glslang::EOpConvInt64ToUint: |
| 5442 | convOp = spv::OpSConvert; |
| 5443 | type = builder.makeIntType(width); |
| 5444 | break; |
| 5445 | default: |
| 5446 | convOp = spv::OpUConvert; |
| 5447 | type = builder.makeUintType(width); |
| 5448 | break; |
| 5449 | } |
| 5450 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5451 | if (vectorSize > 0) |
| 5452 | type = builder.makeVectorType(type, vectorSize); |
| 5453 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5454 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5455 | } |
| 5456 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5457 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 5458 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5459 | { |
| 5460 | spv::Op convOp = spv::OpNop; |
| 5461 | spv::Id zero = 0; |
| 5462 | spv::Id one = 0; |
| 5463 | |
| 5464 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 5465 | |
| 5466 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5467 | case glslang::EOpConvInt8ToBool: |
| 5468 | case glslang::EOpConvUint8ToBool: |
| 5469 | zero = builder.makeUint8Constant(0); |
| 5470 | zero = makeSmearedConstant(zero, vectorSize); |
| 5471 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5472 | case glslang::EOpConvInt16ToBool: |
| 5473 | case glslang::EOpConvUint16ToBool: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5474 | zero = builder.makeUint16Constant(0); |
| 5475 | zero = makeSmearedConstant(zero, vectorSize); |
| 5476 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5477 | case glslang::EOpConvIntToBool: |
| 5478 | case glslang::EOpConvUintToBool: |
| 5479 | zero = builder.makeUintConstant(0); |
| 5480 | zero = makeSmearedConstant(zero, vectorSize); |
| 5481 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5482 | case glslang::EOpConvInt64ToBool: |
| 5483 | case glslang::EOpConvUint64ToBool: |
| 5484 | zero = builder.makeUint64Constant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5485 | zero = makeSmearedConstant(zero, vectorSize); |
| 5486 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5487 | |
| 5488 | case glslang::EOpConvFloatToBool: |
| 5489 | zero = builder.makeFloatConstant(0.0F); |
| 5490 | zero = makeSmearedConstant(zero, vectorSize); |
| 5491 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5492 | |
| 5493 | case glslang::EOpConvDoubleToBool: |
| 5494 | zero = builder.makeDoubleConstant(0.0); |
| 5495 | zero = makeSmearedConstant(zero, vectorSize); |
| 5496 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5497 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5498 | case glslang::EOpConvFloat16ToBool: |
| 5499 | zero = builder.makeFloat16Constant(0.0F); |
| 5500 | zero = makeSmearedConstant(zero, vectorSize); |
| 5501 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5502 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5503 | case glslang::EOpConvBoolToFloat: |
| 5504 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5505 | zero = builder.makeFloatConstant(0.0F); |
| 5506 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5507 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5508 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5509 | case glslang::EOpConvBoolToDouble: |
| 5510 | convOp = spv::OpSelect; |
| 5511 | zero = builder.makeDoubleConstant(0.0); |
| 5512 | one = builder.makeDoubleConstant(1.0); |
| 5513 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5514 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5515 | case glslang::EOpConvBoolToFloat16: |
| 5516 | convOp = spv::OpSelect; |
| 5517 | zero = builder.makeFloat16Constant(0.0F); |
| 5518 | one = builder.makeFloat16Constant(1.0F); |
| 5519 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5520 | |
| 5521 | case glslang::EOpConvBoolToInt8: |
| 5522 | zero = builder.makeInt8Constant(0); |
| 5523 | one = builder.makeInt8Constant(1); |
| 5524 | convOp = spv::OpSelect; |
| 5525 | break; |
| 5526 | |
| 5527 | case glslang::EOpConvBoolToUint8: |
| 5528 | zero = builder.makeUint8Constant(0); |
| 5529 | one = builder.makeUint8Constant(1); |
| 5530 | convOp = spv::OpSelect; |
| 5531 | break; |
| 5532 | |
| 5533 | case glslang::EOpConvBoolToInt16: |
| 5534 | zero = builder.makeInt16Constant(0); |
| 5535 | one = builder.makeInt16Constant(1); |
| 5536 | convOp = spv::OpSelect; |
| 5537 | break; |
| 5538 | |
| 5539 | case glslang::EOpConvBoolToUint16: |
| 5540 | zero = builder.makeUint16Constant(0); |
| 5541 | one = builder.makeUint16Constant(1); |
| 5542 | convOp = spv::OpSelect; |
| 5543 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5544 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5545 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5546 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5547 | if (op == glslang::EOpConvBoolToInt64) |
| 5548 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5549 | else |
| 5550 | zero = builder.makeIntConstant(0); |
| 5551 | |
| 5552 | if (op == glslang::EOpConvBoolToInt64) |
| 5553 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5554 | else |
| 5555 | one = builder.makeIntConstant(1); |
| 5556 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5557 | convOp = spv::OpSelect; |
| 5558 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5559 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5560 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5561 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5562 | if (op == glslang::EOpConvBoolToUint64) |
| 5563 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5564 | else |
| 5565 | zero = builder.makeUintConstant(0); |
| 5566 | |
| 5567 | if (op == glslang::EOpConvBoolToUint64) |
| 5568 | one = builder.makeUint64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5569 | else |
| 5570 | one = builder.makeUintConstant(1); |
| 5571 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5572 | convOp = spv::OpSelect; |
| 5573 | break; |
| 5574 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5575 | case glslang::EOpConvInt8ToFloat16: |
| 5576 | case glslang::EOpConvInt8ToFloat: |
| 5577 | case glslang::EOpConvInt8ToDouble: |
| 5578 | case glslang::EOpConvInt16ToFloat16: |
| 5579 | case glslang::EOpConvInt16ToFloat: |
| 5580 | case glslang::EOpConvInt16ToDouble: |
| 5581 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5582 | case glslang::EOpConvIntToFloat: |
| 5583 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5584 | case glslang::EOpConvInt64ToFloat: |
| 5585 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5586 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5587 | convOp = spv::OpConvertSToF; |
| 5588 | break; |
| 5589 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5590 | case glslang::EOpConvUint8ToFloat16: |
| 5591 | case glslang::EOpConvUint8ToFloat: |
| 5592 | case glslang::EOpConvUint8ToDouble: |
| 5593 | case glslang::EOpConvUint16ToFloat16: |
| 5594 | case glslang::EOpConvUint16ToFloat: |
| 5595 | case glslang::EOpConvUint16ToDouble: |
| 5596 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5597 | case glslang::EOpConvUintToFloat: |
| 5598 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5599 | case glslang::EOpConvUint64ToFloat: |
| 5600 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5601 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5602 | convOp = spv::OpConvertUToF; |
| 5603 | break; |
| 5604 | |
| 5605 | case glslang::EOpConvDoubleToFloat: |
| 5606 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5607 | case glslang::EOpConvDoubleToFloat16: |
| 5608 | case glslang::EOpConvFloat16ToDouble: |
| 5609 | case glslang::EOpConvFloatToFloat16: |
| 5610 | case glslang::EOpConvFloat16ToFloat: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5611 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 5612 | if (builder.isMatrixType(destType)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5613 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5614 | break; |
| 5615 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5616 | case glslang::EOpConvFloat16ToInt8: |
| 5617 | case glslang::EOpConvFloatToInt8: |
| 5618 | case glslang::EOpConvDoubleToInt8: |
| 5619 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5620 | case glslang::EOpConvFloatToInt16: |
| 5621 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5622 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5623 | case glslang::EOpConvFloatToInt: |
| 5624 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5625 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5626 | case glslang::EOpConvFloatToInt64: |
| 5627 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5628 | convOp = spv::OpConvertFToS; |
| 5629 | break; |
| 5630 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5631 | case glslang::EOpConvUint8ToInt8: |
| 5632 | case glslang::EOpConvInt8ToUint8: |
| 5633 | case glslang::EOpConvUint16ToInt16: |
| 5634 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5635 | case glslang::EOpConvUintToInt: |
| 5636 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5637 | case glslang::EOpConvUint64ToInt64: |
| 5638 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 5639 | if (builder.isInSpecConstCodeGenMode()) { |
| 5640 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5641 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 5642 | zero = builder.makeUint8Constant(0); |
| 5643 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5644 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5645 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 5646 | zero = builder.makeUint64Constant(0); |
| 5647 | } else { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5648 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5649 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 5650 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 5651 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5652 | // generating for OpSpecConstantOp instruction. |
| 5653 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5654 | } |
| 5655 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5656 | convOp = spv::OpBitcast; |
| 5657 | break; |
| 5658 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5659 | case glslang::EOpConvFloat16ToUint8: |
| 5660 | case glslang::EOpConvFloatToUint8: |
| 5661 | case glslang::EOpConvDoubleToUint8: |
| 5662 | case glslang::EOpConvFloat16ToUint16: |
| 5663 | case glslang::EOpConvFloatToUint16: |
| 5664 | case glslang::EOpConvDoubleToUint16: |
| 5665 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5666 | case glslang::EOpConvFloatToUint: |
| 5667 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5668 | case glslang::EOpConvFloatToUint64: |
| 5669 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5670 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5671 | convOp = spv::OpConvertFToU; |
| 5672 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5673 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5674 | case glslang::EOpConvInt8ToInt16: |
| 5675 | case glslang::EOpConvInt8ToInt: |
| 5676 | case glslang::EOpConvInt8ToInt64: |
| 5677 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5678 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5679 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5680 | case glslang::EOpConvIntToInt8: |
| 5681 | case glslang::EOpConvIntToInt16: |
| 5682 | case glslang::EOpConvIntToInt64: |
| 5683 | case glslang::EOpConvInt64ToInt8: |
| 5684 | case glslang::EOpConvInt64ToInt16: |
| 5685 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5686 | convOp = spv::OpSConvert; |
| 5687 | break; |
| 5688 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5689 | case glslang::EOpConvUint8ToUint16: |
| 5690 | case glslang::EOpConvUint8ToUint: |
| 5691 | case glslang::EOpConvUint8ToUint64: |
| 5692 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5693 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5694 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5695 | case glslang::EOpConvUintToUint8: |
| 5696 | case glslang::EOpConvUintToUint16: |
| 5697 | case glslang::EOpConvUintToUint64: |
| 5698 | case glslang::EOpConvUint64ToUint8: |
| 5699 | case glslang::EOpConvUint64ToUint16: |
| 5700 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5701 | convOp = spv::OpUConvert; |
| 5702 | break; |
| 5703 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5704 | case glslang::EOpConvInt8ToUint16: |
| 5705 | case glslang::EOpConvInt8ToUint: |
| 5706 | case glslang::EOpConvInt8ToUint64: |
| 5707 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5708 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5709 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5710 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5711 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5712 | case glslang::EOpConvIntToUint64: |
| 5713 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5714 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5715 | case glslang::EOpConvInt64ToUint: |
| 5716 | case glslang::EOpConvUint8ToInt16: |
| 5717 | case glslang::EOpConvUint8ToInt: |
| 5718 | case glslang::EOpConvUint8ToInt64: |
| 5719 | case glslang::EOpConvUint16ToInt8: |
| 5720 | case glslang::EOpConvUint16ToInt: |
| 5721 | case glslang::EOpConvUint16ToInt64: |
| 5722 | case glslang::EOpConvUintToInt8: |
| 5723 | case glslang::EOpConvUintToInt16: |
| 5724 | case glslang::EOpConvUintToInt64: |
| 5725 | case glslang::EOpConvUint64ToInt8: |
| 5726 | case glslang::EOpConvUint64ToInt16: |
| 5727 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5728 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5729 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5730 | |
| 5731 | if (builder.isInSpecConstCodeGenMode()) { |
| 5732 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5733 | switch(op) { |
| 5734 | case glslang::EOpConvInt16ToUint8: |
| 5735 | case glslang::EOpConvIntToUint8: |
| 5736 | case glslang::EOpConvInt64ToUint8: |
| 5737 | case glslang::EOpConvUint16ToInt8: |
| 5738 | case glslang::EOpConvUintToInt8: |
| 5739 | case glslang::EOpConvUint64ToInt8: |
| 5740 | zero = builder.makeUint8Constant(0); |
| 5741 | break; |
| 5742 | case glslang::EOpConvInt8ToUint16: |
| 5743 | case glslang::EOpConvIntToUint16: |
| 5744 | case glslang::EOpConvInt64ToUint16: |
| 5745 | case glslang::EOpConvUint8ToInt16: |
| 5746 | case glslang::EOpConvUintToInt16: |
| 5747 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5748 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5749 | break; |
| 5750 | case glslang::EOpConvInt8ToUint: |
| 5751 | case glslang::EOpConvInt16ToUint: |
| 5752 | case glslang::EOpConvInt64ToUint: |
| 5753 | case glslang::EOpConvUint8ToInt: |
| 5754 | case glslang::EOpConvUint16ToInt: |
| 5755 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5756 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5757 | break; |
| 5758 | case glslang::EOpConvInt8ToUint64: |
| 5759 | case glslang::EOpConvInt16ToUint64: |
| 5760 | case glslang::EOpConvIntToUint64: |
| 5761 | case glslang::EOpConvUint8ToInt64: |
| 5762 | case glslang::EOpConvUint16ToInt64: |
| 5763 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5764 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5765 | break; |
| 5766 | default: |
| 5767 | assert(false && "Default missing"); |
| 5768 | break; |
| 5769 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5770 | zero = makeSmearedConstant(zero, vectorSize); |
| 5771 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5772 | // generating for OpSpecConstantOp instruction. |
| 5773 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5774 | } |
| 5775 | // For normal run-time conversion instruction, use OpBitcast. |
| 5776 | convOp = spv::OpBitcast; |
| 5777 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5778 | default: |
| 5779 | break; |
| 5780 | } |
| 5781 | |
| 5782 | spv::Id result = 0; |
| 5783 | if (convOp == spv::OpNop) |
| 5784 | return result; |
| 5785 | |
| 5786 | if (convOp == spv::OpSelect) { |
| 5787 | zero = makeSmearedConstant(zero, vectorSize); |
| 5788 | one = makeSmearedConstant(one, vectorSize); |
| 5789 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 5790 | } else |
| 5791 | result = builder.createUnaryOp(convOp, destType, operand); |
| 5792 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5793 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5794 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5795 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 5799 | { |
| 5800 | if (vectorSize == 0) |
| 5801 | return constant; |
| 5802 | |
| 5803 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 5804 | std::vector<spv::Id> components; |
| 5805 | for (int c = 0; c < vectorSize; ++c) |
| 5806 | components.push_back(constant); |
| 5807 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 5808 | } |
| 5809 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5810 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5811 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5812 | { |
| 5813 | spv::Op opCode = spv::OpNop; |
| 5814 | |
| 5815 | switch (op) { |
| 5816 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5817 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5818 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5819 | opCode = spv::OpAtomicIAdd; |
| 5820 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5821 | case glslang::EOpAtomicCounterSubtract: |
| 5822 | opCode = spv::OpAtomicISub; |
| 5823 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5824 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5825 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5826 | case glslang::EOpAtomicCounterMin: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5827 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5828 | break; |
| 5829 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5830 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5831 | case glslang::EOpAtomicCounterMax: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5832 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5833 | break; |
| 5834 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5835 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5836 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5837 | opCode = spv::OpAtomicAnd; |
| 5838 | break; |
| 5839 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5840 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5841 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5842 | opCode = spv::OpAtomicOr; |
| 5843 | break; |
| 5844 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5845 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5846 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5847 | opCode = spv::OpAtomicXor; |
| 5848 | break; |
| 5849 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5850 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5851 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5852 | opCode = spv::OpAtomicExchange; |
| 5853 | break; |
| 5854 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5855 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5856 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5857 | opCode = spv::OpAtomicCompareExchange; |
| 5858 | break; |
| 5859 | case glslang::EOpAtomicCounterIncrement: |
| 5860 | opCode = spv::OpAtomicIIncrement; |
| 5861 | break; |
| 5862 | case glslang::EOpAtomicCounterDecrement: |
| 5863 | opCode = spv::OpAtomicIDecrement; |
| 5864 | break; |
| 5865 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5866 | case glslang::EOpImageAtomicLoad: |
| 5867 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5868 | opCode = spv::OpAtomicLoad; |
| 5869 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5870 | case glslang::EOpAtomicStore: |
| 5871 | case glslang::EOpImageAtomicStore: |
| 5872 | opCode = spv::OpAtomicStore; |
| 5873 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5874 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5875 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5876 | break; |
| 5877 | } |
| 5878 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5879 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 5880 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 5881 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5882 | // Sort out the operands |
| 5883 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5884 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 5885 | // - compare-exchange swaps the value and comparator |
| 5886 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5887 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5888 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 5889 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 5890 | spv::Id scopeId; |
| 5891 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 5892 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 5893 | } else { |
| 5894 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 5895 | } |
| 5896 | // semantics default to relaxed |
| 5897 | spv::Id semanticsId = builder.makeUintConstant(spv::MemorySemanticsMaskNone); |
| 5898 | spv::Id semanticsId2 = semanticsId; |
| 5899 | |
| 5900 | pointerId = operands[0]; |
| 5901 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 5902 | // no additional operands |
| 5903 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 5904 | compareId = operands[1]; |
| 5905 | valueId = operands[2]; |
| 5906 | if (operands.size() > 3) { |
| 5907 | scopeId = operands[3]; |
| 5908 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 5909 | semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 5910 | } |
| 5911 | } else if (opCode == spv::OpAtomicLoad) { |
| 5912 | if (operands.size() > 1) { |
| 5913 | scopeId = operands[1]; |
| 5914 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 5915 | } |
| 5916 | } else { |
| 5917 | // atomic store or RMW |
| 5918 | valueId = operands[1]; |
| 5919 | if (operands.size() > 2) { |
| 5920 | scopeId = operands[2]; |
| 5921 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 5922 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5923 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5924 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5925 | // Check for capabilities |
| 5926 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
| 5927 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 5928 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 5929 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5930 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5931 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 5932 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 5933 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5934 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5935 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 5936 | spvAtomicOperands.push_back(pointerId); |
| 5937 | spvAtomicOperands.push_back(scopeId); |
| 5938 | spvAtomicOperands.push_back(semanticsId); |
| 5939 | if (opCode == spv::OpAtomicCompareExchange) { |
| 5940 | spvAtomicOperands.push_back(semanticsId2); |
| 5941 | spvAtomicOperands.push_back(valueId); |
| 5942 | spvAtomicOperands.push_back(compareId); |
| 5943 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 5944 | spvAtomicOperands.push_back(valueId); |
| 5945 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5946 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5947 | if (opCode == spv::OpAtomicStore) { |
| 5948 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 5949 | return 0; |
| 5950 | } else { |
| 5951 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 5952 | |
| 5953 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 5954 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 5955 | if (op == glslang::EOpAtomicCounterDecrement) |
| 5956 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 5957 | |
| 5958 | return resultId; |
| 5959 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5960 | } |
| 5961 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5962 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5963 | 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] | 5964 | { |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 5965 | #ifdef AMD_EXTENSIONS |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5966 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5967 | bool isFloat = isTypeFloat(typeProxy); |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 5968 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5969 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5970 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5971 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5972 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 5973 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5974 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 5975 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5976 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 5977 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5978 | } else if (op == glslang::EOpAnyInvocation || |
| 5979 | op == glslang::EOpAllInvocations || |
| 5980 | op == glslang::EOpAllInvocationsEqual) { |
| 5981 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 5982 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5983 | } else { |
| 5984 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 5985 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 5986 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 5987 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5988 | op == glslang::EOpAddInvocationsNonUniform || |
| 5989 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 5990 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 5991 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 5992 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 5993 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 5994 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 5995 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 5996 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5997 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5998 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5999 | switch (op) { |
| 6000 | case glslang::EOpMinInvocations: |
| 6001 | case glslang::EOpMaxInvocations: |
| 6002 | case glslang::EOpAddInvocations: |
| 6003 | case glslang::EOpMinInvocationsNonUniform: |
| 6004 | case glslang::EOpMaxInvocationsNonUniform: |
| 6005 | case glslang::EOpAddInvocationsNonUniform: |
| 6006 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6007 | break; |
| 6008 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6009 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6010 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6011 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6012 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6013 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6014 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6015 | break; |
| 6016 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6017 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6018 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6019 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6020 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6021 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6022 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6023 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 6024 | default: |
| 6025 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6026 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6027 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6028 | spvGroupOperands.push_back(scope); |
| 6029 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6030 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6031 | spvGroupOperands.push_back(groupOp); |
| 6032 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6033 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6034 | } |
| 6035 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6036 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6037 | spv::IdImmediate op = { true, *opIt }; |
| 6038 | spvGroupOperands.push_back(op); |
| 6039 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6040 | |
| 6041 | switch (op) { |
| 6042 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6043 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6044 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6045 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6046 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6047 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6048 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6049 | opCode = spv::OpSubgroupAllEqualKHR; |
| 6050 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6051 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6052 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6053 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6054 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6055 | break; |
| 6056 | case glslang::EOpReadFirstInvocation: |
| 6057 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6058 | break; |
| 6059 | case glslang::EOpBallot: |
| 6060 | { |
| 6061 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6062 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6063 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6064 | // |
| 6065 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6066 | // |
| 6067 | spv::Id uintType = builder.makeUintType(32); |
| 6068 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6069 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6070 | |
| 6071 | std::vector<spv::Id> components; |
| 6072 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6073 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6074 | |
| 6075 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6076 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6077 | builder.createCompositeConstruct(uvec2Type, components)); |
| 6078 | } |
| 6079 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6080 | #ifdef AMD_EXTENSIONS |
| 6081 | case glslang::EOpMinInvocations: |
| 6082 | case glslang::EOpMaxInvocations: |
| 6083 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6084 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6085 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6086 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6087 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6088 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6089 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6090 | if (op == glslang::EOpMinInvocations || |
| 6091 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 6092 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6093 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6094 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6095 | else { |
| 6096 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6097 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6098 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6099 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6100 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6101 | } else if (op == glslang::EOpMaxInvocations || |
| 6102 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 6103 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6104 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6105 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6106 | else { |
| 6107 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6108 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6109 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6110 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6111 | } |
| 6112 | } else { |
| 6113 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6114 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6115 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6116 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6117 | } |
| 6118 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6119 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6120 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6121 | |
| 6122 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6123 | case glslang::EOpMinInvocationsNonUniform: |
| 6124 | case glslang::EOpMaxInvocationsNonUniform: |
| 6125 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6126 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6127 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6128 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6129 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6130 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6131 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6132 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6133 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6134 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6135 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6136 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6137 | else { |
| 6138 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6139 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6140 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6141 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6142 | } |
| 6143 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6144 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 6145 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6146 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6147 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6148 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6149 | else { |
| 6150 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6151 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6152 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6153 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6154 | } |
| 6155 | } |
| 6156 | else { |
| 6157 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6158 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6159 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6160 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6161 | } |
| 6162 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6163 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6164 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6165 | |
| 6166 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6167 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6168 | default: |
| 6169 | logger->missingFunctionality("invocation operation"); |
| 6170 | return spv::NoResult; |
| 6171 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6172 | |
| 6173 | assert(opCode != spv::OpNop); |
| 6174 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6175 | } |
| 6176 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6177 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6178 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 6179 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6180 | { |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6181 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6182 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6183 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6184 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6185 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6186 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 6187 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 6188 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6189 | #else |
| 6190 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6191 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6192 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 6193 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6194 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6195 | |
| 6196 | // Handle group invocation operations scalar by scalar. |
| 6197 | // The result type is the same type as the original type. |
| 6198 | // The algorithm is to: |
| 6199 | // - break the vector into scalars |
| 6200 | // - apply the operation to each scalar |
| 6201 | // - make a vector out the scalar results |
| 6202 | |
| 6203 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6204 | int numComponents = builder.getNumComponents(operands[0]); |
| 6205 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6206 | std::vector<spv::Id> results; |
| 6207 | |
| 6208 | // do each scalar op |
| 6209 | for (int comp = 0; comp < numComponents; ++comp) { |
| 6210 | std::vector<unsigned int> indexes; |
| 6211 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6212 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 6213 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6214 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 6215 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6216 | spv::IdImmediate operand = { true, operands[1] }; |
| 6217 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6218 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6219 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6220 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6221 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6222 | spv::IdImmediate operand = { true, operands[1] }; |
| 6223 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6224 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6225 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6226 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6227 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6228 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6229 | spvGroupOperands.push_back(scalar); |
| 6230 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6231 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6232 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6233 | } |
| 6234 | |
| 6235 | // put the pieces together |
| 6236 | return builder.createCompositeConstruct(typeId, results); |
| 6237 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6238 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6239 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6240 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 6241 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6242 | { |
| 6243 | // Add the required capabilities. |
| 6244 | switch (op) { |
| 6245 | case glslang::EOpSubgroupElect: |
| 6246 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6247 | break; |
| 6248 | case glslang::EOpSubgroupAll: |
| 6249 | case glslang::EOpSubgroupAny: |
| 6250 | case glslang::EOpSubgroupAllEqual: |
| 6251 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6252 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 6253 | break; |
| 6254 | case glslang::EOpSubgroupBroadcast: |
| 6255 | case glslang::EOpSubgroupBroadcastFirst: |
| 6256 | case glslang::EOpSubgroupBallot: |
| 6257 | case glslang::EOpSubgroupInverseBallot: |
| 6258 | case glslang::EOpSubgroupBallotBitExtract: |
| 6259 | case glslang::EOpSubgroupBallotBitCount: |
| 6260 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6261 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6262 | case glslang::EOpSubgroupBallotFindLSB: |
| 6263 | case glslang::EOpSubgroupBallotFindMSB: |
| 6264 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6265 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 6266 | break; |
| 6267 | case glslang::EOpSubgroupShuffle: |
| 6268 | case glslang::EOpSubgroupShuffleXor: |
| 6269 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6270 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 6271 | break; |
| 6272 | case glslang::EOpSubgroupShuffleUp: |
| 6273 | case glslang::EOpSubgroupShuffleDown: |
| 6274 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6275 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 6276 | break; |
| 6277 | case glslang::EOpSubgroupAdd: |
| 6278 | case glslang::EOpSubgroupMul: |
| 6279 | case glslang::EOpSubgroupMin: |
| 6280 | case glslang::EOpSubgroupMax: |
| 6281 | case glslang::EOpSubgroupAnd: |
| 6282 | case glslang::EOpSubgroupOr: |
| 6283 | case glslang::EOpSubgroupXor: |
| 6284 | case glslang::EOpSubgroupInclusiveAdd: |
| 6285 | case glslang::EOpSubgroupInclusiveMul: |
| 6286 | case glslang::EOpSubgroupInclusiveMin: |
| 6287 | case glslang::EOpSubgroupInclusiveMax: |
| 6288 | case glslang::EOpSubgroupInclusiveAnd: |
| 6289 | case glslang::EOpSubgroupInclusiveOr: |
| 6290 | case glslang::EOpSubgroupInclusiveXor: |
| 6291 | case glslang::EOpSubgroupExclusiveAdd: |
| 6292 | case glslang::EOpSubgroupExclusiveMul: |
| 6293 | case glslang::EOpSubgroupExclusiveMin: |
| 6294 | case glslang::EOpSubgroupExclusiveMax: |
| 6295 | case glslang::EOpSubgroupExclusiveAnd: |
| 6296 | case glslang::EOpSubgroupExclusiveOr: |
| 6297 | case glslang::EOpSubgroupExclusiveXor: |
| 6298 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6299 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 6300 | break; |
| 6301 | case glslang::EOpSubgroupClusteredAdd: |
| 6302 | case glslang::EOpSubgroupClusteredMul: |
| 6303 | case glslang::EOpSubgroupClusteredMin: |
| 6304 | case glslang::EOpSubgroupClusteredMax: |
| 6305 | case glslang::EOpSubgroupClusteredAnd: |
| 6306 | case glslang::EOpSubgroupClusteredOr: |
| 6307 | case glslang::EOpSubgroupClusteredXor: |
| 6308 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6309 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 6310 | break; |
| 6311 | case glslang::EOpSubgroupQuadBroadcast: |
| 6312 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6313 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6314 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 6315 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6316 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 6317 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6318 | #ifdef NV_EXTENSIONS |
| 6319 | case glslang::EOpSubgroupPartitionedAdd: |
| 6320 | case glslang::EOpSubgroupPartitionedMul: |
| 6321 | case glslang::EOpSubgroupPartitionedMin: |
| 6322 | case glslang::EOpSubgroupPartitionedMax: |
| 6323 | case glslang::EOpSubgroupPartitionedAnd: |
| 6324 | case glslang::EOpSubgroupPartitionedOr: |
| 6325 | case glslang::EOpSubgroupPartitionedXor: |
| 6326 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6327 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6328 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6329 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6330 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6331 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6332 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6333 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6334 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6335 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6336 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6337 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6338 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6339 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6340 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 6341 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 6342 | break; |
| 6343 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6344 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6345 | } |
| 6346 | |
| 6347 | const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
| 6348 | const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
| 6349 | const bool isBool = typeProxy == glslang::EbtBool; |
| 6350 | |
| 6351 | spv::Op opCode = spv::OpNop; |
| 6352 | |
| 6353 | // Figure out which opcode to use. |
| 6354 | switch (op) { |
| 6355 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 6356 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 6357 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 6358 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 6359 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 6360 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 6361 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 6362 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 6363 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 6364 | case glslang::EOpSubgroupBallotBitCount: |
| 6365 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6366 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 6367 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 6368 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 6369 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 6370 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 6371 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 6372 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 6373 | case glslang::EOpSubgroupAdd: |
| 6374 | case glslang::EOpSubgroupInclusiveAdd: |
| 6375 | case glslang::EOpSubgroupExclusiveAdd: |
| 6376 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6377 | #ifdef NV_EXTENSIONS |
| 6378 | case glslang::EOpSubgroupPartitionedAdd: |
| 6379 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6380 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6381 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6382 | if (isFloat) { |
| 6383 | opCode = spv::OpGroupNonUniformFAdd; |
| 6384 | } else { |
| 6385 | opCode = spv::OpGroupNonUniformIAdd; |
| 6386 | } |
| 6387 | break; |
| 6388 | case glslang::EOpSubgroupMul: |
| 6389 | case glslang::EOpSubgroupInclusiveMul: |
| 6390 | case glslang::EOpSubgroupExclusiveMul: |
| 6391 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6392 | #ifdef NV_EXTENSIONS |
| 6393 | case glslang::EOpSubgroupPartitionedMul: |
| 6394 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6395 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6396 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6397 | if (isFloat) { |
| 6398 | opCode = spv::OpGroupNonUniformFMul; |
| 6399 | } else { |
| 6400 | opCode = spv::OpGroupNonUniformIMul; |
| 6401 | } |
| 6402 | break; |
| 6403 | case glslang::EOpSubgroupMin: |
| 6404 | case glslang::EOpSubgroupInclusiveMin: |
| 6405 | case glslang::EOpSubgroupExclusiveMin: |
| 6406 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6407 | #ifdef NV_EXTENSIONS |
| 6408 | case glslang::EOpSubgroupPartitionedMin: |
| 6409 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6410 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6411 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6412 | if (isFloat) { |
| 6413 | opCode = spv::OpGroupNonUniformFMin; |
| 6414 | } else if (isUnsigned) { |
| 6415 | opCode = spv::OpGroupNonUniformUMin; |
| 6416 | } else { |
| 6417 | opCode = spv::OpGroupNonUniformSMin; |
| 6418 | } |
| 6419 | break; |
| 6420 | case glslang::EOpSubgroupMax: |
| 6421 | case glslang::EOpSubgroupInclusiveMax: |
| 6422 | case glslang::EOpSubgroupExclusiveMax: |
| 6423 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6424 | #ifdef NV_EXTENSIONS |
| 6425 | case glslang::EOpSubgroupPartitionedMax: |
| 6426 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6427 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6428 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6429 | if (isFloat) { |
| 6430 | opCode = spv::OpGroupNonUniformFMax; |
| 6431 | } else if (isUnsigned) { |
| 6432 | opCode = spv::OpGroupNonUniformUMax; |
| 6433 | } else { |
| 6434 | opCode = spv::OpGroupNonUniformSMax; |
| 6435 | } |
| 6436 | break; |
| 6437 | case glslang::EOpSubgroupAnd: |
| 6438 | case glslang::EOpSubgroupInclusiveAnd: |
| 6439 | case glslang::EOpSubgroupExclusiveAnd: |
| 6440 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6441 | #ifdef NV_EXTENSIONS |
| 6442 | case glslang::EOpSubgroupPartitionedAnd: |
| 6443 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6444 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6445 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6446 | if (isBool) { |
| 6447 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 6448 | } else { |
| 6449 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 6450 | } |
| 6451 | break; |
| 6452 | case glslang::EOpSubgroupOr: |
| 6453 | case glslang::EOpSubgroupInclusiveOr: |
| 6454 | case glslang::EOpSubgroupExclusiveOr: |
| 6455 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6456 | #ifdef NV_EXTENSIONS |
| 6457 | case glslang::EOpSubgroupPartitionedOr: |
| 6458 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6459 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6460 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6461 | if (isBool) { |
| 6462 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 6463 | } else { |
| 6464 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 6465 | } |
| 6466 | break; |
| 6467 | case glslang::EOpSubgroupXor: |
| 6468 | case glslang::EOpSubgroupInclusiveXor: |
| 6469 | case glslang::EOpSubgroupExclusiveXor: |
| 6470 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6471 | #ifdef NV_EXTENSIONS |
| 6472 | case glslang::EOpSubgroupPartitionedXor: |
| 6473 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6474 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6475 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6476 | if (isBool) { |
| 6477 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 6478 | } else { |
| 6479 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 6480 | } |
| 6481 | break; |
| 6482 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 6483 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6484 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6485 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 6486 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6487 | } |
| 6488 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6489 | // get the right Group Operation |
| 6490 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6491 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6492 | default: |
| 6493 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6494 | case glslang::EOpSubgroupBallotBitCount: |
| 6495 | case glslang::EOpSubgroupAdd: |
| 6496 | case glslang::EOpSubgroupMul: |
| 6497 | case glslang::EOpSubgroupMin: |
| 6498 | case glslang::EOpSubgroupMax: |
| 6499 | case glslang::EOpSubgroupAnd: |
| 6500 | case glslang::EOpSubgroupOr: |
| 6501 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6502 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6503 | break; |
| 6504 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6505 | case glslang::EOpSubgroupInclusiveAdd: |
| 6506 | case glslang::EOpSubgroupInclusiveMul: |
| 6507 | case glslang::EOpSubgroupInclusiveMin: |
| 6508 | case glslang::EOpSubgroupInclusiveMax: |
| 6509 | case glslang::EOpSubgroupInclusiveAnd: |
| 6510 | case glslang::EOpSubgroupInclusiveOr: |
| 6511 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6512 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6513 | break; |
| 6514 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6515 | case glslang::EOpSubgroupExclusiveAdd: |
| 6516 | case glslang::EOpSubgroupExclusiveMul: |
| 6517 | case glslang::EOpSubgroupExclusiveMin: |
| 6518 | case glslang::EOpSubgroupExclusiveMax: |
| 6519 | case glslang::EOpSubgroupExclusiveAnd: |
| 6520 | case glslang::EOpSubgroupExclusiveOr: |
| 6521 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6522 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6523 | break; |
| 6524 | case glslang::EOpSubgroupClusteredAdd: |
| 6525 | case glslang::EOpSubgroupClusteredMul: |
| 6526 | case glslang::EOpSubgroupClusteredMin: |
| 6527 | case glslang::EOpSubgroupClusteredMax: |
| 6528 | case glslang::EOpSubgroupClusteredAnd: |
| 6529 | case glslang::EOpSubgroupClusteredOr: |
| 6530 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6531 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6532 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6533 | #ifdef NV_EXTENSIONS |
| 6534 | case glslang::EOpSubgroupPartitionedAdd: |
| 6535 | case glslang::EOpSubgroupPartitionedMul: |
| 6536 | case glslang::EOpSubgroupPartitionedMin: |
| 6537 | case glslang::EOpSubgroupPartitionedMax: |
| 6538 | case glslang::EOpSubgroupPartitionedAnd: |
| 6539 | case glslang::EOpSubgroupPartitionedOr: |
| 6540 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6541 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6542 | break; |
| 6543 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6544 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6545 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6546 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6547 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6548 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6549 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6550 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6551 | break; |
| 6552 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6553 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6554 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6555 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6556 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6557 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6558 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6559 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6560 | break; |
| 6561 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6562 | } |
| 6563 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6564 | // build the instruction |
| 6565 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 6566 | |
| 6567 | // Every operation begins with the Execution Scope operand. |
| 6568 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6569 | spvGroupOperands.push_back(executionScope); |
| 6570 | |
| 6571 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 6572 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6573 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6574 | spvGroupOperands.push_back(groupOperand); |
| 6575 | } |
| 6576 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6577 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6578 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 6579 | spv::IdImmediate operand = { true, *opIt }; |
| 6580 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6581 | } |
| 6582 | |
| 6583 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6584 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6585 | switch (op) { |
| 6586 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6587 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 6588 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 6589 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 6590 | } |
| 6591 | if (directionId != spv::NoResult) { |
| 6592 | spv::IdImmediate direction = { true, directionId }; |
| 6593 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 6597 | } |
| 6598 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6599 | 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] | 6600 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6601 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6602 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6603 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6604 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6605 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6606 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 6607 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6608 | spv::Id typeId0 = 0; |
| 6609 | if (consumedOperands > 0) |
| 6610 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6611 | spv::Id typeId1 = 0; |
| 6612 | if (consumedOperands > 1) |
| 6613 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6614 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6615 | |
| 6616 | switch (op) { |
| 6617 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6618 | if (isFloat) |
| 6619 | libCall = spv::GLSLstd450FMin; |
| 6620 | else if (isUnsigned) |
| 6621 | libCall = spv::GLSLstd450UMin; |
| 6622 | else |
| 6623 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6624 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6625 | break; |
| 6626 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6627 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6628 | break; |
| 6629 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6630 | if (isFloat) |
| 6631 | libCall = spv::GLSLstd450FMax; |
| 6632 | else if (isUnsigned) |
| 6633 | libCall = spv::GLSLstd450UMax; |
| 6634 | else |
| 6635 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6636 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6637 | break; |
| 6638 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6639 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6640 | break; |
| 6641 | case glslang::EOpDot: |
| 6642 | opCode = spv::OpDot; |
| 6643 | break; |
| 6644 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6645 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6646 | break; |
| 6647 | |
| 6648 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6649 | if (isFloat) |
| 6650 | libCall = spv::GLSLstd450FClamp; |
| 6651 | else if (isUnsigned) |
| 6652 | libCall = spv::GLSLstd450UClamp; |
| 6653 | else |
| 6654 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6655 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 6656 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6657 | break; |
| 6658 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6659 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 6660 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6661 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6662 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6663 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6664 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6665 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6666 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6667 | break; |
| 6668 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6669 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6670 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6671 | break; |
| 6672 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6673 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6674 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 6675 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6676 | break; |
| 6677 | |
| 6678 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6679 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6680 | break; |
| 6681 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6682 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6683 | break; |
| 6684 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6685 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6686 | break; |
| 6687 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6688 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6689 | break; |
| 6690 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6691 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6692 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6693 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6694 | #ifdef AMD_EXTENSIONS |
| 6695 | if (typeProxy == glslang::EbtFloat16) |
| 6696 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6697 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6698 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 6699 | break; |
| 6700 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6701 | #ifdef AMD_EXTENSIONS |
| 6702 | if (typeProxy == glslang::EbtFloat16) |
| 6703 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6704 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6705 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 6706 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6707 | case glslang::EOpAddCarry: |
| 6708 | opCode = spv::OpIAddCarry; |
| 6709 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6710 | consumedOperands = 2; |
| 6711 | break; |
| 6712 | case glslang::EOpSubBorrow: |
| 6713 | opCode = spv::OpISubBorrow; |
| 6714 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6715 | consumedOperands = 2; |
| 6716 | break; |
| 6717 | case glslang::EOpUMulExtended: |
| 6718 | opCode = spv::OpUMulExtended; |
| 6719 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6720 | consumedOperands = 2; |
| 6721 | break; |
| 6722 | case glslang::EOpIMulExtended: |
| 6723 | opCode = spv::OpSMulExtended; |
| 6724 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6725 | consumedOperands = 2; |
| 6726 | break; |
| 6727 | case glslang::EOpBitfieldExtract: |
| 6728 | if (isUnsigned) |
| 6729 | opCode = spv::OpBitFieldUExtract; |
| 6730 | else |
| 6731 | opCode = spv::OpBitFieldSExtract; |
| 6732 | break; |
| 6733 | case glslang::EOpBitfieldInsert: |
| 6734 | opCode = spv::OpBitFieldInsert; |
| 6735 | break; |
| 6736 | |
| 6737 | case glslang::EOpFma: |
| 6738 | libCall = spv::GLSLstd450Fma; |
| 6739 | break; |
| 6740 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6741 | { |
| 6742 | libCall = spv::GLSLstd450FrexpStruct; |
| 6743 | assert(builder.isPointerType(typeId1)); |
| 6744 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6745 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 6746 | #ifdef AMD_EXTENSIONS |
| 6747 | if (width == 16) |
| 6748 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 6749 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 6750 | #endif |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6751 | if (builder.getNumComponents(operands[0]) == 1) |
| 6752 | frexpIntType = builder.makeIntegerType(width, true); |
| 6753 | else |
| 6754 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 6755 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 6756 | consumedOperands = 1; |
| 6757 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6758 | break; |
| 6759 | case glslang::EOpLdexp: |
| 6760 | libCall = spv::GLSLstd450Ldexp; |
| 6761 | break; |
| 6762 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6763 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6764 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6765 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6766 | case glslang::EOpSubgroupBroadcast: |
| 6767 | case glslang::EOpSubgroupBallotBitExtract: |
| 6768 | case glslang::EOpSubgroupShuffle: |
| 6769 | case glslang::EOpSubgroupShuffleXor: |
| 6770 | case glslang::EOpSubgroupShuffleUp: |
| 6771 | case glslang::EOpSubgroupShuffleDown: |
| 6772 | case glslang::EOpSubgroupClusteredAdd: |
| 6773 | case glslang::EOpSubgroupClusteredMul: |
| 6774 | case glslang::EOpSubgroupClusteredMin: |
| 6775 | case glslang::EOpSubgroupClusteredMax: |
| 6776 | case glslang::EOpSubgroupClusteredAnd: |
| 6777 | case glslang::EOpSubgroupClusteredOr: |
| 6778 | case glslang::EOpSubgroupClusteredXor: |
| 6779 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6780 | #ifdef NV_EXTENSIONS |
| 6781 | case glslang::EOpSubgroupPartitionedAdd: |
| 6782 | case glslang::EOpSubgroupPartitionedMul: |
| 6783 | case glslang::EOpSubgroupPartitionedMin: |
| 6784 | case glslang::EOpSubgroupPartitionedMax: |
| 6785 | case glslang::EOpSubgroupPartitionedAnd: |
| 6786 | case glslang::EOpSubgroupPartitionedOr: |
| 6787 | case glslang::EOpSubgroupPartitionedXor: |
| 6788 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6789 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6790 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6791 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6792 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6793 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6794 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6795 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6796 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6797 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6798 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6799 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6800 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6801 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6802 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6803 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 6804 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6805 | #ifdef AMD_EXTENSIONS |
| 6806 | case glslang::EOpSwizzleInvocations: |
| 6807 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6808 | libCall = spv::SwizzleInvocationsAMD; |
| 6809 | break; |
| 6810 | case glslang::EOpSwizzleInvocationsMasked: |
| 6811 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6812 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 6813 | break; |
| 6814 | case glslang::EOpWriteInvocation: |
| 6815 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6816 | libCall = spv::WriteInvocationAMD; |
| 6817 | break; |
| 6818 | |
| 6819 | case glslang::EOpMin3: |
| 6820 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6821 | if (isFloat) |
| 6822 | libCall = spv::FMin3AMD; |
| 6823 | else { |
| 6824 | if (isUnsigned) |
| 6825 | libCall = spv::UMin3AMD; |
| 6826 | else |
| 6827 | libCall = spv::SMin3AMD; |
| 6828 | } |
| 6829 | break; |
| 6830 | case glslang::EOpMax3: |
| 6831 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6832 | if (isFloat) |
| 6833 | libCall = spv::FMax3AMD; |
| 6834 | else { |
| 6835 | if (isUnsigned) |
| 6836 | libCall = spv::UMax3AMD; |
| 6837 | else |
| 6838 | libCall = spv::SMax3AMD; |
| 6839 | } |
| 6840 | break; |
| 6841 | case glslang::EOpMid3: |
| 6842 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6843 | if (isFloat) |
| 6844 | libCall = spv::FMid3AMD; |
| 6845 | else { |
| 6846 | if (isUnsigned) |
| 6847 | libCall = spv::UMid3AMD; |
| 6848 | else |
| 6849 | libCall = spv::SMid3AMD; |
| 6850 | } |
| 6851 | break; |
| 6852 | |
| 6853 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6854 | if (typeProxy == glslang::EbtFloat16) |
| 6855 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6856 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 6857 | libCall = spv::InterpolateAtVertexAMD; |
| 6858 | break; |
| 6859 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6860 | case glslang::EOpBarrier: |
| 6861 | { |
| 6862 | // This is for the extended controlBarrier function, with four operands. |
| 6863 | // The unextended barrier() goes through createNoArgOperation. |
| 6864 | assert(operands.size() == 4); |
| 6865 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 6866 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 6867 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 6868 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 6869 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 6870 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6871 | } |
| 6872 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) { |
| 6873 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6874 | } |
| 6875 | return 0; |
| 6876 | } |
| 6877 | break; |
| 6878 | case glslang::EOpMemoryBarrier: |
| 6879 | { |
| 6880 | // This is for the extended memoryBarrier function, with three operands. |
| 6881 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 6882 | assert(operands.size() == 3); |
| 6883 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 6884 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 6885 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 6886 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 6887 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6888 | } |
| 6889 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 6890 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6891 | } |
| 6892 | return 0; |
| 6893 | } |
| 6894 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 6895 | |
| 6896 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 6897 | case glslang::EOpReportIntersectionNV: |
| 6898 | { |
| 6899 | typeId = builder.makeBoolType(); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 6900 | opCode = spv::OpReportIntersectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 6901 | } |
| 6902 | break; |
| 6903 | case glslang::EOpTraceNV: |
| 6904 | { |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 6905 | builder.createNoResultOp(spv::OpTraceNV, operands); |
| 6906 | return 0; |
| 6907 | } |
| 6908 | break; |
| 6909 | case glslang::EOpExecuteCallableNV: |
| 6910 | { |
| 6911 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 6912 | return 0; |
| 6913 | } |
| 6914 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 6915 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 6916 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 6917 | return 0; |
| 6918 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6919 | default: |
| 6920 | return 0; |
| 6921 | } |
| 6922 | |
| 6923 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 6924 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 6925 | // Use an extended instruction from the standard library. |
| 6926 | // Construct the call arguments, without modifying the original operands vector. |
| 6927 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 6928 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6929 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 6930 | } else if (opCode == spv::OpDot && !isFloat) { |
| 6931 | // int dot(int, int) |
| 6932 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 6933 | const int componentCount = builder.getNumComponents(operands[0]); |
| 6934 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 6935 | builder.setPrecision(mulOp, precision); |
| 6936 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 6937 | for (int i = 1; i < componentCount; ++i) { |
| 6938 | builder.setPrecision(id, precision); |
| 6939 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(operands[0], typeId, i)); |
| 6940 | } |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 6941 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6942 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6943 | case 0: |
| 6944 | // should all be handled by visitAggregate and createNoArgOperation |
| 6945 | assert(0); |
| 6946 | return 0; |
| 6947 | case 1: |
| 6948 | // should all be handled by createUnaryOperation |
| 6949 | assert(0); |
| 6950 | return 0; |
| 6951 | case 2: |
| 6952 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 6953 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6954 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6955 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 6956 | assert(consumedOperands == operands.size()); |
| 6957 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6958 | break; |
| 6959 | } |
| 6960 | } |
| 6961 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6962 | // Decode the return types that were structures |
| 6963 | switch (op) { |
| 6964 | case glslang::EOpAddCarry: |
| 6965 | case glslang::EOpSubBorrow: |
| 6966 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 6967 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 6968 | break; |
| 6969 | case glslang::EOpUMulExtended: |
| 6970 | case glslang::EOpIMulExtended: |
| 6971 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 6972 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 6973 | break; |
| 6974 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6975 | { |
| 6976 | assert(operands.size() == 2); |
| 6977 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 6978 | // "exp" is floating-point type (from HLSL intrinsic) |
| 6979 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 6980 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 6981 | builder.createStore(member1, operands[1]); |
| 6982 | } else |
| 6983 | // "exp" is integer type (from GLSL built-in function) |
| 6984 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 6985 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 6986 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6987 | break; |
| 6988 | default: |
| 6989 | break; |
| 6990 | } |
| 6991 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 6992 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6993 | } |
| 6994 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6995 | // Intrinsics with no arguments (or no return value, and no precision). |
| 6996 | 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] | 6997 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6998 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 6999 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7000 | |
| 7001 | switch (op) { |
| 7002 | case glslang::EOpEmitVertex: |
| 7003 | builder.createNoResultOp(spv::OpEmitVertex); |
| 7004 | return 0; |
| 7005 | case glslang::EOpEndPrimitive: |
| 7006 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 7007 | return 0; |
| 7008 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7009 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7010 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7011 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7012 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7013 | spv::MemorySemanticsAcquireReleaseMask); |
| 7014 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7015 | } else { |
| 7016 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 7017 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7018 | } else { |
| 7019 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7020 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7021 | spv::MemorySemanticsAcquireReleaseMask); |
| 7022 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7023 | return 0; |
| 7024 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7025 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 7026 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7027 | return 0; |
| 7028 | case glslang::EOpMemoryBarrierAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7029 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 7030 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7031 | return 0; |
| 7032 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7033 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 7034 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7035 | return 0; |
| 7036 | case glslang::EOpMemoryBarrierImage: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7037 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 7038 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7039 | return 0; |
| 7040 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7041 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7042 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7043 | return 0; |
| 7044 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7045 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 7046 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7047 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7048 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7049 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7050 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7051 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7052 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7053 | case glslang::EOpDeviceMemoryBarrier: |
| 7054 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7055 | spv::MemorySemanticsImageMemoryMask | |
| 7056 | spv::MemorySemanticsAcquireReleaseMask); |
| 7057 | return 0; |
| 7058 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 7059 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7060 | spv::MemorySemanticsImageMemoryMask | |
| 7061 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7062 | return 0; |
| 7063 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7064 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7065 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7066 | return 0; |
| 7067 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7068 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7069 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7070 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7071 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7072 | case glslang::EOpSubgroupBarrier: |
| 7073 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7074 | spv::MemorySemanticsAcquireReleaseMask); |
| 7075 | return spv::NoResult; |
| 7076 | case glslang::EOpSubgroupMemoryBarrier: |
| 7077 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7078 | spv::MemorySemanticsAcquireReleaseMask); |
| 7079 | return spv::NoResult; |
| 7080 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 7081 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 7082 | spv::MemorySemanticsAcquireReleaseMask); |
| 7083 | return spv::NoResult; |
| 7084 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 7085 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 7086 | spv::MemorySemanticsAcquireReleaseMask); |
| 7087 | return spv::NoResult; |
| 7088 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 7089 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7090 | spv::MemorySemanticsAcquireReleaseMask); |
| 7091 | return spv::NoResult; |
| 7092 | case glslang::EOpSubgroupElect: { |
| 7093 | std::vector<spv::Id> operands; |
| 7094 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 7095 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7096 | #ifdef AMD_EXTENSIONS |
| 7097 | case glslang::EOpTime: |
| 7098 | { |
| 7099 | std::vector<spv::Id> args; // Dummy arguments |
| 7100 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 7101 | return builder.setPrecision(id, precision); |
| 7102 | } |
| 7103 | #endif |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7104 | #ifdef NV_EXTENSIONS |
| 7105 | case glslang::EOpIgnoreIntersectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7106 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7107 | return 0; |
| 7108 | case glslang::EOpTerminateRayNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7109 | builder.createNoResultOp(spv::OpTerminateRayNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7110 | return 0; |
| 7111 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7112 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7113 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7114 | return 0; |
| 7115 | } |
| 7116 | } |
| 7117 | |
| 7118 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 7119 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 7120 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7121 | spv::Id id; |
| 7122 | if (symbolValues.end() != iter) { |
| 7123 | id = iter->second; |
| 7124 | return id; |
| 7125 | } |
| 7126 | |
| 7127 | // it was not found, create it |
| 7128 | id = createSpvVariable(symbol); |
| 7129 | symbolValues[symbol->getId()] = id; |
| 7130 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 7131 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7132 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 7133 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 7134 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7135 | #ifdef NV_EXTENSIONS |
| 7136 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
| 7137 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7138 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7139 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7140 | if (symbol->getQualifier().hasIndex()) |
| 7141 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 7142 | if (symbol->getQualifier().hasComponent()) |
| 7143 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 7144 | // atomic counters use this: |
| 7145 | if (symbol->getQualifier().hasOffset()) |
| 7146 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7147 | } |
| 7148 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 7149 | if (symbol->getQualifier().hasLocation()) |
| 7150 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7151 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 7152 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7153 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7154 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7155 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7156 | if (symbol->getQualifier().hasSet()) |
| 7157 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7158 | else if (IsDescriptorResource(symbol->getType())) { |
| 7159 | // default to 0 |
| 7160 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 7161 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7162 | if (symbol->getQualifier().hasBinding()) |
| 7163 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
Jeff Bolz | 0a93cfb | 2018-12-11 20:53:59 -0600 | [diff] [blame] | 7164 | else if (IsDescriptorResource(symbol->getType())) { |
| 7165 | // default to 0 |
| 7166 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 7167 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7168 | if (symbol->getQualifier().hasAttachment()) |
| 7169 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7170 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7171 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7172 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7173 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7174 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 7175 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 7176 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 7177 | } |
| 7178 | if (symbol->getQualifier().hasXfbOffset()) |
| 7179 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7180 | } |
| 7181 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7182 | if (symbol->getType().isImage()) { |
| 7183 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7184 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7185 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7186 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7187 | } |
| 7188 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7189 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 7190 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 7191 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7192 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7193 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 7194 | // nonuniform |
| 7195 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 7196 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 7197 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7198 | if (builtIn == spv::BuiltInSampleMask) { |
| 7199 | spv::Decoration decoration; |
| 7200 | // GL_NV_sample_mask_override_coverage extension |
| 7201 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7202 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7203 | else |
| 7204 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7205 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7206 | if (decoration != spv::DecorationMax) { |
| 7207 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 7208 | } |
| 7209 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7210 | else if (builtIn == spv::BuiltInLayer) { |
| 7211 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7212 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7213 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7214 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 7215 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 7216 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7217 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7218 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 7219 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7220 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 7221 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 7222 | } |
| 7223 | } |
| 7224 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7225 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7226 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7227 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7228 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 7229 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 7230 | if (symbol->getQualifier().pervertexNV) { |
| 7231 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 7232 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 7233 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 7234 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7235 | #endif |
| 7236 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7237 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 7238 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 7239 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 7240 | symbol->getType().getQualifier().semanticName); |
| 7241 | } |
| 7242 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7243 | return id; |
| 7244 | } |
| 7245 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7246 | #ifdef NV_EXTENSIONS |
| 7247 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 7248 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 7249 | { |
| 7250 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7251 | if (qualifier.perPrimitiveNV) { |
| 7252 | // Need to add capability/extension for fragment shader. |
| 7253 | // Mesh shader already adds this by default. |
| 7254 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7255 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7256 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7257 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7258 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7259 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7260 | if (qualifier.perViewNV) |
| 7261 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 7262 | if (qualifier.perTaskNV) |
| 7263 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 7264 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7265 | if (qualifier.perPrimitiveNV) { |
| 7266 | // Need to add capability/extension for fragment shader. |
| 7267 | // Mesh shader already adds this by default. |
| 7268 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7269 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7270 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7271 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7272 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7273 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7274 | if (qualifier.perViewNV) |
| 7275 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 7276 | if (qualifier.perTaskNV) |
| 7277 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 7278 | } |
| 7279 | } |
| 7280 | #endif |
| 7281 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7282 | // 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] | 7283 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7284 | // |
| 7285 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 7286 | // |
| 7287 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 7288 | // regular constants, which themselves are trees that createSpvConstant() |
| 7289 | // recursively walks. So, this function walks the "top" of the tree: |
| 7290 | // - emit specialization constant-building instructions for specConstant |
| 7291 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7292 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7293 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 7294 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7295 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7296 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7297 | if (! node.getQualifier().specConstant) { |
| 7298 | // hand off to the non-spec-constant path |
| 7299 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 7300 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7301 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7302 | nextConst, false); |
| 7303 | } |
| 7304 | |
| 7305 | // We now know we have a specialization constant to build |
| 7306 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 7307 | // 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] | 7308 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 7309 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 7310 | std::vector<spv::Id> dimConstId; |
| 7311 | for (int dim = 0; dim < 3; ++dim) { |
| 7312 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 7313 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7314 | if (specConst) { |
| 7315 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 7316 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 7317 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7318 | } |
| 7319 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 7320 | } |
| 7321 | |
| 7322 | // An AST node labelled as specialization constant should be a symbol node. |
| 7323 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 7324 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7325 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7326 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 7327 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 7328 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 7329 | // will set the builder into spec constant op instruction generating mode. |
| 7330 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7331 | result = accessChainLoad(sub_tree->getType()); |
| 7332 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7333 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7334 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7335 | } else { |
| 7336 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7337 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7338 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7339 | builder.addName(result, sn->getName().c_str()); |
| 7340 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7341 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7342 | |
| 7343 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 7344 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7345 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7346 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7347 | } |
| 7348 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7349 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 7350 | // build the aggregate SPIR-V constant. |
| 7351 | // |
| 7352 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 7353 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 7354 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7355 | 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] | 7356 | { |
| 7357 | // vector of constants for SPIR-V |
| 7358 | std::vector<spv::Id> spvConsts; |
| 7359 | |
| 7360 | // Type is used for struct and array constants |
| 7361 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 7362 | |
| 7363 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7364 | glslang::TType elementType(glslangType, 0); |
| 7365 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7366 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7367 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7368 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7369 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7370 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7371 | } else if (glslangType.getStruct()) { |
| 7372 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 7373 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7374 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 7375 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7376 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 7377 | bool zero = nextConst >= consts.size(); |
| 7378 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7379 | case glslang::EbtInt8: |
| 7380 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 7381 | break; |
| 7382 | case glslang::EbtUint8: |
| 7383 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 7384 | break; |
| 7385 | case glslang::EbtInt16: |
| 7386 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 7387 | break; |
| 7388 | case glslang::EbtUint16: |
| 7389 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 7390 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7391 | case glslang::EbtInt: |
| 7392 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 7393 | break; |
| 7394 | case glslang::EbtUint: |
| 7395 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 7396 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7397 | case glslang::EbtInt64: |
| 7398 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 7399 | break; |
| 7400 | case glslang::EbtUint64: |
| 7401 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 7402 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7403 | case glslang::EbtFloat: |
| 7404 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7405 | break; |
| 7406 | case glslang::EbtDouble: |
| 7407 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 7408 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7409 | case glslang::EbtFloat16: |
| 7410 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7411 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7412 | case glslang::EbtBool: |
| 7413 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 7414 | break; |
| 7415 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7416 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7417 | break; |
| 7418 | } |
| 7419 | ++nextConst; |
| 7420 | } |
| 7421 | } else { |
| 7422 | // we have a non-aggregate (scalar) constant |
| 7423 | bool zero = nextConst >= consts.size(); |
| 7424 | spv::Id scalar = 0; |
| 7425 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7426 | case glslang::EbtInt8: |
| 7427 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 7428 | break; |
| 7429 | case glslang::EbtUint8: |
| 7430 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 7431 | break; |
| 7432 | case glslang::EbtInt16: |
| 7433 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 7434 | break; |
| 7435 | case glslang::EbtUint16: |
| 7436 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 7437 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7438 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7439 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7440 | break; |
| 7441 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7442 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7443 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7444 | case glslang::EbtInt64: |
| 7445 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 7446 | break; |
| 7447 | case glslang::EbtUint64: |
| 7448 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7449 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7450 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7451 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7452 | break; |
| 7453 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7454 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7455 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7456 | case glslang::EbtFloat16: |
| 7457 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 7458 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7459 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7460 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7461 | break; |
| 7462 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7463 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7464 | break; |
| 7465 | } |
| 7466 | ++nextConst; |
| 7467 | return scalar; |
| 7468 | } |
| 7469 | |
| 7470 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 7471 | } |
| 7472 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7473 | // Return true if the node is a constant or symbol whose reading has no |
| 7474 | // non-trivial observable cost or effect. |
| 7475 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 7476 | { |
| 7477 | // don't know what this is |
| 7478 | if (node == nullptr) |
| 7479 | return false; |
| 7480 | |
| 7481 | // a constant is safe |
| 7482 | if (node->getAsConstantUnion() != nullptr) |
| 7483 | return true; |
| 7484 | |
| 7485 | // not a symbol means non-trivial |
| 7486 | if (node->getAsSymbolNode() == nullptr) |
| 7487 | return false; |
| 7488 | |
| 7489 | // a symbol, depends on what's being read |
| 7490 | switch (node->getType().getQualifier().storage) { |
| 7491 | case glslang::EvqTemporary: |
| 7492 | case glslang::EvqGlobal: |
| 7493 | case glslang::EvqIn: |
| 7494 | case glslang::EvqInOut: |
| 7495 | case glslang::EvqConst: |
| 7496 | case glslang::EvqConstReadOnly: |
| 7497 | case glslang::EvqUniform: |
| 7498 | return true; |
| 7499 | default: |
| 7500 | return false; |
| 7501 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 7502 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7503 | |
| 7504 | // 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] | 7505 | // 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] | 7506 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7507 | // Return true if trivial. |
| 7508 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 7509 | { |
| 7510 | if (node == nullptr) |
| 7511 | return false; |
| 7512 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 7513 | // count non scalars as trivial, as well as anything coming from HLSL |
| 7514 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 7515 | return true; |
| 7516 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7517 | // symbols and constants are trivial |
| 7518 | if (isTrivialLeaf(node)) |
| 7519 | return true; |
| 7520 | |
| 7521 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 7522 | |
| 7523 | // not a simple operation |
| 7524 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 7525 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 7526 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 7527 | return false; |
| 7528 | |
| 7529 | // not on leaf nodes |
| 7530 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 7531 | return false; |
| 7532 | |
| 7533 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 7534 | return false; |
| 7535 | } |
| 7536 | |
| 7537 | switch (node->getAsOperator()->getOp()) { |
| 7538 | case glslang::EOpLogicalNot: |
| 7539 | case glslang::EOpConvIntToBool: |
| 7540 | case glslang::EOpConvUintToBool: |
| 7541 | case glslang::EOpConvFloatToBool: |
| 7542 | case glslang::EOpConvDoubleToBool: |
| 7543 | case glslang::EOpEqual: |
| 7544 | case glslang::EOpNotEqual: |
| 7545 | case glslang::EOpLessThan: |
| 7546 | case glslang::EOpGreaterThan: |
| 7547 | case glslang::EOpLessThanEqual: |
| 7548 | case glslang::EOpGreaterThanEqual: |
| 7549 | case glslang::EOpIndexDirect: |
| 7550 | case glslang::EOpIndexDirectStruct: |
| 7551 | case glslang::EOpLogicalXor: |
| 7552 | case glslang::EOpAny: |
| 7553 | case glslang::EOpAll: |
| 7554 | return true; |
| 7555 | default: |
| 7556 | return false; |
| 7557 | } |
| 7558 | } |
| 7559 | |
| 7560 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 7561 | // the left side is true (for &&) or false (for ||). |
| 7562 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 7563 | { |
| 7564 | spv::Id boolTypeId = builder.makeBoolType(); |
| 7565 | |
| 7566 | // emit left operand |
| 7567 | builder.clearAccessChain(); |
| 7568 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 7569 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7570 | |
| 7571 | // Operands to accumulate OpPhi operands |
| 7572 | std::vector<spv::Id> phiOperands; |
| 7573 | // accumulate left operand's phi information |
| 7574 | phiOperands.push_back(leftId); |
| 7575 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 7576 | |
| 7577 | // Make the two kinds of operation symmetric with a "!" |
| 7578 | // || => emit "if (! left) result = right" |
| 7579 | // && => emit "if ( left) result = right" |
| 7580 | // |
| 7581 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 7582 | // to 'builder' to have an "else" without an "then" |
| 7583 | if (op == glslang::EOpLogicalOr) |
| 7584 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 7585 | |
| 7586 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 7587 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7588 | |
| 7589 | // emit right operand as the "then" part of the "if" |
| 7590 | builder.clearAccessChain(); |
| 7591 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 7592 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7593 | |
| 7594 | // accumulate left operand's phi information |
| 7595 | phiOperands.push_back(rightId); |
| 7596 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 7597 | |
| 7598 | // finish the "if" |
| 7599 | ifBuilder.makeEndIf(); |
| 7600 | |
| 7601 | // phi together the two results |
| 7602 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 7603 | } |
| 7604 | |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 7605 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7606 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 7607 | // Import this set if it has not been imported yet. |
| 7608 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 7609 | { |
| 7610 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 7611 | return extBuiltinMap[name]; |
| 7612 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7613 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7614 | spv::Id extBuiltins = builder.import(name); |
| 7615 | extBuiltinMap[name] = extBuiltins; |
| 7616 | return extBuiltins; |
| 7617 | } |
| 7618 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 7619 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7620 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7621 | }; // end anonymous namespace |
| 7622 | |
| 7623 | namespace glslang { |
| 7624 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7625 | void GetSpirvVersion(std::string& version) |
| 7626 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 7627 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 7628 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7629 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7630 | version = buf; |
| 7631 | } |
| 7632 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 7633 | // For low-order part of the generator's magic number. Bump up |
| 7634 | // when there is a change in the style (e.g., if SSA form changes, |
| 7635 | // or a different instruction sequence to do something gets used). |
| 7636 | int GetSpirvGeneratorVersion() |
| 7637 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 7638 | // return 1; // start |
| 7639 | // 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] | 7640 | // 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] | 7641 | // 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] | 7642 | // 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] | 7643 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 7644 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
| 7645 | 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] | 7646 | } |
| 7647 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7648 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7649 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7650 | { |
| 7651 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7652 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 7653 | if (out.fail()) |
| 7654 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7655 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 7656 | unsigned int word = spirv[i]; |
| 7657 | out.write((const char*)&word, 4); |
| 7658 | } |
| 7659 | out.close(); |
| 7660 | } |
| 7661 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7662 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 7663 | 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] | 7664 | { |
| 7665 | std::ofstream out; |
| 7666 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 7667 | if (out.fail()) |
| 7668 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 7669 | out << "\t// " << |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7670 | GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL << |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 7671 | std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 7672 | if (varName != nullptr) { |
| 7673 | out << "\t #pragma once" << std::endl; |
| 7674 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 7675 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7676 | const int WORDS_PER_LINE = 8; |
| 7677 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 7678 | out << "\t"; |
| 7679 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 7680 | const unsigned int word = spirv[i + j]; |
| 7681 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 7682 | if (i + j + 1 < (int)spirv.size()) { |
| 7683 | out << ","; |
| 7684 | } |
| 7685 | } |
| 7686 | out << std::endl; |
| 7687 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 7688 | if (varName != nullptr) { |
| 7689 | out << "};"; |
| 7690 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7691 | out.close(); |
| 7692 | } |
| 7693 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7694 | // |
| 7695 | // Set up the glslang traversal |
| 7696 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7697 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7698 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7699 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7700 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 7701 | } |
| 7702 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7703 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7704 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 7705 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7706 | TIntermNode* root = intermediate.getTreeRoot(); |
| 7707 | |
| 7708 | if (root == 0) |
| 7709 | return; |
| 7710 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7711 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7712 | if (options == nullptr) |
| 7713 | options = &defaultOptions; |
| 7714 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7715 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7716 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 7717 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7718 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 7719 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7720 | it.dumpSpv(spirv); |
| 7721 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 7722 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 7723 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 7724 | // eg. forward and remove memory writes of opaque types. |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7725 | if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer) |
John Kessenich | e7df8e0 | 2018-08-22 17:12:46 -0600 | [diff] [blame] | 7726 | SpirvToolsLegalize(intermediate, spirv, logger, options); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7727 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7728 | if (options->validate) |
| 7729 | SpirvToolsValidate(intermediate, spirv, logger); |
| 7730 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7731 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7732 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7733 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 7734 | #endif |
| 7735 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7736 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7737 | } |
| 7738 | |
| 7739 | }; // end namespace glslang |