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); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 148 | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
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&, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 150 | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
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 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 214 | unsigned int getBufferReferenceAlignment(const glslang::TType &type) const { |
| 215 | if (type.getBasicType() == glslang::EbtReference) { |
| 216 | return type.getReferentType()->getQualifier().hasBufferReferenceAlign() ? |
| 217 | (1u << type.getReferentType()->getQualifier().layoutBufferReferenceAlign) : 16u; |
| 218 | } else { |
| 219 | return 0; |
| 220 | } |
| 221 | } |
| 222 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 223 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 224 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 225 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 226 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 227 | int sequenceDepth; |
| 228 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 229 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 230 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 231 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 232 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 233 | bool inEntryPoint; |
| 234 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 235 | 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] | 236 | 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] | 237 | const glslang::TIntermediate* glslangIntermediate; |
| 238 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 239 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 240 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 241 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 242 | 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] | 243 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 244 | 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] | 245 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 246 | std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 247 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 248 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 249 | // Map pointee types for EbtReference to their forward pointers |
| 250 | std::map<const glslang::TType *, spv::Id> forwardPointers; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | // |
| 254 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 255 | // |
| 256 | |
| 257 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 258 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 259 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 260 | switch (source) { |
| 261 | case glslang::EShSourceGlsl: |
| 262 | switch (profile) { |
| 263 | case ENoProfile: |
| 264 | case ECoreProfile: |
| 265 | case ECompatibilityProfile: |
| 266 | return spv::SourceLanguageGLSL; |
| 267 | case EEsProfile: |
| 268 | return spv::SourceLanguageESSL; |
| 269 | default: |
| 270 | return spv::SourceLanguageUnknown; |
| 271 | } |
| 272 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 273 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 274 | default: |
| 275 | return spv::SourceLanguageUnknown; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // Translate glslang language (stage) to SPIR-V execution model. |
| 280 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 281 | { |
| 282 | switch (stage) { |
| 283 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 284 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 285 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 286 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 287 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 288 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 289 | #ifdef NV_EXTENSIONS |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 290 | case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV; |
| 291 | case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV; |
| 292 | case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV; |
| 293 | case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV; |
| 294 | case EShLangMissNV: return spv::ExecutionModelMissNV; |
| 295 | case EShLangCallableNV: return spv::ExecutionModelCallableNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 296 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 297 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 298 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 299 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 300 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 301 | return spv::ExecutionModelFragment; |
| 302 | } |
| 303 | } |
| 304 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 305 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 306 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 307 | { |
| 308 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 309 | case glslang::Esd1D: return spv::Dim1D; |
| 310 | case glslang::Esd2D: return spv::Dim2D; |
| 311 | case glslang::Esd3D: return spv::Dim3D; |
| 312 | case glslang::EsdCube: return spv::DimCube; |
| 313 | case glslang::EsdRect: return spv::DimRect; |
| 314 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 315 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 316 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 317 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 318 | return spv::Dim2D; |
| 319 | } |
| 320 | } |
| 321 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 322 | // Translate glslang precision to SPIR-V precision decorations. |
| 323 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 324 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 325 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 326 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 327 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 328 | default: |
| 329 | return spv::NoPrecision; |
| 330 | } |
| 331 | } |
| 332 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 333 | // Translate glslang type to SPIR-V precision decorations. |
| 334 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 335 | { |
| 336 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 337 | } |
| 338 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 339 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 340 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 341 | { |
| 342 | if (type.getBasicType() == glslang::EbtBlock) { |
| 343 | switch (type.getQualifier().storage) { |
| 344 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 345 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 346 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 347 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 348 | #ifdef NV_EXTENSIONS |
| 349 | case glslang::EvqPayloadNV: return spv::DecorationBlock; |
| 350 | case glslang::EvqPayloadInNV: return spv::DecorationBlock; |
| 351 | case glslang::EvqHitAttrNV: return spv::DecorationBlock; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 352 | case glslang::EvqCallableDataNV: return spv::DecorationBlock; |
| 353 | case glslang::EvqCallableDataInNV: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 354 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 355 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 356 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 361 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 362 | } |
| 363 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 364 | // Translate glslang type to SPIR-V memory decorations. |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 365 | 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] | 366 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 367 | if (!useVulkanMemoryModel) { |
| 368 | if (qualifier.coherent) |
| 369 | memory.push_back(spv::DecorationCoherent); |
| 370 | if (qualifier.volatil) { |
| 371 | memory.push_back(spv::DecorationVolatile); |
| 372 | memory.push_back(spv::DecorationCoherent); |
| 373 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 374 | } |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 375 | if (qualifier.restrict) |
| 376 | memory.push_back(spv::DecorationRestrict); |
| 377 | if (qualifier.readonly) |
| 378 | memory.push_back(spv::DecorationNonWritable); |
| 379 | if (qualifier.writeonly) |
| 380 | memory.push_back(spv::DecorationNonReadable); |
| 381 | } |
| 382 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 383 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 384 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 385 | { |
| 386 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 387 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 388 | case glslang::ElmRowMajor: |
| 389 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 390 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 391 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 392 | default: |
| 393 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 394 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 395 | } |
| 396 | } else { |
| 397 | switch (type.getBasicType()) { |
| 398 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 399 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 400 | break; |
| 401 | case glslang::EbtBlock: |
| 402 | switch (type.getQualifier().storage) { |
| 403 | case glslang::EvqUniform: |
| 404 | case glslang::EvqBuffer: |
| 405 | switch (type.getQualifier().layoutPacking) { |
| 406 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 407 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 408 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 409 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 410 | } |
| 411 | case glslang::EvqVaryingIn: |
| 412 | case glslang::EvqVaryingOut: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 413 | if (type.getQualifier().isTaskMemory()) { |
| 414 | switch (type.getQualifier().layoutPacking) { |
| 415 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 416 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 417 | default: break; |
| 418 | } |
| 419 | } else { |
| 420 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 421 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 422 | return spv::DecorationMax; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 423 | #ifdef NV_EXTENSIONS |
| 424 | case glslang::EvqPayloadNV: |
| 425 | case glslang::EvqPayloadInNV: |
| 426 | case glslang::EvqHitAttrNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 427 | case glslang::EvqCallableDataNV: |
| 428 | case glslang::EvqCallableDataInNV: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 429 | return spv::DecorationMax; |
| 430 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 431 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 432 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 433 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 440 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 441 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 442 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 443 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 444 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 445 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 446 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 447 | else if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 448 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 449 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 450 | return spv::DecorationFlat; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 451 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 452 | else if (qualifier.explicitInterp) { |
| 453 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 454 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 455 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 456 | #endif |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 457 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 458 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 462 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 463 | // should be applied. |
| 464 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 465 | { |
| 466 | if (qualifier.patch) |
| 467 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 468 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 469 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 470 | else if (qualifier.sample) { |
| 471 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 472 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 473 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 474 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 475 | } |
| 476 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 477 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 478 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 479 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 480 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 481 | return spv::DecorationInvariant; |
| 482 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 483 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 484 | } |
| 485 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 486 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 487 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 488 | { |
| 489 | if (qualifier.noContraction) |
| 490 | return spv::DecorationNoContraction; |
| 491 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 492 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 493 | } |
| 494 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 495 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 496 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 497 | { |
| 498 | if (qualifier.isNonUniform()) { |
| 499 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 500 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 501 | return spv::DecorationNonUniformEXT; |
| 502 | } else |
| 503 | return spv::DecorationMax; |
| 504 | } |
| 505 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 506 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 507 | { |
| 508 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) { |
| 509 | return spv::MemoryAccessMaskNone; |
| 510 | } |
| 511 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
| 512 | if (coherentFlags.volatil || |
| 513 | coherentFlags.coherent || |
| 514 | coherentFlags.devicecoherent || |
| 515 | coherentFlags.queuefamilycoherent || |
| 516 | coherentFlags.workgroupcoherent || |
| 517 | coherentFlags.subgroupcoherent) { |
| 518 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 519 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 520 | } |
| 521 | if (coherentFlags.nonprivate) { |
| 522 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 523 | } |
| 524 | if (coherentFlags.volatil) { |
| 525 | mask = mask | spv::MemoryAccessVolatileMask; |
| 526 | } |
| 527 | if (mask != spv::MemoryAccessMaskNone) { |
| 528 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 529 | } |
| 530 | return mask; |
| 531 | } |
| 532 | |
| 533 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 534 | { |
| 535 | if (!glslangIntermediate->usingVulkanMemoryModel()) { |
| 536 | return spv::ImageOperandsMaskNone; |
| 537 | } |
| 538 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 539 | if (coherentFlags.volatil || |
| 540 | coherentFlags.coherent || |
| 541 | coherentFlags.devicecoherent || |
| 542 | coherentFlags.queuefamilycoherent || |
| 543 | coherentFlags.workgroupcoherent || |
| 544 | coherentFlags.subgroupcoherent) { |
| 545 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 546 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 547 | } |
| 548 | if (coherentFlags.nonprivate) { |
| 549 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 550 | } |
| 551 | if (coherentFlags.volatil) { |
| 552 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 553 | } |
| 554 | if (mask != spv::ImageOperandsMaskNone) { |
| 555 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 556 | } |
| 557 | return mask; |
| 558 | } |
| 559 | |
| 560 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 561 | { |
| 562 | spv::Builder::AccessChain::CoherentFlags flags; |
| 563 | flags.coherent = type.getQualifier().coherent; |
| 564 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 565 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 566 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 567 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 568 | type.getQualifier().storage == glslang::EvqShared; |
| 569 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
| 570 | // *coherent variables are implicitly nonprivate in GLSL |
| 571 | flags.nonprivate = type.getQualifier().nonprivate || |
Jeff Bolz | ab3c965 | 2018-10-15 22:46:48 -0500 | [diff] [blame] | 572 | flags.subgroupcoherent || |
| 573 | flags.workgroupcoherent || |
| 574 | flags.queuefamilycoherent || |
| 575 | flags.devicecoherent || |
| 576 | flags.coherent; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 577 | flags.volatil = type.getQualifier().volatil; |
| 578 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
| 579 | return flags; |
| 580 | } |
| 581 | |
| 582 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 583 | { |
| 584 | spv::Scope scope; |
| 585 | if (coherentFlags.coherent) { |
| 586 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 587 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 588 | } else if (coherentFlags.devicecoherent) { |
| 589 | scope = spv::ScopeDevice; |
| 590 | } else if (coherentFlags.queuefamilycoherent) { |
| 591 | scope = spv::ScopeQueueFamilyKHR; |
| 592 | } else if (coherentFlags.workgroupcoherent) { |
| 593 | scope = spv::ScopeWorkgroup; |
| 594 | } else if (coherentFlags.subgroupcoherent) { |
| 595 | scope = spv::ScopeSubgroup; |
| 596 | } else { |
| 597 | scope = spv::ScopeMax; |
| 598 | } |
| 599 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 600 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 601 | } |
| 602 | return scope; |
| 603 | } |
| 604 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 605 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 606 | // associated capabilities when required. For some built-in variables, a capability |
| 607 | // is generated only when using the variable in an executable instruction, but not when |
| 608 | // just declaring a struct member variable with it. This is true for PointSize, |
| 609 | // ClipDistance, and CullDistance. |
| 610 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 611 | { |
| 612 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 613 | case glslang::EbvPointSize: |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 614 | // Defer adding the capability until the built-in is actually used. |
| 615 | if (! memberDeclaration) { |
| 616 | switch (glslangIntermediate->getStage()) { |
| 617 | case EShLangGeometry: |
| 618 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 619 | break; |
| 620 | case EShLangTessControl: |
| 621 | case EShLangTessEvaluation: |
| 622 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 623 | break; |
| 624 | default: |
| 625 | break; |
| 626 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 627 | } |
| 628 | return spv::BuiltInPointSize; |
| 629 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 630 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 631 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 632 | // They are now generated when used, rather than here when declared. |
| 633 | // Potentially, the specification should be more clear what the minimum |
| 634 | // use needed is to trigger the capability. |
| 635 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 636 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 637 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 638 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 639 | return spv::BuiltInClipDistance; |
| 640 | |
| 641 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 642 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 643 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 644 | return spv::BuiltInCullDistance; |
| 645 | |
| 646 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 647 | builder.addCapability(spv::CapabilityMultiViewport); |
| 648 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 649 | glslangIntermediate->getStage() == EShLangTessControl || |
| 650 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 651 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 652 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 653 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 654 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 655 | return spv::BuiltInViewportIndex; |
| 656 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 657 | case glslang::EbvSampleId: |
| 658 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 659 | return spv::BuiltInSampleId; |
| 660 | |
| 661 | case glslang::EbvSamplePosition: |
| 662 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 663 | return spv::BuiltInSamplePosition; |
| 664 | |
| 665 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 666 | return spv::BuiltInSampleMask; |
| 667 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 668 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 669 | #ifdef NV_EXTENSIONS |
| 670 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 671 | return spv::BuiltInLayer; |
| 672 | } |
| 673 | #endif |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 674 | builder.addCapability(spv::CapabilityGeometry); |
| 675 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 676 | glslangIntermediate->getStage() == EShLangTessControl || |
| 677 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 678 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 679 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 680 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 681 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 682 | return spv::BuiltInLayer; |
| 683 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 684 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 685 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 686 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 687 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 688 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 689 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 690 | case glslang::EbvBaseVertex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 691 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 692 | builder.addCapability(spv::CapabilityDrawParameters); |
| 693 | return spv::BuiltInBaseVertex; |
| 694 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 695 | case glslang::EbvBaseInstance: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 696 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 697 | builder.addCapability(spv::CapabilityDrawParameters); |
| 698 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 699 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 700 | case glslang::EbvDrawId: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 701 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 702 | builder.addCapability(spv::CapabilityDrawParameters); |
| 703 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 704 | |
| 705 | case glslang::EbvPrimitiveId: |
| 706 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 707 | builder.addCapability(spv::CapabilityGeometry); |
| 708 | return spv::BuiltInPrimitiveId; |
| 709 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 710 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 711 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 712 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 713 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 714 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 715 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 716 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 717 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 718 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 719 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 720 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 721 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 722 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 723 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 724 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 725 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 726 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 727 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 728 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 729 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 730 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 731 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 732 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 733 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 734 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 735 | return spv::BuiltInSubgroupSize; |
| 736 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 737 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 738 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 739 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 740 | return spv::BuiltInSubgroupLocalInvocationId; |
| 741 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 742 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 743 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 744 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 745 | return spv::BuiltInSubgroupEqMaskKHR; |
| 746 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 747 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 748 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 749 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 750 | return spv::BuiltInSubgroupGeMaskKHR; |
| 751 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 752 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 753 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 754 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 755 | return spv::BuiltInSubgroupGtMaskKHR; |
| 756 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 757 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 758 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 759 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 760 | return spv::BuiltInSubgroupLeMaskKHR; |
| 761 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 762 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 763 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 764 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 765 | return spv::BuiltInSubgroupLtMaskKHR; |
| 766 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 767 | case glslang::EbvNumSubgroups: |
| 768 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 769 | return spv::BuiltInNumSubgroups; |
| 770 | |
| 771 | case glslang::EbvSubgroupID: |
| 772 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 773 | return spv::BuiltInSubgroupId; |
| 774 | |
| 775 | case glslang::EbvSubgroupSize2: |
| 776 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 777 | return spv::BuiltInSubgroupSize; |
| 778 | |
| 779 | case glslang::EbvSubgroupInvocation2: |
| 780 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 781 | return spv::BuiltInSubgroupLocalInvocationId; |
| 782 | |
| 783 | case glslang::EbvSubgroupEqMask2: |
| 784 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 785 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 786 | return spv::BuiltInSubgroupEqMask; |
| 787 | |
| 788 | case glslang::EbvSubgroupGeMask2: |
| 789 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 790 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 791 | return spv::BuiltInSubgroupGeMask; |
| 792 | |
| 793 | case glslang::EbvSubgroupGtMask2: |
| 794 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 795 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 796 | return spv::BuiltInSubgroupGtMask; |
| 797 | |
| 798 | case glslang::EbvSubgroupLeMask2: |
| 799 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 800 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 801 | return spv::BuiltInSubgroupLeMask; |
| 802 | |
| 803 | case glslang::EbvSubgroupLtMask2: |
| 804 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 805 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 806 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 807 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 808 | case glslang::EbvBaryCoordNoPersp: |
| 809 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 810 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 811 | |
| 812 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 813 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 814 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 815 | |
| 816 | case glslang::EbvBaryCoordNoPerspSample: |
| 817 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 818 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 819 | |
| 820 | case glslang::EbvBaryCoordSmooth: |
| 821 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 822 | return spv::BuiltInBaryCoordSmoothAMD; |
| 823 | |
| 824 | case glslang::EbvBaryCoordSmoothCentroid: |
| 825 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 826 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 827 | |
| 828 | case glslang::EbvBaryCoordSmoothSample: |
| 829 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 830 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 831 | |
| 832 | case glslang::EbvBaryCoordPullModel: |
| 833 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 834 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 835 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 836 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 837 | case glslang::EbvDeviceIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 838 | addPre13Extension(spv::E_SPV_KHR_device_group); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 839 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 840 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 841 | |
| 842 | case glslang::EbvViewIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 843 | addPre13Extension(spv::E_SPV_KHR_multiview); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 844 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 845 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 846 | |
Daniel Koch | 5154db5 | 2018-11-26 10:01:58 -0500 | [diff] [blame] | 847 | case glslang::EbvFragSizeEXT: |
| 848 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 849 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 850 | return spv::BuiltInFragSizeEXT; |
| 851 | |
| 852 | case glslang::EbvFragInvocationCountEXT: |
| 853 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 854 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 855 | return spv::BuiltInFragInvocationCountEXT; |
| 856 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 857 | #ifdef NV_EXTENSIONS |
| 858 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 859 | if (!memberDeclaration) { |
| 860 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 861 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 862 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 863 | return spv::BuiltInViewportMaskNV; |
| 864 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 865 | if (!memberDeclaration) { |
| 866 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 867 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 868 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 869 | return spv::BuiltInSecondaryPositionNV; |
| 870 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 871 | if (!memberDeclaration) { |
| 872 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 873 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 874 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 875 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 876 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 877 | if (!memberDeclaration) { |
| 878 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 879 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 880 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 881 | return spv::BuiltInPositionPerViewNV; |
| 882 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 883 | if (!memberDeclaration) { |
| 884 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 885 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 886 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 887 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 888 | case glslang::EbvFragFullyCoveredNV: |
| 889 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 890 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 891 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 892 | case glslang::EbvFragmentSizeNV: |
| 893 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 894 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 895 | return spv::BuiltInFragmentSizeNV; |
| 896 | case glslang::EbvInvocationsPerPixelNV: |
| 897 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 898 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 899 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 900 | |
| 901 | // raytracing |
| 902 | case glslang::EbvLaunchIdNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 903 | return spv::BuiltInLaunchIdNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 904 | case glslang::EbvLaunchSizeNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 905 | return spv::BuiltInLaunchSizeNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 906 | case glslang::EbvWorldRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 907 | return spv::BuiltInWorldRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 908 | case glslang::EbvWorldRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 909 | return spv::BuiltInWorldRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 910 | case glslang::EbvObjectRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 911 | return spv::BuiltInObjectRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 912 | case glslang::EbvObjectRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 913 | return spv::BuiltInObjectRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 914 | case glslang::EbvRayTminNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 915 | return spv::BuiltInRayTminNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 916 | case glslang::EbvRayTmaxNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 917 | return spv::BuiltInRayTmaxNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 918 | case glslang::EbvInstanceCustomIndexNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 919 | return spv::BuiltInInstanceCustomIndexNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 920 | case glslang::EbvHitTNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 921 | return spv::BuiltInHitTNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 922 | case glslang::EbvHitKindNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 923 | return spv::BuiltInHitKindNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 924 | case glslang::EbvObjectToWorldNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 925 | return spv::BuiltInObjectToWorldNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 926 | case glslang::EbvWorldToObjectNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 927 | return spv::BuiltInWorldToObjectNV; |
| 928 | case glslang::EbvIncomingRayFlagsNV: |
| 929 | return spv::BuiltInIncomingRayFlagsNV; |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 930 | case glslang::EbvBaryCoordNV: |
| 931 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 932 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 933 | return spv::BuiltInBaryCoordNV; |
| 934 | case glslang::EbvBaryCoordNoPerspNV: |
| 935 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 936 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 937 | return spv::BuiltInBaryCoordNoPerspNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 938 | case glslang::EbvTaskCountNV: |
| 939 | return spv::BuiltInTaskCountNV; |
| 940 | case glslang::EbvPrimitiveCountNV: |
| 941 | return spv::BuiltInPrimitiveCountNV; |
| 942 | case glslang::EbvPrimitiveIndicesNV: |
| 943 | return spv::BuiltInPrimitiveIndicesNV; |
| 944 | case glslang::EbvClipDistancePerViewNV: |
| 945 | return spv::BuiltInClipDistancePerViewNV; |
| 946 | case glslang::EbvCullDistancePerViewNV: |
| 947 | return spv::BuiltInCullDistancePerViewNV; |
| 948 | case glslang::EbvLayerPerViewNV: |
| 949 | return spv::BuiltInLayerPerViewNV; |
| 950 | case glslang::EbvMeshViewCountNV: |
| 951 | return spv::BuiltInMeshViewCountNV; |
| 952 | case glslang::EbvMeshViewIndicesNV: |
| 953 | return spv::BuiltInMeshViewIndicesNV; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 954 | #endif |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 955 | default: |
| 956 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 960 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 961 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 962 | { |
| 963 | assert(type.getBasicType() == glslang::EbtSampler); |
| 964 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 965 | // Check for capabilities |
| 966 | switch (type.getQualifier().layoutFormat) { |
| 967 | case glslang::ElfRg32f: |
| 968 | case glslang::ElfRg16f: |
| 969 | case glslang::ElfR11fG11fB10f: |
| 970 | case glslang::ElfR16f: |
| 971 | case glslang::ElfRgba16: |
| 972 | case glslang::ElfRgb10A2: |
| 973 | case glslang::ElfRg16: |
| 974 | case glslang::ElfRg8: |
| 975 | case glslang::ElfR16: |
| 976 | case glslang::ElfR8: |
| 977 | case glslang::ElfRgba16Snorm: |
| 978 | case glslang::ElfRg16Snorm: |
| 979 | case glslang::ElfRg8Snorm: |
| 980 | case glslang::ElfR16Snorm: |
| 981 | case glslang::ElfR8Snorm: |
| 982 | |
| 983 | case glslang::ElfRg32i: |
| 984 | case glslang::ElfRg16i: |
| 985 | case glslang::ElfRg8i: |
| 986 | case glslang::ElfR16i: |
| 987 | case glslang::ElfR8i: |
| 988 | |
| 989 | case glslang::ElfRgb10a2ui: |
| 990 | case glslang::ElfRg32ui: |
| 991 | case glslang::ElfRg16ui: |
| 992 | case glslang::ElfRg8ui: |
| 993 | case glslang::ElfR16ui: |
| 994 | case glslang::ElfR8ui: |
| 995 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 996 | break; |
| 997 | |
| 998 | default: |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1003 | switch (type.getQualifier().layoutFormat) { |
| 1004 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1005 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1006 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1007 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1008 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1009 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1010 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1011 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1012 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1013 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1014 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1015 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1016 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1017 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1018 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1019 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1020 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1021 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1022 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1023 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1024 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1025 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1026 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1027 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1028 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1029 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1030 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1031 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1032 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1033 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1034 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1035 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1036 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1037 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1038 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1039 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1040 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1041 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1042 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1043 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1044 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1048 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1049 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1050 | if (selectionNode.getFlatten()) |
| 1051 | return spv::SelectionControlFlattenMask; |
| 1052 | if (selectionNode.getDontFlatten()) |
| 1053 | return spv::SelectionControlDontFlattenMask; |
| 1054 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1055 | } |
| 1056 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1057 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1058 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1059 | if (switchNode.getFlatten()) |
| 1060 | return spv::SelectionControlFlattenMask; |
| 1061 | if (switchNode.getDontFlatten()) |
| 1062 | return spv::SelectionControlDontFlattenMask; |
| 1063 | return spv::SelectionControlMaskNone; |
| 1064 | } |
| 1065 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1066 | // return a non-0 dependency if the dependency argument must be set |
| 1067 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
| 1068 | unsigned int& dependencyLength) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1069 | { |
| 1070 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1071 | |
| 1072 | if (loopNode.getDontUnroll()) |
| 1073 | control = control | spv::LoopControlDontUnrollMask; |
| 1074 | if (loopNode.getUnroll()) |
| 1075 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1076 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1077 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1078 | else if (loopNode.getLoopDependency() > 0) { |
| 1079 | control = control | spv::LoopControlDependencyLengthMask; |
| 1080 | dependencyLength = loopNode.getLoopDependency(); |
| 1081 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1082 | |
| 1083 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1084 | } |
| 1085 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1086 | // Translate glslang type to SPIR-V storage class. |
| 1087 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1088 | { |
| 1089 | if (type.getQualifier().isPipeInput()) |
| 1090 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1091 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1092 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1093 | |
| 1094 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
| 1095 | type.getQualifier().storage == glslang::EvqUniform) { |
| 1096 | if (type.getBasicType() == glslang::EbtAtomicUint) |
| 1097 | return spv::StorageClassAtomicCounter; |
| 1098 | if (type.containsOpaque()) |
| 1099 | return spv::StorageClassUniformConstant; |
| 1100 | } |
| 1101 | |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1102 | #ifdef NV_EXTENSIONS |
| 1103 | if (type.getQualifier().isUniformOrBuffer() && |
| 1104 | type.getQualifier().layoutShaderRecordNV) { |
| 1105 | return spv::StorageClassShaderRecordBufferNV; |
| 1106 | } |
| 1107 | #endif |
| 1108 | |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1109 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1110 | addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class); |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1111 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1115 | if (type.getQualifier().layoutPushConstant) |
| 1116 | return spv::StorageClassPushConstant; |
| 1117 | if (type.getBasicType() == glslang::EbtBlock) |
| 1118 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1119 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1120 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1121 | |
| 1122 | switch (type.getQualifier().storage) { |
| 1123 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
| 1124 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1125 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1126 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1127 | #ifdef NV_EXTENSIONS |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1128 | case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV; |
| 1129 | case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV; |
| 1130 | case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV; |
| 1131 | case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV; |
| 1132 | case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1133 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1134 | default: |
| 1135 | assert(0); |
| 1136 | break; |
| 1137 | } |
| 1138 | |
| 1139 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1140 | } |
| 1141 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1142 | // Add capabilities pertaining to how an array is indexed. |
| 1143 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1144 | const glslang::TType& indexType) |
| 1145 | { |
| 1146 | if (indexType.getQualifier().isNonUniform()) { |
| 1147 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1148 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1149 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1150 | if (baseType.getQualifier().hasAttachment()) |
| 1151 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
| 1152 | else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1153 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
| 1154 | else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1155 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1156 | else if (baseType.isImage()) |
| 1157 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1158 | else if (baseType.isTexture()) |
| 1159 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1160 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1161 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1162 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1163 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1164 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1165 | } |
| 1166 | } else { |
| 1167 | // assume a dynamically uniform index |
| 1168 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1169 | if (baseType.getQualifier().hasAttachment()) { |
| 1170 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1171 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1172 | } else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1173 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1174 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1175 | } else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1176 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1177 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1178 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1183 | // 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] | 1184 | // descriptor set. |
| 1185 | bool IsDescriptorResource(const glslang::TType& type) |
| 1186 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1187 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1188 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1189 | return type.getQualifier().isUniformOrBuffer() && |
| 1190 | #ifdef NV_EXTENSIONS |
| 1191 | ! type.getQualifier().layoutShaderRecordNV && |
| 1192 | #endif |
| 1193 | ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1194 | |
| 1195 | // non block... |
| 1196 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1197 | // if they are the global-scope-class, not the function parameter |
| 1198 | // (or local, if they ever exist) class. |
| 1199 | if (type.getBasicType() == glslang::EbtSampler) |
| 1200 | return type.getQualifier().isUniformOrBuffer(); |
| 1201 | |
| 1202 | // None of the above. |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1206 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1207 | { |
| 1208 | if (child.layoutMatrix == glslang::ElmNone) |
| 1209 | child.layoutMatrix = parent.layoutMatrix; |
| 1210 | |
| 1211 | if (parent.invariant) |
| 1212 | child.invariant = true; |
| 1213 | if (parent.nopersp) |
| 1214 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1215 | #ifdef AMD_EXTENSIONS |
| 1216 | if (parent.explicitInterp) |
| 1217 | child.explicitInterp = true; |
| 1218 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1219 | if (parent.flat) |
| 1220 | child.flat = true; |
| 1221 | if (parent.centroid) |
| 1222 | child.centroid = true; |
| 1223 | if (parent.patch) |
| 1224 | child.patch = true; |
| 1225 | if (parent.sample) |
| 1226 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1227 | if (parent.coherent) |
| 1228 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1229 | if (parent.devicecoherent) |
| 1230 | child.devicecoherent = true; |
| 1231 | if (parent.queuefamilycoherent) |
| 1232 | child.queuefamilycoherent = true; |
| 1233 | if (parent.workgroupcoherent) |
| 1234 | child.workgroupcoherent = true; |
| 1235 | if (parent.subgroupcoherent) |
| 1236 | child.subgroupcoherent = true; |
| 1237 | if (parent.nonprivate) |
| 1238 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1239 | if (parent.volatil) |
| 1240 | child.volatil = true; |
| 1241 | if (parent.restrict) |
| 1242 | child.restrict = true; |
| 1243 | if (parent.readonly) |
| 1244 | child.readonly = true; |
| 1245 | if (parent.writeonly) |
| 1246 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1247 | #ifdef NV_EXTENSIONS |
| 1248 | if (parent.perPrimitiveNV) |
| 1249 | child.perPrimitiveNV = true; |
| 1250 | if (parent.perViewNV) |
| 1251 | child.perViewNV = true; |
| 1252 | if (parent.perTaskNV) |
| 1253 | child.perTaskNV = true; |
| 1254 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1257 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1258 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1259 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1260 | // - struct members might inherit from a struct declaration |
| 1261 | // (note that non-block structs don't explicitly inherit, |
| 1262 | // only implicitly, meaning no decoration involved) |
| 1263 | // - affect decorations on the struct members |
| 1264 | // (note smooth does not, and expecting something like volatile |
| 1265 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1266 | // - 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] | 1267 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1270 | // |
| 1271 | // Implement the TGlslangToSpvTraverser class. |
| 1272 | // |
| 1273 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1274 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1275 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 1276 | : TIntermTraverser(true, false, true), |
| 1277 | options(options), |
| 1278 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1279 | sequenceDepth(0), logger(buildLogger), |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1280 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1281 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1282 | glslangIntermediate(glslangIntermediate) |
| 1283 | { |
| 1284 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1285 | |
| 1286 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1287 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1288 | glslangIntermediate->getVersion()); |
| 1289 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1290 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1291 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1292 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1293 | |
| 1294 | // Set the source shader's text. If for SPV version 1.0, include |
| 1295 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1296 | // Otherwise, emit those as actual instructions. |
| 1297 | std::string text; |
| 1298 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1299 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1300 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1301 | text.append("// OpModuleProcessed "); |
| 1302 | text.append(processes[p]); |
| 1303 | text.append("\n"); |
| 1304 | } else |
| 1305 | builder.addModuleProcessed(processes[p]); |
| 1306 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1307 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1308 | text.append("#line 1\n"); |
| 1309 | text.append(glslangIntermediate->getSourceText()); |
| 1310 | builder.setSourceText(text); |
Greg Fischer | d445bb2 | 2018-12-06 11:13:15 -0700 | [diff] [blame] | 1311 | // Pass name and text for all included files |
| 1312 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1313 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1314 | builder.addInclude(iItr->first, iItr->second); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1315 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1316 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1317 | |
| 1318 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1319 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1320 | |
| 1321 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1322 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
| 1323 | builder.addExtension(spv::E_SPV_EXT_physical_storage_buffer); |
| 1324 | builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
| 1325 | }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1326 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1327 | memoryModel = spv::MemoryModelVulkanKHR; |
| 1328 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1329 | builder.addExtension(spv::E_SPV_KHR_vulkan_memory_model); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1330 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1331 | builder.setMemoryModel(addressingModel, memoryModel); |
| 1332 | |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1333 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1334 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1335 | |
| 1336 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1337 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1338 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1339 | builder.addSourceExtension(it->c_str()); |
| 1340 | |
| 1341 | // Add the top-level modes for this shader. |
| 1342 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1343 | if (glslangIntermediate->getXfbMode()) { |
| 1344 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1345 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1346 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1347 | |
| 1348 | unsigned int mode; |
| 1349 | switch (glslangIntermediate->getStage()) { |
| 1350 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1351 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1352 | break; |
| 1353 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1354 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1355 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1356 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1357 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1358 | glslang::TLayoutGeometry primitive; |
| 1359 | |
| 1360 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1361 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1362 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1363 | } else { |
| 1364 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1365 | } |
| 1366 | |
| 1367 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1368 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1369 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1370 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1371 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1372 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1373 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1374 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1375 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1376 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1377 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1378 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1379 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1380 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1381 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1382 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1383 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1384 | |
| 1385 | switch (glslangIntermediate->getVertexOrder()) { |
| 1386 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1387 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1388 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1389 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1390 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1391 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1392 | |
| 1393 | if (glslangIntermediate->getPointMode()) |
| 1394 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1395 | break; |
| 1396 | |
| 1397 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1398 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1399 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1400 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1401 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1402 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1403 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1404 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1405 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1406 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1407 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1408 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1409 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1410 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1411 | |
| 1412 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1413 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1414 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1415 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1416 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1417 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1418 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1419 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1420 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1421 | break; |
| 1422 | |
| 1423 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1424 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1425 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1426 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1427 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1428 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1429 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1430 | else |
| 1431 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1432 | |
| 1433 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1434 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1435 | |
chaoc | c120452 | 2017-06-30 17:14:30 -0700 | [diff] [blame] | 1436 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1437 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1438 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1439 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1440 | } |
| 1441 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1442 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1443 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1444 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1445 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1446 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1447 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1448 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1449 | |
| 1450 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1451 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1452 | break; |
| 1453 | |
| 1454 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1455 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 1456 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1457 | glslangIntermediate->getLocalSize(1), |
| 1458 | glslangIntermediate->getLocalSize(2)); |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 1459 | #ifdef NV_EXTENSIONS |
| 1460 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1461 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1462 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1463 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1464 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1465 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1466 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1467 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1468 | } |
| 1469 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1470 | break; |
| 1471 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1472 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1473 | case EShLangRayGenNV: |
| 1474 | case EShLangIntersectNV: |
| 1475 | case EShLangAnyHitNV: |
| 1476 | case EShLangClosestHitNV: |
| 1477 | case EShLangMissNV: |
| 1478 | case EShLangCallableNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1479 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1480 | builder.addExtension("SPV_NV_ray_tracing"); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1481 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1482 | case EShLangTaskNV: |
| 1483 | case EShLangMeshNV: |
| 1484 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1485 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1486 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1487 | glslangIntermediate->getLocalSize(1), |
| 1488 | glslangIntermediate->getLocalSize(2)); |
| 1489 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 1490 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1491 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives()); |
| 1492 | |
| 1493 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1494 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1495 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1496 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1497 | default: mode = spv::ExecutionModeMax; break; |
| 1498 | } |
| 1499 | if (mode != spv::ExecutionModeMax) |
| 1500 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1501 | } |
| 1502 | break; |
| 1503 | #endif |
| 1504 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1505 | default: |
| 1506 | break; |
| 1507 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1508 | } |
| 1509 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1510 | // Finish creating SPV, after the traversal is complete. |
| 1511 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1512 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1513 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1514 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1515 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1516 | builder.leaveFunction(); |
| 1517 | } |
| 1518 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1519 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1520 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1521 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1522 | |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1523 | // Add capabilities, extensions, remove unneeded decorations, etc., |
| 1524 | // based on the resulting SPIR-V. |
| 1525 | builder.postProcess(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1528 | // Write the SPV into 'out'. |
| 1529 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1530 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1531 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | // |
| 1535 | // Implement the traversal functions. |
| 1536 | // |
| 1537 | // Return true from interior nodes to have the external traversal |
| 1538 | // continue on to children. Return false if children were |
| 1539 | // already processed. |
| 1540 | // |
| 1541 | |
| 1542 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1543 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1544 | // - uniform/input reads |
| 1545 | // - output writes |
| 1546 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1547 | // - something simple that degenerates into the last bullet |
| 1548 | // |
| 1549 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1550 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1551 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1552 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1553 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1554 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1555 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1556 | // Formal function parameters were mapped during makeFunctions(). |
| 1557 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1558 | |
| 1559 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1560 | if (builder.isPointer(id)) { |
| 1561 | spv::StorageClass sc = builder.getStorageClass(id); |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1562 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) { |
| 1563 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) |
| 1564 | iOSet.insert(id); |
| 1565 | } |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1569 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1570 | // Prepare to generate code for the access |
| 1571 | |
| 1572 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1573 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1574 | // followed by setting the base. |
| 1575 | builder.clearAccessChain(); |
| 1576 | |
| 1577 | // 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] | 1578 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1579 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1580 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1581 | // 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] | 1582 | // These are also pure R-values. |
| 1583 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1584 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1585 | builder.setAccessChainRValue(id); |
| 1586 | else |
| 1587 | builder.setAccessChainLValue(id); |
| 1588 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1589 | |
| 1590 | // Process linkage-only nodes for any special additional interface work. |
| 1591 | if (linkageOnly) { |
| 1592 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1593 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1594 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1595 | // of declaration. |
| 1596 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1597 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1598 | // Save possible originating buffers for counter buffers, keyed by |
| 1599 | // making the potential counter-buffer name. |
| 1600 | std::string keyName = symbol->getName().c_str(); |
| 1601 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1602 | counterOriginator[keyName] = symbol; |
| 1603 | } else { |
| 1604 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1605 | std::string keyName = symbol->getName().c_str(); |
| 1606 | auto it = counterOriginator.find(keyName); |
| 1607 | if (it != counterOriginator.end()) { |
| 1608 | id = getSymbolId(it->second); |
| 1609 | if (id != spv::NoResult) { |
| 1610 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1611 | if (counterId != spv::NoResult) { |
| 1612 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1613 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1614 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | } |
| 1619 | } |
| 1620 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1624 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1625 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1626 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1627 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1628 | if (node->getType().getQualifier().isSpecConstant()) |
| 1629 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1630 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1631 | // First, handle special cases |
| 1632 | switch (node->getOp()) { |
| 1633 | case glslang::EOpAssign: |
| 1634 | case glslang::EOpAddAssign: |
| 1635 | case glslang::EOpSubAssign: |
| 1636 | case glslang::EOpMulAssign: |
| 1637 | case glslang::EOpVectorTimesMatrixAssign: |
| 1638 | case glslang::EOpVectorTimesScalarAssign: |
| 1639 | case glslang::EOpMatrixTimesScalarAssign: |
| 1640 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1641 | case glslang::EOpDivAssign: |
| 1642 | case glslang::EOpModAssign: |
| 1643 | case glslang::EOpAndAssign: |
| 1644 | case glslang::EOpInclusiveOrAssign: |
| 1645 | case glslang::EOpExclusiveOrAssign: |
| 1646 | case glslang::EOpLeftShiftAssign: |
| 1647 | case glslang::EOpRightShiftAssign: |
| 1648 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1649 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1650 | // says to evaluate the left before the right. So, always, left |
| 1651 | // node then right node. |
| 1652 | { |
| 1653 | // get the left l-value, save it away |
| 1654 | builder.clearAccessChain(); |
| 1655 | node->getLeft()->traverse(this); |
| 1656 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1657 | |
| 1658 | // evaluate the right |
| 1659 | builder.clearAccessChain(); |
| 1660 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1661 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1662 | |
| 1663 | if (node->getOp() != glslang::EOpAssign) { |
| 1664 | // the left is also an r-value |
| 1665 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1666 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1667 | |
| 1668 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1669 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1670 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1671 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1672 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1673 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1674 | node->getType().getBasicType()); |
| 1675 | |
| 1676 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1677 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | // store the result |
| 1681 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1682 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1683 | |
| 1684 | // assignments are expressions having an rValue after they are evaluated... |
| 1685 | builder.clearAccessChain(); |
| 1686 | builder.setAccessChainRValue(rValue); |
| 1687 | } |
| 1688 | return false; |
| 1689 | case glslang::EOpIndexDirect: |
| 1690 | case glslang::EOpIndexDirectStruct: |
| 1691 | { |
| 1692 | // Get the left part of the access chain. |
| 1693 | node->getLeft()->traverse(this); |
| 1694 | |
| 1695 | // Add the next element in the chain |
| 1696 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1697 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1698 | if (! node->getLeft()->getType().isArray() && |
| 1699 | node->getLeft()->getType().isVector() && |
| 1700 | node->getOp() == glslang::EOpIndexDirect) { |
| 1701 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1702 | // so short circuit the access-chain stuff with a swizzle. |
| 1703 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1704 | swizzle.push_back(glslangIndex); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1705 | int dummySize; |
| 1706 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1707 | TranslateCoherent(node->getLeft()->getType()), |
| 1708 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1709 | } else { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1710 | |
| 1711 | // Load through a block reference is performed with a dot operator that |
| 1712 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 1713 | // do a load and reset the access chain. |
| 1714 | if (node->getLeft()->getBasicType() == glslang::EbtReference && |
| 1715 | !node->getLeft()->getType().isArray() && |
| 1716 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1717 | { |
| 1718 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 1719 | builder.clearAccessChain(); |
| 1720 | builder.setAccessChainLValue(left); |
| 1721 | } |
| 1722 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1723 | int spvIndex = glslangIndex; |
| 1724 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1725 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1726 | { |
| 1727 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1728 | // index remapping due to hidden members in anonymous blocks. |
| 1729 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1730 | assert(remapper.size() > 0); |
| 1731 | spvIndex = remapper[glslangIndex]; |
| 1732 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1733 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1734 | // normal case for indexing array or structure or block |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1735 | builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType()), getBufferReferenceAlignment(node->getLeft()->getType())); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1736 | |
| 1737 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1738 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1739 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1740 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | return false; |
| 1744 | case glslang::EOpIndexIndirect: |
| 1745 | { |
| 1746 | // Structure or array or vector indirection. |
| 1747 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 1748 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1749 | // Will use the access chain's 'component' for variable index into a vector. |
| 1750 | |
| 1751 | // This adapter is building access chains left to right. |
| 1752 | // Set up the access chain to the left. |
| 1753 | node->getLeft()->traverse(this); |
| 1754 | |
| 1755 | // save it so that computing the right side doesn't trash it |
| 1756 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1757 | |
| 1758 | // compute the next index in the chain |
| 1759 | builder.clearAccessChain(); |
| 1760 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1761 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1762 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1763 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1764 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1765 | // restore the saved access chain |
| 1766 | builder.setAccessChain(partial); |
| 1767 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1768 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 1769 | int dummySize; |
| 1770 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1771 | TranslateCoherent(node->getLeft()->getType()), |
| 1772 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
| 1773 | } else |
| 1774 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), getBufferReferenceAlignment(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1775 | } |
| 1776 | return false; |
| 1777 | case glslang::EOpVectorSwizzle: |
| 1778 | { |
| 1779 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1780 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1781 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1782 | int dummySize; |
| 1783 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1784 | TranslateCoherent(node->getLeft()->getType()), |
| 1785 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1786 | } |
| 1787 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1788 | case glslang::EOpMatrixSwizzle: |
| 1789 | logger->missingFunctionality("matrix swizzle"); |
| 1790 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1791 | case glslang::EOpLogicalOr: |
| 1792 | case glslang::EOpLogicalAnd: |
| 1793 | { |
| 1794 | |
| 1795 | // These may require short circuiting, but can sometimes be done as straight |
| 1796 | // binary operations. The right operand must be short circuited if it has |
| 1797 | // side effects, and should probably be if it is complex. |
| 1798 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1799 | break; // handle below as a normal binary operation |
| 1800 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1801 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1802 | builder.clearAccessChain(); |
| 1803 | builder.setAccessChainRValue(result); |
| 1804 | } |
| 1805 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1806 | default: |
| 1807 | break; |
| 1808 | } |
| 1809 | |
| 1810 | // Assume generic binary op... |
| 1811 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1812 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1813 | builder.clearAccessChain(); |
| 1814 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1815 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1816 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1817 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1818 | builder.clearAccessChain(); |
| 1819 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1820 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1821 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1822 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1823 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1824 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1825 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1826 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1827 | convertGlslangToSpvType(node->getType()), left, right, |
| 1828 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1829 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1830 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1831 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1832 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1833 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1834 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1835 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1836 | return false; |
| 1837 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1841 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1842 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1843 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1844 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1845 | if (node->getType().getQualifier().isSpecConstant()) |
| 1846 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1847 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1848 | spv::Id result = spv::NoResult; |
| 1849 | |
| 1850 | // try texturing first |
| 1851 | result = createImageTextureFunctionCall(node); |
| 1852 | if (result != spv::NoResult) { |
| 1853 | builder.clearAccessChain(); |
| 1854 | builder.setAccessChainRValue(result); |
| 1855 | |
| 1856 | return false; // done with this node |
| 1857 | } |
| 1858 | |
| 1859 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1860 | |
| 1861 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1862 | // Quite special; won't want to evaluate the operand. |
| 1863 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1864 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 1865 | // except for the last block membeor of an SSBO. |
| 1866 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 1867 | // size extracted. |
| 1868 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1869 | // Normal .length() would have been constant folded by the front-end. |
| 1870 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1871 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1872 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1873 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1874 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1875 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1876 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1877 | |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 1878 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 1879 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 1880 | // AST expectation of a signed result. |
| 1881 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) |
| 1882 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 1883 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1884 | builder.clearAccessChain(); |
| 1885 | builder.setAccessChainRValue(length); |
| 1886 | |
| 1887 | return false; |
| 1888 | } |
| 1889 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1890 | // Start by evaluating the operand |
| 1891 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1892 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1893 | // operate first on the swizzle base, then apply the swizzle. |
| 1894 | spv::Id invertedType = spv::NoType; |
| 1895 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 1896 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 1897 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 1898 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1899 | builder.clearAccessChain(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1900 | if (invertedType != spv::NoType) |
| 1901 | node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this); |
| 1902 | else |
| 1903 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1904 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1905 | spv::Id operand = spv::NoResult; |
| 1906 | |
| 1907 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1908 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1909 | node->getOp() == glslang::EOpAtomicCounter || |
| 1910 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1911 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1912 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1913 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1914 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1915 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1916 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1917 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1918 | |
| 1919 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1920 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1921 | result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1922 | |
| 1923 | // if not, then possibly an operation |
| 1924 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1925 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1926 | |
| 1927 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1928 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1929 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1930 | builder.addDecoration(result, decorations.nonUniform); |
| 1931 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1932 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1933 | builder.clearAccessChain(); |
| 1934 | builder.setAccessChainRValue(result); |
| 1935 | |
| 1936 | return false; // done with this node |
| 1937 | } |
| 1938 | |
| 1939 | // it must be a special case, check... |
| 1940 | switch (node->getOp()) { |
| 1941 | case glslang::EOpPostIncrement: |
| 1942 | case glslang::EOpPostDecrement: |
| 1943 | case glslang::EOpPreIncrement: |
| 1944 | case glslang::EOpPreDecrement: |
| 1945 | { |
| 1946 | // 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] | 1947 | spv::Id one = 0; |
| 1948 | if (node->getBasicType() == glslang::EbtFloat) |
| 1949 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 1950 | else if (node->getBasicType() == glslang::EbtDouble) |
| 1951 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1952 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 1953 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1954 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 1955 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1956 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 1957 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1958 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1959 | one = builder.makeInt64Constant(1); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1960 | else |
| 1961 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1962 | glslang::TOperator op; |
| 1963 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1964 | node->getOp() == glslang::EOpPostIncrement) |
| 1965 | op = glslang::EOpAdd; |
| 1966 | else |
| 1967 | op = glslang::EOpSub; |
| 1968 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1969 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1970 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1971 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1972 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1973 | |
| 1974 | // The result of operation is always stored, but conditionally the |
| 1975 | // consumed result. The consumed result is always an r-value. |
| 1976 | builder.accessChainStore(result); |
| 1977 | builder.clearAccessChain(); |
| 1978 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1979 | node->getOp() == glslang::EOpPreDecrement) |
| 1980 | builder.setAccessChainRValue(result); |
| 1981 | else |
| 1982 | builder.setAccessChainRValue(operand); |
| 1983 | } |
| 1984 | |
| 1985 | return false; |
| 1986 | |
| 1987 | case glslang::EOpEmitStreamVertex: |
| 1988 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1989 | return false; |
| 1990 | case glslang::EOpEndStreamPrimitive: |
| 1991 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1992 | return false; |
| 1993 | |
| 1994 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1995 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1996 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1997 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2001 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 2002 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2003 | if (node->getType().getQualifier().isSpecConstant()) |
| 2004 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2005 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2006 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2007 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2008 | 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] | 2009 | |
| 2010 | // try texturing |
| 2011 | result = createImageTextureFunctionCall(node); |
| 2012 | if (result != spv::NoResult) { |
| 2013 | builder.clearAccessChain(); |
| 2014 | builder.setAccessChainRValue(result); |
| 2015 | |
| 2016 | return false; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2017 | } else if (node->getOp() == glslang::EOpImageStore || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 2018 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2019 | node->getOp() == glslang::EOpImageStoreLod || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 2020 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2021 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2022 | // "imageStore" is a special case, which has no result |
| 2023 | return false; |
| 2024 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2025 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2026 | glslang::TOperator binOp = glslang::EOpNull; |
| 2027 | bool reduceComparison = true; |
| 2028 | bool isMatrix = false; |
| 2029 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2030 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2031 | |
| 2032 | assert(node->getOp()); |
| 2033 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 2034 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2035 | |
| 2036 | switch (node->getOp()) { |
| 2037 | case glslang::EOpSequence: |
| 2038 | { |
| 2039 | if (preVisit) |
| 2040 | ++sequenceDepth; |
| 2041 | else |
| 2042 | --sequenceDepth; |
| 2043 | |
| 2044 | if (sequenceDepth == 1) { |
| 2045 | // If this is the parent node of all the functions, we want to see them |
| 2046 | // early, so all call points have actual SPIR-V functions to reference. |
| 2047 | // In all cases, still let the traverser visit the children for us. |
| 2048 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 2049 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2050 | // 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] | 2051 | // anything else gets there, so visit out of order, doing them all now. |
| 2052 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2053 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2054 | // 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] | 2055 | // so do them manually. |
| 2056 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2057 | |
| 2058 | return false; |
| 2059 | } |
| 2060 | |
| 2061 | return true; |
| 2062 | } |
| 2063 | case glslang::EOpLinkerObjects: |
| 2064 | { |
| 2065 | if (visit == glslang::EvPreVisit) |
| 2066 | linkageOnly = true; |
| 2067 | else |
| 2068 | linkageOnly = false; |
| 2069 | |
| 2070 | return true; |
| 2071 | } |
| 2072 | case glslang::EOpComma: |
| 2073 | { |
| 2074 | // processing from left to right naturally leaves the right-most |
| 2075 | // lying around in the access chain |
| 2076 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2077 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2078 | glslangOperands[i]->traverse(this); |
| 2079 | |
| 2080 | return false; |
| 2081 | } |
| 2082 | case glslang::EOpFunction: |
| 2083 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2084 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2085 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2086 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2087 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2088 | } else { |
| 2089 | handleFunctionEntry(node); |
| 2090 | } |
| 2091 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2092 | if (inEntryPoint) |
| 2093 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2094 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2095 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | return true; |
| 2099 | case glslang::EOpParameters: |
| 2100 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2101 | // the body, so we still visited the function node's children, making this |
| 2102 | // child redundant. |
| 2103 | return false; |
| 2104 | case glslang::EOpFunctionCall: |
| 2105 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2106 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2107 | if (node->isUserDefined()) |
| 2108 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2109 | // 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] | 2110 | if (result) { |
| 2111 | builder.clearAccessChain(); |
| 2112 | builder.setAccessChainRValue(result); |
| 2113 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2114 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2115 | |
| 2116 | return false; |
| 2117 | } |
| 2118 | case glslang::EOpConstructMat2x2: |
| 2119 | case glslang::EOpConstructMat2x3: |
| 2120 | case glslang::EOpConstructMat2x4: |
| 2121 | case glslang::EOpConstructMat3x2: |
| 2122 | case glslang::EOpConstructMat3x3: |
| 2123 | case glslang::EOpConstructMat3x4: |
| 2124 | case glslang::EOpConstructMat4x2: |
| 2125 | case glslang::EOpConstructMat4x3: |
| 2126 | case glslang::EOpConstructMat4x4: |
| 2127 | case glslang::EOpConstructDMat2x2: |
| 2128 | case glslang::EOpConstructDMat2x3: |
| 2129 | case glslang::EOpConstructDMat2x4: |
| 2130 | case glslang::EOpConstructDMat3x2: |
| 2131 | case glslang::EOpConstructDMat3x3: |
| 2132 | case glslang::EOpConstructDMat3x4: |
| 2133 | case glslang::EOpConstructDMat4x2: |
| 2134 | case glslang::EOpConstructDMat4x3: |
| 2135 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2136 | case glslang::EOpConstructIMat2x2: |
| 2137 | case glslang::EOpConstructIMat2x3: |
| 2138 | case glslang::EOpConstructIMat2x4: |
| 2139 | case glslang::EOpConstructIMat3x2: |
| 2140 | case glslang::EOpConstructIMat3x3: |
| 2141 | case glslang::EOpConstructIMat3x4: |
| 2142 | case glslang::EOpConstructIMat4x2: |
| 2143 | case glslang::EOpConstructIMat4x3: |
| 2144 | case glslang::EOpConstructIMat4x4: |
| 2145 | case glslang::EOpConstructUMat2x2: |
| 2146 | case glslang::EOpConstructUMat2x3: |
| 2147 | case glslang::EOpConstructUMat2x4: |
| 2148 | case glslang::EOpConstructUMat3x2: |
| 2149 | case glslang::EOpConstructUMat3x3: |
| 2150 | case glslang::EOpConstructUMat3x4: |
| 2151 | case glslang::EOpConstructUMat4x2: |
| 2152 | case glslang::EOpConstructUMat4x3: |
| 2153 | case glslang::EOpConstructUMat4x4: |
| 2154 | case glslang::EOpConstructBMat2x2: |
| 2155 | case glslang::EOpConstructBMat2x3: |
| 2156 | case glslang::EOpConstructBMat2x4: |
| 2157 | case glslang::EOpConstructBMat3x2: |
| 2158 | case glslang::EOpConstructBMat3x3: |
| 2159 | case glslang::EOpConstructBMat3x4: |
| 2160 | case glslang::EOpConstructBMat4x2: |
| 2161 | case glslang::EOpConstructBMat4x3: |
| 2162 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2163 | case glslang::EOpConstructF16Mat2x2: |
| 2164 | case glslang::EOpConstructF16Mat2x3: |
| 2165 | case glslang::EOpConstructF16Mat2x4: |
| 2166 | case glslang::EOpConstructF16Mat3x2: |
| 2167 | case glslang::EOpConstructF16Mat3x3: |
| 2168 | case glslang::EOpConstructF16Mat3x4: |
| 2169 | case glslang::EOpConstructF16Mat4x2: |
| 2170 | case glslang::EOpConstructF16Mat4x3: |
| 2171 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2172 | isMatrix = true; |
| 2173 | // fall through |
| 2174 | case glslang::EOpConstructFloat: |
| 2175 | case glslang::EOpConstructVec2: |
| 2176 | case glslang::EOpConstructVec3: |
| 2177 | case glslang::EOpConstructVec4: |
| 2178 | case glslang::EOpConstructDouble: |
| 2179 | case glslang::EOpConstructDVec2: |
| 2180 | case glslang::EOpConstructDVec3: |
| 2181 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2182 | case glslang::EOpConstructFloat16: |
| 2183 | case glslang::EOpConstructF16Vec2: |
| 2184 | case glslang::EOpConstructF16Vec3: |
| 2185 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2186 | case glslang::EOpConstructBool: |
| 2187 | case glslang::EOpConstructBVec2: |
| 2188 | case glslang::EOpConstructBVec3: |
| 2189 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2190 | case glslang::EOpConstructInt8: |
| 2191 | case glslang::EOpConstructI8Vec2: |
| 2192 | case glslang::EOpConstructI8Vec3: |
| 2193 | case glslang::EOpConstructI8Vec4: |
| 2194 | case glslang::EOpConstructUint8: |
| 2195 | case glslang::EOpConstructU8Vec2: |
| 2196 | case glslang::EOpConstructU8Vec3: |
| 2197 | case glslang::EOpConstructU8Vec4: |
| 2198 | case glslang::EOpConstructInt16: |
| 2199 | case glslang::EOpConstructI16Vec2: |
| 2200 | case glslang::EOpConstructI16Vec3: |
| 2201 | case glslang::EOpConstructI16Vec4: |
| 2202 | case glslang::EOpConstructUint16: |
| 2203 | case glslang::EOpConstructU16Vec2: |
| 2204 | case glslang::EOpConstructU16Vec3: |
| 2205 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2206 | case glslang::EOpConstructInt: |
| 2207 | case glslang::EOpConstructIVec2: |
| 2208 | case glslang::EOpConstructIVec3: |
| 2209 | case glslang::EOpConstructIVec4: |
| 2210 | case glslang::EOpConstructUint: |
| 2211 | case glslang::EOpConstructUVec2: |
| 2212 | case glslang::EOpConstructUVec3: |
| 2213 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2214 | case glslang::EOpConstructInt64: |
| 2215 | case glslang::EOpConstructI64Vec2: |
| 2216 | case glslang::EOpConstructI64Vec3: |
| 2217 | case glslang::EOpConstructI64Vec4: |
| 2218 | case glslang::EOpConstructUint64: |
| 2219 | case glslang::EOpConstructU64Vec2: |
| 2220 | case glslang::EOpConstructU64Vec3: |
| 2221 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2222 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2223 | case glslang::EOpConstructTextureSampler: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2224 | case glslang::EOpConstructReference: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2225 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2226 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2227 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2228 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2229 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2230 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2231 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2232 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2233 | std::vector<spv::Id> constituents; |
| 2234 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2235 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2236 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2237 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2238 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2239 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2240 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2241 | |
| 2242 | builder.clearAccessChain(); |
| 2243 | builder.setAccessChainRValue(constructed); |
| 2244 | |
| 2245 | return false; |
| 2246 | } |
| 2247 | |
| 2248 | // These six are component-wise compares with component-wise results. |
| 2249 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2250 | case glslang::EOpLessThan: |
| 2251 | case glslang::EOpGreaterThan: |
| 2252 | case glslang::EOpLessThanEqual: |
| 2253 | case glslang::EOpGreaterThanEqual: |
| 2254 | case glslang::EOpVectorEqual: |
| 2255 | case glslang::EOpVectorNotEqual: |
| 2256 | { |
| 2257 | // Map the operation to a binary |
| 2258 | binOp = node->getOp(); |
| 2259 | reduceComparison = false; |
| 2260 | switch (node->getOp()) { |
| 2261 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2262 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2263 | default: binOp = node->getOp(); break; |
| 2264 | } |
| 2265 | |
| 2266 | break; |
| 2267 | } |
| 2268 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2269 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2270 | binOp = glslang::EOpMul; |
| 2271 | break; |
| 2272 | case glslang::EOpOuterProduct: |
| 2273 | // two vectors multiplied to make a matrix |
| 2274 | binOp = glslang::EOpOuterProduct; |
| 2275 | break; |
| 2276 | case glslang::EOpDot: |
| 2277 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2278 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2279 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2280 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2281 | binOp = glslang::EOpMul; |
| 2282 | break; |
| 2283 | } |
| 2284 | case glslang::EOpMod: |
| 2285 | // when an aggregate, this is the floating-point mod built-in function, |
| 2286 | // which can be emitted by the one in createBinaryOperation() |
| 2287 | binOp = glslang::EOpMod; |
| 2288 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2289 | case glslang::EOpEmitVertex: |
| 2290 | case glslang::EOpEndPrimitive: |
| 2291 | case glslang::EOpBarrier: |
| 2292 | case glslang::EOpMemoryBarrier: |
| 2293 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2294 | case glslang::EOpMemoryBarrierBuffer: |
| 2295 | case glslang::EOpMemoryBarrierImage: |
| 2296 | case glslang::EOpMemoryBarrierShared: |
| 2297 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2298 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2299 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2300 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2301 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2302 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2303 | case glslang::EOpSubgroupBarrier: |
| 2304 | case glslang::EOpSubgroupMemoryBarrier: |
| 2305 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2306 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2307 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2308 | noReturnValue = true; |
| 2309 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2310 | break; |
| 2311 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2312 | case glslang::EOpAtomicStore: |
| 2313 | noReturnValue = true; |
| 2314 | // fallthrough |
| 2315 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2316 | case glslang::EOpAtomicAdd: |
| 2317 | case glslang::EOpAtomicMin: |
| 2318 | case glslang::EOpAtomicMax: |
| 2319 | case glslang::EOpAtomicAnd: |
| 2320 | case glslang::EOpAtomicOr: |
| 2321 | case glslang::EOpAtomicXor: |
| 2322 | case glslang::EOpAtomicExchange: |
| 2323 | case glslang::EOpAtomicCompSwap: |
| 2324 | atomic = true; |
| 2325 | break; |
| 2326 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2327 | case glslang::EOpAtomicCounterAdd: |
| 2328 | case glslang::EOpAtomicCounterSubtract: |
| 2329 | case glslang::EOpAtomicCounterMin: |
| 2330 | case glslang::EOpAtomicCounterMax: |
| 2331 | case glslang::EOpAtomicCounterAnd: |
| 2332 | case glslang::EOpAtomicCounterOr: |
| 2333 | case glslang::EOpAtomicCounterXor: |
| 2334 | case glslang::EOpAtomicCounterExchange: |
| 2335 | case glslang::EOpAtomicCounterCompSwap: |
| 2336 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2337 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2338 | atomic = true; |
| 2339 | break; |
| 2340 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2341 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 2342 | case glslang::EOpIgnoreIntersectionNV: |
| 2343 | case glslang::EOpTerminateRayNV: |
| 2344 | case glslang::EOpTraceNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 2345 | case glslang::EOpExecuteCallableNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2346 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2347 | noReturnValue = true; |
| 2348 | break; |
| 2349 | #endif |
| 2350 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2351 | default: |
| 2352 | break; |
| 2353 | } |
| 2354 | |
| 2355 | // |
| 2356 | // See if it maps to a regular operation. |
| 2357 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2358 | if (binOp != glslang::EOpNull) { |
| 2359 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2360 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2361 | assert(left && right); |
| 2362 | |
| 2363 | builder.clearAccessChain(); |
| 2364 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2365 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2366 | |
| 2367 | builder.clearAccessChain(); |
| 2368 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2369 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2370 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2371 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2372 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2373 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2374 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2375 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2376 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2377 | left->getType().getBasicType(), reduceComparison); |
| 2378 | |
| 2379 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2380 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2381 | builder.clearAccessChain(); |
| 2382 | builder.setAccessChainRValue(result); |
| 2383 | |
| 2384 | return false; |
| 2385 | } |
| 2386 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2387 | // |
| 2388 | // Create the list of operands. |
| 2389 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2390 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2391 | std::vector<spv::Id> operands; |
| 2392 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2393 | // special case l-value operands; there are just a few |
| 2394 | bool lvalue = false; |
| 2395 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2396 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2397 | case glslang::EOpModf: |
| 2398 | if (arg == 1) |
| 2399 | lvalue = true; |
| 2400 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2401 | case glslang::EOpInterpolateAtSample: |
| 2402 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2403 | #ifdef AMD_EXTENSIONS |
| 2404 | case glslang::EOpInterpolateAtVertex: |
| 2405 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2406 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2407 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2408 | |
| 2409 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2410 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2411 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2412 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2413 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 2414 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2415 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2416 | case glslang::EOpAtomicAdd: |
| 2417 | case glslang::EOpAtomicMin: |
| 2418 | case glslang::EOpAtomicMax: |
| 2419 | case glslang::EOpAtomicAnd: |
| 2420 | case glslang::EOpAtomicOr: |
| 2421 | case glslang::EOpAtomicXor: |
| 2422 | case glslang::EOpAtomicExchange: |
| 2423 | case glslang::EOpAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2424 | case glslang::EOpAtomicLoad: |
| 2425 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2426 | case glslang::EOpAtomicCounterAdd: |
| 2427 | case glslang::EOpAtomicCounterSubtract: |
| 2428 | case glslang::EOpAtomicCounterMin: |
| 2429 | case glslang::EOpAtomicCounterMax: |
| 2430 | case glslang::EOpAtomicCounterAnd: |
| 2431 | case glslang::EOpAtomicCounterOr: |
| 2432 | case glslang::EOpAtomicCounterXor: |
| 2433 | case glslang::EOpAtomicCounterExchange: |
| 2434 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2435 | if (arg == 0) |
| 2436 | lvalue = true; |
| 2437 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2438 | case glslang::EOpAddCarry: |
| 2439 | case glslang::EOpSubBorrow: |
| 2440 | if (arg == 2) |
| 2441 | lvalue = true; |
| 2442 | break; |
| 2443 | case glslang::EOpUMulExtended: |
| 2444 | case glslang::EOpIMulExtended: |
| 2445 | if (arg >= 2) |
| 2446 | lvalue = true; |
| 2447 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2448 | default: |
| 2449 | break; |
| 2450 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2451 | builder.clearAccessChain(); |
| 2452 | if (invertedType != spv::NoType && arg == 0) |
| 2453 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2454 | else |
| 2455 | glslangOperands[arg]->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2456 | if (lvalue) |
| 2457 | operands.push_back(builder.accessChainGetLValue()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2458 | else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2459 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2460 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2461 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2462 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2463 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2464 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2465 | if (atomic) { |
| 2466 | // Handle all atomics |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2467 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2468 | } else { |
| 2469 | // Pass through to generic operations. |
| 2470 | switch (glslangOperands.size()) { |
| 2471 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2472 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2473 | break; |
| 2474 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2475 | { |
| 2476 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2477 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2478 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2479 | result = createUnaryOperation( |
| 2480 | node->getOp(), decorations, |
| 2481 | resultType(), operands.front(), |
| 2482 | glslangOperands[0]->getAsTyped()->getBasicType()); |
| 2483 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2484 | break; |
| 2485 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2486 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2487 | break; |
| 2488 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2489 | if (invertedType) |
| 2490 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | if (noReturnValue) |
| 2494 | return false; |
| 2495 | |
| 2496 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2497 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2498 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2499 | } else { |
| 2500 | builder.clearAccessChain(); |
| 2501 | builder.setAccessChainRValue(result); |
| 2502 | return false; |
| 2503 | } |
| 2504 | } |
| 2505 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2506 | // This path handles both if-then-else and ?: |
| 2507 | // The if-then-else has a node type of void, while |
| 2508 | // ?: has either a void or a non-void node type |
| 2509 | // |
| 2510 | // Leaving the result, when not void: |
| 2511 | // GLSL only has r-values as the result of a :?, but |
| 2512 | // if we have an l-value, that can be more efficient if it will |
| 2513 | // become the base of a complex r-value expression, because the |
| 2514 | // 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] | 2515 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 2516 | { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2517 | // See if it simple and safe, or required, to execute both sides. |
| 2518 | // Crucially, side effects must be either semantically required or avoided, |
| 2519 | // and there are performance trade-offs. |
| 2520 | // Return true if required or a good idea (and safe) to execute both sides, |
| 2521 | // false otherwise. |
| 2522 | const auto bothSidesPolicy = [&]() -> bool { |
| 2523 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2524 | if (node->getTrueBlock() == nullptr || |
| 2525 | node->getFalseBlock() == nullptr) |
| 2526 | return false; |
| 2527 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2528 | // required? (unless we write additional code to look for side effects |
| 2529 | // and make performance trade-offs if none are present) |
| 2530 | if (!node->getShortCircuit()) |
| 2531 | return true; |
| 2532 | |
| 2533 | // if not required to execute both, decide based on performance/practicality... |
| 2534 | |
| 2535 | // see if OpSelect can handle it |
| 2536 | if ((!node->getType().isScalar() && !node->getType().isVector()) || |
| 2537 | node->getBasicType() == glslang::EbtVoid) |
| 2538 | return false; |
| 2539 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2540 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 2541 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 2542 | |
| 2543 | // return true if a single operand to ? : is okay for OpSelect |
| 2544 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2545 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2546 | }; |
| 2547 | |
| 2548 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 2549 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 2550 | }; |
| 2551 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2552 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 2553 | // emit the condition before doing anything with selection |
| 2554 | node->getCondition()->traverse(this); |
| 2555 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 2556 | |
| 2557 | // Find a way of executing both sides and selecting the right result. |
| 2558 | const auto executeBothSides = [&]() -> void { |
| 2559 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2560 | node->getTrueBlock()->traverse(this); |
| 2561 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2562 | node->getFalseBlock()->traverse(this); |
| 2563 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2564 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2565 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2566 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2567 | // done if void |
| 2568 | if (node->getBasicType() == glslang::EbtVoid) |
| 2569 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 2570 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2571 | // emit code to select between trueValue and falseValue |
| 2572 | |
| 2573 | // see if OpSelect can handle it |
| 2574 | if (node->getType().isScalar() || node->getType().isVector()) { |
| 2575 | // Emit OpSelect for this selection. |
| 2576 | |
| 2577 | // smear condition to vector, if necessary (AST is always scalar) |
| 2578 | if (builder.isVector(trueValue)) |
| 2579 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 2580 | builder.makeVectorType(builder.makeBoolType(), |
| 2581 | builder.getNumComponents(trueValue))); |
| 2582 | |
| 2583 | // OpSelect |
| 2584 | result = builder.createTriOp(spv::OpSelect, |
| 2585 | convertGlslangToSpvType(node->getType()), condition, |
| 2586 | trueValue, falseValue); |
| 2587 | |
| 2588 | builder.clearAccessChain(); |
| 2589 | builder.setAccessChainRValue(result); |
| 2590 | } else { |
| 2591 | // We need control flow to select the result. |
| 2592 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 2593 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2594 | |
| 2595 | // Selection control: |
| 2596 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2597 | |
| 2598 | // make an "if" based on the value created by the condition |
| 2599 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2600 | |
| 2601 | // emit the "then" statement |
| 2602 | builder.createStore(trueValue, result); |
| 2603 | ifBuilder.makeBeginElse(); |
| 2604 | // emit the "else" statement |
| 2605 | builder.createStore(falseValue, result); |
| 2606 | |
| 2607 | // finish off the control flow |
| 2608 | ifBuilder.makeEndIf(); |
| 2609 | |
| 2610 | builder.clearAccessChain(); |
| 2611 | builder.setAccessChainLValue(result); |
| 2612 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2613 | }; |
| 2614 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2615 | // Execute the one side needed, as per the condition |
| 2616 | const auto executeOneSide = [&]() { |
| 2617 | // Always emit control flow. |
| 2618 | if (node->getBasicType() != glslang::EbtVoid) |
| 2619 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2620 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2621 | // Selection control: |
| 2622 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2623 | |
| 2624 | // make an "if" based on the value created by the condition |
| 2625 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2626 | |
| 2627 | // emit the "then" statement |
| 2628 | if (node->getTrueBlock() != nullptr) { |
| 2629 | node->getTrueBlock()->traverse(this); |
| 2630 | if (result != spv::NoResult) |
| 2631 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 2632 | } |
| 2633 | |
| 2634 | if (node->getFalseBlock() != nullptr) { |
| 2635 | ifBuilder.makeBeginElse(); |
| 2636 | // emit the "else" statement |
| 2637 | node->getFalseBlock()->traverse(this); |
| 2638 | if (result != spv::NoResult) |
| 2639 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 2640 | } |
| 2641 | |
| 2642 | // finish off the control flow |
| 2643 | ifBuilder.makeEndIf(); |
| 2644 | |
| 2645 | if (result != spv::NoResult) { |
| 2646 | builder.clearAccessChain(); |
| 2647 | builder.setAccessChainLValue(result); |
| 2648 | } |
| 2649 | }; |
| 2650 | |
| 2651 | // Try for OpSelect (or a requirement to execute both sides) |
| 2652 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2653 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2654 | if (node->getType().getQualifier().isSpecConstant()) |
| 2655 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2656 | executeBothSides(); |
| 2657 | } else |
| 2658 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2659 | |
| 2660 | return false; |
| 2661 | } |
| 2662 | |
| 2663 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2664 | { |
| 2665 | // emit and get the condition before doing anything with switch |
| 2666 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2667 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2668 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2669 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 2670 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2671 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2672 | // browse the children to sort out code segments |
| 2673 | int defaultSegment = -1; |
| 2674 | std::vector<TIntermNode*> codeSegments; |
| 2675 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 2676 | std::vector<int> caseValues; |
| 2677 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 2678 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 2679 | TIntermNode* child = *c; |
| 2680 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2681 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2682 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2683 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2684 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2685 | } else |
| 2686 | codeSegments.push_back(child); |
| 2687 | } |
| 2688 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2689 | // 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] | 2690 | // statements between the last case and the end of the switch statement |
| 2691 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 2692 | (int)codeSegments.size() == defaultSegment) |
| 2693 | codeSegments.push_back(nullptr); |
| 2694 | |
| 2695 | // make the switch statement |
| 2696 | 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] | 2697 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2698 | |
| 2699 | // emit all the code in the segments |
| 2700 | breakForLoop.push(false); |
| 2701 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 2702 | builder.nextSwitchSegment(segmentBlocks, s); |
| 2703 | if (codeSegments[s]) |
| 2704 | codeSegments[s]->traverse(this); |
| 2705 | else |
| 2706 | builder.addSwitchBreak(); |
| 2707 | } |
| 2708 | breakForLoop.pop(); |
| 2709 | |
| 2710 | builder.endSwitch(segmentBlocks); |
| 2711 | |
| 2712 | return false; |
| 2713 | } |
| 2714 | |
| 2715 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 2716 | { |
| 2717 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2718 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2719 | |
| 2720 | builder.clearAccessChain(); |
| 2721 | builder.setAccessChainRValue(constant); |
| 2722 | } |
| 2723 | |
| 2724 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 2725 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2726 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2727 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2728 | |
| 2729 | // Loop control: |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2730 | unsigned int dependencyLength = glslang::TIntermLoop::dependencyInfinite; |
| 2731 | const spv::LoopControlMask control = TranslateLoopControl(*node, dependencyLength); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2732 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2733 | // Spec requires back edges to target header blocks, and every header block |
| 2734 | // must dominate its merge block. Make a header block first to ensure these |
| 2735 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 2736 | // by a block-ending branch. But we don't want to put any other body/test |
| 2737 | // instructions in it, since the body/test may have arbitrary instructions, |
| 2738 | // including merges of its own. |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2739 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2740 | builder.setBuildPoint(&blocks.head); |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2741 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, dependencyLength); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2742 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2743 | spv::Block& test = builder.makeNewBlock(); |
| 2744 | builder.createBranch(&test); |
| 2745 | |
| 2746 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2747 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2748 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2749 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 2750 | |
| 2751 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2752 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2753 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2754 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2755 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2756 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2757 | |
| 2758 | builder.setBuildPoint(&blocks.continue_target); |
| 2759 | if (node->getTerminal()) |
| 2760 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2761 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 2762 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2763 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2764 | builder.createBranch(&blocks.body); |
| 2765 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2766 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2767 | builder.setBuildPoint(&blocks.body); |
| 2768 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2769 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2770 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2771 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2772 | |
| 2773 | builder.setBuildPoint(&blocks.continue_target); |
| 2774 | if (node->getTerminal()) |
| 2775 | node->getTerminal()->traverse(this); |
| 2776 | if (node->getTest()) { |
| 2777 | node->getTest()->traverse(this); |
| 2778 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2779 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2780 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2781 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 2782 | // TODO: unless there was a break/return/discard instruction |
| 2783 | // somewhere in the body, this is an infinite loop, so we should |
| 2784 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2785 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2786 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2787 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2788 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2789 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2790 | return false; |
| 2791 | } |
| 2792 | |
| 2793 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 2794 | { |
| 2795 | if (node->getExpression()) |
| 2796 | node->getExpression()->traverse(this); |
| 2797 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2798 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2799 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2800 | switch (node->getFlowOp()) { |
| 2801 | case glslang::EOpKill: |
| 2802 | builder.makeDiscard(); |
| 2803 | break; |
| 2804 | case glslang::EOpBreak: |
| 2805 | if (breakForLoop.top()) |
| 2806 | builder.createLoopExit(); |
| 2807 | else |
| 2808 | builder.addSwitchBreak(); |
| 2809 | break; |
| 2810 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2811 | builder.createLoopContinue(); |
| 2812 | break; |
| 2813 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2814 | if (node->getExpression()) { |
| 2815 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 2816 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 2817 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 2818 | builder.clearAccessChain(); |
| 2819 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 2820 | builder.setAccessChainLValue(copyId); |
| 2821 | multiTypeStore(glslangReturnType, returnId); |
| 2822 | returnId = builder.createLoad(copyId); |
| 2823 | } |
| 2824 | builder.makeReturn(false, returnId); |
| 2825 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2826 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2827 | |
| 2828 | builder.clearAccessChain(); |
| 2829 | break; |
| 2830 | |
| 2831 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2832 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2833 | break; |
| 2834 | } |
| 2835 | |
| 2836 | return false; |
| 2837 | } |
| 2838 | |
| 2839 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 2840 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2841 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2842 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2843 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 2844 | if (node->getQualifier().isConstant()) { |
Dan Sinclair | 12fcaa2 | 2018-11-13 09:17:44 -0500 | [diff] [blame] | 2845 | spv::Id result = createSpvConstant(*node); |
| 2846 | if (result != spv::NoResult) |
| 2847 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2851 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2852 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 2853 | |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2854 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 2855 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 2856 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2857 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2858 | switch (storageClass) { |
| 2859 | case spv::StorageClassInput: |
| 2860 | case spv::StorageClassOutput: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2861 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2862 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2863 | break; |
| 2864 | case spv::StorageClassPushConstant: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2865 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2866 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2867 | break; |
| 2868 | case spv::StorageClassUniform: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2869 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2870 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2871 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2872 | else |
| 2873 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 2874 | break; |
| 2875 | case spv::StorageClassStorageBuffer: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2876 | case spv::StorageClassPhysicalStorageBufferEXT: |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2877 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
| 2878 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 2879 | break; |
| 2880 | default: |
| 2881 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2882 | } |
| 2883 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2884 | |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 2885 | const bool contains8BitType = node->getType().containsBasicType(glslang::EbtInt8) || |
| 2886 | node->getType().containsBasicType(glslang::EbtUint8); |
| 2887 | if (contains8BitType) { |
| 2888 | if (storageClass == spv::StorageClassPushConstant) { |
| 2889 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2890 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 2891 | } else if (storageClass == spv::StorageClassUniform) { |
| 2892 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2893 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 2894 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
| 2895 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2896 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 2897 | } |
| 2898 | } |
| 2899 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2900 | const char* name = node->getName().c_str(); |
| 2901 | if (glslang::IsAnonymous(name)) |
| 2902 | name = ""; |
| 2903 | |
| 2904 | return builder.createVariable(storageClass, spvType, name); |
| 2905 | } |
| 2906 | |
| 2907 | // Return type Id of the sampled type. |
| 2908 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 2909 | { |
| 2910 | switch (sampler.type) { |
| 2911 | case glslang::EbtFloat: return builder.makeFloatType(32); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 2912 | #ifdef AMD_EXTENSIONS |
| 2913 | case glslang::EbtFloat16: |
| 2914 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 2915 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 2916 | return builder.makeFloatType(16); |
| 2917 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2918 | case glslang::EbtInt: return builder.makeIntType(32); |
| 2919 | case glslang::EbtUint: return builder.makeUintType(32); |
| 2920 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2921 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2922 | return builder.makeFloatType(32); |
| 2923 | } |
| 2924 | } |
| 2925 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2926 | // If node is a swizzle operation, return the type that should be used if |
| 2927 | // the swizzle base is first consumed by another operation, before the swizzle |
| 2928 | // is applied. |
| 2929 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 2930 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2931 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2932 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2933 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 2934 | else |
| 2935 | return spv::NoType; |
| 2936 | } |
| 2937 | |
| 2938 | // When inverting a swizzle with a parent op, this function |
| 2939 | // will apply the swizzle operation to a completed parent operation. |
| 2940 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 2941 | { |
| 2942 | std::vector<unsigned> swizzle; |
| 2943 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 2944 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 2945 | } |
| 2946 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2947 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 2948 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 2949 | { |
| 2950 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 2951 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 2952 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2953 | } |
| 2954 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2955 | // Convert from a glslang type to an SPV type, by calling into a |
| 2956 | // recursive version of this function. This establishes the inherited |
| 2957 | // layout state rooted from the top-level type. |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2958 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2959 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2960 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | // 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] | 2964 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2965 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2966 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2967 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 2968 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2969 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2970 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2971 | |
| 2972 | switch (type.getBasicType()) { |
| 2973 | case glslang::EbtVoid: |
| 2974 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2975 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2976 | break; |
| 2977 | case glslang::EbtFloat: |
| 2978 | spvType = builder.makeFloatType(32); |
| 2979 | break; |
| 2980 | case glslang::EbtDouble: |
| 2981 | spvType = builder.makeFloatType(64); |
| 2982 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2983 | case glslang::EbtFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2984 | spvType = builder.makeFloatType(16); |
| 2985 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2986 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2987 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 2988 | // a 32-bit int where non-0 means true. |
| 2989 | if (explicitLayout != glslang::ElpNone) |
| 2990 | spvType = builder.makeUintType(32); |
| 2991 | else |
| 2992 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2993 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 2994 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2995 | spvType = builder.makeIntType(8); |
| 2996 | break; |
| 2997 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2998 | spvType = builder.makeUintType(8); |
| 2999 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3000 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3001 | spvType = builder.makeIntType(16); |
| 3002 | break; |
| 3003 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3004 | spvType = builder.makeUintType(16); |
| 3005 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3006 | case glslang::EbtInt: |
| 3007 | spvType = builder.makeIntType(32); |
| 3008 | break; |
| 3009 | case glslang::EbtUint: |
| 3010 | spvType = builder.makeUintType(32); |
| 3011 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3012 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3013 | spvType = builder.makeIntType(64); |
| 3014 | break; |
| 3015 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3016 | spvType = builder.makeUintType(64); |
| 3017 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3018 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 3019 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3020 | spvType = builder.makeUintType(32); |
| 3021 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3022 | #ifdef NV_EXTENSIONS |
| 3023 | case glslang::EbtAccStructNV: |
| 3024 | spvType = builder.makeAccelerationStructureNVType(); |
| 3025 | break; |
| 3026 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3027 | case glslang::EbtSampler: |
| 3028 | { |
| 3029 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3030 | if (sampler.sampler) { |
| 3031 | // pure sampler |
| 3032 | spvType = builder.makeSamplerType(); |
| 3033 | } else { |
| 3034 | // an image is present, make its type |
| 3035 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 3036 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 3037 | if (sampler.combined) { |
| 3038 | // already has both image and sampler, make the combined type |
| 3039 | spvType = builder.makeSampledImageType(spvType); |
| 3040 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3041 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3042 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3043 | break; |
| 3044 | case glslang::EbtStruct: |
| 3045 | case glslang::EbtBlock: |
| 3046 | { |
| 3047 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3048 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3049 | |
| 3050 | // Try to share structs for different layouts, but not yet for other |
| 3051 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3052 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3053 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3054 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3055 | break; |
| 3056 | |
| 3057 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3058 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3059 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 3060 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3061 | } |
| 3062 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3063 | case glslang::EbtReference: |
| 3064 | { |
| 3065 | // Make the forward pointer, then recurse to convert the structure type, then |
| 3066 | // patch up the forward pointer with a real pointer type. |
| 3067 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 3068 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 3069 | forwardPointers[type.getReferentType()] = forwardId; |
| 3070 | } |
| 3071 | spvType = forwardPointers[type.getReferentType()]; |
| 3072 | if (!forwardReferenceOnly) { |
| 3073 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 3074 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 3075 | forwardPointers[type.getReferentType()], |
| 3076 | referentType); |
| 3077 | } |
| 3078 | } |
| 3079 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3080 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3081 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3082 | break; |
| 3083 | } |
| 3084 | |
| 3085 | if (type.isMatrix()) |
| 3086 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3087 | else { |
| 3088 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3089 | if (type.getVectorSize() > 1) |
| 3090 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3091 | } |
| 3092 | |
| 3093 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3094 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3095 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3096 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3097 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3098 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3099 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3100 | // Use a dummy glslang type for querying internal strides of |
| 3101 | // arrays of arrays, but using just a one-dimensional array. |
| 3102 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3103 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3104 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3105 | |
| 3106 | // Will compute the higher-order strides here, rather than making a whole |
| 3107 | // pile of types and doing repetitive recursion on their contents. |
| 3108 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3109 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3110 | |
| 3111 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3112 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3113 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3114 | if (stride > 0) |
| 3115 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3116 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3117 | } |
| 3118 | } else { |
| 3119 | // single-dimensional array, and don't yet have stride |
| 3120 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3121 | // 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] | 3122 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3123 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3124 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3125 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3126 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3127 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3128 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3129 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3130 | else { |
| 3131 | if (!lastBufferBlockMember) { |
| 3132 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 3133 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3134 | } |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3135 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3136 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3137 | if (stride > 0) |
| 3138 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | return spvType; |
| 3142 | } |
| 3143 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3144 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3145 | // |
| 3146 | // Identify interface members that don't have their required extension turned on. |
| 3147 | // |
| 3148 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3149 | { |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3150 | #ifdef NV_EXTENSIONS |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3151 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3152 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3153 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3154 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3155 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3156 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3157 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3158 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3159 | |
| 3160 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3161 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3162 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3163 | return true; |
| 3164 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3165 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3166 | return true; |
| 3167 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3168 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3169 | return true; |
| 3170 | } |
| 3171 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3172 | |
| 3173 | return false; |
| 3174 | }; |
| 3175 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3176 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3177 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3178 | // Mutually recursive with convertGlslangToSpvType(). |
| 3179 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3180 | const glslang::TTypeList* glslangMembers, |
| 3181 | glslang::TLayoutPacking explicitLayout, |
| 3182 | const glslang::TQualifier& qualifier) |
| 3183 | { |
| 3184 | // Create a vector of struct types for SPIR-V to consume |
| 3185 | std::vector<spv::Id> spvMembers; |
| 3186 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3187 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3188 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3189 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3190 | if (glslangMember.hiddenMember()) { |
| 3191 | ++memberDelta; |
| 3192 | if (type.getBasicType() == glslang::EbtBlock) |
| 3193 | memberRemapper[glslangMembers][i] = -1; |
| 3194 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3195 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3196 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3197 | if (filterMember(glslangMember)) |
| 3198 | continue; |
| 3199 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3200 | // modify just this child's view of the qualifier |
| 3201 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3202 | InheritQualifiers(memberQualifier, qualifier); |
| 3203 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3204 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3205 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3206 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3207 | |
| 3208 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3209 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3210 | i == (int)glslangMembers->size() - 1; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3211 | |
| 3212 | // Make forward pointers for any pointer members, and create a list of members to |
| 3213 | // convert to spirv types after creating the struct. |
| 3214 | if (glslangMember.getBasicType() == glslang::EbtReference) { |
| 3215 | if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) { |
| 3216 | deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier)); |
| 3217 | } |
| 3218 | spvMembers.push_back( |
| 3219 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, true)); |
| 3220 | } else { |
| 3221 | spvMembers.push_back( |
| 3222 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, false)); |
| 3223 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | // Make the SPIR-V type |
| 3228 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3229 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3230 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3231 | |
| 3232 | // Decorate it |
| 3233 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3234 | |
John Kessenich | d72f488 | 2019-01-16 14:55:37 +0700 | [diff] [blame] | 3235 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3236 | auto it = deferredForwardPointers[i]; |
| 3237 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 3238 | } |
| 3239 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3240 | return spvType; |
| 3241 | } |
| 3242 | |
| 3243 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3244 | const glslang::TTypeList* glslangMembers, |
| 3245 | glslang::TLayoutPacking explicitLayout, |
| 3246 | const glslang::TQualifier& qualifier, |
| 3247 | spv::Id spvType) |
| 3248 | { |
| 3249 | // Name and decorate the non-hidden members |
| 3250 | int offset = -1; |
| 3251 | int locationOffset = 0; // for use within the members of this struct |
| 3252 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3253 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3254 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3255 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3256 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3257 | if (filterMember(glslangMember)) |
| 3258 | continue; |
| 3259 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3260 | |
| 3261 | // modify just this child's view of the qualifier |
| 3262 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3263 | InheritQualifiers(memberQualifier, qualifier); |
| 3264 | |
| 3265 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3266 | if (member < 0) |
| 3267 | continue; |
| 3268 | |
| 3269 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3270 | builder.addMemberDecoration(spvType, member, |
| 3271 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 3272 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 3273 | // Add interpolation and auxiliary storage decorations only to |
| 3274 | // top-level members of Input and Output storage classes |
| 3275 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 3276 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 3277 | if (type.getBasicType() == glslang::EbtBlock || |
| 3278 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 3279 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 3280 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3281 | #ifdef NV_EXTENSIONS |
| 3282 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 3283 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3284 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3285 | } |
| 3286 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3287 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3288 | if (type.getBasicType() == glslang::EbtBlock && |
| 3289 | qualifier.storage == glslang::EvqBuffer) { |
| 3290 | // Add memory decorations only to top-level members of shader storage block |
| 3291 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3292 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3293 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3294 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 3295 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3296 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3297 | // Location assignment was already completed correctly by the front end, |
| 3298 | // just track whether a member needs to be decorated. |
| 3299 | // Ignore member locations if the container is an array, as that's |
| 3300 | // ill-specified and decisions have been made to not allow this. |
| 3301 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 3302 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3303 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3304 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 3305 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 3306 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 3307 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3308 | // component, XFB, others |
| 3309 | if (glslangMember.getQualifier().hasComponent()) |
| 3310 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 3311 | glslangMember.getQualifier().layoutComponent); |
| 3312 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 3313 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 3314 | glslangMember.getQualifier().layoutXfbOffset); |
| 3315 | else if (explicitLayout != glslang::ElpNone) { |
| 3316 | // figure out what to do with offset, which is accumulating |
| 3317 | int nextOffset; |
| 3318 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 3319 | if (offset >= 0) |
| 3320 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 3321 | offset = nextOffset; |
| 3322 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3323 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3324 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 3325 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 3326 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3327 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3328 | // built-in variable decorations |
| 3329 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 3330 | if (builtIn != spv::BuiltInMax) |
| 3331 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3332 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3333 | // nonuniform |
| 3334 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 3335 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3336 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 3337 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 3338 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 3339 | memberQualifier.semanticName); |
| 3340 | } |
| 3341 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3342 | #ifdef NV_EXTENSIONS |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3343 | if (builtIn == spv::BuiltInLayer) { |
| 3344 | // SPV_NV_viewport_array2 extension |
| 3345 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 3346 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 3347 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 3348 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3349 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3350 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 3351 | builder.addMemberDecoration(spvType, member, |
| 3352 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 3353 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 3354 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 3355 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3356 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3357 | } |
| 3358 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 3359 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 3360 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 3361 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 3362 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3363 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3367 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 3368 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3369 | } |
| 3370 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3371 | // Turn the expression forming the array size into an id. |
| 3372 | // This is not quite trivial, because of specialization constants. |
| 3373 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 3374 | // a specialization constant expression is. |
| 3375 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 3376 | { |
| 3377 | // First, see if this is sized with a node, meaning a specialization constant: |
| 3378 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 3379 | if (specNode != nullptr) { |
| 3380 | builder.clearAccessChain(); |
| 3381 | specNode->traverse(this); |
| 3382 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 3383 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3384 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3385 | // Otherwise, need a compile-time (front end) size, get it: |
| 3386 | int size = arraySizes.getDimSize(dim); |
| 3387 | assert(size > 0); |
| 3388 | return builder.makeUintConstant(size); |
| 3389 | } |
| 3390 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3391 | // Wrap the builder's accessChainLoad to: |
| 3392 | // - localize handling of RelaxedPrecision |
| 3393 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 3394 | // (avoids unnecessary work and possible type punning for structures) |
| 3395 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3396 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 3397 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3398 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3399 | |
| 3400 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3401 | coherentFlags |= TranslateCoherent(type); |
| 3402 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3403 | unsigned int alignment = builder.getAccessChain().alignment; |
| 3404 | alignment |= getBufferReferenceAlignment(type); |
| 3405 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3406 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3407 | TranslateNonUniformDecoration(type.getQualifier()), |
| 3408 | nominalTypeId, |
| 3409 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3410 | TranslateMemoryScope(coherentFlags), |
| 3411 | alignment); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3412 | |
| 3413 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3414 | if (type.getBasicType() == glslang::EbtBool) { |
| 3415 | if (builder.isScalarType(nominalTypeId)) { |
| 3416 | // Conversion for bool |
| 3417 | spv::Id boolType = builder.makeBoolType(); |
| 3418 | if (nominalTypeId != boolType) |
| 3419 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 3420 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3421 | // Conversion for bvec |
| 3422 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3423 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 3424 | if (nominalTypeId != bvecType) |
| 3425 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 3426 | } |
| 3427 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3428 | |
| 3429 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3430 | } |
| 3431 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3432 | // Wrap the builder's accessChainStore to: |
| 3433 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3434 | // |
| 3435 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3436 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 3437 | { |
| 3438 | // Need to convert to abstract types when necessary |
| 3439 | if (type.getBasicType() == glslang::EbtBool) { |
| 3440 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 3441 | |
| 3442 | if (builder.isScalarType(nominalTypeId)) { |
| 3443 | // Conversion for bool |
| 3444 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3445 | if (nominalTypeId != boolType) { |
| 3446 | // keep these outside arguments, for determinant order-of-evaluation |
| 3447 | spv::Id one = builder.makeUintConstant(1); |
| 3448 | spv::Id zero = builder.makeUintConstant(0); |
| 3449 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 3450 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3451 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3452 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3453 | // Conversion for bvec |
| 3454 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3455 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3456 | if (nominalTypeId != bvecType) { |
| 3457 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 3458 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 3459 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 3460 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3461 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3462 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 3463 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3464 | } |
| 3465 | } |
| 3466 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3467 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3468 | coherentFlags |= TranslateCoherent(type); |
| 3469 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3470 | unsigned int alignment = builder.getAccessChain().alignment; |
| 3471 | alignment |= getBufferReferenceAlignment(type); |
| 3472 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3473 | builder.accessChainStore(rvalue, |
| 3474 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3475 | TranslateMemoryScope(coherentFlags), alignment); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3476 | } |
| 3477 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3478 | // For storing when types match at the glslang level, but not might match at the |
| 3479 | // SPIR-V level. |
| 3480 | // |
| 3481 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3482 | // 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] | 3483 | // as in a member-decorated way. |
| 3484 | // |
| 3485 | // NOTE: This function can handle any store request; if it's not special it |
| 3486 | // simplifies to a simple OpStore. |
| 3487 | // |
| 3488 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 3489 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 3490 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3491 | // we only do the complex path here if it's an aggregate |
| 3492 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3493 | accessChainStore(type, rValue); |
| 3494 | return; |
| 3495 | } |
| 3496 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3497 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3498 | spv::Id rType = builder.getTypeId(rValue); |
| 3499 | spv::Id lValue = builder.accessChainGetLValue(); |
| 3500 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 3501 | if (lType == rType) { |
| 3502 | accessChainStore(type, rValue); |
| 3503 | return; |
| 3504 | } |
| 3505 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3506 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3507 | // where the two types were the same type in GLSL. This requires member |
| 3508 | // by member copy, recursively. |
| 3509 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3510 | // If an array, copy element by element. |
| 3511 | if (type.isArray()) { |
| 3512 | glslang::TType glslangElementType(type, 0); |
| 3513 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 3514 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 3515 | // get the source member |
| 3516 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3517 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3518 | // set up the target storage |
| 3519 | builder.clearAccessChain(); |
| 3520 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3521 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), getBufferReferenceAlignment(type)); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3522 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3523 | // store the member |
| 3524 | multiTypeStore(glslangElementType, elementRValue); |
| 3525 | } |
| 3526 | } else { |
| 3527 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3528 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3529 | // loop over structure members |
| 3530 | const glslang::TTypeList& members = *type.getStruct(); |
| 3531 | for (int m = 0; m < (int)members.size(); ++m) { |
| 3532 | const glslang::TType& glslangMemberType = *members[m].type; |
| 3533 | |
| 3534 | // get the source member |
| 3535 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 3536 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 3537 | |
| 3538 | // set up the target storage |
| 3539 | builder.clearAccessChain(); |
| 3540 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3541 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), getBufferReferenceAlignment(type)); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3542 | |
| 3543 | // store the member |
| 3544 | multiTypeStore(glslangMemberType, memberRValue); |
| 3545 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3546 | } |
| 3547 | } |
| 3548 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3549 | // Decide whether or not this type should be |
| 3550 | // decorated with offsets and strides, and if so |
| 3551 | // whether std140 or std430 rules should be applied. |
| 3552 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3553 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3554 | // has to be a block |
| 3555 | if (type.getBasicType() != glslang::EbtBlock) |
| 3556 | return glslang::ElpNone; |
| 3557 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3558 | // 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] | 3559 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3560 | type.getQualifier().storage != glslang::EvqBuffer && |
| 3561 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3562 | return glslang::ElpNone; |
| 3563 | |
| 3564 | // return the layout to use |
| 3565 | switch (type.getQualifier().layoutPacking) { |
| 3566 | case glslang::ElpStd140: |
| 3567 | case glslang::ElpStd430: |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3568 | case glslang::ElpScalar: |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3569 | return type.getQualifier().layoutPacking; |
| 3570 | default: |
| 3571 | return glslang::ElpNone; |
| 3572 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3573 | } |
| 3574 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3575 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3576 | 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] | 3577 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3578 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3579 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3580 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 3581 | |
| 3582 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3585 | // 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] | 3586 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3587 | 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] | 3588 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3589 | glslang::TType elementType; |
| 3590 | elementType.shallowCopy(matrixType); |
| 3591 | elementType.clearArraySizes(); |
| 3592 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3593 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3594 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3595 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3596 | |
| 3597 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3598 | } |
| 3599 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3600 | // Given a member type of a struct, realign the current offset for it, and compute |
| 3601 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 3602 | // on the next call. |
| 3603 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 3604 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 3605 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3606 | 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] | 3607 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3608 | { |
| 3609 | // this will get a positive value when deemed necessary |
| 3610 | nextOffset = -1; |
| 3611 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3612 | // override anything in currentOffset with user-set offset |
| 3613 | if (memberType.getQualifier().hasOffset()) |
| 3614 | currentOffset = memberType.getQualifier().layoutOffset; |
| 3615 | |
| 3616 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 3617 | // in which case the following code does not matter. But, that's not quite right |
| 3618 | // once cross-compilation unit GLSL validation is done, as the original user |
| 3619 | // settings are needed in layoutOffset, and then the following will come into play. |
| 3620 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3621 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3622 | if (! memberType.getQualifier().hasOffset()) |
| 3623 | currentOffset = -1; |
| 3624 | |
| 3625 | return; |
| 3626 | } |
| 3627 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3628 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3629 | if (currentOffset < 0) |
| 3630 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3631 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3632 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 3633 | // but possibly not yet correctly aligned. |
| 3634 | |
| 3635 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3636 | int dummyStride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3637 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3638 | |
| 3639 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3640 | // TODO: make this consistent in early phases of code: |
| 3641 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 3642 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 3643 | // 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] | 3644 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3645 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3646 | int dummySize; |
| 3647 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 3648 | if (componentAlignment <= 4) |
| 3649 | memberAlignment = componentAlignment; |
| 3650 | } |
| 3651 | |
| 3652 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3653 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3654 | |
| 3655 | // Bump up to vec4 if there is a bad straddle |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3656 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3657 | glslang::RoundToPow2(currentOffset, 16); |
| 3658 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3659 | nextOffset = currentOffset + memberSize; |
| 3660 | } |
| 3661 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3662 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3663 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3664 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 3665 | switch (glslangBuiltIn) |
| 3666 | { |
| 3667 | case glslang::EbvClipDistance: |
| 3668 | case glslang::EbvCullDistance: |
| 3669 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3670 | #ifdef NV_EXTENSIONS |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3671 | case glslang::EbvViewportMaskNV: |
| 3672 | case glslang::EbvSecondaryPositionNV: |
| 3673 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3674 | case glslang::EbvPositionPerViewNV: |
| 3675 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3676 | case glslang::EbvTaskCountNV: |
| 3677 | case glslang::EbvPrimitiveCountNV: |
| 3678 | case glslang::EbvPrimitiveIndicesNV: |
| 3679 | case glslang::EbvClipDistancePerViewNV: |
| 3680 | case glslang::EbvCullDistancePerViewNV: |
| 3681 | case glslang::EbvLayerPerViewNV: |
| 3682 | case glslang::EbvMeshViewCountNV: |
| 3683 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3684 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3685 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 3686 | // Alternately, we could just call this for any glslang built-in, since the |
| 3687 | // capability already guards against duplicates. |
| 3688 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 3689 | break; |
| 3690 | default: |
| 3691 | // Capabilities were already generated when the struct was declared. |
| 3692 | break; |
| 3693 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3694 | } |
| 3695 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3696 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3697 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 3698 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3699 | } |
| 3700 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3701 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3702 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 3703 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 3704 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3705 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3706 | assert(qualifier == glslang::EvqIn || |
| 3707 | qualifier == glslang::EvqOut || |
| 3708 | qualifier == glslang::EvqInOut || |
| 3709 | qualifier == glslang::EvqConstReadOnly); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3710 | return qualifier != glslang::EvqConstReadOnly; |
| 3711 | } |
| 3712 | |
| 3713 | // Is parameter pass-by-original? |
| 3714 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 3715 | bool implicitThisParam) |
| 3716 | { |
| 3717 | if (implicitThisParam) // implicit this |
| 3718 | return true; |
| 3719 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3720 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3721 | return paramType.containsOpaque() || // sampler, etc. |
| 3722 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 3723 | } |
| 3724 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3725 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 3726 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3727 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3728 | 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] | 3729 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 3730 | if (paramPrecision != spv::NoPrecision) |
| 3731 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3732 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3733 | if (type.getBasicType() == glslang::EbtReference) { |
| 3734 | // Original and non-writable params pass the pointer directly and |
| 3735 | // use restrict/aliased, others are stored to a pointer in Function |
| 3736 | // memory and use RestrictPointer/AliasedPointer. |
| 3737 | if (originalParam(type.getQualifier().storage, type, false) || |
| 3738 | !writableParam(type.getQualifier().storage)) { |
| 3739 | decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrict : spv::DecorationAliased); |
| 3740 | } else { |
| 3741 | decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 3742 | } |
| 3743 | } |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3744 | }; |
| 3745 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3746 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3747 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3748 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3749 | continue; |
| 3750 | |
| 3751 | // 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] | 3752 | // 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] | 3753 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3754 | // 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] | 3755 | // function. What it is an address of varies: |
| 3756 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3757 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 3758 | // 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] | 3759 | // |
| 3760 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 3761 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3762 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 3763 | // 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] | 3764 | |
| 3765 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3766 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3767 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 3768 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3769 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 3770 | glslangIntermediate->implicitThisName; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3771 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3772 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3773 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3774 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 3775 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3776 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3777 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3778 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3779 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 3780 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3781 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3782 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3783 | paramTypes.push_back(typeId); |
| 3784 | } |
| 3785 | |
| 3786 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3787 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 3788 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3789 | glslFunction->getName().c_str(), paramTypes, |
| 3790 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3791 | if (implicitThis) |
| 3792 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3793 | |
| 3794 | // Track function to emit/call later |
| 3795 | functionMap[glslFunction->getName().c_str()] = function; |
| 3796 | |
| 3797 | // Set the parameter id's |
| 3798 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3799 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 3800 | // give a name too |
| 3801 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 3802 | } |
| 3803 | } |
| 3804 | } |
| 3805 | |
| 3806 | // Process all the initializers, while skipping the functions and link objects |
| 3807 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 3808 | { |
| 3809 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 3810 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 3811 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 3812 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 3813 | |
| 3814 | // 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] | 3815 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3816 | initializer->traverse(this); |
| 3817 | } |
| 3818 | } |
| 3819 | } |
| 3820 | |
| 3821 | // Process all the functions, while skipping initializers. |
| 3822 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3823 | { |
| 3824 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3825 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 3826 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3827 | node->traverse(this); |
| 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 3832 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3833 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3834 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3835 | currentFunction = functionMap[node->getName().c_str()]; |
| 3836 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3837 | builder.setBuildPoint(functionBlock); |
| 3838 | } |
| 3839 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3840 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3841 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3842 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3843 | |
| 3844 | glslang::TSampler sampler = {}; |
| 3845 | bool cubeCompare = false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3846 | #ifdef AMD_EXTENSIONS |
| 3847 | bool f16ShadowCompare = false; |
| 3848 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3849 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3850 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 3851 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3852 | #ifdef AMD_EXTENSIONS |
| 3853 | f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 3854 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3855 | } |
| 3856 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3857 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 3858 | builder.clearAccessChain(); |
| 3859 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3860 | |
| 3861 | // Special case l-value operands |
| 3862 | bool lvalue = false; |
| 3863 | switch (node.getOp()) { |
| 3864 | case glslang::EOpImageAtomicAdd: |
| 3865 | case glslang::EOpImageAtomicMin: |
| 3866 | case glslang::EOpImageAtomicMax: |
| 3867 | case glslang::EOpImageAtomicAnd: |
| 3868 | case glslang::EOpImageAtomicOr: |
| 3869 | case glslang::EOpImageAtomicXor: |
| 3870 | case glslang::EOpImageAtomicExchange: |
| 3871 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3872 | case glslang::EOpImageAtomicLoad: |
| 3873 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3874 | if (i == 0) |
| 3875 | lvalue = true; |
| 3876 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3877 | case glslang::EOpSparseImageLoad: |
| 3878 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 3879 | lvalue = true; |
| 3880 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3881 | #ifdef AMD_EXTENSIONS |
| 3882 | case glslang::EOpSparseTexture: |
| 3883 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 3884 | lvalue = true; |
| 3885 | break; |
| 3886 | case glslang::EOpSparseTextureClamp: |
| 3887 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 3888 | lvalue = true; |
| 3889 | break; |
| 3890 | case glslang::EOpSparseTextureLod: |
| 3891 | case glslang::EOpSparseTextureOffset: |
| 3892 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 3893 | lvalue = true; |
| 3894 | break; |
| 3895 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3896 | case glslang::EOpSparseTexture: |
| 3897 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 3898 | lvalue = true; |
| 3899 | break; |
| 3900 | case glslang::EOpSparseTextureClamp: |
| 3901 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 3902 | lvalue = true; |
| 3903 | break; |
| 3904 | case glslang::EOpSparseTextureLod: |
| 3905 | case glslang::EOpSparseTextureOffset: |
| 3906 | if (i == 3) |
| 3907 | lvalue = true; |
| 3908 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3909 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3910 | case glslang::EOpSparseTextureFetch: |
| 3911 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 3912 | lvalue = true; |
| 3913 | break; |
| 3914 | case glslang::EOpSparseTextureFetchOffset: |
| 3915 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 3916 | lvalue = true; |
| 3917 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3918 | #ifdef AMD_EXTENSIONS |
| 3919 | case glslang::EOpSparseTextureLodOffset: |
| 3920 | case glslang::EOpSparseTextureGrad: |
| 3921 | case glslang::EOpSparseTextureOffsetClamp: |
| 3922 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 3923 | lvalue = true; |
| 3924 | break; |
| 3925 | case glslang::EOpSparseTextureGradOffset: |
| 3926 | case glslang::EOpSparseTextureGradClamp: |
| 3927 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 3928 | lvalue = true; |
| 3929 | break; |
| 3930 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3931 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 3932 | lvalue = true; |
| 3933 | break; |
| 3934 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3935 | case glslang::EOpSparseTextureLodOffset: |
| 3936 | case glslang::EOpSparseTextureGrad: |
| 3937 | case glslang::EOpSparseTextureOffsetClamp: |
| 3938 | if (i == 4) |
| 3939 | lvalue = true; |
| 3940 | break; |
| 3941 | case glslang::EOpSparseTextureGradOffset: |
| 3942 | case glslang::EOpSparseTextureGradClamp: |
| 3943 | if (i == 5) |
| 3944 | lvalue = true; |
| 3945 | break; |
| 3946 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3947 | if (i == 6) |
| 3948 | lvalue = true; |
| 3949 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3950 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3951 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3952 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 3953 | lvalue = true; |
| 3954 | break; |
| 3955 | case glslang::EOpSparseTextureGatherOffset: |
| 3956 | case glslang::EOpSparseTextureGatherOffsets: |
| 3957 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 3958 | lvalue = true; |
| 3959 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3960 | #ifdef AMD_EXTENSIONS |
| 3961 | case glslang::EOpSparseTextureGatherLod: |
| 3962 | if (i == 3) |
| 3963 | lvalue = true; |
| 3964 | break; |
| 3965 | case glslang::EOpSparseTextureGatherLodOffset: |
| 3966 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 3967 | if (i == 4) |
| 3968 | lvalue = true; |
| 3969 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3970 | case glslang::EOpSparseImageLoadLod: |
| 3971 | if (i == 3) |
| 3972 | lvalue = true; |
| 3973 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3974 | #endif |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 3975 | #ifdef NV_EXTENSIONS |
| 3976 | case glslang::EOpImageSampleFootprintNV: |
| 3977 | if (i == 4) |
| 3978 | lvalue = true; |
| 3979 | break; |
| 3980 | case glslang::EOpImageSampleFootprintClampNV: |
| 3981 | case glslang::EOpImageSampleFootprintLodNV: |
| 3982 | if (i == 5) |
| 3983 | lvalue = true; |
| 3984 | break; |
| 3985 | case glslang::EOpImageSampleFootprintGradNV: |
| 3986 | if (i == 6) |
| 3987 | lvalue = true; |
| 3988 | break; |
| 3989 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 3990 | if (i == 7) |
| 3991 | lvalue = true; |
| 3992 | break; |
| 3993 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3994 | default: |
| 3995 | break; |
| 3996 | } |
| 3997 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3998 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3999 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 4000 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4001 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4002 | } |
| 4003 | } |
| 4004 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4005 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4006 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4007 | builder.clearAccessChain(); |
| 4008 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4009 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4010 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4011 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4012 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 4013 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4014 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4015 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4016 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 4017 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4018 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4019 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4020 | |
| 4021 | const glslang::TType &imageType = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 4022 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
| 4023 | const glslang::TSampler sampler = imageType.getSampler(); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4024 | #ifdef AMD_EXTENSIONS |
| 4025 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
| 4026 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 4027 | : false; |
| 4028 | #endif |
| 4029 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4030 | std::vector<spv::Id> arguments; |
| 4031 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4032 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4033 | else |
| 4034 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 4035 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4036 | |
| 4037 | spv::Builder::TextureParameters params = { }; |
| 4038 | params.sampler = arguments[0]; |
| 4039 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4040 | glslang::TCrackedTextureOp cracked; |
| 4041 | node->crackTexture(sampler, cracked); |
| 4042 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4043 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4044 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4045 | // Check for queries |
| 4046 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4047 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 4048 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 4049 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4050 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4051 | switch (node->getOp()) { |
| 4052 | case glslang::EOpImageQuerySize: |
| 4053 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4054 | if (arguments.size() > 1) { |
| 4055 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4056 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4057 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4058 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4059 | case glslang::EOpImageQuerySamples: |
| 4060 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4061 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4062 | case glslang::EOpTextureQueryLod: |
| 4063 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4064 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4065 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4066 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4067 | case glslang::EOpSparseTexelsResident: |
| 4068 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4069 | default: |
| 4070 | assert(0); |
| 4071 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4072 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4073 | } |
| 4074 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4075 | int components = node->getType().getVectorSize(); |
| 4076 | |
| 4077 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 4078 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 4079 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 4080 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 4081 | // here around e.g. which ones return scalars or other types. |
| 4082 | components = 4; |
| 4083 | } |
| 4084 | |
| 4085 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 4086 | |
| 4087 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 4088 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4089 | // Check for image functions other than queries |
| 4090 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4091 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4092 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4093 | spv::IdImmediate image = { true, *(opIt++) }; |
| 4094 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4095 | |
| 4096 | // Handle subpass operations |
| 4097 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 4098 | // built-in function. |
| 4099 | if (cracked.subpass) { |
| 4100 | // add on the (0,0) coordinate |
| 4101 | spv::Id zero = builder.makeIntConstant(0); |
| 4102 | std::vector<spv::Id> comps; |
| 4103 | comps.push_back(zero); |
| 4104 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4105 | spv::IdImmediate coord = { true, |
| 4106 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 4107 | operands.push_back(coord); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4108 | if (sampler.ms) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4109 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsSampleMask }; |
| 4110 | operands.push_back(imageOperands); |
| 4111 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 4112 | operands.push_back(imageOperand); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4113 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4114 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 4115 | builder.setPrecision(result, precision); |
| 4116 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4117 | } |
| 4118 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4119 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 4120 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4121 | #ifdef AMD_EXTENSIONS |
| 4122 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 4123 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4124 | if (node->getOp() == glslang::EOpImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4125 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4126 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4127 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4128 | mask = mask | spv::ImageOperandsSampleMask; |
| 4129 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4130 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4131 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4132 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4133 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4134 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4135 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4136 | #endif |
| 4137 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4138 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 4139 | if (mask) { |
| 4140 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4141 | operands.push_back(imageOperands); |
| 4142 | } |
| 4143 | if (mask & spv::ImageOperandsSampleMask) { |
| 4144 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4145 | operands.push_back(imageOperand); |
| 4146 | } |
| 4147 | #ifdef AMD_EXTENSIONS |
| 4148 | if (mask & spv::ImageOperandsLodMask) { |
| 4149 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4150 | operands.push_back(imageOperand); |
| 4151 | } |
| 4152 | #endif |
| 4153 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4154 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4155 | operands.push_back(imageOperand); |
| 4156 | } |
| 4157 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4158 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4159 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4160 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4161 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4162 | builder.setPrecision(result[0], precision); |
| 4163 | |
| 4164 | // If needed, add a conversion constructor to the proper size. |
| 4165 | if (components != node->getType().getVectorSize()) |
| 4166 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4167 | |
| 4168 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4169 | #ifdef AMD_EXTENSIONS |
| 4170 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 4171 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4172 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4173 | #endif |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4174 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4175 | // Push the texel value before the operands |
| 4176 | #ifdef AMD_EXTENSIONS |
| 4177 | if (sampler.ms || cracked.lod) { |
| 4178 | #else |
| 4179 | if (sampler.ms) { |
| 4180 | #endif |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4181 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4182 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4183 | } else { |
| 4184 | spv::IdImmediate texel = { true, *opIt }; |
| 4185 | operands.push_back(texel); |
| 4186 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4187 | |
| 4188 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 4189 | if (sampler.ms) { |
| 4190 | mask = mask | spv::ImageOperandsSampleMask; |
| 4191 | } |
| 4192 | #ifdef AMD_EXTENSIONS |
| 4193 | if (cracked.lod) { |
| 4194 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4195 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4196 | mask = mask | spv::ImageOperandsLodMask; |
| 4197 | } |
| 4198 | #endif |
| 4199 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4200 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
| 4201 | if (mask) { |
| 4202 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4203 | operands.push_back(imageOperands); |
| 4204 | } |
| 4205 | if (mask & spv::ImageOperandsSampleMask) { |
| 4206 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4207 | operands.push_back(imageOperand); |
| 4208 | } |
| 4209 | #ifdef AMD_EXTENSIONS |
| 4210 | if (mask & spv::ImageOperandsLodMask) { |
| 4211 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4212 | operands.push_back(imageOperand); |
| 4213 | } |
| 4214 | #endif |
| 4215 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
| 4216 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4217 | operands.push_back(imageOperand); |
| 4218 | } |
| 4219 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4220 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4221 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4222 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4223 | return spv::NoResult; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4224 | #ifdef AMD_EXTENSIONS |
| 4225 | } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 4226 | #else |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4227 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4228 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4229 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4230 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4231 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4232 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4233 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4234 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4235 | mask = mask | spv::ImageOperandsSampleMask; |
| 4236 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4237 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4238 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4239 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4240 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4241 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4242 | mask = mask | spv::ImageOperandsLodMask; |
| 4243 | } |
| 4244 | #endif |
| 4245 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4246 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 4247 | if (mask) { |
| 4248 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4249 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4250 | } |
| 4251 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4252 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4253 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4254 | } |
| 4255 | #ifdef AMD_EXTENSIONS |
| 4256 | if (mask & spv::ImageOperandsLodMask) { |
| 4257 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4258 | operands.push_back(imageOperand); |
| 4259 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4260 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4261 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4262 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4263 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | // Create the return type that was a special structure |
| 4267 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4268 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4269 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 4270 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 4271 | |
| 4272 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 4273 | |
| 4274 | // Decode the return type |
| 4275 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 4276 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 4277 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 4278 | // Process image atomic operations |
| 4279 | |
| 4280 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 4281 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4282 | // For non-MS, the sample value should be 0 |
| 4283 | spv::IdImmediate sample = { true, sampler.ms ? *(opIt++) : builder.makeUintConstant(0) }; |
| 4284 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4285 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4286 | spv::Id resultTypeId; |
| 4287 | // imageAtomicStore has a void return type so base the pointer type on |
| 4288 | // the type of the value operand. |
| 4289 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 4290 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word)); |
| 4291 | } else { |
| 4292 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 4293 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4294 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4295 | |
| 4296 | std::vector<spv::Id> operands; |
| 4297 | operands.push_back(pointer); |
| 4298 | for (; opIt != arguments.end(); ++opIt) |
| 4299 | operands.push_back(*opIt); |
| 4300 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4301 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4302 | } |
| 4303 | } |
| 4304 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4305 | #ifdef AMD_EXTENSIONS |
| 4306 | // Check for fragment mask functions other than queries |
| 4307 | if (cracked.fragMask) { |
| 4308 | assert(sampler.ms); |
| 4309 | |
| 4310 | auto opIt = arguments.begin(); |
| 4311 | std::vector<spv::Id> operands; |
| 4312 | |
| 4313 | // Extract the image if necessary |
| 4314 | if (builder.isSampledImage(params.sampler)) |
| 4315 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4316 | |
| 4317 | operands.push_back(params.sampler); |
| 4318 | ++opIt; |
| 4319 | |
| 4320 | if (sampler.isSubpass()) { |
| 4321 | // add on the (0,0) coordinate |
| 4322 | spv::Id zero = builder.makeIntConstant(0); |
| 4323 | std::vector<spv::Id> comps; |
| 4324 | comps.push_back(zero); |
| 4325 | comps.push_back(zero); |
| 4326 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 4327 | } |
| 4328 | |
| 4329 | for (; opIt != arguments.end(); ++opIt) |
| 4330 | operands.push_back(*opIt); |
| 4331 | |
| 4332 | spv::Op fragMaskOp = spv::OpNop; |
| 4333 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 4334 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 4335 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 4336 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 4337 | |
| 4338 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 4339 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 4340 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 4341 | } |
| 4342 | #endif |
| 4343 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4344 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4345 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4346 | #ifdef NV_EXTENSIONS |
| 4347 | bool imageFootprint = node->isImageFootprint(); |
| 4348 | #endif |
| 4349 | |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4350 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 4351 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4352 | // check for bias argument |
| 4353 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4354 | #ifdef AMD_EXTENSIONS |
| 4355 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 4356 | #else |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4357 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4358 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4359 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4360 | #ifdef AMD_EXTENSIONS |
| 4361 | if (cracked.gather) |
| 4362 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4363 | |
| 4364 | if (f16ShadowCompare) |
| 4365 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4366 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4367 | if (cracked.offset) |
| 4368 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4369 | #ifdef AMD_EXTENSIONS |
| 4370 | else if (cracked.offsets) |
| 4371 | ++nonBiasArgCount; |
| 4372 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4373 | if (cracked.grad) |
| 4374 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4375 | if (cracked.lodClamp) |
| 4376 | ++nonBiasArgCount; |
| 4377 | if (sparse) |
| 4378 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4379 | #ifdef NV_EXTENSIONS |
| 4380 | if (imageFootprint) |
| 4381 | //Following three extra arguments |
| 4382 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4383 | nonBiasArgCount += 3; |
| 4384 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4385 | if ((int)arguments.size() > nonBiasArgCount) |
| 4386 | bias = true; |
| 4387 | } |
| 4388 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 4389 | // See if the sampler param should really be just the SPV image part |
| 4390 | if (cracked.fetch) { |
| 4391 | // a fetch needs to have the image extracted first |
| 4392 | if (builder.isSampledImage(params.sampler)) |
| 4393 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4394 | } |
| 4395 | |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4396 | #ifdef AMD_EXTENSIONS |
| 4397 | if (cracked.gather) { |
| 4398 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 4399 | if (bias || cracked.lod || |
| 4400 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 4401 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 4402 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | #endif |
| 4406 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4407 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4408 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4409 | params.coords = arguments[1]; |
| 4410 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4411 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4412 | |
| 4413 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4414 | #ifdef AMD_EXTENSIONS |
| 4415 | if (cubeCompare || f16ShadowCompare) { |
| 4416 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4417 | if (cubeCompare) { |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4418 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4419 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4420 | ++extraArgs; |
| 4421 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4422 | params.Dref = arguments[2]; |
| 4423 | ++extraArgs; |
| 4424 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4425 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4426 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4427 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4428 | 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] | 4429 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4430 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 4431 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4432 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 4433 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4434 | |
| 4435 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4436 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4437 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4438 | ++extraArgs; |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 4439 | } else if (glslangIntermediate->getStage() != EShLangFragment |
| 4440 | #ifdef NV_EXTENSIONS |
| 4441 | // NV_compute_shader_derivatives layout qualifiers allow for implicit LODs |
| 4442 | && !(glslangIntermediate->getStage() == EShLangCompute && |
| 4443 | (glslangIntermediate->getLayoutDerivativeModeNone() != glslang::LayoutDerivativeNone)) |
| 4444 | #endif |
| 4445 | ) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4446 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 4447 | noImplicitLod = true; |
| 4448 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4449 | |
| 4450 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4451 | if (sampler.ms) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4452 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4453 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4454 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4455 | |
| 4456 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4457 | if (cracked.grad) { |
| 4458 | params.gradX = arguments[2 + extraArgs]; |
| 4459 | params.gradY = arguments[3 + extraArgs]; |
| 4460 | extraArgs += 2; |
| 4461 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4462 | |
| 4463 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4464 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4465 | params.offset = arguments[2 + extraArgs]; |
| 4466 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4467 | } else if (cracked.offsets) { |
| 4468 | params.offsets = arguments[2 + extraArgs]; |
| 4469 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4470 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4471 | |
| 4472 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4473 | if (cracked.lodClamp) { |
| 4474 | params.lodClamp = arguments[2 + extraArgs]; |
| 4475 | ++extraArgs; |
| 4476 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4477 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4478 | if (sparse) { |
| 4479 | params.texelOut = arguments[2 + extraArgs]; |
| 4480 | ++extraArgs; |
| 4481 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4482 | |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4483 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4484 | if (cracked.gather && ! sampler.shadow) { |
| 4485 | // default component is 0, if missing, otherwise an argument |
| 4486 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4487 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4488 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4489 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4490 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4491 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4492 | #ifdef NV_EXTENSIONS |
| 4493 | spv::Id resultStruct = spv::NoResult; |
| 4494 | if (imageFootprint) { |
| 4495 | //Following three extra arguments |
| 4496 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4497 | params.granularity = arguments[2 + extraArgs]; |
| 4498 | params.coarse = arguments[3 + extraArgs]; |
| 4499 | resultStruct = arguments[4 + extraArgs]; |
| 4500 | extraArgs += 3; |
| 4501 | } |
| 4502 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4503 | // bias |
| 4504 | if (bias) { |
| 4505 | params.bias = arguments[2 + extraArgs]; |
| 4506 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4507 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4508 | |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4509 | #ifdef NV_EXTENSIONS |
| 4510 | if (imageFootprint) { |
| 4511 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 4512 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 4513 | |
| 4514 | |
| 4515 | //resultStructType(OpenGL type) contains 5 elements: |
| 4516 | //struct gl_TextureFootprint2DNV { |
| 4517 | // uvec2 anchor; |
| 4518 | // uvec2 offset; |
| 4519 | // uvec2 mask; |
| 4520 | // uint lod; |
| 4521 | // uint granularity; |
| 4522 | //}; |
| 4523 | //or |
| 4524 | //struct gl_TextureFootprint3DNV { |
| 4525 | // uvec3 anchor; |
| 4526 | // uvec3 offset; |
| 4527 | // uvec2 mask; |
| 4528 | // uint lod; |
| 4529 | // uint granularity; |
| 4530 | //}; |
| 4531 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 4532 | assert(builder.isStructType(resultStructType)); |
| 4533 | |
| 4534 | //resType (SPIR-V type) contains 6 elements: |
| 4535 | //Member 0 must be a Boolean type scalar(LOD), |
| 4536 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 4537 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 4538 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 4539 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 4540 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 4541 | std::vector<spv::Id> members; |
| 4542 | members.push_back(resultType()); |
| 4543 | for (int i = 0; i < 5; i++) { |
| 4544 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 4545 | } |
| 4546 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 4547 | |
| 4548 | //call ImageFootprintNV |
| 4549 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params); |
| 4550 | |
| 4551 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 4552 | for (int i = 0; i < 5; i++) { |
| 4553 | builder.clearAccessChain(); |
| 4554 | builder.setAccessChainLValue(resultStruct); |
| 4555 | |
| 4556 | //Accessing to a struct we created, no coherent flag is set |
| 4557 | spv::Builder::AccessChain::CoherentFlags flags; |
| 4558 | flags.clear(); |
| 4559 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4560 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4561 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1)); |
| 4562 | } |
| 4563 | return builder.createCompositeExtract(res, resultType(), 0); |
| 4564 | } |
| 4565 | #endif |
| 4566 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4567 | // projective component (might not to move) |
| 4568 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 4569 | // are divided by the last component of P." |
| 4570 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 4571 | // unused components will appear after all used components." |
| 4572 | if (cracked.proj) { |
| 4573 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 4574 | int projTargetComp; |
| 4575 | switch (sampler.dim) { |
| 4576 | case glslang::Esd1D: projTargetComp = 1; break; |
| 4577 | case glslang::Esd2D: projTargetComp = 2; break; |
| 4578 | case glslang::EsdRect: projTargetComp = 2; break; |
| 4579 | default: projTargetComp = projSourceComp; break; |
| 4580 | } |
| 4581 | // copy the projective coordinate if we have to |
| 4582 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 4583 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4584 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 4585 | projSourceComp); |
| 4586 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 4587 | builder.getTypeId(params.coords), projTargetComp); |
| 4588 | } |
| 4589 | } |
| 4590 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4591 | // nonprivate |
| 4592 | if (imageType.getQualifier().nonprivate) { |
| 4593 | params.nonprivate = true; |
| 4594 | } |
| 4595 | |
| 4596 | // volatile |
| 4597 | if (imageType.getQualifier().volatil) { |
| 4598 | params.volatil = true; |
| 4599 | } |
| 4600 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4601 | std::vector<spv::Id> result( 1, |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4602 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4603 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4604 | |
| 4605 | if (components != node->getType().getVectorSize()) |
| 4606 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4607 | |
| 4608 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 4612 | { |
| 4613 | // Grab the function's pointer from the previously created function |
| 4614 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 4615 | if (! function) |
| 4616 | return 0; |
| 4617 | |
| 4618 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 4619 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 4620 | |
| 4621 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 4622 | // |
| 4623 | // These imply we need a four step process: |
| 4624 | // 1. Evaluate the arguments |
| 4625 | // 2. Allocate and make copies of in, out, and inout arguments |
| 4626 | // 3. Make the call |
| 4627 | // 4. Copy back the results |
| 4628 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4629 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4630 | std::vector<spv::Builder::AccessChain> lValues; |
| 4631 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4632 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4633 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4634 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4635 | // build l-value |
| 4636 | builder.clearAccessChain(); |
| 4637 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4638 | // 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] | 4639 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4640 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4641 | // save l-value |
| 4642 | lValues.push_back(builder.getAccessChain()); |
| 4643 | } else { |
| 4644 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4645 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4646 | } |
| 4647 | } |
| 4648 | |
| 4649 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 4650 | // copy the original into that space. |
| 4651 | // |
| 4652 | // Also, build up the list of actual arguments to pass in for the call |
| 4653 | int lValueCount = 0; |
| 4654 | int rValueCount = 0; |
| 4655 | std::vector<spv::Id> spvArgs; |
| 4656 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 4657 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4658 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 4659 | builder.setAccessChain(lValues[lValueCount]); |
| 4660 | arg = builder.accessChainGetLValue(); |
| 4661 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4662 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4663 | // need space to hold the copy |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4664 | arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4665 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 4666 | // need to copy the input into output space |
| 4667 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4668 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4669 | builder.clearAccessChain(); |
| 4670 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4671 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4672 | } |
| 4673 | ++lValueCount; |
| 4674 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4675 | // process r-value, which involves a copy for a type mismatch |
| 4676 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) { |
| 4677 | spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg"); |
| 4678 | builder.clearAccessChain(); |
| 4679 | builder.setAccessChainLValue(argCopy); |
| 4680 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 4681 | arg = builder.createLoad(argCopy); |
| 4682 | } else |
| 4683 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4684 | ++rValueCount; |
| 4685 | } |
| 4686 | spvArgs.push_back(arg); |
| 4687 | } |
| 4688 | |
| 4689 | // 3. Make the call. |
| 4690 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4691 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4692 | |
| 4693 | // 4. Copy back out an "out" arguments. |
| 4694 | lValueCount = 0; |
| 4695 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4696 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4697 | ++lValueCount; |
| 4698 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4699 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 4700 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 4701 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4702 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4703 | } |
| 4704 | ++lValueCount; |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | return result; |
| 4709 | } |
| 4710 | |
| 4711 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4712 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4713 | spv::Id typeId, spv::Id left, spv::Id right, |
| 4714 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 4715 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4716 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 4717 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4718 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4719 | |
| 4720 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4721 | 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] | 4722 | bool comparison = false; |
| 4723 | |
| 4724 | switch (op) { |
| 4725 | case glslang::EOpAdd: |
| 4726 | case glslang::EOpAddAssign: |
| 4727 | if (isFloat) |
| 4728 | binOp = spv::OpFAdd; |
| 4729 | else |
| 4730 | binOp = spv::OpIAdd; |
| 4731 | break; |
| 4732 | case glslang::EOpSub: |
| 4733 | case glslang::EOpSubAssign: |
| 4734 | if (isFloat) |
| 4735 | binOp = spv::OpFSub; |
| 4736 | else |
| 4737 | binOp = spv::OpISub; |
| 4738 | break; |
| 4739 | case glslang::EOpMul: |
| 4740 | case glslang::EOpMulAssign: |
| 4741 | if (isFloat) |
| 4742 | binOp = spv::OpFMul; |
| 4743 | else |
| 4744 | binOp = spv::OpIMul; |
| 4745 | break; |
| 4746 | case glslang::EOpVectorTimesScalar: |
| 4747 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 4748 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4749 | if (builder.isVector(right)) |
| 4750 | std::swap(left, right); |
| 4751 | assert(builder.isScalar(right)); |
| 4752 | needMatchingVectors = false; |
| 4753 | binOp = spv::OpVectorTimesScalar; |
t.jung | 697fdf0 | 2018-11-14 13:04:39 +0100 | [diff] [blame] | 4754 | } else if (isFloat) |
| 4755 | binOp = spv::OpFMul; |
| 4756 | else |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4757 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4758 | break; |
| 4759 | case glslang::EOpVectorTimesMatrix: |
| 4760 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4761 | binOp = spv::OpVectorTimesMatrix; |
| 4762 | break; |
| 4763 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4764 | binOp = spv::OpMatrixTimesVector; |
| 4765 | break; |
| 4766 | case glslang::EOpMatrixTimesScalar: |
| 4767 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4768 | binOp = spv::OpMatrixTimesScalar; |
| 4769 | break; |
| 4770 | case glslang::EOpMatrixTimesMatrix: |
| 4771 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4772 | binOp = spv::OpMatrixTimesMatrix; |
| 4773 | break; |
| 4774 | case glslang::EOpOuterProduct: |
| 4775 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4776 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4777 | break; |
| 4778 | |
| 4779 | case glslang::EOpDiv: |
| 4780 | case glslang::EOpDivAssign: |
| 4781 | if (isFloat) |
| 4782 | binOp = spv::OpFDiv; |
| 4783 | else if (isUnsigned) |
| 4784 | binOp = spv::OpUDiv; |
| 4785 | else |
| 4786 | binOp = spv::OpSDiv; |
| 4787 | break; |
| 4788 | case glslang::EOpMod: |
| 4789 | case glslang::EOpModAssign: |
| 4790 | if (isFloat) |
| 4791 | binOp = spv::OpFMod; |
| 4792 | else if (isUnsigned) |
| 4793 | binOp = spv::OpUMod; |
| 4794 | else |
| 4795 | binOp = spv::OpSMod; |
| 4796 | break; |
| 4797 | case glslang::EOpRightShift: |
| 4798 | case glslang::EOpRightShiftAssign: |
| 4799 | if (isUnsigned) |
| 4800 | binOp = spv::OpShiftRightLogical; |
| 4801 | else |
| 4802 | binOp = spv::OpShiftRightArithmetic; |
| 4803 | break; |
| 4804 | case glslang::EOpLeftShift: |
| 4805 | case glslang::EOpLeftShiftAssign: |
| 4806 | binOp = spv::OpShiftLeftLogical; |
| 4807 | break; |
| 4808 | case glslang::EOpAnd: |
| 4809 | case glslang::EOpAndAssign: |
| 4810 | binOp = spv::OpBitwiseAnd; |
| 4811 | break; |
| 4812 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4813 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4814 | binOp = spv::OpLogicalAnd; |
| 4815 | break; |
| 4816 | case glslang::EOpInclusiveOr: |
| 4817 | case glslang::EOpInclusiveOrAssign: |
| 4818 | binOp = spv::OpBitwiseOr; |
| 4819 | break; |
| 4820 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4821 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4822 | binOp = spv::OpLogicalOr; |
| 4823 | break; |
| 4824 | case glslang::EOpExclusiveOr: |
| 4825 | case glslang::EOpExclusiveOrAssign: |
| 4826 | binOp = spv::OpBitwiseXor; |
| 4827 | break; |
| 4828 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4829 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4830 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4831 | break; |
| 4832 | |
| 4833 | case glslang::EOpLessThan: |
| 4834 | case glslang::EOpGreaterThan: |
| 4835 | case glslang::EOpLessThanEqual: |
| 4836 | case glslang::EOpGreaterThanEqual: |
| 4837 | case glslang::EOpEqual: |
| 4838 | case glslang::EOpNotEqual: |
| 4839 | case glslang::EOpVectorEqual: |
| 4840 | case glslang::EOpVectorNotEqual: |
| 4841 | comparison = true; |
| 4842 | break; |
| 4843 | default: |
| 4844 | break; |
| 4845 | } |
| 4846 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4847 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4848 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4849 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4850 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4851 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4852 | |
| 4853 | // 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] | 4854 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4855 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4856 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4857 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4858 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4859 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4860 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | if (! comparison) |
| 4864 | return 0; |
| 4865 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4866 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4867 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 4868 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4869 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 4870 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4871 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4872 | return result; |
| 4873 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4874 | |
| 4875 | switch (op) { |
| 4876 | case glslang::EOpLessThan: |
| 4877 | if (isFloat) |
| 4878 | binOp = spv::OpFOrdLessThan; |
| 4879 | else if (isUnsigned) |
| 4880 | binOp = spv::OpULessThan; |
| 4881 | else |
| 4882 | binOp = spv::OpSLessThan; |
| 4883 | break; |
| 4884 | case glslang::EOpGreaterThan: |
| 4885 | if (isFloat) |
| 4886 | binOp = spv::OpFOrdGreaterThan; |
| 4887 | else if (isUnsigned) |
| 4888 | binOp = spv::OpUGreaterThan; |
| 4889 | else |
| 4890 | binOp = spv::OpSGreaterThan; |
| 4891 | break; |
| 4892 | case glslang::EOpLessThanEqual: |
| 4893 | if (isFloat) |
| 4894 | binOp = spv::OpFOrdLessThanEqual; |
| 4895 | else if (isUnsigned) |
| 4896 | binOp = spv::OpULessThanEqual; |
| 4897 | else |
| 4898 | binOp = spv::OpSLessThanEqual; |
| 4899 | break; |
| 4900 | case glslang::EOpGreaterThanEqual: |
| 4901 | if (isFloat) |
| 4902 | binOp = spv::OpFOrdGreaterThanEqual; |
| 4903 | else if (isUnsigned) |
| 4904 | binOp = spv::OpUGreaterThanEqual; |
| 4905 | else |
| 4906 | binOp = spv::OpSGreaterThanEqual; |
| 4907 | break; |
| 4908 | case glslang::EOpEqual: |
| 4909 | case glslang::EOpVectorEqual: |
| 4910 | if (isFloat) |
| 4911 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4912 | else if (isBool) |
| 4913 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4914 | else |
| 4915 | binOp = spv::OpIEqual; |
| 4916 | break; |
| 4917 | case glslang::EOpNotEqual: |
| 4918 | case glslang::EOpVectorNotEqual: |
| 4919 | if (isFloat) |
| 4920 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4921 | else if (isBool) |
| 4922 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4923 | else |
| 4924 | binOp = spv::OpINotEqual; |
| 4925 | break; |
| 4926 | default: |
| 4927 | break; |
| 4928 | } |
| 4929 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4930 | if (binOp != spv::OpNop) { |
| 4931 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4932 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4933 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4934 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4935 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4936 | |
| 4937 | return 0; |
| 4938 | } |
| 4939 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4940 | // |
| 4941 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 4942 | // These can be any of: |
| 4943 | // |
| 4944 | // matrix * scalar |
| 4945 | // scalar * matrix |
| 4946 | // matrix * matrix linear algebraic |
| 4947 | // matrix * vector |
| 4948 | // vector * matrix |
| 4949 | // matrix * matrix componentwise |
| 4950 | // matrix op matrix op in {+, -, /} |
| 4951 | // matrix op scalar op in {+, -, /} |
| 4952 | // scalar op matrix op in {+, -, /} |
| 4953 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4954 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 4955 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4956 | { |
| 4957 | bool firstClass = true; |
| 4958 | |
| 4959 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 4960 | switch (op) { |
| 4961 | case spv::OpFDiv: |
| 4962 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 4963 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 4964 | spv::Id resultType = builder.getTypeId(right); |
| 4965 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4966 | op = spv::OpMatrixTimesScalar; |
| 4967 | } else |
| 4968 | firstClass = false; |
| 4969 | break; |
| 4970 | case spv::OpMatrixTimesScalar: |
| 4971 | if (builder.isMatrix(right)) |
| 4972 | std::swap(left, right); |
| 4973 | assert(builder.isScalar(right)); |
| 4974 | break; |
| 4975 | case spv::OpVectorTimesMatrix: |
| 4976 | assert(builder.isVector(left)); |
| 4977 | assert(builder.isMatrix(right)); |
| 4978 | break; |
| 4979 | case spv::OpMatrixTimesVector: |
| 4980 | assert(builder.isMatrix(left)); |
| 4981 | assert(builder.isVector(right)); |
| 4982 | break; |
| 4983 | case spv::OpMatrixTimesMatrix: |
| 4984 | assert(builder.isMatrix(left)); |
| 4985 | assert(builder.isMatrix(right)); |
| 4986 | break; |
| 4987 | default: |
| 4988 | firstClass = false; |
| 4989 | break; |
| 4990 | } |
| 4991 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4992 | if (firstClass) { |
| 4993 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4994 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4995 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4996 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4997 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4998 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4999 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5000 | // The result type of all of them is the same type as the (a) matrix operand. |
| 5001 | // The algorithm is to: |
| 5002 | // - break the matrix(es) into vectors |
| 5003 | // - smear any scalar to a vector |
| 5004 | // - do vector operations |
| 5005 | // - make a matrix out the vector results |
| 5006 | switch (op) { |
| 5007 | case spv::OpFAdd: |
| 5008 | case spv::OpFSub: |
| 5009 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5010 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5011 | case spv::OpFMul: |
| 5012 | { |
| 5013 | // one time set up... |
| 5014 | bool leftMat = builder.isMatrix(left); |
| 5015 | bool rightMat = builder.isMatrix(right); |
| 5016 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 5017 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 5018 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 5019 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 5020 | std::vector<spv::Id> results; |
| 5021 | spv::Id smearVec = spv::NoResult; |
| 5022 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5023 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5024 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5025 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5026 | |
| 5027 | // do each vector op |
| 5028 | for (unsigned int c = 0; c < numCols; ++c) { |
| 5029 | std::vector<unsigned int> indexes; |
| 5030 | indexes.push_back(c); |
| 5031 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 5032 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5033 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5034 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5035 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5036 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5040 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5041 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5042 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5043 | } |
| 5044 | default: |
| 5045 | assert(0); |
| 5046 | return spv::NoResult; |
| 5047 | } |
| 5048 | } |
| 5049 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5050 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
| 5051 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5052 | { |
| 5053 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5054 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5055 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5056 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5057 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5058 | |
| 5059 | switch (op) { |
| 5060 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5061 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5062 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5063 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5064 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5065 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5066 | unaryOp = spv::OpSNegate; |
| 5067 | break; |
| 5068 | |
| 5069 | case glslang::EOpLogicalNot: |
| 5070 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5071 | unaryOp = spv::OpLogicalNot; |
| 5072 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5073 | case glslang::EOpBitwiseNot: |
| 5074 | unaryOp = spv::OpNot; |
| 5075 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5076 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5077 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5078 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5079 | break; |
| 5080 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5081 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5082 | break; |
| 5083 | case glslang::EOpTranspose: |
| 5084 | unaryOp = spv::OpTranspose; |
| 5085 | break; |
| 5086 | |
| 5087 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5088 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5089 | break; |
| 5090 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5091 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5092 | break; |
| 5093 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5094 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5095 | break; |
| 5096 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5097 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5098 | break; |
| 5099 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5100 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5101 | break; |
| 5102 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5103 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5104 | break; |
| 5105 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5106 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5107 | break; |
| 5108 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5109 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5110 | break; |
| 5111 | |
| 5112 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5113 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5114 | break; |
| 5115 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5116 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5117 | break; |
| 5118 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5119 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5120 | break; |
| 5121 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5122 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5123 | break; |
| 5124 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5125 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5126 | break; |
| 5127 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5128 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5129 | break; |
| 5130 | |
| 5131 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5132 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5133 | break; |
| 5134 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5135 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5136 | break; |
| 5137 | |
| 5138 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5139 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5140 | break; |
| 5141 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5142 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5143 | break; |
| 5144 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5145 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5146 | break; |
| 5147 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5148 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5149 | break; |
| 5150 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5151 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5152 | break; |
| 5153 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5154 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5155 | break; |
| 5156 | |
| 5157 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5158 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5159 | break; |
| 5160 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5161 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5162 | break; |
| 5163 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5164 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5165 | break; |
| 5166 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5167 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5168 | break; |
| 5169 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5170 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5171 | break; |
| 5172 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5173 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5174 | break; |
| 5175 | |
| 5176 | case glslang::EOpIsNan: |
| 5177 | unaryOp = spv::OpIsNan; |
| 5178 | break; |
| 5179 | case glslang::EOpIsInf: |
| 5180 | unaryOp = spv::OpIsInf; |
| 5181 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5182 | case glslang::EOpIsFinite: |
| 5183 | unaryOp = spv::OpIsFinite; |
| 5184 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5185 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5186 | case glslang::EOpFloatBitsToInt: |
| 5187 | case glslang::EOpFloatBitsToUint: |
| 5188 | case glslang::EOpIntBitsToFloat: |
| 5189 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5190 | case glslang::EOpDoubleBitsToInt64: |
| 5191 | case glslang::EOpDoubleBitsToUint64: |
| 5192 | case glslang::EOpInt64BitsToDouble: |
| 5193 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5194 | case glslang::EOpFloat16BitsToInt16: |
| 5195 | case glslang::EOpFloat16BitsToUint16: |
| 5196 | case glslang::EOpInt16BitsToFloat16: |
| 5197 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5198 | unaryOp = spv::OpBitcast; |
| 5199 | break; |
| 5200 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5201 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5202 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5203 | break; |
| 5204 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5205 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5206 | break; |
| 5207 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5208 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5209 | break; |
| 5210 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5211 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5212 | break; |
| 5213 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5214 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5215 | break; |
| 5216 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5217 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5218 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5219 | case glslang::EOpPackSnorm4x8: |
| 5220 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 5221 | break; |
| 5222 | case glslang::EOpUnpackSnorm4x8: |
| 5223 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5224 | break; |
| 5225 | case glslang::EOpPackUnorm4x8: |
| 5226 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 5227 | break; |
| 5228 | case glslang::EOpUnpackUnorm4x8: |
| 5229 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5230 | break; |
| 5231 | case glslang::EOpPackDouble2x32: |
| 5232 | libCall = spv::GLSLstd450PackDouble2x32; |
| 5233 | break; |
| 5234 | case glslang::EOpUnpackDouble2x32: |
| 5235 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5236 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5237 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5238 | case glslang::EOpPackInt2x32: |
| 5239 | case glslang::EOpUnpackInt2x32: |
| 5240 | case glslang::EOpPackUint2x32: |
| 5241 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5242 | case glslang::EOpPack16: |
| 5243 | case glslang::EOpPack32: |
| 5244 | case glslang::EOpPack64: |
| 5245 | case glslang::EOpUnpack32: |
| 5246 | case glslang::EOpUnpack16: |
| 5247 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5248 | case glslang::EOpPackInt2x16: |
| 5249 | case glslang::EOpUnpackInt2x16: |
| 5250 | case glslang::EOpPackUint2x16: |
| 5251 | case glslang::EOpUnpackUint2x16: |
| 5252 | case glslang::EOpPackInt4x16: |
| 5253 | case glslang::EOpUnpackInt4x16: |
| 5254 | case glslang::EOpPackUint4x16: |
| 5255 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5256 | case glslang::EOpPackFloat2x16: |
| 5257 | case glslang::EOpUnpackFloat2x16: |
| 5258 | unaryOp = spv::OpBitcast; |
| 5259 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5260 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5261 | case glslang::EOpDPdx: |
| 5262 | unaryOp = spv::OpDPdx; |
| 5263 | break; |
| 5264 | case glslang::EOpDPdy: |
| 5265 | unaryOp = spv::OpDPdy; |
| 5266 | break; |
| 5267 | case glslang::EOpFwidth: |
| 5268 | unaryOp = spv::OpFwidth; |
| 5269 | break; |
| 5270 | case glslang::EOpDPdxFine: |
| 5271 | unaryOp = spv::OpDPdxFine; |
| 5272 | break; |
| 5273 | case glslang::EOpDPdyFine: |
| 5274 | unaryOp = spv::OpDPdyFine; |
| 5275 | break; |
| 5276 | case glslang::EOpFwidthFine: |
| 5277 | unaryOp = spv::OpFwidthFine; |
| 5278 | break; |
| 5279 | case glslang::EOpDPdxCoarse: |
| 5280 | unaryOp = spv::OpDPdxCoarse; |
| 5281 | break; |
| 5282 | case glslang::EOpDPdyCoarse: |
| 5283 | unaryOp = spv::OpDPdyCoarse; |
| 5284 | break; |
| 5285 | case glslang::EOpFwidthCoarse: |
| 5286 | unaryOp = spv::OpFwidthCoarse; |
| 5287 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5288 | case glslang::EOpInterpolateAtCentroid: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 5289 | #ifdef AMD_EXTENSIONS |
| 5290 | if (typeProxy == glslang::EbtFloat16) |
| 5291 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 5292 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5293 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 5294 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5295 | case glslang::EOpAny: |
| 5296 | unaryOp = spv::OpAny; |
| 5297 | break; |
| 5298 | case glslang::EOpAll: |
| 5299 | unaryOp = spv::OpAll; |
| 5300 | break; |
| 5301 | |
| 5302 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5303 | if (isFloat) |
| 5304 | libCall = spv::GLSLstd450FAbs; |
| 5305 | else |
| 5306 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5307 | break; |
| 5308 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5309 | if (isFloat) |
| 5310 | libCall = spv::GLSLstd450FSign; |
| 5311 | else |
| 5312 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5313 | break; |
| 5314 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5315 | case glslang::EOpAtomicCounterIncrement: |
| 5316 | case glslang::EOpAtomicCounterDecrement: |
| 5317 | case glslang::EOpAtomicCounter: |
| 5318 | { |
| 5319 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 5320 | std::vector<spv::Id> operands; |
| 5321 | operands.push_back(operand); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5322 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5323 | } |
| 5324 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5325 | case glslang::EOpBitFieldReverse: |
| 5326 | unaryOp = spv::OpBitReverse; |
| 5327 | break; |
| 5328 | case glslang::EOpBitCount: |
| 5329 | unaryOp = spv::OpBitCount; |
| 5330 | break; |
| 5331 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5332 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5333 | break; |
| 5334 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5335 | if (isUnsigned) |
| 5336 | libCall = spv::GLSLstd450FindUMsb; |
| 5337 | else |
| 5338 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5339 | break; |
| 5340 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5341 | case glslang::EOpBallot: |
| 5342 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5343 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5344 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5345 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5346 | #ifdef AMD_EXTENSIONS |
| 5347 | case glslang::EOpMinInvocations: |
| 5348 | case glslang::EOpMaxInvocations: |
| 5349 | case glslang::EOpAddInvocations: |
| 5350 | case glslang::EOpMinInvocationsNonUniform: |
| 5351 | case glslang::EOpMaxInvocationsNonUniform: |
| 5352 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5353 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5354 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5355 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5356 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5357 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5358 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5359 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5360 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5361 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5362 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5363 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5364 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5365 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5366 | { |
| 5367 | std::vector<spv::Id> operands; |
| 5368 | operands.push_back(operand); |
| 5369 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 5370 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5371 | case glslang::EOpSubgroupAll: |
| 5372 | case glslang::EOpSubgroupAny: |
| 5373 | case glslang::EOpSubgroupAllEqual: |
| 5374 | case glslang::EOpSubgroupBroadcastFirst: |
| 5375 | case glslang::EOpSubgroupBallot: |
| 5376 | case glslang::EOpSubgroupInverseBallot: |
| 5377 | case glslang::EOpSubgroupBallotBitCount: |
| 5378 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5379 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5380 | case glslang::EOpSubgroupBallotFindLSB: |
| 5381 | case glslang::EOpSubgroupBallotFindMSB: |
| 5382 | case glslang::EOpSubgroupAdd: |
| 5383 | case glslang::EOpSubgroupMul: |
| 5384 | case glslang::EOpSubgroupMin: |
| 5385 | case glslang::EOpSubgroupMax: |
| 5386 | case glslang::EOpSubgroupAnd: |
| 5387 | case glslang::EOpSubgroupOr: |
| 5388 | case glslang::EOpSubgroupXor: |
| 5389 | case glslang::EOpSubgroupInclusiveAdd: |
| 5390 | case glslang::EOpSubgroupInclusiveMul: |
| 5391 | case glslang::EOpSubgroupInclusiveMin: |
| 5392 | case glslang::EOpSubgroupInclusiveMax: |
| 5393 | case glslang::EOpSubgroupInclusiveAnd: |
| 5394 | case glslang::EOpSubgroupInclusiveOr: |
| 5395 | case glslang::EOpSubgroupInclusiveXor: |
| 5396 | case glslang::EOpSubgroupExclusiveAdd: |
| 5397 | case glslang::EOpSubgroupExclusiveMul: |
| 5398 | case glslang::EOpSubgroupExclusiveMin: |
| 5399 | case glslang::EOpSubgroupExclusiveMax: |
| 5400 | case glslang::EOpSubgroupExclusiveAnd: |
| 5401 | case glslang::EOpSubgroupExclusiveOr: |
| 5402 | case glslang::EOpSubgroupExclusiveXor: |
| 5403 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5404 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5405 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 5406 | std::vector<spv::Id> operands; |
| 5407 | operands.push_back(operand); |
| 5408 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 5409 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5410 | #ifdef AMD_EXTENSIONS |
| 5411 | case glslang::EOpMbcnt: |
| 5412 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5413 | libCall = spv::MbcntAMD; |
| 5414 | break; |
| 5415 | |
| 5416 | case glslang::EOpCubeFaceIndex: |
| 5417 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5418 | libCall = spv::CubeFaceIndexAMD; |
| 5419 | break; |
| 5420 | |
| 5421 | case glslang::EOpCubeFaceCoord: |
| 5422 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5423 | libCall = spv::CubeFaceCoordAMD; |
| 5424 | break; |
| 5425 | #endif |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5426 | #ifdef NV_EXTENSIONS |
| 5427 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5428 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 5429 | break; |
| 5430 | #endif |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 5431 | case glslang::EOpConstructReference: |
| 5432 | unaryOp = spv::OpBitcast; |
| 5433 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5434 | default: |
| 5435 | return 0; |
| 5436 | } |
| 5437 | |
| 5438 | spv::Id id; |
| 5439 | if (libCall >= 0) { |
| 5440 | std::vector<spv::Id> args; |
| 5441 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5442 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5443 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5444 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5445 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5446 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5447 | builder.addDecoration(id, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5448 | builder.addDecoration(id, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5449 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5450 | } |
| 5451 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5452 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5453 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5454 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5455 | { |
| 5456 | // Handle unary operations vector by vector. |
| 5457 | // The result type is the same type as the original type. |
| 5458 | // The algorithm is to: |
| 5459 | // - break the matrix into vectors |
| 5460 | // - apply the operation to each vector |
| 5461 | // - make a matrix out the vector results |
| 5462 | |
| 5463 | // get the types sorted out |
| 5464 | int numCols = builder.getNumColumns(operand); |
| 5465 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5466 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 5467 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5468 | std::vector<spv::Id> results; |
| 5469 | |
| 5470 | // do each vector op |
| 5471 | for (int c = 0; c < numCols; ++c) { |
| 5472 | std::vector<unsigned int> indexes; |
| 5473 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5474 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 5475 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5476 | builder.addDecoration(destVec, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5477 | builder.addDecoration(destVec, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5478 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5482 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5483 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5484 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5485 | } |
| 5486 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5487 | // For converting integers where both the bitwidth and the signedness could |
| 5488 | // change, but only do the width change here. The caller is still responsible |
| 5489 | // for the signedness conversion. |
| 5490 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5491 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5492 | // Get the result type width, based on the type to convert to. |
| 5493 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5494 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5495 | case glslang::EOpConvInt16ToUint8: |
| 5496 | case glslang::EOpConvIntToUint8: |
| 5497 | case glslang::EOpConvInt64ToUint8: |
| 5498 | case glslang::EOpConvUint16ToInt8: |
| 5499 | case glslang::EOpConvUintToInt8: |
| 5500 | case glslang::EOpConvUint64ToInt8: |
| 5501 | width = 8; |
| 5502 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5503 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5504 | case glslang::EOpConvIntToUint16: |
| 5505 | case glslang::EOpConvInt64ToUint16: |
| 5506 | case glslang::EOpConvUint8ToInt16: |
| 5507 | case glslang::EOpConvUintToInt16: |
| 5508 | case glslang::EOpConvUint64ToInt16: |
| 5509 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5510 | break; |
| 5511 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5512 | case glslang::EOpConvInt16ToUint: |
| 5513 | case glslang::EOpConvInt64ToUint: |
| 5514 | case glslang::EOpConvUint8ToInt: |
| 5515 | case glslang::EOpConvUint16ToInt: |
| 5516 | case glslang::EOpConvUint64ToInt: |
| 5517 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5518 | break; |
| 5519 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5520 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5521 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5522 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5523 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5524 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5525 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5526 | break; |
| 5527 | |
| 5528 | default: |
| 5529 | assert(false && "Default missing"); |
| 5530 | break; |
| 5531 | } |
| 5532 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5533 | // Get the conversion operation and result type, |
| 5534 | // based on the target width, but the source type. |
| 5535 | spv::Id type = spv::NoType; |
| 5536 | spv::Op convOp = spv::OpNop; |
| 5537 | switch(op) { |
| 5538 | case glslang::EOpConvInt8ToUint16: |
| 5539 | case glslang::EOpConvInt8ToUint: |
| 5540 | case glslang::EOpConvInt8ToUint64: |
| 5541 | case glslang::EOpConvInt16ToUint8: |
| 5542 | case glslang::EOpConvInt16ToUint: |
| 5543 | case glslang::EOpConvInt16ToUint64: |
| 5544 | case glslang::EOpConvIntToUint8: |
| 5545 | case glslang::EOpConvIntToUint16: |
| 5546 | case glslang::EOpConvIntToUint64: |
| 5547 | case glslang::EOpConvInt64ToUint8: |
| 5548 | case glslang::EOpConvInt64ToUint16: |
| 5549 | case glslang::EOpConvInt64ToUint: |
| 5550 | convOp = spv::OpSConvert; |
| 5551 | type = builder.makeIntType(width); |
| 5552 | break; |
| 5553 | default: |
| 5554 | convOp = spv::OpUConvert; |
| 5555 | type = builder.makeUintType(width); |
| 5556 | break; |
| 5557 | } |
| 5558 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5559 | if (vectorSize > 0) |
| 5560 | type = builder.makeVectorType(type, vectorSize); |
| 5561 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5562 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5563 | } |
| 5564 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5565 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 5566 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5567 | { |
| 5568 | spv::Op convOp = spv::OpNop; |
| 5569 | spv::Id zero = 0; |
| 5570 | spv::Id one = 0; |
| 5571 | |
| 5572 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 5573 | |
| 5574 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5575 | case glslang::EOpConvInt8ToBool: |
| 5576 | case glslang::EOpConvUint8ToBool: |
| 5577 | zero = builder.makeUint8Constant(0); |
| 5578 | zero = makeSmearedConstant(zero, vectorSize); |
| 5579 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5580 | case glslang::EOpConvInt16ToBool: |
| 5581 | case glslang::EOpConvUint16ToBool: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5582 | zero = builder.makeUint16Constant(0); |
| 5583 | zero = makeSmearedConstant(zero, vectorSize); |
| 5584 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5585 | case glslang::EOpConvIntToBool: |
| 5586 | case glslang::EOpConvUintToBool: |
| 5587 | zero = builder.makeUintConstant(0); |
| 5588 | zero = makeSmearedConstant(zero, vectorSize); |
| 5589 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5590 | case glslang::EOpConvInt64ToBool: |
| 5591 | case glslang::EOpConvUint64ToBool: |
| 5592 | zero = builder.makeUint64Constant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5593 | zero = makeSmearedConstant(zero, vectorSize); |
| 5594 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5595 | |
| 5596 | case glslang::EOpConvFloatToBool: |
| 5597 | zero = builder.makeFloatConstant(0.0F); |
| 5598 | zero = makeSmearedConstant(zero, vectorSize); |
| 5599 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5600 | |
| 5601 | case glslang::EOpConvDoubleToBool: |
| 5602 | zero = builder.makeDoubleConstant(0.0); |
| 5603 | zero = makeSmearedConstant(zero, vectorSize); |
| 5604 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5605 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5606 | case glslang::EOpConvFloat16ToBool: |
| 5607 | zero = builder.makeFloat16Constant(0.0F); |
| 5608 | zero = makeSmearedConstant(zero, vectorSize); |
| 5609 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5610 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5611 | case glslang::EOpConvBoolToFloat: |
| 5612 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5613 | zero = builder.makeFloatConstant(0.0F); |
| 5614 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5615 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5616 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5617 | case glslang::EOpConvBoolToDouble: |
| 5618 | convOp = spv::OpSelect; |
| 5619 | zero = builder.makeDoubleConstant(0.0); |
| 5620 | one = builder.makeDoubleConstant(1.0); |
| 5621 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5622 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5623 | case glslang::EOpConvBoolToFloat16: |
| 5624 | convOp = spv::OpSelect; |
| 5625 | zero = builder.makeFloat16Constant(0.0F); |
| 5626 | one = builder.makeFloat16Constant(1.0F); |
| 5627 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5628 | |
| 5629 | case glslang::EOpConvBoolToInt8: |
| 5630 | zero = builder.makeInt8Constant(0); |
| 5631 | one = builder.makeInt8Constant(1); |
| 5632 | convOp = spv::OpSelect; |
| 5633 | break; |
| 5634 | |
| 5635 | case glslang::EOpConvBoolToUint8: |
| 5636 | zero = builder.makeUint8Constant(0); |
| 5637 | one = builder.makeUint8Constant(1); |
| 5638 | convOp = spv::OpSelect; |
| 5639 | break; |
| 5640 | |
| 5641 | case glslang::EOpConvBoolToInt16: |
| 5642 | zero = builder.makeInt16Constant(0); |
| 5643 | one = builder.makeInt16Constant(1); |
| 5644 | convOp = spv::OpSelect; |
| 5645 | break; |
| 5646 | |
| 5647 | case glslang::EOpConvBoolToUint16: |
| 5648 | zero = builder.makeUint16Constant(0); |
| 5649 | one = builder.makeUint16Constant(1); |
| 5650 | convOp = spv::OpSelect; |
| 5651 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5652 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5653 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5654 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5655 | if (op == glslang::EOpConvBoolToInt64) |
| 5656 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5657 | else |
| 5658 | zero = builder.makeIntConstant(0); |
| 5659 | |
| 5660 | if (op == glslang::EOpConvBoolToInt64) |
| 5661 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5662 | else |
| 5663 | one = builder.makeIntConstant(1); |
| 5664 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5665 | convOp = spv::OpSelect; |
| 5666 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5667 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5668 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5669 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5670 | if (op == glslang::EOpConvBoolToUint64) |
| 5671 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5672 | else |
| 5673 | zero = builder.makeUintConstant(0); |
| 5674 | |
| 5675 | if (op == glslang::EOpConvBoolToUint64) |
| 5676 | one = builder.makeUint64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5677 | else |
| 5678 | one = builder.makeUintConstant(1); |
| 5679 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5680 | convOp = spv::OpSelect; |
| 5681 | break; |
| 5682 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5683 | case glslang::EOpConvInt8ToFloat16: |
| 5684 | case glslang::EOpConvInt8ToFloat: |
| 5685 | case glslang::EOpConvInt8ToDouble: |
| 5686 | case glslang::EOpConvInt16ToFloat16: |
| 5687 | case glslang::EOpConvInt16ToFloat: |
| 5688 | case glslang::EOpConvInt16ToDouble: |
| 5689 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5690 | case glslang::EOpConvIntToFloat: |
| 5691 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5692 | case glslang::EOpConvInt64ToFloat: |
| 5693 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5694 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5695 | convOp = spv::OpConvertSToF; |
| 5696 | break; |
| 5697 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5698 | case glslang::EOpConvUint8ToFloat16: |
| 5699 | case glslang::EOpConvUint8ToFloat: |
| 5700 | case glslang::EOpConvUint8ToDouble: |
| 5701 | case glslang::EOpConvUint16ToFloat16: |
| 5702 | case glslang::EOpConvUint16ToFloat: |
| 5703 | case glslang::EOpConvUint16ToDouble: |
| 5704 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5705 | case glslang::EOpConvUintToFloat: |
| 5706 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5707 | case glslang::EOpConvUint64ToFloat: |
| 5708 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5709 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5710 | convOp = spv::OpConvertUToF; |
| 5711 | break; |
| 5712 | |
| 5713 | case glslang::EOpConvDoubleToFloat: |
| 5714 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5715 | case glslang::EOpConvDoubleToFloat16: |
| 5716 | case glslang::EOpConvFloat16ToDouble: |
| 5717 | case glslang::EOpConvFloatToFloat16: |
| 5718 | case glslang::EOpConvFloat16ToFloat: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5719 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 5720 | if (builder.isMatrixType(destType)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5721 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5722 | break; |
| 5723 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5724 | case glslang::EOpConvFloat16ToInt8: |
| 5725 | case glslang::EOpConvFloatToInt8: |
| 5726 | case glslang::EOpConvDoubleToInt8: |
| 5727 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5728 | case glslang::EOpConvFloatToInt16: |
| 5729 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5730 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5731 | case glslang::EOpConvFloatToInt: |
| 5732 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5733 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5734 | case glslang::EOpConvFloatToInt64: |
| 5735 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5736 | convOp = spv::OpConvertFToS; |
| 5737 | break; |
| 5738 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5739 | case glslang::EOpConvUint8ToInt8: |
| 5740 | case glslang::EOpConvInt8ToUint8: |
| 5741 | case glslang::EOpConvUint16ToInt16: |
| 5742 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5743 | case glslang::EOpConvUintToInt: |
| 5744 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5745 | case glslang::EOpConvUint64ToInt64: |
| 5746 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 5747 | if (builder.isInSpecConstCodeGenMode()) { |
| 5748 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5749 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 5750 | zero = builder.makeUint8Constant(0); |
| 5751 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5752 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5753 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 5754 | zero = builder.makeUint64Constant(0); |
| 5755 | } else { |
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 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 5758 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 5759 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5760 | // generating for OpSpecConstantOp instruction. |
| 5761 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5762 | } |
| 5763 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5764 | convOp = spv::OpBitcast; |
| 5765 | break; |
| 5766 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5767 | case glslang::EOpConvFloat16ToUint8: |
| 5768 | case glslang::EOpConvFloatToUint8: |
| 5769 | case glslang::EOpConvDoubleToUint8: |
| 5770 | case glslang::EOpConvFloat16ToUint16: |
| 5771 | case glslang::EOpConvFloatToUint16: |
| 5772 | case glslang::EOpConvDoubleToUint16: |
| 5773 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5774 | case glslang::EOpConvFloatToUint: |
| 5775 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5776 | case glslang::EOpConvFloatToUint64: |
| 5777 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5778 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5779 | convOp = spv::OpConvertFToU; |
| 5780 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5781 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5782 | case glslang::EOpConvInt8ToInt16: |
| 5783 | case glslang::EOpConvInt8ToInt: |
| 5784 | case glslang::EOpConvInt8ToInt64: |
| 5785 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5786 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5787 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5788 | case glslang::EOpConvIntToInt8: |
| 5789 | case glslang::EOpConvIntToInt16: |
| 5790 | case glslang::EOpConvIntToInt64: |
| 5791 | case glslang::EOpConvInt64ToInt8: |
| 5792 | case glslang::EOpConvInt64ToInt16: |
| 5793 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5794 | convOp = spv::OpSConvert; |
| 5795 | break; |
| 5796 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5797 | case glslang::EOpConvUint8ToUint16: |
| 5798 | case glslang::EOpConvUint8ToUint: |
| 5799 | case glslang::EOpConvUint8ToUint64: |
| 5800 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5801 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5802 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5803 | case glslang::EOpConvUintToUint8: |
| 5804 | case glslang::EOpConvUintToUint16: |
| 5805 | case glslang::EOpConvUintToUint64: |
| 5806 | case glslang::EOpConvUint64ToUint8: |
| 5807 | case glslang::EOpConvUint64ToUint16: |
| 5808 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5809 | convOp = spv::OpUConvert; |
| 5810 | break; |
| 5811 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5812 | case glslang::EOpConvInt8ToUint16: |
| 5813 | case glslang::EOpConvInt8ToUint: |
| 5814 | case glslang::EOpConvInt8ToUint64: |
| 5815 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5816 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5817 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5818 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5819 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5820 | case glslang::EOpConvIntToUint64: |
| 5821 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5822 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5823 | case glslang::EOpConvInt64ToUint: |
| 5824 | case glslang::EOpConvUint8ToInt16: |
| 5825 | case glslang::EOpConvUint8ToInt: |
| 5826 | case glslang::EOpConvUint8ToInt64: |
| 5827 | case glslang::EOpConvUint16ToInt8: |
| 5828 | case glslang::EOpConvUint16ToInt: |
| 5829 | case glslang::EOpConvUint16ToInt64: |
| 5830 | case glslang::EOpConvUintToInt8: |
| 5831 | case glslang::EOpConvUintToInt16: |
| 5832 | case glslang::EOpConvUintToInt64: |
| 5833 | case glslang::EOpConvUint64ToInt8: |
| 5834 | case glslang::EOpConvUint64ToInt16: |
| 5835 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5836 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5837 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5838 | |
| 5839 | if (builder.isInSpecConstCodeGenMode()) { |
| 5840 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5841 | switch(op) { |
| 5842 | case glslang::EOpConvInt16ToUint8: |
| 5843 | case glslang::EOpConvIntToUint8: |
| 5844 | case glslang::EOpConvInt64ToUint8: |
| 5845 | case glslang::EOpConvUint16ToInt8: |
| 5846 | case glslang::EOpConvUintToInt8: |
| 5847 | case glslang::EOpConvUint64ToInt8: |
| 5848 | zero = builder.makeUint8Constant(0); |
| 5849 | break; |
| 5850 | case glslang::EOpConvInt8ToUint16: |
| 5851 | case glslang::EOpConvIntToUint16: |
| 5852 | case glslang::EOpConvInt64ToUint16: |
| 5853 | case glslang::EOpConvUint8ToInt16: |
| 5854 | case glslang::EOpConvUintToInt16: |
| 5855 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5856 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5857 | break; |
| 5858 | case glslang::EOpConvInt8ToUint: |
| 5859 | case glslang::EOpConvInt16ToUint: |
| 5860 | case glslang::EOpConvInt64ToUint: |
| 5861 | case glslang::EOpConvUint8ToInt: |
| 5862 | case glslang::EOpConvUint16ToInt: |
| 5863 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5864 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5865 | break; |
| 5866 | case glslang::EOpConvInt8ToUint64: |
| 5867 | case glslang::EOpConvInt16ToUint64: |
| 5868 | case glslang::EOpConvIntToUint64: |
| 5869 | case glslang::EOpConvUint8ToInt64: |
| 5870 | case glslang::EOpConvUint16ToInt64: |
| 5871 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5872 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5873 | break; |
| 5874 | default: |
| 5875 | assert(false && "Default missing"); |
| 5876 | break; |
| 5877 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5878 | zero = makeSmearedConstant(zero, vectorSize); |
| 5879 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5880 | // generating for OpSpecConstantOp instruction. |
| 5881 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5882 | } |
| 5883 | // For normal run-time conversion instruction, use OpBitcast. |
| 5884 | convOp = spv::OpBitcast; |
| 5885 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 5886 | case glslang::EOpConvUint64ToPtr: |
| 5887 | convOp = spv::OpConvertUToPtr; |
| 5888 | break; |
| 5889 | case glslang::EOpConvPtrToUint64: |
| 5890 | convOp = spv::OpConvertPtrToU; |
| 5891 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5892 | default: |
| 5893 | break; |
| 5894 | } |
| 5895 | |
| 5896 | spv::Id result = 0; |
| 5897 | if (convOp == spv::OpNop) |
| 5898 | return result; |
| 5899 | |
| 5900 | if (convOp == spv::OpSelect) { |
| 5901 | zero = makeSmearedConstant(zero, vectorSize); |
| 5902 | one = makeSmearedConstant(one, vectorSize); |
| 5903 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 5904 | } else |
| 5905 | result = builder.createUnaryOp(convOp, destType, operand); |
| 5906 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5907 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5908 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5909 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5910 | } |
| 5911 | |
| 5912 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 5913 | { |
| 5914 | if (vectorSize == 0) |
| 5915 | return constant; |
| 5916 | |
| 5917 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 5918 | std::vector<spv::Id> components; |
| 5919 | for (int c = 0; c < vectorSize; ++c) |
| 5920 | components.push_back(constant); |
| 5921 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 5922 | } |
| 5923 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5924 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5925 | 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] | 5926 | { |
| 5927 | spv::Op opCode = spv::OpNop; |
| 5928 | |
| 5929 | switch (op) { |
| 5930 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5931 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5932 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5933 | opCode = spv::OpAtomicIAdd; |
| 5934 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5935 | case glslang::EOpAtomicCounterSubtract: |
| 5936 | opCode = spv::OpAtomicISub; |
| 5937 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5938 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5939 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5940 | case glslang::EOpAtomicCounterMin: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5941 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5942 | break; |
| 5943 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5944 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5945 | case glslang::EOpAtomicCounterMax: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5946 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5947 | break; |
| 5948 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5949 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5950 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5951 | opCode = spv::OpAtomicAnd; |
| 5952 | break; |
| 5953 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5954 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5955 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5956 | opCode = spv::OpAtomicOr; |
| 5957 | break; |
| 5958 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5959 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5960 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5961 | opCode = spv::OpAtomicXor; |
| 5962 | break; |
| 5963 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5964 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5965 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5966 | opCode = spv::OpAtomicExchange; |
| 5967 | break; |
| 5968 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5969 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5970 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5971 | opCode = spv::OpAtomicCompareExchange; |
| 5972 | break; |
| 5973 | case glslang::EOpAtomicCounterIncrement: |
| 5974 | opCode = spv::OpAtomicIIncrement; |
| 5975 | break; |
| 5976 | case glslang::EOpAtomicCounterDecrement: |
| 5977 | opCode = spv::OpAtomicIDecrement; |
| 5978 | break; |
| 5979 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5980 | case glslang::EOpImageAtomicLoad: |
| 5981 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5982 | opCode = spv::OpAtomicLoad; |
| 5983 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5984 | case glslang::EOpAtomicStore: |
| 5985 | case glslang::EOpImageAtomicStore: |
| 5986 | opCode = spv::OpAtomicStore; |
| 5987 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5988 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5989 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5990 | break; |
| 5991 | } |
| 5992 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5993 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 5994 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 5995 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5996 | // Sort out the operands |
| 5997 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5998 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 5999 | // - compare-exchange swaps the value and comparator |
| 6000 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6001 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6002 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 6003 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 6004 | spv::Id scopeId; |
| 6005 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6006 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 6007 | } else { |
| 6008 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 6009 | } |
| 6010 | // semantics default to relaxed |
| 6011 | spv::Id semanticsId = builder.makeUintConstant(spv::MemorySemanticsMaskNone); |
| 6012 | spv::Id semanticsId2 = semanticsId; |
| 6013 | |
| 6014 | pointerId = operands[0]; |
| 6015 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 6016 | // no additional operands |
| 6017 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 6018 | compareId = operands[1]; |
| 6019 | valueId = operands[2]; |
| 6020 | if (operands.size() > 3) { |
| 6021 | scopeId = operands[3]; |
| 6022 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 6023 | semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 6024 | } |
| 6025 | } else if (opCode == spv::OpAtomicLoad) { |
| 6026 | if (operands.size() > 1) { |
| 6027 | scopeId = operands[1]; |
| 6028 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 6029 | } |
| 6030 | } else { |
| 6031 | // atomic store or RMW |
| 6032 | valueId = operands[1]; |
| 6033 | if (operands.size() > 2) { |
| 6034 | scopeId = operands[2]; |
| 6035 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 6036 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 6037 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6038 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6039 | // Check for capabilities |
| 6040 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
| 6041 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 6042 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6043 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6044 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6045 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 6046 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6047 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6048 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6049 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 6050 | spvAtomicOperands.push_back(pointerId); |
| 6051 | spvAtomicOperands.push_back(scopeId); |
| 6052 | spvAtomicOperands.push_back(semanticsId); |
| 6053 | if (opCode == spv::OpAtomicCompareExchange) { |
| 6054 | spvAtomicOperands.push_back(semanticsId2); |
| 6055 | spvAtomicOperands.push_back(valueId); |
| 6056 | spvAtomicOperands.push_back(compareId); |
| 6057 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 6058 | spvAtomicOperands.push_back(valueId); |
| 6059 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6060 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6061 | if (opCode == spv::OpAtomicStore) { |
| 6062 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 6063 | return 0; |
| 6064 | } else { |
| 6065 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 6066 | |
| 6067 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 6068 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 6069 | if (op == glslang::EOpAtomicCounterDecrement) |
| 6070 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 6071 | |
| 6072 | return resultId; |
| 6073 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6074 | } |
| 6075 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6076 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6077 | 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] | 6078 | { |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 6079 | #ifdef AMD_EXTENSIONS |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6080 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6081 | bool isFloat = isTypeFloat(typeProxy); |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 6082 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6083 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6084 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6085 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6086 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 6087 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6088 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 6089 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6090 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 6091 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6092 | } else if (op == glslang::EOpAnyInvocation || |
| 6093 | op == glslang::EOpAllInvocations || |
| 6094 | op == glslang::EOpAllInvocationsEqual) { |
| 6095 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 6096 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6097 | } else { |
| 6098 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 6099 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6100 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6101 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6102 | op == glslang::EOpAddInvocationsNonUniform || |
| 6103 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6104 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6105 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 6106 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 6107 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 6108 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6109 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 6110 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6111 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6112 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6113 | switch (op) { |
| 6114 | case glslang::EOpMinInvocations: |
| 6115 | case glslang::EOpMaxInvocations: |
| 6116 | case glslang::EOpAddInvocations: |
| 6117 | case glslang::EOpMinInvocationsNonUniform: |
| 6118 | case glslang::EOpMaxInvocationsNonUniform: |
| 6119 | case glslang::EOpAddInvocationsNonUniform: |
| 6120 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6121 | break; |
| 6122 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6123 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6124 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6125 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6126 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6127 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6128 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6129 | break; |
| 6130 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6131 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6132 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6133 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6134 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6135 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6136 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6137 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 6138 | default: |
| 6139 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6140 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6141 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6142 | spvGroupOperands.push_back(scope); |
| 6143 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6144 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6145 | spvGroupOperands.push_back(groupOp); |
| 6146 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6147 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6148 | } |
| 6149 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6150 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6151 | spv::IdImmediate op = { true, *opIt }; |
| 6152 | spvGroupOperands.push_back(op); |
| 6153 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6154 | |
| 6155 | switch (op) { |
| 6156 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6157 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6158 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6159 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6160 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6161 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6162 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6163 | opCode = spv::OpSubgroupAllEqualKHR; |
| 6164 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6165 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6166 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6167 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6168 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6169 | break; |
| 6170 | case glslang::EOpReadFirstInvocation: |
| 6171 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6172 | break; |
| 6173 | case glslang::EOpBallot: |
| 6174 | { |
| 6175 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6176 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6177 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6178 | // |
| 6179 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6180 | // |
| 6181 | spv::Id uintType = builder.makeUintType(32); |
| 6182 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6183 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6184 | |
| 6185 | std::vector<spv::Id> components; |
| 6186 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6187 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6188 | |
| 6189 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6190 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6191 | builder.createCompositeConstruct(uvec2Type, components)); |
| 6192 | } |
| 6193 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6194 | #ifdef AMD_EXTENSIONS |
| 6195 | case glslang::EOpMinInvocations: |
| 6196 | case glslang::EOpMaxInvocations: |
| 6197 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6198 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6199 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6200 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6201 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6202 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6203 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6204 | if (op == glslang::EOpMinInvocations || |
| 6205 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 6206 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6207 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6208 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6209 | else { |
| 6210 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6211 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6212 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6213 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6214 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6215 | } else if (op == glslang::EOpMaxInvocations || |
| 6216 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 6217 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6218 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6219 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6220 | else { |
| 6221 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6222 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6223 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6224 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6225 | } |
| 6226 | } else { |
| 6227 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6228 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6229 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6230 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6231 | } |
| 6232 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6233 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6234 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6235 | |
| 6236 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6237 | case glslang::EOpMinInvocationsNonUniform: |
| 6238 | case glslang::EOpMaxInvocationsNonUniform: |
| 6239 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6240 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6241 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6242 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6243 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6244 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6245 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6246 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6247 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6248 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6249 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6250 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6251 | else { |
| 6252 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6253 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6254 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6255 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6256 | } |
| 6257 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6258 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 6259 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6260 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6261 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6262 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6263 | else { |
| 6264 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6265 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6266 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6267 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6268 | } |
| 6269 | } |
| 6270 | else { |
| 6271 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6272 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6273 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6274 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6275 | } |
| 6276 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6277 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6278 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6279 | |
| 6280 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6281 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6282 | default: |
| 6283 | logger->missingFunctionality("invocation operation"); |
| 6284 | return spv::NoResult; |
| 6285 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6286 | |
| 6287 | assert(opCode != spv::OpNop); |
| 6288 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6289 | } |
| 6290 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6291 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6292 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 6293 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6294 | { |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6295 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6296 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6297 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6298 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6299 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6300 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 6301 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 6302 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6303 | #else |
| 6304 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6305 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6306 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 6307 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6308 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6309 | |
| 6310 | // Handle group invocation operations scalar by scalar. |
| 6311 | // The result type is the same type as the original type. |
| 6312 | // The algorithm is to: |
| 6313 | // - break the vector into scalars |
| 6314 | // - apply the operation to each scalar |
| 6315 | // - make a vector out the scalar results |
| 6316 | |
| 6317 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6318 | int numComponents = builder.getNumComponents(operands[0]); |
| 6319 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6320 | std::vector<spv::Id> results; |
| 6321 | |
| 6322 | // do each scalar op |
| 6323 | for (int comp = 0; comp < numComponents; ++comp) { |
| 6324 | std::vector<unsigned int> indexes; |
| 6325 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6326 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 6327 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6328 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 6329 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6330 | spv::IdImmediate operand = { true, operands[1] }; |
| 6331 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6332 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6333 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6334 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6335 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6336 | spv::IdImmediate operand = { true, operands[1] }; |
| 6337 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6338 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6339 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6340 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6341 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6342 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6343 | spvGroupOperands.push_back(scalar); |
| 6344 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6345 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6346 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6347 | } |
| 6348 | |
| 6349 | // put the pieces together |
| 6350 | return builder.createCompositeConstruct(typeId, results); |
| 6351 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6352 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6353 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6354 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 6355 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6356 | { |
| 6357 | // Add the required capabilities. |
| 6358 | switch (op) { |
| 6359 | case glslang::EOpSubgroupElect: |
| 6360 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6361 | break; |
| 6362 | case glslang::EOpSubgroupAll: |
| 6363 | case glslang::EOpSubgroupAny: |
| 6364 | case glslang::EOpSubgroupAllEqual: |
| 6365 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6366 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 6367 | break; |
| 6368 | case glslang::EOpSubgroupBroadcast: |
| 6369 | case glslang::EOpSubgroupBroadcastFirst: |
| 6370 | case glslang::EOpSubgroupBallot: |
| 6371 | case glslang::EOpSubgroupInverseBallot: |
| 6372 | case glslang::EOpSubgroupBallotBitExtract: |
| 6373 | case glslang::EOpSubgroupBallotBitCount: |
| 6374 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6375 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6376 | case glslang::EOpSubgroupBallotFindLSB: |
| 6377 | case glslang::EOpSubgroupBallotFindMSB: |
| 6378 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6379 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 6380 | break; |
| 6381 | case glslang::EOpSubgroupShuffle: |
| 6382 | case glslang::EOpSubgroupShuffleXor: |
| 6383 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6384 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 6385 | break; |
| 6386 | case glslang::EOpSubgroupShuffleUp: |
| 6387 | case glslang::EOpSubgroupShuffleDown: |
| 6388 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6389 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 6390 | break; |
| 6391 | case glslang::EOpSubgroupAdd: |
| 6392 | case glslang::EOpSubgroupMul: |
| 6393 | case glslang::EOpSubgroupMin: |
| 6394 | case glslang::EOpSubgroupMax: |
| 6395 | case glslang::EOpSubgroupAnd: |
| 6396 | case glslang::EOpSubgroupOr: |
| 6397 | case glslang::EOpSubgroupXor: |
| 6398 | case glslang::EOpSubgroupInclusiveAdd: |
| 6399 | case glslang::EOpSubgroupInclusiveMul: |
| 6400 | case glslang::EOpSubgroupInclusiveMin: |
| 6401 | case glslang::EOpSubgroupInclusiveMax: |
| 6402 | case glslang::EOpSubgroupInclusiveAnd: |
| 6403 | case glslang::EOpSubgroupInclusiveOr: |
| 6404 | case glslang::EOpSubgroupInclusiveXor: |
| 6405 | case glslang::EOpSubgroupExclusiveAdd: |
| 6406 | case glslang::EOpSubgroupExclusiveMul: |
| 6407 | case glslang::EOpSubgroupExclusiveMin: |
| 6408 | case glslang::EOpSubgroupExclusiveMax: |
| 6409 | case glslang::EOpSubgroupExclusiveAnd: |
| 6410 | case glslang::EOpSubgroupExclusiveOr: |
| 6411 | case glslang::EOpSubgroupExclusiveXor: |
| 6412 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6413 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 6414 | break; |
| 6415 | case glslang::EOpSubgroupClusteredAdd: |
| 6416 | case glslang::EOpSubgroupClusteredMul: |
| 6417 | case glslang::EOpSubgroupClusteredMin: |
| 6418 | case glslang::EOpSubgroupClusteredMax: |
| 6419 | case glslang::EOpSubgroupClusteredAnd: |
| 6420 | case glslang::EOpSubgroupClusteredOr: |
| 6421 | case glslang::EOpSubgroupClusteredXor: |
| 6422 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6423 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 6424 | break; |
| 6425 | case glslang::EOpSubgroupQuadBroadcast: |
| 6426 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6427 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6428 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 6429 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6430 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 6431 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6432 | #ifdef NV_EXTENSIONS |
| 6433 | case glslang::EOpSubgroupPartitionedAdd: |
| 6434 | case glslang::EOpSubgroupPartitionedMul: |
| 6435 | case glslang::EOpSubgroupPartitionedMin: |
| 6436 | case glslang::EOpSubgroupPartitionedMax: |
| 6437 | case glslang::EOpSubgroupPartitionedAnd: |
| 6438 | case glslang::EOpSubgroupPartitionedOr: |
| 6439 | case glslang::EOpSubgroupPartitionedXor: |
| 6440 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6441 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6442 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6443 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6444 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6445 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6446 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6447 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6448 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6449 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6450 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6451 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6452 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6453 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6454 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 6455 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 6456 | break; |
| 6457 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6458 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6459 | } |
| 6460 | |
| 6461 | const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
| 6462 | const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
| 6463 | const bool isBool = typeProxy == glslang::EbtBool; |
| 6464 | |
| 6465 | spv::Op opCode = spv::OpNop; |
| 6466 | |
| 6467 | // Figure out which opcode to use. |
| 6468 | switch (op) { |
| 6469 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 6470 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 6471 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 6472 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 6473 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 6474 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 6475 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 6476 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 6477 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 6478 | case glslang::EOpSubgroupBallotBitCount: |
| 6479 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6480 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 6481 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 6482 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 6483 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 6484 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 6485 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 6486 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 6487 | case glslang::EOpSubgroupAdd: |
| 6488 | case glslang::EOpSubgroupInclusiveAdd: |
| 6489 | case glslang::EOpSubgroupExclusiveAdd: |
| 6490 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6491 | #ifdef NV_EXTENSIONS |
| 6492 | case glslang::EOpSubgroupPartitionedAdd: |
| 6493 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6494 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6495 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6496 | if (isFloat) { |
| 6497 | opCode = spv::OpGroupNonUniformFAdd; |
| 6498 | } else { |
| 6499 | opCode = spv::OpGroupNonUniformIAdd; |
| 6500 | } |
| 6501 | break; |
| 6502 | case glslang::EOpSubgroupMul: |
| 6503 | case glslang::EOpSubgroupInclusiveMul: |
| 6504 | case glslang::EOpSubgroupExclusiveMul: |
| 6505 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6506 | #ifdef NV_EXTENSIONS |
| 6507 | case glslang::EOpSubgroupPartitionedMul: |
| 6508 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6509 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6510 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6511 | if (isFloat) { |
| 6512 | opCode = spv::OpGroupNonUniformFMul; |
| 6513 | } else { |
| 6514 | opCode = spv::OpGroupNonUniformIMul; |
| 6515 | } |
| 6516 | break; |
| 6517 | case glslang::EOpSubgroupMin: |
| 6518 | case glslang::EOpSubgroupInclusiveMin: |
| 6519 | case glslang::EOpSubgroupExclusiveMin: |
| 6520 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6521 | #ifdef NV_EXTENSIONS |
| 6522 | case glslang::EOpSubgroupPartitionedMin: |
| 6523 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6524 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6525 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6526 | if (isFloat) { |
| 6527 | opCode = spv::OpGroupNonUniformFMin; |
| 6528 | } else if (isUnsigned) { |
| 6529 | opCode = spv::OpGroupNonUniformUMin; |
| 6530 | } else { |
| 6531 | opCode = spv::OpGroupNonUniformSMin; |
| 6532 | } |
| 6533 | break; |
| 6534 | case glslang::EOpSubgroupMax: |
| 6535 | case glslang::EOpSubgroupInclusiveMax: |
| 6536 | case glslang::EOpSubgroupExclusiveMax: |
| 6537 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6538 | #ifdef NV_EXTENSIONS |
| 6539 | case glslang::EOpSubgroupPartitionedMax: |
| 6540 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6541 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6542 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6543 | if (isFloat) { |
| 6544 | opCode = spv::OpGroupNonUniformFMax; |
| 6545 | } else if (isUnsigned) { |
| 6546 | opCode = spv::OpGroupNonUniformUMax; |
| 6547 | } else { |
| 6548 | opCode = spv::OpGroupNonUniformSMax; |
| 6549 | } |
| 6550 | break; |
| 6551 | case glslang::EOpSubgroupAnd: |
| 6552 | case glslang::EOpSubgroupInclusiveAnd: |
| 6553 | case glslang::EOpSubgroupExclusiveAnd: |
| 6554 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6555 | #ifdef NV_EXTENSIONS |
| 6556 | case glslang::EOpSubgroupPartitionedAnd: |
| 6557 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6558 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6559 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6560 | if (isBool) { |
| 6561 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 6562 | } else { |
| 6563 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 6564 | } |
| 6565 | break; |
| 6566 | case glslang::EOpSubgroupOr: |
| 6567 | case glslang::EOpSubgroupInclusiveOr: |
| 6568 | case glslang::EOpSubgroupExclusiveOr: |
| 6569 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6570 | #ifdef NV_EXTENSIONS |
| 6571 | case glslang::EOpSubgroupPartitionedOr: |
| 6572 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6573 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6574 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6575 | if (isBool) { |
| 6576 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 6577 | } else { |
| 6578 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 6579 | } |
| 6580 | break; |
| 6581 | case glslang::EOpSubgroupXor: |
| 6582 | case glslang::EOpSubgroupInclusiveXor: |
| 6583 | case glslang::EOpSubgroupExclusiveXor: |
| 6584 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6585 | #ifdef NV_EXTENSIONS |
| 6586 | case glslang::EOpSubgroupPartitionedXor: |
| 6587 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6588 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6589 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6590 | if (isBool) { |
| 6591 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 6592 | } else { |
| 6593 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 6594 | } |
| 6595 | break; |
| 6596 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 6597 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6598 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6599 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 6600 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6601 | } |
| 6602 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6603 | // get the right Group Operation |
| 6604 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6605 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6606 | default: |
| 6607 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6608 | case glslang::EOpSubgroupBallotBitCount: |
| 6609 | case glslang::EOpSubgroupAdd: |
| 6610 | case glslang::EOpSubgroupMul: |
| 6611 | case glslang::EOpSubgroupMin: |
| 6612 | case glslang::EOpSubgroupMax: |
| 6613 | case glslang::EOpSubgroupAnd: |
| 6614 | case glslang::EOpSubgroupOr: |
| 6615 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6616 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6617 | break; |
| 6618 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6619 | case glslang::EOpSubgroupInclusiveAdd: |
| 6620 | case glslang::EOpSubgroupInclusiveMul: |
| 6621 | case glslang::EOpSubgroupInclusiveMin: |
| 6622 | case glslang::EOpSubgroupInclusiveMax: |
| 6623 | case glslang::EOpSubgroupInclusiveAnd: |
| 6624 | case glslang::EOpSubgroupInclusiveOr: |
| 6625 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6626 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6627 | break; |
| 6628 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6629 | case glslang::EOpSubgroupExclusiveAdd: |
| 6630 | case glslang::EOpSubgroupExclusiveMul: |
| 6631 | case glslang::EOpSubgroupExclusiveMin: |
| 6632 | case glslang::EOpSubgroupExclusiveMax: |
| 6633 | case glslang::EOpSubgroupExclusiveAnd: |
| 6634 | case glslang::EOpSubgroupExclusiveOr: |
| 6635 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6636 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6637 | break; |
| 6638 | case glslang::EOpSubgroupClusteredAdd: |
| 6639 | case glslang::EOpSubgroupClusteredMul: |
| 6640 | case glslang::EOpSubgroupClusteredMin: |
| 6641 | case glslang::EOpSubgroupClusteredMax: |
| 6642 | case glslang::EOpSubgroupClusteredAnd: |
| 6643 | case glslang::EOpSubgroupClusteredOr: |
| 6644 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6645 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6646 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6647 | #ifdef NV_EXTENSIONS |
| 6648 | case glslang::EOpSubgroupPartitionedAdd: |
| 6649 | case glslang::EOpSubgroupPartitionedMul: |
| 6650 | case glslang::EOpSubgroupPartitionedMin: |
| 6651 | case glslang::EOpSubgroupPartitionedMax: |
| 6652 | case glslang::EOpSubgroupPartitionedAnd: |
| 6653 | case glslang::EOpSubgroupPartitionedOr: |
| 6654 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6655 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6656 | break; |
| 6657 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6658 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6659 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6660 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6661 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6662 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6663 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6664 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6665 | break; |
| 6666 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6667 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6668 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6669 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6670 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6671 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6672 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6673 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6674 | break; |
| 6675 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6676 | } |
| 6677 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6678 | // build the instruction |
| 6679 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 6680 | |
| 6681 | // Every operation begins with the Execution Scope operand. |
| 6682 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6683 | spvGroupOperands.push_back(executionScope); |
| 6684 | |
| 6685 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 6686 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6687 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6688 | spvGroupOperands.push_back(groupOperand); |
| 6689 | } |
| 6690 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6691 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6692 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 6693 | spv::IdImmediate operand = { true, *opIt }; |
| 6694 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6695 | } |
| 6696 | |
| 6697 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6698 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6699 | switch (op) { |
| 6700 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6701 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 6702 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 6703 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 6704 | } |
| 6705 | if (directionId != spv::NoResult) { |
| 6706 | spv::IdImmediate direction = { true, directionId }; |
| 6707 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6708 | } |
| 6709 | |
| 6710 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 6711 | } |
| 6712 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6713 | 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] | 6714 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6715 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6716 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6717 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6718 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6719 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6720 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 6721 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6722 | spv::Id typeId0 = 0; |
| 6723 | if (consumedOperands > 0) |
| 6724 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6725 | spv::Id typeId1 = 0; |
| 6726 | if (consumedOperands > 1) |
| 6727 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6728 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6729 | |
| 6730 | switch (op) { |
| 6731 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6732 | if (isFloat) |
| 6733 | libCall = spv::GLSLstd450FMin; |
| 6734 | else if (isUnsigned) |
| 6735 | libCall = spv::GLSLstd450UMin; |
| 6736 | else |
| 6737 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6738 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6739 | break; |
| 6740 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6741 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6742 | break; |
| 6743 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6744 | if (isFloat) |
| 6745 | libCall = spv::GLSLstd450FMax; |
| 6746 | else if (isUnsigned) |
| 6747 | libCall = spv::GLSLstd450UMax; |
| 6748 | else |
| 6749 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6750 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6751 | break; |
| 6752 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6753 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6754 | break; |
| 6755 | case glslang::EOpDot: |
| 6756 | opCode = spv::OpDot; |
| 6757 | break; |
| 6758 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6759 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6760 | break; |
| 6761 | |
| 6762 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6763 | if (isFloat) |
| 6764 | libCall = spv::GLSLstd450FClamp; |
| 6765 | else if (isUnsigned) |
| 6766 | libCall = spv::GLSLstd450UClamp; |
| 6767 | else |
| 6768 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6769 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 6770 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6771 | break; |
| 6772 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6773 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 6774 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6775 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6776 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6777 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6778 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6779 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6780 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6781 | break; |
| 6782 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6783 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6784 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6785 | break; |
| 6786 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6787 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6788 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 6789 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6790 | break; |
| 6791 | |
| 6792 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6793 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6794 | break; |
| 6795 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6796 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6797 | break; |
| 6798 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6799 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6800 | break; |
| 6801 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6802 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6803 | break; |
| 6804 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6805 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6806 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6807 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6808 | #ifdef AMD_EXTENSIONS |
| 6809 | if (typeProxy == glslang::EbtFloat16) |
| 6810 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6811 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6812 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 6813 | break; |
| 6814 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6815 | #ifdef AMD_EXTENSIONS |
| 6816 | if (typeProxy == glslang::EbtFloat16) |
| 6817 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6818 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6819 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 6820 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6821 | case glslang::EOpAddCarry: |
| 6822 | opCode = spv::OpIAddCarry; |
| 6823 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6824 | consumedOperands = 2; |
| 6825 | break; |
| 6826 | case glslang::EOpSubBorrow: |
| 6827 | opCode = spv::OpISubBorrow; |
| 6828 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6829 | consumedOperands = 2; |
| 6830 | break; |
| 6831 | case glslang::EOpUMulExtended: |
| 6832 | opCode = spv::OpUMulExtended; |
| 6833 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6834 | consumedOperands = 2; |
| 6835 | break; |
| 6836 | case glslang::EOpIMulExtended: |
| 6837 | opCode = spv::OpSMulExtended; |
| 6838 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6839 | consumedOperands = 2; |
| 6840 | break; |
| 6841 | case glslang::EOpBitfieldExtract: |
| 6842 | if (isUnsigned) |
| 6843 | opCode = spv::OpBitFieldUExtract; |
| 6844 | else |
| 6845 | opCode = spv::OpBitFieldSExtract; |
| 6846 | break; |
| 6847 | case glslang::EOpBitfieldInsert: |
| 6848 | opCode = spv::OpBitFieldInsert; |
| 6849 | break; |
| 6850 | |
| 6851 | case glslang::EOpFma: |
| 6852 | libCall = spv::GLSLstd450Fma; |
| 6853 | break; |
| 6854 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6855 | { |
| 6856 | libCall = spv::GLSLstd450FrexpStruct; |
| 6857 | assert(builder.isPointerType(typeId1)); |
| 6858 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6859 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 6860 | #ifdef AMD_EXTENSIONS |
| 6861 | if (width == 16) |
| 6862 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 6863 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 6864 | #endif |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6865 | if (builder.getNumComponents(operands[0]) == 1) |
| 6866 | frexpIntType = builder.makeIntegerType(width, true); |
| 6867 | else |
| 6868 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 6869 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 6870 | consumedOperands = 1; |
| 6871 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6872 | break; |
| 6873 | case glslang::EOpLdexp: |
| 6874 | libCall = spv::GLSLstd450Ldexp; |
| 6875 | break; |
| 6876 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6877 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6878 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6879 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6880 | case glslang::EOpSubgroupBroadcast: |
| 6881 | case glslang::EOpSubgroupBallotBitExtract: |
| 6882 | case glslang::EOpSubgroupShuffle: |
| 6883 | case glslang::EOpSubgroupShuffleXor: |
| 6884 | case glslang::EOpSubgroupShuffleUp: |
| 6885 | case glslang::EOpSubgroupShuffleDown: |
| 6886 | case glslang::EOpSubgroupClusteredAdd: |
| 6887 | case glslang::EOpSubgroupClusteredMul: |
| 6888 | case glslang::EOpSubgroupClusteredMin: |
| 6889 | case glslang::EOpSubgroupClusteredMax: |
| 6890 | case glslang::EOpSubgroupClusteredAnd: |
| 6891 | case glslang::EOpSubgroupClusteredOr: |
| 6892 | case glslang::EOpSubgroupClusteredXor: |
| 6893 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6894 | #ifdef NV_EXTENSIONS |
| 6895 | case glslang::EOpSubgroupPartitionedAdd: |
| 6896 | case glslang::EOpSubgroupPartitionedMul: |
| 6897 | case glslang::EOpSubgroupPartitionedMin: |
| 6898 | case glslang::EOpSubgroupPartitionedMax: |
| 6899 | case glslang::EOpSubgroupPartitionedAnd: |
| 6900 | case glslang::EOpSubgroupPartitionedOr: |
| 6901 | case glslang::EOpSubgroupPartitionedXor: |
| 6902 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6903 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6904 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6905 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6906 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6907 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6908 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6909 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6910 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6911 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6912 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6913 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6914 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6915 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6916 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6917 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 6918 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6919 | #ifdef AMD_EXTENSIONS |
| 6920 | case glslang::EOpSwizzleInvocations: |
| 6921 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6922 | libCall = spv::SwizzleInvocationsAMD; |
| 6923 | break; |
| 6924 | case glslang::EOpSwizzleInvocationsMasked: |
| 6925 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6926 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 6927 | break; |
| 6928 | case glslang::EOpWriteInvocation: |
| 6929 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6930 | libCall = spv::WriteInvocationAMD; |
| 6931 | break; |
| 6932 | |
| 6933 | case glslang::EOpMin3: |
| 6934 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6935 | if (isFloat) |
| 6936 | libCall = spv::FMin3AMD; |
| 6937 | else { |
| 6938 | if (isUnsigned) |
| 6939 | libCall = spv::UMin3AMD; |
| 6940 | else |
| 6941 | libCall = spv::SMin3AMD; |
| 6942 | } |
| 6943 | break; |
| 6944 | case glslang::EOpMax3: |
| 6945 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6946 | if (isFloat) |
| 6947 | libCall = spv::FMax3AMD; |
| 6948 | else { |
| 6949 | if (isUnsigned) |
| 6950 | libCall = spv::UMax3AMD; |
| 6951 | else |
| 6952 | libCall = spv::SMax3AMD; |
| 6953 | } |
| 6954 | break; |
| 6955 | case glslang::EOpMid3: |
| 6956 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6957 | if (isFloat) |
| 6958 | libCall = spv::FMid3AMD; |
| 6959 | else { |
| 6960 | if (isUnsigned) |
| 6961 | libCall = spv::UMid3AMD; |
| 6962 | else |
| 6963 | libCall = spv::SMid3AMD; |
| 6964 | } |
| 6965 | break; |
| 6966 | |
| 6967 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6968 | if (typeProxy == glslang::EbtFloat16) |
| 6969 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6970 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 6971 | libCall = spv::InterpolateAtVertexAMD; |
| 6972 | break; |
| 6973 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6974 | case glslang::EOpBarrier: |
| 6975 | { |
| 6976 | // This is for the extended controlBarrier function, with four operands. |
| 6977 | // The unextended barrier() goes through createNoArgOperation. |
| 6978 | assert(operands.size() == 4); |
| 6979 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 6980 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 6981 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 6982 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 6983 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 6984 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6985 | } |
| 6986 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) { |
| 6987 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6988 | } |
| 6989 | return 0; |
| 6990 | } |
| 6991 | break; |
| 6992 | case glslang::EOpMemoryBarrier: |
| 6993 | { |
| 6994 | // This is for the extended memoryBarrier function, with three operands. |
| 6995 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 6996 | assert(operands.size() == 3); |
| 6997 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 6998 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 6999 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 7000 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 7001 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7002 | } |
| 7003 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 7004 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7005 | } |
| 7006 | return 0; |
| 7007 | } |
| 7008 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7009 | |
| 7010 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7011 | case glslang::EOpReportIntersectionNV: |
| 7012 | { |
| 7013 | typeId = builder.makeBoolType(); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7014 | opCode = spv::OpReportIntersectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7015 | } |
| 7016 | break; |
| 7017 | case glslang::EOpTraceNV: |
| 7018 | { |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7019 | builder.createNoResultOp(spv::OpTraceNV, operands); |
| 7020 | return 0; |
| 7021 | } |
| 7022 | break; |
| 7023 | case glslang::EOpExecuteCallableNV: |
| 7024 | { |
| 7025 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7026 | return 0; |
| 7027 | } |
| 7028 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7029 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 7030 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 7031 | return 0; |
| 7032 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7033 | default: |
| 7034 | return 0; |
| 7035 | } |
| 7036 | |
| 7037 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7038 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 7039 | // Use an extended instruction from the standard library. |
| 7040 | // Construct the call arguments, without modifying the original operands vector. |
| 7041 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 7042 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7043 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 7044 | } else if (opCode == spv::OpDot && !isFloat) { |
| 7045 | // int dot(int, int) |
| 7046 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 7047 | const int componentCount = builder.getNumComponents(operands[0]); |
| 7048 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 7049 | builder.setPrecision(mulOp, precision); |
| 7050 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 7051 | for (int i = 1; i < componentCount; ++i) { |
| 7052 | builder.setPrecision(id, precision); |
| 7053 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(operands[0], typeId, i)); |
| 7054 | } |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7055 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7056 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7057 | case 0: |
| 7058 | // should all be handled by visitAggregate and createNoArgOperation |
| 7059 | assert(0); |
| 7060 | return 0; |
| 7061 | case 1: |
| 7062 | // should all be handled by createUnaryOperation |
| 7063 | assert(0); |
| 7064 | return 0; |
| 7065 | case 2: |
| 7066 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 7067 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7068 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7069 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 7070 | assert(consumedOperands == operands.size()); |
| 7071 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7072 | break; |
| 7073 | } |
| 7074 | } |
| 7075 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7076 | // Decode the return types that were structures |
| 7077 | switch (op) { |
| 7078 | case glslang::EOpAddCarry: |
| 7079 | case glslang::EOpSubBorrow: |
| 7080 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7081 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7082 | break; |
| 7083 | case glslang::EOpUMulExtended: |
| 7084 | case glslang::EOpIMulExtended: |
| 7085 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 7086 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7087 | break; |
| 7088 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7089 | { |
| 7090 | assert(operands.size() == 2); |
| 7091 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 7092 | // "exp" is floating-point type (from HLSL intrinsic) |
| 7093 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 7094 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 7095 | builder.createStore(member1, operands[1]); |
| 7096 | } else |
| 7097 | // "exp" is integer type (from GLSL built-in function) |
| 7098 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 7099 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7100 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7101 | break; |
| 7102 | default: |
| 7103 | break; |
| 7104 | } |
| 7105 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 7106 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7107 | } |
| 7108 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7109 | // Intrinsics with no arguments (or no return value, and no precision). |
| 7110 | 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] | 7111 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7112 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 7113 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7114 | |
| 7115 | switch (op) { |
| 7116 | case glslang::EOpEmitVertex: |
| 7117 | builder.createNoResultOp(spv::OpEmitVertex); |
| 7118 | return 0; |
| 7119 | case glslang::EOpEndPrimitive: |
| 7120 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 7121 | return 0; |
| 7122 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7123 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7124 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7125 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7126 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7127 | spv::MemorySemanticsAcquireReleaseMask); |
| 7128 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7129 | } else { |
| 7130 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 7131 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7132 | } else { |
| 7133 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7134 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7135 | spv::MemorySemanticsAcquireReleaseMask); |
| 7136 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7137 | return 0; |
| 7138 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7139 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 7140 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7141 | return 0; |
| 7142 | case glslang::EOpMemoryBarrierAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7143 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 7144 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7145 | return 0; |
| 7146 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7147 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 7148 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7149 | return 0; |
| 7150 | case glslang::EOpMemoryBarrierImage: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7151 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 7152 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7153 | return 0; |
| 7154 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7155 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7156 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7157 | return 0; |
| 7158 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7159 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 7160 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7161 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7162 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7163 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7164 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7165 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7166 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7167 | case glslang::EOpDeviceMemoryBarrier: |
| 7168 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7169 | spv::MemorySemanticsImageMemoryMask | |
| 7170 | spv::MemorySemanticsAcquireReleaseMask); |
| 7171 | return 0; |
| 7172 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 7173 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7174 | spv::MemorySemanticsImageMemoryMask | |
| 7175 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7176 | return 0; |
| 7177 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7178 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7179 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7180 | return 0; |
| 7181 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7182 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7183 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7184 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7185 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7186 | case glslang::EOpSubgroupBarrier: |
| 7187 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7188 | spv::MemorySemanticsAcquireReleaseMask); |
| 7189 | return spv::NoResult; |
| 7190 | case glslang::EOpSubgroupMemoryBarrier: |
| 7191 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7192 | spv::MemorySemanticsAcquireReleaseMask); |
| 7193 | return spv::NoResult; |
| 7194 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 7195 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 7196 | spv::MemorySemanticsAcquireReleaseMask); |
| 7197 | return spv::NoResult; |
| 7198 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 7199 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 7200 | spv::MemorySemanticsAcquireReleaseMask); |
| 7201 | return spv::NoResult; |
| 7202 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 7203 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7204 | spv::MemorySemanticsAcquireReleaseMask); |
| 7205 | return spv::NoResult; |
| 7206 | case glslang::EOpSubgroupElect: { |
| 7207 | std::vector<spv::Id> operands; |
| 7208 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 7209 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7210 | #ifdef AMD_EXTENSIONS |
| 7211 | case glslang::EOpTime: |
| 7212 | { |
| 7213 | std::vector<spv::Id> args; // Dummy arguments |
| 7214 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 7215 | return builder.setPrecision(id, precision); |
| 7216 | } |
| 7217 | #endif |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7218 | #ifdef NV_EXTENSIONS |
| 7219 | case glslang::EOpIgnoreIntersectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7220 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7221 | return 0; |
| 7222 | case glslang::EOpTerminateRayNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7223 | builder.createNoResultOp(spv::OpTerminateRayNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7224 | return 0; |
| 7225 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7226 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7227 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7228 | return 0; |
| 7229 | } |
| 7230 | } |
| 7231 | |
| 7232 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 7233 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 7234 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7235 | spv::Id id; |
| 7236 | if (symbolValues.end() != iter) { |
| 7237 | id = iter->second; |
| 7238 | return id; |
| 7239 | } |
| 7240 | |
| 7241 | // it was not found, create it |
| 7242 | id = createSpvVariable(symbol); |
| 7243 | symbolValues[symbol->getId()] = id; |
| 7244 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 7245 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7246 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 7247 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 7248 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7249 | #ifdef NV_EXTENSIONS |
| 7250 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
| 7251 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7252 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7253 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7254 | if (symbol->getQualifier().hasIndex()) |
| 7255 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 7256 | if (symbol->getQualifier().hasComponent()) |
| 7257 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 7258 | // atomic counters use this: |
| 7259 | if (symbol->getQualifier().hasOffset()) |
| 7260 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7261 | } |
| 7262 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 7263 | if (symbol->getQualifier().hasLocation()) |
| 7264 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7265 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 7266 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7267 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7268 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7269 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7270 | if (symbol->getQualifier().hasSet()) |
| 7271 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7272 | else if (IsDescriptorResource(symbol->getType())) { |
| 7273 | // default to 0 |
| 7274 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 7275 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7276 | if (symbol->getQualifier().hasBinding()) |
| 7277 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
Jeff Bolz | 0a93cfb | 2018-12-11 20:53:59 -0600 | [diff] [blame] | 7278 | else if (IsDescriptorResource(symbol->getType())) { |
| 7279 | // default to 0 |
| 7280 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 7281 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7282 | if (symbol->getQualifier().hasAttachment()) |
| 7283 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7284 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7285 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7286 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7287 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7288 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 7289 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 7290 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 7291 | } |
| 7292 | if (symbol->getQualifier().hasXfbOffset()) |
| 7293 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7294 | } |
| 7295 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7296 | if (symbol->getType().isImage()) { |
| 7297 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7298 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7299 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7300 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7301 | } |
| 7302 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7303 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 7304 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 7305 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7306 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7307 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 7308 | // nonuniform |
| 7309 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 7310 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 7311 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7312 | if (builtIn == spv::BuiltInSampleMask) { |
| 7313 | spv::Decoration decoration; |
| 7314 | // GL_NV_sample_mask_override_coverage extension |
| 7315 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7316 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7317 | else |
| 7318 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7319 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7320 | if (decoration != spv::DecorationMax) { |
| 7321 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 7322 | } |
| 7323 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7324 | else if (builtIn == spv::BuiltInLayer) { |
| 7325 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7326 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7327 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7328 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 7329 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 7330 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7331 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7332 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 7333 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7334 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 7335 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 7336 | } |
| 7337 | } |
| 7338 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7339 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7340 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7341 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7342 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 7343 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 7344 | if (symbol->getQualifier().pervertexNV) { |
| 7345 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 7346 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 7347 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 7348 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7349 | #endif |
| 7350 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7351 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 7352 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 7353 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 7354 | symbol->getType().getQualifier().semanticName); |
| 7355 | } |
| 7356 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7357 | if (symbol->getBasicType() == glslang::EbtReference) { |
| 7358 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 7359 | } |
| 7360 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7361 | return id; |
| 7362 | } |
| 7363 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7364 | #ifdef NV_EXTENSIONS |
| 7365 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 7366 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 7367 | { |
| 7368 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7369 | if (qualifier.perPrimitiveNV) { |
| 7370 | // Need to add capability/extension for fragment shader. |
| 7371 | // Mesh shader already adds this by default. |
| 7372 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7373 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7374 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7375 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7376 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7377 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7378 | if (qualifier.perViewNV) |
| 7379 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 7380 | if (qualifier.perTaskNV) |
| 7381 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 7382 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7383 | if (qualifier.perPrimitiveNV) { |
| 7384 | // Need to add capability/extension for fragment shader. |
| 7385 | // Mesh shader already adds this by default. |
| 7386 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7387 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7388 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7389 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7390 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7391 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7392 | if (qualifier.perViewNV) |
| 7393 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 7394 | if (qualifier.perTaskNV) |
| 7395 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 7396 | } |
| 7397 | } |
| 7398 | #endif |
| 7399 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7400 | // 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] | 7401 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7402 | // |
| 7403 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 7404 | // |
| 7405 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 7406 | // regular constants, which themselves are trees that createSpvConstant() |
| 7407 | // recursively walks. So, this function walks the "top" of the tree: |
| 7408 | // - emit specialization constant-building instructions for specConstant |
| 7409 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7410 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7411 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 7412 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7413 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7414 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7415 | if (! node.getQualifier().specConstant) { |
| 7416 | // hand off to the non-spec-constant path |
| 7417 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 7418 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7419 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7420 | nextConst, false); |
| 7421 | } |
| 7422 | |
| 7423 | // We now know we have a specialization constant to build |
| 7424 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 7425 | // 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] | 7426 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 7427 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 7428 | std::vector<spv::Id> dimConstId; |
| 7429 | for (int dim = 0; dim < 3; ++dim) { |
| 7430 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 7431 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7432 | if (specConst) { |
| 7433 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 7434 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 7435 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7436 | } |
| 7437 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 7438 | } |
| 7439 | |
| 7440 | // An AST node labelled as specialization constant should be a symbol node. |
| 7441 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 7442 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7443 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7444 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 7445 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 7446 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 7447 | // will set the builder into spec constant op instruction generating mode. |
| 7448 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7449 | result = accessChainLoad(sub_tree->getType()); |
| 7450 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7451 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7452 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7453 | } else { |
| 7454 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7455 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7456 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7457 | builder.addName(result, sn->getName().c_str()); |
| 7458 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7459 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7460 | |
| 7461 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 7462 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7463 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7464 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7465 | } |
| 7466 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7467 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 7468 | // build the aggregate SPIR-V constant. |
| 7469 | // |
| 7470 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 7471 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 7472 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7473 | 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] | 7474 | { |
| 7475 | // vector of constants for SPIR-V |
| 7476 | std::vector<spv::Id> spvConsts; |
| 7477 | |
| 7478 | // Type is used for struct and array constants |
| 7479 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 7480 | |
| 7481 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7482 | glslang::TType elementType(glslangType, 0); |
| 7483 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7484 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7485 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7486 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7487 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7488 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7489 | } else if (glslangType.isStruct()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7490 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 7491 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7492 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 7493 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7494 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 7495 | bool zero = nextConst >= consts.size(); |
| 7496 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7497 | case glslang::EbtInt8: |
| 7498 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 7499 | break; |
| 7500 | case glslang::EbtUint8: |
| 7501 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 7502 | break; |
| 7503 | case glslang::EbtInt16: |
| 7504 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 7505 | break; |
| 7506 | case glslang::EbtUint16: |
| 7507 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 7508 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7509 | case glslang::EbtInt: |
| 7510 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 7511 | break; |
| 7512 | case glslang::EbtUint: |
| 7513 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 7514 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7515 | case glslang::EbtInt64: |
| 7516 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 7517 | break; |
| 7518 | case glslang::EbtUint64: |
| 7519 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 7520 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7521 | case glslang::EbtFloat: |
| 7522 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7523 | break; |
| 7524 | case glslang::EbtDouble: |
| 7525 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 7526 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7527 | case glslang::EbtFloat16: |
| 7528 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7529 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7530 | case glslang::EbtBool: |
| 7531 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 7532 | break; |
| 7533 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7534 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7535 | break; |
| 7536 | } |
| 7537 | ++nextConst; |
| 7538 | } |
| 7539 | } else { |
| 7540 | // we have a non-aggregate (scalar) constant |
| 7541 | bool zero = nextConst >= consts.size(); |
| 7542 | spv::Id scalar = 0; |
| 7543 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7544 | case glslang::EbtInt8: |
| 7545 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 7546 | break; |
| 7547 | case glslang::EbtUint8: |
| 7548 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 7549 | break; |
| 7550 | case glslang::EbtInt16: |
| 7551 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 7552 | break; |
| 7553 | case glslang::EbtUint16: |
| 7554 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 7555 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7556 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7557 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7558 | break; |
| 7559 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7560 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7561 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7562 | case glslang::EbtInt64: |
| 7563 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 7564 | break; |
| 7565 | case glslang::EbtUint64: |
| 7566 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7567 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7568 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7569 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7570 | break; |
| 7571 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7572 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7573 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7574 | case glslang::EbtFloat16: |
| 7575 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 7576 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7577 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7578 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7579 | break; |
| 7580 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7581 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7582 | break; |
| 7583 | } |
| 7584 | ++nextConst; |
| 7585 | return scalar; |
| 7586 | } |
| 7587 | |
| 7588 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 7589 | } |
| 7590 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7591 | // Return true if the node is a constant or symbol whose reading has no |
| 7592 | // non-trivial observable cost or effect. |
| 7593 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 7594 | { |
| 7595 | // don't know what this is |
| 7596 | if (node == nullptr) |
| 7597 | return false; |
| 7598 | |
| 7599 | // a constant is safe |
| 7600 | if (node->getAsConstantUnion() != nullptr) |
| 7601 | return true; |
| 7602 | |
| 7603 | // not a symbol means non-trivial |
| 7604 | if (node->getAsSymbolNode() == nullptr) |
| 7605 | return false; |
| 7606 | |
| 7607 | // a symbol, depends on what's being read |
| 7608 | switch (node->getType().getQualifier().storage) { |
| 7609 | case glslang::EvqTemporary: |
| 7610 | case glslang::EvqGlobal: |
| 7611 | case glslang::EvqIn: |
| 7612 | case glslang::EvqInOut: |
| 7613 | case glslang::EvqConst: |
| 7614 | case glslang::EvqConstReadOnly: |
| 7615 | case glslang::EvqUniform: |
| 7616 | return true; |
| 7617 | default: |
| 7618 | return false; |
| 7619 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 7620 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7621 | |
| 7622 | // 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] | 7623 | // 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] | 7624 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7625 | // Return true if trivial. |
| 7626 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 7627 | { |
| 7628 | if (node == nullptr) |
| 7629 | return false; |
| 7630 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 7631 | // count non scalars as trivial, as well as anything coming from HLSL |
| 7632 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 7633 | return true; |
| 7634 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7635 | // symbols and constants are trivial |
| 7636 | if (isTrivialLeaf(node)) |
| 7637 | return true; |
| 7638 | |
| 7639 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 7640 | |
| 7641 | // not a simple operation |
| 7642 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 7643 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 7644 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 7645 | return false; |
| 7646 | |
| 7647 | // not on leaf nodes |
| 7648 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 7649 | return false; |
| 7650 | |
| 7651 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 7652 | return false; |
| 7653 | } |
| 7654 | |
| 7655 | switch (node->getAsOperator()->getOp()) { |
| 7656 | case glslang::EOpLogicalNot: |
| 7657 | case glslang::EOpConvIntToBool: |
| 7658 | case glslang::EOpConvUintToBool: |
| 7659 | case glslang::EOpConvFloatToBool: |
| 7660 | case glslang::EOpConvDoubleToBool: |
| 7661 | case glslang::EOpEqual: |
| 7662 | case glslang::EOpNotEqual: |
| 7663 | case glslang::EOpLessThan: |
| 7664 | case glslang::EOpGreaterThan: |
| 7665 | case glslang::EOpLessThanEqual: |
| 7666 | case glslang::EOpGreaterThanEqual: |
| 7667 | case glslang::EOpIndexDirect: |
| 7668 | case glslang::EOpIndexDirectStruct: |
| 7669 | case glslang::EOpLogicalXor: |
| 7670 | case glslang::EOpAny: |
| 7671 | case glslang::EOpAll: |
| 7672 | return true; |
| 7673 | default: |
| 7674 | return false; |
| 7675 | } |
| 7676 | } |
| 7677 | |
| 7678 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 7679 | // the left side is true (for &&) or false (for ||). |
| 7680 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 7681 | { |
| 7682 | spv::Id boolTypeId = builder.makeBoolType(); |
| 7683 | |
| 7684 | // emit left operand |
| 7685 | builder.clearAccessChain(); |
| 7686 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 7687 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7688 | |
| 7689 | // Operands to accumulate OpPhi operands |
| 7690 | std::vector<spv::Id> phiOperands; |
| 7691 | // accumulate left operand's phi information |
| 7692 | phiOperands.push_back(leftId); |
| 7693 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 7694 | |
| 7695 | // Make the two kinds of operation symmetric with a "!" |
| 7696 | // || => emit "if (! left) result = right" |
| 7697 | // && => emit "if ( left) result = right" |
| 7698 | // |
| 7699 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 7700 | // to 'builder' to have an "else" without an "then" |
| 7701 | if (op == glslang::EOpLogicalOr) |
| 7702 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 7703 | |
| 7704 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 7705 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7706 | |
| 7707 | // emit right operand as the "then" part of the "if" |
| 7708 | builder.clearAccessChain(); |
| 7709 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 7710 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7711 | |
| 7712 | // accumulate left operand's phi information |
| 7713 | phiOperands.push_back(rightId); |
| 7714 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 7715 | |
| 7716 | // finish the "if" |
| 7717 | ifBuilder.makeEndIf(); |
| 7718 | |
| 7719 | // phi together the two results |
| 7720 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 7721 | } |
| 7722 | |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 7723 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7724 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 7725 | // Import this set if it has not been imported yet. |
| 7726 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 7727 | { |
| 7728 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 7729 | return extBuiltinMap[name]; |
| 7730 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7731 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7732 | spv::Id extBuiltins = builder.import(name); |
| 7733 | extBuiltinMap[name] = extBuiltins; |
| 7734 | return extBuiltins; |
| 7735 | } |
| 7736 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 7737 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7738 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7739 | }; // end anonymous namespace |
| 7740 | |
| 7741 | namespace glslang { |
| 7742 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7743 | void GetSpirvVersion(std::string& version) |
| 7744 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 7745 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 7746 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7747 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7748 | version = buf; |
| 7749 | } |
| 7750 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 7751 | // For low-order part of the generator's magic number. Bump up |
| 7752 | // when there is a change in the style (e.g., if SSA form changes, |
| 7753 | // or a different instruction sequence to do something gets used). |
| 7754 | int GetSpirvGeneratorVersion() |
| 7755 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 7756 | // return 1; // start |
| 7757 | // 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] | 7758 | // 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] | 7759 | // 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] | 7760 | // 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] | 7761 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 7762 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
| 7763 | 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] | 7764 | } |
| 7765 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7766 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7767 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7768 | { |
| 7769 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7770 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 7771 | if (out.fail()) |
| 7772 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7773 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 7774 | unsigned int word = spirv[i]; |
| 7775 | out.write((const char*)&word, 4); |
| 7776 | } |
| 7777 | out.close(); |
| 7778 | } |
| 7779 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7780 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 7781 | 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] | 7782 | { |
| 7783 | std::ofstream out; |
| 7784 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 7785 | if (out.fail()) |
| 7786 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 7787 | out << "\t// " << |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7788 | GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL << |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 7789 | std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 7790 | if (varName != nullptr) { |
| 7791 | out << "\t #pragma once" << std::endl; |
| 7792 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 7793 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7794 | const int WORDS_PER_LINE = 8; |
| 7795 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 7796 | out << "\t"; |
| 7797 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 7798 | const unsigned int word = spirv[i + j]; |
| 7799 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 7800 | if (i + j + 1 < (int)spirv.size()) { |
| 7801 | out << ","; |
| 7802 | } |
| 7803 | } |
| 7804 | out << std::endl; |
| 7805 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 7806 | if (varName != nullptr) { |
| 7807 | out << "};"; |
| 7808 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7809 | out.close(); |
| 7810 | } |
| 7811 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7812 | // |
| 7813 | // Set up the glslang traversal |
| 7814 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7815 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7816 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7817 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7818 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 7819 | } |
| 7820 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7821 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7822 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 7823 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7824 | TIntermNode* root = intermediate.getTreeRoot(); |
| 7825 | |
| 7826 | if (root == 0) |
| 7827 | return; |
| 7828 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7829 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7830 | if (options == nullptr) |
| 7831 | options = &defaultOptions; |
| 7832 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7833 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7834 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 7835 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7836 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 7837 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7838 | it.dumpSpv(spirv); |
| 7839 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 7840 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 7841 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 7842 | // eg. forward and remove memory writes of opaque types. |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7843 | if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer) |
John Kessenich | e7df8e0 | 2018-08-22 17:12:46 -0600 | [diff] [blame] | 7844 | SpirvToolsLegalize(intermediate, spirv, logger, options); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7845 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7846 | if (options->validate) |
| 7847 | SpirvToolsValidate(intermediate, spirv, logger); |
| 7848 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7849 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7850 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7851 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 7852 | #endif |
| 7853 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7854 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7855 | } |
| 7856 | |
| 7857 | }; // end namespace glslang |