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); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 197 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 198 | 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] | 199 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 200 | bool isTrivial(const glslang::TIntermTyped* node); |
| 201 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 202 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 203 | spv::Id getExtBuiltins(const char* name); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 204 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 205 | void addPre13Extension(const char* ext) |
| 206 | { |
| 207 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 208 | builder.addExtension(ext); |
| 209 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 210 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 211 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 212 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 213 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 214 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 215 | int sequenceDepth; |
| 216 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 217 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 218 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 219 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 220 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 221 | bool inEntryPoint; |
| 222 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 223 | 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] | 224 | 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] | 225 | const glslang::TIntermediate* glslangIntermediate; |
| 226 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 227 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 228 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 229 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 230 | 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] | 231 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 232 | 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] | 233 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 234 | std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 235 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 236 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | // |
| 240 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 241 | // |
| 242 | |
| 243 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 244 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 245 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 246 | switch (source) { |
| 247 | case glslang::EShSourceGlsl: |
| 248 | switch (profile) { |
| 249 | case ENoProfile: |
| 250 | case ECoreProfile: |
| 251 | case ECompatibilityProfile: |
| 252 | return spv::SourceLanguageGLSL; |
| 253 | case EEsProfile: |
| 254 | return spv::SourceLanguageESSL; |
| 255 | default: |
| 256 | return spv::SourceLanguageUnknown; |
| 257 | } |
| 258 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 259 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 260 | default: |
| 261 | return spv::SourceLanguageUnknown; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Translate glslang language (stage) to SPIR-V execution model. |
| 266 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 267 | { |
| 268 | switch (stage) { |
| 269 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 270 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 271 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 272 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 273 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 274 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
| 275 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 276 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 277 | return spv::ExecutionModelFragment; |
| 278 | } |
| 279 | } |
| 280 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 281 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 282 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 283 | { |
| 284 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 285 | case glslang::Esd1D: return spv::Dim1D; |
| 286 | case glslang::Esd2D: return spv::Dim2D; |
| 287 | case glslang::Esd3D: return spv::Dim3D; |
| 288 | case glslang::EsdCube: return spv::DimCube; |
| 289 | case glslang::EsdRect: return spv::DimRect; |
| 290 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 291 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 292 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 293 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 294 | return spv::Dim2D; |
| 295 | } |
| 296 | } |
| 297 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 298 | // Translate glslang precision to SPIR-V precision decorations. |
| 299 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 300 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 301 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 302 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 303 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 304 | default: |
| 305 | return spv::NoPrecision; |
| 306 | } |
| 307 | } |
| 308 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 309 | // Translate glslang type to SPIR-V precision decorations. |
| 310 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 311 | { |
| 312 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 313 | } |
| 314 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 315 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 316 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 317 | { |
| 318 | if (type.getBasicType() == glslang::EbtBlock) { |
| 319 | switch (type.getQualifier().storage) { |
| 320 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 321 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 322 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 323 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
| 324 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 325 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | } |
| 329 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 330 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 331 | } |
| 332 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 333 | // Translate glslang type to SPIR-V memory decorations. |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 334 | 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] | 335 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 336 | if (!useVulkanMemoryModel) { |
| 337 | if (qualifier.coherent) |
| 338 | memory.push_back(spv::DecorationCoherent); |
| 339 | if (qualifier.volatil) { |
| 340 | memory.push_back(spv::DecorationVolatile); |
| 341 | memory.push_back(spv::DecorationCoherent); |
| 342 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 343 | } |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 344 | if (qualifier.restrict) |
| 345 | memory.push_back(spv::DecorationRestrict); |
| 346 | if (qualifier.readonly) |
| 347 | memory.push_back(spv::DecorationNonWritable); |
| 348 | if (qualifier.writeonly) |
| 349 | memory.push_back(spv::DecorationNonReadable); |
| 350 | } |
| 351 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 352 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 353 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 354 | { |
| 355 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 356 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 357 | case glslang::ElmRowMajor: |
| 358 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 359 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 360 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 361 | default: |
| 362 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 363 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 364 | } |
| 365 | } else { |
| 366 | switch (type.getBasicType()) { |
| 367 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 368 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 369 | break; |
| 370 | case glslang::EbtBlock: |
| 371 | switch (type.getQualifier().storage) { |
| 372 | case glslang::EvqUniform: |
| 373 | case glslang::EvqBuffer: |
| 374 | switch (type.getQualifier().layoutPacking) { |
| 375 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 376 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 377 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 378 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 379 | } |
| 380 | case glslang::EvqVaryingIn: |
| 381 | case glslang::EvqVaryingOut: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 382 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
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 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 385 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 386 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 393 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 394 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 395 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 396 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 397 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 398 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 399 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 400 | else if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 401 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 402 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 403 | return spv::DecorationFlat; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 404 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 405 | else if (qualifier.explicitInterp) { |
| 406 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 407 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 408 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 409 | #endif |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 410 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 411 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 415 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 416 | // should be applied. |
| 417 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 418 | { |
| 419 | if (qualifier.patch) |
| 420 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 421 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 422 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 423 | else if (qualifier.sample) { |
| 424 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 425 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 426 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 427 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 428 | } |
| 429 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 430 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 431 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 432 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 433 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 434 | return spv::DecorationInvariant; |
| 435 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 436 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 437 | } |
| 438 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 439 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 440 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 441 | { |
| 442 | if (qualifier.noContraction) |
| 443 | return spv::DecorationNoContraction; |
| 444 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 445 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 446 | } |
| 447 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 448 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 449 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 450 | { |
| 451 | if (qualifier.isNonUniform()) { |
| 452 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 453 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 454 | return spv::DecorationNonUniformEXT; |
| 455 | } else |
| 456 | return spv::DecorationMax; |
| 457 | } |
| 458 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 459 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 460 | { |
| 461 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) { |
| 462 | return spv::MemoryAccessMaskNone; |
| 463 | } |
| 464 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
| 465 | if (coherentFlags.volatil || |
| 466 | coherentFlags.coherent || |
| 467 | coherentFlags.devicecoherent || |
| 468 | coherentFlags.queuefamilycoherent || |
| 469 | coherentFlags.workgroupcoherent || |
| 470 | coherentFlags.subgroupcoherent) { |
| 471 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 472 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 473 | } |
| 474 | if (coherentFlags.nonprivate) { |
| 475 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 476 | } |
| 477 | if (coherentFlags.volatil) { |
| 478 | mask = mask | spv::MemoryAccessVolatileMask; |
| 479 | } |
| 480 | if (mask != spv::MemoryAccessMaskNone) { |
| 481 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 482 | } |
| 483 | return mask; |
| 484 | } |
| 485 | |
| 486 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 487 | { |
| 488 | if (!glslangIntermediate->usingVulkanMemoryModel()) { |
| 489 | return spv::ImageOperandsMaskNone; |
| 490 | } |
| 491 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 492 | if (coherentFlags.volatil || |
| 493 | coherentFlags.coherent || |
| 494 | coherentFlags.devicecoherent || |
| 495 | coherentFlags.queuefamilycoherent || |
| 496 | coherentFlags.workgroupcoherent || |
| 497 | coherentFlags.subgroupcoherent) { |
| 498 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 499 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 500 | } |
| 501 | if (coherentFlags.nonprivate) { |
| 502 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 503 | } |
| 504 | if (coherentFlags.volatil) { |
| 505 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 506 | } |
| 507 | if (mask != spv::ImageOperandsMaskNone) { |
| 508 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 509 | } |
| 510 | return mask; |
| 511 | } |
| 512 | |
| 513 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 514 | { |
| 515 | spv::Builder::AccessChain::CoherentFlags flags; |
| 516 | flags.coherent = type.getQualifier().coherent; |
| 517 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 518 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 519 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 520 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 521 | type.getQualifier().storage == glslang::EvqShared; |
| 522 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
| 523 | // *coherent variables are implicitly nonprivate in GLSL |
| 524 | flags.nonprivate = type.getQualifier().nonprivate || |
| 525 | type.getQualifier().subgroupcoherent || |
| 526 | type.getQualifier().workgroupcoherent || |
| 527 | type.getQualifier().queuefamilycoherent || |
| 528 | type.getQualifier().devicecoherent || |
| 529 | type.getQualifier().coherent; |
| 530 | flags.volatil = type.getQualifier().volatil; |
| 531 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
| 532 | return flags; |
| 533 | } |
| 534 | |
| 535 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 536 | { |
| 537 | spv::Scope scope; |
| 538 | if (coherentFlags.coherent) { |
| 539 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 540 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 541 | } else if (coherentFlags.devicecoherent) { |
| 542 | scope = spv::ScopeDevice; |
| 543 | } else if (coherentFlags.queuefamilycoherent) { |
| 544 | scope = spv::ScopeQueueFamilyKHR; |
| 545 | } else if (coherentFlags.workgroupcoherent) { |
| 546 | scope = spv::ScopeWorkgroup; |
| 547 | } else if (coherentFlags.subgroupcoherent) { |
| 548 | scope = spv::ScopeSubgroup; |
| 549 | } else { |
| 550 | scope = spv::ScopeMax; |
| 551 | } |
| 552 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 553 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 554 | } |
| 555 | return scope; |
| 556 | } |
| 557 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 558 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 559 | // associated capabilities when required. For some built-in variables, a capability |
| 560 | // is generated only when using the variable in an executable instruction, but not when |
| 561 | // just declaring a struct member variable with it. This is true for PointSize, |
| 562 | // ClipDistance, and CullDistance. |
| 563 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 564 | { |
| 565 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 566 | case glslang::EbvPointSize: |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 567 | // Defer adding the capability until the built-in is actually used. |
| 568 | if (! memberDeclaration) { |
| 569 | switch (glslangIntermediate->getStage()) { |
| 570 | case EShLangGeometry: |
| 571 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 572 | break; |
| 573 | case EShLangTessControl: |
| 574 | case EShLangTessEvaluation: |
| 575 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 576 | break; |
| 577 | default: |
| 578 | break; |
| 579 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 580 | } |
| 581 | return spv::BuiltInPointSize; |
| 582 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 583 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 584 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 585 | // They are now generated when used, rather than here when declared. |
| 586 | // Potentially, the specification should be more clear what the minimum |
| 587 | // use needed is to trigger the capability. |
| 588 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 589 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 590 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 591 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 592 | return spv::BuiltInClipDistance; |
| 593 | |
| 594 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 595 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 596 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 597 | return spv::BuiltInCullDistance; |
| 598 | |
| 599 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 600 | builder.addCapability(spv::CapabilityMultiViewport); |
| 601 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 602 | glslangIntermediate->getStage() == EShLangTessControl || |
| 603 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 604 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 605 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 606 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 607 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 608 | return spv::BuiltInViewportIndex; |
| 609 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 610 | case glslang::EbvSampleId: |
| 611 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 612 | return spv::BuiltInSampleId; |
| 613 | |
| 614 | case glslang::EbvSamplePosition: |
| 615 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 616 | return spv::BuiltInSamplePosition; |
| 617 | |
| 618 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 619 | return spv::BuiltInSampleMask; |
| 620 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 621 | case glslang::EbvLayer: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 622 | builder.addCapability(spv::CapabilityGeometry); |
| 623 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 624 | glslangIntermediate->getStage() == EShLangTessControl || |
| 625 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 626 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 627 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 628 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 629 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 630 | return spv::BuiltInLayer; |
| 631 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 632 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 633 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 634 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 635 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 636 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 637 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 638 | case glslang::EbvBaseVertex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 639 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 640 | builder.addCapability(spv::CapabilityDrawParameters); |
| 641 | return spv::BuiltInBaseVertex; |
| 642 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 643 | case glslang::EbvBaseInstance: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 644 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 645 | builder.addCapability(spv::CapabilityDrawParameters); |
| 646 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 647 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 648 | case glslang::EbvDrawId: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 649 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 650 | builder.addCapability(spv::CapabilityDrawParameters); |
| 651 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 652 | |
| 653 | case glslang::EbvPrimitiveId: |
| 654 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 655 | builder.addCapability(spv::CapabilityGeometry); |
| 656 | return spv::BuiltInPrimitiveId; |
| 657 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 658 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 659 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 660 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 661 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 662 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 663 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 664 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 665 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 666 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 667 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 668 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 669 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 670 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 671 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 672 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 673 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 674 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 675 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 676 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 677 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 678 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 679 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 680 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 681 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 682 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 683 | return spv::BuiltInSubgroupSize; |
| 684 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 685 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 686 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 687 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 688 | return spv::BuiltInSubgroupLocalInvocationId; |
| 689 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 690 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 691 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 692 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 693 | return spv::BuiltInSubgroupEqMaskKHR; |
| 694 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 695 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 696 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 697 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 698 | return spv::BuiltInSubgroupGeMaskKHR; |
| 699 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 700 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 701 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 702 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 703 | return spv::BuiltInSubgroupGtMaskKHR; |
| 704 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 705 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 706 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 707 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 708 | return spv::BuiltInSubgroupLeMaskKHR; |
| 709 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 710 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 711 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 712 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 713 | return spv::BuiltInSubgroupLtMaskKHR; |
| 714 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 715 | case glslang::EbvNumSubgroups: |
| 716 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 717 | return spv::BuiltInNumSubgroups; |
| 718 | |
| 719 | case glslang::EbvSubgroupID: |
| 720 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 721 | return spv::BuiltInSubgroupId; |
| 722 | |
| 723 | case glslang::EbvSubgroupSize2: |
| 724 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 725 | return spv::BuiltInSubgroupSize; |
| 726 | |
| 727 | case glslang::EbvSubgroupInvocation2: |
| 728 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 729 | return spv::BuiltInSubgroupLocalInvocationId; |
| 730 | |
| 731 | case glslang::EbvSubgroupEqMask2: |
| 732 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 733 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 734 | return spv::BuiltInSubgroupEqMask; |
| 735 | |
| 736 | case glslang::EbvSubgroupGeMask2: |
| 737 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 738 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 739 | return spv::BuiltInSubgroupGeMask; |
| 740 | |
| 741 | case glslang::EbvSubgroupGtMask2: |
| 742 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 743 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 744 | return spv::BuiltInSubgroupGtMask; |
| 745 | |
| 746 | case glslang::EbvSubgroupLeMask2: |
| 747 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 748 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 749 | return spv::BuiltInSubgroupLeMask; |
| 750 | |
| 751 | case glslang::EbvSubgroupLtMask2: |
| 752 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 753 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 754 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 755 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 756 | case glslang::EbvBaryCoordNoPersp: |
| 757 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 758 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 759 | |
| 760 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 761 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 762 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 763 | |
| 764 | case glslang::EbvBaryCoordNoPerspSample: |
| 765 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 766 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 767 | |
| 768 | case glslang::EbvBaryCoordSmooth: |
| 769 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 770 | return spv::BuiltInBaryCoordSmoothAMD; |
| 771 | |
| 772 | case glslang::EbvBaryCoordSmoothCentroid: |
| 773 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 774 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 775 | |
| 776 | case glslang::EbvBaryCoordSmoothSample: |
| 777 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 778 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 779 | |
| 780 | case glslang::EbvBaryCoordPullModel: |
| 781 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 782 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 783 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 784 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 785 | case glslang::EbvDeviceIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 786 | addPre13Extension(spv::E_SPV_KHR_device_group); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 787 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 788 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 789 | |
| 790 | case glslang::EbvViewIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 791 | addPre13Extension(spv::E_SPV_KHR_multiview); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 792 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 793 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 794 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 795 | #ifdef NV_EXTENSIONS |
| 796 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 797 | if (!memberDeclaration) { |
| 798 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 799 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 800 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 801 | return spv::BuiltInViewportMaskNV; |
| 802 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 803 | if (!memberDeclaration) { |
| 804 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 805 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 806 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 807 | return spv::BuiltInSecondaryPositionNV; |
| 808 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 809 | if (!memberDeclaration) { |
| 810 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 811 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 812 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 813 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 814 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 815 | if (!memberDeclaration) { |
| 816 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 817 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 818 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 819 | return spv::BuiltInPositionPerViewNV; |
| 820 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 821 | if (!memberDeclaration) { |
| 822 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 823 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 824 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 825 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 826 | case glslang::EbvFragFullyCoveredNV: |
| 827 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 828 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 829 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 830 | case glslang::EbvBaryCoordNV: |
| 831 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 832 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 833 | return spv::BuiltInBaryCoordNV; |
| 834 | case glslang::EbvBaryCoordNoPerspNV: |
| 835 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 836 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 837 | return spv::BuiltInBaryCoordNoPerspNV; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 838 | #endif |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 839 | default: |
| 840 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 844 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 845 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 846 | { |
| 847 | assert(type.getBasicType() == glslang::EbtSampler); |
| 848 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 849 | // Check for capabilities |
| 850 | switch (type.getQualifier().layoutFormat) { |
| 851 | case glslang::ElfRg32f: |
| 852 | case glslang::ElfRg16f: |
| 853 | case glslang::ElfR11fG11fB10f: |
| 854 | case glslang::ElfR16f: |
| 855 | case glslang::ElfRgba16: |
| 856 | case glslang::ElfRgb10A2: |
| 857 | case glslang::ElfRg16: |
| 858 | case glslang::ElfRg8: |
| 859 | case glslang::ElfR16: |
| 860 | case glslang::ElfR8: |
| 861 | case glslang::ElfRgba16Snorm: |
| 862 | case glslang::ElfRg16Snorm: |
| 863 | case glslang::ElfRg8Snorm: |
| 864 | case glslang::ElfR16Snorm: |
| 865 | case glslang::ElfR8Snorm: |
| 866 | |
| 867 | case glslang::ElfRg32i: |
| 868 | case glslang::ElfRg16i: |
| 869 | case glslang::ElfRg8i: |
| 870 | case glslang::ElfR16i: |
| 871 | case glslang::ElfR8i: |
| 872 | |
| 873 | case glslang::ElfRgb10a2ui: |
| 874 | case glslang::ElfRg32ui: |
| 875 | case glslang::ElfRg16ui: |
| 876 | case glslang::ElfRg8ui: |
| 877 | case glslang::ElfR16ui: |
| 878 | case glslang::ElfR8ui: |
| 879 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 880 | break; |
| 881 | |
| 882 | default: |
| 883 | break; |
| 884 | } |
| 885 | |
| 886 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 887 | switch (type.getQualifier().layoutFormat) { |
| 888 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 889 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 890 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 891 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 892 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 893 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 894 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 895 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 896 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 897 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 898 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 899 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 900 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 901 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 902 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 903 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 904 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 905 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 906 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 907 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 908 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 909 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 910 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 911 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 912 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 913 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 914 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 915 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 916 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 917 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 918 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 919 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 920 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 921 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 922 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 923 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 924 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 925 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 926 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 927 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 928 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 932 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 933 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 934 | if (selectionNode.getFlatten()) |
| 935 | return spv::SelectionControlFlattenMask; |
| 936 | if (selectionNode.getDontFlatten()) |
| 937 | return spv::SelectionControlDontFlattenMask; |
| 938 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 939 | } |
| 940 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 941 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 942 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 943 | if (switchNode.getFlatten()) |
| 944 | return spv::SelectionControlFlattenMask; |
| 945 | if (switchNode.getDontFlatten()) |
| 946 | return spv::SelectionControlDontFlattenMask; |
| 947 | return spv::SelectionControlMaskNone; |
| 948 | } |
| 949 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 950 | // return a non-0 dependency if the dependency argument must be set |
| 951 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
| 952 | unsigned int& dependencyLength) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 953 | { |
| 954 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 955 | |
| 956 | if (loopNode.getDontUnroll()) |
| 957 | control = control | spv::LoopControlDontUnrollMask; |
| 958 | if (loopNode.getUnroll()) |
| 959 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 960 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 961 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 962 | else if (loopNode.getLoopDependency() > 0) { |
| 963 | control = control | spv::LoopControlDependencyLengthMask; |
| 964 | dependencyLength = loopNode.getLoopDependency(); |
| 965 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 966 | |
| 967 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 968 | } |
| 969 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 970 | // Translate glslang type to SPIR-V storage class. |
| 971 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 972 | { |
| 973 | if (type.getQualifier().isPipeInput()) |
| 974 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 975 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 976 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 977 | |
| 978 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
| 979 | type.getQualifier().storage == glslang::EvqUniform) { |
| 980 | if (type.getBasicType() == glslang::EbtAtomicUint) |
| 981 | return spv::StorageClassAtomicCounter; |
| 982 | if (type.containsOpaque()) |
| 983 | return spv::StorageClassUniformConstant; |
| 984 | } |
| 985 | |
| 986 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 987 | addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class); |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 988 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 992 | if (type.getQualifier().layoutPushConstant) |
| 993 | return spv::StorageClassPushConstant; |
| 994 | if (type.getBasicType() == glslang::EbtBlock) |
| 995 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 996 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 997 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 998 | |
| 999 | switch (type.getQualifier().storage) { |
| 1000 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
| 1001 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1002 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1003 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
| 1004 | default: |
| 1005 | assert(0); |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1010 | } |
| 1011 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1012 | // Add capabilities pertaining to how an array is indexed. |
| 1013 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1014 | const glslang::TType& indexType) |
| 1015 | { |
| 1016 | if (indexType.getQualifier().isNonUniform()) { |
| 1017 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1018 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1019 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1020 | if (baseType.getQualifier().hasAttachment()) |
| 1021 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
| 1022 | else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1023 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
| 1024 | else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) |
| 1025 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1026 | else if (baseType.isImage()) |
| 1027 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1028 | else if (baseType.isTexture()) |
| 1029 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1030 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1031 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1032 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1033 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1034 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1035 | } |
| 1036 | } else { |
| 1037 | // assume a dynamically uniform index |
| 1038 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1039 | if (baseType.getQualifier().hasAttachment()) { |
| 1040 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1041 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1042 | } else if (baseType.isImage() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1043 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1044 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1045 | } else if (baseType.isTexture() && baseType.getSampler().dim == glslang::EsdBuffer) { |
| 1046 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1047 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1048 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1053 | // 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] | 1054 | // descriptor set. |
| 1055 | bool IsDescriptorResource(const glslang::TType& type) |
| 1056 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1057 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1058 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1059 | return type.getQualifier().isUniformOrBuffer() && ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1060 | |
| 1061 | // non block... |
| 1062 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1063 | // if they are the global-scope-class, not the function parameter |
| 1064 | // (or local, if they ever exist) class. |
| 1065 | if (type.getBasicType() == glslang::EbtSampler) |
| 1066 | return type.getQualifier().isUniformOrBuffer(); |
| 1067 | |
| 1068 | // None of the above. |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1072 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1073 | { |
| 1074 | if (child.layoutMatrix == glslang::ElmNone) |
| 1075 | child.layoutMatrix = parent.layoutMatrix; |
| 1076 | |
| 1077 | if (parent.invariant) |
| 1078 | child.invariant = true; |
| 1079 | if (parent.nopersp) |
| 1080 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1081 | #ifdef AMD_EXTENSIONS |
| 1082 | if (parent.explicitInterp) |
| 1083 | child.explicitInterp = true; |
| 1084 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1085 | if (parent.flat) |
| 1086 | child.flat = true; |
| 1087 | if (parent.centroid) |
| 1088 | child.centroid = true; |
| 1089 | if (parent.patch) |
| 1090 | child.patch = true; |
| 1091 | if (parent.sample) |
| 1092 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1093 | if (parent.coherent) |
| 1094 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1095 | if (parent.devicecoherent) |
| 1096 | child.devicecoherent = true; |
| 1097 | if (parent.queuefamilycoherent) |
| 1098 | child.queuefamilycoherent = true; |
| 1099 | if (parent.workgroupcoherent) |
| 1100 | child.workgroupcoherent = true; |
| 1101 | if (parent.subgroupcoherent) |
| 1102 | child.subgroupcoherent = true; |
| 1103 | if (parent.nonprivate) |
| 1104 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1105 | if (parent.volatil) |
| 1106 | child.volatil = true; |
| 1107 | if (parent.restrict) |
| 1108 | child.restrict = true; |
| 1109 | if (parent.readonly) |
| 1110 | child.readonly = true; |
| 1111 | if (parent.writeonly) |
| 1112 | child.writeonly = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1115 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1116 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1117 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1118 | // - struct members might inherit from a struct declaration |
| 1119 | // (note that non-block structs don't explicitly inherit, |
| 1120 | // only implicitly, meaning no decoration involved) |
| 1121 | // - affect decorations on the struct members |
| 1122 | // (note smooth does not, and expecting something like volatile |
| 1123 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1124 | // - 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] | 1125 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1128 | // |
| 1129 | // Implement the TGlslangToSpvTraverser class. |
| 1130 | // |
| 1131 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1132 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1133 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 1134 | : TIntermTraverser(true, false, true), |
| 1135 | options(options), |
| 1136 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1137 | sequenceDepth(0), logger(buildLogger), |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1138 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1139 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1140 | glslangIntermediate(glslangIntermediate) |
| 1141 | { |
| 1142 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1143 | |
| 1144 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1145 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1146 | glslangIntermediate->getVersion()); |
| 1147 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1148 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1149 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1150 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1151 | |
| 1152 | // Set the source shader's text. If for SPV version 1.0, include |
| 1153 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1154 | // Otherwise, emit those as actual instructions. |
| 1155 | std::string text; |
| 1156 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1157 | for (int p = 0; p < (int)processes.size(); ++p) { |
| 1158 | if (glslangIntermediate->getSpv().spv < 0x00010100) { |
| 1159 | text.append("// OpModuleProcessed "); |
| 1160 | text.append(processes[p]); |
| 1161 | text.append("\n"); |
| 1162 | } else |
| 1163 | builder.addModuleProcessed(processes[p]); |
| 1164 | } |
| 1165 | if (glslangIntermediate->getSpv().spv < 0x00010100 && (int)processes.size() > 0) |
| 1166 | text.append("#line 1\n"); |
| 1167 | text.append(glslangIntermediate->getSourceText()); |
| 1168 | builder.setSourceText(text); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1169 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1170 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1171 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 1172 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelVulkanKHR); |
| 1173 | builder.addExtension(spv::E_SPV_KHR_vulkan_memory_model); |
| 1174 | } else { |
| 1175 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); |
| 1176 | } |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1177 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1178 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1179 | |
| 1180 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1181 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1182 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1183 | builder.addSourceExtension(it->c_str()); |
| 1184 | |
| 1185 | // Add the top-level modes for this shader. |
| 1186 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1187 | if (glslangIntermediate->getXfbMode()) { |
| 1188 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1189 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1190 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1191 | |
| 1192 | unsigned int mode; |
| 1193 | switch (glslangIntermediate->getStage()) { |
| 1194 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1195 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1196 | break; |
| 1197 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1198 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1199 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1200 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1201 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1202 | glslang::TLayoutGeometry primitive; |
| 1203 | |
| 1204 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1205 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1206 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1207 | } else { |
| 1208 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1209 | } |
| 1210 | |
| 1211 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1212 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1213 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1214 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1215 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1216 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1217 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1218 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1219 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1220 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1221 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1222 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1223 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1224 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1225 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1226 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1227 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1228 | |
| 1229 | switch (glslangIntermediate->getVertexOrder()) { |
| 1230 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1231 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1232 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1233 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1234 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1235 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1236 | |
| 1237 | if (glslangIntermediate->getPointMode()) |
| 1238 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1239 | break; |
| 1240 | |
| 1241 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1242 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1243 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1244 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1245 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1246 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1247 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1248 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1249 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1250 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1251 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1252 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1253 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1254 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1255 | |
| 1256 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1257 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1258 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1259 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1260 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1261 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1262 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1263 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1264 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1265 | break; |
| 1266 | |
| 1267 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1268 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1269 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1270 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1271 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1272 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1273 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1274 | else |
| 1275 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1276 | |
| 1277 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1278 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1279 | |
chaoc | c120452 | 2017-06-30 17:14:30 -0700 | [diff] [blame] | 1280 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1281 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1282 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1283 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1284 | } |
| 1285 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1286 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1287 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1288 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1289 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1290 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1291 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1292 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1293 | |
| 1294 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1295 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1296 | break; |
| 1297 | |
| 1298 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1299 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 1300 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1301 | glslangIntermediate->getLocalSize(1), |
| 1302 | glslangIntermediate->getLocalSize(2)); |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame^] | 1303 | #ifdef NV_EXTENSIONS |
| 1304 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1305 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1306 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1307 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1308 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1309 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1310 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1311 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1312 | } |
| 1313 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1314 | break; |
| 1315 | |
| 1316 | default: |
| 1317 | break; |
| 1318 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1319 | } |
| 1320 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1321 | // Finish creating SPV, after the traversal is complete. |
| 1322 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1323 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1324 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1325 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1326 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1327 | builder.leaveFunction(); |
| 1328 | } |
| 1329 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1330 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1331 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1332 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1333 | |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1334 | // Add capabilities, extensions, remove unneeded decorations, etc., |
| 1335 | // based on the resulting SPIR-V. |
| 1336 | builder.postProcess(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1339 | // Write the SPV into 'out'. |
| 1340 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1341 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1342 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | // |
| 1346 | // Implement the traversal functions. |
| 1347 | // |
| 1348 | // Return true from interior nodes to have the external traversal |
| 1349 | // continue on to children. Return false if children were |
| 1350 | // already processed. |
| 1351 | // |
| 1352 | |
| 1353 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1354 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1355 | // - uniform/input reads |
| 1356 | // - output writes |
| 1357 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1358 | // - something simple that degenerates into the last bullet |
| 1359 | // |
| 1360 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1361 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1362 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1363 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1364 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1365 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1366 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1367 | // Formal function parameters were mapped during makeFunctions(). |
| 1368 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1369 | |
| 1370 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1371 | if (builder.isPointer(id)) { |
| 1372 | spv::StorageClass sc = builder.getStorageClass(id); |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1373 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) { |
| 1374 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) |
| 1375 | iOSet.insert(id); |
| 1376 | } |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1380 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1381 | // Prepare to generate code for the access |
| 1382 | |
| 1383 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1384 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1385 | // followed by setting the base. |
| 1386 | builder.clearAccessChain(); |
| 1387 | |
| 1388 | // 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] | 1389 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1390 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1391 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1392 | // 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] | 1393 | // These are also pure R-values. |
| 1394 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1395 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1396 | builder.setAccessChainRValue(id); |
| 1397 | else |
| 1398 | builder.setAccessChainLValue(id); |
| 1399 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1400 | |
| 1401 | // Process linkage-only nodes for any special additional interface work. |
| 1402 | if (linkageOnly) { |
| 1403 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1404 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1405 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1406 | // of declaration. |
| 1407 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1408 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1409 | // Save possible originating buffers for counter buffers, keyed by |
| 1410 | // making the potential counter-buffer name. |
| 1411 | std::string keyName = symbol->getName().c_str(); |
| 1412 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1413 | counterOriginator[keyName] = symbol; |
| 1414 | } else { |
| 1415 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1416 | std::string keyName = symbol->getName().c_str(); |
| 1417 | auto it = counterOriginator.find(keyName); |
| 1418 | if (it != counterOriginator.end()) { |
| 1419 | id = getSymbolId(it->second); |
| 1420 | if (id != spv::NoResult) { |
| 1421 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1422 | if (counterId != spv::NoResult) { |
| 1423 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1424 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1425 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1435 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1436 | builder.setLine(node->getLoc().line); |
| 1437 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1438 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1439 | if (node->getType().getQualifier().isSpecConstant()) |
| 1440 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1441 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1442 | // First, handle special cases |
| 1443 | switch (node->getOp()) { |
| 1444 | case glslang::EOpAssign: |
| 1445 | case glslang::EOpAddAssign: |
| 1446 | case glslang::EOpSubAssign: |
| 1447 | case glslang::EOpMulAssign: |
| 1448 | case glslang::EOpVectorTimesMatrixAssign: |
| 1449 | case glslang::EOpVectorTimesScalarAssign: |
| 1450 | case glslang::EOpMatrixTimesScalarAssign: |
| 1451 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1452 | case glslang::EOpDivAssign: |
| 1453 | case glslang::EOpModAssign: |
| 1454 | case glslang::EOpAndAssign: |
| 1455 | case glslang::EOpInclusiveOrAssign: |
| 1456 | case glslang::EOpExclusiveOrAssign: |
| 1457 | case glslang::EOpLeftShiftAssign: |
| 1458 | case glslang::EOpRightShiftAssign: |
| 1459 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1460 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1461 | // says to evaluate the left before the right. So, always, left |
| 1462 | // node then right node. |
| 1463 | { |
| 1464 | // get the left l-value, save it away |
| 1465 | builder.clearAccessChain(); |
| 1466 | node->getLeft()->traverse(this); |
| 1467 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1468 | |
| 1469 | // evaluate the right |
| 1470 | builder.clearAccessChain(); |
| 1471 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1472 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1473 | |
| 1474 | if (node->getOp() != glslang::EOpAssign) { |
| 1475 | // the left is also an r-value |
| 1476 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1477 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1478 | |
| 1479 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1480 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1481 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1482 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1483 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1484 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1485 | node->getType().getBasicType()); |
| 1486 | |
| 1487 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1488 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | // store the result |
| 1492 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1493 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1494 | |
| 1495 | // assignments are expressions having an rValue after they are evaluated... |
| 1496 | builder.clearAccessChain(); |
| 1497 | builder.setAccessChainRValue(rValue); |
| 1498 | } |
| 1499 | return false; |
| 1500 | case glslang::EOpIndexDirect: |
| 1501 | case glslang::EOpIndexDirectStruct: |
| 1502 | { |
| 1503 | // Get the left part of the access chain. |
| 1504 | node->getLeft()->traverse(this); |
| 1505 | |
| 1506 | // Add the next element in the chain |
| 1507 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1508 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1509 | if (! node->getLeft()->getType().isArray() && |
| 1510 | node->getLeft()->getType().isVector() && |
| 1511 | node->getOp() == glslang::EOpIndexDirect) { |
| 1512 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1513 | // so short circuit the access-chain stuff with a swizzle. |
| 1514 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1515 | swizzle.push_back(glslangIndex); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1516 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1517 | } else { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1518 | int spvIndex = glslangIndex; |
| 1519 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1520 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1521 | { |
| 1522 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1523 | // index remapping due to hidden members in anonymous blocks. |
| 1524 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1525 | assert(remapper.size() > 0); |
| 1526 | spvIndex = remapper[glslangIndex]; |
| 1527 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1528 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1529 | // normal case for indexing array or structure or block |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1530 | builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType())); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1531 | |
| 1532 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1533 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1534 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1535 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | return false; |
| 1539 | case glslang::EOpIndexIndirect: |
| 1540 | { |
| 1541 | // Structure or array or vector indirection. |
| 1542 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 1543 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1544 | // Will use the access chain's 'component' for variable index into a vector. |
| 1545 | |
| 1546 | // This adapter is building access chains left to right. |
| 1547 | // Set up the access chain to the left. |
| 1548 | node->getLeft()->traverse(this); |
| 1549 | |
| 1550 | // save it so that computing the right side doesn't trash it |
| 1551 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1552 | |
| 1553 | // compute the next index in the chain |
| 1554 | builder.clearAccessChain(); |
| 1555 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1556 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1557 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1558 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1559 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1560 | // restore the saved access chain |
| 1561 | builder.setAccessChain(partial); |
| 1562 | |
| 1563 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1564 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1565 | else |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1566 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1567 | } |
| 1568 | return false; |
| 1569 | case glslang::EOpVectorSwizzle: |
| 1570 | { |
| 1571 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1572 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1573 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1574 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1575 | } |
| 1576 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1577 | case glslang::EOpMatrixSwizzle: |
| 1578 | logger->missingFunctionality("matrix swizzle"); |
| 1579 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1580 | case glslang::EOpLogicalOr: |
| 1581 | case glslang::EOpLogicalAnd: |
| 1582 | { |
| 1583 | |
| 1584 | // These may require short circuiting, but can sometimes be done as straight |
| 1585 | // binary operations. The right operand must be short circuited if it has |
| 1586 | // side effects, and should probably be if it is complex. |
| 1587 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1588 | break; // handle below as a normal binary operation |
| 1589 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1590 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1591 | builder.clearAccessChain(); |
| 1592 | builder.setAccessChainRValue(result); |
| 1593 | } |
| 1594 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1595 | default: |
| 1596 | break; |
| 1597 | } |
| 1598 | |
| 1599 | // Assume generic binary op... |
| 1600 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1601 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1602 | builder.clearAccessChain(); |
| 1603 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1604 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1605 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1606 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1607 | builder.clearAccessChain(); |
| 1608 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1609 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1610 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1611 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1612 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1613 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1614 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1615 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1616 | convertGlslangToSpvType(node->getType()), left, right, |
| 1617 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1618 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1619 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1620 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1621 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1622 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1623 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1624 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1625 | return false; |
| 1626 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1630 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1631 | builder.setLine(node->getLoc().line); |
| 1632 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1633 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1634 | if (node->getType().getQualifier().isSpecConstant()) |
| 1635 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1636 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1637 | spv::Id result = spv::NoResult; |
| 1638 | |
| 1639 | // try texturing first |
| 1640 | result = createImageTextureFunctionCall(node); |
| 1641 | if (result != spv::NoResult) { |
| 1642 | builder.clearAccessChain(); |
| 1643 | builder.setAccessChainRValue(result); |
| 1644 | |
| 1645 | return false; // done with this node |
| 1646 | } |
| 1647 | |
| 1648 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1649 | |
| 1650 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1651 | // Quite special; won't want to evaluate the operand. |
| 1652 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1653 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 1654 | // except for the last block membeor of an SSBO. |
| 1655 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 1656 | // size extracted. |
| 1657 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1658 | // Normal .length() would have been constant folded by the front-end. |
| 1659 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1660 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1661 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1662 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1663 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1664 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1665 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1666 | |
| 1667 | builder.clearAccessChain(); |
| 1668 | builder.setAccessChainRValue(length); |
| 1669 | |
| 1670 | return false; |
| 1671 | } |
| 1672 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1673 | // Start by evaluating the operand |
| 1674 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1675 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1676 | // operate first on the swizzle base, then apply the swizzle. |
| 1677 | spv::Id invertedType = spv::NoType; |
| 1678 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 1679 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 1680 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 1681 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1682 | builder.clearAccessChain(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1683 | if (invertedType != spv::NoType) |
| 1684 | node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this); |
| 1685 | else |
| 1686 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1687 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1688 | spv::Id operand = spv::NoResult; |
| 1689 | |
| 1690 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1691 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1692 | node->getOp() == glslang::EOpAtomicCounter || |
| 1693 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1694 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1695 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1696 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1697 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1698 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1699 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1700 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1701 | |
| 1702 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1703 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1704 | result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1705 | |
| 1706 | // if not, then possibly an operation |
| 1707 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1708 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1709 | |
| 1710 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1711 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1712 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1713 | builder.addDecoration(result, decorations.nonUniform); |
| 1714 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1715 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1716 | builder.clearAccessChain(); |
| 1717 | builder.setAccessChainRValue(result); |
| 1718 | |
| 1719 | return false; // done with this node |
| 1720 | } |
| 1721 | |
| 1722 | // it must be a special case, check... |
| 1723 | switch (node->getOp()) { |
| 1724 | case glslang::EOpPostIncrement: |
| 1725 | case glslang::EOpPostDecrement: |
| 1726 | case glslang::EOpPreIncrement: |
| 1727 | case glslang::EOpPreDecrement: |
| 1728 | { |
| 1729 | // 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] | 1730 | spv::Id one = 0; |
| 1731 | if (node->getBasicType() == glslang::EbtFloat) |
| 1732 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 1733 | else if (node->getBasicType() == glslang::EbtDouble) |
| 1734 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1735 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 1736 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1737 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 1738 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1739 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 1740 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1741 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1742 | one = builder.makeInt64Constant(1); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1743 | else |
| 1744 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1745 | glslang::TOperator op; |
| 1746 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1747 | node->getOp() == glslang::EOpPostIncrement) |
| 1748 | op = glslang::EOpAdd; |
| 1749 | else |
| 1750 | op = glslang::EOpSub; |
| 1751 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1752 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1753 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1754 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1755 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1756 | |
| 1757 | // The result of operation is always stored, but conditionally the |
| 1758 | // consumed result. The consumed result is always an r-value. |
| 1759 | builder.accessChainStore(result); |
| 1760 | builder.clearAccessChain(); |
| 1761 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1762 | node->getOp() == glslang::EOpPreDecrement) |
| 1763 | builder.setAccessChainRValue(result); |
| 1764 | else |
| 1765 | builder.setAccessChainRValue(operand); |
| 1766 | } |
| 1767 | |
| 1768 | return false; |
| 1769 | |
| 1770 | case glslang::EOpEmitStreamVertex: |
| 1771 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1772 | return false; |
| 1773 | case glslang::EOpEndStreamPrimitive: |
| 1774 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1775 | return false; |
| 1776 | |
| 1777 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1778 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1779 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1780 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 1784 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 1785 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1786 | if (node->getType().getQualifier().isSpecConstant()) |
| 1787 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1788 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1789 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1790 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 1791 | 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] | 1792 | |
| 1793 | // try texturing |
| 1794 | result = createImageTextureFunctionCall(node); |
| 1795 | if (result != spv::NoResult) { |
| 1796 | builder.clearAccessChain(); |
| 1797 | builder.setAccessChainRValue(result); |
| 1798 | |
| 1799 | return false; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1800 | } else if (node->getOp() == glslang::EOpImageStore || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1801 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1802 | node->getOp() == glslang::EOpImageStoreLod || |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1803 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1804 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1805 | // "imageStore" is a special case, which has no result |
| 1806 | return false; |
| 1807 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1808 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1809 | glslang::TOperator binOp = glslang::EOpNull; |
| 1810 | bool reduceComparison = true; |
| 1811 | bool isMatrix = false; |
| 1812 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1813 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1814 | |
| 1815 | assert(node->getOp()); |
| 1816 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1817 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1818 | |
| 1819 | switch (node->getOp()) { |
| 1820 | case glslang::EOpSequence: |
| 1821 | { |
| 1822 | if (preVisit) |
| 1823 | ++sequenceDepth; |
| 1824 | else |
| 1825 | --sequenceDepth; |
| 1826 | |
| 1827 | if (sequenceDepth == 1) { |
| 1828 | // If this is the parent node of all the functions, we want to see them |
| 1829 | // early, so all call points have actual SPIR-V functions to reference. |
| 1830 | // In all cases, still let the traverser visit the children for us. |
| 1831 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 1832 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1833 | // 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] | 1834 | // anything else gets there, so visit out of order, doing them all now. |
| 1835 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 1836 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 1837 | // 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] | 1838 | // so do them manually. |
| 1839 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 1840 | |
| 1841 | return false; |
| 1842 | } |
| 1843 | |
| 1844 | return true; |
| 1845 | } |
| 1846 | case glslang::EOpLinkerObjects: |
| 1847 | { |
| 1848 | if (visit == glslang::EvPreVisit) |
| 1849 | linkageOnly = true; |
| 1850 | else |
| 1851 | linkageOnly = false; |
| 1852 | |
| 1853 | return true; |
| 1854 | } |
| 1855 | case glslang::EOpComma: |
| 1856 | { |
| 1857 | // processing from left to right naturally leaves the right-most |
| 1858 | // lying around in the access chain |
| 1859 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1860 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 1861 | glslangOperands[i]->traverse(this); |
| 1862 | |
| 1863 | return false; |
| 1864 | } |
| 1865 | case glslang::EOpFunction: |
| 1866 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1867 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1868 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1869 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1870 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1871 | } else { |
| 1872 | handleFunctionEntry(node); |
| 1873 | } |
| 1874 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1875 | if (inEntryPoint) |
| 1876 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 1877 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1878 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | return true; |
| 1882 | case glslang::EOpParameters: |
| 1883 | // Parameters will have been consumed by EOpFunction processing, but not |
| 1884 | // the body, so we still visited the function node's children, making this |
| 1885 | // child redundant. |
| 1886 | return false; |
| 1887 | case glslang::EOpFunctionCall: |
| 1888 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1889 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1890 | if (node->isUserDefined()) |
| 1891 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 1892 | // 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] | 1893 | if (result) { |
| 1894 | builder.clearAccessChain(); |
| 1895 | builder.setAccessChainRValue(result); |
| 1896 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1897 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1898 | |
| 1899 | return false; |
| 1900 | } |
| 1901 | case glslang::EOpConstructMat2x2: |
| 1902 | case glslang::EOpConstructMat2x3: |
| 1903 | case glslang::EOpConstructMat2x4: |
| 1904 | case glslang::EOpConstructMat3x2: |
| 1905 | case glslang::EOpConstructMat3x3: |
| 1906 | case glslang::EOpConstructMat3x4: |
| 1907 | case glslang::EOpConstructMat4x2: |
| 1908 | case glslang::EOpConstructMat4x3: |
| 1909 | case glslang::EOpConstructMat4x4: |
| 1910 | case glslang::EOpConstructDMat2x2: |
| 1911 | case glslang::EOpConstructDMat2x3: |
| 1912 | case glslang::EOpConstructDMat2x4: |
| 1913 | case glslang::EOpConstructDMat3x2: |
| 1914 | case glslang::EOpConstructDMat3x3: |
| 1915 | case glslang::EOpConstructDMat3x4: |
| 1916 | case glslang::EOpConstructDMat4x2: |
| 1917 | case glslang::EOpConstructDMat4x3: |
| 1918 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 1919 | case glslang::EOpConstructIMat2x2: |
| 1920 | case glslang::EOpConstructIMat2x3: |
| 1921 | case glslang::EOpConstructIMat2x4: |
| 1922 | case glslang::EOpConstructIMat3x2: |
| 1923 | case glslang::EOpConstructIMat3x3: |
| 1924 | case glslang::EOpConstructIMat3x4: |
| 1925 | case glslang::EOpConstructIMat4x2: |
| 1926 | case glslang::EOpConstructIMat4x3: |
| 1927 | case glslang::EOpConstructIMat4x4: |
| 1928 | case glslang::EOpConstructUMat2x2: |
| 1929 | case glslang::EOpConstructUMat2x3: |
| 1930 | case glslang::EOpConstructUMat2x4: |
| 1931 | case glslang::EOpConstructUMat3x2: |
| 1932 | case glslang::EOpConstructUMat3x3: |
| 1933 | case glslang::EOpConstructUMat3x4: |
| 1934 | case glslang::EOpConstructUMat4x2: |
| 1935 | case glslang::EOpConstructUMat4x3: |
| 1936 | case glslang::EOpConstructUMat4x4: |
| 1937 | case glslang::EOpConstructBMat2x2: |
| 1938 | case glslang::EOpConstructBMat2x3: |
| 1939 | case glslang::EOpConstructBMat2x4: |
| 1940 | case glslang::EOpConstructBMat3x2: |
| 1941 | case glslang::EOpConstructBMat3x3: |
| 1942 | case glslang::EOpConstructBMat3x4: |
| 1943 | case glslang::EOpConstructBMat4x2: |
| 1944 | case glslang::EOpConstructBMat4x3: |
| 1945 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1946 | case glslang::EOpConstructF16Mat2x2: |
| 1947 | case glslang::EOpConstructF16Mat2x3: |
| 1948 | case glslang::EOpConstructF16Mat2x4: |
| 1949 | case glslang::EOpConstructF16Mat3x2: |
| 1950 | case glslang::EOpConstructF16Mat3x3: |
| 1951 | case glslang::EOpConstructF16Mat3x4: |
| 1952 | case glslang::EOpConstructF16Mat4x2: |
| 1953 | case glslang::EOpConstructF16Mat4x3: |
| 1954 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1955 | isMatrix = true; |
| 1956 | // fall through |
| 1957 | case glslang::EOpConstructFloat: |
| 1958 | case glslang::EOpConstructVec2: |
| 1959 | case glslang::EOpConstructVec3: |
| 1960 | case glslang::EOpConstructVec4: |
| 1961 | case glslang::EOpConstructDouble: |
| 1962 | case glslang::EOpConstructDVec2: |
| 1963 | case glslang::EOpConstructDVec3: |
| 1964 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1965 | case glslang::EOpConstructFloat16: |
| 1966 | case glslang::EOpConstructF16Vec2: |
| 1967 | case glslang::EOpConstructF16Vec3: |
| 1968 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1969 | case glslang::EOpConstructBool: |
| 1970 | case glslang::EOpConstructBVec2: |
| 1971 | case glslang::EOpConstructBVec3: |
| 1972 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1973 | case glslang::EOpConstructInt8: |
| 1974 | case glslang::EOpConstructI8Vec2: |
| 1975 | case glslang::EOpConstructI8Vec3: |
| 1976 | case glslang::EOpConstructI8Vec4: |
| 1977 | case glslang::EOpConstructUint8: |
| 1978 | case glslang::EOpConstructU8Vec2: |
| 1979 | case glslang::EOpConstructU8Vec3: |
| 1980 | case glslang::EOpConstructU8Vec4: |
| 1981 | case glslang::EOpConstructInt16: |
| 1982 | case glslang::EOpConstructI16Vec2: |
| 1983 | case glslang::EOpConstructI16Vec3: |
| 1984 | case glslang::EOpConstructI16Vec4: |
| 1985 | case glslang::EOpConstructUint16: |
| 1986 | case glslang::EOpConstructU16Vec2: |
| 1987 | case glslang::EOpConstructU16Vec3: |
| 1988 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1989 | case glslang::EOpConstructInt: |
| 1990 | case glslang::EOpConstructIVec2: |
| 1991 | case glslang::EOpConstructIVec3: |
| 1992 | case glslang::EOpConstructIVec4: |
| 1993 | case glslang::EOpConstructUint: |
| 1994 | case glslang::EOpConstructUVec2: |
| 1995 | case glslang::EOpConstructUVec3: |
| 1996 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1997 | case glslang::EOpConstructInt64: |
| 1998 | case glslang::EOpConstructI64Vec2: |
| 1999 | case glslang::EOpConstructI64Vec3: |
| 2000 | case glslang::EOpConstructI64Vec4: |
| 2001 | case glslang::EOpConstructUint64: |
| 2002 | case glslang::EOpConstructU64Vec2: |
| 2003 | case glslang::EOpConstructU64Vec3: |
| 2004 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2005 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2006 | case glslang::EOpConstructTextureSampler: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2007 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2008 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2009 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2010 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2011 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2012 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2013 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2014 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2015 | std::vector<spv::Id> constituents; |
| 2016 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2017 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2018 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2019 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2020 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2021 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2022 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2023 | |
| 2024 | builder.clearAccessChain(); |
| 2025 | builder.setAccessChainRValue(constructed); |
| 2026 | |
| 2027 | return false; |
| 2028 | } |
| 2029 | |
| 2030 | // These six are component-wise compares with component-wise results. |
| 2031 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2032 | case glslang::EOpLessThan: |
| 2033 | case glslang::EOpGreaterThan: |
| 2034 | case glslang::EOpLessThanEqual: |
| 2035 | case glslang::EOpGreaterThanEqual: |
| 2036 | case glslang::EOpVectorEqual: |
| 2037 | case glslang::EOpVectorNotEqual: |
| 2038 | { |
| 2039 | // Map the operation to a binary |
| 2040 | binOp = node->getOp(); |
| 2041 | reduceComparison = false; |
| 2042 | switch (node->getOp()) { |
| 2043 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2044 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2045 | default: binOp = node->getOp(); break; |
| 2046 | } |
| 2047 | |
| 2048 | break; |
| 2049 | } |
| 2050 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2051 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2052 | binOp = glslang::EOpMul; |
| 2053 | break; |
| 2054 | case glslang::EOpOuterProduct: |
| 2055 | // two vectors multiplied to make a matrix |
| 2056 | binOp = glslang::EOpOuterProduct; |
| 2057 | break; |
| 2058 | case glslang::EOpDot: |
| 2059 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2060 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2061 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2062 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2063 | binOp = glslang::EOpMul; |
| 2064 | break; |
| 2065 | } |
| 2066 | case glslang::EOpMod: |
| 2067 | // when an aggregate, this is the floating-point mod built-in function, |
| 2068 | // which can be emitted by the one in createBinaryOperation() |
| 2069 | binOp = glslang::EOpMod; |
| 2070 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2071 | case glslang::EOpEmitVertex: |
| 2072 | case glslang::EOpEndPrimitive: |
| 2073 | case glslang::EOpBarrier: |
| 2074 | case glslang::EOpMemoryBarrier: |
| 2075 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2076 | case glslang::EOpMemoryBarrierBuffer: |
| 2077 | case glslang::EOpMemoryBarrierImage: |
| 2078 | case glslang::EOpMemoryBarrierShared: |
| 2079 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2080 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2081 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2082 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2083 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2084 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2085 | case glslang::EOpSubgroupBarrier: |
| 2086 | case glslang::EOpSubgroupMemoryBarrier: |
| 2087 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2088 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2089 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2090 | noReturnValue = true; |
| 2091 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2092 | break; |
| 2093 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2094 | case glslang::EOpAtomicStore: |
| 2095 | noReturnValue = true; |
| 2096 | // fallthrough |
| 2097 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2098 | case glslang::EOpAtomicAdd: |
| 2099 | case glslang::EOpAtomicMin: |
| 2100 | case glslang::EOpAtomicMax: |
| 2101 | case glslang::EOpAtomicAnd: |
| 2102 | case glslang::EOpAtomicOr: |
| 2103 | case glslang::EOpAtomicXor: |
| 2104 | case glslang::EOpAtomicExchange: |
| 2105 | case glslang::EOpAtomicCompSwap: |
| 2106 | atomic = true; |
| 2107 | break; |
| 2108 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2109 | case glslang::EOpAtomicCounterAdd: |
| 2110 | case glslang::EOpAtomicCounterSubtract: |
| 2111 | case glslang::EOpAtomicCounterMin: |
| 2112 | case glslang::EOpAtomicCounterMax: |
| 2113 | case glslang::EOpAtomicCounterAnd: |
| 2114 | case glslang::EOpAtomicCounterOr: |
| 2115 | case glslang::EOpAtomicCounterXor: |
| 2116 | case glslang::EOpAtomicCounterExchange: |
| 2117 | case glslang::EOpAtomicCounterCompSwap: |
| 2118 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2119 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2120 | atomic = true; |
| 2121 | break; |
| 2122 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2123 | default: |
| 2124 | break; |
| 2125 | } |
| 2126 | |
| 2127 | // |
| 2128 | // See if it maps to a regular operation. |
| 2129 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2130 | if (binOp != glslang::EOpNull) { |
| 2131 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2132 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2133 | assert(left && right); |
| 2134 | |
| 2135 | builder.clearAccessChain(); |
| 2136 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2137 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2138 | |
| 2139 | builder.clearAccessChain(); |
| 2140 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2141 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2142 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2143 | builder.setLine(node->getLoc().line); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2144 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2145 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2146 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2147 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2148 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2149 | left->getType().getBasicType(), reduceComparison); |
| 2150 | |
| 2151 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2152 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2153 | builder.clearAccessChain(); |
| 2154 | builder.setAccessChainRValue(result); |
| 2155 | |
| 2156 | return false; |
| 2157 | } |
| 2158 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2159 | // |
| 2160 | // Create the list of operands. |
| 2161 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2162 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2163 | std::vector<spv::Id> operands; |
| 2164 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2165 | // special case l-value operands; there are just a few |
| 2166 | bool lvalue = false; |
| 2167 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2168 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2169 | case glslang::EOpModf: |
| 2170 | if (arg == 1) |
| 2171 | lvalue = true; |
| 2172 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2173 | case glslang::EOpInterpolateAtSample: |
| 2174 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2175 | #ifdef AMD_EXTENSIONS |
| 2176 | case glslang::EOpInterpolateAtVertex: |
| 2177 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2178 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2179 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2180 | |
| 2181 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2182 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2183 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2184 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2185 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 2186 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2187 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2188 | case glslang::EOpAtomicAdd: |
| 2189 | case glslang::EOpAtomicMin: |
| 2190 | case glslang::EOpAtomicMax: |
| 2191 | case glslang::EOpAtomicAnd: |
| 2192 | case glslang::EOpAtomicOr: |
| 2193 | case glslang::EOpAtomicXor: |
| 2194 | case glslang::EOpAtomicExchange: |
| 2195 | case glslang::EOpAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2196 | case glslang::EOpAtomicLoad: |
| 2197 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2198 | case glslang::EOpAtomicCounterAdd: |
| 2199 | case glslang::EOpAtomicCounterSubtract: |
| 2200 | case glslang::EOpAtomicCounterMin: |
| 2201 | case glslang::EOpAtomicCounterMax: |
| 2202 | case glslang::EOpAtomicCounterAnd: |
| 2203 | case glslang::EOpAtomicCounterOr: |
| 2204 | case glslang::EOpAtomicCounterXor: |
| 2205 | case glslang::EOpAtomicCounterExchange: |
| 2206 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2207 | if (arg == 0) |
| 2208 | lvalue = true; |
| 2209 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2210 | case glslang::EOpAddCarry: |
| 2211 | case glslang::EOpSubBorrow: |
| 2212 | if (arg == 2) |
| 2213 | lvalue = true; |
| 2214 | break; |
| 2215 | case glslang::EOpUMulExtended: |
| 2216 | case glslang::EOpIMulExtended: |
| 2217 | if (arg >= 2) |
| 2218 | lvalue = true; |
| 2219 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2220 | default: |
| 2221 | break; |
| 2222 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2223 | builder.clearAccessChain(); |
| 2224 | if (invertedType != spv::NoType && arg == 0) |
| 2225 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2226 | else |
| 2227 | glslangOperands[arg]->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2228 | if (lvalue) |
| 2229 | operands.push_back(builder.accessChainGetLValue()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2230 | else { |
| 2231 | builder.setLine(node->getLoc().line); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2232 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2233 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2234 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2235 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2236 | builder.setLine(node->getLoc().line); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2237 | if (atomic) { |
| 2238 | // Handle all atomics |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2239 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2240 | } else { |
| 2241 | // Pass through to generic operations. |
| 2242 | switch (glslangOperands.size()) { |
| 2243 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2244 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2245 | break; |
| 2246 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2247 | { |
| 2248 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2249 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2250 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2251 | result = createUnaryOperation( |
| 2252 | node->getOp(), decorations, |
| 2253 | resultType(), operands.front(), |
| 2254 | glslangOperands[0]->getAsTyped()->getBasicType()); |
| 2255 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2256 | break; |
| 2257 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2258 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2259 | break; |
| 2260 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2261 | if (invertedType) |
| 2262 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | if (noReturnValue) |
| 2266 | return false; |
| 2267 | |
| 2268 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2269 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2270 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2271 | } else { |
| 2272 | builder.clearAccessChain(); |
| 2273 | builder.setAccessChainRValue(result); |
| 2274 | return false; |
| 2275 | } |
| 2276 | } |
| 2277 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2278 | // This path handles both if-then-else and ?: |
| 2279 | // The if-then-else has a node type of void, while |
| 2280 | // ?: has either a void or a non-void node type |
| 2281 | // |
| 2282 | // Leaving the result, when not void: |
| 2283 | // GLSL only has r-values as the result of a :?, but |
| 2284 | // if we have an l-value, that can be more efficient if it will |
| 2285 | // become the base of a complex r-value expression, because the |
| 2286 | // 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] | 2287 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 2288 | { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2289 | // See if it simple and safe, or required, to execute both sides. |
| 2290 | // Crucially, side effects must be either semantically required or avoided, |
| 2291 | // and there are performance trade-offs. |
| 2292 | // Return true if required or a good idea (and safe) to execute both sides, |
| 2293 | // false otherwise. |
| 2294 | const auto bothSidesPolicy = [&]() -> bool { |
| 2295 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2296 | if (node->getTrueBlock() == nullptr || |
| 2297 | node->getFalseBlock() == nullptr) |
| 2298 | return false; |
| 2299 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2300 | // required? (unless we write additional code to look for side effects |
| 2301 | // and make performance trade-offs if none are present) |
| 2302 | if (!node->getShortCircuit()) |
| 2303 | return true; |
| 2304 | |
| 2305 | // if not required to execute both, decide based on performance/practicality... |
| 2306 | |
| 2307 | // see if OpSelect can handle it |
| 2308 | if ((!node->getType().isScalar() && !node->getType().isVector()) || |
| 2309 | node->getBasicType() == glslang::EbtVoid) |
| 2310 | return false; |
| 2311 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2312 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 2313 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 2314 | |
| 2315 | // return true if a single operand to ? : is okay for OpSelect |
| 2316 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2317 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2318 | }; |
| 2319 | |
| 2320 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 2321 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 2322 | }; |
| 2323 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2324 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 2325 | // emit the condition before doing anything with selection |
| 2326 | node->getCondition()->traverse(this); |
| 2327 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 2328 | |
| 2329 | // Find a way of executing both sides and selecting the right result. |
| 2330 | const auto executeBothSides = [&]() -> void { |
| 2331 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2332 | node->getTrueBlock()->traverse(this); |
| 2333 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2334 | node->getFalseBlock()->traverse(this); |
| 2335 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2336 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2337 | builder.setLine(node->getLoc().line); |
| 2338 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2339 | // done if void |
| 2340 | if (node->getBasicType() == glslang::EbtVoid) |
| 2341 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 2342 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2343 | // emit code to select between trueValue and falseValue |
| 2344 | |
| 2345 | // see if OpSelect can handle it |
| 2346 | if (node->getType().isScalar() || node->getType().isVector()) { |
| 2347 | // Emit OpSelect for this selection. |
| 2348 | |
| 2349 | // smear condition to vector, if necessary (AST is always scalar) |
| 2350 | if (builder.isVector(trueValue)) |
| 2351 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 2352 | builder.makeVectorType(builder.makeBoolType(), |
| 2353 | builder.getNumComponents(trueValue))); |
| 2354 | |
| 2355 | // OpSelect |
| 2356 | result = builder.createTriOp(spv::OpSelect, |
| 2357 | convertGlslangToSpvType(node->getType()), condition, |
| 2358 | trueValue, falseValue); |
| 2359 | |
| 2360 | builder.clearAccessChain(); |
| 2361 | builder.setAccessChainRValue(result); |
| 2362 | } else { |
| 2363 | // We need control flow to select the result. |
| 2364 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 2365 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2366 | |
| 2367 | // Selection control: |
| 2368 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2369 | |
| 2370 | // make an "if" based on the value created by the condition |
| 2371 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2372 | |
| 2373 | // emit the "then" statement |
| 2374 | builder.createStore(trueValue, result); |
| 2375 | ifBuilder.makeBeginElse(); |
| 2376 | // emit the "else" statement |
| 2377 | builder.createStore(falseValue, result); |
| 2378 | |
| 2379 | // finish off the control flow |
| 2380 | ifBuilder.makeEndIf(); |
| 2381 | |
| 2382 | builder.clearAccessChain(); |
| 2383 | builder.setAccessChainLValue(result); |
| 2384 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2385 | }; |
| 2386 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2387 | // Execute the one side needed, as per the condition |
| 2388 | const auto executeOneSide = [&]() { |
| 2389 | // Always emit control flow. |
| 2390 | if (node->getBasicType() != glslang::EbtVoid) |
| 2391 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2392 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2393 | // Selection control: |
| 2394 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2395 | |
| 2396 | // make an "if" based on the value created by the condition |
| 2397 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2398 | |
| 2399 | // emit the "then" statement |
| 2400 | if (node->getTrueBlock() != nullptr) { |
| 2401 | node->getTrueBlock()->traverse(this); |
| 2402 | if (result != spv::NoResult) |
| 2403 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 2404 | } |
| 2405 | |
| 2406 | if (node->getFalseBlock() != nullptr) { |
| 2407 | ifBuilder.makeBeginElse(); |
| 2408 | // emit the "else" statement |
| 2409 | node->getFalseBlock()->traverse(this); |
| 2410 | if (result != spv::NoResult) |
| 2411 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 2412 | } |
| 2413 | |
| 2414 | // finish off the control flow |
| 2415 | ifBuilder.makeEndIf(); |
| 2416 | |
| 2417 | if (result != spv::NoResult) { |
| 2418 | builder.clearAccessChain(); |
| 2419 | builder.setAccessChainLValue(result); |
| 2420 | } |
| 2421 | }; |
| 2422 | |
| 2423 | // Try for OpSelect (or a requirement to execute both sides) |
| 2424 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2425 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2426 | if (node->getType().getQualifier().isSpecConstant()) |
| 2427 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2428 | executeBothSides(); |
| 2429 | } else |
| 2430 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2431 | |
| 2432 | return false; |
| 2433 | } |
| 2434 | |
| 2435 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2436 | { |
| 2437 | // emit and get the condition before doing anything with switch |
| 2438 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2439 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2440 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2441 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 2442 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2443 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2444 | // browse the children to sort out code segments |
| 2445 | int defaultSegment = -1; |
| 2446 | std::vector<TIntermNode*> codeSegments; |
| 2447 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 2448 | std::vector<int> caseValues; |
| 2449 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 2450 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 2451 | TIntermNode* child = *c; |
| 2452 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2453 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2454 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2455 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2456 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2457 | } else |
| 2458 | codeSegments.push_back(child); |
| 2459 | } |
| 2460 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2461 | // 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] | 2462 | // statements between the last case and the end of the switch statement |
| 2463 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 2464 | (int)codeSegments.size() == defaultSegment) |
| 2465 | codeSegments.push_back(nullptr); |
| 2466 | |
| 2467 | // make the switch statement |
| 2468 | 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] | 2469 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2470 | |
| 2471 | // emit all the code in the segments |
| 2472 | breakForLoop.push(false); |
| 2473 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 2474 | builder.nextSwitchSegment(segmentBlocks, s); |
| 2475 | if (codeSegments[s]) |
| 2476 | codeSegments[s]->traverse(this); |
| 2477 | else |
| 2478 | builder.addSwitchBreak(); |
| 2479 | } |
| 2480 | breakForLoop.pop(); |
| 2481 | |
| 2482 | builder.endSwitch(segmentBlocks); |
| 2483 | |
| 2484 | return false; |
| 2485 | } |
| 2486 | |
| 2487 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 2488 | { |
| 2489 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2490 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2491 | |
| 2492 | builder.clearAccessChain(); |
| 2493 | builder.setAccessChainRValue(constant); |
| 2494 | } |
| 2495 | |
| 2496 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 2497 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2498 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2499 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2500 | |
| 2501 | // Loop control: |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2502 | unsigned int dependencyLength = glslang::TIntermLoop::dependencyInfinite; |
| 2503 | const spv::LoopControlMask control = TranslateLoopControl(*node, dependencyLength); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2504 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2505 | // Spec requires back edges to target header blocks, and every header block |
| 2506 | // must dominate its merge block. Make a header block first to ensure these |
| 2507 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 2508 | // by a block-ending branch. But we don't want to put any other body/test |
| 2509 | // instructions in it, since the body/test may have arbitrary instructions, |
| 2510 | // including merges of its own. |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2511 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2512 | builder.setBuildPoint(&blocks.head); |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2513 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, dependencyLength); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2514 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2515 | spv::Block& test = builder.makeNewBlock(); |
| 2516 | builder.createBranch(&test); |
| 2517 | |
| 2518 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2519 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2520 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2521 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 2522 | |
| 2523 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2524 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2525 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2526 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2527 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2528 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2529 | |
| 2530 | builder.setBuildPoint(&blocks.continue_target); |
| 2531 | if (node->getTerminal()) |
| 2532 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2533 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 2534 | } else { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2535 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2536 | builder.createBranch(&blocks.body); |
| 2537 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2538 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2539 | builder.setBuildPoint(&blocks.body); |
| 2540 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2541 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2542 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2543 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2544 | |
| 2545 | builder.setBuildPoint(&blocks.continue_target); |
| 2546 | if (node->getTerminal()) |
| 2547 | node->getTerminal()->traverse(this); |
| 2548 | if (node->getTest()) { |
| 2549 | node->getTest()->traverse(this); |
| 2550 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2551 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2552 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2553 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 2554 | // TODO: unless there was a break/return/discard instruction |
| 2555 | // somewhere in the body, this is an infinite loop, so we should |
| 2556 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2557 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2558 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2559 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2560 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2561 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2562 | return false; |
| 2563 | } |
| 2564 | |
| 2565 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 2566 | { |
| 2567 | if (node->getExpression()) |
| 2568 | node->getExpression()->traverse(this); |
| 2569 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2570 | builder.setLine(node->getLoc().line); |
| 2571 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2572 | switch (node->getFlowOp()) { |
| 2573 | case glslang::EOpKill: |
| 2574 | builder.makeDiscard(); |
| 2575 | break; |
| 2576 | case glslang::EOpBreak: |
| 2577 | if (breakForLoop.top()) |
| 2578 | builder.createLoopExit(); |
| 2579 | else |
| 2580 | builder.addSwitchBreak(); |
| 2581 | break; |
| 2582 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2583 | builder.createLoopContinue(); |
| 2584 | break; |
| 2585 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2586 | if (node->getExpression()) { |
| 2587 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 2588 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 2589 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 2590 | builder.clearAccessChain(); |
| 2591 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 2592 | builder.setAccessChainLValue(copyId); |
| 2593 | multiTypeStore(glslangReturnType, returnId); |
| 2594 | returnId = builder.createLoad(copyId); |
| 2595 | } |
| 2596 | builder.makeReturn(false, returnId); |
| 2597 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2598 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2599 | |
| 2600 | builder.clearAccessChain(); |
| 2601 | break; |
| 2602 | |
| 2603 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2604 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2605 | break; |
| 2606 | } |
| 2607 | |
| 2608 | return false; |
| 2609 | } |
| 2610 | |
| 2611 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 2612 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2613 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2614 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2615 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 2616 | if (node->getQualifier().isConstant()) { |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2617 | return createSpvConstant(*node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2621 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2622 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 2623 | |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2624 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 2625 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 2626 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2627 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2628 | switch (storageClass) { |
| 2629 | case spv::StorageClassInput: |
| 2630 | case spv::StorageClassOutput: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2631 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2632 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2633 | break; |
| 2634 | case spv::StorageClassPushConstant: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2635 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2636 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2637 | break; |
| 2638 | case spv::StorageClassUniform: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2639 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2640 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2641 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 2642 | else |
| 2643 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 2644 | break; |
| 2645 | case spv::StorageClassStorageBuffer: |
| 2646 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
| 2647 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 2648 | break; |
| 2649 | default: |
| 2650 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2651 | } |
| 2652 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2653 | |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 2654 | const bool contains8BitType = node->getType().containsBasicType(glslang::EbtInt8) || |
| 2655 | node->getType().containsBasicType(glslang::EbtUint8); |
| 2656 | if (contains8BitType) { |
| 2657 | if (storageClass == spv::StorageClassPushConstant) { |
| 2658 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2659 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 2660 | } else if (storageClass == spv::StorageClassUniform) { |
| 2661 | builder.addExtension(spv::E_SPV_KHR_8bit_storage); |
| 2662 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
| 2663 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2664 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
| 2665 | } |
| 2666 | } |
| 2667 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2668 | const char* name = node->getName().c_str(); |
| 2669 | if (glslang::IsAnonymous(name)) |
| 2670 | name = ""; |
| 2671 | |
| 2672 | return builder.createVariable(storageClass, spvType, name); |
| 2673 | } |
| 2674 | |
| 2675 | // Return type Id of the sampled type. |
| 2676 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 2677 | { |
| 2678 | switch (sampler.type) { |
| 2679 | case glslang::EbtFloat: return builder.makeFloatType(32); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 2680 | #ifdef AMD_EXTENSIONS |
| 2681 | case glslang::EbtFloat16: |
| 2682 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 2683 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 2684 | return builder.makeFloatType(16); |
| 2685 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2686 | case glslang::EbtInt: return builder.makeIntType(32); |
| 2687 | case glslang::EbtUint: return builder.makeUintType(32); |
| 2688 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2689 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2690 | return builder.makeFloatType(32); |
| 2691 | } |
| 2692 | } |
| 2693 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2694 | // If node is a swizzle operation, return the type that should be used if |
| 2695 | // the swizzle base is first consumed by another operation, before the swizzle |
| 2696 | // is applied. |
| 2697 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 2698 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2699 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2700 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2701 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 2702 | else |
| 2703 | return spv::NoType; |
| 2704 | } |
| 2705 | |
| 2706 | // When inverting a swizzle with a parent op, this function |
| 2707 | // will apply the swizzle operation to a completed parent operation. |
| 2708 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 2709 | { |
| 2710 | std::vector<unsigned> swizzle; |
| 2711 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 2712 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 2713 | } |
| 2714 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2715 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 2716 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 2717 | { |
| 2718 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 2719 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 2720 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2721 | } |
| 2722 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2723 | // Convert from a glslang type to an SPV type, by calling into a |
| 2724 | // recursive version of this function. This establishes the inherited |
| 2725 | // layout state rooted from the top-level type. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2726 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type) |
| 2727 | { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2728 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | // 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] | 2732 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2733 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2734 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
| 2735 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, bool lastBufferBlockMember) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2736 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2737 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2738 | |
| 2739 | switch (type.getBasicType()) { |
| 2740 | case glslang::EbtVoid: |
| 2741 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2742 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2743 | break; |
| 2744 | case glslang::EbtFloat: |
| 2745 | spvType = builder.makeFloatType(32); |
| 2746 | break; |
| 2747 | case glslang::EbtDouble: |
| 2748 | spvType = builder.makeFloatType(64); |
| 2749 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2750 | case glslang::EbtFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2751 | spvType = builder.makeFloatType(16); |
| 2752 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2753 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2754 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 2755 | // a 32-bit int where non-0 means true. |
| 2756 | if (explicitLayout != glslang::ElpNone) |
| 2757 | spvType = builder.makeUintType(32); |
| 2758 | else |
| 2759 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2760 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 2761 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2762 | spvType = builder.makeIntType(8); |
| 2763 | break; |
| 2764 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2765 | spvType = builder.makeUintType(8); |
| 2766 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 2767 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2768 | spvType = builder.makeIntType(16); |
| 2769 | break; |
| 2770 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2771 | spvType = builder.makeUintType(16); |
| 2772 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2773 | case glslang::EbtInt: |
| 2774 | spvType = builder.makeIntType(32); |
| 2775 | break; |
| 2776 | case glslang::EbtUint: |
| 2777 | spvType = builder.makeUintType(32); |
| 2778 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2779 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2780 | spvType = builder.makeIntType(64); |
| 2781 | break; |
| 2782 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2783 | spvType = builder.makeUintType(64); |
| 2784 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2785 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 2786 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2787 | spvType = builder.makeUintType(32); |
| 2788 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2789 | case glslang::EbtSampler: |
| 2790 | { |
| 2791 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2792 | if (sampler.sampler) { |
| 2793 | // pure sampler |
| 2794 | spvType = builder.makeSamplerType(); |
| 2795 | } else { |
| 2796 | // an image is present, make its type |
| 2797 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 2798 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 2799 | if (sampler.combined) { |
| 2800 | // already has both image and sampler, make the combined type |
| 2801 | spvType = builder.makeSampledImageType(spvType); |
| 2802 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2803 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2804 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2805 | break; |
| 2806 | case glslang::EbtStruct: |
| 2807 | case glslang::EbtBlock: |
| 2808 | { |
| 2809 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2810 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2811 | |
| 2812 | // Try to share structs for different layouts, but not yet for other |
| 2813 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2814 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2815 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2816 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2817 | break; |
| 2818 | |
| 2819 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2820 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2821 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 2822 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2823 | } |
| 2824 | break; |
| 2825 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2826 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2827 | break; |
| 2828 | } |
| 2829 | |
| 2830 | if (type.isMatrix()) |
| 2831 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 2832 | else { |
| 2833 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 2834 | if (type.getVectorSize() > 1) |
| 2835 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 2836 | } |
| 2837 | |
| 2838 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2839 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 2840 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2841 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2842 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2843 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 2844 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2845 | // Use a dummy glslang type for querying internal strides of |
| 2846 | // arrays of arrays, but using just a one-dimensional array. |
| 2847 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 2848 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 2849 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2850 | |
| 2851 | // Will compute the higher-order strides here, rather than making a whole |
| 2852 | // pile of types and doing repetitive recursion on their contents. |
| 2853 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 2854 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2855 | |
| 2856 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2857 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2858 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2859 | if (stride > 0) |
| 2860 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2861 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2862 | } |
| 2863 | } else { |
| 2864 | // single-dimensional array, and don't yet have stride |
| 2865 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2866 | // 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] | 2867 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 2868 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2869 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2870 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2871 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 2872 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 2873 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2874 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2875 | else { |
| 2876 | if (!lastBufferBlockMember) { |
| 2877 | builder.addExtension("SPV_EXT_descriptor_indexing"); |
| 2878 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 2879 | } |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2880 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2881 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2882 | if (stride > 0) |
| 2883 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | return spvType; |
| 2887 | } |
| 2888 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2889 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 2890 | // |
| 2891 | // Identify interface members that don't have their required extension turned on. |
| 2892 | // |
| 2893 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 2894 | { |
| 2895 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 2896 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2897 | if (member.getFieldName() == "gl_ViewportMask" && |
| 2898 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 2899 | return true; |
| 2900 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 2901 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2902 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2903 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 2904 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2905 | return true; |
| 2906 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 2907 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2908 | return true; |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2909 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 2910 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2911 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2912 | |
| 2913 | return false; |
| 2914 | }; |
| 2915 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2916 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 2917 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 2918 | // Mutually recursive with convertGlslangToSpvType(). |
| 2919 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 2920 | const glslang::TTypeList* glslangMembers, |
| 2921 | glslang::TLayoutPacking explicitLayout, |
| 2922 | const glslang::TQualifier& qualifier) |
| 2923 | { |
| 2924 | // Create a vector of struct types for SPIR-V to consume |
| 2925 | std::vector<spv::Id> spvMembers; |
| 2926 | 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] | 2927 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2928 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2929 | if (glslangMember.hiddenMember()) { |
| 2930 | ++memberDelta; |
| 2931 | if (type.getBasicType() == glslang::EbtBlock) |
| 2932 | memberRemapper[glslangMembers][i] = -1; |
| 2933 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2934 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2935 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2936 | if (filterMember(glslangMember)) |
| 2937 | continue; |
| 2938 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2939 | // modify just this child's view of the qualifier |
| 2940 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2941 | InheritQualifiers(memberQualifier, qualifier); |
| 2942 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2943 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2944 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2945 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2946 | |
| 2947 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2948 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 2949 | i == (int)glslangMembers->size() - 1; |
| 2950 | spvMembers.push_back( |
| 2951 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | // Make the SPIR-V type |
| 2956 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2957 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2958 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 2959 | |
| 2960 | // Decorate it |
| 2961 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 2962 | |
| 2963 | return spvType; |
| 2964 | } |
| 2965 | |
| 2966 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 2967 | const glslang::TTypeList* glslangMembers, |
| 2968 | glslang::TLayoutPacking explicitLayout, |
| 2969 | const glslang::TQualifier& qualifier, |
| 2970 | spv::Id spvType) |
| 2971 | { |
| 2972 | // Name and decorate the non-hidden members |
| 2973 | int offset = -1; |
| 2974 | int locationOffset = 0; // for use within the members of this struct |
| 2975 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2976 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2977 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2978 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2979 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2980 | if (filterMember(glslangMember)) |
| 2981 | continue; |
| 2982 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2983 | |
| 2984 | // modify just this child's view of the qualifier |
| 2985 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2986 | InheritQualifiers(memberQualifier, qualifier); |
| 2987 | |
| 2988 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 2989 | if (member < 0) |
| 2990 | continue; |
| 2991 | |
| 2992 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 2993 | builder.addMemberDecoration(spvType, member, |
| 2994 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 2995 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 2996 | // Add interpolation and auxiliary storage decorations only to |
| 2997 | // top-level members of Input and Output storage classes |
| 2998 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 2999 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 3000 | if (type.getBasicType() == glslang::EbtBlock || |
| 3001 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 3002 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 3003 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3004 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3005 | } |
| 3006 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3007 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3008 | if (type.getBasicType() == glslang::EbtBlock && |
| 3009 | qualifier.storage == glslang::EvqBuffer) { |
| 3010 | // Add memory decorations only to top-level members of shader storage block |
| 3011 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3012 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3013 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3014 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 3015 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3016 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3017 | // Location assignment was already completed correctly by the front end, |
| 3018 | // just track whether a member needs to be decorated. |
| 3019 | // Ignore member locations if the container is an array, as that's |
| 3020 | // ill-specified and decisions have been made to not allow this. |
| 3021 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 3022 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3023 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3024 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 3025 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 3026 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 3027 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3028 | // component, XFB, others |
| 3029 | if (glslangMember.getQualifier().hasComponent()) |
| 3030 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 3031 | glslangMember.getQualifier().layoutComponent); |
| 3032 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 3033 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 3034 | glslangMember.getQualifier().layoutXfbOffset); |
| 3035 | else if (explicitLayout != glslang::ElpNone) { |
| 3036 | // figure out what to do with offset, which is accumulating |
| 3037 | int nextOffset; |
| 3038 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 3039 | if (offset >= 0) |
| 3040 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 3041 | offset = nextOffset; |
| 3042 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3043 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3044 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 3045 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 3046 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3047 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3048 | // built-in variable decorations |
| 3049 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 3050 | if (builtIn != spv::BuiltInMax) |
| 3051 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3052 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3053 | // nonuniform |
| 3054 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 3055 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3056 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 3057 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 3058 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 3059 | memberQualifier.semanticName); |
| 3060 | } |
| 3061 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3062 | #ifdef NV_EXTENSIONS |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3063 | if (builtIn == spv::BuiltInLayer) { |
| 3064 | // SPV_NV_viewport_array2 extension |
| 3065 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 3066 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 3067 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 3068 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3069 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3070 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 3071 | builder.addMemberDecoration(spvType, member, |
| 3072 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 3073 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 3074 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 3075 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3076 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3077 | } |
| 3078 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 3079 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 3080 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 3081 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 3082 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3083 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3087 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 3088 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3089 | if (type.getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
| 3090 | builder.addCapability(spv::CapabilityGeometryStreams); |
| 3091 | builder.addDecoration(spvType, spv::DecorationStream, type.getQualifier().layoutStream); |
| 3092 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3093 | } |
| 3094 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3095 | // Turn the expression forming the array size into an id. |
| 3096 | // This is not quite trivial, because of specialization constants. |
| 3097 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 3098 | // a specialization constant expression is. |
| 3099 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 3100 | { |
| 3101 | // First, see if this is sized with a node, meaning a specialization constant: |
| 3102 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 3103 | if (specNode != nullptr) { |
| 3104 | builder.clearAccessChain(); |
| 3105 | specNode->traverse(this); |
| 3106 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 3107 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3108 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3109 | // Otherwise, need a compile-time (front end) size, get it: |
| 3110 | int size = arraySizes.getDimSize(dim); |
| 3111 | assert(size > 0); |
| 3112 | return builder.makeUintConstant(size); |
| 3113 | } |
| 3114 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3115 | // Wrap the builder's accessChainLoad to: |
| 3116 | // - localize handling of RelaxedPrecision |
| 3117 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 3118 | // (avoids unnecessary work and possible type punning for structures) |
| 3119 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3120 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 3121 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3122 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3123 | |
| 3124 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3125 | coherentFlags |= TranslateCoherent(type); |
| 3126 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3127 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3128 | TranslateNonUniformDecoration(type.getQualifier()), |
| 3129 | nominalTypeId, |
| 3130 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
| 3131 | TranslateMemoryScope(coherentFlags)); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3132 | |
| 3133 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3134 | if (type.getBasicType() == glslang::EbtBool) { |
| 3135 | if (builder.isScalarType(nominalTypeId)) { |
| 3136 | // Conversion for bool |
| 3137 | spv::Id boolType = builder.makeBoolType(); |
| 3138 | if (nominalTypeId != boolType) |
| 3139 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 3140 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3141 | // Conversion for bvec |
| 3142 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3143 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 3144 | if (nominalTypeId != bvecType) |
| 3145 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 3146 | } |
| 3147 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3148 | |
| 3149 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3150 | } |
| 3151 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3152 | // Wrap the builder's accessChainStore to: |
| 3153 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3154 | // |
| 3155 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3156 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 3157 | { |
| 3158 | // Need to convert to abstract types when necessary |
| 3159 | if (type.getBasicType() == glslang::EbtBool) { |
| 3160 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 3161 | |
| 3162 | if (builder.isScalarType(nominalTypeId)) { |
| 3163 | // Conversion for bool |
| 3164 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3165 | if (nominalTypeId != boolType) { |
| 3166 | // keep these outside arguments, for determinant order-of-evaluation |
| 3167 | spv::Id one = builder.makeUintConstant(1); |
| 3168 | spv::Id zero = builder.makeUintConstant(0); |
| 3169 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 3170 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3171 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3172 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3173 | // Conversion for bvec |
| 3174 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3175 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3176 | if (nominalTypeId != bvecType) { |
| 3177 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 3178 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 3179 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 3180 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3181 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3182 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 3183 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3187 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3188 | coherentFlags |= TranslateCoherent(type); |
| 3189 | |
| 3190 | builder.accessChainStore(rvalue, |
| 3191 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask), |
| 3192 | TranslateMemoryScope(coherentFlags)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3193 | } |
| 3194 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3195 | // For storing when types match at the glslang level, but not might match at the |
| 3196 | // SPIR-V level. |
| 3197 | // |
| 3198 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3199 | // 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] | 3200 | // as in a member-decorated way. |
| 3201 | // |
| 3202 | // NOTE: This function can handle any store request; if it's not special it |
| 3203 | // simplifies to a simple OpStore. |
| 3204 | // |
| 3205 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 3206 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 3207 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3208 | // we only do the complex path here if it's an aggregate |
| 3209 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3210 | accessChainStore(type, rValue); |
| 3211 | return; |
| 3212 | } |
| 3213 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3214 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3215 | spv::Id rType = builder.getTypeId(rValue); |
| 3216 | spv::Id lValue = builder.accessChainGetLValue(); |
| 3217 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 3218 | if (lType == rType) { |
| 3219 | accessChainStore(type, rValue); |
| 3220 | return; |
| 3221 | } |
| 3222 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3223 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3224 | // where the two types were the same type in GLSL. This requires member |
| 3225 | // by member copy, recursively. |
| 3226 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3227 | // If an array, copy element by element. |
| 3228 | if (type.isArray()) { |
| 3229 | glslang::TType glslangElementType(type, 0); |
| 3230 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 3231 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 3232 | // get the source member |
| 3233 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3234 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3235 | // set up the target storage |
| 3236 | builder.clearAccessChain(); |
| 3237 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3238 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type)); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3239 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3240 | // store the member |
| 3241 | multiTypeStore(glslangElementType, elementRValue); |
| 3242 | } |
| 3243 | } else { |
| 3244 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3245 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3246 | // loop over structure members |
| 3247 | const glslang::TTypeList& members = *type.getStruct(); |
| 3248 | for (int m = 0; m < (int)members.size(); ++m) { |
| 3249 | const glslang::TType& glslangMemberType = *members[m].type; |
| 3250 | |
| 3251 | // get the source member |
| 3252 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 3253 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 3254 | |
| 3255 | // set up the target storage |
| 3256 | builder.clearAccessChain(); |
| 3257 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3258 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type)); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3259 | |
| 3260 | // store the member |
| 3261 | multiTypeStore(glslangMemberType, memberRValue); |
| 3262 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3263 | } |
| 3264 | } |
| 3265 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3266 | // Decide whether or not this type should be |
| 3267 | // decorated with offsets and strides, and if so |
| 3268 | // whether std140 or std430 rules should be applied. |
| 3269 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3270 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3271 | // has to be a block |
| 3272 | if (type.getBasicType() != glslang::EbtBlock) |
| 3273 | return glslang::ElpNone; |
| 3274 | |
| 3275 | // has to be a uniform or buffer block |
| 3276 | if (type.getQualifier().storage != glslang::EvqUniform && |
| 3277 | type.getQualifier().storage != glslang::EvqBuffer) |
| 3278 | return glslang::ElpNone; |
| 3279 | |
| 3280 | // return the layout to use |
| 3281 | switch (type.getQualifier().layoutPacking) { |
| 3282 | case glslang::ElpStd140: |
| 3283 | case glslang::ElpStd430: |
| 3284 | return type.getQualifier().layoutPacking; |
| 3285 | default: |
| 3286 | return glslang::ElpNone; |
| 3287 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3288 | } |
| 3289 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3290 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3291 | 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] | 3292 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3293 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3294 | int stride; |
| 3295 | glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 3296 | |
| 3297 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3298 | } |
| 3299 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3300 | // 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] | 3301 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3302 | 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] | 3303 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3304 | glslang::TType elementType; |
| 3305 | elementType.shallowCopy(matrixType); |
| 3306 | elementType.clearArraySizes(); |
| 3307 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3308 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3309 | int stride; |
| 3310 | glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
| 3311 | |
| 3312 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3315 | // Given a member type of a struct, realign the current offset for it, and compute |
| 3316 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 3317 | // on the next call. |
| 3318 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 3319 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 3320 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3321 | 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] | 3322 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3323 | { |
| 3324 | // this will get a positive value when deemed necessary |
| 3325 | nextOffset = -1; |
| 3326 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3327 | // override anything in currentOffset with user-set offset |
| 3328 | if (memberType.getQualifier().hasOffset()) |
| 3329 | currentOffset = memberType.getQualifier().layoutOffset; |
| 3330 | |
| 3331 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 3332 | // in which case the following code does not matter. But, that's not quite right |
| 3333 | // once cross-compilation unit GLSL validation is done, as the original user |
| 3334 | // settings are needed in layoutOffset, and then the following will come into play. |
| 3335 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3336 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3337 | if (! memberType.getQualifier().hasOffset()) |
| 3338 | currentOffset = -1; |
| 3339 | |
| 3340 | return; |
| 3341 | } |
| 3342 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3343 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3344 | if (currentOffset < 0) |
| 3345 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3346 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3347 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 3348 | // but possibly not yet correctly aligned. |
| 3349 | |
| 3350 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3351 | int dummyStride; |
| 3352 | 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] | 3353 | |
| 3354 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3355 | // TODO: make this consistent in early phases of code: |
| 3356 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 3357 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 3358 | // 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] | 3359 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3360 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3361 | int dummySize; |
| 3362 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 3363 | if (componentAlignment <= 4) |
| 3364 | memberAlignment = componentAlignment; |
| 3365 | } |
| 3366 | |
| 3367 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3368 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3369 | |
| 3370 | // Bump up to vec4 if there is a bad straddle |
| 3371 | if (glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
| 3372 | glslang::RoundToPow2(currentOffset, 16); |
| 3373 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3374 | nextOffset = currentOffset + memberSize; |
| 3375 | } |
| 3376 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3377 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3378 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3379 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 3380 | switch (glslangBuiltIn) |
| 3381 | { |
| 3382 | case glslang::EbvClipDistance: |
| 3383 | case glslang::EbvCullDistance: |
| 3384 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3385 | #ifdef NV_EXTENSIONS |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3386 | case glslang::EbvViewportMaskNV: |
| 3387 | case glslang::EbvSecondaryPositionNV: |
| 3388 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3389 | case glslang::EbvPositionPerViewNV: |
| 3390 | case glslang::EbvViewportMaskPerViewNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3391 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3392 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 3393 | // Alternately, we could just call this for any glslang built-in, since the |
| 3394 | // capability already guards against duplicates. |
| 3395 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 3396 | break; |
| 3397 | default: |
| 3398 | // Capabilities were already generated when the struct was declared. |
| 3399 | break; |
| 3400 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3401 | } |
| 3402 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3403 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3404 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 3405 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3406 | } |
| 3407 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3408 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3409 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 3410 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 3411 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3412 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3413 | assert(qualifier == glslang::EvqIn || |
| 3414 | qualifier == glslang::EvqOut || |
| 3415 | qualifier == glslang::EvqInOut || |
| 3416 | qualifier == glslang::EvqConstReadOnly); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3417 | return qualifier != glslang::EvqConstReadOnly; |
| 3418 | } |
| 3419 | |
| 3420 | // Is parameter pass-by-original? |
| 3421 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 3422 | bool implicitThisParam) |
| 3423 | { |
| 3424 | if (implicitThisParam) // implicit this |
| 3425 | return true; |
| 3426 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3427 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3428 | return paramType.containsOpaque() || // sampler, etc. |
| 3429 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 3430 | } |
| 3431 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3432 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 3433 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3434 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3435 | 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] | 3436 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 3437 | if (paramPrecision != spv::NoPrecision) |
| 3438 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3439 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3440 | }; |
| 3441 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3442 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3443 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3444 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3445 | continue; |
| 3446 | |
| 3447 | // 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] | 3448 | // 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] | 3449 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3450 | // 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] | 3451 | // function. What it is an address of varies: |
| 3452 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3453 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 3454 | // 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] | 3455 | // |
| 3456 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 3457 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3458 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 3459 | // 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] | 3460 | |
| 3461 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3462 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3463 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 3464 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3465 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 3466 | glslangIntermediate->implicitThisName; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3467 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3468 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3469 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3470 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 3471 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3472 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3473 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3474 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3475 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 3476 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3477 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3478 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3479 | paramTypes.push_back(typeId); |
| 3480 | } |
| 3481 | |
| 3482 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3483 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 3484 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3485 | glslFunction->getName().c_str(), paramTypes, |
| 3486 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3487 | if (implicitThis) |
| 3488 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3489 | |
| 3490 | // Track function to emit/call later |
| 3491 | functionMap[glslFunction->getName().c_str()] = function; |
| 3492 | |
| 3493 | // Set the parameter id's |
| 3494 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3495 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 3496 | // give a name too |
| 3497 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 3498 | } |
| 3499 | } |
| 3500 | } |
| 3501 | |
| 3502 | // Process all the initializers, while skipping the functions and link objects |
| 3503 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 3504 | { |
| 3505 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 3506 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 3507 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 3508 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 3509 | |
| 3510 | // 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] | 3511 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3512 | initializer->traverse(this); |
| 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | // Process all the functions, while skipping initializers. |
| 3518 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3519 | { |
| 3520 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3521 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 3522 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3523 | node->traverse(this); |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 3528 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3529 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3530 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3531 | currentFunction = functionMap[node->getName().c_str()]; |
| 3532 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3533 | builder.setBuildPoint(functionBlock); |
| 3534 | } |
| 3535 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3536 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3537 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3538 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3539 | |
| 3540 | glslang::TSampler sampler = {}; |
| 3541 | bool cubeCompare = false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3542 | #ifdef AMD_EXTENSIONS |
| 3543 | bool f16ShadowCompare = false; |
| 3544 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3545 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3546 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 3547 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3548 | #ifdef AMD_EXTENSIONS |
| 3549 | f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 3550 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3551 | } |
| 3552 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3553 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 3554 | builder.clearAccessChain(); |
| 3555 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3556 | |
| 3557 | // Special case l-value operands |
| 3558 | bool lvalue = false; |
| 3559 | switch (node.getOp()) { |
| 3560 | case glslang::EOpImageAtomicAdd: |
| 3561 | case glslang::EOpImageAtomicMin: |
| 3562 | case glslang::EOpImageAtomicMax: |
| 3563 | case glslang::EOpImageAtomicAnd: |
| 3564 | case glslang::EOpImageAtomicOr: |
| 3565 | case glslang::EOpImageAtomicXor: |
| 3566 | case glslang::EOpImageAtomicExchange: |
| 3567 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3568 | case glslang::EOpImageAtomicLoad: |
| 3569 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3570 | if (i == 0) |
| 3571 | lvalue = true; |
| 3572 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3573 | case glslang::EOpSparseImageLoad: |
| 3574 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 3575 | lvalue = true; |
| 3576 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3577 | #ifdef AMD_EXTENSIONS |
| 3578 | case glslang::EOpSparseTexture: |
| 3579 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 3580 | lvalue = true; |
| 3581 | break; |
| 3582 | case glslang::EOpSparseTextureClamp: |
| 3583 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 3584 | lvalue = true; |
| 3585 | break; |
| 3586 | case glslang::EOpSparseTextureLod: |
| 3587 | case glslang::EOpSparseTextureOffset: |
| 3588 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 3589 | lvalue = true; |
| 3590 | break; |
| 3591 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3592 | case glslang::EOpSparseTexture: |
| 3593 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 3594 | lvalue = true; |
| 3595 | break; |
| 3596 | case glslang::EOpSparseTextureClamp: |
| 3597 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 3598 | lvalue = true; |
| 3599 | break; |
| 3600 | case glslang::EOpSparseTextureLod: |
| 3601 | case glslang::EOpSparseTextureOffset: |
| 3602 | if (i == 3) |
| 3603 | lvalue = true; |
| 3604 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3605 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3606 | case glslang::EOpSparseTextureFetch: |
| 3607 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 3608 | lvalue = true; |
| 3609 | break; |
| 3610 | case glslang::EOpSparseTextureFetchOffset: |
| 3611 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 3612 | lvalue = true; |
| 3613 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3614 | #ifdef AMD_EXTENSIONS |
| 3615 | case glslang::EOpSparseTextureLodOffset: |
| 3616 | case glslang::EOpSparseTextureGrad: |
| 3617 | case glslang::EOpSparseTextureOffsetClamp: |
| 3618 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 3619 | lvalue = true; |
| 3620 | break; |
| 3621 | case glslang::EOpSparseTextureGradOffset: |
| 3622 | case glslang::EOpSparseTextureGradClamp: |
| 3623 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 3624 | lvalue = true; |
| 3625 | break; |
| 3626 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3627 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 3628 | lvalue = true; |
| 3629 | break; |
| 3630 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3631 | case glslang::EOpSparseTextureLodOffset: |
| 3632 | case glslang::EOpSparseTextureGrad: |
| 3633 | case glslang::EOpSparseTextureOffsetClamp: |
| 3634 | if (i == 4) |
| 3635 | lvalue = true; |
| 3636 | break; |
| 3637 | case glslang::EOpSparseTextureGradOffset: |
| 3638 | case glslang::EOpSparseTextureGradClamp: |
| 3639 | if (i == 5) |
| 3640 | lvalue = true; |
| 3641 | break; |
| 3642 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3643 | if (i == 6) |
| 3644 | lvalue = true; |
| 3645 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3646 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3647 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3648 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 3649 | lvalue = true; |
| 3650 | break; |
| 3651 | case glslang::EOpSparseTextureGatherOffset: |
| 3652 | case glslang::EOpSparseTextureGatherOffsets: |
| 3653 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 3654 | lvalue = true; |
| 3655 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3656 | #ifdef AMD_EXTENSIONS |
| 3657 | case glslang::EOpSparseTextureGatherLod: |
| 3658 | if (i == 3) |
| 3659 | lvalue = true; |
| 3660 | break; |
| 3661 | case glslang::EOpSparseTextureGatherLodOffset: |
| 3662 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 3663 | if (i == 4) |
| 3664 | lvalue = true; |
| 3665 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3666 | case glslang::EOpSparseImageLoadLod: |
| 3667 | if (i == 3) |
| 3668 | lvalue = true; |
| 3669 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3670 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3671 | default: |
| 3672 | break; |
| 3673 | } |
| 3674 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3675 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3676 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3677 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3678 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3682 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3683 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3684 | builder.clearAccessChain(); |
| 3685 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3686 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3687 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3688 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3689 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 3690 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3691 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3692 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3693 | |
| 3694 | builder.setLine(node->getLoc().line); |
| 3695 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3696 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3697 | |
| 3698 | const glslang::TType &imageType = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 3699 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
| 3700 | const glslang::TSampler sampler = imageType.getSampler(); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3701 | #ifdef AMD_EXTENSIONS |
| 3702 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
| 3703 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 3704 | : false; |
| 3705 | #endif |
| 3706 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3707 | std::vector<spv::Id> arguments; |
| 3708 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3709 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3710 | else |
| 3711 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 3712 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3713 | |
| 3714 | spv::Builder::TextureParameters params = { }; |
| 3715 | params.sampler = arguments[0]; |
| 3716 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3717 | glslang::TCrackedTextureOp cracked; |
| 3718 | node->crackTexture(sampler, cracked); |
| 3719 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 3720 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3721 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3722 | // Check for queries |
| 3723 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3724 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 3725 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 3726 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3727 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3728 | switch (node->getOp()) { |
| 3729 | case glslang::EOpImageQuerySize: |
| 3730 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3731 | if (arguments.size() > 1) { |
| 3732 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3733 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3734 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3735 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3736 | case glslang::EOpImageQuerySamples: |
| 3737 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3738 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3739 | case glslang::EOpTextureQueryLod: |
| 3740 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3741 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3742 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3743 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3744 | case glslang::EOpSparseTexelsResident: |
| 3745 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3746 | default: |
| 3747 | assert(0); |
| 3748 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3749 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3750 | } |
| 3751 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 3752 | int components = node->getType().getVectorSize(); |
| 3753 | |
| 3754 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 3755 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 3756 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 3757 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 3758 | // here around e.g. which ones return scalars or other types. |
| 3759 | components = 4; |
| 3760 | } |
| 3761 | |
| 3762 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 3763 | |
| 3764 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 3765 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3766 | // Check for image functions other than queries |
| 3767 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3768 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3769 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3770 | spv::IdImmediate image = { true, *(opIt++) }; |
| 3771 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3772 | |
| 3773 | // Handle subpass operations |
| 3774 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 3775 | // built-in function. |
| 3776 | if (cracked.subpass) { |
| 3777 | // add on the (0,0) coordinate |
| 3778 | spv::Id zero = builder.makeIntConstant(0); |
| 3779 | std::vector<spv::Id> comps; |
| 3780 | comps.push_back(zero); |
| 3781 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3782 | spv::IdImmediate coord = { true, |
| 3783 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 3784 | operands.push_back(coord); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3785 | if (sampler.ms) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3786 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsSampleMask }; |
| 3787 | operands.push_back(imageOperands); |
| 3788 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 3789 | operands.push_back(imageOperand); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3790 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 3791 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 3792 | builder.setPrecision(result, precision); |
| 3793 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3796 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 3797 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3798 | #ifdef AMD_EXTENSIONS |
| 3799 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 3800 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3801 | if (node->getOp() == glslang::EOpImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3802 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3803 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3804 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3805 | mask = mask | spv::ImageOperandsSampleMask; |
| 3806 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3807 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3808 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3809 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3810 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3811 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3812 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3813 | #endif |
| 3814 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 3815 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 3816 | if (mask) { |
| 3817 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 3818 | operands.push_back(imageOperands); |
| 3819 | } |
| 3820 | if (mask & spv::ImageOperandsSampleMask) { |
| 3821 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 3822 | operands.push_back(imageOperand); |
| 3823 | } |
| 3824 | #ifdef AMD_EXTENSIONS |
| 3825 | if (mask & spv::ImageOperandsLodMask) { |
| 3826 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 3827 | operands.push_back(imageOperand); |
| 3828 | } |
| 3829 | #endif |
| 3830 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 3831 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 3832 | operands.push_back(imageOperand); |
| 3833 | } |
| 3834 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3835 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3836 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 3837 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3838 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 3839 | builder.setPrecision(result[0], precision); |
| 3840 | |
| 3841 | // If needed, add a conversion constructor to the proper size. |
| 3842 | if (components != node->getType().getVectorSize()) |
| 3843 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 3844 | |
| 3845 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3846 | #ifdef AMD_EXTENSIONS |
| 3847 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 3848 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3849 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3850 | #endif |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3851 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3852 | // Push the texel value before the operands |
| 3853 | #ifdef AMD_EXTENSIONS |
| 3854 | if (sampler.ms || cracked.lod) { |
| 3855 | #else |
| 3856 | if (sampler.ms) { |
| 3857 | #endif |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3858 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 3859 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3860 | } else { |
| 3861 | spv::IdImmediate texel = { true, *opIt }; |
| 3862 | operands.push_back(texel); |
| 3863 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3864 | |
| 3865 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 3866 | if (sampler.ms) { |
| 3867 | mask = mask | spv::ImageOperandsSampleMask; |
| 3868 | } |
| 3869 | #ifdef AMD_EXTENSIONS |
| 3870 | if (cracked.lod) { |
| 3871 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3872 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3873 | mask = mask | spv::ImageOperandsLodMask; |
| 3874 | } |
| 3875 | #endif |
| 3876 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 3877 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
| 3878 | if (mask) { |
| 3879 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 3880 | operands.push_back(imageOperands); |
| 3881 | } |
| 3882 | if (mask & spv::ImageOperandsSampleMask) { |
| 3883 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 3884 | operands.push_back(imageOperand); |
| 3885 | } |
| 3886 | #ifdef AMD_EXTENSIONS |
| 3887 | if (mask & spv::ImageOperandsLodMask) { |
| 3888 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 3889 | operands.push_back(imageOperand); |
| 3890 | } |
| 3891 | #endif |
| 3892 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
| 3893 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 3894 | operands.push_back(imageOperand); |
| 3895 | } |
| 3896 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3897 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3898 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3899 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3900 | return spv::NoResult; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3901 | #ifdef AMD_EXTENSIONS |
| 3902 | } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 3903 | #else |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3904 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3905 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3906 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3907 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3908 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 3909 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3910 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3911 | if (sampler.ms) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3912 | mask = mask | spv::ImageOperandsSampleMask; |
| 3913 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3914 | #ifdef AMD_EXTENSIONS |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3915 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3916 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3917 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3918 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3919 | mask = mask | spv::ImageOperandsLodMask; |
| 3920 | } |
| 3921 | #endif |
| 3922 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 3923 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 3924 | if (mask) { |
| 3925 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3926 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3927 | } |
| 3928 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3929 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 3930 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3931 | } |
| 3932 | #ifdef AMD_EXTENSIONS |
| 3933 | if (mask & spv::ImageOperandsLodMask) { |
| 3934 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 3935 | operands.push_back(imageOperand); |
| 3936 | } |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3937 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3938 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 3939 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 3940 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | // Create the return type that was a special structure |
| 3944 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3945 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3946 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 3947 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 3948 | |
| 3949 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 3950 | |
| 3951 | // Decode the return type |
| 3952 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 3953 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3954 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3955 | // Process image atomic operations |
| 3956 | |
| 3957 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 3958 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 3959 | // For non-MS, the sample value should be 0 |
| 3960 | spv::IdImmediate sample = { true, sampler.ms ? *(opIt++) : builder.makeUintConstant(0) }; |
| 3961 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3962 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3963 | spv::Id resultTypeId; |
| 3964 | // imageAtomicStore has a void return type so base the pointer type on |
| 3965 | // the type of the value operand. |
| 3966 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 3967 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word)); |
| 3968 | } else { |
| 3969 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 3970 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3971 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3972 | |
| 3973 | std::vector<spv::Id> operands; |
| 3974 | operands.push_back(pointer); |
| 3975 | for (; opIt != arguments.end(); ++opIt) |
| 3976 | operands.push_back(*opIt); |
| 3977 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3978 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 3982 | #ifdef AMD_EXTENSIONS |
| 3983 | // Check for fragment mask functions other than queries |
| 3984 | if (cracked.fragMask) { |
| 3985 | assert(sampler.ms); |
| 3986 | |
| 3987 | auto opIt = arguments.begin(); |
| 3988 | std::vector<spv::Id> operands; |
| 3989 | |
| 3990 | // Extract the image if necessary |
| 3991 | if (builder.isSampledImage(params.sampler)) |
| 3992 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 3993 | |
| 3994 | operands.push_back(params.sampler); |
| 3995 | ++opIt; |
| 3996 | |
| 3997 | if (sampler.isSubpass()) { |
| 3998 | // add on the (0,0) coordinate |
| 3999 | spv::Id zero = builder.makeIntConstant(0); |
| 4000 | std::vector<spv::Id> comps; |
| 4001 | comps.push_back(zero); |
| 4002 | comps.push_back(zero); |
| 4003 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 4004 | } |
| 4005 | |
| 4006 | for (; opIt != arguments.end(); ++opIt) |
| 4007 | operands.push_back(*opIt); |
| 4008 | |
| 4009 | spv::Op fragMaskOp = spv::OpNop; |
| 4010 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 4011 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 4012 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 4013 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 4014 | |
| 4015 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 4016 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 4017 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 4018 | } |
| 4019 | #endif |
| 4020 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4021 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4022 | bool sparse = node->isSparseTexture(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4023 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 4024 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4025 | // check for bias argument |
| 4026 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4027 | #ifdef AMD_EXTENSIONS |
| 4028 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 4029 | #else |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4030 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4031 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4032 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4033 | #ifdef AMD_EXTENSIONS |
| 4034 | if (cracked.gather) |
| 4035 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4036 | |
| 4037 | if (f16ShadowCompare) |
| 4038 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4039 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4040 | if (cracked.offset) |
| 4041 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4042 | #ifdef AMD_EXTENSIONS |
| 4043 | else if (cracked.offsets) |
| 4044 | ++nonBiasArgCount; |
| 4045 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4046 | if (cracked.grad) |
| 4047 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4048 | if (cracked.lodClamp) |
| 4049 | ++nonBiasArgCount; |
| 4050 | if (sparse) |
| 4051 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4052 | |
| 4053 | if ((int)arguments.size() > nonBiasArgCount) |
| 4054 | bias = true; |
| 4055 | } |
| 4056 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 4057 | // See if the sampler param should really be just the SPV image part |
| 4058 | if (cracked.fetch) { |
| 4059 | // a fetch needs to have the image extracted first |
| 4060 | if (builder.isSampledImage(params.sampler)) |
| 4061 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4062 | } |
| 4063 | |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4064 | #ifdef AMD_EXTENSIONS |
| 4065 | if (cracked.gather) { |
| 4066 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 4067 | if (bias || cracked.lod || |
| 4068 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 4069 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 4070 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | #endif |
| 4074 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4075 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4076 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4077 | params.coords = arguments[1]; |
| 4078 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4079 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4080 | |
| 4081 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4082 | #ifdef AMD_EXTENSIONS |
| 4083 | if (cubeCompare || f16ShadowCompare) { |
| 4084 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4085 | if (cubeCompare) { |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4086 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4087 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4088 | ++extraArgs; |
| 4089 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4090 | params.Dref = arguments[2]; |
| 4091 | ++extraArgs; |
| 4092 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4093 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4094 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4095 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4096 | 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] | 4097 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4098 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 4099 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4100 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 4101 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4102 | |
| 4103 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4104 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4105 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4106 | ++extraArgs; |
Chao Chen | beae225 | 2018-09-19 11:40:45 -0700 | [diff] [blame^] | 4107 | } else if (glslangIntermediate->getStage() != EShLangFragment |
| 4108 | #ifdef NV_EXTENSIONS |
| 4109 | // NV_compute_shader_derivatives layout qualifiers allow for implicit LODs |
| 4110 | && !(glslangIntermediate->getStage() == EShLangCompute && |
| 4111 | (glslangIntermediate->getLayoutDerivativeModeNone() != glslang::LayoutDerivativeNone)) |
| 4112 | #endif |
| 4113 | ) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4114 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 4115 | noImplicitLod = true; |
| 4116 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4117 | |
| 4118 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4119 | if (sampler.ms) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4120 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4121 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4122 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4123 | |
| 4124 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4125 | if (cracked.grad) { |
| 4126 | params.gradX = arguments[2 + extraArgs]; |
| 4127 | params.gradY = arguments[3 + extraArgs]; |
| 4128 | extraArgs += 2; |
| 4129 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4130 | |
| 4131 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4132 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4133 | params.offset = arguments[2 + extraArgs]; |
| 4134 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4135 | } else if (cracked.offsets) { |
| 4136 | params.offsets = arguments[2 + extraArgs]; |
| 4137 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4138 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4139 | |
| 4140 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4141 | if (cracked.lodClamp) { |
| 4142 | params.lodClamp = arguments[2 + extraArgs]; |
| 4143 | ++extraArgs; |
| 4144 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4145 | |
| 4146 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4147 | if (sparse) { |
| 4148 | params.texelOut = arguments[2 + extraArgs]; |
| 4149 | ++extraArgs; |
| 4150 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4151 | |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4152 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4153 | if (cracked.gather && ! sampler.shadow) { |
| 4154 | // default component is 0, if missing, otherwise an argument |
| 4155 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4156 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4157 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4158 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4159 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | // bias |
| 4163 | if (bias) { |
| 4164 | params.bias = arguments[2 + extraArgs]; |
| 4165 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4166 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4167 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4168 | // projective component (might not to move) |
| 4169 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 4170 | // are divided by the last component of P." |
| 4171 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 4172 | // unused components will appear after all used components." |
| 4173 | if (cracked.proj) { |
| 4174 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 4175 | int projTargetComp; |
| 4176 | switch (sampler.dim) { |
| 4177 | case glslang::Esd1D: projTargetComp = 1; break; |
| 4178 | case glslang::Esd2D: projTargetComp = 2; break; |
| 4179 | case glslang::EsdRect: projTargetComp = 2; break; |
| 4180 | default: projTargetComp = projSourceComp; break; |
| 4181 | } |
| 4182 | // copy the projective coordinate if we have to |
| 4183 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 4184 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4185 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 4186 | projSourceComp); |
| 4187 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 4188 | builder.getTypeId(params.coords), projTargetComp); |
| 4189 | } |
| 4190 | } |
| 4191 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4192 | // nonprivate |
| 4193 | if (imageType.getQualifier().nonprivate) { |
| 4194 | params.nonprivate = true; |
| 4195 | } |
| 4196 | |
| 4197 | // volatile |
| 4198 | if (imageType.getQualifier().volatil) { |
| 4199 | params.volatil = true; |
| 4200 | } |
| 4201 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4202 | std::vector<spv::Id> result( 1, |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4203 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4204 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4205 | |
| 4206 | if (components != node->getType().getVectorSize()) |
| 4207 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4208 | |
| 4209 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 4213 | { |
| 4214 | // Grab the function's pointer from the previously created function |
| 4215 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 4216 | if (! function) |
| 4217 | return 0; |
| 4218 | |
| 4219 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 4220 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 4221 | |
| 4222 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 4223 | // |
| 4224 | // These imply we need a four step process: |
| 4225 | // 1. Evaluate the arguments |
| 4226 | // 2. Allocate and make copies of in, out, and inout arguments |
| 4227 | // 3. Make the call |
| 4228 | // 4. Copy back the results |
| 4229 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4230 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4231 | std::vector<spv::Builder::AccessChain> lValues; |
| 4232 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4233 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4234 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4235 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4236 | // build l-value |
| 4237 | builder.clearAccessChain(); |
| 4238 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4239 | // 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] | 4240 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4241 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4242 | // save l-value |
| 4243 | lValues.push_back(builder.getAccessChain()); |
| 4244 | } else { |
| 4245 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4246 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4247 | } |
| 4248 | } |
| 4249 | |
| 4250 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 4251 | // copy the original into that space. |
| 4252 | // |
| 4253 | // Also, build up the list of actual arguments to pass in for the call |
| 4254 | int lValueCount = 0; |
| 4255 | int rValueCount = 0; |
| 4256 | std::vector<spv::Id> spvArgs; |
| 4257 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 4258 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4259 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 4260 | builder.setAccessChain(lValues[lValueCount]); |
| 4261 | arg = builder.accessChainGetLValue(); |
| 4262 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4263 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4264 | // need space to hold the copy |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4265 | arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4266 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 4267 | // need to copy the input into output space |
| 4268 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4269 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4270 | builder.clearAccessChain(); |
| 4271 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4272 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4273 | } |
| 4274 | ++lValueCount; |
| 4275 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4276 | // process r-value, which involves a copy for a type mismatch |
| 4277 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) { |
| 4278 | spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg"); |
| 4279 | builder.clearAccessChain(); |
| 4280 | builder.setAccessChainLValue(argCopy); |
| 4281 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 4282 | arg = builder.createLoad(argCopy); |
| 4283 | } else |
| 4284 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4285 | ++rValueCount; |
| 4286 | } |
| 4287 | spvArgs.push_back(arg); |
| 4288 | } |
| 4289 | |
| 4290 | // 3. Make the call. |
| 4291 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4292 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4293 | |
| 4294 | // 4. Copy back out an "out" arguments. |
| 4295 | lValueCount = 0; |
| 4296 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4297 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4298 | ++lValueCount; |
| 4299 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4300 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 4301 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 4302 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4303 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4304 | } |
| 4305 | ++lValueCount; |
| 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | return result; |
| 4310 | } |
| 4311 | |
| 4312 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4313 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4314 | spv::Id typeId, spv::Id left, spv::Id right, |
| 4315 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 4316 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4317 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 4318 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4319 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4320 | |
| 4321 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4322 | 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] | 4323 | bool comparison = false; |
| 4324 | |
| 4325 | switch (op) { |
| 4326 | case glslang::EOpAdd: |
| 4327 | case glslang::EOpAddAssign: |
| 4328 | if (isFloat) |
| 4329 | binOp = spv::OpFAdd; |
| 4330 | else |
| 4331 | binOp = spv::OpIAdd; |
| 4332 | break; |
| 4333 | case glslang::EOpSub: |
| 4334 | case glslang::EOpSubAssign: |
| 4335 | if (isFloat) |
| 4336 | binOp = spv::OpFSub; |
| 4337 | else |
| 4338 | binOp = spv::OpISub; |
| 4339 | break; |
| 4340 | case glslang::EOpMul: |
| 4341 | case glslang::EOpMulAssign: |
| 4342 | if (isFloat) |
| 4343 | binOp = spv::OpFMul; |
| 4344 | else |
| 4345 | binOp = spv::OpIMul; |
| 4346 | break; |
| 4347 | case glslang::EOpVectorTimesScalar: |
| 4348 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 4349 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4350 | if (builder.isVector(right)) |
| 4351 | std::swap(left, right); |
| 4352 | assert(builder.isScalar(right)); |
| 4353 | needMatchingVectors = false; |
| 4354 | binOp = spv::OpVectorTimesScalar; |
| 4355 | } else |
| 4356 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4357 | break; |
| 4358 | case glslang::EOpVectorTimesMatrix: |
| 4359 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4360 | binOp = spv::OpVectorTimesMatrix; |
| 4361 | break; |
| 4362 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4363 | binOp = spv::OpMatrixTimesVector; |
| 4364 | break; |
| 4365 | case glslang::EOpMatrixTimesScalar: |
| 4366 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4367 | binOp = spv::OpMatrixTimesScalar; |
| 4368 | break; |
| 4369 | case glslang::EOpMatrixTimesMatrix: |
| 4370 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4371 | binOp = spv::OpMatrixTimesMatrix; |
| 4372 | break; |
| 4373 | case glslang::EOpOuterProduct: |
| 4374 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4375 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4376 | break; |
| 4377 | |
| 4378 | case glslang::EOpDiv: |
| 4379 | case glslang::EOpDivAssign: |
| 4380 | if (isFloat) |
| 4381 | binOp = spv::OpFDiv; |
| 4382 | else if (isUnsigned) |
| 4383 | binOp = spv::OpUDiv; |
| 4384 | else |
| 4385 | binOp = spv::OpSDiv; |
| 4386 | break; |
| 4387 | case glslang::EOpMod: |
| 4388 | case glslang::EOpModAssign: |
| 4389 | if (isFloat) |
| 4390 | binOp = spv::OpFMod; |
| 4391 | else if (isUnsigned) |
| 4392 | binOp = spv::OpUMod; |
| 4393 | else |
| 4394 | binOp = spv::OpSMod; |
| 4395 | break; |
| 4396 | case glslang::EOpRightShift: |
| 4397 | case glslang::EOpRightShiftAssign: |
| 4398 | if (isUnsigned) |
| 4399 | binOp = spv::OpShiftRightLogical; |
| 4400 | else |
| 4401 | binOp = spv::OpShiftRightArithmetic; |
| 4402 | break; |
| 4403 | case glslang::EOpLeftShift: |
| 4404 | case glslang::EOpLeftShiftAssign: |
| 4405 | binOp = spv::OpShiftLeftLogical; |
| 4406 | break; |
| 4407 | case glslang::EOpAnd: |
| 4408 | case glslang::EOpAndAssign: |
| 4409 | binOp = spv::OpBitwiseAnd; |
| 4410 | break; |
| 4411 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4412 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4413 | binOp = spv::OpLogicalAnd; |
| 4414 | break; |
| 4415 | case glslang::EOpInclusiveOr: |
| 4416 | case glslang::EOpInclusiveOrAssign: |
| 4417 | binOp = spv::OpBitwiseOr; |
| 4418 | break; |
| 4419 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4420 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4421 | binOp = spv::OpLogicalOr; |
| 4422 | break; |
| 4423 | case glslang::EOpExclusiveOr: |
| 4424 | case glslang::EOpExclusiveOrAssign: |
| 4425 | binOp = spv::OpBitwiseXor; |
| 4426 | break; |
| 4427 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4428 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4429 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4430 | break; |
| 4431 | |
| 4432 | case glslang::EOpLessThan: |
| 4433 | case glslang::EOpGreaterThan: |
| 4434 | case glslang::EOpLessThanEqual: |
| 4435 | case glslang::EOpGreaterThanEqual: |
| 4436 | case glslang::EOpEqual: |
| 4437 | case glslang::EOpNotEqual: |
| 4438 | case glslang::EOpVectorEqual: |
| 4439 | case glslang::EOpVectorNotEqual: |
| 4440 | comparison = true; |
| 4441 | break; |
| 4442 | default: |
| 4443 | break; |
| 4444 | } |
| 4445 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4446 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4447 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4448 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4449 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4450 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4451 | |
| 4452 | // 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] | 4453 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4454 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4455 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4456 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4457 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4458 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4459 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4460 | } |
| 4461 | |
| 4462 | if (! comparison) |
| 4463 | return 0; |
| 4464 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4465 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4466 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 4467 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4468 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 4469 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4470 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4471 | return result; |
| 4472 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4473 | |
| 4474 | switch (op) { |
| 4475 | case glslang::EOpLessThan: |
| 4476 | if (isFloat) |
| 4477 | binOp = spv::OpFOrdLessThan; |
| 4478 | else if (isUnsigned) |
| 4479 | binOp = spv::OpULessThan; |
| 4480 | else |
| 4481 | binOp = spv::OpSLessThan; |
| 4482 | break; |
| 4483 | case glslang::EOpGreaterThan: |
| 4484 | if (isFloat) |
| 4485 | binOp = spv::OpFOrdGreaterThan; |
| 4486 | else if (isUnsigned) |
| 4487 | binOp = spv::OpUGreaterThan; |
| 4488 | else |
| 4489 | binOp = spv::OpSGreaterThan; |
| 4490 | break; |
| 4491 | case glslang::EOpLessThanEqual: |
| 4492 | if (isFloat) |
| 4493 | binOp = spv::OpFOrdLessThanEqual; |
| 4494 | else if (isUnsigned) |
| 4495 | binOp = spv::OpULessThanEqual; |
| 4496 | else |
| 4497 | binOp = spv::OpSLessThanEqual; |
| 4498 | break; |
| 4499 | case glslang::EOpGreaterThanEqual: |
| 4500 | if (isFloat) |
| 4501 | binOp = spv::OpFOrdGreaterThanEqual; |
| 4502 | else if (isUnsigned) |
| 4503 | binOp = spv::OpUGreaterThanEqual; |
| 4504 | else |
| 4505 | binOp = spv::OpSGreaterThanEqual; |
| 4506 | break; |
| 4507 | case glslang::EOpEqual: |
| 4508 | case glslang::EOpVectorEqual: |
| 4509 | if (isFloat) |
| 4510 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4511 | else if (isBool) |
| 4512 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4513 | else |
| 4514 | binOp = spv::OpIEqual; |
| 4515 | break; |
| 4516 | case glslang::EOpNotEqual: |
| 4517 | case glslang::EOpVectorNotEqual: |
| 4518 | if (isFloat) |
| 4519 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4520 | else if (isBool) |
| 4521 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4522 | else |
| 4523 | binOp = spv::OpINotEqual; |
| 4524 | break; |
| 4525 | default: |
| 4526 | break; |
| 4527 | } |
| 4528 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4529 | if (binOp != spv::OpNop) { |
| 4530 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4531 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4532 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4533 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4534 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4535 | |
| 4536 | return 0; |
| 4537 | } |
| 4538 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4539 | // |
| 4540 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 4541 | // These can be any of: |
| 4542 | // |
| 4543 | // matrix * scalar |
| 4544 | // scalar * matrix |
| 4545 | // matrix * matrix linear algebraic |
| 4546 | // matrix * vector |
| 4547 | // vector * matrix |
| 4548 | // matrix * matrix componentwise |
| 4549 | // matrix op matrix op in {+, -, /} |
| 4550 | // matrix op scalar op in {+, -, /} |
| 4551 | // scalar op matrix op in {+, -, /} |
| 4552 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4553 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 4554 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4555 | { |
| 4556 | bool firstClass = true; |
| 4557 | |
| 4558 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 4559 | switch (op) { |
| 4560 | case spv::OpFDiv: |
| 4561 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 4562 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 4563 | spv::Id resultType = builder.getTypeId(right); |
| 4564 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4565 | op = spv::OpMatrixTimesScalar; |
| 4566 | } else |
| 4567 | firstClass = false; |
| 4568 | break; |
| 4569 | case spv::OpMatrixTimesScalar: |
| 4570 | if (builder.isMatrix(right)) |
| 4571 | std::swap(left, right); |
| 4572 | assert(builder.isScalar(right)); |
| 4573 | break; |
| 4574 | case spv::OpVectorTimesMatrix: |
| 4575 | assert(builder.isVector(left)); |
| 4576 | assert(builder.isMatrix(right)); |
| 4577 | break; |
| 4578 | case spv::OpMatrixTimesVector: |
| 4579 | assert(builder.isMatrix(left)); |
| 4580 | assert(builder.isVector(right)); |
| 4581 | break; |
| 4582 | case spv::OpMatrixTimesMatrix: |
| 4583 | assert(builder.isMatrix(left)); |
| 4584 | assert(builder.isMatrix(right)); |
| 4585 | break; |
| 4586 | default: |
| 4587 | firstClass = false; |
| 4588 | break; |
| 4589 | } |
| 4590 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4591 | if (firstClass) { |
| 4592 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4593 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4594 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4595 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4596 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4597 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4598 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4599 | // The result type of all of them is the same type as the (a) matrix operand. |
| 4600 | // The algorithm is to: |
| 4601 | // - break the matrix(es) into vectors |
| 4602 | // - smear any scalar to a vector |
| 4603 | // - do vector operations |
| 4604 | // - make a matrix out the vector results |
| 4605 | switch (op) { |
| 4606 | case spv::OpFAdd: |
| 4607 | case spv::OpFSub: |
| 4608 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4609 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4610 | case spv::OpFMul: |
| 4611 | { |
| 4612 | // one time set up... |
| 4613 | bool leftMat = builder.isMatrix(left); |
| 4614 | bool rightMat = builder.isMatrix(right); |
| 4615 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 4616 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 4617 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 4618 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 4619 | std::vector<spv::Id> results; |
| 4620 | spv::Id smearVec = spv::NoResult; |
| 4621 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4622 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4623 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4624 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4625 | |
| 4626 | // do each vector op |
| 4627 | for (unsigned int c = 0; c < numCols; ++c) { |
| 4628 | std::vector<unsigned int> indexes; |
| 4629 | indexes.push_back(c); |
| 4630 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 4631 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4632 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4633 | builder.addDecoration(result, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4634 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4635 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4639 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4640 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4641 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4642 | } |
| 4643 | default: |
| 4644 | assert(0); |
| 4645 | return spv::NoResult; |
| 4646 | } |
| 4647 | } |
| 4648 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4649 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
| 4650 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4651 | { |
| 4652 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4653 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4654 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4655 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 4656 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4657 | |
| 4658 | switch (op) { |
| 4659 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4660 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4661 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4662 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4663 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4664 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4665 | unaryOp = spv::OpSNegate; |
| 4666 | break; |
| 4667 | |
| 4668 | case glslang::EOpLogicalNot: |
| 4669 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4670 | unaryOp = spv::OpLogicalNot; |
| 4671 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4672 | case glslang::EOpBitwiseNot: |
| 4673 | unaryOp = spv::OpNot; |
| 4674 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4675 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4676 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4677 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4678 | break; |
| 4679 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4680 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4681 | break; |
| 4682 | case glslang::EOpTranspose: |
| 4683 | unaryOp = spv::OpTranspose; |
| 4684 | break; |
| 4685 | |
| 4686 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4687 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4688 | break; |
| 4689 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4690 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4691 | break; |
| 4692 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4693 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4694 | break; |
| 4695 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4696 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4697 | break; |
| 4698 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4699 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4700 | break; |
| 4701 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4702 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4703 | break; |
| 4704 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4705 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4706 | break; |
| 4707 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4708 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4709 | break; |
| 4710 | |
| 4711 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4712 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4713 | break; |
| 4714 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4715 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4716 | break; |
| 4717 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4718 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4719 | break; |
| 4720 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4721 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4722 | break; |
| 4723 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4724 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4725 | break; |
| 4726 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4727 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4728 | break; |
| 4729 | |
| 4730 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4731 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4732 | break; |
| 4733 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4734 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4735 | break; |
| 4736 | |
| 4737 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4738 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4739 | break; |
| 4740 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4741 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4742 | break; |
| 4743 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4744 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4745 | break; |
| 4746 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4747 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4748 | break; |
| 4749 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4750 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4751 | break; |
| 4752 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4753 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4754 | break; |
| 4755 | |
| 4756 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4757 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4758 | break; |
| 4759 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4760 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4761 | break; |
| 4762 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4763 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4764 | break; |
| 4765 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4766 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4767 | break; |
| 4768 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4769 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4770 | break; |
| 4771 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4772 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4773 | break; |
| 4774 | |
| 4775 | case glslang::EOpIsNan: |
| 4776 | unaryOp = spv::OpIsNan; |
| 4777 | break; |
| 4778 | case glslang::EOpIsInf: |
| 4779 | unaryOp = spv::OpIsInf; |
| 4780 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4781 | case glslang::EOpIsFinite: |
| 4782 | unaryOp = spv::OpIsFinite; |
| 4783 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4784 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 4785 | case glslang::EOpFloatBitsToInt: |
| 4786 | case glslang::EOpFloatBitsToUint: |
| 4787 | case glslang::EOpIntBitsToFloat: |
| 4788 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4789 | case glslang::EOpDoubleBitsToInt64: |
| 4790 | case glslang::EOpDoubleBitsToUint64: |
| 4791 | case glslang::EOpInt64BitsToDouble: |
| 4792 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4793 | case glslang::EOpFloat16BitsToInt16: |
| 4794 | case glslang::EOpFloat16BitsToUint16: |
| 4795 | case glslang::EOpInt16BitsToFloat16: |
| 4796 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 4797 | unaryOp = spv::OpBitcast; |
| 4798 | break; |
| 4799 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4800 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4801 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4802 | break; |
| 4803 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4804 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4805 | break; |
| 4806 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4807 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4808 | break; |
| 4809 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4810 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4811 | break; |
| 4812 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4813 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4814 | break; |
| 4815 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4816 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4817 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4818 | case glslang::EOpPackSnorm4x8: |
| 4819 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 4820 | break; |
| 4821 | case glslang::EOpUnpackSnorm4x8: |
| 4822 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 4823 | break; |
| 4824 | case glslang::EOpPackUnorm4x8: |
| 4825 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 4826 | break; |
| 4827 | case glslang::EOpUnpackUnorm4x8: |
| 4828 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 4829 | break; |
| 4830 | case glslang::EOpPackDouble2x32: |
| 4831 | libCall = spv::GLSLstd450PackDouble2x32; |
| 4832 | break; |
| 4833 | case glslang::EOpUnpackDouble2x32: |
| 4834 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 4835 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4836 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4837 | case glslang::EOpPackInt2x32: |
| 4838 | case glslang::EOpUnpackInt2x32: |
| 4839 | case glslang::EOpPackUint2x32: |
| 4840 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4841 | case glslang::EOpPack16: |
| 4842 | case glslang::EOpPack32: |
| 4843 | case glslang::EOpPack64: |
| 4844 | case glslang::EOpUnpack32: |
| 4845 | case glslang::EOpUnpack16: |
| 4846 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4847 | case glslang::EOpPackInt2x16: |
| 4848 | case glslang::EOpUnpackInt2x16: |
| 4849 | case glslang::EOpPackUint2x16: |
| 4850 | case glslang::EOpUnpackUint2x16: |
| 4851 | case glslang::EOpPackInt4x16: |
| 4852 | case glslang::EOpUnpackInt4x16: |
| 4853 | case glslang::EOpPackUint4x16: |
| 4854 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4855 | case glslang::EOpPackFloat2x16: |
| 4856 | case glslang::EOpUnpackFloat2x16: |
| 4857 | unaryOp = spv::OpBitcast; |
| 4858 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4859 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4860 | case glslang::EOpDPdx: |
| 4861 | unaryOp = spv::OpDPdx; |
| 4862 | break; |
| 4863 | case glslang::EOpDPdy: |
| 4864 | unaryOp = spv::OpDPdy; |
| 4865 | break; |
| 4866 | case glslang::EOpFwidth: |
| 4867 | unaryOp = spv::OpFwidth; |
| 4868 | break; |
| 4869 | case glslang::EOpDPdxFine: |
| 4870 | unaryOp = spv::OpDPdxFine; |
| 4871 | break; |
| 4872 | case glslang::EOpDPdyFine: |
| 4873 | unaryOp = spv::OpDPdyFine; |
| 4874 | break; |
| 4875 | case glslang::EOpFwidthFine: |
| 4876 | unaryOp = spv::OpFwidthFine; |
| 4877 | break; |
| 4878 | case glslang::EOpDPdxCoarse: |
| 4879 | unaryOp = spv::OpDPdxCoarse; |
| 4880 | break; |
| 4881 | case glslang::EOpDPdyCoarse: |
| 4882 | unaryOp = spv::OpDPdyCoarse; |
| 4883 | break; |
| 4884 | case glslang::EOpFwidthCoarse: |
| 4885 | unaryOp = spv::OpFwidthCoarse; |
| 4886 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4887 | case glslang::EOpInterpolateAtCentroid: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 4888 | #ifdef AMD_EXTENSIONS |
| 4889 | if (typeProxy == glslang::EbtFloat16) |
| 4890 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 4891 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4892 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 4893 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4894 | case glslang::EOpAny: |
| 4895 | unaryOp = spv::OpAny; |
| 4896 | break; |
| 4897 | case glslang::EOpAll: |
| 4898 | unaryOp = spv::OpAll; |
| 4899 | break; |
| 4900 | |
| 4901 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4902 | if (isFloat) |
| 4903 | libCall = spv::GLSLstd450FAbs; |
| 4904 | else |
| 4905 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4906 | break; |
| 4907 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4908 | if (isFloat) |
| 4909 | libCall = spv::GLSLstd450FSign; |
| 4910 | else |
| 4911 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4912 | break; |
| 4913 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4914 | case glslang::EOpAtomicCounterIncrement: |
| 4915 | case glslang::EOpAtomicCounterDecrement: |
| 4916 | case glslang::EOpAtomicCounter: |
| 4917 | { |
| 4918 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 4919 | std::vector<spv::Id> operands; |
| 4920 | operands.push_back(operand); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4921 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4922 | } |
| 4923 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4924 | case glslang::EOpBitFieldReverse: |
| 4925 | unaryOp = spv::OpBitReverse; |
| 4926 | break; |
| 4927 | case glslang::EOpBitCount: |
| 4928 | unaryOp = spv::OpBitCount; |
| 4929 | break; |
| 4930 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4931 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4932 | break; |
| 4933 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4934 | if (isUnsigned) |
| 4935 | libCall = spv::GLSLstd450FindUMsb; |
| 4936 | else |
| 4937 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4938 | break; |
| 4939 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 4940 | case glslang::EOpBallot: |
| 4941 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4942 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4943 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4944 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4945 | #ifdef AMD_EXTENSIONS |
| 4946 | case glslang::EOpMinInvocations: |
| 4947 | case glslang::EOpMaxInvocations: |
| 4948 | case glslang::EOpAddInvocations: |
| 4949 | case glslang::EOpMinInvocationsNonUniform: |
| 4950 | case glslang::EOpMaxInvocationsNonUniform: |
| 4951 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4952 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4953 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4954 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4955 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4956 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4957 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4958 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4959 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4960 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4961 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4962 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4963 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4964 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4965 | { |
| 4966 | std::vector<spv::Id> operands; |
| 4967 | operands.push_back(operand); |
| 4968 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 4969 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4970 | case glslang::EOpSubgroupAll: |
| 4971 | case glslang::EOpSubgroupAny: |
| 4972 | case glslang::EOpSubgroupAllEqual: |
| 4973 | case glslang::EOpSubgroupBroadcastFirst: |
| 4974 | case glslang::EOpSubgroupBallot: |
| 4975 | case glslang::EOpSubgroupInverseBallot: |
| 4976 | case glslang::EOpSubgroupBallotBitCount: |
| 4977 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 4978 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 4979 | case glslang::EOpSubgroupBallotFindLSB: |
| 4980 | case glslang::EOpSubgroupBallotFindMSB: |
| 4981 | case glslang::EOpSubgroupAdd: |
| 4982 | case glslang::EOpSubgroupMul: |
| 4983 | case glslang::EOpSubgroupMin: |
| 4984 | case glslang::EOpSubgroupMax: |
| 4985 | case glslang::EOpSubgroupAnd: |
| 4986 | case glslang::EOpSubgroupOr: |
| 4987 | case glslang::EOpSubgroupXor: |
| 4988 | case glslang::EOpSubgroupInclusiveAdd: |
| 4989 | case glslang::EOpSubgroupInclusiveMul: |
| 4990 | case glslang::EOpSubgroupInclusiveMin: |
| 4991 | case glslang::EOpSubgroupInclusiveMax: |
| 4992 | case glslang::EOpSubgroupInclusiveAnd: |
| 4993 | case glslang::EOpSubgroupInclusiveOr: |
| 4994 | case glslang::EOpSubgroupInclusiveXor: |
| 4995 | case glslang::EOpSubgroupExclusiveAdd: |
| 4996 | case glslang::EOpSubgroupExclusiveMul: |
| 4997 | case glslang::EOpSubgroupExclusiveMin: |
| 4998 | case glslang::EOpSubgroupExclusiveMax: |
| 4999 | case glslang::EOpSubgroupExclusiveAnd: |
| 5000 | case glslang::EOpSubgroupExclusiveOr: |
| 5001 | case glslang::EOpSubgroupExclusiveXor: |
| 5002 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5003 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5004 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 5005 | std::vector<spv::Id> operands; |
| 5006 | operands.push_back(operand); |
| 5007 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 5008 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5009 | #ifdef AMD_EXTENSIONS |
| 5010 | case glslang::EOpMbcnt: |
| 5011 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5012 | libCall = spv::MbcntAMD; |
| 5013 | break; |
| 5014 | |
| 5015 | case glslang::EOpCubeFaceIndex: |
| 5016 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5017 | libCall = spv::CubeFaceIndexAMD; |
| 5018 | break; |
| 5019 | |
| 5020 | case glslang::EOpCubeFaceCoord: |
| 5021 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5022 | libCall = spv::CubeFaceCoordAMD; |
| 5023 | break; |
| 5024 | #endif |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5025 | #ifdef NV_EXTENSIONS |
| 5026 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5027 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 5028 | break; |
| 5029 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5030 | default: |
| 5031 | return 0; |
| 5032 | } |
| 5033 | |
| 5034 | spv::Id id; |
| 5035 | if (libCall >= 0) { |
| 5036 | std::vector<spv::Id> args; |
| 5037 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5038 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5039 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5040 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5041 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5042 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5043 | builder.addDecoration(id, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5044 | builder.addDecoration(id, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5045 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5046 | } |
| 5047 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5048 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5049 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5050 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5051 | { |
| 5052 | // Handle unary operations vector by vector. |
| 5053 | // The result type is the same type as the original type. |
| 5054 | // The algorithm is to: |
| 5055 | // - break the matrix into vectors |
| 5056 | // - apply the operation to each vector |
| 5057 | // - make a matrix out the vector results |
| 5058 | |
| 5059 | // get the types sorted out |
| 5060 | int numCols = builder.getNumColumns(operand); |
| 5061 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5062 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 5063 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5064 | std::vector<spv::Id> results; |
| 5065 | |
| 5066 | // do each vector op |
| 5067 | for (int c = 0; c < numCols; ++c) { |
| 5068 | std::vector<unsigned int> indexes; |
| 5069 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5070 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 5071 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5072 | builder.addDecoration(destVec, decorations.noContraction); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5073 | builder.addDecoration(destVec, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5074 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5075 | } |
| 5076 | |
| 5077 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5078 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5079 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5080 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5081 | } |
| 5082 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5083 | // For converting integers where both the bitwidth and the signedness could |
| 5084 | // change, but only do the width change here. The caller is still responsible |
| 5085 | // for the signedness conversion. |
| 5086 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5087 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5088 | // Get the result type width, based on the type to convert to. |
| 5089 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5090 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5091 | case glslang::EOpConvInt16ToUint8: |
| 5092 | case glslang::EOpConvIntToUint8: |
| 5093 | case glslang::EOpConvInt64ToUint8: |
| 5094 | case glslang::EOpConvUint16ToInt8: |
| 5095 | case glslang::EOpConvUintToInt8: |
| 5096 | case glslang::EOpConvUint64ToInt8: |
| 5097 | width = 8; |
| 5098 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5099 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5100 | case glslang::EOpConvIntToUint16: |
| 5101 | case glslang::EOpConvInt64ToUint16: |
| 5102 | case glslang::EOpConvUint8ToInt16: |
| 5103 | case glslang::EOpConvUintToInt16: |
| 5104 | case glslang::EOpConvUint64ToInt16: |
| 5105 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5106 | break; |
| 5107 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5108 | case glslang::EOpConvInt16ToUint: |
| 5109 | case glslang::EOpConvInt64ToUint: |
| 5110 | case glslang::EOpConvUint8ToInt: |
| 5111 | case glslang::EOpConvUint16ToInt: |
| 5112 | case glslang::EOpConvUint64ToInt: |
| 5113 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5114 | break; |
| 5115 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5116 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5117 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5118 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5119 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5120 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5121 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5122 | break; |
| 5123 | |
| 5124 | default: |
| 5125 | assert(false && "Default missing"); |
| 5126 | break; |
| 5127 | } |
| 5128 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5129 | // Get the conversion operation and result type, |
| 5130 | // based on the target width, but the source type. |
| 5131 | spv::Id type = spv::NoType; |
| 5132 | spv::Op convOp = spv::OpNop; |
| 5133 | switch(op) { |
| 5134 | case glslang::EOpConvInt8ToUint16: |
| 5135 | case glslang::EOpConvInt8ToUint: |
| 5136 | case glslang::EOpConvInt8ToUint64: |
| 5137 | case glslang::EOpConvInt16ToUint8: |
| 5138 | case glslang::EOpConvInt16ToUint: |
| 5139 | case glslang::EOpConvInt16ToUint64: |
| 5140 | case glslang::EOpConvIntToUint8: |
| 5141 | case glslang::EOpConvIntToUint16: |
| 5142 | case glslang::EOpConvIntToUint64: |
| 5143 | case glslang::EOpConvInt64ToUint8: |
| 5144 | case glslang::EOpConvInt64ToUint16: |
| 5145 | case glslang::EOpConvInt64ToUint: |
| 5146 | convOp = spv::OpSConvert; |
| 5147 | type = builder.makeIntType(width); |
| 5148 | break; |
| 5149 | default: |
| 5150 | convOp = spv::OpUConvert; |
| 5151 | type = builder.makeUintType(width); |
| 5152 | break; |
| 5153 | } |
| 5154 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5155 | if (vectorSize > 0) |
| 5156 | type = builder.makeVectorType(type, vectorSize); |
| 5157 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5158 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5159 | } |
| 5160 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5161 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 5162 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5163 | { |
| 5164 | spv::Op convOp = spv::OpNop; |
| 5165 | spv::Id zero = 0; |
| 5166 | spv::Id one = 0; |
| 5167 | |
| 5168 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 5169 | |
| 5170 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5171 | case glslang::EOpConvInt8ToBool: |
| 5172 | case glslang::EOpConvUint8ToBool: |
| 5173 | zero = builder.makeUint8Constant(0); |
| 5174 | zero = makeSmearedConstant(zero, vectorSize); |
| 5175 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5176 | case glslang::EOpConvInt16ToBool: |
| 5177 | case glslang::EOpConvUint16ToBool: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5178 | zero = builder.makeUint16Constant(0); |
| 5179 | zero = makeSmearedConstant(zero, vectorSize); |
| 5180 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5181 | case glslang::EOpConvIntToBool: |
| 5182 | case glslang::EOpConvUintToBool: |
| 5183 | zero = builder.makeUintConstant(0); |
| 5184 | zero = makeSmearedConstant(zero, vectorSize); |
| 5185 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5186 | case glslang::EOpConvInt64ToBool: |
| 5187 | case glslang::EOpConvUint64ToBool: |
| 5188 | zero = builder.makeUint64Constant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5189 | zero = makeSmearedConstant(zero, vectorSize); |
| 5190 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 5191 | |
| 5192 | case glslang::EOpConvFloatToBool: |
| 5193 | zero = builder.makeFloatConstant(0.0F); |
| 5194 | zero = makeSmearedConstant(zero, vectorSize); |
| 5195 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5196 | |
| 5197 | case glslang::EOpConvDoubleToBool: |
| 5198 | zero = builder.makeDoubleConstant(0.0); |
| 5199 | zero = makeSmearedConstant(zero, vectorSize); |
| 5200 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 5201 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5202 | case glslang::EOpConvFloat16ToBool: |
| 5203 | zero = builder.makeFloat16Constant(0.0F); |
| 5204 | zero = makeSmearedConstant(zero, vectorSize); |
| 5205 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5206 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5207 | case glslang::EOpConvBoolToFloat: |
| 5208 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5209 | zero = builder.makeFloatConstant(0.0F); |
| 5210 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5211 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5212 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5213 | case glslang::EOpConvBoolToDouble: |
| 5214 | convOp = spv::OpSelect; |
| 5215 | zero = builder.makeDoubleConstant(0.0); |
| 5216 | one = builder.makeDoubleConstant(1.0); |
| 5217 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5218 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5219 | case glslang::EOpConvBoolToFloat16: |
| 5220 | convOp = spv::OpSelect; |
| 5221 | zero = builder.makeFloat16Constant(0.0F); |
| 5222 | one = builder.makeFloat16Constant(1.0F); |
| 5223 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5224 | |
| 5225 | case glslang::EOpConvBoolToInt8: |
| 5226 | zero = builder.makeInt8Constant(0); |
| 5227 | one = builder.makeInt8Constant(1); |
| 5228 | convOp = spv::OpSelect; |
| 5229 | break; |
| 5230 | |
| 5231 | case glslang::EOpConvBoolToUint8: |
| 5232 | zero = builder.makeUint8Constant(0); |
| 5233 | one = builder.makeUint8Constant(1); |
| 5234 | convOp = spv::OpSelect; |
| 5235 | break; |
| 5236 | |
| 5237 | case glslang::EOpConvBoolToInt16: |
| 5238 | zero = builder.makeInt16Constant(0); |
| 5239 | one = builder.makeInt16Constant(1); |
| 5240 | convOp = spv::OpSelect; |
| 5241 | break; |
| 5242 | |
| 5243 | case glslang::EOpConvBoolToUint16: |
| 5244 | zero = builder.makeUint16Constant(0); |
| 5245 | one = builder.makeUint16Constant(1); |
| 5246 | convOp = spv::OpSelect; |
| 5247 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5248 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5249 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5250 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5251 | if (op == glslang::EOpConvBoolToInt64) |
| 5252 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5253 | else |
| 5254 | zero = builder.makeIntConstant(0); |
| 5255 | |
| 5256 | if (op == glslang::EOpConvBoolToInt64) |
| 5257 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5258 | else |
| 5259 | one = builder.makeIntConstant(1); |
| 5260 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5261 | convOp = spv::OpSelect; |
| 5262 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5263 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5264 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5265 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5266 | if (op == glslang::EOpConvBoolToUint64) |
| 5267 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5268 | else |
| 5269 | zero = builder.makeUintConstant(0); |
| 5270 | |
| 5271 | if (op == glslang::EOpConvBoolToUint64) |
| 5272 | one = builder.makeUint64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5273 | else |
| 5274 | one = builder.makeUintConstant(1); |
| 5275 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5276 | convOp = spv::OpSelect; |
| 5277 | break; |
| 5278 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5279 | case glslang::EOpConvInt8ToFloat16: |
| 5280 | case glslang::EOpConvInt8ToFloat: |
| 5281 | case glslang::EOpConvInt8ToDouble: |
| 5282 | case glslang::EOpConvInt16ToFloat16: |
| 5283 | case glslang::EOpConvInt16ToFloat: |
| 5284 | case glslang::EOpConvInt16ToDouble: |
| 5285 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5286 | case glslang::EOpConvIntToFloat: |
| 5287 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5288 | case glslang::EOpConvInt64ToFloat: |
| 5289 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5290 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5291 | convOp = spv::OpConvertSToF; |
| 5292 | break; |
| 5293 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5294 | case glslang::EOpConvUint8ToFloat16: |
| 5295 | case glslang::EOpConvUint8ToFloat: |
| 5296 | case glslang::EOpConvUint8ToDouble: |
| 5297 | case glslang::EOpConvUint16ToFloat16: |
| 5298 | case glslang::EOpConvUint16ToFloat: |
| 5299 | case glslang::EOpConvUint16ToDouble: |
| 5300 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5301 | case glslang::EOpConvUintToFloat: |
| 5302 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5303 | case glslang::EOpConvUint64ToFloat: |
| 5304 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5305 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5306 | convOp = spv::OpConvertUToF; |
| 5307 | break; |
| 5308 | |
| 5309 | case glslang::EOpConvDoubleToFloat: |
| 5310 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5311 | case glslang::EOpConvDoubleToFloat16: |
| 5312 | case glslang::EOpConvFloat16ToDouble: |
| 5313 | case glslang::EOpConvFloatToFloat16: |
| 5314 | case glslang::EOpConvFloat16ToFloat: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5315 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 5316 | if (builder.isMatrixType(destType)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5317 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5318 | break; |
| 5319 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5320 | case glslang::EOpConvFloat16ToInt8: |
| 5321 | case glslang::EOpConvFloatToInt8: |
| 5322 | case glslang::EOpConvDoubleToInt8: |
| 5323 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5324 | case glslang::EOpConvFloatToInt16: |
| 5325 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5326 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5327 | case glslang::EOpConvFloatToInt: |
| 5328 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5329 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5330 | case glslang::EOpConvFloatToInt64: |
| 5331 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5332 | convOp = spv::OpConvertFToS; |
| 5333 | break; |
| 5334 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5335 | case glslang::EOpConvUint8ToInt8: |
| 5336 | case glslang::EOpConvInt8ToUint8: |
| 5337 | case glslang::EOpConvUint16ToInt16: |
| 5338 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5339 | case glslang::EOpConvUintToInt: |
| 5340 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5341 | case glslang::EOpConvUint64ToInt64: |
| 5342 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 5343 | if (builder.isInSpecConstCodeGenMode()) { |
| 5344 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5345 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 5346 | zero = builder.makeUint8Constant(0); |
| 5347 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5348 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5349 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 5350 | zero = builder.makeUint64Constant(0); |
| 5351 | } else { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5352 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5353 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 5354 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 5355 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5356 | // generating for OpSpecConstantOp instruction. |
| 5357 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5358 | } |
| 5359 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5360 | convOp = spv::OpBitcast; |
| 5361 | break; |
| 5362 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5363 | case glslang::EOpConvFloat16ToUint8: |
| 5364 | case glslang::EOpConvFloatToUint8: |
| 5365 | case glslang::EOpConvDoubleToUint8: |
| 5366 | case glslang::EOpConvFloat16ToUint16: |
| 5367 | case glslang::EOpConvFloatToUint16: |
| 5368 | case glslang::EOpConvDoubleToUint16: |
| 5369 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5370 | case glslang::EOpConvFloatToUint: |
| 5371 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5372 | case glslang::EOpConvFloatToUint64: |
| 5373 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5374 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5375 | convOp = spv::OpConvertFToU; |
| 5376 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5377 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5378 | case glslang::EOpConvInt8ToInt16: |
| 5379 | case glslang::EOpConvInt8ToInt: |
| 5380 | case glslang::EOpConvInt8ToInt64: |
| 5381 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5382 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5383 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5384 | case glslang::EOpConvIntToInt8: |
| 5385 | case glslang::EOpConvIntToInt16: |
| 5386 | case glslang::EOpConvIntToInt64: |
| 5387 | case glslang::EOpConvInt64ToInt8: |
| 5388 | case glslang::EOpConvInt64ToInt16: |
| 5389 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5390 | convOp = spv::OpSConvert; |
| 5391 | break; |
| 5392 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5393 | case glslang::EOpConvUint8ToUint16: |
| 5394 | case glslang::EOpConvUint8ToUint: |
| 5395 | case glslang::EOpConvUint8ToUint64: |
| 5396 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5397 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5398 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5399 | case glslang::EOpConvUintToUint8: |
| 5400 | case glslang::EOpConvUintToUint16: |
| 5401 | case glslang::EOpConvUintToUint64: |
| 5402 | case glslang::EOpConvUint64ToUint8: |
| 5403 | case glslang::EOpConvUint64ToUint16: |
| 5404 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5405 | convOp = spv::OpUConvert; |
| 5406 | break; |
| 5407 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5408 | case glslang::EOpConvInt8ToUint16: |
| 5409 | case glslang::EOpConvInt8ToUint: |
| 5410 | case glslang::EOpConvInt8ToUint64: |
| 5411 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5412 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5413 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5414 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5415 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5416 | case glslang::EOpConvIntToUint64: |
| 5417 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5418 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5419 | case glslang::EOpConvInt64ToUint: |
| 5420 | case glslang::EOpConvUint8ToInt16: |
| 5421 | case glslang::EOpConvUint8ToInt: |
| 5422 | case glslang::EOpConvUint8ToInt64: |
| 5423 | case glslang::EOpConvUint16ToInt8: |
| 5424 | case glslang::EOpConvUint16ToInt: |
| 5425 | case glslang::EOpConvUint16ToInt64: |
| 5426 | case glslang::EOpConvUintToInt8: |
| 5427 | case glslang::EOpConvUintToInt16: |
| 5428 | case glslang::EOpConvUintToInt64: |
| 5429 | case glslang::EOpConvUint64ToInt8: |
| 5430 | case glslang::EOpConvUint64ToInt16: |
| 5431 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5432 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5433 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5434 | |
| 5435 | if (builder.isInSpecConstCodeGenMode()) { |
| 5436 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5437 | switch(op) { |
| 5438 | case glslang::EOpConvInt16ToUint8: |
| 5439 | case glslang::EOpConvIntToUint8: |
| 5440 | case glslang::EOpConvInt64ToUint8: |
| 5441 | case glslang::EOpConvUint16ToInt8: |
| 5442 | case glslang::EOpConvUintToInt8: |
| 5443 | case glslang::EOpConvUint64ToInt8: |
| 5444 | zero = builder.makeUint8Constant(0); |
| 5445 | break; |
| 5446 | case glslang::EOpConvInt8ToUint16: |
| 5447 | case glslang::EOpConvIntToUint16: |
| 5448 | case glslang::EOpConvInt64ToUint16: |
| 5449 | case glslang::EOpConvUint8ToInt16: |
| 5450 | case glslang::EOpConvUintToInt16: |
| 5451 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5452 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5453 | break; |
| 5454 | case glslang::EOpConvInt8ToUint: |
| 5455 | case glslang::EOpConvInt16ToUint: |
| 5456 | case glslang::EOpConvInt64ToUint: |
| 5457 | case glslang::EOpConvUint8ToInt: |
| 5458 | case glslang::EOpConvUint16ToInt: |
| 5459 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5460 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5461 | break; |
| 5462 | case glslang::EOpConvInt8ToUint64: |
| 5463 | case glslang::EOpConvInt16ToUint64: |
| 5464 | case glslang::EOpConvIntToUint64: |
| 5465 | case glslang::EOpConvUint8ToInt64: |
| 5466 | case glslang::EOpConvUint16ToInt64: |
| 5467 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5468 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5469 | break; |
| 5470 | default: |
| 5471 | assert(false && "Default missing"); |
| 5472 | break; |
| 5473 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5474 | zero = makeSmearedConstant(zero, vectorSize); |
| 5475 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5476 | // generating for OpSpecConstantOp instruction. |
| 5477 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5478 | } |
| 5479 | // For normal run-time conversion instruction, use OpBitcast. |
| 5480 | convOp = spv::OpBitcast; |
| 5481 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5482 | default: |
| 5483 | break; |
| 5484 | } |
| 5485 | |
| 5486 | spv::Id result = 0; |
| 5487 | if (convOp == spv::OpNop) |
| 5488 | return result; |
| 5489 | |
| 5490 | if (convOp == spv::OpSelect) { |
| 5491 | zero = makeSmearedConstant(zero, vectorSize); |
| 5492 | one = makeSmearedConstant(one, vectorSize); |
| 5493 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 5494 | } else |
| 5495 | result = builder.createUnaryOp(convOp, destType, operand); |
| 5496 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5497 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 5498 | builder.addDecoration(result, decorations.nonUniform); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5499 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 5503 | { |
| 5504 | if (vectorSize == 0) |
| 5505 | return constant; |
| 5506 | |
| 5507 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 5508 | std::vector<spv::Id> components; |
| 5509 | for (int c = 0; c < vectorSize; ++c) |
| 5510 | components.push_back(constant); |
| 5511 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 5512 | } |
| 5513 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5514 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5515 | 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] | 5516 | { |
| 5517 | spv::Op opCode = spv::OpNop; |
| 5518 | |
| 5519 | switch (op) { |
| 5520 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5521 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5522 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5523 | opCode = spv::OpAtomicIAdd; |
| 5524 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5525 | case glslang::EOpAtomicCounterSubtract: |
| 5526 | opCode = spv::OpAtomicISub; |
| 5527 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5528 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5529 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5530 | case glslang::EOpAtomicCounterMin: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5531 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5532 | break; |
| 5533 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5534 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5535 | case glslang::EOpAtomicCounterMax: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5536 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5537 | break; |
| 5538 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5539 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5540 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5541 | opCode = spv::OpAtomicAnd; |
| 5542 | break; |
| 5543 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5544 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5545 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5546 | opCode = spv::OpAtomicOr; |
| 5547 | break; |
| 5548 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5549 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5550 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5551 | opCode = spv::OpAtomicXor; |
| 5552 | break; |
| 5553 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5554 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5555 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5556 | opCode = spv::OpAtomicExchange; |
| 5557 | break; |
| 5558 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5559 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5560 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5561 | opCode = spv::OpAtomicCompareExchange; |
| 5562 | break; |
| 5563 | case glslang::EOpAtomicCounterIncrement: |
| 5564 | opCode = spv::OpAtomicIIncrement; |
| 5565 | break; |
| 5566 | case glslang::EOpAtomicCounterDecrement: |
| 5567 | opCode = spv::OpAtomicIDecrement; |
| 5568 | break; |
| 5569 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5570 | case glslang::EOpImageAtomicLoad: |
| 5571 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5572 | opCode = spv::OpAtomicLoad; |
| 5573 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5574 | case glslang::EOpAtomicStore: |
| 5575 | case glslang::EOpImageAtomicStore: |
| 5576 | opCode = spv::OpAtomicStore; |
| 5577 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5578 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5579 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5580 | break; |
| 5581 | } |
| 5582 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5583 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 5584 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 5585 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5586 | // Sort out the operands |
| 5587 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5588 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 5589 | // - compare-exchange swaps the value and comparator |
| 5590 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5591 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5592 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 5593 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 5594 | spv::Id scopeId; |
| 5595 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 5596 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 5597 | } else { |
| 5598 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 5599 | } |
| 5600 | // semantics default to relaxed |
| 5601 | spv::Id semanticsId = builder.makeUintConstant(spv::MemorySemanticsMaskNone); |
| 5602 | spv::Id semanticsId2 = semanticsId; |
| 5603 | |
| 5604 | pointerId = operands[0]; |
| 5605 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 5606 | // no additional operands |
| 5607 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 5608 | compareId = operands[1]; |
| 5609 | valueId = operands[2]; |
| 5610 | if (operands.size() > 3) { |
| 5611 | scopeId = operands[3]; |
| 5612 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 5613 | semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 5614 | } |
| 5615 | } else if (opCode == spv::OpAtomicLoad) { |
| 5616 | if (operands.size() > 1) { |
| 5617 | scopeId = operands[1]; |
| 5618 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 5619 | } |
| 5620 | } else { |
| 5621 | // atomic store or RMW |
| 5622 | valueId = operands[1]; |
| 5623 | if (operands.size() > 2) { |
| 5624 | scopeId = operands[2]; |
| 5625 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 5626 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5627 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5628 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5629 | // Check for capabilities |
| 5630 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
| 5631 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 5632 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 5633 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5634 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5635 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 5636 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 5637 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5638 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5639 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 5640 | spvAtomicOperands.push_back(pointerId); |
| 5641 | spvAtomicOperands.push_back(scopeId); |
| 5642 | spvAtomicOperands.push_back(semanticsId); |
| 5643 | if (opCode == spv::OpAtomicCompareExchange) { |
| 5644 | spvAtomicOperands.push_back(semanticsId2); |
| 5645 | spvAtomicOperands.push_back(valueId); |
| 5646 | spvAtomicOperands.push_back(compareId); |
| 5647 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 5648 | spvAtomicOperands.push_back(valueId); |
| 5649 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5650 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5651 | if (opCode == spv::OpAtomicStore) { |
| 5652 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 5653 | return 0; |
| 5654 | } else { |
| 5655 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 5656 | |
| 5657 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 5658 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 5659 | if (op == glslang::EOpAtomicCounterDecrement) |
| 5660 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 5661 | |
| 5662 | return resultId; |
| 5663 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5664 | } |
| 5665 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5666 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5667 | 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] | 5668 | { |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 5669 | #ifdef AMD_EXTENSIONS |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5670 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5671 | bool isFloat = isTypeFloat(typeProxy); |
Corentin Wallez | e706142 | 2018-08-08 15:20:15 +0200 | [diff] [blame] | 5672 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5673 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5674 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5675 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5676 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 5677 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5678 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 5679 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5680 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 5681 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5682 | } else if (op == glslang::EOpAnyInvocation || |
| 5683 | op == glslang::EOpAllInvocations || |
| 5684 | op == glslang::EOpAllInvocationsEqual) { |
| 5685 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 5686 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5687 | } else { |
| 5688 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 5689 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 5690 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 5691 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5692 | op == glslang::EOpAddInvocationsNonUniform || |
| 5693 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 5694 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 5695 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 5696 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 5697 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 5698 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 5699 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 5700 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5701 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5702 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5703 | switch (op) { |
| 5704 | case glslang::EOpMinInvocations: |
| 5705 | case glslang::EOpMaxInvocations: |
| 5706 | case glslang::EOpAddInvocations: |
| 5707 | case glslang::EOpMinInvocationsNonUniform: |
| 5708 | case glslang::EOpMaxInvocationsNonUniform: |
| 5709 | case glslang::EOpAddInvocationsNonUniform: |
| 5710 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5711 | break; |
| 5712 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5713 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5714 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5715 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5716 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5717 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5718 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5719 | break; |
| 5720 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5721 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5722 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5723 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5724 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5725 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 5726 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5727 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 5728 | default: |
| 5729 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5730 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5731 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 5732 | spvGroupOperands.push_back(scope); |
| 5733 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 5734 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5735 | spvGroupOperands.push_back(groupOp); |
| 5736 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5737 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5738 | } |
| 5739 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5740 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 5741 | spv::IdImmediate op = { true, *opIt }; |
| 5742 | spvGroupOperands.push_back(op); |
| 5743 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5744 | |
| 5745 | switch (op) { |
| 5746 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5747 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5748 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5749 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5750 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5751 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5752 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5753 | opCode = spv::OpSubgroupAllEqualKHR; |
| 5754 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5755 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5756 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5757 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5758 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5759 | break; |
| 5760 | case glslang::EOpReadFirstInvocation: |
| 5761 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 5762 | break; |
| 5763 | case glslang::EOpBallot: |
| 5764 | { |
| 5765 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 5766 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 5767 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 5768 | // |
| 5769 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 5770 | // |
| 5771 | spv::Id uintType = builder.makeUintType(32); |
| 5772 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 5773 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 5774 | |
| 5775 | std::vector<spv::Id> components; |
| 5776 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 5777 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 5778 | |
| 5779 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 5780 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 5781 | builder.createCompositeConstruct(uvec2Type, components)); |
| 5782 | } |
| 5783 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5784 | #ifdef AMD_EXTENSIONS |
| 5785 | case glslang::EOpMinInvocations: |
| 5786 | case glslang::EOpMaxInvocations: |
| 5787 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5788 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5789 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5790 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5791 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5792 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5793 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5794 | if (op == glslang::EOpMinInvocations || |
| 5795 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 5796 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5797 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5798 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5799 | else { |
| 5800 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5801 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5802 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5803 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5804 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5805 | } else if (op == glslang::EOpMaxInvocations || |
| 5806 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 5807 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5808 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5809 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5810 | else { |
| 5811 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5812 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5813 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5814 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5815 | } |
| 5816 | } else { |
| 5817 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5818 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5819 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5820 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5821 | } |
| 5822 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5823 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5824 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5825 | |
| 5826 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5827 | case glslang::EOpMinInvocationsNonUniform: |
| 5828 | case glslang::EOpMaxInvocationsNonUniform: |
| 5829 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5830 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5831 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5832 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5833 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5834 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5835 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 5836 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 5837 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 5838 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5839 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5840 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5841 | else { |
| 5842 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5843 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5844 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5845 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5846 | } |
| 5847 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5848 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 5849 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 5850 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5851 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5852 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5853 | else { |
| 5854 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5855 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5856 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5857 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | else { |
| 5861 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5862 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5863 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5864 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5865 | } |
| 5866 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5867 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5868 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5869 | |
| 5870 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5871 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5872 | default: |
| 5873 | logger->missingFunctionality("invocation operation"); |
| 5874 | return spv::NoResult; |
| 5875 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5876 | |
| 5877 | assert(opCode != spv::OpNop); |
| 5878 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5879 | } |
| 5880 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5881 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5882 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 5883 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5884 | { |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5885 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5886 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 5887 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5888 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5889 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5890 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 5891 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 5892 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5893 | #else |
| 5894 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 5895 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5896 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 5897 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5898 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5899 | |
| 5900 | // Handle group invocation operations scalar by scalar. |
| 5901 | // The result type is the same type as the original type. |
| 5902 | // The algorithm is to: |
| 5903 | // - break the vector into scalars |
| 5904 | // - apply the operation to each scalar |
| 5905 | // - make a vector out the scalar results |
| 5906 | |
| 5907 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5908 | int numComponents = builder.getNumComponents(operands[0]); |
| 5909 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5910 | std::vector<spv::Id> results; |
| 5911 | |
| 5912 | // do each scalar op |
| 5913 | for (int comp = 0; comp < numComponents; ++comp) { |
| 5914 | std::vector<unsigned int> indexes; |
| 5915 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5916 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 5917 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5918 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 5919 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5920 | spv::IdImmediate operand = { true, operands[1] }; |
| 5921 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5922 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5923 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 5924 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5925 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5926 | spv::IdImmediate operand = { true, operands[1] }; |
| 5927 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5928 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5929 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 5930 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 5931 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5932 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5933 | spvGroupOperands.push_back(scalar); |
| 5934 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5935 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5936 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5937 | } |
| 5938 | |
| 5939 | // put the pieces together |
| 5940 | return builder.createCompositeConstruct(typeId, results); |
| 5941 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5942 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5943 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5944 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 5945 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5946 | { |
| 5947 | // Add the required capabilities. |
| 5948 | switch (op) { |
| 5949 | case glslang::EOpSubgroupElect: |
| 5950 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5951 | break; |
| 5952 | case glslang::EOpSubgroupAll: |
| 5953 | case glslang::EOpSubgroupAny: |
| 5954 | case glslang::EOpSubgroupAllEqual: |
| 5955 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5956 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 5957 | break; |
| 5958 | case glslang::EOpSubgroupBroadcast: |
| 5959 | case glslang::EOpSubgroupBroadcastFirst: |
| 5960 | case glslang::EOpSubgroupBallot: |
| 5961 | case glslang::EOpSubgroupInverseBallot: |
| 5962 | case glslang::EOpSubgroupBallotBitExtract: |
| 5963 | case glslang::EOpSubgroupBallotBitCount: |
| 5964 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5965 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5966 | case glslang::EOpSubgroupBallotFindLSB: |
| 5967 | case glslang::EOpSubgroupBallotFindMSB: |
| 5968 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5969 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 5970 | break; |
| 5971 | case glslang::EOpSubgroupShuffle: |
| 5972 | case glslang::EOpSubgroupShuffleXor: |
| 5973 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5974 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 5975 | break; |
| 5976 | case glslang::EOpSubgroupShuffleUp: |
| 5977 | case glslang::EOpSubgroupShuffleDown: |
| 5978 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5979 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 5980 | break; |
| 5981 | case glslang::EOpSubgroupAdd: |
| 5982 | case glslang::EOpSubgroupMul: |
| 5983 | case glslang::EOpSubgroupMin: |
| 5984 | case glslang::EOpSubgroupMax: |
| 5985 | case glslang::EOpSubgroupAnd: |
| 5986 | case glslang::EOpSubgroupOr: |
| 5987 | case glslang::EOpSubgroupXor: |
| 5988 | case glslang::EOpSubgroupInclusiveAdd: |
| 5989 | case glslang::EOpSubgroupInclusiveMul: |
| 5990 | case glslang::EOpSubgroupInclusiveMin: |
| 5991 | case glslang::EOpSubgroupInclusiveMax: |
| 5992 | case glslang::EOpSubgroupInclusiveAnd: |
| 5993 | case glslang::EOpSubgroupInclusiveOr: |
| 5994 | case glslang::EOpSubgroupInclusiveXor: |
| 5995 | case glslang::EOpSubgroupExclusiveAdd: |
| 5996 | case glslang::EOpSubgroupExclusiveMul: |
| 5997 | case glslang::EOpSubgroupExclusiveMin: |
| 5998 | case glslang::EOpSubgroupExclusiveMax: |
| 5999 | case glslang::EOpSubgroupExclusiveAnd: |
| 6000 | case glslang::EOpSubgroupExclusiveOr: |
| 6001 | case glslang::EOpSubgroupExclusiveXor: |
| 6002 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6003 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 6004 | break; |
| 6005 | case glslang::EOpSubgroupClusteredAdd: |
| 6006 | case glslang::EOpSubgroupClusteredMul: |
| 6007 | case glslang::EOpSubgroupClusteredMin: |
| 6008 | case glslang::EOpSubgroupClusteredMax: |
| 6009 | case glslang::EOpSubgroupClusteredAnd: |
| 6010 | case glslang::EOpSubgroupClusteredOr: |
| 6011 | case glslang::EOpSubgroupClusteredXor: |
| 6012 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6013 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 6014 | break; |
| 6015 | case glslang::EOpSubgroupQuadBroadcast: |
| 6016 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6017 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6018 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 6019 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6020 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 6021 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6022 | #ifdef NV_EXTENSIONS |
| 6023 | case glslang::EOpSubgroupPartitionedAdd: |
| 6024 | case glslang::EOpSubgroupPartitionedMul: |
| 6025 | case glslang::EOpSubgroupPartitionedMin: |
| 6026 | case glslang::EOpSubgroupPartitionedMax: |
| 6027 | case glslang::EOpSubgroupPartitionedAnd: |
| 6028 | case glslang::EOpSubgroupPartitionedOr: |
| 6029 | case glslang::EOpSubgroupPartitionedXor: |
| 6030 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6031 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6032 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6033 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6034 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6035 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6036 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6037 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6038 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6039 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6040 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6041 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6042 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6043 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6044 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 6045 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 6046 | break; |
| 6047 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6048 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6049 | } |
| 6050 | |
| 6051 | const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
| 6052 | const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
| 6053 | const bool isBool = typeProxy == glslang::EbtBool; |
| 6054 | |
| 6055 | spv::Op opCode = spv::OpNop; |
| 6056 | |
| 6057 | // Figure out which opcode to use. |
| 6058 | switch (op) { |
| 6059 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 6060 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 6061 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 6062 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 6063 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 6064 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 6065 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 6066 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 6067 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 6068 | case glslang::EOpSubgroupBallotBitCount: |
| 6069 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6070 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 6071 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 6072 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 6073 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 6074 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 6075 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 6076 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 6077 | case glslang::EOpSubgroupAdd: |
| 6078 | case glslang::EOpSubgroupInclusiveAdd: |
| 6079 | case glslang::EOpSubgroupExclusiveAdd: |
| 6080 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6081 | #ifdef NV_EXTENSIONS |
| 6082 | case glslang::EOpSubgroupPartitionedAdd: |
| 6083 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6084 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6085 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6086 | if (isFloat) { |
| 6087 | opCode = spv::OpGroupNonUniformFAdd; |
| 6088 | } else { |
| 6089 | opCode = spv::OpGroupNonUniformIAdd; |
| 6090 | } |
| 6091 | break; |
| 6092 | case glslang::EOpSubgroupMul: |
| 6093 | case glslang::EOpSubgroupInclusiveMul: |
| 6094 | case glslang::EOpSubgroupExclusiveMul: |
| 6095 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6096 | #ifdef NV_EXTENSIONS |
| 6097 | case glslang::EOpSubgroupPartitionedMul: |
| 6098 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6099 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6100 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6101 | if (isFloat) { |
| 6102 | opCode = spv::OpGroupNonUniformFMul; |
| 6103 | } else { |
| 6104 | opCode = spv::OpGroupNonUniformIMul; |
| 6105 | } |
| 6106 | break; |
| 6107 | case glslang::EOpSubgroupMin: |
| 6108 | case glslang::EOpSubgroupInclusiveMin: |
| 6109 | case glslang::EOpSubgroupExclusiveMin: |
| 6110 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6111 | #ifdef NV_EXTENSIONS |
| 6112 | case glslang::EOpSubgroupPartitionedMin: |
| 6113 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6114 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6115 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6116 | if (isFloat) { |
| 6117 | opCode = spv::OpGroupNonUniformFMin; |
| 6118 | } else if (isUnsigned) { |
| 6119 | opCode = spv::OpGroupNonUniformUMin; |
| 6120 | } else { |
| 6121 | opCode = spv::OpGroupNonUniformSMin; |
| 6122 | } |
| 6123 | break; |
| 6124 | case glslang::EOpSubgroupMax: |
| 6125 | case glslang::EOpSubgroupInclusiveMax: |
| 6126 | case glslang::EOpSubgroupExclusiveMax: |
| 6127 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6128 | #ifdef NV_EXTENSIONS |
| 6129 | case glslang::EOpSubgroupPartitionedMax: |
| 6130 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6131 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6132 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6133 | if (isFloat) { |
| 6134 | opCode = spv::OpGroupNonUniformFMax; |
| 6135 | } else if (isUnsigned) { |
| 6136 | opCode = spv::OpGroupNonUniformUMax; |
| 6137 | } else { |
| 6138 | opCode = spv::OpGroupNonUniformSMax; |
| 6139 | } |
| 6140 | break; |
| 6141 | case glslang::EOpSubgroupAnd: |
| 6142 | case glslang::EOpSubgroupInclusiveAnd: |
| 6143 | case glslang::EOpSubgroupExclusiveAnd: |
| 6144 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6145 | #ifdef NV_EXTENSIONS |
| 6146 | case glslang::EOpSubgroupPartitionedAnd: |
| 6147 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6148 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6149 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6150 | if (isBool) { |
| 6151 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 6152 | } else { |
| 6153 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 6154 | } |
| 6155 | break; |
| 6156 | case glslang::EOpSubgroupOr: |
| 6157 | case glslang::EOpSubgroupInclusiveOr: |
| 6158 | case glslang::EOpSubgroupExclusiveOr: |
| 6159 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6160 | #ifdef NV_EXTENSIONS |
| 6161 | case glslang::EOpSubgroupPartitionedOr: |
| 6162 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6163 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6164 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6165 | if (isBool) { |
| 6166 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 6167 | } else { |
| 6168 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 6169 | } |
| 6170 | break; |
| 6171 | case glslang::EOpSubgroupXor: |
| 6172 | case glslang::EOpSubgroupInclusiveXor: |
| 6173 | case glslang::EOpSubgroupExclusiveXor: |
| 6174 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6175 | #ifdef NV_EXTENSIONS |
| 6176 | case glslang::EOpSubgroupPartitionedXor: |
| 6177 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6178 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6179 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6180 | if (isBool) { |
| 6181 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 6182 | } else { |
| 6183 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 6184 | } |
| 6185 | break; |
| 6186 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 6187 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6188 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6189 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 6190 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6191 | } |
| 6192 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6193 | // get the right Group Operation |
| 6194 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6195 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6196 | default: |
| 6197 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6198 | case glslang::EOpSubgroupBallotBitCount: |
| 6199 | case glslang::EOpSubgroupAdd: |
| 6200 | case glslang::EOpSubgroupMul: |
| 6201 | case glslang::EOpSubgroupMin: |
| 6202 | case glslang::EOpSubgroupMax: |
| 6203 | case glslang::EOpSubgroupAnd: |
| 6204 | case glslang::EOpSubgroupOr: |
| 6205 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6206 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6207 | break; |
| 6208 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6209 | case glslang::EOpSubgroupInclusiveAdd: |
| 6210 | case glslang::EOpSubgroupInclusiveMul: |
| 6211 | case glslang::EOpSubgroupInclusiveMin: |
| 6212 | case glslang::EOpSubgroupInclusiveMax: |
| 6213 | case glslang::EOpSubgroupInclusiveAnd: |
| 6214 | case glslang::EOpSubgroupInclusiveOr: |
| 6215 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6216 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6217 | break; |
| 6218 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6219 | case glslang::EOpSubgroupExclusiveAdd: |
| 6220 | case glslang::EOpSubgroupExclusiveMul: |
| 6221 | case glslang::EOpSubgroupExclusiveMin: |
| 6222 | case glslang::EOpSubgroupExclusiveMax: |
| 6223 | case glslang::EOpSubgroupExclusiveAnd: |
| 6224 | case glslang::EOpSubgroupExclusiveOr: |
| 6225 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6226 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6227 | break; |
| 6228 | case glslang::EOpSubgroupClusteredAdd: |
| 6229 | case glslang::EOpSubgroupClusteredMul: |
| 6230 | case glslang::EOpSubgroupClusteredMin: |
| 6231 | case glslang::EOpSubgroupClusteredMax: |
| 6232 | case glslang::EOpSubgroupClusteredAnd: |
| 6233 | case glslang::EOpSubgroupClusteredOr: |
| 6234 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6235 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6236 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6237 | #ifdef NV_EXTENSIONS |
| 6238 | case glslang::EOpSubgroupPartitionedAdd: |
| 6239 | case glslang::EOpSubgroupPartitionedMul: |
| 6240 | case glslang::EOpSubgroupPartitionedMin: |
| 6241 | case glslang::EOpSubgroupPartitionedMax: |
| 6242 | case glslang::EOpSubgroupPartitionedAnd: |
| 6243 | case glslang::EOpSubgroupPartitionedOr: |
| 6244 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6245 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6246 | break; |
| 6247 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6248 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6249 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6250 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6251 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6252 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6253 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6254 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6255 | break; |
| 6256 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6257 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6258 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6259 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6260 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6261 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6262 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6263 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6264 | break; |
| 6265 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6266 | } |
| 6267 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6268 | // build the instruction |
| 6269 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 6270 | |
| 6271 | // Every operation begins with the Execution Scope operand. |
| 6272 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6273 | spvGroupOperands.push_back(executionScope); |
| 6274 | |
| 6275 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 6276 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6277 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6278 | spvGroupOperands.push_back(groupOperand); |
| 6279 | } |
| 6280 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6281 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6282 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 6283 | spv::IdImmediate operand = { true, *opIt }; |
| 6284 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6288 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6289 | switch (op) { |
| 6290 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6291 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 6292 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 6293 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 6294 | } |
| 6295 | if (directionId != spv::NoResult) { |
| 6296 | spv::IdImmediate direction = { true, directionId }; |
| 6297 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6298 | } |
| 6299 | |
| 6300 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 6301 | } |
| 6302 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6303 | 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] | 6304 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6305 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6306 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6307 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6308 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6309 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6310 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 6311 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6312 | spv::Id typeId0 = 0; |
| 6313 | if (consumedOperands > 0) |
| 6314 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6315 | spv::Id typeId1 = 0; |
| 6316 | if (consumedOperands > 1) |
| 6317 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6318 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6319 | |
| 6320 | switch (op) { |
| 6321 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6322 | if (isFloat) |
| 6323 | libCall = spv::GLSLstd450FMin; |
| 6324 | else if (isUnsigned) |
| 6325 | libCall = spv::GLSLstd450UMin; |
| 6326 | else |
| 6327 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6328 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6329 | break; |
| 6330 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6331 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6332 | break; |
| 6333 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6334 | if (isFloat) |
| 6335 | libCall = spv::GLSLstd450FMax; |
| 6336 | else if (isUnsigned) |
| 6337 | libCall = spv::GLSLstd450UMax; |
| 6338 | else |
| 6339 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6340 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6341 | break; |
| 6342 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6343 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6344 | break; |
| 6345 | case glslang::EOpDot: |
| 6346 | opCode = spv::OpDot; |
| 6347 | break; |
| 6348 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6349 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6350 | break; |
| 6351 | |
| 6352 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6353 | if (isFloat) |
| 6354 | libCall = spv::GLSLstd450FClamp; |
| 6355 | else if (isUnsigned) |
| 6356 | libCall = spv::GLSLstd450UClamp; |
| 6357 | else |
| 6358 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6359 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 6360 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6361 | break; |
| 6362 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6363 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 6364 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6365 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6366 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6367 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 6368 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6369 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6370 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6371 | break; |
| 6372 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6373 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6374 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6375 | break; |
| 6376 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6377 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 6378 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 6379 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6380 | break; |
| 6381 | |
| 6382 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6383 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6384 | break; |
| 6385 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6386 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6387 | break; |
| 6388 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6389 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6390 | break; |
| 6391 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6392 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6393 | break; |
| 6394 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6395 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6396 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6397 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6398 | #ifdef AMD_EXTENSIONS |
| 6399 | if (typeProxy == glslang::EbtFloat16) |
| 6400 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6401 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6402 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 6403 | break; |
| 6404 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6405 | #ifdef AMD_EXTENSIONS |
| 6406 | if (typeProxy == glslang::EbtFloat16) |
| 6407 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6408 | #endif |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 6409 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 6410 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6411 | case glslang::EOpAddCarry: |
| 6412 | opCode = spv::OpIAddCarry; |
| 6413 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6414 | consumedOperands = 2; |
| 6415 | break; |
| 6416 | case glslang::EOpSubBorrow: |
| 6417 | opCode = spv::OpISubBorrow; |
| 6418 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6419 | consumedOperands = 2; |
| 6420 | break; |
| 6421 | case glslang::EOpUMulExtended: |
| 6422 | opCode = spv::OpUMulExtended; |
| 6423 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6424 | consumedOperands = 2; |
| 6425 | break; |
| 6426 | case glslang::EOpIMulExtended: |
| 6427 | opCode = spv::OpSMulExtended; |
| 6428 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 6429 | consumedOperands = 2; |
| 6430 | break; |
| 6431 | case glslang::EOpBitfieldExtract: |
| 6432 | if (isUnsigned) |
| 6433 | opCode = spv::OpBitFieldUExtract; |
| 6434 | else |
| 6435 | opCode = spv::OpBitFieldSExtract; |
| 6436 | break; |
| 6437 | case glslang::EOpBitfieldInsert: |
| 6438 | opCode = spv::OpBitFieldInsert; |
| 6439 | break; |
| 6440 | |
| 6441 | case glslang::EOpFma: |
| 6442 | libCall = spv::GLSLstd450Fma; |
| 6443 | break; |
| 6444 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6445 | { |
| 6446 | libCall = spv::GLSLstd450FrexpStruct; |
| 6447 | assert(builder.isPointerType(typeId1)); |
| 6448 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6449 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 6450 | #ifdef AMD_EXTENSIONS |
| 6451 | if (width == 16) |
| 6452 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 6453 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 6454 | #endif |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6455 | if (builder.getNumComponents(operands[0]) == 1) |
| 6456 | frexpIntType = builder.makeIntegerType(width, true); |
| 6457 | else |
| 6458 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 6459 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 6460 | consumedOperands = 1; |
| 6461 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6462 | break; |
| 6463 | case glslang::EOpLdexp: |
| 6464 | libCall = spv::GLSLstd450Ldexp; |
| 6465 | break; |
| 6466 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6467 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6468 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6469 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6470 | case glslang::EOpSubgroupBroadcast: |
| 6471 | case glslang::EOpSubgroupBallotBitExtract: |
| 6472 | case glslang::EOpSubgroupShuffle: |
| 6473 | case glslang::EOpSubgroupShuffleXor: |
| 6474 | case glslang::EOpSubgroupShuffleUp: |
| 6475 | case glslang::EOpSubgroupShuffleDown: |
| 6476 | case glslang::EOpSubgroupClusteredAdd: |
| 6477 | case glslang::EOpSubgroupClusteredMul: |
| 6478 | case glslang::EOpSubgroupClusteredMin: |
| 6479 | case glslang::EOpSubgroupClusteredMax: |
| 6480 | case glslang::EOpSubgroupClusteredAnd: |
| 6481 | case glslang::EOpSubgroupClusteredOr: |
| 6482 | case glslang::EOpSubgroupClusteredXor: |
| 6483 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6484 | #ifdef NV_EXTENSIONS |
| 6485 | case glslang::EOpSubgroupPartitionedAdd: |
| 6486 | case glslang::EOpSubgroupPartitionedMul: |
| 6487 | case glslang::EOpSubgroupPartitionedMin: |
| 6488 | case glslang::EOpSubgroupPartitionedMax: |
| 6489 | case glslang::EOpSubgroupPartitionedAnd: |
| 6490 | case glslang::EOpSubgroupPartitionedOr: |
| 6491 | case glslang::EOpSubgroupPartitionedXor: |
| 6492 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6493 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6494 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6495 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6496 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6497 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6498 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6499 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6500 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6501 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6502 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6503 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6504 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6505 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6506 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6507 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 6508 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6509 | #ifdef AMD_EXTENSIONS |
| 6510 | case glslang::EOpSwizzleInvocations: |
| 6511 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6512 | libCall = spv::SwizzleInvocationsAMD; |
| 6513 | break; |
| 6514 | case glslang::EOpSwizzleInvocationsMasked: |
| 6515 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6516 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 6517 | break; |
| 6518 | case glslang::EOpWriteInvocation: |
| 6519 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6520 | libCall = spv::WriteInvocationAMD; |
| 6521 | break; |
| 6522 | |
| 6523 | case glslang::EOpMin3: |
| 6524 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6525 | if (isFloat) |
| 6526 | libCall = spv::FMin3AMD; |
| 6527 | else { |
| 6528 | if (isUnsigned) |
| 6529 | libCall = spv::UMin3AMD; |
| 6530 | else |
| 6531 | libCall = spv::SMin3AMD; |
| 6532 | } |
| 6533 | break; |
| 6534 | case glslang::EOpMax3: |
| 6535 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6536 | if (isFloat) |
| 6537 | libCall = spv::FMax3AMD; |
| 6538 | else { |
| 6539 | if (isUnsigned) |
| 6540 | libCall = spv::UMax3AMD; |
| 6541 | else |
| 6542 | libCall = spv::SMax3AMD; |
| 6543 | } |
| 6544 | break; |
| 6545 | case glslang::EOpMid3: |
| 6546 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 6547 | if (isFloat) |
| 6548 | libCall = spv::FMid3AMD; |
| 6549 | else { |
| 6550 | if (isUnsigned) |
| 6551 | libCall = spv::UMid3AMD; |
| 6552 | else |
| 6553 | libCall = spv::SMid3AMD; |
| 6554 | } |
| 6555 | break; |
| 6556 | |
| 6557 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 6558 | if (typeProxy == glslang::EbtFloat16) |
| 6559 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6560 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 6561 | libCall = spv::InterpolateAtVertexAMD; |
| 6562 | break; |
| 6563 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6564 | case glslang::EOpBarrier: |
| 6565 | { |
| 6566 | // This is for the extended controlBarrier function, with four operands. |
| 6567 | // The unextended barrier() goes through createNoArgOperation. |
| 6568 | assert(operands.size() == 4); |
| 6569 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 6570 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 6571 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 6572 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 6573 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 6574 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6575 | } |
| 6576 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) { |
| 6577 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6578 | } |
| 6579 | return 0; |
| 6580 | } |
| 6581 | break; |
| 6582 | case glslang::EOpMemoryBarrier: |
| 6583 | { |
| 6584 | // This is for the extended memoryBarrier function, with three operands. |
| 6585 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 6586 | assert(operands.size() == 3); |
| 6587 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 6588 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 6589 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 6590 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | spv::MemorySemanticsMakeVisibleKHRMask | spv::MemorySemanticsOutputMemoryKHRMask)) { |
| 6591 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6592 | } |
| 6593 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 6594 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6595 | } |
| 6596 | return 0; |
| 6597 | } |
| 6598 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6599 | default: |
| 6600 | return 0; |
| 6601 | } |
| 6602 | |
| 6603 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 6604 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 6605 | // Use an extended instruction from the standard library. |
| 6606 | // Construct the call arguments, without modifying the original operands vector. |
| 6607 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 6608 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6609 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 6610 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6611 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6612 | case 0: |
| 6613 | // should all be handled by visitAggregate and createNoArgOperation |
| 6614 | assert(0); |
| 6615 | return 0; |
| 6616 | case 1: |
| 6617 | // should all be handled by createUnaryOperation |
| 6618 | assert(0); |
| 6619 | return 0; |
| 6620 | case 2: |
| 6621 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 6622 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6623 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6624 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 6625 | assert(consumedOperands == operands.size()); |
| 6626 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6627 | break; |
| 6628 | } |
| 6629 | } |
| 6630 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6631 | // Decode the return types that were structures |
| 6632 | switch (op) { |
| 6633 | case glslang::EOpAddCarry: |
| 6634 | case glslang::EOpSubBorrow: |
| 6635 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 6636 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 6637 | break; |
| 6638 | case glslang::EOpUMulExtended: |
| 6639 | case glslang::EOpIMulExtended: |
| 6640 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 6641 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 6642 | break; |
| 6643 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6644 | { |
| 6645 | assert(operands.size() == 2); |
| 6646 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 6647 | // "exp" is floating-point type (from HLSL intrinsic) |
| 6648 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 6649 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 6650 | builder.createStore(member1, operands[1]); |
| 6651 | } else |
| 6652 | // "exp" is integer type (from GLSL built-in function) |
| 6653 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 6654 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 6655 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6656 | break; |
| 6657 | default: |
| 6658 | break; |
| 6659 | } |
| 6660 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 6661 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6662 | } |
| 6663 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6664 | // Intrinsics with no arguments (or no return value, and no precision). |
| 6665 | 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] | 6666 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6667 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 6668 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6669 | |
| 6670 | switch (op) { |
| 6671 | case glslang::EOpEmitVertex: |
| 6672 | builder.createNoResultOp(spv::OpEmitVertex); |
| 6673 | return 0; |
| 6674 | case glslang::EOpEndPrimitive: |
| 6675 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 6676 | return 0; |
| 6677 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6678 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6679 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6680 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 6681 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 6682 | spv::MemorySemanticsAcquireReleaseMask); |
| 6683 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6684 | } else { |
| 6685 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 6686 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6687 | } else { |
| 6688 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 6689 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 6690 | spv::MemorySemanticsAcquireReleaseMask); |
| 6691 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6692 | return 0; |
| 6693 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6694 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 6695 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6696 | return 0; |
| 6697 | case glslang::EOpMemoryBarrierAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6698 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 6699 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6700 | return 0; |
| 6701 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6702 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 6703 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6704 | return 0; |
| 6705 | case glslang::EOpMemoryBarrierImage: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6706 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 6707 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6708 | return 0; |
| 6709 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6710 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 6711 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6712 | return 0; |
| 6713 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6714 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 6715 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6716 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6717 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6718 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6719 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6720 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6721 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6722 | case glslang::EOpDeviceMemoryBarrier: |
| 6723 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 6724 | spv::MemorySemanticsImageMemoryMask | |
| 6725 | spv::MemorySemanticsAcquireReleaseMask); |
| 6726 | return 0; |
| 6727 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 6728 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 6729 | spv::MemorySemanticsImageMemoryMask | |
| 6730 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6731 | return 0; |
| 6732 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6733 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 6734 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6735 | return 0; |
| 6736 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6737 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 6738 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 6739 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6740 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6741 | case glslang::EOpSubgroupBarrier: |
| 6742 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 6743 | spv::MemorySemanticsAcquireReleaseMask); |
| 6744 | return spv::NoResult; |
| 6745 | case glslang::EOpSubgroupMemoryBarrier: |
| 6746 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 6747 | spv::MemorySemanticsAcquireReleaseMask); |
| 6748 | return spv::NoResult; |
| 6749 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 6750 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 6751 | spv::MemorySemanticsAcquireReleaseMask); |
| 6752 | return spv::NoResult; |
| 6753 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 6754 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 6755 | spv::MemorySemanticsAcquireReleaseMask); |
| 6756 | return spv::NoResult; |
| 6757 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 6758 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 6759 | spv::MemorySemanticsAcquireReleaseMask); |
| 6760 | return spv::NoResult; |
| 6761 | case glslang::EOpSubgroupElect: { |
| 6762 | std::vector<spv::Id> operands; |
| 6763 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 6764 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6765 | #ifdef AMD_EXTENSIONS |
| 6766 | case glslang::EOpTime: |
| 6767 | { |
| 6768 | std::vector<spv::Id> args; // Dummy arguments |
| 6769 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 6770 | return builder.setPrecision(id, precision); |
| 6771 | } |
| 6772 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6773 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 6774 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6775 | return 0; |
| 6776 | } |
| 6777 | } |
| 6778 | |
| 6779 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 6780 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 6781 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6782 | spv::Id id; |
| 6783 | if (symbolValues.end() != iter) { |
| 6784 | id = iter->second; |
| 6785 | return id; |
| 6786 | } |
| 6787 | |
| 6788 | // it was not found, create it |
| 6789 | id = createSpvVariable(symbol); |
| 6790 | symbolValues[symbol->getId()] = id; |
| 6791 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 6792 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6793 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 6794 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 6795 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6796 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6797 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6798 | if (symbol->getQualifier().hasIndex()) |
| 6799 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 6800 | if (symbol->getQualifier().hasComponent()) |
| 6801 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 6802 | // atomic counters use this: |
| 6803 | if (symbol->getQualifier().hasOffset()) |
| 6804 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6805 | } |
| 6806 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 6807 | if (symbol->getQualifier().hasLocation()) |
| 6808 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6809 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 6810 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6811 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6812 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6813 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6814 | if (symbol->getQualifier().hasSet()) |
| 6815 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6816 | else if (IsDescriptorResource(symbol->getType())) { |
| 6817 | // default to 0 |
| 6818 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 6819 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6820 | if (symbol->getQualifier().hasBinding()) |
| 6821 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6822 | if (symbol->getQualifier().hasAttachment()) |
| 6823 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6824 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6825 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6826 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6827 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 6828 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6829 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 6830 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 6831 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 6832 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 6833 | } |
| 6834 | if (symbol->getQualifier().hasXfbOffset()) |
| 6835 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6836 | } |
| 6837 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 6838 | if (symbol->getType().isImage()) { |
| 6839 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6840 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 6841 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6842 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 6843 | } |
| 6844 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6845 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 6846 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 6847 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6848 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6849 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 6850 | // nonuniform |
| 6851 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 6852 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 6853 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 6854 | if (builtIn == spv::BuiltInSampleMask) { |
| 6855 | spv::Decoration decoration; |
| 6856 | // GL_NV_sample_mask_override_coverage extension |
| 6857 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6858 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 6859 | else |
| 6860 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6861 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 6862 | if (decoration != spv::DecorationMax) { |
| 6863 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 6864 | } |
| 6865 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6866 | else if (builtIn == spv::BuiltInLayer) { |
| 6867 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 6868 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6869 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6870 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 6871 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 6872 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 6873 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6874 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 6875 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6876 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 6877 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 6878 | } |
| 6879 | } |
| 6880 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 6881 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6882 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6883 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 6884 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 6885 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 6886 | if (symbol->getQualifier().pervertexNV) { |
| 6887 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 6888 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 6889 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 6890 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 6891 | #endif |
| 6892 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6893 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 6894 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 6895 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 6896 | symbol->getType().getQualifier().semanticName); |
| 6897 | } |
| 6898 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6899 | return id; |
| 6900 | } |
| 6901 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6902 | // 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] | 6903 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6904 | // |
| 6905 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 6906 | // |
| 6907 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 6908 | // regular constants, which themselves are trees that createSpvConstant() |
| 6909 | // recursively walks. So, this function walks the "top" of the tree: |
| 6910 | // - emit specialization constant-building instructions for specConstant |
| 6911 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6912 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6913 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 6914 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6915 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6916 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6917 | if (! node.getQualifier().specConstant) { |
| 6918 | // hand off to the non-spec-constant path |
| 6919 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 6920 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6921 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6922 | nextConst, false); |
| 6923 | } |
| 6924 | |
| 6925 | // We now know we have a specialization constant to build |
| 6926 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 6927 | // 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] | 6928 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 6929 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 6930 | std::vector<spv::Id> dimConstId; |
| 6931 | for (int dim = 0; dim < 3; ++dim) { |
| 6932 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 6933 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 6934 | if (specConst) { |
| 6935 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 6936 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 6937 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6938 | } |
| 6939 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 6940 | } |
| 6941 | |
| 6942 | // An AST node labelled as specialization constant should be a symbol node. |
| 6943 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 6944 | if (auto* sn = node.getAsSymbolNode()) { |
| 6945 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 6946 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 6947 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 6948 | // will set the builder into spec constant op instruction generating mode. |
| 6949 | sub_tree->traverse(this); |
| 6950 | return accessChainLoad(sub_tree->getType()); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6951 | } else if (auto* const_union_array = &sn->getConstArray()){ |
| 6952 | int nextConst = 0; |
Endre Oma | ad58d45 | 2017-01-31 21:08:19 +0100 | [diff] [blame] | 6953 | spv::Id id = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
| 6954 | builder.addName(id, sn->getName().c_str()); |
| 6955 | return id; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6956 | } |
| 6957 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6958 | |
| 6959 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 6960 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 6961 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6962 | exit(1); |
| 6963 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6964 | } |
| 6965 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6966 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 6967 | // build the aggregate SPIR-V constant. |
| 6968 | // |
| 6969 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 6970 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 6971 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6972 | 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] | 6973 | { |
| 6974 | // vector of constants for SPIR-V |
| 6975 | std::vector<spv::Id> spvConsts; |
| 6976 | |
| 6977 | // Type is used for struct and array constants |
| 6978 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 6979 | |
| 6980 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 6981 | glslang::TType elementType(glslangType, 0); |
| 6982 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6983 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6984 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 6985 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6986 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6987 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6988 | } else if (glslangType.getStruct()) { |
| 6989 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 6990 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6991 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 6992 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6993 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 6994 | bool zero = nextConst >= consts.size(); |
| 6995 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6996 | case glslang::EbtInt8: |
| 6997 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 6998 | break; |
| 6999 | case glslang::EbtUint8: |
| 7000 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 7001 | break; |
| 7002 | case glslang::EbtInt16: |
| 7003 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 7004 | break; |
| 7005 | case glslang::EbtUint16: |
| 7006 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 7007 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7008 | case glslang::EbtInt: |
| 7009 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 7010 | break; |
| 7011 | case glslang::EbtUint: |
| 7012 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 7013 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7014 | case glslang::EbtInt64: |
| 7015 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 7016 | break; |
| 7017 | case glslang::EbtUint64: |
| 7018 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 7019 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7020 | case glslang::EbtFloat: |
| 7021 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7022 | break; |
| 7023 | case glslang::EbtDouble: |
| 7024 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 7025 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7026 | case glslang::EbtFloat16: |
| 7027 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7028 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7029 | case glslang::EbtBool: |
| 7030 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 7031 | break; |
| 7032 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7033 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7034 | break; |
| 7035 | } |
| 7036 | ++nextConst; |
| 7037 | } |
| 7038 | } else { |
| 7039 | // we have a non-aggregate (scalar) constant |
| 7040 | bool zero = nextConst >= consts.size(); |
| 7041 | spv::Id scalar = 0; |
| 7042 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7043 | case glslang::EbtInt8: |
| 7044 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 7045 | break; |
| 7046 | case glslang::EbtUint8: |
| 7047 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 7048 | break; |
| 7049 | case glslang::EbtInt16: |
| 7050 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 7051 | break; |
| 7052 | case glslang::EbtUint16: |
| 7053 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 7054 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7055 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7056 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7057 | break; |
| 7058 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7059 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7060 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7061 | case glslang::EbtInt64: |
| 7062 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 7063 | break; |
| 7064 | case glslang::EbtUint64: |
| 7065 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7066 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7067 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7068 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7069 | break; |
| 7070 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7071 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7072 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7073 | case glslang::EbtFloat16: |
| 7074 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 7075 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7076 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7077 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7078 | break; |
| 7079 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7080 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7081 | break; |
| 7082 | } |
| 7083 | ++nextConst; |
| 7084 | return scalar; |
| 7085 | } |
| 7086 | |
| 7087 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 7088 | } |
| 7089 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7090 | // Return true if the node is a constant or symbol whose reading has no |
| 7091 | // non-trivial observable cost or effect. |
| 7092 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 7093 | { |
| 7094 | // don't know what this is |
| 7095 | if (node == nullptr) |
| 7096 | return false; |
| 7097 | |
| 7098 | // a constant is safe |
| 7099 | if (node->getAsConstantUnion() != nullptr) |
| 7100 | return true; |
| 7101 | |
| 7102 | // not a symbol means non-trivial |
| 7103 | if (node->getAsSymbolNode() == nullptr) |
| 7104 | return false; |
| 7105 | |
| 7106 | // a symbol, depends on what's being read |
| 7107 | switch (node->getType().getQualifier().storage) { |
| 7108 | case glslang::EvqTemporary: |
| 7109 | case glslang::EvqGlobal: |
| 7110 | case glslang::EvqIn: |
| 7111 | case glslang::EvqInOut: |
| 7112 | case glslang::EvqConst: |
| 7113 | case glslang::EvqConstReadOnly: |
| 7114 | case glslang::EvqUniform: |
| 7115 | return true; |
| 7116 | default: |
| 7117 | return false; |
| 7118 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 7119 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7120 | |
| 7121 | // 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] | 7122 | // 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] | 7123 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7124 | // Return true if trivial. |
| 7125 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 7126 | { |
| 7127 | if (node == nullptr) |
| 7128 | return false; |
| 7129 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 7130 | // count non scalars as trivial, as well as anything coming from HLSL |
| 7131 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 7132 | return true; |
| 7133 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7134 | // symbols and constants are trivial |
| 7135 | if (isTrivialLeaf(node)) |
| 7136 | return true; |
| 7137 | |
| 7138 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 7139 | |
| 7140 | // not a simple operation |
| 7141 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 7142 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 7143 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 7144 | return false; |
| 7145 | |
| 7146 | // not on leaf nodes |
| 7147 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 7148 | return false; |
| 7149 | |
| 7150 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 7151 | return false; |
| 7152 | } |
| 7153 | |
| 7154 | switch (node->getAsOperator()->getOp()) { |
| 7155 | case glslang::EOpLogicalNot: |
| 7156 | case glslang::EOpConvIntToBool: |
| 7157 | case glslang::EOpConvUintToBool: |
| 7158 | case glslang::EOpConvFloatToBool: |
| 7159 | case glslang::EOpConvDoubleToBool: |
| 7160 | case glslang::EOpEqual: |
| 7161 | case glslang::EOpNotEqual: |
| 7162 | case glslang::EOpLessThan: |
| 7163 | case glslang::EOpGreaterThan: |
| 7164 | case glslang::EOpLessThanEqual: |
| 7165 | case glslang::EOpGreaterThanEqual: |
| 7166 | case glslang::EOpIndexDirect: |
| 7167 | case glslang::EOpIndexDirectStruct: |
| 7168 | case glslang::EOpLogicalXor: |
| 7169 | case glslang::EOpAny: |
| 7170 | case glslang::EOpAll: |
| 7171 | return true; |
| 7172 | default: |
| 7173 | return false; |
| 7174 | } |
| 7175 | } |
| 7176 | |
| 7177 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 7178 | // the left side is true (for &&) or false (for ||). |
| 7179 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 7180 | { |
| 7181 | spv::Id boolTypeId = builder.makeBoolType(); |
| 7182 | |
| 7183 | // emit left operand |
| 7184 | builder.clearAccessChain(); |
| 7185 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 7186 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7187 | |
| 7188 | // Operands to accumulate OpPhi operands |
| 7189 | std::vector<spv::Id> phiOperands; |
| 7190 | // accumulate left operand's phi information |
| 7191 | phiOperands.push_back(leftId); |
| 7192 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 7193 | |
| 7194 | // Make the two kinds of operation symmetric with a "!" |
| 7195 | // || => emit "if (! left) result = right" |
| 7196 | // && => emit "if ( left) result = right" |
| 7197 | // |
| 7198 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 7199 | // to 'builder' to have an "else" without an "then" |
| 7200 | if (op == glslang::EOpLogicalOr) |
| 7201 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 7202 | |
| 7203 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 7204 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7205 | |
| 7206 | // emit right operand as the "then" part of the "if" |
| 7207 | builder.clearAccessChain(); |
| 7208 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 7209 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 7210 | |
| 7211 | // accumulate left operand's phi information |
| 7212 | phiOperands.push_back(rightId); |
| 7213 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 7214 | |
| 7215 | // finish the "if" |
| 7216 | ifBuilder.makeEndIf(); |
| 7217 | |
| 7218 | // phi together the two results |
| 7219 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 7220 | } |
| 7221 | |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 7222 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7223 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 7224 | // Import this set if it has not been imported yet. |
| 7225 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 7226 | { |
| 7227 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 7228 | return extBuiltinMap[name]; |
| 7229 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7230 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7231 | spv::Id extBuiltins = builder.import(name); |
| 7232 | extBuiltinMap[name] = extBuiltins; |
| 7233 | return extBuiltins; |
| 7234 | } |
| 7235 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 7236 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7237 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7238 | }; // end anonymous namespace |
| 7239 | |
| 7240 | namespace glslang { |
| 7241 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7242 | void GetSpirvVersion(std::string& version) |
| 7243 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 7244 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 7245 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7246 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7247 | version = buf; |
| 7248 | } |
| 7249 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 7250 | // For low-order part of the generator's magic number. Bump up |
| 7251 | // when there is a change in the style (e.g., if SSA form changes, |
| 7252 | // or a different instruction sequence to do something gets used). |
| 7253 | int GetSpirvGeneratorVersion() |
| 7254 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 7255 | // return 1; // start |
| 7256 | // 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] | 7257 | // 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] | 7258 | // 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] | 7259 | // 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] | 7260 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 7261 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
| 7262 | 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] | 7263 | } |
| 7264 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7265 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7266 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7267 | { |
| 7268 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 7269 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 7270 | if (out.fail()) |
| 7271 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7272 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 7273 | unsigned int word = spirv[i]; |
| 7274 | out.write((const char*)&word, 4); |
| 7275 | } |
| 7276 | out.close(); |
| 7277 | } |
| 7278 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7279 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 7280 | 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] | 7281 | { |
| 7282 | std::ofstream out; |
| 7283 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 7284 | if (out.fail()) |
| 7285 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 7286 | out << "\t// " << |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7287 | GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL << |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 7288 | std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 7289 | if (varName != nullptr) { |
| 7290 | out << "\t #pragma once" << std::endl; |
| 7291 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 7292 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7293 | const int WORDS_PER_LINE = 8; |
| 7294 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 7295 | out << "\t"; |
| 7296 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 7297 | const unsigned int word = spirv[i + j]; |
| 7298 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 7299 | if (i + j + 1 < (int)spirv.size()) { |
| 7300 | out << ","; |
| 7301 | } |
| 7302 | } |
| 7303 | out << std::endl; |
| 7304 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 7305 | if (varName != nullptr) { |
| 7306 | out << "};"; |
| 7307 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 7308 | out.close(); |
| 7309 | } |
| 7310 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7311 | // |
| 7312 | // Set up the glslang traversal |
| 7313 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7314 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7315 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7316 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7317 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 7318 | } |
| 7319 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7320 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7321 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 7322 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7323 | TIntermNode* root = intermediate.getTreeRoot(); |
| 7324 | |
| 7325 | if (root == 0) |
| 7326 | return; |
| 7327 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7328 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 7329 | if (options == nullptr) |
| 7330 | options = &defaultOptions; |
| 7331 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7332 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7333 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 7334 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7335 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 7336 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7337 | it.dumpSpv(spirv); |
| 7338 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 7339 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 7340 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 7341 | // eg. forward and remove memory writes of opaque types. |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7342 | if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer) |
John Kessenich | e7df8e0 | 2018-08-22 17:12:46 -0600 | [diff] [blame] | 7343 | SpirvToolsLegalize(intermediate, spirv, logger, options); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7344 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7345 | if (options->validate) |
| 7346 | SpirvToolsValidate(intermediate, spirv, logger); |
| 7347 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7348 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7349 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 7350 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 7351 | #endif |
| 7352 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 7353 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7354 | } |
| 7355 | |
| 7356 | }; // end namespace glslang |