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. |
| 3 | // Copyright (C) 2015-2016 Google, Inc. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4 | // Copyright (C) 2017 ARM Limited. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 6 | // All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions |
| 10 | // are met: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 11 | // |
| 12 | // Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // |
| 15 | // Redistributions in binary form must reproduce the above |
| 16 | // copyright notice, this list of conditions and the following |
| 17 | // disclaimer in the documentation and/or other materials provided |
| 18 | // with the distribution. |
| 19 | // |
| 20 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 21 | // contributors may be used to endorse or promote products derived |
| 22 | // from this software without specific prior written permission. |
| 23 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 24 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 34 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 | // POSSIBILITY OF SUCH DAMAGE. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 36 | |
| 37 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 38 | // Visit the nodes in the glslang intermediate tree representation to |
| 39 | // translate them to SPIR-V. |
| 40 | // |
| 41 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 42 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 43 | #include "GlslangToSpv.h" |
| 44 | #include "SpvBuilder.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 45 | namespace spv { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 46 | #include "GLSL.std.450.h" |
| 47 | #include "GLSL.ext.KHR.h" |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 48 | #include "GLSL.ext.EXT.h" |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 49 | #ifdef AMD_EXTENSIONS |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 50 | #include "GLSL.ext.AMD.h" |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 51 | #endif |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 52 | #ifdef NV_EXTENSIONS |
| 53 | #include "GLSL.ext.NV.h" |
| 54 | #endif |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 55 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 56 | |
| 57 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 58 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 59 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 60 | #include "../glslang/Include/Common.h" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 61 | #include "../glslang/Include/revision.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 62 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 63 | #include <fstream> |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 64 | #include <iomanip> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 65 | #include <list> |
| 66 | #include <map> |
| 67 | #include <stack> |
| 68 | #include <string> |
| 69 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 70 | |
| 71 | namespace { |
| 72 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 73 | namespace { |
| 74 | class SpecConstantOpModeGuard { |
| 75 | public: |
| 76 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 77 | : builder_(builder) { |
| 78 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 79 | } |
| 80 | ~SpecConstantOpModeGuard() { |
| 81 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 82 | : builder_->setToNormalCodeGenMode(); |
| 83 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 84 | void turnOnSpecConstantOpMode() { |
| 85 | builder_->setToSpecConstCodeGenMode(); |
| 86 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | spv::Builder* builder_; |
| 90 | bool previous_flag_; |
| 91 | }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 92 | |
| 93 | struct OpDecorations { |
| 94 | spv::Decoration precision; |
| 95 | spv::Decoration noContraction; |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 96 | spv::Decoration nonUniform; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 100 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 101 | // |
| 102 | // The main holder of information for translating glslang to SPIR-V. |
| 103 | // |
| 104 | // Derives from the AST walking base class. |
| 105 | // |
| 106 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 107 | public: |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 108 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 109 | glslang::SpvOptions& options); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 110 | virtual ~TGlslangToSpvTraverser() { } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 111 | |
| 112 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 113 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 114 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 115 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 116 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 117 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 118 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 119 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 120 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 121 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 122 | void finishSpv(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 123 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 124 | |
| 125 | protected: |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 126 | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
| 127 | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
| 128 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 129 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 130 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 131 | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 132 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 133 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 134 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 135 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 136 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 137 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 138 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 139 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 140 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, unsigned int& dependencyLength) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 141 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 142 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 143 | spv::Id createSpvVariable(const glslang::TIntermSymbol*); |
| 144 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 145 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 146 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 147 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 148 | spv::Id convertGlslangToSpvType(const glslang::TType& type); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 149 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
| 150 | bool lastBufferBlockMember); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 151 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 152 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 153 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 154 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 155 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 156 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 157 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 158 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 159 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 160 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 161 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 162 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 163 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 164 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 165 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 166 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 167 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 168 | bool writableParam(glslang::TStorageQualifier) const; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 169 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 170 | void makeFunctions(const glslang::TIntermSequence&); |
| 171 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 172 | void visitFunctions(const glslang::TIntermSequence&); |
| 173 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 174 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 175 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 176 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 177 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 178 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 179 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 180 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 181 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 182 | spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 183 | glslang::TBasicType typeProxy); |
| 184 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 185 | glslang::TBasicType typeProxy); |
| 186 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 187 | glslang::TBasicType typeProxy); |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 188 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 189 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 190 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 191 | spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 192 | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 193 | spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 194 | spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 195 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 196 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 197 | #ifdef NV_EXTENSIONS |
| 198 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
| 199 | #endif |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 200 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 201 | spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, int& nextConst, bool specConstant); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 202 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 203 | bool isTrivial(const glslang::TIntermTyped* node); |
| 204 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 205 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 206 | spv::Id getExtBuiltins(const char* name); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 207 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 208 | void addPre13Extension(const char* ext) |
| 209 | { |
| 210 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 211 | builder.addExtension(ext); |
| 212 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 213 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 214 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 215 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 216 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 217 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 218 | int sequenceDepth; |
| 219 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 220 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 221 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 222 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 223 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 224 | bool inEntryPoint; |
| 225 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 226 | bool linkageOnly; // true when visiting the set of objects in the AST present only for establishing interface, whether or not they were statically used |
John Kessenich | 59420fd | 2015-12-21 11:45:34 -0700 | [diff] [blame] | 227 | std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 228 | const glslang::TIntermediate* glslangIntermediate; |
| 229 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 230 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 231 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 232 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 233 | std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, rather than a pointer |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 234 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 235 | std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount]; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 236 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 237 | std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 238 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 239 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | // |
| 243 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 244 | // |
| 245 | |
| 246 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 247 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 248 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 249 | switch (source) { |
| 250 | case glslang::EShSourceGlsl: |
| 251 | switch (profile) { |
| 252 | case ENoProfile: |
| 253 | case ECoreProfile: |
| 254 | case ECompatibilityProfile: |
| 255 | return spv::SourceLanguageGLSL; |
| 256 | case EEsProfile: |
| 257 | return spv::SourceLanguageESSL; |
| 258 | default: |
| 259 | return spv::SourceLanguageUnknown; |
| 260 | } |
| 261 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 262 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 263 | default: |
| 264 | return spv::SourceLanguageUnknown; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Translate glslang language (stage) to SPIR-V execution model. |
| 269 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 270 | { |
| 271 | switch (stage) { |
| 272 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 273 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 274 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 275 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 276 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 277 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 278 | #ifdef NV_EXTENSIONS |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 279 | case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV; |
| 280 | case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV; |
| 281 | case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV; |
| 282 | case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV; |
| 283 | case EShLangMissNV: return spv::ExecutionModelMissNV; |
| 284 | case EShLangCallableNV: return spv::ExecutionModelCallableNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 285 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 286 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 287 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 288 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 289 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 290 | return spv::ExecutionModelFragment; |
| 291 | } |
| 292 | } |
| 293 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 294 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 295 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 296 | { |
| 297 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 298 | case glslang::Esd1D: return spv::Dim1D; |
| 299 | case glslang::Esd2D: return spv::Dim2D; |
| 300 | case glslang::Esd3D: return spv::Dim3D; |
| 301 | case glslang::EsdCube: return spv::DimCube; |
| 302 | case glslang::EsdRect: return spv::DimRect; |
| 303 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 304 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 305 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 306 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 307 | return spv::Dim2D; |
| 308 | } |
| 309 | } |
| 310 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 311 | // Translate glslang precision to SPIR-V precision decorations. |
| 312 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 313 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 314 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 315 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 316 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 317 | default: |
| 318 | return spv::NoPrecision; |
| 319 | } |
| 320 | } |
| 321 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 322 | // Translate glslang type to SPIR-V precision decorations. |
| 323 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 324 | { |
| 325 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 326 | } |
| 327 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 328 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 329 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 330 | { |
| 331 | if (type.getBasicType() == glslang::EbtBlock) { |
| 332 | switch (type.getQualifier().storage) { |
| 333 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 334 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 335 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 336 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 337 | #ifdef NV_EXTENSIONS |
| 338 | case glslang::EvqPayloadNV: return spv::DecorationBlock; |
| 339 | case glslang::EvqPayloadInNV: return spv::DecorationBlock; |
| 340 | case glslang::EvqHitAttrNV: return spv::DecorationBlock; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 341 | case glslang::EvqCallableDataNV: return spv::DecorationBlock; |
| 342 | case glslang::EvqCallableDataInNV: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 343 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 344 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 345 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 350 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 351 | } |
| 352 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 353 | // Translate glslang type to SPIR-V memory decorations. |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 354 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, bool useVulkanMemoryModel) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 355 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 356 | if (!useVulkanMemoryModel) { |
| 357 | if (qualifier.coherent) |
| 358 | memory.push_back(spv::DecorationCoherent); |
| 359 | if (qualifier.volatil) { |
| 360 | memory.push_back(spv::DecorationVolatile); |
| 361 | memory.push_back(spv::DecorationCoherent); |
| 362 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 363 | } |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 364 | if (qualifier.restrict) |
| 365 | memory.push_back(spv::DecorationRestrict); |
| 366 | if (qualifier.readonly) |
| 367 | memory.push_back(spv::DecorationNonWritable); |
| 368 | if (qualifier.writeonly) |
| 369 | memory.push_back(spv::DecorationNonReadable); |
| 370 | } |
| 371 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 372 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 373 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 374 | { |
| 375 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 376 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 377 | case glslang::ElmRowMajor: |
| 378 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 379 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 380 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 381 | default: |
| 382 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 383 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 384 | } |
| 385 | } else { |
| 386 | switch (type.getBasicType()) { |
| 387 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 388 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 389 | break; |
| 390 | case glslang::EbtBlock: |
| 391 | switch (type.getQualifier().storage) { |
| 392 | case glslang::EvqUniform: |
| 393 | case glslang::EvqBuffer: |
| 394 | switch (type.getQualifier().layoutPacking) { |
| 395 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 396 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 397 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 398 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 399 | } |
| 400 | case glslang::EvqVaryingIn: |
| 401 | case glslang::EvqVaryingOut: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 402 | if (type.getQualifier().isTaskMemory()) { |
| 403 | switch (type.getQualifier().layoutPacking) { |
| 404 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 405 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 406 | default: break; |
| 407 | } |
| 408 | } else { |
| 409 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 410 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 411 | return spv::DecorationMax; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 412 | #ifdef NV_EXTENSIONS |
| 413 | case glslang::EvqPayloadNV: |
| 414 | case glslang::EvqPayloadInNV: |
| 415 | case glslang::EvqHitAttrNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 416 | case glslang::EvqCallableDataNV: |
| 417 | case glslang::EvqCallableDataInNV: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 418 | return spv::DecorationMax; |
| 419 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 420 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 421 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 422 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 429 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 430 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 431 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 432 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 433 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 434 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 435 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 436 | else if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 437 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 438 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 439 | return spv::DecorationFlat; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 440 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 441 | else if (qualifier.explicitInterp) { |
| 442 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 443 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 444 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 445 | #endif |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 446 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 447 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 451 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 452 | // should be applied. |
| 453 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 454 | { |
| 455 | if (qualifier.patch) |
| 456 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 457 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 458 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 459 | else if (qualifier.sample) { |
| 460 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 461 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 462 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 463 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 464 | } |
| 465 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 466 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 467 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 468 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 469 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 470 | return spv::DecorationInvariant; |
| 471 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 472 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 473 | } |
| 474 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 475 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 476 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 477 | { |
| 478 | if (qualifier.noContraction) |
| 479 | return spv::DecorationNoContraction; |
| 480 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 481 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 482 | } |
| 483 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 484 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 485 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 486 | { |
| 487 | if (qualifier.isNonUniform()) { |
| 488 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 489 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 490 | return spv::DecorationNonUniformEXT; |
| 491 | } else |
| 492 | return spv::DecorationMax; |
| 493 | } |
| 494 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 495 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 496 | { |
| 497 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) { |
| 498 | return spv::MemoryAccessMaskNone; |
| 499 | } |
| 500 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
| 501 | if (coherentFlags.volatil || |
| 502 | coherentFlags.coherent || |
| 503 | coherentFlags.devicecoherent || |
| 504 | coherentFlags.queuefamilycoherent || |
| 505 | coherentFlags.workgroupcoherent || |
| 506 | coherentFlags.subgroupcoherent) { |
| 507 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 508 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 509 | } |
| 510 | if (coherentFlags.nonprivate) { |
| 511 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 512 | } |
| 513 | if (coherentFlags.volatil) { |
| 514 | mask = mask | spv::MemoryAccessVolatileMask; |
| 515 | } |
| 516 | if (mask != spv::MemoryAccessMaskNone) { |
| 517 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 518 | } |
| 519 | return mask; |
| 520 | } |
| 521 | |
| 522 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 523 | { |
| 524 | if (!glslangIntermediate->usingVulkanMemoryModel()) { |
| 525 | return spv::ImageOperandsMaskNone; |
| 526 | } |
| 527 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 528 | if (coherentFlags.volatil || |
| 529 | coherentFlags.coherent || |
| 530 | coherentFlags.devicecoherent || |
| 531 | coherentFlags.queuefamilycoherent || |
| 532 | coherentFlags.workgroupcoherent || |
| 533 | coherentFlags.subgroupcoherent) { |
| 534 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 535 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 536 | } |
| 537 | if (coherentFlags.nonprivate) { |
| 538 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 539 | } |
| 540 | if (coherentFlags.volatil) { |
| 541 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 542 | } |
| 543 | if (mask != spv::ImageOperandsMaskNone) { |
| 544 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 545 | } |
| 546 | return mask; |
| 547 | } |
| 548 | |
| 549 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 550 | { |
| 551 | spv::Builder::AccessChain::CoherentFlags flags; |
| 552 | flags.coherent = type.getQualifier().coherent; |
| 553 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 554 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 555 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 556 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 557 | type.getQualifier().storage == glslang::EvqShared; |
| 558 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
| 559 | // *coherent variables are implicitly nonprivate in GLSL |
| 560 | flags.nonprivate = type.getQualifier().nonprivate || |
Jeff Bolz | ab3c965 | 2018-10-15 22:46:48 -0500 | [diff] [blame] | 561 | flags.subgroupcoherent || |
| 562 | flags.workgroupcoherent || |
| 563 | flags.queuefamilycoherent || |
| 564 | flags.devicecoherent || |
| 565 | flags.coherent; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 566 | flags.volatil = type.getQualifier().volatil; |
| 567 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
| 568 | return flags; |
| 569 | } |
| 570 | |
| 571 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 572 | { |
| 573 | spv::Scope scope; |
| 574 | if (coherentFlags.coherent) { |
| 575 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 576 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 577 | } else if (coherentFlags.devicecoherent) { |
| 578 | scope = spv::ScopeDevice; |
| 579 | } else if (coherentFlags.queuefamilycoherent) { |
| 580 | scope = spv::ScopeQueueFamilyKHR; |
| 581 | } else if (coherentFlags.workgroupcoherent) { |
| 582 | scope = spv::ScopeWorkgroup; |
| 583 | } else if (coherentFlags.subgroupcoherent) { |
| 584 | scope = spv::ScopeSubgroup; |
| 585 | } else { |
| 586 | scope = spv::ScopeMax; |
| 587 | } |
| 588 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 589 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 590 | } |
| 591 | return scope; |
| 592 | } |
| 593 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 594 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 595 | // associated capabilities when required. For some built-in variables, a capability |
| 596 | // is generated only when using the variable in an executable instruction, but not when |
| 597 | // just declaring a struct member variable with it. This is true for PointSize, |
| 598 | // ClipDistance, and CullDistance. |
| 599 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 600 | { |
| 601 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 602 | case glslang::EbvPointSize: |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 603 | // Defer adding the capability until the built-in is actually used. |
| 604 | if (! memberDeclaration) { |
| 605 | switch (glslangIntermediate->getStage()) { |
| 606 | case EShLangGeometry: |
| 607 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 608 | break; |
| 609 | case EShLangTessControl: |
| 610 | case EShLangTessEvaluation: |
| 611 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 612 | break; |
| 613 | default: |
| 614 | break; |
| 615 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 616 | } |
| 617 | return spv::BuiltInPointSize; |
| 618 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 619 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 620 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 621 | // They are now generated when used, rather than here when declared. |
| 622 | // Potentially, the specification should be more clear what the minimum |
| 623 | // use needed is to trigger the capability. |
| 624 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 625 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 626 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 627 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 628 | return spv::BuiltInClipDistance; |
| 629 | |
| 630 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 631 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 632 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 633 | return spv::BuiltInCullDistance; |
| 634 | |
| 635 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 636 | builder.addCapability(spv::CapabilityMultiViewport); |
| 637 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 638 | glslangIntermediate->getStage() == EShLangTessControl || |
| 639 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 640 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 641 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 642 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 643 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 644 | return spv::BuiltInViewportIndex; |
| 645 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 646 | case glslang::EbvSampleId: |
| 647 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 648 | return spv::BuiltInSampleId; |
| 649 | |
| 650 | case glslang::EbvSamplePosition: |
| 651 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 652 | return spv::BuiltInSamplePosition; |
| 653 | |
| 654 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 655 | return spv::BuiltInSampleMask; |
| 656 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 657 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 658 | #ifdef NV_EXTENSIONS |
| 659 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 660 | return spv::BuiltInLayer; |
| 661 | } |
| 662 | #endif |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 663 | builder.addCapability(spv::CapabilityGeometry); |
| 664 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 665 | glslangIntermediate->getStage() == EShLangTessControl || |
| 666 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 667 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 668 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 669 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 670 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 671 | return spv::BuiltInLayer; |
| 672 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 673 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 674 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 675 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 676 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 677 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 678 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 679 | case glslang::EbvBaseVertex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 680 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 681 | builder.addCapability(spv::CapabilityDrawParameters); |
| 682 | return spv::BuiltInBaseVertex; |
| 683 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 684 | case glslang::EbvBaseInstance: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 685 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 686 | builder.addCapability(spv::CapabilityDrawParameters); |
| 687 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 688 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 689 | case glslang::EbvDrawId: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 690 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 691 | builder.addCapability(spv::CapabilityDrawParameters); |
| 692 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 693 | |
| 694 | case glslang::EbvPrimitiveId: |
| 695 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 696 | builder.addCapability(spv::CapabilityGeometry); |
| 697 | return spv::BuiltInPrimitiveId; |
| 698 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 699 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 700 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 701 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 702 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 703 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 704 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 705 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 706 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 707 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 708 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 709 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 710 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 711 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 712 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 713 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 714 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 715 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 716 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 717 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 718 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 719 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 720 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 721 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 722 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 723 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 724 | return spv::BuiltInSubgroupSize; |
| 725 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 726 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 727 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 728 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 729 | return spv::BuiltInSubgroupLocalInvocationId; |
| 730 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 731 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 732 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 733 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 734 | return spv::BuiltInSubgroupEqMaskKHR; |
| 735 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 736 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 737 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 738 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 739 | return spv::BuiltInSubgroupGeMaskKHR; |
| 740 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 741 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 742 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 743 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 744 | return spv::BuiltInSubgroupGtMaskKHR; |
| 745 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 746 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 747 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 748 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 749 | return spv::BuiltInSubgroupLeMaskKHR; |
| 750 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 751 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 752 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 753 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 754 | return spv::BuiltInSubgroupLtMaskKHR; |
| 755 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 756 | case glslang::EbvNumSubgroups: |
| 757 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 758 | return spv::BuiltInNumSubgroups; |
| 759 | |
| 760 | case glslang::EbvSubgroupID: |
| 761 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 762 | return spv::BuiltInSubgroupId; |
| 763 | |
| 764 | case glslang::EbvSubgroupSize2: |
| 765 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 766 | return spv::BuiltInSubgroupSize; |
| 767 | |
| 768 | case glslang::EbvSubgroupInvocation2: |
| 769 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 770 | return spv::BuiltInSubgroupLocalInvocationId; |
| 771 | |
| 772 | case glslang::EbvSubgroupEqMask2: |
| 773 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 774 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 775 | return spv::BuiltInSubgroupEqMask; |
| 776 | |
| 777 | case glslang::EbvSubgroupGeMask2: |
| 778 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 779 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 780 | return spv::BuiltInSubgroupGeMask; |
| 781 | |
| 782 | case glslang::EbvSubgroupGtMask2: |
| 783 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 784 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 785 | return spv::BuiltInSubgroupGtMask; |
| 786 | |
| 787 | case glslang::EbvSubgroupLeMask2: |
| 788 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 789 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 790 | return spv::BuiltInSubgroupLeMask; |
| 791 | |
| 792 | case glslang::EbvSubgroupLtMask2: |
| 793 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 794 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 795 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 796 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 797 | case glslang::EbvBaryCoordNoPersp: |
| 798 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 799 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 800 | |
| 801 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 802 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 803 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 804 | |
| 805 | case glslang::EbvBaryCoordNoPerspSample: |
| 806 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 807 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 808 | |
| 809 | case glslang::EbvBaryCoordSmooth: |
| 810 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 811 | return spv::BuiltInBaryCoordSmoothAMD; |
| 812 | |
| 813 | case glslang::EbvBaryCoordSmoothCentroid: |
| 814 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 815 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 816 | |
| 817 | case glslang::EbvBaryCoordSmoothSample: |
| 818 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 819 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 820 | |
| 821 | case glslang::EbvBaryCoordPullModel: |
| 822 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 823 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 824 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 825 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 826 | case glslang::EbvDeviceIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 827 | addPre13Extension(spv::E_SPV_KHR_device_group); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 828 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 829 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 830 | |
| 831 | case glslang::EbvViewIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 832 | addPre13Extension(spv::E_SPV_KHR_multiview); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 833 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 834 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 835 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 836 | #ifdef NV_EXTENSIONS |
| 837 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 838 | if (!memberDeclaration) { |
| 839 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 840 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 841 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 842 | return spv::BuiltInViewportMaskNV; |
| 843 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 844 | if (!memberDeclaration) { |
| 845 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 846 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 847 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 848 | return spv::BuiltInSecondaryPositionNV; |
| 849 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 850 | if (!memberDeclaration) { |
| 851 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 852 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 853 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 854 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 855 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 856 | if (!memberDeclaration) { |
| 857 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 858 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 859 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 860 | return spv::BuiltInPositionPerViewNV; |
| 861 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 862 | if (!memberDeclaration) { |
| 863 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 864 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 865 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 866 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 867 | case glslang::EbvFragFullyCoveredNV: |
| 868 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 869 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 870 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 871 | case glslang::EbvFragmentSizeNV: |
| 872 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 873 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 874 | return spv::BuiltInFragmentSizeNV; |
| 875 | case glslang::EbvInvocationsPerPixelNV: |
| 876 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 877 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 878 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 879 | |
| 880 | // raytracing |
| 881 | case glslang::EbvLaunchIdNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 882 | return spv::BuiltInLaunchIdNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 883 | case glslang::EbvLaunchSizeNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 884 | return spv::BuiltInLaunchSizeNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 885 | case glslang::EbvWorldRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 886 | return spv::BuiltInWorldRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 887 | case glslang::EbvWorldRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 888 | return spv::BuiltInWorldRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 889 | case glslang::EbvObjectRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 890 | return spv::BuiltInObjectRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 891 | case glslang::EbvObjectRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 892 | return spv::BuiltInObjectRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 893 | case glslang::EbvRayTminNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 894 | return spv::BuiltInRayTminNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 895 | case glslang::EbvRayTmaxNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 896 | return spv::BuiltInRayTmaxNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 897 | case glslang::EbvInstanceCustomIndexNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 898 | return spv::BuiltInInstanceCustomIndexNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 899 | case glslang::EbvHitTNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 900 | return spv::BuiltInHitTNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 901 | case glslang::EbvHitKindNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 902 | return spv::BuiltInHitKindNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 903 | case glslang::EbvObjectToWorldNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 904 | return spv::BuiltInObjectToWorldNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 905 | case glslang::EbvWorldToObjectNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 906 | return spv::BuiltInWorldToObjectNV; |
| 907 | case glslang::EbvIncomingRayFlagsNV: |
| 908 | return spv::BuiltInIncomingRayFlagsNV; |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 909 | case glslang::EbvBaryCoordNV: |
| 910 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 911 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 912 | return spv::BuiltInBaryCoordNV; |
| 913 | case glslang::EbvBaryCoordNoPerspNV: |
| 914 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 915 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 916 | return spv::BuiltInBaryCoordNoPerspNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 917 | case glslang::EbvTaskCountNV: |
| 918 | return spv::BuiltInTaskCountNV; |
| 919 | case glslang::EbvPrimitiveCountNV: |
| 920 | return spv::BuiltInPrimitiveCountNV; |
| 921 | case glslang::EbvPrimitiveIndicesNV: |
| 922 | return spv::BuiltInPrimitiveIndicesNV; |
| 923 | case glslang::EbvClipDistancePerViewNV: |
| 924 | return spv::BuiltInClipDistancePerViewNV; |
| 925 | case glslang::EbvCullDistancePerViewNV: |
| 926 | return spv::BuiltInCullDistancePerViewNV; |
| 927 | case glslang::EbvLayerPerViewNV: |
| 928 | return spv::BuiltInLayerPerViewNV; |
| 929 | case glslang::EbvMeshViewCountNV: |
| 930 | return spv::BuiltInMeshViewCountNV; |
| 931 | case glslang::EbvMeshViewIndicesNV: |
| 932 | return spv::BuiltInMeshViewIndicesNV; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 933 | #endif |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 934 | default: |
| 935 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 939 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 940 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 941 | { |
| 942 | assert(type.getBasicType() == glslang::EbtSampler); |
| 943 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 944 | // Check for capabilities |
| 945 | switch (type.getQualifier().layoutFormat) { |
| 946 | case glslang::ElfRg32f: |
| 947 | case glslang::ElfRg16f: |
| 948 | case glslang::ElfR11fG11fB10f: |
| 949 | case glslang::ElfR16f: |
| 950 | case glslang::ElfRgba16: |
| 951 | case glslang::ElfRgb10A2: |
| 952 | case glslang::ElfRg16: |
| 953 | case glslang::ElfRg8: |
| 954 | case glslang::ElfR16: |
| 955 | case glslang::ElfR8: |
| 956 | case glslang::ElfRgba16Snorm: |
| 957 | case glslang::ElfRg16Snorm: |
| 958 | case glslang::ElfRg8Snorm: |
| 959 | case glslang::ElfR16Snorm: |
| 960 | case glslang::ElfR8Snorm: |
| 961 | |
| 962 | case glslang::ElfRg32i: |
| 963 | case glslang::ElfRg16i: |
| 964 | case glslang::ElfRg8i: |
| 965 | case glslang::ElfR16i: |
| 966 | case glslang::ElfR8i: |
| 967 | |
| 968 | case glslang::ElfRgb10a2ui: |
| 969 | case glslang::ElfRg32ui: |
| 970 | case glslang::ElfRg16ui: |
| 971 | case glslang::ElfRg8ui: |
| 972 | case glslang::ElfR16ui: |
| 973 | case glslang::ElfR8ui: |
| 974 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 975 | break; |
| 976 | |
| 977 | default: |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 982 | switch (type.getQualifier().layoutFormat) { |
| 983 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 984 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 985 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 986 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 987 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 988 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 989 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 990 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 991 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 992 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 993 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 994 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 995 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 996 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 997 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 998 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 999 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1000 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1001 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1002 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1003 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1004 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1005 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1006 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1007 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1008 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1009 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1010 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1011 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1012 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1013 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1014 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1015 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1016 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1017 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1018 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1019 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1020 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1021 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1022 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1023 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1027 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1028 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1029 | if (selectionNode.getFlatten()) |
| 1030 | return spv::SelectionControlFlattenMask; |
| 1031 | if (selectionNode.getDontFlatten()) |
| 1032 | return spv::SelectionControlDontFlattenMask; |
| 1033 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1034 | } |
| 1035 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1036 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1037 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1038 | if (switchNode.getFlatten()) |
| 1039 | return spv::SelectionControlFlattenMask; |
| 1040 | if (switchNode.getDontFlatten()) |
| 1041 | return spv::SelectionControlDontFlattenMask; |
| 1042 | return spv::SelectionControlMaskNone; |
| 1043 | } |
| 1044 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1045 | // return a non-0 dependency if the dependency argument must be set |
| 1046 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
| 1047 | unsigned int& dependencyLength) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1048 | { |
| 1049 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1050 | |
| 1051 | if (loopNode.getDontUnroll()) |
| 1052 | control = control | spv::LoopControlDontUnrollMask; |
| 1053 | if (loopNode.getUnroll()) |
| 1054 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1055 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1056 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1057 | else if (loopNode.getLoopDependency() > 0) { |
| 1058 | control = control | spv::LoopControlDependencyLengthMask; |
| 1059 | dependencyLength = loopNode.getLoopDependency(); |
| 1060 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1061 | |
| 1062 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1063 | } |
| 1064 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1065 | // Translate glslang type to SPIR-V storage class. |
| 1066 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1067 | { |
| 1068 | if (type.getQualifier().isPipeInput()) |
| 1069 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1070 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1071 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1072 | |
| 1073 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
| 1074 | type.getQualifier().storage == glslang::EvqUniform) { |
| 1075 | if (type.getBasicType() == glslang::EbtAtomicUint) |
| 1076 | return spv::StorageClassAtomicCounter; |
| 1077 | if (type.containsOpaque()) |
| 1078 | return spv::StorageClassUniformConstant; |
| 1079 | } |
| 1080 | |
| 1081 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1082 | addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class); |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1083 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1087 | if (type.getQualifier().layoutPushConstant) |
| 1088 | return spv::StorageClassPushConstant; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1089 | #ifdef NV_EXTENSIONS |
| 1090 | if (type.getQualifier().layoutShaderRecordNV) |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1091 | return spv::StorageClassShaderRecordBufferNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1092 | #endif |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1093 | if (type.getBasicType() == glslang::EbtBlock) |
| 1094 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1095 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1096 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1097 | |
| 1098 | switch (type.getQualifier().storage) { |
| 1099 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
| 1100 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1101 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1102 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1103 | #ifdef NV_EXTENSIONS |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1104 | case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV; |
| 1105 | case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV; |
| 1106 | case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV; |
| 1107 | case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV; |
| 1108 | case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1109 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1110 | default: |
| 1111 | assert(0); |
| 1112 | break; |
| 1113 | } |
| 1114 | |
| 1115 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1116 | } |
| 1117 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1118 | // Add capabilities pertaining to how an array is indexed. |
| 1119 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1120 | const glslang::TType& indexType) |
| 1121 | { |
| 1122 | if (indexType.getQualifier().isNonUniform()) { |
| 1123 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1124 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1125 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1126 | if (baseType.getQualifier().hasAttachment()) |
| 1127 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
| 1128 | else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1129 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
| 1130 | else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1131 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1132 | else if (baseType.isImage()) |
| 1133 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1134 | else if (baseType.isTexture()) |
| 1135 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1136 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1137 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1138 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1139 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1140 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1141 | } |
| 1142 | } else { |
| 1143 | // assume a dynamically uniform index |
| 1144 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1145 | if (baseType.getQualifier().hasAttachment()) { |
| 1146 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1147 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1148 | } else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1149 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1150 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1151 | } else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1152 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1153 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1154 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1159 | // 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] | 1160 | // descriptor set. |
| 1161 | bool IsDescriptorResource(const glslang::TType& type) |
| 1162 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1163 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1164 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1165 | return type.getQualifier().isUniformOrBuffer() && |
| 1166 | #ifdef NV_EXTENSIONS |
| 1167 | ! type.getQualifier().layoutShaderRecordNV && |
| 1168 | #endif |
| 1169 | ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1170 | |
| 1171 | // non block... |
| 1172 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1173 | // if they are the global-scope-class, not the function parameter |
| 1174 | // (or local, if they ever exist) class. |
| 1175 | if (type.getBasicType() == glslang::EbtSampler) |
| 1176 | return type.getQualifier().isUniformOrBuffer(); |
| 1177 | |
| 1178 | // None of the above. |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1182 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1183 | { |
| 1184 | if (child.layoutMatrix == glslang::ElmNone) |
| 1185 | child.layoutMatrix = parent.layoutMatrix; |
| 1186 | |
| 1187 | if (parent.invariant) |
| 1188 | child.invariant = true; |
| 1189 | if (parent.nopersp) |
| 1190 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1191 | #ifdef AMD_EXTENSIONS |
| 1192 | if (parent.explicitInterp) |
| 1193 | child.explicitInterp = true; |
| 1194 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1195 | if (parent.flat) |
| 1196 | child.flat = true; |
| 1197 | if (parent.centroid) |
| 1198 | child.centroid = true; |
| 1199 | if (parent.patch) |
| 1200 | child.patch = true; |
| 1201 | if (parent.sample) |
| 1202 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1203 | if (parent.coherent) |
| 1204 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1205 | if (parent.devicecoherent) |
| 1206 | child.devicecoherent = true; |
| 1207 | if (parent.queuefamilycoherent) |
| 1208 | child.queuefamilycoherent = true; |
| 1209 | if (parent.workgroupcoherent) |
| 1210 | child.workgroupcoherent = true; |
| 1211 | if (parent.subgroupcoherent) |
| 1212 | child.subgroupcoherent = true; |
| 1213 | if (parent.nonprivate) |
| 1214 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1215 | if (parent.volatil) |
| 1216 | child.volatil = true; |
| 1217 | if (parent.restrict) |
| 1218 | child.restrict = true; |
| 1219 | if (parent.readonly) |
| 1220 | child.readonly = true; |
| 1221 | if (parent.writeonly) |
| 1222 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1223 | #ifdef NV_EXTENSIONS |
| 1224 | if (parent.perPrimitiveNV) |
| 1225 | child.perPrimitiveNV = true; |
| 1226 | if (parent.perViewNV) |
| 1227 | child.perViewNV = true; |
| 1228 | if (parent.perTaskNV) |
| 1229 | child.perTaskNV = true; |
| 1230 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1233 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1234 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1235 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1236 | // - struct members might inherit from a struct declaration |
| 1237 | // (note that non-block structs don't explicitly inherit, |
| 1238 | // only implicitly, meaning no decoration involved) |
| 1239 | // - affect decorations on the struct members |
| 1240 | // (note smooth does not, and expecting something like volatile |
| 1241 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1242 | // - 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] | 1243 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1246 | // |
| 1247 | // Implement the TGlslangToSpvTraverser class. |
| 1248 | // |
| 1249 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1250 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1251 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 1252 | : TIntermTraverser(true, false, true), |
| 1253 | options(options), |
| 1254 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1255 | sequenceDepth(0), logger(buildLogger), |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1256 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1257 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1258 | glslangIntermediate(glslangIntermediate) |
| 1259 | { |
| 1260 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1261 | |
| 1262 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1263 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1264 | glslangIntermediate->getVersion()); |
| 1265 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1266 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1267 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1268 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1269 | |
| 1270 | // Set the source shader's text. If for SPV version 1.0, include |
| 1271 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1272 | // Otherwise, emit those as actual instructions. |
| 1273 | std::string text; |
| 1274 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1275 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1276 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1277 | text.append("// OpModuleProcessed "); |
| 1278 | text.append(processes[p]); |
| 1279 | text.append("\n"); |
| 1280 | } else |
| 1281 | builder.addModuleProcessed(processes[p]); |
| 1282 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1283 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1284 | text.append("#line 1\n"); |
| 1285 | text.append(glslangIntermediate->getSourceText()); |
| 1286 | builder.setSourceText(text); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1287 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1288 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1289 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 1290 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelVulkanKHR); |
| 1291 | builder.addExtension(spv::E_SPV_KHR_vulkan_memory_model); |
| 1292 | } else { |
| 1293 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); |
| 1294 | } |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1295 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1296 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1297 | |
| 1298 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1299 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1300 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1301 | builder.addSourceExtension(it->c_str()); |
| 1302 | |
| 1303 | // Add the top-level modes for this shader. |
| 1304 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1305 | if (glslangIntermediate->getXfbMode()) { |
| 1306 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1307 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1308 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1309 | |
| 1310 | unsigned int mode; |
| 1311 | switch (glslangIntermediate->getStage()) { |
| 1312 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1313 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1314 | break; |
| 1315 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1316 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1317 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1318 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1319 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1320 | glslang::TLayoutGeometry primitive; |
| 1321 | |
| 1322 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1323 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1324 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1325 | } else { |
| 1326 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1327 | } |
| 1328 | |
| 1329 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1330 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1331 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1332 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1333 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1334 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1335 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1336 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1337 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1338 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1339 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1340 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1341 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1342 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1343 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1344 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1345 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1346 | |
| 1347 | switch (glslangIntermediate->getVertexOrder()) { |
| 1348 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1349 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1350 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1351 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1352 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1353 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1354 | |
| 1355 | if (glslangIntermediate->getPointMode()) |
| 1356 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1357 | break; |
| 1358 | |
| 1359 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1360 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1361 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1362 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1363 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1364 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1365 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1366 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1367 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1368 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1369 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1370 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1371 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1372 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1373 | |
| 1374 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1375 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1376 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1377 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1378 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1379 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1380 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1381 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1382 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1383 | break; |
| 1384 | |
| 1385 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1386 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1387 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1388 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1389 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1390 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1391 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1392 | else |
| 1393 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1394 | |
| 1395 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1396 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1397 | |
chaoc | c120452 | 2017-06-30 17:14:30 -0700 | [diff] [blame] | 1398 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1399 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1400 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1401 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1402 | } |
| 1403 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1404 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1405 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1406 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1407 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1408 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1409 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1410 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1411 | |
| 1412 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1413 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1414 | break; |
| 1415 | |
| 1416 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1417 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 1418 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1419 | glslangIntermediate->getLocalSize(1), |
| 1420 | glslangIntermediate->getLocalSize(2)); |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 1421 | #ifdef NV_EXTENSIONS |
| 1422 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1423 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1424 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1425 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1426 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1427 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1428 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1429 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1430 | } |
| 1431 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1432 | break; |
| 1433 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1434 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1435 | case EShLangRayGenNV: |
| 1436 | case EShLangIntersectNV: |
| 1437 | case EShLangAnyHitNV: |
| 1438 | case EShLangClosestHitNV: |
| 1439 | case EShLangMissNV: |
| 1440 | case EShLangCallableNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1441 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1442 | builder.addExtension("SPV_NV_ray_tracing"); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1443 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1444 | case EShLangTaskNV: |
| 1445 | case EShLangMeshNV: |
| 1446 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1447 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1448 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1449 | glslangIntermediate->getLocalSize(1), |
| 1450 | glslangIntermediate->getLocalSize(2)); |
| 1451 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 1452 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1453 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives()); |
| 1454 | |
| 1455 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1456 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1457 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1458 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1459 | default: mode = spv::ExecutionModeMax; break; |
| 1460 | } |
| 1461 | if (mode != spv::ExecutionModeMax) |
| 1462 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1463 | } |
| 1464 | break; |
| 1465 | #endif |
| 1466 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1467 | default: |
| 1468 | break; |
| 1469 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1470 | } |
| 1471 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1472 | // Finish creating SPV, after the traversal is complete. |
| 1473 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1474 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1475 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1476 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1477 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1478 | builder.leaveFunction(); |
| 1479 | } |
| 1480 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1481 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1482 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1483 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1484 | |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1485 | // Add capabilities, extensions, remove unneeded decorations, etc., |
| 1486 | // based on the resulting SPIR-V. |
| 1487 | builder.postProcess(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1490 | // Write the SPV into 'out'. |
| 1491 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1492 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1493 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | // |
| 1497 | // Implement the traversal functions. |
| 1498 | // |
| 1499 | // Return true from interior nodes to have the external traversal |
| 1500 | // continue on to children. Return false if children were |
| 1501 | // already processed. |
| 1502 | // |
| 1503 | |
| 1504 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1505 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1506 | // - uniform/input reads |
| 1507 | // - output writes |
| 1508 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1509 | // - something simple that degenerates into the last bullet |
| 1510 | // |
| 1511 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1512 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1513 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1514 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1515 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1516 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1517 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1518 | // Formal function parameters were mapped during makeFunctions(). |
| 1519 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1520 | |
| 1521 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1522 | if (builder.isPointer(id)) { |
| 1523 | spv::StorageClass sc = builder.getStorageClass(id); |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1524 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) { |
| 1525 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) |
| 1526 | iOSet.insert(id); |
| 1527 | } |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1531 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1532 | // Prepare to generate code for the access |
| 1533 | |
| 1534 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1535 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1536 | // followed by setting the base. |
| 1537 | builder.clearAccessChain(); |
| 1538 | |
| 1539 | // 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] | 1540 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1541 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1542 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1543 | // 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] | 1544 | // These are also pure R-values. |
| 1545 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1546 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1547 | builder.setAccessChainRValue(id); |
| 1548 | else |
| 1549 | builder.setAccessChainLValue(id); |
| 1550 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1551 | |
| 1552 | // Process linkage-only nodes for any special additional interface work. |
| 1553 | if (linkageOnly) { |
| 1554 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1555 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1556 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1557 | // of declaration. |
| 1558 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1559 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1560 | // Save possible originating buffers for counter buffers, keyed by |
| 1561 | // making the potential counter-buffer name. |
| 1562 | std::string keyName = symbol->getName().c_str(); |
| 1563 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1564 | counterOriginator[keyName] = symbol; |
| 1565 | } else { |
| 1566 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1567 | std::string keyName = symbol->getName().c_str(); |
| 1568 | auto it = counterOriginator.find(keyName); |
| 1569 | if (it != counterOriginator.end()) { |
| 1570 | id = getSymbolId(it->second); |
| 1571 | if (id != spv::NoResult) { |
| 1572 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1573 | if (counterId != spv::NoResult) { |
| 1574 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1575 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1576 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1586 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1587 | builder.setLine(node->getLoc().line); |
| 1588 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1589 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1590 | if (node->getType().getQualifier().isSpecConstant()) |
| 1591 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1592 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1593 | // First, handle special cases |
| 1594 | switch (node->getOp()) { |
| 1595 | case glslang::EOpAssign: |
| 1596 | case glslang::EOpAddAssign: |
| 1597 | case glslang::EOpSubAssign: |
| 1598 | case glslang::EOpMulAssign: |
| 1599 | case glslang::EOpVectorTimesMatrixAssign: |
| 1600 | case glslang::EOpVectorTimesScalarAssign: |
| 1601 | case glslang::EOpMatrixTimesScalarAssign: |
| 1602 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1603 | case glslang::EOpDivAssign: |
| 1604 | case glslang::EOpModAssign: |
| 1605 | case glslang::EOpAndAssign: |
| 1606 | case glslang::EOpInclusiveOrAssign: |
| 1607 | case glslang::EOpExclusiveOrAssign: |
| 1608 | case glslang::EOpLeftShiftAssign: |
| 1609 | case glslang::EOpRightShiftAssign: |
| 1610 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1611 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1612 | // says to evaluate the left before the right. So, always, left |
| 1613 | // node then right node. |
| 1614 | { |
| 1615 | // get the left l-value, save it away |
| 1616 | builder.clearAccessChain(); |
| 1617 | node->getLeft()->traverse(this); |
| 1618 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1619 | |
| 1620 | // evaluate the right |
| 1621 | builder.clearAccessChain(); |
| 1622 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1623 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1624 | |
| 1625 | if (node->getOp() != glslang::EOpAssign) { |
| 1626 | // the left is also an r-value |
| 1627 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1628 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1629 | |
| 1630 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1631 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1632 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1633 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1634 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1635 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1636 | node->getType().getBasicType()); |
| 1637 | |
| 1638 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1639 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | // store the result |
| 1643 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1644 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1645 | |
| 1646 | // assignments are expressions having an rValue after they are evaluated... |
| 1647 | builder.clearAccessChain(); |
| 1648 | builder.setAccessChainRValue(rValue); |
| 1649 | } |
| 1650 | return false; |
| 1651 | case glslang::EOpIndexDirect: |
| 1652 | case glslang::EOpIndexDirectStruct: |
| 1653 | { |
| 1654 | // Get the left part of the access chain. |
| 1655 | node->getLeft()->traverse(this); |
| 1656 | |
| 1657 | // Add the next element in the chain |
| 1658 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1659 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1660 | if (! node->getLeft()->getType().isArray() && |
| 1661 | node->getLeft()->getType().isVector() && |
| 1662 | node->getOp() == glslang::EOpIndexDirect) { |
| 1663 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1664 | // so short circuit the access-chain stuff with a swizzle. |
| 1665 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1666 | swizzle.push_back(glslangIndex); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1667 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1668 | } else { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1669 | int spvIndex = glslangIndex; |
| 1670 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1671 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1672 | { |
| 1673 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1674 | // index remapping due to hidden members in anonymous blocks. |
| 1675 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1676 | assert(remapper.size() > 0); |
| 1677 | spvIndex = remapper[glslangIndex]; |
| 1678 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1679 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1680 | // normal case for indexing array or structure or block |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1681 | builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType())); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1682 | |
| 1683 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1684 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1685 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1686 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | return false; |
| 1690 | case glslang::EOpIndexIndirect: |
| 1691 | { |
| 1692 | // Structure or array or vector indirection. |
| 1693 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 1694 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1695 | // Will use the access chain's 'component' for variable index into a vector. |
| 1696 | |
| 1697 | // This adapter is building access chains left to right. |
| 1698 | // Set up the access chain to the left. |
| 1699 | node->getLeft()->traverse(this); |
| 1700 | |
| 1701 | // save it so that computing the right side doesn't trash it |
| 1702 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1703 | |
| 1704 | // compute the next index in the chain |
| 1705 | builder.clearAccessChain(); |
| 1706 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1707 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1708 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1709 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1710 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1711 | // restore the saved access chain |
| 1712 | builder.setAccessChain(partial); |
| 1713 | |
| 1714 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1715 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1716 | else |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1717 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1718 | } |
| 1719 | return false; |
| 1720 | case glslang::EOpVectorSwizzle: |
| 1721 | { |
| 1722 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1723 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1724 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1725 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1726 | } |
| 1727 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1728 | case glslang::EOpMatrixSwizzle: |
| 1729 | logger->missingFunctionality("matrix swizzle"); |
| 1730 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1731 | case glslang::EOpLogicalOr: |
| 1732 | case glslang::EOpLogicalAnd: |
| 1733 | { |
| 1734 | |
| 1735 | // These may require short circuiting, but can sometimes be done as straight |
| 1736 | // binary operations. The right operand must be short circuited if it has |
| 1737 | // side effects, and should probably be if it is complex. |
| 1738 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1739 | break; // handle below as a normal binary operation |
| 1740 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1741 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1742 | builder.clearAccessChain(); |
| 1743 | builder.setAccessChainRValue(result); |
| 1744 | } |
| 1745 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1746 | default: |
| 1747 | break; |
| 1748 | } |
| 1749 | |
| 1750 | // Assume generic binary op... |
| 1751 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1752 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1753 | builder.clearAccessChain(); |
| 1754 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1755 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1756 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1757 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1758 | builder.clearAccessChain(); |
| 1759 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1760 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1761 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1762 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1763 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1764 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1765 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1766 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1767 | convertGlslangToSpvType(node->getType()), left, right, |
| 1768 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1769 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1770 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1771 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1772 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1773 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1774 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1775 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1776 | return false; |
| 1777 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1781 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1782 | builder.setLine(node->getLoc().line); |
| 1783 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1784 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1785 | if (node->getType().getQualifier().isSpecConstant()) |
| 1786 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1787 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1788 | spv::Id result = spv::NoResult; |
| 1789 | |
| 1790 | // try texturing first |
| 1791 | result = createImageTextureFunctionCall(node); |
| 1792 | if (result != spv::NoResult) { |
| 1793 | builder.clearAccessChain(); |
| 1794 | builder.setAccessChainRValue(result); |
| 1795 | |
| 1796 | return false; // done with this node |
| 1797 | } |
| 1798 | |
| 1799 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1800 | |
| 1801 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1802 | // Quite special; won't want to evaluate the operand. |
| 1803 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1804 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 1805 | // except for the last block membeor of an SSBO. |
| 1806 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 1807 | // size extracted. |
| 1808 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1809 | // Normal .length() would have been constant folded by the front-end. |
| 1810 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1811 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1812 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1813 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1814 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1815 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1816 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1817 | |
| 1818 | builder.clearAccessChain(); |
| 1819 | builder.setAccessChainRValue(length); |
| 1820 | |
| 1821 | return false; |
| 1822 | } |
| 1823 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1824 | // Start by evaluating the operand |
| 1825 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1826 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1827 | // operate first on the swizzle base, then apply the swizzle. |
| 1828 | spv::Id invertedType = spv::NoType; |
| 1829 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 1830 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 1831 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 1832 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1833 | builder.clearAccessChain(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1834 | if (invertedType != spv::NoType) |
| 1835 | node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this); |
| 1836 | else |
| 1837 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1838 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1839 | spv::Id operand = spv::NoResult; |
| 1840 | |
| 1841 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1842 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1843 | node->getOp() == glslang::EOpAtomicCounter || |
| 1844 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1845 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1846 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1847 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1848 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1849 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1850 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1851 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1852 | |
| 1853 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1854 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1855 | result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1856 | |
| 1857 | // if not, then possibly an operation |
| 1858 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1859 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1860 | |
| 1861 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1862 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1863 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1864 | builder.addDecoration(result, decorations.nonUniform); |
| 1865 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1866 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1867 | builder.clearAccessChain(); |
| 1868 | builder.setAccessChainRValue(result); |
| 1869 | |
| 1870 | return false; // done with this node |
| 1871 | } |
| 1872 | |
| 1873 | // it must be a special case, check... |
| 1874 | switch (node->getOp()) { |
| 1875 | case glslang::EOpPostIncrement: |
| 1876 | case glslang::EOpPostDecrement: |
| 1877 | case glslang::EOpPreIncrement: |
| 1878 | case glslang::EOpPreDecrement: |
| 1879 | { |
| 1880 | // 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] | 1881 | spv::Id one = 0; |
| 1882 | if (node->getBasicType() == glslang::EbtFloat) |
| 1883 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 1884 | else if (node->getBasicType() == glslang::EbtDouble) |
| 1885 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1886 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 1887 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1888 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 1889 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1890 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 1891 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1892 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1893 | one = builder.makeInt64Constant(1); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1894 | else |
| 1895 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1896 | glslang::TOperator op; |
| 1897 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1898 | node->getOp() == glslang::EOpPostIncrement) |
| 1899 | op = glslang::EOpAdd; |
| 1900 | else |
| 1901 | op = glslang::EOpSub; |
| 1902 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1903 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1904 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1905 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1906 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1907 | |
| 1908 | // The result of operation is always stored, but conditionally the |
| 1909 | // consumed result. The consumed result is always an r-value. |
| 1910 | builder.accessChainStore(result); |
| 1911 | builder.clearAccessChain(); |
| 1912 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1913 | node->getOp() == glslang::EOpPreDecrement) |
| 1914 | builder.setAccessChainRValue(result); |
| 1915 | else |
| 1916 | builder.setAccessChainRValue(operand); |
| 1917 | } |
| 1918 | |
| 1919 | return false; |
| 1920 | |
| 1921 | case glslang::EOpEmitStreamVertex: |
| 1922 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1923 | return false; |
| 1924 | case glslang::EOpEndStreamPrimitive: |
| 1925 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1926 | return false; |
| 1927 | |
| 1928 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1929 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1930 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1931 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 1935 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 1936 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1937 | if (node->getType().getQualifier().isSpecConstant()) |
| 1938 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1939 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1940 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1941 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 1942 | 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] | 1943 | |
| 1944 | // try texturing |
| 1945 | result = createImageTextureFunctionCall(node); |
| 1946 | if (result != spv::NoResult) { |
| 1947 | builder.clearAccessChain(); |
| 1948 | builder.setAccessChainRValue(result); |
| 1949 | |
| 1950 | return false; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1951 | } else if (node->getOp() == glslang::EOpImageStore || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1952 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1953 | node->getOp() == glslang::EOpImageStoreLod || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1954 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1955 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1956 | // "imageStore" is a special case, which has no result |
| 1957 | return false; |
| 1958 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1959 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1960 | glslang::TOperator binOp = glslang::EOpNull; |
| 1961 | bool reduceComparison = true; |
| 1962 | bool isMatrix = false; |
| 1963 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1964 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1965 | |
| 1966 | assert(node->getOp()); |
| 1967 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1968 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1969 | |
| 1970 | switch (node->getOp()) { |
| 1971 | case glslang::EOpSequence: |
| 1972 | { |
| 1973 | if (preVisit) |
| 1974 | ++sequenceDepth; |
| 1975 | else |
| 1976 | --sequenceDepth; |
| 1977 | |
| 1978 | if (sequenceDepth == 1) { |
| 1979 | // If this is the parent node of all the functions, we want to see them |
| 1980 | // early, so all call points have actual SPIR-V functions to reference. |
| 1981 | // In all cases, still let the traverser visit the children for us. |
| 1982 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 1983 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1984 | // 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] | 1985 | // anything else gets there, so visit out of order, doing them all now. |
| 1986 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 1987 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 1988 | // 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] | 1989 | // so do them manually. |
| 1990 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 1991 | |
| 1992 | return false; |
| 1993 | } |
| 1994 | |
| 1995 | return true; |
| 1996 | } |
| 1997 | case glslang::EOpLinkerObjects: |
| 1998 | { |
| 1999 | if (visit == glslang::EvPreVisit) |
| 2000 | linkageOnly = true; |
| 2001 | else |
| 2002 | linkageOnly = false; |
| 2003 | |
| 2004 | return true; |
| 2005 | } |
| 2006 | case glslang::EOpComma: |
| 2007 | { |
| 2008 | // processing from left to right naturally leaves the right-most |
| 2009 | // lying around in the access chain |
| 2010 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2011 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2012 | glslangOperands[i]->traverse(this); |
| 2013 | |
| 2014 | return false; |
| 2015 | } |
| 2016 | case glslang::EOpFunction: |
| 2017 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2018 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2019 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2020 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2021 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2022 | } else { |
| 2023 | handleFunctionEntry(node); |
| 2024 | } |
| 2025 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2026 | if (inEntryPoint) |
| 2027 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2028 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2029 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | return true; |
| 2033 | case glslang::EOpParameters: |
| 2034 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2035 | // the body, so we still visited the function node's children, making this |
| 2036 | // child redundant. |
| 2037 | return false; |
| 2038 | case glslang::EOpFunctionCall: |
| 2039 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2040 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2041 | if (node->isUserDefined()) |
| 2042 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2043 | // 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] | 2044 | if (result) { |
| 2045 | builder.clearAccessChain(); |
| 2046 | builder.setAccessChainRValue(result); |
| 2047 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2048 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2049 | |
| 2050 | return false; |
| 2051 | } |
| 2052 | case glslang::EOpConstructMat2x2: |
| 2053 | case glslang::EOpConstructMat2x3: |
| 2054 | case glslang::EOpConstructMat2x4: |
| 2055 | case glslang::EOpConstructMat3x2: |
| 2056 | case glslang::EOpConstructMat3x3: |
| 2057 | case glslang::EOpConstructMat3x4: |
| 2058 | case glslang::EOpConstructMat4x2: |
| 2059 | case glslang::EOpConstructMat4x3: |
| 2060 | case glslang::EOpConstructMat4x4: |
| 2061 | case glslang::EOpConstructDMat2x2: |
| 2062 | case glslang::EOpConstructDMat2x3: |
| 2063 | case glslang::EOpConstructDMat2x4: |
| 2064 | case glslang::EOpConstructDMat3x2: |
| 2065 | case glslang::EOpConstructDMat3x3: |
| 2066 | case glslang::EOpConstructDMat3x4: |
| 2067 | case glslang::EOpConstructDMat4x2: |
| 2068 | case glslang::EOpConstructDMat4x3: |
| 2069 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2070 | case glslang::EOpConstructIMat2x2: |
| 2071 | case glslang::EOpConstructIMat2x3: |
| 2072 | case glslang::EOpConstructIMat2x4: |
| 2073 | case glslang::EOpConstructIMat3x2: |
| 2074 | case glslang::EOpConstructIMat3x3: |
| 2075 | case glslang::EOpConstructIMat3x4: |
| 2076 | case glslang::EOpConstructIMat4x2: |
| 2077 | case glslang::EOpConstructIMat4x3: |
| 2078 | case glslang::EOpConstructIMat4x4: |
| 2079 | case glslang::EOpConstructUMat2x2: |
| 2080 | case glslang::EOpConstructUMat2x3: |
| 2081 | case glslang::EOpConstructUMat2x4: |
| 2082 | case glslang::EOpConstructUMat3x2: |
| 2083 | case glslang::EOpConstructUMat3x3: |
| 2084 | case glslang::EOpConstructUMat3x4: |
| 2085 | case glslang::EOpConstructUMat4x2: |
| 2086 | case glslang::EOpConstructUMat4x3: |
| 2087 | case glslang::EOpConstructUMat4x4: |
| 2088 | case glslang::EOpConstructBMat2x2: |
| 2089 | case glslang::EOpConstructBMat2x3: |
| 2090 | case glslang::EOpConstructBMat2x4: |
| 2091 | case glslang::EOpConstructBMat3x2: |
| 2092 | case glslang::EOpConstructBMat3x3: |
| 2093 | case glslang::EOpConstructBMat3x4: |
| 2094 | case glslang::EOpConstructBMat4x2: |
| 2095 | case glslang::EOpConstructBMat4x3: |
| 2096 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2097 | case glslang::EOpConstructF16Mat2x2: |
| 2098 | case glslang::EOpConstructF16Mat2x3: |
| 2099 | case glslang::EOpConstructF16Mat2x4: |
| 2100 | case glslang::EOpConstructF16Mat3x2: |
| 2101 | case glslang::EOpConstructF16Mat3x3: |
| 2102 | case glslang::EOpConstructF16Mat3x4: |
| 2103 | case glslang::EOpConstructF16Mat4x2: |
| 2104 | case glslang::EOpConstructF16Mat4x3: |
| 2105 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2106 | isMatrix = true; |
| 2107 | // fall through |
| 2108 | case glslang::EOpConstructFloat: |
| 2109 | case glslang::EOpConstructVec2: |
| 2110 | case glslang::EOpConstructVec3: |
| 2111 | case glslang::EOpConstructVec4: |
| 2112 | case glslang::EOpConstructDouble: |
| 2113 | case glslang::EOpConstructDVec2: |
| 2114 | case glslang::EOpConstructDVec3: |
| 2115 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2116 | case glslang::EOpConstructFloat16: |
| 2117 | case glslang::EOpConstructF16Vec2: |
| 2118 | case glslang::EOpConstructF16Vec3: |
| 2119 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2120 | case glslang::EOpConstructBool: |
| 2121 | case glslang::EOpConstructBVec2: |
| 2122 | case glslang::EOpConstructBVec3: |
| 2123 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2124 | case glslang::EOpConstructInt8: |
| 2125 | case glslang::EOpConstructI8Vec2: |
| 2126 | case glslang::EOpConstructI8Vec3: |
| 2127 | case glslang::EOpConstructI8Vec4: |
| 2128 | case glslang::EOpConstructUint8: |
| 2129 | case glslang::EOpConstructU8Vec2: |
| 2130 | case glslang::EOpConstructU8Vec3: |
| 2131 | case glslang::EOpConstructU8Vec4: |
| 2132 | case glslang::EOpConstructInt16: |
| 2133 | case glslang::EOpConstructI16Vec2: |
| 2134 | case glslang::EOpConstructI16Vec3: |
| 2135 | case glslang::EOpConstructI16Vec4: |
| 2136 | case glslang::EOpConstructUint16: |
| 2137 | case glslang::EOpConstructU16Vec2: |
| 2138 | case glslang::EOpConstructU16Vec3: |
| 2139 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2140 | case glslang::EOpConstructInt: |
| 2141 | case glslang::EOpConstructIVec2: |
| 2142 | case glslang::EOpConstructIVec3: |
| 2143 | case glslang::EOpConstructIVec4: |
| 2144 | case glslang::EOpConstructUint: |
| 2145 | case glslang::EOpConstructUVec2: |
| 2146 | case glslang::EOpConstructUVec3: |
| 2147 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2148 | case glslang::EOpConstructInt64: |
| 2149 | case glslang::EOpConstructI64Vec2: |
| 2150 | case glslang::EOpConstructI64Vec3: |
| 2151 | case glslang::EOpConstructI64Vec4: |
| 2152 | case glslang::EOpConstructUint64: |
| 2153 | case glslang::EOpConstructU64Vec2: |
| 2154 | case glslang::EOpConstructU64Vec3: |
| 2155 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2156 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2157 | case glslang::EOpConstructTextureSampler: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2158 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2159 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2160 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2161 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2162 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2163 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2164 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2165 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2166 | std::vector<spv::Id> constituents; |
| 2167 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2168 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2169 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2170 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2171 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2172 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2173 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2174 | |
| 2175 | builder.clearAccessChain(); |
| 2176 | builder.setAccessChainRValue(constructed); |
| 2177 | |
| 2178 | return false; |
| 2179 | } |
| 2180 | |
| 2181 | // These six are component-wise compares with component-wise results. |
| 2182 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2183 | case glslang::EOpLessThan: |
| 2184 | case glslang::EOpGreaterThan: |
| 2185 | case glslang::EOpLessThanEqual: |
| 2186 | case glslang::EOpGreaterThanEqual: |
| 2187 | case glslang::EOpVectorEqual: |
| 2188 | case glslang::EOpVectorNotEqual: |
| 2189 | { |
| 2190 | // Map the operation to a binary |
| 2191 | binOp = node->getOp(); |
| 2192 | reduceComparison = false; |
| 2193 | switch (node->getOp()) { |
| 2194 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2195 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2196 | default: binOp = node->getOp(); break; |
| 2197 | } |
| 2198 | |
| 2199 | break; |
| 2200 | } |
| 2201 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2202 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2203 | binOp = glslang::EOpMul; |
| 2204 | break; |
| 2205 | case glslang::EOpOuterProduct: |
| 2206 | // two vectors multiplied to make a matrix |
| 2207 | binOp = glslang::EOpOuterProduct; |
| 2208 | break; |
| 2209 | case glslang::EOpDot: |
| 2210 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2211 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2212 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2213 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2214 | binOp = glslang::EOpMul; |
| 2215 | break; |
| 2216 | } |
| 2217 | case glslang::EOpMod: |
| 2218 | // when an aggregate, this is the floating-point mod built-in function, |
| 2219 | // which can be emitted by the one in createBinaryOperation() |
| 2220 | binOp = glslang::EOpMod; |
| 2221 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2222 | case glslang::EOpEmitVertex: |
| 2223 | case glslang::EOpEndPrimitive: |
| 2224 | case glslang::EOpBarrier: |
| 2225 | case glslang::EOpMemoryBarrier: |
| 2226 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2227 | case glslang::EOpMemoryBarrierBuffer: |
| 2228 | case glslang::EOpMemoryBarrierImage: |
| 2229 | case glslang::EOpMemoryBarrierShared: |
| 2230 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2231 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2232 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2233 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2234 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2235 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2236 | case glslang::EOpSubgroupBarrier: |
| 2237 | case glslang::EOpSubgroupMemoryBarrier: |
| 2238 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2239 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2240 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2241 | noReturnValue = true; |
| 2242 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2243 | break; |
| 2244 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2245 | case glslang::EOpAtomicStore: |
| 2246 | noReturnValue = true; |
| 2247 | // fallthrough |
| 2248 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2249 | case glslang::EOpAtomicAdd: |
| 2250 | case glslang::EOpAtomicMin: |
| 2251 | case glslang::EOpAtomicMax: |
| 2252 | case glslang::EOpAtomicAnd: |
| 2253 | case glslang::EOpAtomicOr: |
| 2254 | case glslang::EOpAtomicXor: |
| 2255 | case glslang::EOpAtomicExchange: |
| 2256 | case glslang::EOpAtomicCompSwap: |
| 2257 | atomic = true; |
| 2258 | break; |
| 2259 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2260 | case glslang::EOpAtomicCounterAdd: |
| 2261 | case glslang::EOpAtomicCounterSubtract: |
| 2262 | case glslang::EOpAtomicCounterMin: |
| 2263 | case glslang::EOpAtomicCounterMax: |
| 2264 | case glslang::EOpAtomicCounterAnd: |
| 2265 | case glslang::EOpAtomicCounterOr: |
| 2266 | case glslang::EOpAtomicCounterXor: |
| 2267 | case glslang::EOpAtomicCounterExchange: |
| 2268 | case glslang::EOpAtomicCounterCompSwap: |
| 2269 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2270 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2271 | atomic = true; |
| 2272 | break; |
| 2273 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2274 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 2275 | case glslang::EOpIgnoreIntersectionNV: |
| 2276 | case glslang::EOpTerminateRayNV: |
| 2277 | case glslang::EOpTraceNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 2278 | case glslang::EOpExecuteCallableNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2279 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2280 | noReturnValue = true; |
| 2281 | break; |
| 2282 | #endif |
| 2283 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2284 | default: |
| 2285 | break; |
| 2286 | } |
| 2287 | |
| 2288 | // |
| 2289 | // See if it maps to a regular operation. |
| 2290 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2291 | if (binOp != glslang::EOpNull) { |
| 2292 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2293 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2294 | assert(left && right); |
| 2295 | |
| 2296 | builder.clearAccessChain(); |
| 2297 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2298 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2299 | |
| 2300 | builder.clearAccessChain(); |
| 2301 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2302 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2303 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2304 | builder.setLine(node->getLoc().line); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2305 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2306 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2307 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2308 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2309 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2310 | left->getType().getBasicType(), reduceComparison); |
| 2311 | |
| 2312 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2313 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2314 | builder.clearAccessChain(); |
| 2315 | builder.setAccessChainRValue(result); |
| 2316 | |
| 2317 | return false; |
| 2318 | } |
| 2319 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2320 | // |
| 2321 | // Create the list of operands. |
| 2322 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2323 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2324 | std::vector<spv::Id> operands; |
| 2325 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2326 | // special case l-value operands; there are just a few |
| 2327 | bool lvalue = false; |
| 2328 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2329 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2330 | case glslang::EOpModf: |
| 2331 | if (arg == 1) |
| 2332 | lvalue = true; |
| 2333 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2334 | case glslang::EOpInterpolateAtSample: |
| 2335 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2336 | #ifdef AMD_EXTENSIONS |
| 2337 | case glslang::EOpInterpolateAtVertex: |
| 2338 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2339 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2340 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2341 | |
| 2342 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2343 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2344 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2345 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2346 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 2347 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2348 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2349 | case glslang::EOpAtomicAdd: |
| 2350 | case glslang::EOpAtomicMin: |
| 2351 | case glslang::EOpAtomicMax: |
| 2352 | case glslang::EOpAtomicAnd: |
| 2353 | case glslang::EOpAtomicOr: |
| 2354 | case glslang::EOpAtomicXor: |
| 2355 | case glslang::EOpAtomicExchange: |
| 2356 | case glslang::EOpAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2357 | case glslang::EOpAtomicLoad: |
| 2358 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2359 | case glslang::EOpAtomicCounterAdd: |
| 2360 | case glslang::EOpAtomicCounterSubtract: |
| 2361 | case glslang::EOpAtomicCounterMin: |
| 2362 | case glslang::EOpAtomicCounterMax: |
| 2363 | case glslang::EOpAtomicCounterAnd: |
| 2364 | case glslang::EOpAtomicCounterOr: |
| 2365 | case glslang::EOpAtomicCounterXor: |
| 2366 | case glslang::EOpAtomicCounterExchange: |
| 2367 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2368 | if (arg == 0) |
| 2369 | lvalue = true; |
| 2370 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2371 | case glslang::EOpAddCarry: |
| 2372 | case glslang::EOpSubBorrow: |
| 2373 | if (arg == 2) |
| 2374 | lvalue = true; |
| 2375 | break; |
| 2376 | case glslang::EOpUMulExtended: |
| 2377 | case glslang::EOpIMulExtended: |
| 2378 | if (arg >= 2) |
| 2379 | lvalue = true; |
| 2380 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2381 | default: |
| 2382 | break; |
| 2383 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2384 | builder.clearAccessChain(); |
| 2385 | if (invertedType != spv::NoType && arg == 0) |
| 2386 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2387 | else |
| 2388 | glslangOperands[arg]->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2389 | if (lvalue) |
| 2390 | operands.push_back(builder.accessChainGetLValue()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2391 | else { |
| 2392 | builder.setLine(node->getLoc().line); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2393 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2394 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2395 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2396 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2397 | builder.setLine(node->getLoc().line); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2398 | if (atomic) { |
| 2399 | // Handle all atomics |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2400 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2401 | } else { |
| 2402 | // Pass through to generic operations. |
| 2403 | switch (glslangOperands.size()) { |
| 2404 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2405 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2406 | break; |
| 2407 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2408 | { |
| 2409 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2410 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2411 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2412 | result = createUnaryOperation( |
| 2413 | node->getOp(), decorations, |
| 2414 | resultType(), operands.front(), |
| 2415 | glslangOperands[0]->getAsTyped()->getBasicType()); |
| 2416 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2417 | break; |
| 2418 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2419 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2420 | break; |
| 2421 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2422 | if (invertedType) |
| 2423 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | if (noReturnValue) |
| 2427 | return false; |
| 2428 | |
| 2429 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2430 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2431 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2432 | } else { |
| 2433 | builder.clearAccessChain(); |
| 2434 | builder.setAccessChainRValue(result); |
| 2435 | return false; |
| 2436 | } |
| 2437 | } |
| 2438 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2439 | // This path handles both if-then-else and ?: |
| 2440 | // The if-then-else has a node type of void, while |
| 2441 | // ?: has either a void or a non-void node type |
| 2442 | // |
| 2443 | // Leaving the result, when not void: |
| 2444 | // GLSL only has r-values as the result of a :?, but |
| 2445 | // if we have an l-value, that can be more efficient if it will |
| 2446 | // become the base of a complex r-value expression, because the |
| 2447 | // 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] | 2448 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 2449 | { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2450 | // See if it simple and safe, or required, to execute both sides. |
| 2451 | // Crucially, side effects must be either semantically required or avoided, |
| 2452 | // and there are performance trade-offs. |
| 2453 | // Return true if required or a good idea (and safe) to execute both sides, |
| 2454 | // false otherwise. |
| 2455 | const auto bothSidesPolicy = [&]() -> bool { |
| 2456 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2457 | if (node->getTrueBlock() == nullptr || |
| 2458 | node->getFalseBlock() == nullptr) |
| 2459 | return false; |
| 2460 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2461 | // required? (unless we write additional code to look for side effects |
| 2462 | // and make performance trade-offs if none are present) |
| 2463 | if (!node->getShortCircuit()) |
| 2464 | return true; |
| 2465 | |
| 2466 | // if not required to execute both, decide based on performance/practicality... |
| 2467 | |
| 2468 | // see if OpSelect can handle it |
| 2469 | if ((!node->getType().isScalar() && !node->getType().isVector()) || |
| 2470 | node->getBasicType() == glslang::EbtVoid) |
| 2471 | return false; |
| 2472 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2473 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 2474 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 2475 | |
| 2476 | // return true if a single operand to ? : is okay for OpSelect |
| 2477 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2478 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2479 | }; |
| 2480 | |
| 2481 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 2482 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 2483 | }; |
| 2484 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2485 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 2486 | // emit the condition before doing anything with selection |
| 2487 | node->getCondition()->traverse(this); |
| 2488 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 2489 | |
| 2490 | // Find a way of executing both sides and selecting the right result. |
| 2491 | const auto executeBothSides = [&]() -> void { |
| 2492 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2493 | node->getTrueBlock()->traverse(this); |
| 2494 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2495 | node->getFalseBlock()->traverse(this); |
| 2496 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2497 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2498 | builder.setLine(node->getLoc().line); |
| 2499 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2500 | // done if void |
| 2501 | if (node->getBasicType() == glslang::EbtVoid) |
| 2502 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 2503 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2504 | // emit code to select between trueValue and falseValue |
| 2505 | |
| 2506 | // see if OpSelect can handle it |
| 2507 | if (node->getType().isScalar() || node->getType().isVector()) { |
| 2508 | // Emit OpSelect for this selection. |
| 2509 | |
| 2510 | // smear condition to vector, if necessary (AST is always scalar) |
| 2511 | if (builder.isVector(trueValue)) |
| 2512 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 2513 | builder.makeVectorType(builder.makeBoolType(), |
| 2514 | builder.getNumComponents(trueValue))); |
| 2515 | |
| 2516 | // OpSelect |
| 2517 | result = builder.createTriOp(spv::OpSelect, |
| 2518 | convertGlslangToSpvType(node->getType()), condition, |
| 2519 | trueValue, falseValue); |
| 2520 | |
| 2521 | builder.clearAccessChain(); |
| 2522 | builder.setAccessChainRValue(result); |
| 2523 | } else { |
| 2524 | // We need control flow to select the result. |
| 2525 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 2526 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2527 | |
| 2528 | // Selection control: |
| 2529 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2530 | |
| 2531 | // make an "if" based on the value created by the condition |
| 2532 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2533 | |
| 2534 | // emit the "then" statement |
| 2535 | builder.createStore(trueValue, result); |
| 2536 | ifBuilder.makeBeginElse(); |
| 2537 | // emit the "else" statement |
| 2538 | builder.createStore(falseValue, result); |
| 2539 | |
| 2540 | // finish off the control flow |
| 2541 | ifBuilder.makeEndIf(); |
| 2542 | |
| 2543 | builder.clearAccessChain(); |
| 2544 | builder.setAccessChainLValue(result); |
| 2545 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2546 | }; |
| 2547 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2548 | // Execute the one side needed, as per the condition |
| 2549 | const auto executeOneSide = [&]() { |
| 2550 | // Always emit control flow. |
| 2551 | if (node->getBasicType() != glslang::EbtVoid) |
| 2552 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2553 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2554 | // Selection control: |
| 2555 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2556 | |
| 2557 | // make an "if" based on the value created by the condition |
| 2558 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2559 | |
| 2560 | // emit the "then" statement |
| 2561 | if (node->getTrueBlock() != nullptr) { |
| 2562 | node->getTrueBlock()->traverse(this); |
| 2563 | if (result != spv::NoResult) |
| 2564 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 2565 | } |
| 2566 | |
| 2567 | if (node->getFalseBlock() != nullptr) { |
| 2568 | ifBuilder.makeBeginElse(); |
| 2569 | // emit the "else" statement |
| 2570 | node->getFalseBlock()->traverse(this); |
| 2571 | if (result != spv::NoResult) |
| 2572 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 2573 | } |
| 2574 | |
| 2575 | // finish off the control flow |
| 2576 | ifBuilder.makeEndIf(); |
| 2577 | |
| 2578 | if (result != spv::NoResult) { |
| 2579 | builder.clearAccessChain(); |
| 2580 | builder.setAccessChainLValue(result); |
| 2581 | } |
| 2582 | }; |
| 2583 | |
| 2584 | // Try for OpSelect (or a requirement to execute both sides) |
| 2585 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2586 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2587 | if (node->getType().getQualifier().isSpecConstant()) |
| 2588 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2589 | executeBothSides(); |
| 2590 | } else |
| 2591 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2592 | |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
| 2596 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2597 | { |
| 2598 | // emit and get the condition before doing anything with switch |
| 2599 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2600 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2601 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2602 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 2603 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2604 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2605 | // browse the children to sort out code segments |
| 2606 | int defaultSegment = -1; |
| 2607 | std::vector<TIntermNode*> codeSegments; |
| 2608 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 2609 | std::vector<int> caseValues; |
| 2610 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 2611 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 2612 | TIntermNode* child = *c; |
| 2613 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2614 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2615 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2616 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2617 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2618 | } else |
| 2619 | codeSegments.push_back(child); |
| 2620 | } |
| 2621 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2622 | // 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] | 2623 | // statements between the last case and the end of the switch statement |
| 2624 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 2625 | (int)codeSegments.size() == defaultSegment) |
| 2626 | codeSegments.push_back(nullptr); |
| 2627 | |
| 2628 | // make the switch statement |
| 2629 | 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] | 2630 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2631 | |
| 2632 | // emit all the code in the segments |
| 2633 | breakForLoop.push(false); |
| 2634 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 2635 | builder.nextSwitchSegment(segmentBlocks, s); |
| 2636 | if (codeSegments[s]) |
| 2637 | codeSegments[s]->traverse(this); |
| 2638 | else |
| 2639 | builder.addSwitchBreak(); |
| 2640 | } |
| 2641 | breakForLoop.pop(); |
| 2642 | |
| 2643 | builder.endSwitch(segmentBlocks); |
| 2644 | |
| 2645 | return false; |
| 2646 | } |
| 2647 | |
| 2648 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 2649 | { |
| 2650 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2651 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2652 | |
| 2653 | builder.clearAccessChain(); |
| 2654 | builder.setAccessChainRValue(constant); |
| 2655 | } |
| 2656 | |
| 2657 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 2658 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2659 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2660 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2661 | |
| 2662 | // Loop control: |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2663 | unsigned int dependencyLength = glslang::TIntermLoop::dependencyInfinite; |
| 2664 | const spv::LoopControlMask control = TranslateLoopControl(*node, dependencyLength); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2665 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2666 | // Spec requires back edges to target header blocks, and every header block |
| 2667 | // must dominate its merge block. Make a header block first to ensure these |
| 2668 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 2669 | // by a block-ending branch. But we don't want to put any other body/test |
| 2670 | // instructions in it, since the body/test may have arbitrary instructions, |
| 2671 | // including merges of its own. |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2672 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2673 | builder.setBuildPoint(&blocks.head); |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2674 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, dependencyLength); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2675 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2676 | spv::Block& test = builder.makeNewBlock(); |
| 2677 | builder.createBranch(&test); |
| 2678 | |
| 2679 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2680 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2681 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2682 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 2683 | |
| 2684 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2685 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2686 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2687 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2688 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2689 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2690 | |
| 2691 | builder.setBuildPoint(&blocks.continue_target); |
| 2692 | if (node->getTerminal()) |
| 2693 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2694 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 2695 | } else { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2696 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2697 | builder.createBranch(&blocks.body); |
| 2698 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2699 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2700 | builder.setBuildPoint(&blocks.body); |
| 2701 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2702 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2703 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2704 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2705 | |
| 2706 | builder.setBuildPoint(&blocks.continue_target); |
| 2707 | if (node->getTerminal()) |
| 2708 | node->getTerminal()->traverse(this); |
| 2709 | if (node->getTest()) { |
| 2710 | node->getTest()->traverse(this); |
| 2711 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2712 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2713 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2714 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 2715 | // TODO: unless there was a break/return/discard instruction |
| 2716 | // somewhere in the body, this is an infinite loop, so we should |
| 2717 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2718 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2719 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2720 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2721 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2722 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2723 | return false; |
| 2724 | } |
| 2725 | |
| 2726 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 2727 | { |
| 2728 | if (node->getExpression()) |
| 2729 | node->getExpression()->traverse(this); |
| 2730 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2731 | builder.setLine(node->getLoc().line); |
| 2732 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2733 | switch (node->getFlowOp()) { |
| 2734 | case glslang::EOpKill: |
| 2735 | builder.makeDiscard(); |
| 2736 | break; |
| 2737 | case glslang::EOpBreak: |
| 2738 | if (breakForLoop.top()) |
| 2739 | builder.createLoopExit(); |
| 2740 | else |
| 2741 | builder.addSwitchBreak(); |
| 2742 | break; |
| 2743 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2744 | builder.createLoopContinue(); |
| 2745 | break; |
| 2746 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2747 | if (node->getExpression()) { |
| 2748 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 2749 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 2750 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 2751 | builder.clearAccessChain(); |
| 2752 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 2753 | builder.setAccessChainLValue(copyId); |
| 2754 | multiTypeStore(glslangReturnType, returnId); |
| 2755 | returnId = builder.createLoad(copyId); |
| 2756 | } |
| 2757 | builder.makeReturn(false, returnId); |
| 2758 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2759 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2760 | |
| 2761 | builder.clearAccessChain(); |
| 2762 | break; |
| 2763 | |
| 2764 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2765 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2766 | break; |
| 2767 | } |
| 2768 | |
| 2769 | return false; |
| 2770 | } |
| 2771 | |
| 2772 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 2773 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2774 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2775 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2776 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 2777 | if (node->getQualifier().isConstant()) { |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2778 | return createSpvConstant(*node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2782 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2783 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 2784 | |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2785 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 2786 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 2787 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2788 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2789 | switch (storageClass) { |
| 2790 | case spv::StorageClassInput: |
| 2791 | case spv::StorageClassOutput: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2792 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2793 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2794 | break; |
| 2795 | case spv::StorageClassPushConstant: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2796 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2797 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2798 | break; |
| 2799 | case spv::StorageClassUniform: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2800 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2801 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2802 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2803 | else |
| 2804 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 2805 | break; |
| 2806 | case spv::StorageClassStorageBuffer: |
| 2807 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
| 2808 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 2809 | break; |
| 2810 | default: |
| 2811 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2812 | } |
| 2813 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2814 | |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 2815 | const bool contains8BitType = node->getType().containsBasicType(glslang::EbtInt8) || |
| 2816 | node->getType().containsBasicType(glslang::EbtUint8); |
| 2817 | if (contains8BitType) { |
| 2818 | if (storageClass == spv::StorageClassPushConstant) { |
| 2819 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2820 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 2821 | } else if (storageClass == spv::StorageClassUniform) { |
| 2822 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2823 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 2824 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
| 2825 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2826 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2830 | const char* name = node->getName().c_str(); |
| 2831 | if (glslang::IsAnonymous(name)) |
| 2832 | name = ""; |
| 2833 | |
| 2834 | return builder.createVariable(storageClass, spvType, name); |
| 2835 | } |
| 2836 | |
| 2837 | // Return type Id of the sampled type. |
| 2838 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 2839 | { |
| 2840 | switch (sampler.type) { |
| 2841 | case glslang::EbtFloat: return builder.makeFloatType(32); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 2842 | #ifdef AMD_EXTENSIONS |
| 2843 | case glslang::EbtFloat16: |
| 2844 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 2845 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 2846 | return builder.makeFloatType(16); |
| 2847 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2848 | case glslang::EbtInt: return builder.makeIntType(32); |
| 2849 | case glslang::EbtUint: return builder.makeUintType(32); |
| 2850 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2851 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2852 | return builder.makeFloatType(32); |
| 2853 | } |
| 2854 | } |
| 2855 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2856 | // If node is a swizzle operation, return the type that should be used if |
| 2857 | // the swizzle base is first consumed by another operation, before the swizzle |
| 2858 | // is applied. |
| 2859 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 2860 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2861 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2862 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2863 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 2864 | else |
| 2865 | return spv::NoType; |
| 2866 | } |
| 2867 | |
| 2868 | // When inverting a swizzle with a parent op, this function |
| 2869 | // will apply the swizzle operation to a completed parent operation. |
| 2870 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 2871 | { |
| 2872 | std::vector<unsigned> swizzle; |
| 2873 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 2874 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 2875 | } |
| 2876 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2877 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 2878 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 2879 | { |
| 2880 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 2881 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 2882 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2883 | } |
| 2884 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2885 | // Convert from a glslang type to an SPV type, by calling into a |
| 2886 | // recursive version of this function. This establishes the inherited |
| 2887 | // layout state rooted from the top-level type. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2888 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type) |
| 2889 | { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2890 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | // 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] | 2894 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2895 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2896 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
| 2897 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, bool lastBufferBlockMember) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2898 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2899 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2900 | |
| 2901 | switch (type.getBasicType()) { |
| 2902 | case glslang::EbtVoid: |
| 2903 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2904 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2905 | break; |
| 2906 | case glslang::EbtFloat: |
| 2907 | spvType = builder.makeFloatType(32); |
| 2908 | break; |
| 2909 | case glslang::EbtDouble: |
| 2910 | spvType = builder.makeFloatType(64); |
| 2911 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2912 | case glslang::EbtFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2913 | spvType = builder.makeFloatType(16); |
| 2914 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2915 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2916 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 2917 | // a 32-bit int where non-0 means true. |
| 2918 | if (explicitLayout != glslang::ElpNone) |
| 2919 | spvType = builder.makeUintType(32); |
| 2920 | else |
| 2921 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2922 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 2923 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2924 | spvType = builder.makeIntType(8); |
| 2925 | break; |
| 2926 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2927 | spvType = builder.makeUintType(8); |
| 2928 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 2929 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2930 | spvType = builder.makeIntType(16); |
| 2931 | break; |
| 2932 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2933 | spvType = builder.makeUintType(16); |
| 2934 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2935 | case glslang::EbtInt: |
| 2936 | spvType = builder.makeIntType(32); |
| 2937 | break; |
| 2938 | case glslang::EbtUint: |
| 2939 | spvType = builder.makeUintType(32); |
| 2940 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2941 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2942 | spvType = builder.makeIntType(64); |
| 2943 | break; |
| 2944 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2945 | spvType = builder.makeUintType(64); |
| 2946 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2947 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 2948 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2949 | spvType = builder.makeUintType(32); |
| 2950 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 2951 | #ifdef NV_EXTENSIONS |
| 2952 | case glslang::EbtAccStructNV: |
| 2953 | spvType = builder.makeAccelerationStructureNVType(); |
| 2954 | break; |
| 2955 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2956 | case glslang::EbtSampler: |
| 2957 | { |
| 2958 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2959 | if (sampler.sampler) { |
| 2960 | // pure sampler |
| 2961 | spvType = builder.makeSamplerType(); |
| 2962 | } else { |
| 2963 | // an image is present, make its type |
| 2964 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 2965 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 2966 | if (sampler.combined) { |
| 2967 | // already has both image and sampler, make the combined type |
| 2968 | spvType = builder.makeSampledImageType(spvType); |
| 2969 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2970 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2971 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2972 | break; |
| 2973 | case glslang::EbtStruct: |
| 2974 | case glslang::EbtBlock: |
| 2975 | { |
| 2976 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2977 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2978 | |
| 2979 | // Try to share structs for different layouts, but not yet for other |
| 2980 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2981 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2982 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2983 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2984 | break; |
| 2985 | |
| 2986 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2987 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2988 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 2989 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2990 | } |
| 2991 | break; |
| 2992 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2993 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2994 | break; |
| 2995 | } |
| 2996 | |
| 2997 | if (type.isMatrix()) |
| 2998 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 2999 | else { |
| 3000 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3001 | if (type.getVectorSize() > 1) |
| 3002 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3003 | } |
| 3004 | |
| 3005 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3006 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3007 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3008 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3009 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3010 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3011 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3012 | // Use a dummy glslang type for querying internal strides of |
| 3013 | // arrays of arrays, but using just a one-dimensional array. |
| 3014 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3015 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3016 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3017 | |
| 3018 | // Will compute the higher-order strides here, rather than making a whole |
| 3019 | // pile of types and doing repetitive recursion on their contents. |
| 3020 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3021 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3022 | |
| 3023 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3024 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3025 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3026 | if (stride > 0) |
| 3027 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3028 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3029 | } |
| 3030 | } else { |
| 3031 | // single-dimensional array, and don't yet have stride |
| 3032 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3033 | // 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] | 3034 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3035 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3036 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3037 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3038 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3039 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3040 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3041 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3042 | else { |
| 3043 | if (!lastBufferBlockMember) { |
| 3044 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 3045 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3046 | } |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3047 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3048 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3049 | if (stride > 0) |
| 3050 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | return spvType; |
| 3054 | } |
| 3055 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3056 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3057 | // |
| 3058 | // Identify interface members that don't have their required extension turned on. |
| 3059 | // |
| 3060 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3061 | { |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3062 | #ifdef NV_EXTENSIONS |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3063 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3064 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3065 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3066 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3067 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3068 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3069 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3070 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3071 | |
| 3072 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3073 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3074 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3075 | return true; |
| 3076 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3077 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3078 | return true; |
| 3079 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3080 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3081 | return true; |
| 3082 | } |
| 3083 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3084 | |
| 3085 | return false; |
| 3086 | }; |
| 3087 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3088 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3089 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3090 | // Mutually recursive with convertGlslangToSpvType(). |
| 3091 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3092 | const glslang::TTypeList* glslangMembers, |
| 3093 | glslang::TLayoutPacking explicitLayout, |
| 3094 | const glslang::TQualifier& qualifier) |
| 3095 | { |
| 3096 | // Create a vector of struct types for SPIR-V to consume |
| 3097 | std::vector<spv::Id> spvMembers; |
| 3098 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3099 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3100 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3101 | if (glslangMember.hiddenMember()) { |
| 3102 | ++memberDelta; |
| 3103 | if (type.getBasicType() == glslang::EbtBlock) |
| 3104 | memberRemapper[glslangMembers][i] = -1; |
| 3105 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3106 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3107 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3108 | if (filterMember(glslangMember)) |
| 3109 | continue; |
| 3110 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3111 | // modify just this child's view of the qualifier |
| 3112 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3113 | InheritQualifiers(memberQualifier, qualifier); |
| 3114 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3115 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3116 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3117 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3118 | |
| 3119 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3120 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3121 | i == (int)glslangMembers->size() - 1; |
| 3122 | spvMembers.push_back( |
| 3123 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3124 | } |
| 3125 | } |
| 3126 | |
| 3127 | // Make the SPIR-V type |
| 3128 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3129 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3130 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3131 | |
| 3132 | // Decorate it |
| 3133 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3134 | |
| 3135 | return spvType; |
| 3136 | } |
| 3137 | |
| 3138 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3139 | const glslang::TTypeList* glslangMembers, |
| 3140 | glslang::TLayoutPacking explicitLayout, |
| 3141 | const glslang::TQualifier& qualifier, |
| 3142 | spv::Id spvType) |
| 3143 | { |
| 3144 | // Name and decorate the non-hidden members |
| 3145 | int offset = -1; |
| 3146 | int locationOffset = 0; // for use within the members of this struct |
| 3147 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3148 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3149 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3150 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3151 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3152 | if (filterMember(glslangMember)) |
| 3153 | continue; |
| 3154 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3155 | |
| 3156 | // modify just this child's view of the qualifier |
| 3157 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3158 | InheritQualifiers(memberQualifier, qualifier); |
| 3159 | |
| 3160 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3161 | if (member < 0) |
| 3162 | continue; |
| 3163 | |
| 3164 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3165 | builder.addMemberDecoration(spvType, member, |
| 3166 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 3167 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 3168 | // Add interpolation and auxiliary storage decorations only to |
| 3169 | // top-level members of Input and Output storage classes |
| 3170 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 3171 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 3172 | if (type.getBasicType() == glslang::EbtBlock || |
| 3173 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 3174 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 3175 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3176 | #ifdef NV_EXTENSIONS |
| 3177 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 3178 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3179 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3180 | } |
| 3181 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3182 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3183 | if (type.getBasicType() == glslang::EbtBlock && |
| 3184 | qualifier.storage == glslang::EvqBuffer) { |
| 3185 | // Add memory decorations only to top-level members of shader storage block |
| 3186 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3187 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3188 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3189 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 3190 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3191 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3192 | // Location assignment was already completed correctly by the front end, |
| 3193 | // just track whether a member needs to be decorated. |
| 3194 | // Ignore member locations if the container is an array, as that's |
| 3195 | // ill-specified and decisions have been made to not allow this. |
| 3196 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 3197 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3198 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3199 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 3200 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 3201 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 3202 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3203 | // component, XFB, others |
| 3204 | if (glslangMember.getQualifier().hasComponent()) |
| 3205 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 3206 | glslangMember.getQualifier().layoutComponent); |
| 3207 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 3208 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 3209 | glslangMember.getQualifier().layoutXfbOffset); |
| 3210 | else if (explicitLayout != glslang::ElpNone) { |
| 3211 | // figure out what to do with offset, which is accumulating |
| 3212 | int nextOffset; |
| 3213 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 3214 | if (offset >= 0) |
| 3215 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 3216 | offset = nextOffset; |
| 3217 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3218 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3219 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 3220 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 3221 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3222 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3223 | // built-in variable decorations |
| 3224 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 3225 | if (builtIn != spv::BuiltInMax) |
| 3226 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3227 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3228 | // nonuniform |
| 3229 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 3230 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3231 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 3232 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 3233 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 3234 | memberQualifier.semanticName); |
| 3235 | } |
| 3236 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3237 | #ifdef NV_EXTENSIONS |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3238 | if (builtIn == spv::BuiltInLayer) { |
| 3239 | // SPV_NV_viewport_array2 extension |
| 3240 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 3241 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 3242 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 3243 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3244 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3245 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 3246 | builder.addMemberDecoration(spvType, member, |
| 3247 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 3248 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 3249 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 3250 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3251 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3252 | } |
| 3253 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 3254 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 3255 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 3256 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 3257 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3258 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3262 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 3263 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3264 | if (type.getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
| 3265 | builder.addCapability(spv::CapabilityGeometryStreams); |
| 3266 | builder.addDecoration(spvType, spv::DecorationStream, type.getQualifier().layoutStream); |
| 3267 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3268 | } |
| 3269 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3270 | // Turn the expression forming the array size into an id. |
| 3271 | // This is not quite trivial, because of specialization constants. |
| 3272 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 3273 | // a specialization constant expression is. |
| 3274 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 3275 | { |
| 3276 | // First, see if this is sized with a node, meaning a specialization constant: |
| 3277 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 3278 | if (specNode != nullptr) { |
| 3279 | builder.clearAccessChain(); |
| 3280 | specNode->traverse(this); |
| 3281 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 3282 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3283 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3284 | // Otherwise, need a compile-time (front end) size, get it: |
| 3285 | int size = arraySizes.getDimSize(dim); |
| 3286 | assert(size > 0); |
| 3287 | return builder.makeUintConstant(size); |
| 3288 | } |
| 3289 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3290 | // Wrap the builder's accessChainLoad to: |
| 3291 | // - localize handling of RelaxedPrecision |
| 3292 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 3293 | // (avoids unnecessary work and possible type punning for structures) |
| 3294 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3295 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 3296 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3297 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3298 | |
| 3299 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3300 | coherentFlags |= TranslateCoherent(type); |
| 3301 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3302 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3303 | TranslateNonUniformDecoration(type.getQualifier()), |
| 3304 | nominalTypeId, |
| 3305 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
| 3306 | TranslateMemoryScope(coherentFlags)); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3307 | |
| 3308 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3309 | if (type.getBasicType() == glslang::EbtBool) { |
| 3310 | if (builder.isScalarType(nominalTypeId)) { |
| 3311 | // Conversion for bool |
| 3312 | spv::Id boolType = builder.makeBoolType(); |
| 3313 | if (nominalTypeId != boolType) |
| 3314 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 3315 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3316 | // Conversion for bvec |
| 3317 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3318 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 3319 | if (nominalTypeId != bvecType) |
| 3320 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 3321 | } |
| 3322 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3323 | |
| 3324 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3325 | } |
| 3326 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3327 | // Wrap the builder's accessChainStore to: |
| 3328 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3329 | // |
| 3330 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3331 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 3332 | { |
| 3333 | // Need to convert to abstract types when necessary |
| 3334 | if (type.getBasicType() == glslang::EbtBool) { |
| 3335 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 3336 | |
| 3337 | if (builder.isScalarType(nominalTypeId)) { |
| 3338 | // Conversion for bool |
| 3339 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3340 | if (nominalTypeId != boolType) { |
| 3341 | // keep these outside arguments, for determinant order-of-evaluation |
| 3342 | spv::Id one = builder.makeUintConstant(1); |
| 3343 | spv::Id zero = builder.makeUintConstant(0); |
| 3344 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 3345 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3346 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3347 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3348 | // Conversion for bvec |
| 3349 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3350 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3351 | if (nominalTypeId != bvecType) { |
| 3352 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 3353 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 3354 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 3355 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3356 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3357 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 3358 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3359 | } |
| 3360 | } |
| 3361 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3362 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3363 | coherentFlags |= TranslateCoherent(type); |
| 3364 | |
| 3365 | builder.accessChainStore(rvalue, |
| 3366 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask), |
| 3367 | TranslateMemoryScope(coherentFlags)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3368 | } |
| 3369 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3370 | // For storing when types match at the glslang level, but not might match at the |
| 3371 | // SPIR-V level. |
| 3372 | // |
| 3373 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3374 | // 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] | 3375 | // as in a member-decorated way. |
| 3376 | // |
| 3377 | // NOTE: This function can handle any store request; if it's not special it |
| 3378 | // simplifies to a simple OpStore. |
| 3379 | // |
| 3380 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 3381 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 3382 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3383 | // we only do the complex path here if it's an aggregate |
| 3384 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3385 | accessChainStore(type, rValue); |
| 3386 | return; |
| 3387 | } |
| 3388 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3389 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3390 | spv::Id rType = builder.getTypeId(rValue); |
| 3391 | spv::Id lValue = builder.accessChainGetLValue(); |
| 3392 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 3393 | if (lType == rType) { |
| 3394 | accessChainStore(type, rValue); |
| 3395 | return; |
| 3396 | } |
| 3397 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3398 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3399 | // where the two types were the same type in GLSL. This requires member |
| 3400 | // by member copy, recursively. |
| 3401 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3402 | // If an array, copy element by element. |
| 3403 | if (type.isArray()) { |
| 3404 | glslang::TType glslangElementType(type, 0); |
| 3405 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 3406 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 3407 | // get the source member |
| 3408 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3409 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3410 | // set up the target storage |
| 3411 | builder.clearAccessChain(); |
| 3412 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3413 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type)); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3414 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3415 | // store the member |
| 3416 | multiTypeStore(glslangElementType, elementRValue); |
| 3417 | } |
| 3418 | } else { |
| 3419 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3420 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3421 | // loop over structure members |
| 3422 | const glslang::TTypeList& members = *type.getStruct(); |
| 3423 | for (int m = 0; m < (int)members.size(); ++m) { |
| 3424 | const glslang::TType& glslangMemberType = *members[m].type; |
| 3425 | |
| 3426 | // get the source member |
| 3427 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 3428 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 3429 | |
| 3430 | // set up the target storage |
| 3431 | builder.clearAccessChain(); |
| 3432 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3433 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type)); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3434 | |
| 3435 | // store the member |
| 3436 | multiTypeStore(glslangMemberType, memberRValue); |
| 3437 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3438 | } |
| 3439 | } |
| 3440 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3441 | // Decide whether or not this type should be |
| 3442 | // decorated with offsets and strides, and if so |
| 3443 | // whether std140 or std430 rules should be applied. |
| 3444 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3445 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3446 | // has to be a block |
| 3447 | if (type.getBasicType() != glslang::EbtBlock) |
| 3448 | return glslang::ElpNone; |
| 3449 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3450 | // 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] | 3451 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3452 | type.getQualifier().storage != glslang::EvqBuffer && |
| 3453 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3454 | return glslang::ElpNone; |
| 3455 | |
| 3456 | // return the layout to use |
| 3457 | switch (type.getQualifier().layoutPacking) { |
| 3458 | case glslang::ElpStd140: |
| 3459 | case glslang::ElpStd430: |
| 3460 | return type.getQualifier().layoutPacking; |
| 3461 | default: |
| 3462 | return glslang::ElpNone; |
| 3463 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3464 | } |
| 3465 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3466 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3467 | 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] | 3468 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3469 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3470 | int stride; |
| 3471 | glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 3472 | |
| 3473 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3474 | } |
| 3475 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3476 | // 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] | 3477 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3478 | 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] | 3479 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3480 | glslang::TType elementType; |
| 3481 | elementType.shallowCopy(matrixType); |
| 3482 | elementType.clearArraySizes(); |
| 3483 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3484 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3485 | int stride; |
| 3486 | glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
| 3487 | |
| 3488 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3489 | } |
| 3490 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3491 | // Given a member type of a struct, realign the current offset for it, and compute |
| 3492 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 3493 | // on the next call. |
| 3494 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 3495 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 3496 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3497 | 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] | 3498 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3499 | { |
| 3500 | // this will get a positive value when deemed necessary |
| 3501 | nextOffset = -1; |
| 3502 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3503 | // override anything in currentOffset with user-set offset |
| 3504 | if (memberType.getQualifier().hasOffset()) |
| 3505 | currentOffset = memberType.getQualifier().layoutOffset; |
| 3506 | |
| 3507 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 3508 | // in which case the following code does not matter. But, that's not quite right |
| 3509 | // once cross-compilation unit GLSL validation is done, as the original user |
| 3510 | // settings are needed in layoutOffset, and then the following will come into play. |
| 3511 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3512 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3513 | if (! memberType.getQualifier().hasOffset()) |
| 3514 | currentOffset = -1; |
| 3515 | |
| 3516 | return; |
| 3517 | } |
| 3518 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3519 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3520 | if (currentOffset < 0) |
| 3521 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3522 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3523 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 3524 | // but possibly not yet correctly aligned. |
| 3525 | |
| 3526 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3527 | int dummyStride; |
| 3528 | int memberAlignment = glslangIntermediate->getBaseAlignment(memberType, memberSize, dummyStride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3529 | |
| 3530 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3531 | // TODO: make this consistent in early phases of code: |
| 3532 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 3533 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 3534 | // 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] | 3535 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3536 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3537 | int dummySize; |
| 3538 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 3539 | if (componentAlignment <= 4) |
| 3540 | memberAlignment = componentAlignment; |
| 3541 | } |
| 3542 | |
| 3543 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3544 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3545 | |
| 3546 | // Bump up to vec4 if there is a bad straddle |
| 3547 | if (glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
| 3548 | glslang::RoundToPow2(currentOffset, 16); |
| 3549 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3550 | nextOffset = currentOffset + memberSize; |
| 3551 | } |
| 3552 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3553 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3554 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3555 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 3556 | switch (glslangBuiltIn) |
| 3557 | { |
| 3558 | case glslang::EbvClipDistance: |
| 3559 | case glslang::EbvCullDistance: |
| 3560 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3561 | #ifdef NV_EXTENSIONS |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3562 | case glslang::EbvViewportMaskNV: |
| 3563 | case glslang::EbvSecondaryPositionNV: |
| 3564 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3565 | case glslang::EbvPositionPerViewNV: |
| 3566 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3567 | case glslang::EbvTaskCountNV: |
| 3568 | case glslang::EbvPrimitiveCountNV: |
| 3569 | case glslang::EbvPrimitiveIndicesNV: |
| 3570 | case glslang::EbvClipDistancePerViewNV: |
| 3571 | case glslang::EbvCullDistancePerViewNV: |
| 3572 | case glslang::EbvLayerPerViewNV: |
| 3573 | case glslang::EbvMeshViewCountNV: |
| 3574 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3575 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3576 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 3577 | // Alternately, we could just call this for any glslang built-in, since the |
| 3578 | // capability already guards against duplicates. |
| 3579 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 3580 | break; |
| 3581 | default: |
| 3582 | // Capabilities were already generated when the struct was declared. |
| 3583 | break; |
| 3584 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3585 | } |
| 3586 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3587 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3588 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 3589 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3590 | } |
| 3591 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3592 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3593 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 3594 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 3595 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3596 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3597 | assert(qualifier == glslang::EvqIn || |
| 3598 | qualifier == glslang::EvqOut || |
| 3599 | qualifier == glslang::EvqInOut || |
| 3600 | qualifier == glslang::EvqConstReadOnly); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3601 | return qualifier != glslang::EvqConstReadOnly; |
| 3602 | } |
| 3603 | |
| 3604 | // Is parameter pass-by-original? |
| 3605 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 3606 | bool implicitThisParam) |
| 3607 | { |
| 3608 | if (implicitThisParam) // implicit this |
| 3609 | return true; |
| 3610 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3611 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3612 | return paramType.containsOpaque() || // sampler, etc. |
| 3613 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 3614 | } |
| 3615 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3616 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 3617 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3618 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3619 | 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] | 3620 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 3621 | if (paramPrecision != spv::NoPrecision) |
| 3622 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3623 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3624 | }; |
| 3625 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3626 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3627 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3628 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3629 | continue; |
| 3630 | |
| 3631 | // 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] | 3632 | // 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] | 3633 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3634 | // 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] | 3635 | // function. What it is an address of varies: |
| 3636 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3637 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 3638 | // 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] | 3639 | // |
| 3640 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 3641 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3642 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 3643 | // 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] | 3644 | |
| 3645 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3646 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3647 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 3648 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3649 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 3650 | glslangIntermediate->implicitThisName; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3651 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3652 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3653 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3654 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 3655 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3656 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3657 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3658 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3659 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 3660 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3661 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3662 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3663 | paramTypes.push_back(typeId); |
| 3664 | } |
| 3665 | |
| 3666 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3667 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 3668 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3669 | glslFunction->getName().c_str(), paramTypes, |
| 3670 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3671 | if (implicitThis) |
| 3672 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3673 | |
| 3674 | // Track function to emit/call later |
| 3675 | functionMap[glslFunction->getName().c_str()] = function; |
| 3676 | |
| 3677 | // Set the parameter id's |
| 3678 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3679 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 3680 | // give a name too |
| 3681 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 3682 | } |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | // Process all the initializers, while skipping the functions and link objects |
| 3687 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 3688 | { |
| 3689 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 3690 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 3691 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 3692 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 3693 | |
| 3694 | // 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] | 3695 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3696 | initializer->traverse(this); |
| 3697 | } |
| 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | // Process all the functions, while skipping initializers. |
| 3702 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3703 | { |
| 3704 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3705 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 3706 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3707 | node->traverse(this); |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 3712 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3713 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3714 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3715 | currentFunction = functionMap[node->getName().c_str()]; |
| 3716 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3717 | builder.setBuildPoint(functionBlock); |
| 3718 | } |
| 3719 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3720 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3721 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3722 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3723 | |
| 3724 | glslang::TSampler sampler = {}; |
| 3725 | bool cubeCompare = false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3726 | #ifdef AMD_EXTENSIONS |
| 3727 | bool f16ShadowCompare = false; |
| 3728 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3729 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3730 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 3731 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3732 | #ifdef AMD_EXTENSIONS |
| 3733 | f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 3734 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3735 | } |
| 3736 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3737 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 3738 | builder.clearAccessChain(); |
| 3739 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3740 | |
| 3741 | // Special case l-value operands |
| 3742 | bool lvalue = false; |
| 3743 | switch (node.getOp()) { |
| 3744 | case glslang::EOpImageAtomicAdd: |
| 3745 | case glslang::EOpImageAtomicMin: |
| 3746 | case glslang::EOpImageAtomicMax: |
| 3747 | case glslang::EOpImageAtomicAnd: |
| 3748 | case glslang::EOpImageAtomicOr: |
| 3749 | case glslang::EOpImageAtomicXor: |
| 3750 | case glslang::EOpImageAtomicExchange: |
| 3751 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3752 | case glslang::EOpImageAtomicLoad: |
| 3753 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3754 | if (i == 0) |
| 3755 | lvalue = true; |
| 3756 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3757 | case glslang::EOpSparseImageLoad: |
| 3758 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 3759 | lvalue = true; |
| 3760 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3761 | #ifdef AMD_EXTENSIONS |
| 3762 | case glslang::EOpSparseTexture: |
| 3763 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 3764 | lvalue = true; |
| 3765 | break; |
| 3766 | case glslang::EOpSparseTextureClamp: |
| 3767 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 3768 | lvalue = true; |
| 3769 | break; |
| 3770 | case glslang::EOpSparseTextureLod: |
| 3771 | case glslang::EOpSparseTextureOffset: |
| 3772 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 3773 | lvalue = true; |
| 3774 | break; |
| 3775 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3776 | case glslang::EOpSparseTexture: |
| 3777 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 3778 | lvalue = true; |
| 3779 | break; |
| 3780 | case glslang::EOpSparseTextureClamp: |
| 3781 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 3782 | lvalue = true; |
| 3783 | break; |
| 3784 | case glslang::EOpSparseTextureLod: |
| 3785 | case glslang::EOpSparseTextureOffset: |
| 3786 | if (i == 3) |
| 3787 | lvalue = true; |
| 3788 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3789 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3790 | case glslang::EOpSparseTextureFetch: |
| 3791 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 3792 | lvalue = true; |
| 3793 | break; |
| 3794 | case glslang::EOpSparseTextureFetchOffset: |
| 3795 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 3796 | lvalue = true; |
| 3797 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3798 | #ifdef AMD_EXTENSIONS |
| 3799 | case glslang::EOpSparseTextureLodOffset: |
| 3800 | case glslang::EOpSparseTextureGrad: |
| 3801 | case glslang::EOpSparseTextureOffsetClamp: |
| 3802 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 3803 | lvalue = true; |
| 3804 | break; |
| 3805 | case glslang::EOpSparseTextureGradOffset: |
| 3806 | case glslang::EOpSparseTextureGradClamp: |
| 3807 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 3808 | lvalue = true; |
| 3809 | break; |
| 3810 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3811 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 3812 | lvalue = true; |
| 3813 | break; |
| 3814 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3815 | case glslang::EOpSparseTextureLodOffset: |
| 3816 | case glslang::EOpSparseTextureGrad: |
| 3817 | case glslang::EOpSparseTextureOffsetClamp: |
| 3818 | if (i == 4) |
| 3819 | lvalue = true; |
| 3820 | break; |
| 3821 | case glslang::EOpSparseTextureGradOffset: |
| 3822 | case glslang::EOpSparseTextureGradClamp: |
| 3823 | if (i == 5) |
| 3824 | lvalue = true; |
| 3825 | break; |
| 3826 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3827 | if (i == 6) |
| 3828 | lvalue = true; |
| 3829 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3830 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3831 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3832 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 3833 | lvalue = true; |
| 3834 | break; |
| 3835 | case glslang::EOpSparseTextureGatherOffset: |
| 3836 | case glslang::EOpSparseTextureGatherOffsets: |
| 3837 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 3838 | lvalue = true; |
| 3839 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3840 | #ifdef AMD_EXTENSIONS |
| 3841 | case glslang::EOpSparseTextureGatherLod: |
| 3842 | if (i == 3) |
| 3843 | lvalue = true; |
| 3844 | break; |
| 3845 | case glslang::EOpSparseTextureGatherLodOffset: |
| 3846 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 3847 | if (i == 4) |
| 3848 | lvalue = true; |
| 3849 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3850 | case glslang::EOpSparseImageLoadLod: |
| 3851 | if (i == 3) |
| 3852 | lvalue = true; |
| 3853 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3854 | #endif |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 3855 | #ifdef NV_EXTENSIONS |
| 3856 | case glslang::EOpImageSampleFootprintNV: |
| 3857 | if (i == 4) |
| 3858 | lvalue = true; |
| 3859 | break; |
| 3860 | case glslang::EOpImageSampleFootprintClampNV: |
| 3861 | case glslang::EOpImageSampleFootprintLodNV: |
| 3862 | if (i == 5) |
| 3863 | lvalue = true; |
| 3864 | break; |
| 3865 | case glslang::EOpImageSampleFootprintGradNV: |
| 3866 | if (i == 6) |
| 3867 | lvalue = true; |
| 3868 | break; |
| 3869 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 3870 | if (i == 7) |
| 3871 | lvalue = true; |
| 3872 | break; |
| 3873 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3874 | default: |
| 3875 | break; |
| 3876 | } |
| 3877 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3878 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3879 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3880 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3881 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3882 | } |
| 3883 | } |
| 3884 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3885 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3886 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3887 | builder.clearAccessChain(); |
| 3888 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3889 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3890 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3891 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3892 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 3893 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3894 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3895 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3896 | |
| 3897 | builder.setLine(node->getLoc().line); |
| 3898 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3899 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3900 | |
| 3901 | const glslang::TType &imageType = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 3902 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
| 3903 | const glslang::TSampler sampler = imageType.getSampler(); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3904 | #ifdef AMD_EXTENSIONS |
| 3905 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
| 3906 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 3907 | : false; |
| 3908 | #endif |
| 3909 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3910 | std::vector<spv::Id> arguments; |
| 3911 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3912 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3913 | else |
| 3914 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 3915 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3916 | |
| 3917 | spv::Builder::TextureParameters params = { }; |
| 3918 | params.sampler = arguments[0]; |
| 3919 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3920 | glslang::TCrackedTextureOp cracked; |
| 3921 | node->crackTexture(sampler, cracked); |
| 3922 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 3923 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3924 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3925 | // Check for queries |
| 3926 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3927 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 3928 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 3929 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3930 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3931 | switch (node->getOp()) { |
| 3932 | case glslang::EOpImageQuerySize: |
| 3933 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3934 | if (arguments.size() > 1) { |
| 3935 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3936 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3937 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3938 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3939 | case glslang::EOpImageQuerySamples: |
| 3940 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3941 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3942 | case glslang::EOpTextureQueryLod: |
| 3943 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3944 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3945 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3946 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3947 | case glslang::EOpSparseTexelsResident: |
| 3948 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3949 | default: |
| 3950 | assert(0); |
| 3951 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3952 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3953 | } |
| 3954 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 3955 | int components = node->getType().getVectorSize(); |
| 3956 | |
| 3957 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 3958 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 3959 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 3960 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 3961 | // here around e.g. which ones return scalars or other types. |
| 3962 | components = 4; |
| 3963 | } |
| 3964 | |
| 3965 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 3966 | |
| 3967 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 3968 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3969 | // Check for image functions other than queries |
| 3970 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3971 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3972 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3973 | spv::IdImmediate image = { true, *(opIt++) }; |
| 3974 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3975 | |
| 3976 | // Handle subpass operations |
| 3977 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 3978 | // built-in function. |
| 3979 | if (cracked.subpass) { |
| 3980 | // add on the (0,0) coordinate |
| 3981 | spv::Id zero = builder.makeIntConstant(0); |
| 3982 | std::vector<spv::Id> comps; |
| 3983 | comps.push_back(zero); |
| 3984 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3985 | spv::IdImmediate coord = { true, |
| 3986 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 3987 | operands.push_back(coord); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3988 | if (sampler.ms) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3989 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsSampleMask }; |
| 3990 | operands.push_back(imageOperands); |
| 3991 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 3992 | operands.push_back(imageOperand); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3993 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 3994 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 3995 | builder.setPrecision(result, precision); |
| 3996 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3997 | } |
| 3998 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3999 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 4000 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4001 | #ifdef AMD_EXTENSIONS |
| 4002 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 4003 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4004 | if (node->getOp() == glslang::EOpImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4005 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4006 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4007 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4008 | mask = mask | spv::ImageOperandsSampleMask; |
| 4009 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4010 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4011 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4012 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4013 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4014 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4015 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4016 | #endif |
| 4017 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4018 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 4019 | if (mask) { |
| 4020 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4021 | operands.push_back(imageOperands); |
| 4022 | } |
| 4023 | if (mask & spv::ImageOperandsSampleMask) { |
| 4024 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4025 | operands.push_back(imageOperand); |
| 4026 | } |
| 4027 | #ifdef AMD_EXTENSIONS |
| 4028 | if (mask & spv::ImageOperandsLodMask) { |
| 4029 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4030 | operands.push_back(imageOperand); |
| 4031 | } |
| 4032 | #endif |
| 4033 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4034 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4035 | operands.push_back(imageOperand); |
| 4036 | } |
| 4037 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4038 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4039 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4040 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4041 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4042 | builder.setPrecision(result[0], precision); |
| 4043 | |
| 4044 | // If needed, add a conversion constructor to the proper size. |
| 4045 | if (components != node->getType().getVectorSize()) |
| 4046 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4047 | |
| 4048 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4049 | #ifdef AMD_EXTENSIONS |
| 4050 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 4051 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4052 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4053 | #endif |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4054 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4055 | // Push the texel value before the operands |
| 4056 | #ifdef AMD_EXTENSIONS |
| 4057 | if (sampler.ms || cracked.lod) { |
| 4058 | #else |
| 4059 | if (sampler.ms) { |
| 4060 | #endif |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4061 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4062 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4063 | } else { |
| 4064 | spv::IdImmediate texel = { true, *opIt }; |
| 4065 | operands.push_back(texel); |
| 4066 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4067 | |
| 4068 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 4069 | if (sampler.ms) { |
| 4070 | mask = mask | spv::ImageOperandsSampleMask; |
| 4071 | } |
| 4072 | #ifdef AMD_EXTENSIONS |
| 4073 | if (cracked.lod) { |
| 4074 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4075 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4076 | mask = mask | spv::ImageOperandsLodMask; |
| 4077 | } |
| 4078 | #endif |
| 4079 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4080 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
| 4081 | if (mask) { |
| 4082 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4083 | operands.push_back(imageOperands); |
| 4084 | } |
| 4085 | if (mask & spv::ImageOperandsSampleMask) { |
| 4086 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4087 | operands.push_back(imageOperand); |
| 4088 | } |
| 4089 | #ifdef AMD_EXTENSIONS |
| 4090 | if (mask & spv::ImageOperandsLodMask) { |
| 4091 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4092 | operands.push_back(imageOperand); |
| 4093 | } |
| 4094 | #endif |
| 4095 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
| 4096 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4097 | operands.push_back(imageOperand); |
| 4098 | } |
| 4099 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4100 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4101 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4102 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4103 | return spv::NoResult; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4104 | #ifdef AMD_EXTENSIONS |
| 4105 | } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 4106 | #else |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4107 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4108 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4109 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4110 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4111 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4112 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4113 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4114 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4115 | mask = mask | spv::ImageOperandsSampleMask; |
| 4116 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4117 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4118 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4119 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4120 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4121 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4122 | mask = mask | spv::ImageOperandsLodMask; |
| 4123 | } |
| 4124 | #endif |
| 4125 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4126 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 4127 | if (mask) { |
| 4128 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4129 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4130 | } |
| 4131 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4132 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4133 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4134 | } |
| 4135 | #ifdef AMD_EXTENSIONS |
| 4136 | if (mask & spv::ImageOperandsLodMask) { |
| 4137 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4138 | operands.push_back(imageOperand); |
| 4139 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4140 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4141 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4142 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4143 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | // Create the return type that was a special structure |
| 4147 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4148 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4149 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 4150 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 4151 | |
| 4152 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 4153 | |
| 4154 | // Decode the return type |
| 4155 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 4156 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 4157 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 4158 | // Process image atomic operations |
| 4159 | |
| 4160 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 4161 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4162 | // For non-MS, the sample value should be 0 |
| 4163 | spv::IdImmediate sample = { true, sampler.ms ? *(opIt++) : builder.makeUintConstant(0) }; |
| 4164 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4165 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4166 | spv::Id resultTypeId; |
| 4167 | // imageAtomicStore has a void return type so base the pointer type on |
| 4168 | // the type of the value operand. |
| 4169 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 4170 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word)); |
| 4171 | } else { |
| 4172 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 4173 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4174 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4175 | |
| 4176 | std::vector<spv::Id> operands; |
| 4177 | operands.push_back(pointer); |
| 4178 | for (; opIt != arguments.end(); ++opIt) |
| 4179 | operands.push_back(*opIt); |
| 4180 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4181 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4182 | } |
| 4183 | } |
| 4184 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4185 | #ifdef AMD_EXTENSIONS |
| 4186 | // Check for fragment mask functions other than queries |
| 4187 | if (cracked.fragMask) { |
| 4188 | assert(sampler.ms); |
| 4189 | |
| 4190 | auto opIt = arguments.begin(); |
| 4191 | std::vector<spv::Id> operands; |
| 4192 | |
| 4193 | // Extract the image if necessary |
| 4194 | if (builder.isSampledImage(params.sampler)) |
| 4195 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4196 | |
| 4197 | operands.push_back(params.sampler); |
| 4198 | ++opIt; |
| 4199 | |
| 4200 | if (sampler.isSubpass()) { |
| 4201 | // add on the (0,0) coordinate |
| 4202 | spv::Id zero = builder.makeIntConstant(0); |
| 4203 | std::vector<spv::Id> comps; |
| 4204 | comps.push_back(zero); |
| 4205 | comps.push_back(zero); |
| 4206 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 4207 | } |
| 4208 | |
| 4209 | for (; opIt != arguments.end(); ++opIt) |
| 4210 | operands.push_back(*opIt); |
| 4211 | |
| 4212 | spv::Op fragMaskOp = spv::OpNop; |
| 4213 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 4214 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 4215 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 4216 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 4217 | |
| 4218 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 4219 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 4220 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 4221 | } |
| 4222 | #endif |
| 4223 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4224 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4225 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4226 | #ifdef NV_EXTENSIONS |
| 4227 | bool imageFootprint = node->isImageFootprint(); |
| 4228 | #endif |
| 4229 | |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4230 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 4231 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4232 | // check for bias argument |
| 4233 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4234 | #ifdef AMD_EXTENSIONS |
| 4235 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 4236 | #else |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4237 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4238 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4239 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4240 | #ifdef AMD_EXTENSIONS |
| 4241 | if (cracked.gather) |
| 4242 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4243 | |
| 4244 | if (f16ShadowCompare) |
| 4245 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4246 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4247 | if (cracked.offset) |
| 4248 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4249 | #ifdef AMD_EXTENSIONS |
| 4250 | else if (cracked.offsets) |
| 4251 | ++nonBiasArgCount; |
| 4252 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4253 | if (cracked.grad) |
| 4254 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4255 | if (cracked.lodClamp) |
| 4256 | ++nonBiasArgCount; |
| 4257 | if (sparse) |
| 4258 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4259 | #ifdef NV_EXTENSIONS |
| 4260 | if (imageFootprint) |
| 4261 | //Following three extra arguments |
| 4262 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4263 | nonBiasArgCount += 3; |
| 4264 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4265 | if ((int)arguments.size() > nonBiasArgCount) |
| 4266 | bias = true; |
| 4267 | } |
| 4268 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 4269 | // See if the sampler param should really be just the SPV image part |
| 4270 | if (cracked.fetch) { |
| 4271 | // a fetch needs to have the image extracted first |
| 4272 | if (builder.isSampledImage(params.sampler)) |
| 4273 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4274 | } |
| 4275 | |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4276 | #ifdef AMD_EXTENSIONS |
| 4277 | if (cracked.gather) { |
| 4278 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 4279 | if (bias || cracked.lod || |
| 4280 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 4281 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 4282 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4283 | } |
| 4284 | } |
| 4285 | #endif |
| 4286 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4287 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4288 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4289 | params.coords = arguments[1]; |
| 4290 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4291 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4292 | |
| 4293 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4294 | #ifdef AMD_EXTENSIONS |
| 4295 | if (cubeCompare || f16ShadowCompare) { |
| 4296 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4297 | if (cubeCompare) { |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4298 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4299 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4300 | ++extraArgs; |
| 4301 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4302 | params.Dref = arguments[2]; |
| 4303 | ++extraArgs; |
| 4304 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4305 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4306 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4307 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4308 | 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] | 4309 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4310 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 4311 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4312 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 4313 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4314 | |
| 4315 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4316 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4317 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4318 | ++extraArgs; |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame] | 4319 | } else if (glslangIntermediate->getStage() != EShLangFragment |
| 4320 | #ifdef NV_EXTENSIONS |
| 4321 | // NV_compute_shader_derivatives layout qualifiers allow for implicit LODs |
| 4322 | && !(glslangIntermediate->getStage() == EShLangCompute && |
| 4323 | (glslangIntermediate->getLayoutDerivativeModeNone() != glslang::LayoutDerivativeNone)) |
| 4324 | #endif |
| 4325 | ) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4326 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 4327 | noImplicitLod = true; |
| 4328 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4329 | |
| 4330 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4331 | if (sampler.ms) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4332 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4333 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4334 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4335 | |
| 4336 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4337 | if (cracked.grad) { |
| 4338 | params.gradX = arguments[2 + extraArgs]; |
| 4339 | params.gradY = arguments[3 + extraArgs]; |
| 4340 | extraArgs += 2; |
| 4341 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4342 | |
| 4343 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4344 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4345 | params.offset = arguments[2 + extraArgs]; |
| 4346 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4347 | } else if (cracked.offsets) { |
| 4348 | params.offsets = arguments[2 + extraArgs]; |
| 4349 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4350 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4351 | |
| 4352 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4353 | if (cracked.lodClamp) { |
| 4354 | params.lodClamp = arguments[2 + extraArgs]; |
| 4355 | ++extraArgs; |
| 4356 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4357 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4358 | if (sparse) { |
| 4359 | params.texelOut = arguments[2 + extraArgs]; |
| 4360 | ++extraArgs; |
| 4361 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4362 | |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4363 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4364 | if (cracked.gather && ! sampler.shadow) { |
| 4365 | // default component is 0, if missing, otherwise an argument |
| 4366 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4367 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4368 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4369 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4370 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4371 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4372 | #ifdef NV_EXTENSIONS |
| 4373 | spv::Id resultStruct = spv::NoResult; |
| 4374 | if (imageFootprint) { |
| 4375 | //Following three extra arguments |
| 4376 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4377 | params.granularity = arguments[2 + extraArgs]; |
| 4378 | params.coarse = arguments[3 + extraArgs]; |
| 4379 | resultStruct = arguments[4 + extraArgs]; |
| 4380 | extraArgs += 3; |
| 4381 | } |
| 4382 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4383 | // bias |
| 4384 | if (bias) { |
| 4385 | params.bias = arguments[2 + extraArgs]; |
| 4386 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4387 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4388 | |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4389 | #ifdef NV_EXTENSIONS |
| 4390 | if (imageFootprint) { |
| 4391 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 4392 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 4393 | |
| 4394 | |
| 4395 | //resultStructType(OpenGL type) contains 5 elements: |
| 4396 | //struct gl_TextureFootprint2DNV { |
| 4397 | // uvec2 anchor; |
| 4398 | // uvec2 offset; |
| 4399 | // uvec2 mask; |
| 4400 | // uint lod; |
| 4401 | // uint granularity; |
| 4402 | //}; |
| 4403 | //or |
| 4404 | //struct gl_TextureFootprint3DNV { |
| 4405 | // uvec3 anchor; |
| 4406 | // uvec3 offset; |
| 4407 | // uvec2 mask; |
| 4408 | // uint lod; |
| 4409 | // uint granularity; |
| 4410 | //}; |
| 4411 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 4412 | assert(builder.isStructType(resultStructType)); |
| 4413 | |
| 4414 | //resType (SPIR-V type) contains 6 elements: |
| 4415 | //Member 0 must be a Boolean type scalar(LOD), |
| 4416 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 4417 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 4418 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 4419 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 4420 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 4421 | std::vector<spv::Id> members; |
| 4422 | members.push_back(resultType()); |
| 4423 | for (int i = 0; i < 5; i++) { |
| 4424 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 4425 | } |
| 4426 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 4427 | |
| 4428 | //call ImageFootprintNV |
| 4429 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params); |
| 4430 | |
| 4431 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 4432 | for (int i = 0; i < 5; i++) { |
| 4433 | builder.clearAccessChain(); |
| 4434 | builder.setAccessChainLValue(resultStruct); |
| 4435 | |
| 4436 | //Accessing to a struct we created, no coherent flag is set |
| 4437 | spv::Builder::AccessChain::CoherentFlags flags; |
| 4438 | flags.clear(); |
| 4439 | |
| 4440 | builder.accessChainPush(builder.makeIntConstant(i), flags); |
| 4441 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1)); |
| 4442 | } |
| 4443 | return builder.createCompositeExtract(res, resultType(), 0); |
| 4444 | } |
| 4445 | #endif |
| 4446 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4447 | // projective component (might not to move) |
| 4448 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 4449 | // are divided by the last component of P." |
| 4450 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 4451 | // unused components will appear after all used components." |
| 4452 | if (cracked.proj) { |
| 4453 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 4454 | int projTargetComp; |
| 4455 | switch (sampler.dim) { |
| 4456 | case glslang::Esd1D: projTargetComp = 1; break; |
| 4457 | case glslang::Esd2D: projTargetComp = 2; break; |
| 4458 | case glslang::EsdRect: projTargetComp = 2; break; |
| 4459 | default: projTargetComp = projSourceComp; break; |
| 4460 | } |
| 4461 | // copy the projective coordinate if we have to |
| 4462 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 4463 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4464 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 4465 | projSourceComp); |
| 4466 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 4467 | builder.getTypeId(params.coords), projTargetComp); |
| 4468 | } |
| 4469 | } |
| 4470 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4471 | // nonprivate |
| 4472 | if (imageType.getQualifier().nonprivate) { |
| 4473 | params.nonprivate = true; |
| 4474 | } |
| 4475 | |
| 4476 | // volatile |
| 4477 | if (imageType.getQualifier().volatil) { |
| 4478 | params.volatil = true; |
| 4479 | } |
| 4480 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4481 | std::vector<spv::Id> result( 1, |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4482 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4483 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4484 | |
| 4485 | if (components != node->getType().getVectorSize()) |
| 4486 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4487 | |
| 4488 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 4492 | { |
| 4493 | // Grab the function's pointer from the previously created function |
| 4494 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 4495 | if (! function) |
| 4496 | return 0; |
| 4497 | |
| 4498 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 4499 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 4500 | |
| 4501 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 4502 | // |
| 4503 | // These imply we need a four step process: |
| 4504 | // 1. Evaluate the arguments |
| 4505 | // 2. Allocate and make copies of in, out, and inout arguments |
| 4506 | // 3. Make the call |
| 4507 | // 4. Copy back the results |
| 4508 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4509 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4510 | std::vector<spv::Builder::AccessChain> lValues; |
| 4511 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4512 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4513 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4514 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4515 | // build l-value |
| 4516 | builder.clearAccessChain(); |
| 4517 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4518 | // 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] | 4519 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4520 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4521 | // save l-value |
| 4522 | lValues.push_back(builder.getAccessChain()); |
| 4523 | } else { |
| 4524 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4525 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 4530 | // copy the original into that space. |
| 4531 | // |
| 4532 | // Also, build up the list of actual arguments to pass in for the call |
| 4533 | int lValueCount = 0; |
| 4534 | int rValueCount = 0; |
| 4535 | std::vector<spv::Id> spvArgs; |
| 4536 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 4537 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4538 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 4539 | builder.setAccessChain(lValues[lValueCount]); |
| 4540 | arg = builder.accessChainGetLValue(); |
| 4541 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4542 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4543 | // need space to hold the copy |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4544 | arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4545 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 4546 | // need to copy the input into output space |
| 4547 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4548 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4549 | builder.clearAccessChain(); |
| 4550 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4551 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4552 | } |
| 4553 | ++lValueCount; |
| 4554 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4555 | // process r-value, which involves a copy for a type mismatch |
| 4556 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) { |
| 4557 | spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg"); |
| 4558 | builder.clearAccessChain(); |
| 4559 | builder.setAccessChainLValue(argCopy); |
| 4560 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 4561 | arg = builder.createLoad(argCopy); |
| 4562 | } else |
| 4563 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4564 | ++rValueCount; |
| 4565 | } |
| 4566 | spvArgs.push_back(arg); |
| 4567 | } |
| 4568 | |
| 4569 | // 3. Make the call. |
| 4570 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4571 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4572 | |
| 4573 | // 4. Copy back out an "out" arguments. |
| 4574 | lValueCount = 0; |
| 4575 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4576 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4577 | ++lValueCount; |
| 4578 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4579 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 4580 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 4581 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4582 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4583 | } |
| 4584 | ++lValueCount; |
| 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | return result; |
| 4589 | } |
| 4590 | |
| 4591 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4592 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4593 | spv::Id typeId, spv::Id left, spv::Id right, |
| 4594 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 4595 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4596 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 4597 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4598 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4599 | |
| 4600 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4601 | 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] | 4602 | bool comparison = false; |
| 4603 | |
| 4604 | switch (op) { |
| 4605 | case glslang::EOpAdd: |
| 4606 | case glslang::EOpAddAssign: |
| 4607 | if (isFloat) |
| 4608 | binOp = spv::OpFAdd; |
| 4609 | else |
| 4610 | binOp = spv::OpIAdd; |
| 4611 | break; |
| 4612 | case glslang::EOpSub: |
| 4613 | case glslang::EOpSubAssign: |
| 4614 | if (isFloat) |
| 4615 | binOp = spv::OpFSub; |
| 4616 | else |
| 4617 | binOp = spv::OpISub; |
| 4618 | break; |
| 4619 | case glslang::EOpMul: |
| 4620 | case glslang::EOpMulAssign: |
| 4621 | if (isFloat) |
| 4622 | binOp = spv::OpFMul; |
| 4623 | else |
| 4624 | binOp = spv::OpIMul; |
| 4625 | break; |
| 4626 | case glslang::EOpVectorTimesScalar: |
| 4627 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 4628 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4629 | if (builder.isVector(right)) |
| 4630 | std::swap(left, right); |
| 4631 | assert(builder.isScalar(right)); |
| 4632 | needMatchingVectors = false; |
| 4633 | binOp = spv::OpVectorTimesScalar; |
| 4634 | } else |
| 4635 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4636 | break; |
| 4637 | case glslang::EOpVectorTimesMatrix: |
| 4638 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4639 | binOp = spv::OpVectorTimesMatrix; |
| 4640 | break; |
| 4641 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4642 | binOp = spv::OpMatrixTimesVector; |
| 4643 | break; |
| 4644 | case glslang::EOpMatrixTimesScalar: |
| 4645 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4646 | binOp = spv::OpMatrixTimesScalar; |
| 4647 | break; |
| 4648 | case glslang::EOpMatrixTimesMatrix: |
| 4649 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4650 | binOp = spv::OpMatrixTimesMatrix; |
| 4651 | break; |
| 4652 | case glslang::EOpOuterProduct: |
| 4653 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4654 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4655 | break; |
| 4656 | |
| 4657 | case glslang::EOpDiv: |
| 4658 | case glslang::EOpDivAssign: |
| 4659 | if (isFloat) |
| 4660 | binOp = spv::OpFDiv; |
| 4661 | else if (isUnsigned) |
| 4662 | binOp = spv::OpUDiv; |
| 4663 | else |
| 4664 | binOp = spv::OpSDiv; |
| 4665 | break; |
| 4666 | case glslang::EOpMod: |
| 4667 | case glslang::EOpModAssign: |
| 4668 | if (isFloat) |
| 4669 | binOp = spv::OpFMod; |
| 4670 | else if (isUnsigned) |
| 4671 | binOp = spv::OpUMod; |
| 4672 | else |
| 4673 | binOp = spv::OpSMod; |
| 4674 | break; |
| 4675 | case glslang::EOpRightShift: |
| 4676 | case glslang::EOpRightShiftAssign: |
| 4677 | if (isUnsigned) |
| 4678 | binOp = spv::OpShiftRightLogical; |
| 4679 | else |
| 4680 | binOp = spv::OpShiftRightArithmetic; |
| 4681 | break; |
| 4682 | case glslang::EOpLeftShift: |
| 4683 | case glslang::EOpLeftShiftAssign: |
| 4684 | binOp = spv::OpShiftLeftLogical; |
| 4685 | break; |
| 4686 | case glslang::EOpAnd: |
| 4687 | case glslang::EOpAndAssign: |
| 4688 | binOp = spv::OpBitwiseAnd; |
| 4689 | break; |
| 4690 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4691 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4692 | binOp = spv::OpLogicalAnd; |
| 4693 | break; |
| 4694 | case glslang::EOpInclusiveOr: |
| 4695 | case glslang::EOpInclusiveOrAssign: |
| 4696 | binOp = spv::OpBitwiseOr; |
| 4697 | break; |
| 4698 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4699 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4700 | binOp = spv::OpLogicalOr; |
| 4701 | break; |
| 4702 | case glslang::EOpExclusiveOr: |
| 4703 | case glslang::EOpExclusiveOrAssign: |
| 4704 | binOp = spv::OpBitwiseXor; |
| 4705 | break; |
| 4706 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4707 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4708 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4709 | break; |
| 4710 | |
| 4711 | case glslang::EOpLessThan: |
| 4712 | case glslang::EOpGreaterThan: |
| 4713 | case glslang::EOpLessThanEqual: |
| 4714 | case glslang::EOpGreaterThanEqual: |
| 4715 | case glslang::EOpEqual: |
| 4716 | case glslang::EOpNotEqual: |
| 4717 | case glslang::EOpVectorEqual: |
| 4718 | case glslang::EOpVectorNotEqual: |
| 4719 | comparison = true; |
| 4720 | break; |
| 4721 | default: |
| 4722 | break; |
| 4723 | } |
| 4724 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4725 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4726 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4727 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4728 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4729 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4730 | |
| 4731 | // 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] | 4732 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4733 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4734 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4735 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4736 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4737 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4738 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | if (! comparison) |
| 4742 | return 0; |
| 4743 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4744 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4745 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 4746 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4747 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 4748 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4749 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4750 | return result; |
| 4751 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4752 | |
| 4753 | switch (op) { |
| 4754 | case glslang::EOpLessThan: |
| 4755 | if (isFloat) |
| 4756 | binOp = spv::OpFOrdLessThan; |
| 4757 | else if (isUnsigned) |
| 4758 | binOp = spv::OpULessThan; |
| 4759 | else |
| 4760 | binOp = spv::OpSLessThan; |
| 4761 | break; |
| 4762 | case glslang::EOpGreaterThan: |
| 4763 | if (isFloat) |
| 4764 | binOp = spv::OpFOrdGreaterThan; |
| 4765 | else if (isUnsigned) |
| 4766 | binOp = spv::OpUGreaterThan; |
| 4767 | else |
| 4768 | binOp = spv::OpSGreaterThan; |
| 4769 | break; |
| 4770 | case glslang::EOpLessThanEqual: |
| 4771 | if (isFloat) |
| 4772 | binOp = spv::OpFOrdLessThanEqual; |
| 4773 | else if (isUnsigned) |
| 4774 | binOp = spv::OpULessThanEqual; |
| 4775 | else |
| 4776 | binOp = spv::OpSLessThanEqual; |
| 4777 | break; |
| 4778 | case glslang::EOpGreaterThanEqual: |
| 4779 | if (isFloat) |
| 4780 | binOp = spv::OpFOrdGreaterThanEqual; |
| 4781 | else if (isUnsigned) |
| 4782 | binOp = spv::OpUGreaterThanEqual; |
| 4783 | else |
| 4784 | binOp = spv::OpSGreaterThanEqual; |
| 4785 | break; |
| 4786 | case glslang::EOpEqual: |
| 4787 | case glslang::EOpVectorEqual: |
| 4788 | if (isFloat) |
| 4789 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4790 | else if (isBool) |
| 4791 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4792 | else |
| 4793 | binOp = spv::OpIEqual; |
| 4794 | break; |
| 4795 | case glslang::EOpNotEqual: |
| 4796 | case glslang::EOpVectorNotEqual: |
| 4797 | if (isFloat) |
| 4798 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4799 | else if (isBool) |
| 4800 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4801 | else |
| 4802 | binOp = spv::OpINotEqual; |
| 4803 | break; |
| 4804 | default: |
| 4805 | break; |
| 4806 | } |
| 4807 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4808 | if (binOp != spv::OpNop) { |
| 4809 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4810 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4811 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4812 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4813 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4814 | |
| 4815 | return 0; |
| 4816 | } |
| 4817 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4818 | // |
| 4819 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 4820 | // These can be any of: |
| 4821 | // |
| 4822 | // matrix * scalar |
| 4823 | // scalar * matrix |
| 4824 | // matrix * matrix linear algebraic |
| 4825 | // matrix * vector |
| 4826 | // vector * matrix |
| 4827 | // matrix * matrix componentwise |
| 4828 | // matrix op matrix op in {+, -, /} |
| 4829 | // matrix op scalar op in {+, -, /} |
| 4830 | // scalar op matrix op in {+, -, /} |
| 4831 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4832 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 4833 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4834 | { |
| 4835 | bool firstClass = true; |
| 4836 | |
| 4837 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 4838 | switch (op) { |
| 4839 | case spv::OpFDiv: |
| 4840 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 4841 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 4842 | spv::Id resultType = builder.getTypeId(right); |
| 4843 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4844 | op = spv::OpMatrixTimesScalar; |
| 4845 | } else |
| 4846 | firstClass = false; |
| 4847 | break; |
| 4848 | case spv::OpMatrixTimesScalar: |
| 4849 | if (builder.isMatrix(right)) |
| 4850 | std::swap(left, right); |
| 4851 | assert(builder.isScalar(right)); |
| 4852 | break; |
| 4853 | case spv::OpVectorTimesMatrix: |
| 4854 | assert(builder.isVector(left)); |
| 4855 | assert(builder.isMatrix(right)); |
| 4856 | break; |
| 4857 | case spv::OpMatrixTimesVector: |
| 4858 | assert(builder.isMatrix(left)); |
| 4859 | assert(builder.isVector(right)); |
| 4860 | break; |
| 4861 | case spv::OpMatrixTimesMatrix: |
| 4862 | assert(builder.isMatrix(left)); |
| 4863 | assert(builder.isMatrix(right)); |
| 4864 | break; |
| 4865 | default: |
| 4866 | firstClass = false; |
| 4867 | break; |
| 4868 | } |
| 4869 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4870 | if (firstClass) { |
| 4871 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4872 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4873 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4874 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4875 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4876 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4877 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4878 | // The result type of all of them is the same type as the (a) matrix operand. |
| 4879 | // The algorithm is to: |
| 4880 | // - break the matrix(es) into vectors |
| 4881 | // - smear any scalar to a vector |
| 4882 | // - do vector operations |
| 4883 | // - make a matrix out the vector results |
| 4884 | switch (op) { |
| 4885 | case spv::OpFAdd: |
| 4886 | case spv::OpFSub: |
| 4887 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4888 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4889 | case spv::OpFMul: |
| 4890 | { |
| 4891 | // one time set up... |
| 4892 | bool leftMat = builder.isMatrix(left); |
| 4893 | bool rightMat = builder.isMatrix(right); |
| 4894 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 4895 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 4896 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 4897 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 4898 | std::vector<spv::Id> results; |
| 4899 | spv::Id smearVec = spv::NoResult; |
| 4900 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4901 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4902 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4903 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4904 | |
| 4905 | // do each vector op |
| 4906 | for (unsigned int c = 0; c < numCols; ++c) { |
| 4907 | std::vector<unsigned int> indexes; |
| 4908 | indexes.push_back(c); |
| 4909 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 4910 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4911 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4912 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4913 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4914 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4918 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4919 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4920 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4921 | } |
| 4922 | default: |
| 4923 | assert(0); |
| 4924 | return spv::NoResult; |
| 4925 | } |
| 4926 | } |
| 4927 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4928 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
| 4929 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4930 | { |
| 4931 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4932 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4933 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4934 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 4935 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4936 | |
| 4937 | switch (op) { |
| 4938 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4939 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4940 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4941 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4942 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4943 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4944 | unaryOp = spv::OpSNegate; |
| 4945 | break; |
| 4946 | |
| 4947 | case glslang::EOpLogicalNot: |
| 4948 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4949 | unaryOp = spv::OpLogicalNot; |
| 4950 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4951 | case glslang::EOpBitwiseNot: |
| 4952 | unaryOp = spv::OpNot; |
| 4953 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4954 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4955 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4956 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4957 | break; |
| 4958 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4959 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4960 | break; |
| 4961 | case glslang::EOpTranspose: |
| 4962 | unaryOp = spv::OpTranspose; |
| 4963 | break; |
| 4964 | |
| 4965 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4966 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4967 | break; |
| 4968 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4969 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4970 | break; |
| 4971 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4972 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4973 | break; |
| 4974 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4975 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4976 | break; |
| 4977 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4978 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4979 | break; |
| 4980 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4981 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4982 | break; |
| 4983 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4984 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4985 | break; |
| 4986 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4987 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4988 | break; |
| 4989 | |
| 4990 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4991 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4992 | break; |
| 4993 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4994 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4995 | break; |
| 4996 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4997 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4998 | break; |
| 4999 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5000 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5001 | break; |
| 5002 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5003 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5004 | break; |
| 5005 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5006 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5007 | break; |
| 5008 | |
| 5009 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5010 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5011 | break; |
| 5012 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5013 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5014 | break; |
| 5015 | |
| 5016 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5017 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5018 | break; |
| 5019 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5020 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5021 | break; |
| 5022 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5023 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5024 | break; |
| 5025 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5026 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5027 | break; |
| 5028 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5029 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5030 | break; |
| 5031 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5032 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5033 | break; |
| 5034 | |
| 5035 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5036 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5037 | break; |
| 5038 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5039 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5040 | break; |
| 5041 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5042 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5043 | break; |
| 5044 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5045 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5046 | break; |
| 5047 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5048 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5049 | break; |
| 5050 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5051 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5052 | break; |
| 5053 | |
| 5054 | case glslang::EOpIsNan: |
| 5055 | unaryOp = spv::OpIsNan; |
| 5056 | break; |
| 5057 | case glslang::EOpIsInf: |
| 5058 | unaryOp = spv::OpIsInf; |
| 5059 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5060 | case glslang::EOpIsFinite: |
| 5061 | unaryOp = spv::OpIsFinite; |
| 5062 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5063 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5064 | case glslang::EOpFloatBitsToInt: |
| 5065 | case glslang::EOpFloatBitsToUint: |
| 5066 | case glslang::EOpIntBitsToFloat: |
| 5067 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5068 | case glslang::EOpDoubleBitsToInt64: |
| 5069 | case glslang::EOpDoubleBitsToUint64: |
| 5070 | case glslang::EOpInt64BitsToDouble: |
| 5071 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5072 | case glslang::EOpFloat16BitsToInt16: |
| 5073 | case glslang::EOpFloat16BitsToUint16: |
| 5074 | case glslang::EOpInt16BitsToFloat16: |
| 5075 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5076 | unaryOp = spv::OpBitcast; |
| 5077 | break; |
| 5078 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5079 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5080 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5081 | break; |
| 5082 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5083 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5084 | break; |
| 5085 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5086 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5087 | break; |
| 5088 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5089 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5090 | break; |
| 5091 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5092 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5093 | break; |
| 5094 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5095 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5096 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5097 | case glslang::EOpPackSnorm4x8: |
| 5098 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 5099 | break; |
| 5100 | case glslang::EOpUnpackSnorm4x8: |
| 5101 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5102 | break; |
| 5103 | case glslang::EOpPackUnorm4x8: |
| 5104 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 5105 | break; |
| 5106 | case glslang::EOpUnpackUnorm4x8: |
| 5107 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5108 | break; |
| 5109 | case glslang::EOpPackDouble2x32: |
| 5110 | libCall = spv::GLSLstd450PackDouble2x32; |
| 5111 | break; |
| 5112 | case glslang::EOpUnpackDouble2x32: |
| 5113 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5114 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5115 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5116 | case glslang::EOpPackInt2x32: |
| 5117 | case glslang::EOpUnpackInt2x32: |
| 5118 | case glslang::EOpPackUint2x32: |
| 5119 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5120 | case glslang::EOpPack16: |
| 5121 | case glslang::EOpPack32: |
| 5122 | case glslang::EOpPack64: |
| 5123 | case glslang::EOpUnpack32: |
| 5124 | case glslang::EOpUnpack16: |
| 5125 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5126 | case glslang::EOpPackInt2x16: |
| 5127 | case glslang::EOpUnpackInt2x16: |
| 5128 | case glslang::EOpPackUint2x16: |
| 5129 | case glslang::EOpUnpackUint2x16: |
| 5130 | case glslang::EOpPackInt4x16: |
| 5131 | case glslang::EOpUnpackInt4x16: |
| 5132 | case glslang::EOpPackUint4x16: |
| 5133 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5134 | case glslang::EOpPackFloat2x16: |
| 5135 | case glslang::EOpUnpackFloat2x16: |
| 5136 | unaryOp = spv::OpBitcast; |
| 5137 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5138 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5139 | case glslang::EOpDPdx: |
| 5140 | unaryOp = spv::OpDPdx; |
| 5141 | break; |
| 5142 | case glslang::EOpDPdy: |
| 5143 | unaryOp = spv::OpDPdy; |
| 5144 | break; |
| 5145 | case glslang::EOpFwidth: |
| 5146 | unaryOp = spv::OpFwidth; |
| 5147 | break; |
| 5148 | case glslang::EOpDPdxFine: |
| 5149 | unaryOp = spv::OpDPdxFine; |
| 5150 | break; |
| 5151 | case glslang::EOpDPdyFine: |
| 5152 | unaryOp = spv::OpDPdyFine; |
| 5153 | break; |
| 5154 | case glslang::EOpFwidthFine: |
| 5155 | unaryOp = spv::OpFwidthFine; |
| 5156 | break; |
| 5157 | case glslang::EOpDPdxCoarse: |
| 5158 | unaryOp = spv::OpDPdxCoarse; |
| 5159 | break; |
| 5160 | case glslang::EOpDPdyCoarse: |
| 5161 | unaryOp = spv::OpDPdyCoarse; |
| 5162 | break; |
| 5163 | case glslang::EOpFwidthCoarse: |
| 5164 | unaryOp = spv::OpFwidthCoarse; |
| 5165 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5166 | case glslang::EOpInterpolateAtCentroid: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 5167 | #ifdef AMD_EXTENSIONS |
| 5168 | if (typeProxy == glslang::EbtFloat16) |
| 5169 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 5170 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5171 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 5172 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5173 | case glslang::EOpAny: |
| 5174 | unaryOp = spv::OpAny; |
| 5175 | break; |
| 5176 | case glslang::EOpAll: |
| 5177 | unaryOp = spv::OpAll; |
| 5178 | break; |
| 5179 | |
| 5180 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5181 | if (isFloat) |
| 5182 | libCall = spv::GLSLstd450FAbs; |
| 5183 | else |
| 5184 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5185 | break; |
| 5186 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5187 | if (isFloat) |
| 5188 | libCall = spv::GLSLstd450FSign; |
| 5189 | else |
| 5190 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5191 | break; |
| 5192 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5193 | case glslang::EOpAtomicCounterIncrement: |
| 5194 | case glslang::EOpAtomicCounterDecrement: |
| 5195 | case glslang::EOpAtomicCounter: |
| 5196 | { |
| 5197 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 5198 | std::vector<spv::Id> operands; |
| 5199 | operands.push_back(operand); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5200 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5201 | } |
| 5202 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5203 | case glslang::EOpBitFieldReverse: |
| 5204 | unaryOp = spv::OpBitReverse; |
| 5205 | break; |
| 5206 | case glslang::EOpBitCount: |
| 5207 | unaryOp = spv::OpBitCount; |
| 5208 | break; |
| 5209 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5210 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5211 | break; |
| 5212 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5213 | if (isUnsigned) |
| 5214 | libCall = spv::GLSLstd450FindUMsb; |
| 5215 | else |
| 5216 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5217 | break; |
| 5218 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5219 | case glslang::EOpBallot: |
| 5220 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5221 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5222 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5223 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5224 | #ifdef AMD_EXTENSIONS |
| 5225 | case glslang::EOpMinInvocations: |
| 5226 | case glslang::EOpMaxInvocations: |
| 5227 | case glslang::EOpAddInvocations: |
| 5228 | case glslang::EOpMinInvocationsNonUniform: |
| 5229 | case glslang::EOpMaxInvocationsNonUniform: |
| 5230 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5231 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5232 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5233 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5234 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5235 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5236 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5237 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5238 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5239 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5240 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5241 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5242 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5243 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5244 | { |
| 5245 | std::vector<spv::Id> operands; |
| 5246 | operands.push_back(operand); |
| 5247 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 5248 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5249 | case glslang::EOpSubgroupAll: |
| 5250 | case glslang::EOpSubgroupAny: |
| 5251 | case glslang::EOpSubgroupAllEqual: |
| 5252 | case glslang::EOpSubgroupBroadcastFirst: |
| 5253 | case glslang::EOpSubgroupBallot: |
| 5254 | case glslang::EOpSubgroupInverseBallot: |
| 5255 | case glslang::EOpSubgroupBallotBitCount: |
| 5256 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5257 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5258 | case glslang::EOpSubgroupBallotFindLSB: |
| 5259 | case glslang::EOpSubgroupBallotFindMSB: |
| 5260 | case glslang::EOpSubgroupAdd: |
| 5261 | case glslang::EOpSubgroupMul: |
| 5262 | case glslang::EOpSubgroupMin: |
| 5263 | case glslang::EOpSubgroupMax: |
| 5264 | case glslang::EOpSubgroupAnd: |
| 5265 | case glslang::EOpSubgroupOr: |
| 5266 | case glslang::EOpSubgroupXor: |
| 5267 | case glslang::EOpSubgroupInclusiveAdd: |
| 5268 | case glslang::EOpSubgroupInclusiveMul: |
| 5269 | case glslang::EOpSubgroupInclusiveMin: |
| 5270 | case glslang::EOpSubgroupInclusiveMax: |
| 5271 | case glslang::EOpSubgroupInclusiveAnd: |
| 5272 | case glslang::EOpSubgroupInclusiveOr: |
| 5273 | case glslang::EOpSubgroupInclusiveXor: |
| 5274 | case glslang::EOpSubgroupExclusiveAdd: |
| 5275 | case glslang::EOpSubgroupExclusiveMul: |
| 5276 | case glslang::EOpSubgroupExclusiveMin: |
| 5277 | case glslang::EOpSubgroupExclusiveMax: |
| 5278 | case glslang::EOpSubgroupExclusiveAnd: |
| 5279 | case glslang::EOpSubgroupExclusiveOr: |
| 5280 | case glslang::EOpSubgroupExclusiveXor: |
| 5281 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5282 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5283 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 5284 | std::vector<spv::Id> operands; |
| 5285 | operands.push_back(operand); |
| 5286 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 5287 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5288 | #ifdef AMD_EXTENSIONS |
| 5289 | case glslang::EOpMbcnt: |
| 5290 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5291 | libCall = spv::MbcntAMD; |
| 5292 | break; |
| 5293 | |
| 5294 | case glslang::EOpCubeFaceIndex: |
| 5295 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5296 | libCall = spv::CubeFaceIndexAMD; |
| 5297 | break; |
| 5298 | |
| 5299 | case glslang::EOpCubeFaceCoord: |
| 5300 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5301 | libCall = spv::CubeFaceCoordAMD; |
| 5302 | break; |
| 5303 | #endif |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5304 | #ifdef NV_EXTENSIONS |
| 5305 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5306 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 5307 | break; |
| 5308 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5309 | default: |
| 5310 | return 0; |
| 5311 | } |
| 5312 | |
| 5313 | spv::Id id; |
| 5314 | if (libCall >= 0) { |
| 5315 | std::vector<spv::Id> args; |
| 5316 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5317 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5318 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5319 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5320 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5321 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5322 | builder.addDecoration(id, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5323 | builder.addDecoration(id, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5324 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5325 | } |
| 5326 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5327 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5328 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5329 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5330 | { |
| 5331 | // Handle unary operations vector by vector. |
| 5332 | // The result type is the same type as the original type. |
| 5333 | // The algorithm is to: |
| 5334 | // - break the matrix into vectors |
| 5335 | // - apply the operation to each vector |
| 5336 | // - make a matrix out the vector results |
| 5337 | |
| 5338 | // get the types sorted out |
| 5339 | int numCols = builder.getNumColumns(operand); |
| 5340 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5341 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 5342 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5343 | std::vector<spv::Id> results; |
| 5344 | |
| 5345 | // do each vector op |
| 5346 | for (int c = 0; c < numCols; ++c) { |
| 5347 | std::vector<unsigned int> indexes; |
| 5348 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5349 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 5350 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5351 | builder.addDecoration(destVec, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5352 | builder.addDecoration(destVec, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5353 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5354 | } |
| 5355 | |
| 5356 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5357 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5358 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5359 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5360 | } |
| 5361 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5362 | // For converting integers where both the bitwidth and the signedness could |
| 5363 | // change, but only do the width change here. The caller is still responsible |
| 5364 | // for the signedness conversion. |
| 5365 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5366 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5367 | // Get the result type width, based on the type to convert to. |
| 5368 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5369 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5370 | case glslang::EOpConvInt16ToUint8: |
| 5371 | case glslang::EOpConvIntToUint8: |
| 5372 | case glslang::EOpConvInt64ToUint8: |
| 5373 | case glslang::EOpConvUint16ToInt8: |
| 5374 | case glslang::EOpConvUintToInt8: |
| 5375 | case glslang::EOpConvUint64ToInt8: |
| 5376 | width = 8; |
| 5377 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5378 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5379 | case glslang::EOpConvIntToUint16: |
| 5380 | case glslang::EOpConvInt64ToUint16: |
| 5381 | case glslang::EOpConvUint8ToInt16: |
| 5382 | case glslang::EOpConvUintToInt16: |
| 5383 | case glslang::EOpConvUint64ToInt16: |
| 5384 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5385 | break; |
| 5386 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5387 | case glslang::EOpConvInt16ToUint: |
| 5388 | case glslang::EOpConvInt64ToUint: |
| 5389 | case glslang::EOpConvUint8ToInt: |
| 5390 | case glslang::EOpConvUint16ToInt: |
| 5391 | case glslang::EOpConvUint64ToInt: |
| 5392 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5393 | break; |
| 5394 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5395 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5396 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5397 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5398 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5399 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5400 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5401 | break; |
| 5402 | |
| 5403 | default: |
| 5404 | assert(false && "Default missing"); |
| 5405 | break; |
| 5406 | } |
| 5407 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5408 | // Get the conversion operation and result type, |
| 5409 | // based on the target width, but the source type. |
| 5410 | spv::Id type = spv::NoType; |
| 5411 | spv::Op convOp = spv::OpNop; |
| 5412 | switch(op) { |
| 5413 | case glslang::EOpConvInt8ToUint16: |
| 5414 | case glslang::EOpConvInt8ToUint: |
| 5415 | case glslang::EOpConvInt8ToUint64: |
| 5416 | case glslang::EOpConvInt16ToUint8: |
| 5417 | case glslang::EOpConvInt16ToUint: |
| 5418 | case glslang::EOpConvInt16ToUint64: |
| 5419 | case glslang::EOpConvIntToUint8: |
| 5420 | case glslang::EOpConvIntToUint16: |
| 5421 | case glslang::EOpConvIntToUint64: |
| 5422 | case glslang::EOpConvInt64ToUint8: |
| 5423 | case glslang::EOpConvInt64ToUint16: |
| 5424 | case glslang::EOpConvInt64ToUint: |
| 5425 | convOp = spv::OpSConvert; |
| 5426 | type = builder.makeIntType(width); |
| 5427 | break; |
| 5428 | default: |
| 5429 | convOp = spv::OpUConvert; |
| 5430 | type = builder.makeUintType(width); |
| 5431 | break; |
| 5432 | } |
| 5433 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5434 | if (vectorSize > 0) |
| 5435 | type = builder.makeVectorType(type, vectorSize); |
| 5436 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5437 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5438 | } |
| 5439 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5440 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 5441 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5442 | { |
| 5443 | spv::Op convOp = spv::OpNop; |
| 5444 | spv::Id zero = 0; |
| 5445 | spv::Id one = 0; |
| 5446 | |
| 5447 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 5448 | |
| 5449 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5450 | case glslang::EOpConvInt8ToBool: |
| 5451 | case glslang::EOpConvUint8ToBool: |
| 5452 | zero = builder.makeUint8Constant(0); |
| 5453 | zero = makeSmearedConstant(zero, vectorSize); |
| 5454 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5455 | case glslang::EOpConvInt16ToBool: |
| 5456 | case glslang::EOpConvUint16ToBool: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5457 | zero = builder.makeUint16Constant(0); |
| 5458 | zero = makeSmearedConstant(zero, vectorSize); |
| 5459 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5460 | case glslang::EOpConvIntToBool: |
| 5461 | case glslang::EOpConvUintToBool: |
| 5462 | zero = builder.makeUintConstant(0); |
| 5463 | zero = makeSmearedConstant(zero, vectorSize); |
| 5464 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5465 | case glslang::EOpConvInt64ToBool: |
| 5466 | case glslang::EOpConvUint64ToBool: |
| 5467 | zero = builder.makeUint64Constant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5468 | zero = makeSmearedConstant(zero, vectorSize); |
| 5469 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5470 | |
| 5471 | case glslang::EOpConvFloatToBool: |
| 5472 | zero = builder.makeFloatConstant(0.0F); |
| 5473 | zero = makeSmearedConstant(zero, vectorSize); |
| 5474 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5475 | |
| 5476 | case glslang::EOpConvDoubleToBool: |
| 5477 | zero = builder.makeDoubleConstant(0.0); |
| 5478 | zero = makeSmearedConstant(zero, vectorSize); |
| 5479 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5480 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5481 | case glslang::EOpConvFloat16ToBool: |
| 5482 | zero = builder.makeFloat16Constant(0.0F); |
| 5483 | zero = makeSmearedConstant(zero, vectorSize); |
| 5484 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5485 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5486 | case glslang::EOpConvBoolToFloat: |
| 5487 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5488 | zero = builder.makeFloatConstant(0.0F); |
| 5489 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5490 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5491 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5492 | case glslang::EOpConvBoolToDouble: |
| 5493 | convOp = spv::OpSelect; |
| 5494 | zero = builder.makeDoubleConstant(0.0); |
| 5495 | one = builder.makeDoubleConstant(1.0); |
| 5496 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5497 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5498 | case glslang::EOpConvBoolToFloat16: |
| 5499 | convOp = spv::OpSelect; |
| 5500 | zero = builder.makeFloat16Constant(0.0F); |
| 5501 | one = builder.makeFloat16Constant(1.0F); |
| 5502 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5503 | |
| 5504 | case glslang::EOpConvBoolToInt8: |
| 5505 | zero = builder.makeInt8Constant(0); |
| 5506 | one = builder.makeInt8Constant(1); |
| 5507 | convOp = spv::OpSelect; |
| 5508 | break; |
| 5509 | |
| 5510 | case glslang::EOpConvBoolToUint8: |
| 5511 | zero = builder.makeUint8Constant(0); |
| 5512 | one = builder.makeUint8Constant(1); |
| 5513 | convOp = spv::OpSelect; |
| 5514 | break; |
| 5515 | |
| 5516 | case glslang::EOpConvBoolToInt16: |
| 5517 | zero = builder.makeInt16Constant(0); |
| 5518 | one = builder.makeInt16Constant(1); |
| 5519 | convOp = spv::OpSelect; |
| 5520 | break; |
| 5521 | |
| 5522 | case glslang::EOpConvBoolToUint16: |
| 5523 | zero = builder.makeUint16Constant(0); |
| 5524 | one = builder.makeUint16Constant(1); |
| 5525 | convOp = spv::OpSelect; |
| 5526 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5527 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5528 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5529 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5530 | if (op == glslang::EOpConvBoolToInt64) |
| 5531 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5532 | else |
| 5533 | zero = builder.makeIntConstant(0); |
| 5534 | |
| 5535 | if (op == glslang::EOpConvBoolToInt64) |
| 5536 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5537 | else |
| 5538 | one = builder.makeIntConstant(1); |
| 5539 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5540 | convOp = spv::OpSelect; |
| 5541 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5542 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5543 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5544 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5545 | if (op == glslang::EOpConvBoolToUint64) |
| 5546 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5547 | else |
| 5548 | zero = builder.makeUintConstant(0); |
| 5549 | |
| 5550 | if (op == glslang::EOpConvBoolToUint64) |
| 5551 | one = builder.makeUint64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5552 | else |
| 5553 | one = builder.makeUintConstant(1); |
| 5554 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5555 | convOp = spv::OpSelect; |
| 5556 | break; |
| 5557 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5558 | case glslang::EOpConvInt8ToFloat16: |
| 5559 | case glslang::EOpConvInt8ToFloat: |
| 5560 | case glslang::EOpConvInt8ToDouble: |
| 5561 | case glslang::EOpConvInt16ToFloat16: |
| 5562 | case glslang::EOpConvInt16ToFloat: |
| 5563 | case glslang::EOpConvInt16ToDouble: |
| 5564 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5565 | case glslang::EOpConvIntToFloat: |
| 5566 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5567 | case glslang::EOpConvInt64ToFloat: |
| 5568 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5569 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5570 | convOp = spv::OpConvertSToF; |
| 5571 | break; |
| 5572 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5573 | case glslang::EOpConvUint8ToFloat16: |
| 5574 | case glslang::EOpConvUint8ToFloat: |
| 5575 | case glslang::EOpConvUint8ToDouble: |
| 5576 | case glslang::EOpConvUint16ToFloat16: |
| 5577 | case glslang::EOpConvUint16ToFloat: |
| 5578 | case glslang::EOpConvUint16ToDouble: |
| 5579 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5580 | case glslang::EOpConvUintToFloat: |
| 5581 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5582 | case glslang::EOpConvUint64ToFloat: |
| 5583 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5584 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5585 | convOp = spv::OpConvertUToF; |
| 5586 | break; |
| 5587 | |
| 5588 | case glslang::EOpConvDoubleToFloat: |
| 5589 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5590 | case glslang::EOpConvDoubleToFloat16: |
| 5591 | case glslang::EOpConvFloat16ToDouble: |
| 5592 | case glslang::EOpConvFloatToFloat16: |
| 5593 | case glslang::EOpConvFloat16ToFloat: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5594 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 5595 | if (builder.isMatrixType(destType)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5596 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5597 | break; |
| 5598 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5599 | case glslang::EOpConvFloat16ToInt8: |
| 5600 | case glslang::EOpConvFloatToInt8: |
| 5601 | case glslang::EOpConvDoubleToInt8: |
| 5602 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5603 | case glslang::EOpConvFloatToInt16: |
| 5604 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5605 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5606 | case glslang::EOpConvFloatToInt: |
| 5607 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5608 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5609 | case glslang::EOpConvFloatToInt64: |
| 5610 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5611 | convOp = spv::OpConvertFToS; |
| 5612 | break; |
| 5613 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5614 | case glslang::EOpConvUint8ToInt8: |
| 5615 | case glslang::EOpConvInt8ToUint8: |
| 5616 | case glslang::EOpConvUint16ToInt16: |
| 5617 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5618 | case glslang::EOpConvUintToInt: |
| 5619 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5620 | case glslang::EOpConvUint64ToInt64: |
| 5621 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 5622 | if (builder.isInSpecConstCodeGenMode()) { |
| 5623 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5624 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 5625 | zero = builder.makeUint8Constant(0); |
| 5626 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5627 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5628 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 5629 | zero = builder.makeUint64Constant(0); |
| 5630 | } else { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5631 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5632 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 5633 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 5634 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5635 | // generating for OpSpecConstantOp instruction. |
| 5636 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5637 | } |
| 5638 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5639 | convOp = spv::OpBitcast; |
| 5640 | break; |
| 5641 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5642 | case glslang::EOpConvFloat16ToUint8: |
| 5643 | case glslang::EOpConvFloatToUint8: |
| 5644 | case glslang::EOpConvDoubleToUint8: |
| 5645 | case glslang::EOpConvFloat16ToUint16: |
| 5646 | case glslang::EOpConvFloatToUint16: |
| 5647 | case glslang::EOpConvDoubleToUint16: |
| 5648 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5649 | case glslang::EOpConvFloatToUint: |
| 5650 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5651 | case glslang::EOpConvFloatToUint64: |
| 5652 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5653 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5654 | convOp = spv::OpConvertFToU; |
| 5655 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5656 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5657 | case glslang::EOpConvInt8ToInt16: |
| 5658 | case glslang::EOpConvInt8ToInt: |
| 5659 | case glslang::EOpConvInt8ToInt64: |
| 5660 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5661 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5662 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5663 | case glslang::EOpConvIntToInt8: |
| 5664 | case glslang::EOpConvIntToInt16: |
| 5665 | case glslang::EOpConvIntToInt64: |
| 5666 | case glslang::EOpConvInt64ToInt8: |
| 5667 | case glslang::EOpConvInt64ToInt16: |
| 5668 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5669 | convOp = spv::OpSConvert; |
| 5670 | break; |
| 5671 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5672 | case glslang::EOpConvUint8ToUint16: |
| 5673 | case glslang::EOpConvUint8ToUint: |
| 5674 | case glslang::EOpConvUint8ToUint64: |
| 5675 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5676 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5677 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5678 | case glslang::EOpConvUintToUint8: |
| 5679 | case glslang::EOpConvUintToUint16: |
| 5680 | case glslang::EOpConvUintToUint64: |
| 5681 | case glslang::EOpConvUint64ToUint8: |
| 5682 | case glslang::EOpConvUint64ToUint16: |
| 5683 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5684 | convOp = spv::OpUConvert; |
| 5685 | break; |
| 5686 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5687 | case glslang::EOpConvInt8ToUint16: |
| 5688 | case glslang::EOpConvInt8ToUint: |
| 5689 | case glslang::EOpConvInt8ToUint64: |
| 5690 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5691 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5692 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5693 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5694 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5695 | case glslang::EOpConvIntToUint64: |
| 5696 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5697 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5698 | case glslang::EOpConvInt64ToUint: |
| 5699 | case glslang::EOpConvUint8ToInt16: |
| 5700 | case glslang::EOpConvUint8ToInt: |
| 5701 | case glslang::EOpConvUint8ToInt64: |
| 5702 | case glslang::EOpConvUint16ToInt8: |
| 5703 | case glslang::EOpConvUint16ToInt: |
| 5704 | case glslang::EOpConvUint16ToInt64: |
| 5705 | case glslang::EOpConvUintToInt8: |
| 5706 | case glslang::EOpConvUintToInt16: |
| 5707 | case glslang::EOpConvUintToInt64: |
| 5708 | case glslang::EOpConvUint64ToInt8: |
| 5709 | case glslang::EOpConvUint64ToInt16: |
| 5710 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5711 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5712 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5713 | |
| 5714 | if (builder.isInSpecConstCodeGenMode()) { |
| 5715 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5716 | switch(op) { |
| 5717 | case glslang::EOpConvInt16ToUint8: |
| 5718 | case glslang::EOpConvIntToUint8: |
| 5719 | case glslang::EOpConvInt64ToUint8: |
| 5720 | case glslang::EOpConvUint16ToInt8: |
| 5721 | case glslang::EOpConvUintToInt8: |
| 5722 | case glslang::EOpConvUint64ToInt8: |
| 5723 | zero = builder.makeUint8Constant(0); |
| 5724 | break; |
| 5725 | case glslang::EOpConvInt8ToUint16: |
| 5726 | case glslang::EOpConvIntToUint16: |
| 5727 | case glslang::EOpConvInt64ToUint16: |
| 5728 | case glslang::EOpConvUint8ToInt16: |
| 5729 | case glslang::EOpConvUintToInt16: |
| 5730 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5731 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5732 | break; |
| 5733 | case glslang::EOpConvInt8ToUint: |
| 5734 | case glslang::EOpConvInt16ToUint: |
| 5735 | case glslang::EOpConvInt64ToUint: |
| 5736 | case glslang::EOpConvUint8ToInt: |
| 5737 | case glslang::EOpConvUint16ToInt: |
| 5738 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5739 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5740 | break; |
| 5741 | case glslang::EOpConvInt8ToUint64: |
| 5742 | case glslang::EOpConvInt16ToUint64: |
| 5743 | case glslang::EOpConvIntToUint64: |
| 5744 | case glslang::EOpConvUint8ToInt64: |
| 5745 | case glslang::EOpConvUint16ToInt64: |
| 5746 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5747 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5748 | break; |
| 5749 | default: |
| 5750 | assert(false && "Default missing"); |
| 5751 | break; |
| 5752 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5753 | zero = makeSmearedConstant(zero, vectorSize); |
| 5754 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5755 | // generating for OpSpecConstantOp instruction. |
| 5756 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5757 | } |
| 5758 | // For normal run-time conversion instruction, use OpBitcast. |
| 5759 | convOp = spv::OpBitcast; |
| 5760 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5761 | default: |
| 5762 | break; |
| 5763 | } |
| 5764 | |
| 5765 | spv::Id result = 0; |
| 5766 | if (convOp == spv::OpNop) |
| 5767 | return result; |
| 5768 | |
| 5769 | if (convOp == spv::OpSelect) { |
| 5770 | zero = makeSmearedConstant(zero, vectorSize); |
| 5771 | one = makeSmearedConstant(one, vectorSize); |
| 5772 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 5773 | } else |
| 5774 | result = builder.createUnaryOp(convOp, destType, operand); |
| 5775 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5776 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5777 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5778 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 5782 | { |
| 5783 | if (vectorSize == 0) |
| 5784 | return constant; |
| 5785 | |
| 5786 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 5787 | std::vector<spv::Id> components; |
| 5788 | for (int c = 0; c < vectorSize; ++c) |
| 5789 | components.push_back(constant); |
| 5790 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 5791 | } |
| 5792 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5793 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5794 | 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] | 5795 | { |
| 5796 | spv::Op opCode = spv::OpNop; |
| 5797 | |
| 5798 | switch (op) { |
| 5799 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5800 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5801 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5802 | opCode = spv::OpAtomicIAdd; |
| 5803 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5804 | case glslang::EOpAtomicCounterSubtract: |
| 5805 | opCode = spv::OpAtomicISub; |
| 5806 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5807 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5808 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5809 | case glslang::EOpAtomicCounterMin: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5810 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5811 | break; |
| 5812 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5813 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5814 | case glslang::EOpAtomicCounterMax: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5815 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5816 | break; |
| 5817 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5818 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5819 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5820 | opCode = spv::OpAtomicAnd; |
| 5821 | break; |
| 5822 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5823 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5824 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5825 | opCode = spv::OpAtomicOr; |
| 5826 | break; |
| 5827 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5828 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5829 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5830 | opCode = spv::OpAtomicXor; |
| 5831 | break; |
| 5832 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5833 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5834 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5835 | opCode = spv::OpAtomicExchange; |
| 5836 | break; |
| 5837 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5838 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5839 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5840 | opCode = spv::OpAtomicCompareExchange; |
| 5841 | break; |
| 5842 | case glslang::EOpAtomicCounterIncrement: |
| 5843 | opCode = spv::OpAtomicIIncrement; |
| 5844 | break; |
| 5845 | case glslang::EOpAtomicCounterDecrement: |
| 5846 | opCode = spv::OpAtomicIDecrement; |
| 5847 | break; |
| 5848 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5849 | case glslang::EOpImageAtomicLoad: |
| 5850 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5851 | opCode = spv::OpAtomicLoad; |
| 5852 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5853 | case glslang::EOpAtomicStore: |
| 5854 | case glslang::EOpImageAtomicStore: |
| 5855 | opCode = spv::OpAtomicStore; |
| 5856 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5857 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5858 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5859 | break; |
| 5860 | } |
| 5861 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5862 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 5863 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 5864 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5865 | // Sort out the operands |
| 5866 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5867 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 5868 | // - compare-exchange swaps the value and comparator |
| 5869 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5870 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5871 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 5872 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 5873 | spv::Id scopeId; |
| 5874 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 5875 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 5876 | } else { |
| 5877 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 5878 | } |
| 5879 | // semantics default to relaxed |
| 5880 | spv::Id semanticsId = builder.makeUintConstant(spv::MemorySemanticsMaskNone); |
| 5881 | spv::Id semanticsId2 = semanticsId; |
| 5882 | |
| 5883 | pointerId = operands[0]; |
| 5884 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 5885 | // no additional operands |
| 5886 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 5887 | compareId = operands[1]; |
| 5888 | valueId = operands[2]; |
| 5889 | if (operands.size() > 3) { |
| 5890 | scopeId = operands[3]; |
| 5891 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 5892 | semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 5893 | } |
| 5894 | } else if (opCode == spv::OpAtomicLoad) { |
| 5895 | if (operands.size() > 1) { |
| 5896 | scopeId = operands[1]; |
| 5897 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 5898 | } |
| 5899 | } else { |
| 5900 | // atomic store or RMW |
| 5901 | valueId = operands[1]; |
| 5902 | if (operands.size() > 2) { |
| 5903 | scopeId = operands[2]; |
| 5904 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 5905 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5906 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5907 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5908 | // Check for capabilities |
| 5909 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
| 5910 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 5911 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 5912 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5913 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5914 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 5915 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 5916 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5917 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5918 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 5919 | spvAtomicOperands.push_back(pointerId); |
| 5920 | spvAtomicOperands.push_back(scopeId); |
| 5921 | spvAtomicOperands.push_back(semanticsId); |
| 5922 | if (opCode == spv::OpAtomicCompareExchange) { |
| 5923 | spvAtomicOperands.push_back(semanticsId2); |
| 5924 | spvAtomicOperands.push_back(valueId); |
| 5925 | spvAtomicOperands.push_back(compareId); |
| 5926 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 5927 | spvAtomicOperands.push_back(valueId); |
| 5928 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5929 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5930 | if (opCode == spv::OpAtomicStore) { |
| 5931 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 5932 | return 0; |
| 5933 | } else { |
| 5934 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 5935 | |
| 5936 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 5937 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 5938 | if (op == glslang::EOpAtomicCounterDecrement) |
| 5939 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 5940 | |
| 5941 | return resultId; |
| 5942 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5943 | } |
| 5944 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5945 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5946 | 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] | 5947 | { |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 5948 | #ifdef AMD_EXTENSIONS |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5949 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5950 | bool isFloat = isTypeFloat(typeProxy); |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 5951 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5952 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5953 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5954 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5955 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 5956 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5957 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 5958 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5959 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 5960 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5961 | } else if (op == glslang::EOpAnyInvocation || |
| 5962 | op == glslang::EOpAllInvocations || |
| 5963 | op == glslang::EOpAllInvocationsEqual) { |
| 5964 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 5965 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5966 | } else { |
| 5967 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 5968 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 5969 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 5970 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5971 | op == glslang::EOpAddInvocationsNonUniform || |
| 5972 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 5973 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 5974 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 5975 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 5976 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 5977 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 5978 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 5979 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5980 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5981 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5982 | switch (op) { |
| 5983 | case glslang::EOpMinInvocations: |
| 5984 | case glslang::EOpMaxInvocations: |
| 5985 | case glslang::EOpAddInvocations: |
| 5986 | case glslang::EOpMinInvocationsNonUniform: |
| 5987 | case glslang::EOpMaxInvocationsNonUniform: |
| 5988 | case glslang::EOpAddInvocationsNonUniform: |
| 5989 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5990 | break; |
| 5991 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5992 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5993 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5994 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5995 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5996 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5997 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5998 | break; |
| 5999 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6000 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6001 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6002 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6003 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6004 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6005 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6006 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 6007 | default: |
| 6008 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6009 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6010 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6011 | spvGroupOperands.push_back(scope); |
| 6012 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6013 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6014 | spvGroupOperands.push_back(groupOp); |
| 6015 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6016 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6017 | } |
| 6018 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6019 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6020 | spv::IdImmediate op = { true, *opIt }; |
| 6021 | spvGroupOperands.push_back(op); |
| 6022 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6023 | |
| 6024 | switch (op) { |
| 6025 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6026 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6027 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6028 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6029 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6030 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6031 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6032 | opCode = spv::OpSubgroupAllEqualKHR; |
| 6033 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6034 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6035 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6036 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6037 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6038 | break; |
| 6039 | case glslang::EOpReadFirstInvocation: |
| 6040 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6041 | break; |
| 6042 | case glslang::EOpBallot: |
| 6043 | { |
| 6044 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6045 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6046 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6047 | // |
| 6048 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6049 | // |
| 6050 | spv::Id uintType = builder.makeUintType(32); |
| 6051 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6052 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6053 | |
| 6054 | std::vector<spv::Id> components; |
| 6055 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6056 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6057 | |
| 6058 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6059 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6060 | builder.createCompositeConstruct(uvec2Type, components)); |
| 6061 | } |
| 6062 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6063 | #ifdef AMD_EXTENSIONS |
| 6064 | case glslang::EOpMinInvocations: |
| 6065 | case glslang::EOpMaxInvocations: |
| 6066 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6067 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6068 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6069 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6070 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6071 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6072 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6073 | if (op == glslang::EOpMinInvocations || |
| 6074 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 6075 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6076 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6077 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6078 | else { |
| 6079 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6080 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6081 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6082 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6083 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6084 | } else if (op == glslang::EOpMaxInvocations || |
| 6085 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 6086 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6087 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6088 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6089 | else { |
| 6090 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6091 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6092 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6093 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6094 | } |
| 6095 | } else { |
| 6096 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6097 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6098 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6099 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6100 | } |
| 6101 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6102 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6103 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6104 | |
| 6105 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6106 | case glslang::EOpMinInvocationsNonUniform: |
| 6107 | case glslang::EOpMaxInvocationsNonUniform: |
| 6108 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6109 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6110 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6111 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6112 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6113 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6114 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6115 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6116 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6117 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6118 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6119 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6120 | else { |
| 6121 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6122 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6123 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6124 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6125 | } |
| 6126 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6127 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 6128 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6129 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6130 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6131 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6132 | else { |
| 6133 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6134 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6135 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6136 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6137 | } |
| 6138 | } |
| 6139 | else { |
| 6140 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6141 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6142 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6143 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6144 | } |
| 6145 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6146 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6147 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6148 | |
| 6149 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6150 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6151 | default: |
| 6152 | logger->missingFunctionality("invocation operation"); |
| 6153 | return spv::NoResult; |
| 6154 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6155 | |
| 6156 | assert(opCode != spv::OpNop); |
| 6157 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6158 | } |
| 6159 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6160 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6161 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 6162 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6163 | { |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6164 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6165 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6166 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6167 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6168 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6169 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 6170 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 6171 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6172 | #else |
| 6173 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6174 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6175 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 6176 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6177 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6178 | |
| 6179 | // Handle group invocation operations scalar by scalar. |
| 6180 | // The result type is the same type as the original type. |
| 6181 | // The algorithm is to: |
| 6182 | // - break the vector into scalars |
| 6183 | // - apply the operation to each scalar |
| 6184 | // - make a vector out the scalar results |
| 6185 | |
| 6186 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6187 | int numComponents = builder.getNumComponents(operands[0]); |
| 6188 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6189 | std::vector<spv::Id> results; |
| 6190 | |
| 6191 | // do each scalar op |
| 6192 | for (int comp = 0; comp < numComponents; ++comp) { |
| 6193 | std::vector<unsigned int> indexes; |
| 6194 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6195 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 6196 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6197 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 6198 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6199 | spv::IdImmediate operand = { true, operands[1] }; |
| 6200 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6201 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6202 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6203 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6204 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6205 | spv::IdImmediate operand = { true, operands[1] }; |
| 6206 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6207 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6208 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6209 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6210 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6211 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6212 | spvGroupOperands.push_back(scalar); |
| 6213 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6214 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6215 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | // put the pieces together |
| 6219 | return builder.createCompositeConstruct(typeId, results); |
| 6220 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6221 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6222 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6223 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 6224 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6225 | { |
| 6226 | // Add the required capabilities. |
| 6227 | switch (op) { |
| 6228 | case glslang::EOpSubgroupElect: |
| 6229 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6230 | break; |
| 6231 | case glslang::EOpSubgroupAll: |
| 6232 | case glslang::EOpSubgroupAny: |
| 6233 | case glslang::EOpSubgroupAllEqual: |
| 6234 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6235 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 6236 | break; |
| 6237 | case glslang::EOpSubgroupBroadcast: |
| 6238 | case glslang::EOpSubgroupBroadcastFirst: |
| 6239 | case glslang::EOpSubgroupBallot: |
| 6240 | case glslang::EOpSubgroupInverseBallot: |
| 6241 | case glslang::EOpSubgroupBallotBitExtract: |
| 6242 | case glslang::EOpSubgroupBallotBitCount: |
| 6243 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6244 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6245 | case glslang::EOpSubgroupBallotFindLSB: |
| 6246 | case glslang::EOpSubgroupBallotFindMSB: |
| 6247 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6248 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 6249 | break; |
| 6250 | case glslang::EOpSubgroupShuffle: |
| 6251 | case glslang::EOpSubgroupShuffleXor: |
| 6252 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6253 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 6254 | break; |
| 6255 | case glslang::EOpSubgroupShuffleUp: |
| 6256 | case glslang::EOpSubgroupShuffleDown: |
| 6257 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6258 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 6259 | break; |
| 6260 | case glslang::EOpSubgroupAdd: |
| 6261 | case glslang::EOpSubgroupMul: |
| 6262 | case glslang::EOpSubgroupMin: |
| 6263 | case glslang::EOpSubgroupMax: |
| 6264 | case glslang::EOpSubgroupAnd: |
| 6265 | case glslang::EOpSubgroupOr: |
| 6266 | case glslang::EOpSubgroupXor: |
| 6267 | case glslang::EOpSubgroupInclusiveAdd: |
| 6268 | case glslang::EOpSubgroupInclusiveMul: |
| 6269 | case glslang::EOpSubgroupInclusiveMin: |
| 6270 | case glslang::EOpSubgroupInclusiveMax: |
| 6271 | case glslang::EOpSubgroupInclusiveAnd: |
| 6272 | case glslang::EOpSubgroupInclusiveOr: |
| 6273 | case glslang::EOpSubgroupInclusiveXor: |
| 6274 | case glslang::EOpSubgroupExclusiveAdd: |
| 6275 | case glslang::EOpSubgroupExclusiveMul: |
| 6276 | case glslang::EOpSubgroupExclusiveMin: |
| 6277 | case glslang::EOpSubgroupExclusiveMax: |
| 6278 | case glslang::EOpSubgroupExclusiveAnd: |
| 6279 | case glslang::EOpSubgroupExclusiveOr: |
| 6280 | case glslang::EOpSubgroupExclusiveXor: |
| 6281 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6282 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 6283 | break; |
| 6284 | case glslang::EOpSubgroupClusteredAdd: |
| 6285 | case glslang::EOpSubgroupClusteredMul: |
| 6286 | case glslang::EOpSubgroupClusteredMin: |
| 6287 | case glslang::EOpSubgroupClusteredMax: |
| 6288 | case glslang::EOpSubgroupClusteredAnd: |
| 6289 | case glslang::EOpSubgroupClusteredOr: |
| 6290 | case glslang::EOpSubgroupClusteredXor: |
| 6291 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6292 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 6293 | break; |
| 6294 | case glslang::EOpSubgroupQuadBroadcast: |
| 6295 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6296 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6297 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 6298 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6299 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 6300 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6301 | #ifdef NV_EXTENSIONS |
| 6302 | case glslang::EOpSubgroupPartitionedAdd: |
| 6303 | case glslang::EOpSubgroupPartitionedMul: |
| 6304 | case glslang::EOpSubgroupPartitionedMin: |
| 6305 | case glslang::EOpSubgroupPartitionedMax: |
| 6306 | case glslang::EOpSubgroupPartitionedAnd: |
| 6307 | case glslang::EOpSubgroupPartitionedOr: |
| 6308 | case glslang::EOpSubgroupPartitionedXor: |
| 6309 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6310 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6311 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6312 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6313 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6314 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6315 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6316 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6317 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6318 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6319 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6320 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6321 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6322 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6323 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 6324 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 6325 | break; |
| 6326 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6327 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6328 | } |
| 6329 | |
| 6330 | const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
| 6331 | const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
| 6332 | const bool isBool = typeProxy == glslang::EbtBool; |
| 6333 | |
| 6334 | spv::Op opCode = spv::OpNop; |
| 6335 | |
| 6336 | // Figure out which opcode to use. |
| 6337 | switch (op) { |
| 6338 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 6339 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 6340 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 6341 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 6342 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 6343 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 6344 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 6345 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 6346 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 6347 | case glslang::EOpSubgroupBallotBitCount: |
| 6348 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6349 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 6350 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 6351 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 6352 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 6353 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 6354 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 6355 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 6356 | case glslang::EOpSubgroupAdd: |
| 6357 | case glslang::EOpSubgroupInclusiveAdd: |
| 6358 | case glslang::EOpSubgroupExclusiveAdd: |
| 6359 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6360 | #ifdef NV_EXTENSIONS |
| 6361 | case glslang::EOpSubgroupPartitionedAdd: |
| 6362 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6363 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6364 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6365 | if (isFloat) { |
| 6366 | opCode = spv::OpGroupNonUniformFAdd; |
| 6367 | } else { |
| 6368 | opCode = spv::OpGroupNonUniformIAdd; |
| 6369 | } |
| 6370 | break; |
| 6371 | case glslang::EOpSubgroupMul: |
| 6372 | case glslang::EOpSubgroupInclusiveMul: |
| 6373 | case glslang::EOpSubgroupExclusiveMul: |
| 6374 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6375 | #ifdef NV_EXTENSIONS |
| 6376 | case glslang::EOpSubgroupPartitionedMul: |
| 6377 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6378 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6379 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6380 | if (isFloat) { |
| 6381 | opCode = spv::OpGroupNonUniformFMul; |
| 6382 | } else { |
| 6383 | opCode = spv::OpGroupNonUniformIMul; |
| 6384 | } |
| 6385 | break; |
| 6386 | case glslang::EOpSubgroupMin: |
| 6387 | case glslang::EOpSubgroupInclusiveMin: |
| 6388 | case glslang::EOpSubgroupExclusiveMin: |
| 6389 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6390 | #ifdef NV_EXTENSIONS |
| 6391 | case glslang::EOpSubgroupPartitionedMin: |
| 6392 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6393 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6394 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6395 | if (isFloat) { |
| 6396 | opCode = spv::OpGroupNonUniformFMin; |
| 6397 | } else if (isUnsigned) { |
| 6398 | opCode = spv::OpGroupNonUniformUMin; |
| 6399 | } else { |
| 6400 | opCode = spv::OpGroupNonUniformSMin; |
| 6401 | } |
| 6402 | break; |
| 6403 | case glslang::EOpSubgroupMax: |
| 6404 | case glslang::EOpSubgroupInclusiveMax: |
| 6405 | case glslang::EOpSubgroupExclusiveMax: |
| 6406 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6407 | #ifdef NV_EXTENSIONS |
| 6408 | case glslang::EOpSubgroupPartitionedMax: |
| 6409 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6410 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6411 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6412 | if (isFloat) { |
| 6413 | opCode = spv::OpGroupNonUniformFMax; |
| 6414 | } else if (isUnsigned) { |
| 6415 | opCode = spv::OpGroupNonUniformUMax; |
| 6416 | } else { |
| 6417 | opCode = spv::OpGroupNonUniformSMax; |
| 6418 | } |
| 6419 | break; |
| 6420 | case glslang::EOpSubgroupAnd: |
| 6421 | case glslang::EOpSubgroupInclusiveAnd: |
| 6422 | case glslang::EOpSubgroupExclusiveAnd: |
| 6423 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6424 | #ifdef NV_EXTENSIONS |
| 6425 | case glslang::EOpSubgroupPartitionedAnd: |
| 6426 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6427 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6428 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6429 | if (isBool) { |
| 6430 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 6431 | } else { |
| 6432 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 6433 | } |
| 6434 | break; |
| 6435 | case glslang::EOpSubgroupOr: |
| 6436 | case glslang::EOpSubgroupInclusiveOr: |
| 6437 | case glslang::EOpSubgroupExclusiveOr: |
| 6438 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6439 | #ifdef NV_EXTENSIONS |
| 6440 | case glslang::EOpSubgroupPartitionedOr: |
| 6441 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6442 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6443 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6444 | if (isBool) { |
| 6445 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 6446 | } else { |
| 6447 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 6448 | } |
| 6449 | break; |
| 6450 | case glslang::EOpSubgroupXor: |
| 6451 | case glslang::EOpSubgroupInclusiveXor: |
| 6452 | case glslang::EOpSubgroupExclusiveXor: |
| 6453 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6454 | #ifdef NV_EXTENSIONS |
| 6455 | case glslang::EOpSubgroupPartitionedXor: |
| 6456 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6457 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6458 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6459 | if (isBool) { |
| 6460 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 6461 | } else { |
| 6462 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 6463 | } |
| 6464 | break; |
| 6465 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 6466 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6467 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6468 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 6469 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6470 | } |
| 6471 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6472 | // get the right Group Operation |
| 6473 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6474 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6475 | default: |
| 6476 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6477 | case glslang::EOpSubgroupBallotBitCount: |
| 6478 | case glslang::EOpSubgroupAdd: |
| 6479 | case glslang::EOpSubgroupMul: |
| 6480 | case glslang::EOpSubgroupMin: |
| 6481 | case glslang::EOpSubgroupMax: |
| 6482 | case glslang::EOpSubgroupAnd: |
| 6483 | case glslang::EOpSubgroupOr: |
| 6484 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6485 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6486 | break; |
| 6487 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6488 | case glslang::EOpSubgroupInclusiveAdd: |
| 6489 | case glslang::EOpSubgroupInclusiveMul: |
| 6490 | case glslang::EOpSubgroupInclusiveMin: |
| 6491 | case glslang::EOpSubgroupInclusiveMax: |
| 6492 | case glslang::EOpSubgroupInclusiveAnd: |
| 6493 | case glslang::EOpSubgroupInclusiveOr: |
| 6494 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6495 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6496 | break; |
| 6497 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6498 | case glslang::EOpSubgroupExclusiveAdd: |
| 6499 | case glslang::EOpSubgroupExclusiveMul: |
| 6500 | case glslang::EOpSubgroupExclusiveMin: |
| 6501 | case glslang::EOpSubgroupExclusiveMax: |
| 6502 | case glslang::EOpSubgroupExclusiveAnd: |
| 6503 | case glslang::EOpSubgroupExclusiveOr: |
| 6504 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6505 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6506 | break; |
| 6507 | case glslang::EOpSubgroupClusteredAdd: |
| 6508 | case glslang::EOpSubgroupClusteredMul: |
| 6509 | case glslang::EOpSubgroupClusteredMin: |
| 6510 | case glslang::EOpSubgroupClusteredMax: |
| 6511 | case glslang::EOpSubgroupClusteredAnd: |
| 6512 | case glslang::EOpSubgroupClusteredOr: |
| 6513 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6514 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6515 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6516 | #ifdef NV_EXTENSIONS |
| 6517 | case glslang::EOpSubgroupPartitionedAdd: |
| 6518 | case glslang::EOpSubgroupPartitionedMul: |
| 6519 | case glslang::EOpSubgroupPartitionedMin: |
| 6520 | case glslang::EOpSubgroupPartitionedMax: |
| 6521 | case glslang::EOpSubgroupPartitionedAnd: |
| 6522 | case glslang::EOpSubgroupPartitionedOr: |
| 6523 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6524 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6525 | break; |
| 6526 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6527 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6528 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6529 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6530 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6531 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6532 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6533 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6534 | break; |
| 6535 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6536 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6537 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6538 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6539 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6540 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6541 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6542 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6543 | break; |
| 6544 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6545 | } |
| 6546 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6547 | // build the instruction |
| 6548 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 6549 | |
| 6550 | // Every operation begins with the Execution Scope operand. |
| 6551 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6552 | spvGroupOperands.push_back(executionScope); |
| 6553 | |
| 6554 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 6555 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6556 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6557 | spvGroupOperands.push_back(groupOperand); |
| 6558 | } |
| 6559 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6560 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6561 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 6562 | spv::IdImmediate operand = { true, *opIt }; |
| 6563 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6564 | } |
| 6565 | |
| 6566 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6567 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6568 | switch (op) { |
| 6569 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6570 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 6571 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 6572 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 6573 | } |
| 6574 | if (directionId != spv::NoResult) { |
| 6575 | spv::IdImmediate direction = { true, directionId }; |
| 6576 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6577 | } |
| 6578 | |
| 6579 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 6580 | } |
| 6581 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6582 | 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] | 6583 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6584 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6585 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6586 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6587 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6588 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6589 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 6590 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6591 | spv::Id typeId0 = 0; |
| 6592 | if (consumedOperands > 0) |
| 6593 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6594 | spv::Id typeId1 = 0; |
| 6595 | if (consumedOperands > 1) |
| 6596 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6597 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6598 | |
| 6599 | switch (op) { |
| 6600 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6601 | if (isFloat) |
| 6602 | libCall = spv::GLSLstd450FMin; |
| 6603 | else if (isUnsigned) |
| 6604 | libCall = spv::GLSLstd450UMin; |
| 6605 | else |
| 6606 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6607 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6608 | break; |
| 6609 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6610 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6611 | break; |
| 6612 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6613 | if (isFloat) |
| 6614 | libCall = spv::GLSLstd450FMax; |
| 6615 | else if (isUnsigned) |
| 6616 | libCall = spv::GLSLstd450UMax; |
| 6617 | else |
| 6618 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6619 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6620 | break; |
| 6621 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6622 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6623 | break; |
| 6624 | case glslang::EOpDot: |
| 6625 | opCode = spv::OpDot; |
| 6626 | break; |
| 6627 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6628 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6629 | break; |
| 6630 | |
| 6631 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6632 | if (isFloat) |
| 6633 | libCall = spv::GLSLstd450FClamp; |
| 6634 | else if (isUnsigned) |
| 6635 | libCall = spv::GLSLstd450UClamp; |
| 6636 | else |
| 6637 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6638 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 6639 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6640 | break; |
| 6641 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6642 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 6643 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6644 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6645 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6646 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6647 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6648 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6649 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6650 | break; |
| 6651 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6652 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6653 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6654 | break; |
| 6655 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6656 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6657 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 6658 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6659 | break; |
| 6660 | |
| 6661 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6662 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6663 | break; |
| 6664 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6665 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6666 | break; |
| 6667 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6668 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6669 | break; |
| 6670 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6671 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6672 | break; |
| 6673 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6674 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6675 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6676 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6677 | #ifdef AMD_EXTENSIONS |
| 6678 | if (typeProxy == glslang::EbtFloat16) |
| 6679 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6680 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6681 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 6682 | break; |
| 6683 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6684 | #ifdef AMD_EXTENSIONS |
| 6685 | if (typeProxy == glslang::EbtFloat16) |
| 6686 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6687 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6688 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 6689 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6690 | case glslang::EOpAddCarry: |
| 6691 | opCode = spv::OpIAddCarry; |
| 6692 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6693 | consumedOperands = 2; |
| 6694 | break; |
| 6695 | case glslang::EOpSubBorrow: |
| 6696 | opCode = spv::OpISubBorrow; |
| 6697 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6698 | consumedOperands = 2; |
| 6699 | break; |
| 6700 | case glslang::EOpUMulExtended: |
| 6701 | opCode = spv::OpUMulExtended; |
| 6702 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6703 | consumedOperands = 2; |
| 6704 | break; |
| 6705 | case glslang::EOpIMulExtended: |
| 6706 | opCode = spv::OpSMulExtended; |
| 6707 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6708 | consumedOperands = 2; |
| 6709 | break; |
| 6710 | case glslang::EOpBitfieldExtract: |
| 6711 | if (isUnsigned) |
| 6712 | opCode = spv::OpBitFieldUExtract; |
| 6713 | else |
| 6714 | opCode = spv::OpBitFieldSExtract; |
| 6715 | break; |
| 6716 | case glslang::EOpBitfieldInsert: |
| 6717 | opCode = spv::OpBitFieldInsert; |
| 6718 | break; |
| 6719 | |
| 6720 | case glslang::EOpFma: |
| 6721 | libCall = spv::GLSLstd450Fma; |
| 6722 | break; |
| 6723 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6724 | { |
| 6725 | libCall = spv::GLSLstd450FrexpStruct; |
| 6726 | assert(builder.isPointerType(typeId1)); |
| 6727 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6728 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 6729 | #ifdef AMD_EXTENSIONS |
| 6730 | if (width == 16) |
| 6731 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 6732 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 6733 | #endif |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6734 | if (builder.getNumComponents(operands[0]) == 1) |
| 6735 | frexpIntType = builder.makeIntegerType(width, true); |
| 6736 | else |
| 6737 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 6738 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 6739 | consumedOperands = 1; |
| 6740 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6741 | break; |
| 6742 | case glslang::EOpLdexp: |
| 6743 | libCall = spv::GLSLstd450Ldexp; |
| 6744 | break; |
| 6745 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6746 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6747 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6748 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6749 | case glslang::EOpSubgroupBroadcast: |
| 6750 | case glslang::EOpSubgroupBallotBitExtract: |
| 6751 | case glslang::EOpSubgroupShuffle: |
| 6752 | case glslang::EOpSubgroupShuffleXor: |
| 6753 | case glslang::EOpSubgroupShuffleUp: |
| 6754 | case glslang::EOpSubgroupShuffleDown: |
| 6755 | case glslang::EOpSubgroupClusteredAdd: |
| 6756 | case glslang::EOpSubgroupClusteredMul: |
| 6757 | case glslang::EOpSubgroupClusteredMin: |
| 6758 | case glslang::EOpSubgroupClusteredMax: |
| 6759 | case glslang::EOpSubgroupClusteredAnd: |
| 6760 | case glslang::EOpSubgroupClusteredOr: |
| 6761 | case glslang::EOpSubgroupClusteredXor: |
| 6762 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6763 | #ifdef NV_EXTENSIONS |
| 6764 | case glslang::EOpSubgroupPartitionedAdd: |
| 6765 | case glslang::EOpSubgroupPartitionedMul: |
| 6766 | case glslang::EOpSubgroupPartitionedMin: |
| 6767 | case glslang::EOpSubgroupPartitionedMax: |
| 6768 | case glslang::EOpSubgroupPartitionedAnd: |
| 6769 | case glslang::EOpSubgroupPartitionedOr: |
| 6770 | case glslang::EOpSubgroupPartitionedXor: |
| 6771 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6772 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6773 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6774 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6775 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6776 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6777 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6778 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6779 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6780 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6781 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6782 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6783 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6784 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6785 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6786 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 6787 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6788 | #ifdef AMD_EXTENSIONS |
| 6789 | case glslang::EOpSwizzleInvocations: |
| 6790 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6791 | libCall = spv::SwizzleInvocationsAMD; |
| 6792 | break; |
| 6793 | case glslang::EOpSwizzleInvocationsMasked: |
| 6794 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6795 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 6796 | break; |
| 6797 | case glslang::EOpWriteInvocation: |
| 6798 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6799 | libCall = spv::WriteInvocationAMD; |
| 6800 | break; |
| 6801 | |
| 6802 | case glslang::EOpMin3: |
| 6803 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6804 | if (isFloat) |
| 6805 | libCall = spv::FMin3AMD; |
| 6806 | else { |
| 6807 | if (isUnsigned) |
| 6808 | libCall = spv::UMin3AMD; |
| 6809 | else |
| 6810 | libCall = spv::SMin3AMD; |
| 6811 | } |
| 6812 | break; |
| 6813 | case glslang::EOpMax3: |
| 6814 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6815 | if (isFloat) |
| 6816 | libCall = spv::FMax3AMD; |
| 6817 | else { |
| 6818 | if (isUnsigned) |
| 6819 | libCall = spv::UMax3AMD; |
| 6820 | else |
| 6821 | libCall = spv::SMax3AMD; |
| 6822 | } |
| 6823 | break; |
| 6824 | case glslang::EOpMid3: |
| 6825 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6826 | if (isFloat) |
| 6827 | libCall = spv::FMid3AMD; |
| 6828 | else { |
| 6829 | if (isUnsigned) |
| 6830 | libCall = spv::UMid3AMD; |
| 6831 | else |
| 6832 | libCall = spv::SMid3AMD; |
| 6833 | } |
| 6834 | break; |
| 6835 | |
| 6836 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6837 | if (typeProxy == glslang::EbtFloat16) |
| 6838 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6839 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 6840 | libCall = spv::InterpolateAtVertexAMD; |
| 6841 | break; |
| 6842 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6843 | case glslang::EOpBarrier: |
| 6844 | { |
| 6845 | // This is for the extended controlBarrier function, with four operands. |
| 6846 | // The unextended barrier() goes through createNoArgOperation. |
| 6847 | assert(operands.size() == 4); |
| 6848 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 6849 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 6850 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 6851 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 6852 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 6853 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6854 | } |
| 6855 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) { |
| 6856 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6857 | } |
| 6858 | return 0; |
| 6859 | } |
| 6860 | break; |
| 6861 | case glslang::EOpMemoryBarrier: |
| 6862 | { |
| 6863 | // This is for the extended memoryBarrier function, with three operands. |
| 6864 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 6865 | assert(operands.size() == 3); |
| 6866 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 6867 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 6868 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 6869 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 6870 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6871 | } |
| 6872 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 6873 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6874 | } |
| 6875 | return 0; |
| 6876 | } |
| 6877 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 6878 | |
| 6879 | #ifdef NV_EXTENSIONS |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 6880 | case glslang::EOpReportIntersectionNV: |
| 6881 | { |
| 6882 | typeId = builder.makeBoolType(); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 6883 | opCode = spv::OpReportIntersectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 6884 | } |
| 6885 | break; |
| 6886 | case glslang::EOpTraceNV: |
| 6887 | { |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 6888 | builder.createNoResultOp(spv::OpTraceNV, operands); |
| 6889 | return 0; |
| 6890 | } |
| 6891 | break; |
| 6892 | case glslang::EOpExecuteCallableNV: |
| 6893 | { |
| 6894 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 6895 | return 0; |
| 6896 | } |
| 6897 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 6898 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 6899 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 6900 | return 0; |
| 6901 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6902 | default: |
| 6903 | return 0; |
| 6904 | } |
| 6905 | |
| 6906 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 6907 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 6908 | // Use an extended instruction from the standard library. |
| 6909 | // Construct the call arguments, without modifying the original operands vector. |
| 6910 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 6911 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6912 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 6913 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6914 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6915 | case 0: |
| 6916 | // should all be handled by visitAggregate and createNoArgOperation |
| 6917 | assert(0); |
| 6918 | return 0; |
| 6919 | case 1: |
| 6920 | // should all be handled by createUnaryOperation |
| 6921 | assert(0); |
| 6922 | return 0; |
| 6923 | case 2: |
| 6924 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 6925 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6926 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6927 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 6928 | assert(consumedOperands == operands.size()); |
| 6929 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6930 | break; |
| 6931 | } |
| 6932 | } |
| 6933 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6934 | // Decode the return types that were structures |
| 6935 | switch (op) { |
| 6936 | case glslang::EOpAddCarry: |
| 6937 | case glslang::EOpSubBorrow: |
| 6938 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 6939 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 6940 | break; |
| 6941 | case glslang::EOpUMulExtended: |
| 6942 | case glslang::EOpIMulExtended: |
| 6943 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 6944 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 6945 | break; |
| 6946 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6947 | { |
| 6948 | assert(operands.size() == 2); |
| 6949 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 6950 | // "exp" is floating-point type (from HLSL intrinsic) |
| 6951 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 6952 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 6953 | builder.createStore(member1, operands[1]); |
| 6954 | } else |
| 6955 | // "exp" is integer type (from GLSL built-in function) |
| 6956 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 6957 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 6958 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6959 | break; |
| 6960 | default: |
| 6961 | break; |
| 6962 | } |
| 6963 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 6964 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6965 | } |
| 6966 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6967 | // Intrinsics with no arguments (or no return value, and no precision). |
| 6968 | 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] | 6969 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6970 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 6971 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6972 | |
| 6973 | switch (op) { |
| 6974 | case glslang::EOpEmitVertex: |
| 6975 | builder.createNoResultOp(spv::OpEmitVertex); |
| 6976 | return 0; |
| 6977 | case glslang::EOpEndPrimitive: |
| 6978 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 6979 | return 0; |
| 6980 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6981 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6982 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6983 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 6984 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 6985 | spv::MemorySemanticsAcquireReleaseMask); |
| 6986 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6987 | } else { |
| 6988 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 6989 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6990 | } else { |
| 6991 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 6992 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 6993 | spv::MemorySemanticsAcquireReleaseMask); |
| 6994 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6995 | return 0; |
| 6996 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6997 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 6998 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6999 | return 0; |
| 7000 | case glslang::EOpMemoryBarrierAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7001 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 7002 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7003 | return 0; |
| 7004 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7005 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 7006 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7007 | return 0; |
| 7008 | case glslang::EOpMemoryBarrierImage: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7009 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 7010 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7011 | return 0; |
| 7012 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7013 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7014 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7015 | return 0; |
| 7016 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7017 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 7018 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7019 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7020 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7021 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7022 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7023 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7024 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7025 | case glslang::EOpDeviceMemoryBarrier: |
| 7026 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7027 | spv::MemorySemanticsImageMemoryMask | |
| 7028 | spv::MemorySemanticsAcquireReleaseMask); |
| 7029 | return 0; |
| 7030 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 7031 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7032 | spv::MemorySemanticsImageMemoryMask | |
| 7033 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7034 | return 0; |
| 7035 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7036 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7037 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7038 | return 0; |
| 7039 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7040 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7041 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7042 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7043 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7044 | case glslang::EOpSubgroupBarrier: |
| 7045 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7046 | spv::MemorySemanticsAcquireReleaseMask); |
| 7047 | return spv::NoResult; |
| 7048 | case glslang::EOpSubgroupMemoryBarrier: |
| 7049 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7050 | spv::MemorySemanticsAcquireReleaseMask); |
| 7051 | return spv::NoResult; |
| 7052 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 7053 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 7054 | spv::MemorySemanticsAcquireReleaseMask); |
| 7055 | return spv::NoResult; |
| 7056 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 7057 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 7058 | spv::MemorySemanticsAcquireReleaseMask); |
| 7059 | return spv::NoResult; |
| 7060 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 7061 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7062 | spv::MemorySemanticsAcquireReleaseMask); |
| 7063 | return spv::NoResult; |
| 7064 | case glslang::EOpSubgroupElect: { |
| 7065 | std::vector<spv::Id> operands; |
| 7066 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 7067 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7068 | #ifdef AMD_EXTENSIONS |
| 7069 | case glslang::EOpTime: |
| 7070 | { |
| 7071 | std::vector<spv::Id> args; // Dummy arguments |
| 7072 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 7073 | return builder.setPrecision(id, precision); |
| 7074 | } |
| 7075 | #endif |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7076 | #ifdef NV_EXTENSIONS |
| 7077 | case glslang::EOpIgnoreIntersectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7078 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7079 | return 0; |
| 7080 | case glslang::EOpTerminateRayNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7081 | builder.createNoResultOp(spv::OpTerminateRayNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7082 | return 0; |
| 7083 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7084 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7085 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7086 | return 0; |
| 7087 | } |
| 7088 | } |
| 7089 | |
| 7090 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 7091 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 7092 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7093 | spv::Id id; |
| 7094 | if (symbolValues.end() != iter) { |
| 7095 | id = iter->second; |
| 7096 | return id; |
| 7097 | } |
| 7098 | |
| 7099 | // it was not found, create it |
| 7100 | id = createSpvVariable(symbol); |
| 7101 | symbolValues[symbol->getId()] = id; |
| 7102 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 7103 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7104 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 7105 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 7106 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7107 | #ifdef NV_EXTENSIONS |
| 7108 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
| 7109 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7110 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7111 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7112 | if (symbol->getQualifier().hasIndex()) |
| 7113 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 7114 | if (symbol->getQualifier().hasComponent()) |
| 7115 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 7116 | // atomic counters use this: |
| 7117 | if (symbol->getQualifier().hasOffset()) |
| 7118 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7119 | } |
| 7120 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 7121 | if (symbol->getQualifier().hasLocation()) |
| 7122 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7123 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 7124 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7125 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7126 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7127 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7128 | if (symbol->getQualifier().hasSet()) |
| 7129 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7130 | else if (IsDescriptorResource(symbol->getType())) { |
| 7131 | // default to 0 |
| 7132 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 7133 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7134 | if (symbol->getQualifier().hasBinding()) |
| 7135 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7136 | if (symbol->getQualifier().hasAttachment()) |
| 7137 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7138 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7139 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7140 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7141 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7142 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7143 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7144 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 7145 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 7146 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 7147 | } |
| 7148 | if (symbol->getQualifier().hasXfbOffset()) |
| 7149 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7150 | } |
| 7151 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7152 | if (symbol->getType().isImage()) { |
| 7153 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7154 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7155 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7156 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7157 | } |
| 7158 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7159 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 7160 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 7161 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7162 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7163 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 7164 | // nonuniform |
| 7165 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 7166 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 7167 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7168 | if (builtIn == spv::BuiltInSampleMask) { |
| 7169 | spv::Decoration decoration; |
| 7170 | // GL_NV_sample_mask_override_coverage extension |
| 7171 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7172 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7173 | else |
| 7174 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7175 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7176 | if (decoration != spv::DecorationMax) { |
| 7177 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 7178 | } |
| 7179 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7180 | else if (builtIn == spv::BuiltInLayer) { |
| 7181 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7182 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7183 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7184 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 7185 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 7186 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7187 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7188 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 7189 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7190 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 7191 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 7192 | } |
| 7193 | } |
| 7194 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7195 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7196 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7197 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7198 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 7199 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 7200 | if (symbol->getQualifier().pervertexNV) { |
| 7201 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 7202 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 7203 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 7204 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7205 | #endif |
| 7206 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7207 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 7208 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 7209 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 7210 | symbol->getType().getQualifier().semanticName); |
| 7211 | } |
| 7212 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7213 | return id; |
| 7214 | } |
| 7215 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7216 | #ifdef NV_EXTENSIONS |
| 7217 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 7218 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 7219 | { |
| 7220 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7221 | if (qualifier.perPrimitiveNV) { |
| 7222 | // Need to add capability/extension for fragment shader. |
| 7223 | // Mesh shader already adds this by default. |
| 7224 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7225 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7226 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7227 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7228 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7229 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7230 | if (qualifier.perViewNV) |
| 7231 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 7232 | if (qualifier.perTaskNV) |
| 7233 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 7234 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7235 | if (qualifier.perPrimitiveNV) { |
| 7236 | // Need to add capability/extension for fragment shader. |
| 7237 | // Mesh shader already adds this by default. |
| 7238 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7239 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7240 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7241 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7242 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7243 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7244 | if (qualifier.perViewNV) |
| 7245 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 7246 | if (qualifier.perTaskNV) |
| 7247 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 7248 | } |
| 7249 | } |
| 7250 | #endif |
| 7251 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7252 | // 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] | 7253 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7254 | // |
| 7255 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 7256 | // |
| 7257 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 7258 | // regular constants, which themselves are trees that createSpvConstant() |
| 7259 | // recursively walks. So, this function walks the "top" of the tree: |
| 7260 | // - emit specialization constant-building instructions for specConstant |
| 7261 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7262 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7263 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 7264 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7265 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7266 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7267 | if (! node.getQualifier().specConstant) { |
| 7268 | // hand off to the non-spec-constant path |
| 7269 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 7270 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7271 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7272 | nextConst, false); |
| 7273 | } |
| 7274 | |
| 7275 | // We now know we have a specialization constant to build |
| 7276 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 7277 | // 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] | 7278 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 7279 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 7280 | std::vector<spv::Id> dimConstId; |
| 7281 | for (int dim = 0; dim < 3; ++dim) { |
| 7282 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 7283 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7284 | if (specConst) { |
| 7285 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 7286 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 7287 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7288 | } |
| 7289 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 7290 | } |
| 7291 | |
| 7292 | // An AST node labelled as specialization constant should be a symbol node. |
| 7293 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 7294 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7295 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7296 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 7297 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 7298 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 7299 | // will set the builder into spec constant op instruction generating mode. |
| 7300 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7301 | result = accessChainLoad(sub_tree->getType()); |
| 7302 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7303 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7304 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame^] | 7305 | } else { |
| 7306 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
| 7307 | exit(1); |
| 7308 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7309 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7310 | builder.addName(result, sn->getName().c_str()); |
| 7311 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7312 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7313 | |
| 7314 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 7315 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7316 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7317 | exit(1); |
| 7318 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7319 | } |
| 7320 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7321 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 7322 | // build the aggregate SPIR-V constant. |
| 7323 | // |
| 7324 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 7325 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 7326 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7327 | 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] | 7328 | { |
| 7329 | // vector of constants for SPIR-V |
| 7330 | std::vector<spv::Id> spvConsts; |
| 7331 | |
| 7332 | // Type is used for struct and array constants |
| 7333 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 7334 | |
| 7335 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7336 | glslang::TType elementType(glslangType, 0); |
| 7337 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7338 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7339 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7340 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7341 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7342 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7343 | } else if (glslangType.getStruct()) { |
| 7344 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 7345 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7346 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 7347 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7348 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 7349 | bool zero = nextConst >= consts.size(); |
| 7350 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7351 | case glslang::EbtInt8: |
| 7352 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 7353 | break; |
| 7354 | case glslang::EbtUint8: |
| 7355 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 7356 | break; |
| 7357 | case glslang::EbtInt16: |
| 7358 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 7359 | break; |
| 7360 | case glslang::EbtUint16: |
| 7361 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 7362 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7363 | case glslang::EbtInt: |
| 7364 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 7365 | break; |
| 7366 | case glslang::EbtUint: |
| 7367 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 7368 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7369 | case glslang::EbtInt64: |
| 7370 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 7371 | break; |
| 7372 | case glslang::EbtUint64: |
| 7373 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 7374 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7375 | case glslang::EbtFloat: |
| 7376 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7377 | break; |
| 7378 | case glslang::EbtDouble: |
| 7379 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 7380 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7381 | case glslang::EbtFloat16: |
| 7382 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7383 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7384 | case glslang::EbtBool: |
| 7385 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 7386 | break; |
| 7387 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7388 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7389 | break; |
| 7390 | } |
| 7391 | ++nextConst; |
| 7392 | } |
| 7393 | } else { |
| 7394 | // we have a non-aggregate (scalar) constant |
| 7395 | bool zero = nextConst >= consts.size(); |
| 7396 | spv::Id scalar = 0; |
| 7397 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7398 | case glslang::EbtInt8: |
| 7399 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 7400 | break; |
| 7401 | case glslang::EbtUint8: |
| 7402 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 7403 | break; |
| 7404 | case glslang::EbtInt16: |
| 7405 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 7406 | break; |
| 7407 | case glslang::EbtUint16: |
| 7408 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 7409 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7410 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7411 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7412 | break; |
| 7413 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7414 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7415 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7416 | case glslang::EbtInt64: |
| 7417 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 7418 | break; |
| 7419 | case glslang::EbtUint64: |
| 7420 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7421 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7422 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7423 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7424 | break; |
| 7425 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7426 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7427 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7428 | case glslang::EbtFloat16: |
| 7429 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 7430 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7431 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7432 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7433 | break; |
| 7434 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7435 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7436 | break; |
| 7437 | } |
| 7438 | ++nextConst; |
| 7439 | return scalar; |
| 7440 | } |
| 7441 | |
| 7442 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 7443 | } |
| 7444 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7445 | // Return true if the node is a constant or symbol whose reading has no |
| 7446 | // non-trivial observable cost or effect. |
| 7447 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 7448 | { |
| 7449 | // don't know what this is |
| 7450 | if (node == nullptr) |
| 7451 | return false; |
| 7452 | |
| 7453 | // a constant is safe |
| 7454 | if (node->getAsConstantUnion() != nullptr) |
| 7455 | return true; |
| 7456 | |
| 7457 | // not a symbol means non-trivial |
| 7458 | if (node->getAsSymbolNode() == nullptr) |
| 7459 | return false; |
| 7460 | |
| 7461 | // a symbol, depends on what's being read |
| 7462 | switch (node->getType().getQualifier().storage) { |
| 7463 | case glslang::EvqTemporary: |
| 7464 | case glslang::EvqGlobal: |
| 7465 | case glslang::EvqIn: |
| 7466 | case glslang::EvqInOut: |
| 7467 | case glslang::EvqConst: |
| 7468 | case glslang::EvqConstReadOnly: |
| 7469 | case glslang::EvqUniform: |
| 7470 | return true; |
| 7471 | default: |
| 7472 | return false; |
| 7473 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 7474 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7475 | |
| 7476 | // 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] | 7477 | // 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] | 7478 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7479 | // Return true if trivial. |
| 7480 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 7481 | { |
| 7482 | if (node == nullptr) |
| 7483 | return false; |
| 7484 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 7485 | // count non scalars as trivial, as well as anything coming from HLSL |
| 7486 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 7487 | return true; |
| 7488 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7489 | // symbols and constants are trivial |
| 7490 | if (isTrivialLeaf(node)) |
| 7491 | return true; |
| 7492 | |
| 7493 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 7494 | |
| 7495 | // not a simple operation |
| 7496 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 7497 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 7498 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 7499 | return false; |
| 7500 | |
| 7501 | // not on leaf nodes |
| 7502 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 7503 | return false; |
| 7504 | |
| 7505 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 7506 | return false; |
| 7507 | } |
| 7508 | |
| 7509 | switch (node->getAsOperator()->getOp()) { |
| 7510 | case glslang::EOpLogicalNot: |
| 7511 | case glslang::EOpConvIntToBool: |
| 7512 | case glslang::EOpConvUintToBool: |
| 7513 | case glslang::EOpConvFloatToBool: |
| 7514 | case glslang::EOpConvDoubleToBool: |
| 7515 | case glslang::EOpEqual: |
| 7516 | case glslang::EOpNotEqual: |
| 7517 | case glslang::EOpLessThan: |
| 7518 | case glslang::EOpGreaterThan: |
| 7519 | case glslang::EOpLessThanEqual: |
| 7520 | case glslang::EOpGreaterThanEqual: |
| 7521 | case glslang::EOpIndexDirect: |
| 7522 | case glslang::EOpIndexDirectStruct: |
| 7523 | case glslang::EOpLogicalXor: |
| 7524 | case glslang::EOpAny: |
| 7525 | case glslang::EOpAll: |
| 7526 | return true; |
| 7527 | default: |
| 7528 | return false; |
| 7529 | } |
| 7530 | } |
| 7531 | |
| 7532 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 7533 | // the left side is true (for &&) or false (for ||). |
| 7534 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 7535 | { |
| 7536 | spv::Id boolTypeId = builder.makeBoolType(); |
| 7537 | |
| 7538 | // emit left operand |
| 7539 | builder.clearAccessChain(); |
| 7540 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 7541 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7542 | |
| 7543 | // Operands to accumulate OpPhi operands |
| 7544 | std::vector<spv::Id> phiOperands; |
| 7545 | // accumulate left operand's phi information |
| 7546 | phiOperands.push_back(leftId); |
| 7547 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 7548 | |
| 7549 | // Make the two kinds of operation symmetric with a "!" |
| 7550 | // || => emit "if (! left) result = right" |
| 7551 | // && => emit "if ( left) result = right" |
| 7552 | // |
| 7553 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 7554 | // to 'builder' to have an "else" without an "then" |
| 7555 | if (op == glslang::EOpLogicalOr) |
| 7556 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 7557 | |
| 7558 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 7559 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7560 | |
| 7561 | // emit right operand as the "then" part of the "if" |
| 7562 | builder.clearAccessChain(); |
| 7563 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 7564 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7565 | |
| 7566 | // accumulate left operand's phi information |
| 7567 | phiOperands.push_back(rightId); |
| 7568 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 7569 | |
| 7570 | // finish the "if" |
| 7571 | ifBuilder.makeEndIf(); |
| 7572 | |
| 7573 | // phi together the two results |
| 7574 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 7575 | } |
| 7576 | |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 7577 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7578 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 7579 | // Import this set if it has not been imported yet. |
| 7580 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 7581 | { |
| 7582 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 7583 | return extBuiltinMap[name]; |
| 7584 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7585 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7586 | spv::Id extBuiltins = builder.import(name); |
| 7587 | extBuiltinMap[name] = extBuiltins; |
| 7588 | return extBuiltins; |
| 7589 | } |
| 7590 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 7591 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7592 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7593 | }; // end anonymous namespace |
| 7594 | |
| 7595 | namespace glslang { |
| 7596 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7597 | void GetSpirvVersion(std::string& version) |
| 7598 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 7599 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 7600 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7601 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7602 | version = buf; |
| 7603 | } |
| 7604 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 7605 | // For low-order part of the generator's magic number. Bump up |
| 7606 | // when there is a change in the style (e.g., if SSA form changes, |
| 7607 | // or a different instruction sequence to do something gets used). |
| 7608 | int GetSpirvGeneratorVersion() |
| 7609 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 7610 | // return 1; // start |
| 7611 | // 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] | 7612 | // 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] | 7613 | // 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] | 7614 | // 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] | 7615 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 7616 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
| 7617 | 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] | 7618 | } |
| 7619 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7620 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7621 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7622 | { |
| 7623 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7624 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 7625 | if (out.fail()) |
| 7626 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7627 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 7628 | unsigned int word = spirv[i]; |
| 7629 | out.write((const char*)&word, 4); |
| 7630 | } |
| 7631 | out.close(); |
| 7632 | } |
| 7633 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7634 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 7635 | 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] | 7636 | { |
| 7637 | std::ofstream out; |
| 7638 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 7639 | if (out.fail()) |
| 7640 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 7641 | out << "\t// " << |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7642 | GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL << |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 7643 | std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 7644 | if (varName != nullptr) { |
| 7645 | out << "\t #pragma once" << std::endl; |
| 7646 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 7647 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7648 | const int WORDS_PER_LINE = 8; |
| 7649 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 7650 | out << "\t"; |
| 7651 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 7652 | const unsigned int word = spirv[i + j]; |
| 7653 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 7654 | if (i + j + 1 < (int)spirv.size()) { |
| 7655 | out << ","; |
| 7656 | } |
| 7657 | } |
| 7658 | out << std::endl; |
| 7659 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 7660 | if (varName != nullptr) { |
| 7661 | out << "};"; |
| 7662 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7663 | out.close(); |
| 7664 | } |
| 7665 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7666 | // |
| 7667 | // Set up the glslang traversal |
| 7668 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7669 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7670 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7671 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7672 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 7673 | } |
| 7674 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7675 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7676 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 7677 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7678 | TIntermNode* root = intermediate.getTreeRoot(); |
| 7679 | |
| 7680 | if (root == 0) |
| 7681 | return; |
| 7682 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7683 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7684 | if (options == nullptr) |
| 7685 | options = &defaultOptions; |
| 7686 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7687 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7688 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 7689 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7690 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 7691 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7692 | it.dumpSpv(spirv); |
| 7693 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 7694 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 7695 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 7696 | // eg. forward and remove memory writes of opaque types. |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7697 | if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer) |
John Kessenich | e7df8e0 | 2018-08-22 17:12:46 -0600 | [diff] [blame] | 7698 | SpirvToolsLegalize(intermediate, spirv, logger, options); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7699 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7700 | if (options->validate) |
| 7701 | SpirvToolsValidate(intermediate, spirv, logger); |
| 7702 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7703 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7704 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7705 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 7706 | #endif |
| 7707 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7708 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7709 | } |
| 7710 | |
| 7711 | }; // end namespace glslang |