John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2 | // Copyright (C) 2014-2016 LunarG, Inc. |
John Kessenich | b23d232 | 2018-12-14 10:47:35 -0700 | [diff] [blame] | 3 | // Copyright (C) 2015-2018 Google, Inc. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4 | // Copyright (C) 2017 ARM Limited. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 6 | // All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions |
| 10 | // are met: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 11 | // |
| 12 | // Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // |
| 15 | // Redistributions in binary form must reproduce the above |
| 16 | // copyright notice, this list of conditions and the following |
| 17 | // disclaimer in the documentation and/or other materials provided |
| 18 | // with the distribution. |
| 19 | // |
| 20 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 21 | // contributors may be used to endorse or promote products derived |
| 22 | // from this software without specific prior written permission. |
| 23 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 24 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 34 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 | // POSSIBILITY OF SUCH DAMAGE. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 36 | |
| 37 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 38 | // Visit the nodes in the glslang intermediate tree representation to |
| 39 | // translate them to SPIR-V. |
| 40 | // |
| 41 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 42 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 43 | #include "GlslangToSpv.h" |
| 44 | #include "SpvBuilder.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 45 | namespace spv { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 46 | #include "GLSL.std.450.h" |
| 47 | #include "GLSL.ext.KHR.h" |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 48 | #include "GLSL.ext.EXT.h" |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 49 | #include "GLSL.ext.AMD.h" |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 50 | #include "GLSL.ext.NV.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 51 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 52 | |
| 53 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 54 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 55 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 56 | #include "../glslang/Include/Common.h" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 57 | #include "../glslang/Include/revision.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 58 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 59 | #include <fstream> |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 60 | #include <iomanip> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 61 | #include <list> |
| 62 | #include <map> |
| 63 | #include <stack> |
| 64 | #include <string> |
| 65 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 66 | |
| 67 | namespace { |
| 68 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 69 | namespace { |
| 70 | class SpecConstantOpModeGuard { |
| 71 | public: |
| 72 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 73 | : builder_(builder) { |
| 74 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 75 | } |
| 76 | ~SpecConstantOpModeGuard() { |
| 77 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 78 | : builder_->setToNormalCodeGenMode(); |
| 79 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 80 | void turnOnSpecConstantOpMode() { |
| 81 | builder_->setToSpecConstCodeGenMode(); |
| 82 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | spv::Builder* builder_; |
| 86 | bool previous_flag_; |
| 87 | }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 88 | |
| 89 | struct OpDecorations { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 90 | public: |
| 91 | OpDecorations(spv::Decoration precision, spv::Decoration noContraction, spv::Decoration nonUniform) : |
| 92 | precision(precision) |
| 93 | #ifndef GLSLANG_WEB |
| 94 | , |
| 95 | noContraction(noContraction), |
| 96 | nonUniform(nonUniform) |
| 97 | #endif |
| 98 | { } |
| 99 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 100 | spv::Decoration precision; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 101 | |
| 102 | #ifdef GLSLANG_WEB |
| 103 | void addNoContraction(spv::Builder&, spv::Id) const { }; |
| 104 | void addNonUniform(spv::Builder&, spv::Id) const { }; |
| 105 | #else |
| 106 | void addNoContraction(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, noContraction); }; |
| 107 | void addNonUniform(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, nonUniform); }; |
| 108 | protected: |
| 109 | spv::Decoration noContraction; |
| 110 | spv::Decoration nonUniform; |
| 111 | #endif |
| 112 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 116 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 117 | // |
| 118 | // The main holder of information for translating glslang to SPIR-V. |
| 119 | // |
| 120 | // Derives from the AST walking base class. |
| 121 | // |
| 122 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 123 | public: |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 124 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 125 | glslang::SpvOptions& options); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 126 | virtual ~TGlslangToSpvTraverser() { } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 127 | |
| 128 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 129 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 130 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 131 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 132 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 133 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 134 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 135 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 136 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 137 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 138 | void finishSpv(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 139 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 140 | |
| 141 | protected: |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 142 | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
| 143 | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
| 144 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 145 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 146 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 147 | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 148 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 149 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 150 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 151 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 152 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 153 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 154 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 155 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 156 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 157 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 158 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 159 | spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 160 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 161 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 162 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 163 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 164 | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 165 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 166 | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 167 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 168 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 169 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 170 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 171 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 172 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 173 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 174 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 175 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 176 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 177 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 178 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 179 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 180 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 181 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 182 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 183 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 184 | bool writableParam(glslang::TStorageQualifier) const; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 185 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 186 | void makeFunctions(const glslang::TIntermSequence&); |
| 187 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 188 | void visitFunctions(const glslang::TIntermSequence&); |
| 189 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 190 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 191 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 192 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 193 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 194 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 195 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 196 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 197 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 198 | spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 199 | glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 200 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 201 | glslang::TBasicType typeProxy); |
| 202 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 203 | glslang::TBasicType typeProxy); |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 204 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 205 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 206 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 207 | 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] | 208 | 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] | 209 | 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] | 210 | 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] | 211 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 212 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 213 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 214 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 215 | 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] | 216 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 217 | bool isTrivial(const glslang::TIntermTyped* node); |
| 218 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 219 | spv::Id getExtBuiltins(const char* name); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 220 | std::pair<spv::Id, spv::Id> getForcedType(spv::BuiltIn, const glslang::TType&); |
| 221 | spv::Id translateForcedType(spv::Id object); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 222 | spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 223 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 224 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 225 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 226 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 227 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 228 | int sequenceDepth; |
| 229 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 230 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 231 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 232 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 233 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 234 | bool inEntryPoint; |
| 235 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 236 | 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] | 237 | 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] | 238 | const glslang::TIntermediate* glslangIntermediate; |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 239 | bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 240 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 241 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 242 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 243 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 244 | 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] | 245 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 246 | 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] | 247 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 248 | std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 249 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 250 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 251 | // Map pointee types for EbtReference to their forward pointers |
| 252 | std::map<const glslang::TType *, spv::Id> forwardPointers; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 253 | // Type forcing, for when SPIR-V wants a different type than the AST, |
| 254 | // requiring local translation to and from SPIR-V type on every access. |
| 255 | // Maps <builtin-variable-id -> AST-required-type-id> |
| 256 | std::unordered_map<spv::Id, spv::Id> forceType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | // |
| 260 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 261 | // |
| 262 | |
| 263 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 264 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 265 | { |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 266 | #ifdef GLSLANG_WEB |
| 267 | return spv::SourceLanguageESSL; |
| 268 | #endif |
| 269 | |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 270 | switch (source) { |
| 271 | case glslang::EShSourceGlsl: |
| 272 | switch (profile) { |
| 273 | case ENoProfile: |
| 274 | case ECoreProfile: |
| 275 | case ECompatibilityProfile: |
| 276 | return spv::SourceLanguageGLSL; |
| 277 | case EEsProfile: |
| 278 | return spv::SourceLanguageESSL; |
| 279 | default: |
| 280 | return spv::SourceLanguageUnknown; |
| 281 | } |
| 282 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 283 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 284 | default: |
| 285 | return spv::SourceLanguageUnknown; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Translate glslang language (stage) to SPIR-V execution model. |
| 290 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 291 | { |
| 292 | switch (stage) { |
| 293 | case EShLangVertex: return spv::ExecutionModelVertex; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 294 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 295 | #ifndef GLSLANG_WEB |
| 296 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 297 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 298 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 299 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 300 | case EShLangRayGenNV: return spv::ExecutionModelRayGenerationNV; |
| 301 | case EShLangIntersectNV: return spv::ExecutionModelIntersectionNV; |
| 302 | case EShLangAnyHitNV: return spv::ExecutionModelAnyHitNV; |
| 303 | case EShLangClosestHitNV: return spv::ExecutionModelClosestHitNV; |
| 304 | case EShLangMissNV: return spv::ExecutionModelMissNV; |
| 305 | case EShLangCallableNV: return spv::ExecutionModelCallableNV; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 306 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 307 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 308 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 309 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 310 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 311 | return spv::ExecutionModelFragment; |
| 312 | } |
| 313 | } |
| 314 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 315 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 316 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 317 | { |
| 318 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 319 | case glslang::Esd1D: return spv::Dim1D; |
| 320 | case glslang::Esd2D: return spv::Dim2D; |
| 321 | case glslang::Esd3D: return spv::Dim3D; |
| 322 | case glslang::EsdCube: return spv::DimCube; |
| 323 | case glslang::EsdRect: return spv::DimRect; |
| 324 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 325 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 326 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 327 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 328 | return spv::Dim2D; |
| 329 | } |
| 330 | } |
| 331 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 332 | // Translate glslang precision to SPIR-V precision decorations. |
| 333 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 334 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 335 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 336 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 337 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 338 | default: |
| 339 | return spv::NoPrecision; |
| 340 | } |
| 341 | } |
| 342 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 343 | // Translate glslang type to SPIR-V precision decorations. |
| 344 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 345 | { |
| 346 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 347 | } |
| 348 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 349 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 350 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 351 | { |
| 352 | if (type.getBasicType() == glslang::EbtBlock) { |
| 353 | switch (type.getQualifier().storage) { |
| 354 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 355 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 356 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 357 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 358 | #ifndef GLSLANG_WEB |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 359 | case glslang::EvqPayloadNV: return spv::DecorationBlock; |
| 360 | case glslang::EvqPayloadInNV: return spv::DecorationBlock; |
| 361 | case glslang::EvqHitAttrNV: return spv::DecorationBlock; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 362 | case glslang::EvqCallableDataNV: return spv::DecorationBlock; |
| 363 | case glslang::EvqCallableDataInNV: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 364 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 365 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 366 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 371 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 372 | } |
| 373 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 374 | // Translate glslang type to SPIR-V memory decorations. |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 375 | 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] | 376 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 377 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 378 | if (!useVulkanMemoryModel) { |
| 379 | if (qualifier.coherent) |
| 380 | memory.push_back(spv::DecorationCoherent); |
| 381 | if (qualifier.volatil) { |
| 382 | memory.push_back(spv::DecorationVolatile); |
| 383 | memory.push_back(spv::DecorationCoherent); |
| 384 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 385 | } |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 386 | if (qualifier.restrict) |
| 387 | memory.push_back(spv::DecorationRestrict); |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 388 | if (qualifier.isReadOnly()) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 389 | memory.push_back(spv::DecorationNonWritable); |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 390 | if (qualifier.isWriteOnly()) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 391 | memory.push_back(spv::DecorationNonReadable); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 392 | #endif |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 393 | } |
| 394 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 395 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 396 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 397 | { |
| 398 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 399 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 400 | case glslang::ElmRowMajor: |
| 401 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 402 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 403 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 404 | default: |
| 405 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 406 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 407 | } |
| 408 | } else { |
| 409 | switch (type.getBasicType()) { |
| 410 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 411 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 412 | break; |
| 413 | case glslang::EbtBlock: |
| 414 | switch (type.getQualifier().storage) { |
| 415 | case glslang::EvqUniform: |
| 416 | case glslang::EvqBuffer: |
| 417 | switch (type.getQualifier().layoutPacking) { |
| 418 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 419 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 420 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 421 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 422 | } |
| 423 | case glslang::EvqVaryingIn: |
| 424 | case glslang::EvqVaryingOut: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 425 | if (type.getQualifier().isTaskMemory()) { |
| 426 | switch (type.getQualifier().layoutPacking) { |
| 427 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 428 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 429 | default: break; |
| 430 | } |
| 431 | } else { |
| 432 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 433 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 434 | return spv::DecorationMax; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 435 | #ifndef GLSLANG_WEB |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 436 | case glslang::EvqPayloadNV: |
| 437 | case glslang::EvqPayloadInNV: |
| 438 | case glslang::EvqHitAttrNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 439 | case glslang::EvqCallableDataNV: |
| 440 | case glslang::EvqCallableDataInNV: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 441 | return spv::DecorationMax; |
| 442 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 443 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 444 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 445 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 452 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 453 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 454 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 455 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 456 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 457 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 458 | return spv::DecorationMax; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 459 | else if (qualifier.isNonPerspective()) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 460 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 461 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 462 | return spv::DecorationFlat; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 463 | else if (qualifier.isExplicitInterpolation()) { |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 464 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 465 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 466 | } |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 467 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 468 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 472 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 473 | // should be applied. |
| 474 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 475 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 476 | if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 477 | return spv::DecorationCentroid; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 478 | #ifndef GLSLANG_WEB |
| 479 | else if (qualifier.patch) |
| 480 | return spv::DecorationPatch; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 481 | else if (qualifier.sample) { |
| 482 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 483 | return spv::DecorationSample; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 484 | } |
| 485 | #endif |
| 486 | |
| 487 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 488 | } |
| 489 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 490 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 491 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 492 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 493 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 494 | return spv::DecorationInvariant; |
| 495 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 496 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 497 | } |
| 498 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 499 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 500 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 501 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 502 | #ifndef GLSLANG_WEB |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 503 | if (qualifier.isNoContraction()) |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 504 | return spv::DecorationNoContraction; |
| 505 | else |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 506 | #endif |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 507 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 508 | } |
| 509 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 510 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 511 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 512 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 513 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 514 | if (qualifier.isNonUniform()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 515 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 516 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 517 | return spv::DecorationNonUniformEXT; |
| 518 | } else |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 519 | #endif |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 520 | return spv::DecorationMax; |
| 521 | } |
| 522 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 523 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess( |
| 524 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 525 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 526 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 527 | |
| 528 | #ifndef GLSLANG_WEB |
| 529 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) |
| 530 | return mask; |
| 531 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 532 | if (coherentFlags.volatil || |
| 533 | coherentFlags.coherent || |
| 534 | coherentFlags.devicecoherent || |
| 535 | coherentFlags.queuefamilycoherent || |
| 536 | coherentFlags.workgroupcoherent || |
| 537 | coherentFlags.subgroupcoherent) { |
| 538 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 539 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 540 | } |
| 541 | if (coherentFlags.nonprivate) { |
| 542 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 543 | } |
| 544 | if (coherentFlags.volatil) { |
| 545 | mask = mask | spv::MemoryAccessVolatileMask; |
| 546 | } |
| 547 | if (mask != spv::MemoryAccessMaskNone) { |
| 548 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 549 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 550 | #endif |
| 551 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 552 | return mask; |
| 553 | } |
| 554 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 555 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands( |
| 556 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 557 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 558 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 559 | |
| 560 | #ifndef GLSLANG_WEB |
| 561 | if (!glslangIntermediate->usingVulkanMemoryModel()) |
| 562 | return mask; |
| 563 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 564 | if (coherentFlags.volatil || |
| 565 | coherentFlags.coherent || |
| 566 | coherentFlags.devicecoherent || |
| 567 | coherentFlags.queuefamilycoherent || |
| 568 | coherentFlags.workgroupcoherent || |
| 569 | coherentFlags.subgroupcoherent) { |
| 570 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 571 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 572 | } |
| 573 | if (coherentFlags.nonprivate) { |
| 574 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 575 | } |
| 576 | if (coherentFlags.volatil) { |
| 577 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 578 | } |
| 579 | if (mask != spv::ImageOperandsMaskNone) { |
| 580 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 581 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 582 | #endif |
| 583 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 584 | return mask; |
| 585 | } |
| 586 | |
| 587 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 588 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 589 | spv::Builder::AccessChain::CoherentFlags flags = {}; |
| 590 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 591 | flags.coherent = type.getQualifier().coherent; |
| 592 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 593 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 594 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 595 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 596 | type.getQualifier().storage == glslang::EvqShared; |
| 597 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 598 | flags.volatil = type.getQualifier().volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 599 | // *coherent variables are implicitly nonprivate in GLSL |
| 600 | flags.nonprivate = type.getQualifier().nonprivate || |
Jeff Bolz | ab3c965 | 2018-10-15 22:46:48 -0500 | [diff] [blame] | 601 | flags.subgroupcoherent || |
| 602 | flags.workgroupcoherent || |
| 603 | flags.queuefamilycoherent || |
| 604 | flags.devicecoherent || |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 605 | flags.coherent || |
| 606 | flags.volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 607 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 608 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 609 | return flags; |
| 610 | } |
| 611 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 612 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope( |
| 613 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 614 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 615 | spv::Scope scope = spv::ScopeMax; |
| 616 | |
| 617 | #ifndef GLSLANG_WEB |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 618 | if (coherentFlags.volatil || coherentFlags.coherent) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 619 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 620 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 621 | } else if (coherentFlags.devicecoherent) { |
| 622 | scope = spv::ScopeDevice; |
| 623 | } else if (coherentFlags.queuefamilycoherent) { |
| 624 | scope = spv::ScopeQueueFamilyKHR; |
| 625 | } else if (coherentFlags.workgroupcoherent) { |
| 626 | scope = spv::ScopeWorkgroup; |
| 627 | } else if (coherentFlags.subgroupcoherent) { |
| 628 | scope = spv::ScopeSubgroup; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 629 | } |
| 630 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 631 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 632 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 633 | #endif |
| 634 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 635 | return scope; |
| 636 | } |
| 637 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 638 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 639 | // associated capabilities when required. For some built-in variables, a capability |
| 640 | // is generated only when using the variable in an executable instruction, but not when |
| 641 | // just declaring a struct member variable with it. This is true for PointSize, |
| 642 | // ClipDistance, and CullDistance. |
| 643 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 644 | { |
| 645 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 646 | case glslang::EbvPointSize: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 647 | #ifndef GLSLANG_WEB |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 648 | // Defer adding the capability until the built-in is actually used. |
| 649 | if (! memberDeclaration) { |
| 650 | switch (glslangIntermediate->getStage()) { |
| 651 | case EShLangGeometry: |
| 652 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 653 | break; |
| 654 | case EShLangTessControl: |
| 655 | case EShLangTessEvaluation: |
| 656 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 657 | break; |
| 658 | default: |
| 659 | break; |
| 660 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 661 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 662 | #endif |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 663 | return spv::BuiltInPointSize; |
| 664 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 665 | case glslang::EbvPosition: return spv::BuiltInPosition; |
| 666 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 667 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
| 668 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 669 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
| 670 | |
| 671 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 672 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 673 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
| 674 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 675 | |
| 676 | #ifndef GLSLANG_WEB |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 677 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 678 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 679 | // They are now generated when used, rather than here when declared. |
| 680 | // Potentially, the specification should be more clear what the minimum |
| 681 | // use needed is to trigger the capability. |
| 682 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 683 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 684 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 685 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 686 | return spv::BuiltInClipDistance; |
| 687 | |
| 688 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 689 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 690 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 691 | return spv::BuiltInCullDistance; |
| 692 | |
| 693 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 694 | builder.addCapability(spv::CapabilityMultiViewport); |
| 695 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 696 | glslangIntermediate->getStage() == EShLangTessControl || |
| 697 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 698 | |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 699 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 700 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 701 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 702 | return spv::BuiltInViewportIndex; |
| 703 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 704 | case glslang::EbvSampleId: |
| 705 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 706 | return spv::BuiltInSampleId; |
| 707 | |
| 708 | case glslang::EbvSamplePosition: |
| 709 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 710 | return spv::BuiltInSamplePosition; |
| 711 | |
| 712 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 713 | return spv::BuiltInSampleMask; |
| 714 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 715 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 716 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 717 | return spv::BuiltInLayer; |
| 718 | } |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 719 | builder.addCapability(spv::CapabilityGeometry); |
| 720 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 721 | glslangIntermediate->getStage() == EShLangTessControl || |
| 722 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 723 | |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 724 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 725 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 726 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 727 | return spv::BuiltInLayer; |
| 728 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 729 | case glslang::EbvBaseVertex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 730 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 731 | builder.addCapability(spv::CapabilityDrawParameters); |
| 732 | return spv::BuiltInBaseVertex; |
| 733 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 734 | case glslang::EbvBaseInstance: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 735 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 736 | builder.addCapability(spv::CapabilityDrawParameters); |
| 737 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 738 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 739 | case glslang::EbvDrawId: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 740 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 741 | builder.addCapability(spv::CapabilityDrawParameters); |
| 742 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 743 | |
| 744 | case glslang::EbvPrimitiveId: |
| 745 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 746 | builder.addCapability(spv::CapabilityGeometry); |
| 747 | return spv::BuiltInPrimitiveId; |
| 748 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 749 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 750 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 751 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 752 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 753 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 754 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 755 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 756 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 757 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 758 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 759 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 760 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 761 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 762 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 763 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 764 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 765 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 766 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 767 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 768 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 769 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 770 | return spv::BuiltInSubgroupSize; |
| 771 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 772 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 773 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 774 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 775 | return spv::BuiltInSubgroupLocalInvocationId; |
| 776 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 777 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 778 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 779 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 780 | return spv::BuiltInSubgroupEqMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 781 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 782 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 783 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 784 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 785 | return spv::BuiltInSubgroupGeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 786 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 787 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 788 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 789 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 790 | return spv::BuiltInSubgroupGtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 791 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 792 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 793 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 794 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 795 | return spv::BuiltInSubgroupLeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 796 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 797 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 798 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 799 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 800 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 801 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 802 | case glslang::EbvNumSubgroups: |
| 803 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 804 | return spv::BuiltInNumSubgroups; |
| 805 | |
| 806 | case glslang::EbvSubgroupID: |
| 807 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 808 | return spv::BuiltInSubgroupId; |
| 809 | |
| 810 | case glslang::EbvSubgroupSize2: |
| 811 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 812 | return spv::BuiltInSubgroupSize; |
| 813 | |
| 814 | case glslang::EbvSubgroupInvocation2: |
| 815 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 816 | return spv::BuiltInSubgroupLocalInvocationId; |
| 817 | |
| 818 | case glslang::EbvSubgroupEqMask2: |
| 819 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 820 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 821 | return spv::BuiltInSubgroupEqMask; |
| 822 | |
| 823 | case glslang::EbvSubgroupGeMask2: |
| 824 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 825 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 826 | return spv::BuiltInSubgroupGeMask; |
| 827 | |
| 828 | case glslang::EbvSubgroupGtMask2: |
| 829 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 830 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 831 | return spv::BuiltInSubgroupGtMask; |
| 832 | |
| 833 | case glslang::EbvSubgroupLeMask2: |
| 834 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 835 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 836 | return spv::BuiltInSubgroupLeMask; |
| 837 | |
| 838 | case glslang::EbvSubgroupLtMask2: |
| 839 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 840 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 841 | return spv::BuiltInSubgroupLtMask; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 842 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 843 | case glslang::EbvBaryCoordNoPersp: |
| 844 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 845 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 846 | |
| 847 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 848 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 849 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 850 | |
| 851 | case glslang::EbvBaryCoordNoPerspSample: |
| 852 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 853 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 854 | |
| 855 | case glslang::EbvBaryCoordSmooth: |
| 856 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 857 | return spv::BuiltInBaryCoordSmoothAMD; |
| 858 | |
| 859 | case glslang::EbvBaryCoordSmoothCentroid: |
| 860 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 861 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 862 | |
| 863 | case glslang::EbvBaryCoordSmoothSample: |
| 864 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 865 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 866 | |
| 867 | case glslang::EbvBaryCoordPullModel: |
| 868 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 869 | return spv::BuiltInBaryCoordPullModelAMD; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 870 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 871 | case glslang::EbvDeviceIndex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 872 | builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 873 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 874 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 875 | |
| 876 | case glslang::EbvViewIndex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 877 | builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 878 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 879 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 880 | |
Daniel Koch | 5154db5 | 2018-11-26 10:01:58 -0500 | [diff] [blame] | 881 | case glslang::EbvFragSizeEXT: |
| 882 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 883 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 884 | return spv::BuiltInFragSizeEXT; |
| 885 | |
| 886 | case glslang::EbvFragInvocationCountEXT: |
| 887 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 888 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 889 | return spv::BuiltInFragInvocationCountEXT; |
| 890 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 891 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 892 | if (!memberDeclaration) { |
| 893 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 894 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 895 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 896 | return spv::BuiltInViewportMaskNV; |
| 897 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 898 | if (!memberDeclaration) { |
| 899 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 900 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 901 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 902 | return spv::BuiltInSecondaryPositionNV; |
| 903 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 904 | if (!memberDeclaration) { |
| 905 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 906 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 907 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 908 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 909 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 910 | if (!memberDeclaration) { |
| 911 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 912 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 913 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 914 | return spv::BuiltInPositionPerViewNV; |
| 915 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 916 | if (!memberDeclaration) { |
| 917 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 918 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 919 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 920 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 921 | case glslang::EbvFragFullyCoveredNV: |
| 922 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 923 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 924 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 925 | case glslang::EbvFragmentSizeNV: |
| 926 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 927 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 928 | return spv::BuiltInFragmentSizeNV; |
| 929 | case glslang::EbvInvocationsPerPixelNV: |
| 930 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 931 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 932 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 933 | |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 934 | // ray tracing |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 935 | case glslang::EbvLaunchIdNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 936 | return spv::BuiltInLaunchIdNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 937 | case glslang::EbvLaunchSizeNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 938 | return spv::BuiltInLaunchSizeNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 939 | case glslang::EbvWorldRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 940 | return spv::BuiltInWorldRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 941 | case glslang::EbvWorldRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 942 | return spv::BuiltInWorldRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 943 | case glslang::EbvObjectRayOriginNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 944 | return spv::BuiltInObjectRayOriginNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 945 | case glslang::EbvObjectRayDirectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 946 | return spv::BuiltInObjectRayDirectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 947 | case glslang::EbvRayTminNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 948 | return spv::BuiltInRayTminNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 949 | case glslang::EbvRayTmaxNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 950 | return spv::BuiltInRayTmaxNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 951 | case glslang::EbvInstanceCustomIndexNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 952 | return spv::BuiltInInstanceCustomIndexNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 953 | case glslang::EbvHitTNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 954 | return spv::BuiltInHitTNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 955 | case glslang::EbvHitKindNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 956 | return spv::BuiltInHitKindNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 957 | case glslang::EbvObjectToWorldNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 958 | return spv::BuiltInObjectToWorldNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 959 | case glslang::EbvWorldToObjectNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 960 | return spv::BuiltInWorldToObjectNV; |
| 961 | case glslang::EbvIncomingRayFlagsNV: |
| 962 | return spv::BuiltInIncomingRayFlagsNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 963 | |
| 964 | // barycentrics |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 965 | case glslang::EbvBaryCoordNV: |
| 966 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 967 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 968 | return spv::BuiltInBaryCoordNV; |
| 969 | case glslang::EbvBaryCoordNoPerspNV: |
| 970 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 971 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 972 | return spv::BuiltInBaryCoordNoPerspNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 973 | |
| 974 | // mesh shaders |
| 975 | case glslang::EbvTaskCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 976 | return spv::BuiltInTaskCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 977 | case glslang::EbvPrimitiveCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 978 | return spv::BuiltInPrimitiveCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 979 | case glslang::EbvPrimitiveIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 980 | return spv::BuiltInPrimitiveIndicesNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 981 | case glslang::EbvClipDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 982 | return spv::BuiltInClipDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 983 | case glslang::EbvCullDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 984 | return spv::BuiltInCullDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 985 | case glslang::EbvLayerPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 986 | return spv::BuiltInLayerPerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 987 | case glslang::EbvMeshViewCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 988 | return spv::BuiltInMeshViewCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 989 | case glslang::EbvMeshViewIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 990 | return spv::BuiltInMeshViewIndicesNV; |
Daniel Koch | 2cb2f19 | 2019-06-04 08:43:32 -0400 | [diff] [blame] | 991 | |
| 992 | // sm builtins |
| 993 | case glslang::EbvWarpsPerSM: |
| 994 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 995 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 996 | return spv::BuiltInWarpsPerSMNV; |
| 997 | case glslang::EbvSMCount: |
| 998 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 999 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1000 | return spv::BuiltInSMCountNV; |
| 1001 | case glslang::EbvWarpID: |
| 1002 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1003 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1004 | return spv::BuiltInWarpIDNV; |
| 1005 | case glslang::EbvSMID: |
| 1006 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1007 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1008 | return spv::BuiltInSMIDNV; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1009 | #endif |
| 1010 | |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 1011 | default: |
| 1012 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1016 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1017 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1018 | { |
| 1019 | assert(type.getBasicType() == glslang::EbtSampler); |
| 1020 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1021 | #ifdef GLSLANG_WEB |
| 1022 | return spv::ImageFormatUnknown; |
| 1023 | #endif |
| 1024 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1025 | // Check for capabilities |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1026 | switch (type.getQualifier().getFormat()) { |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1027 | case glslang::ElfRg32f: |
| 1028 | case glslang::ElfRg16f: |
| 1029 | case glslang::ElfR11fG11fB10f: |
| 1030 | case glslang::ElfR16f: |
| 1031 | case glslang::ElfRgba16: |
| 1032 | case glslang::ElfRgb10A2: |
| 1033 | case glslang::ElfRg16: |
| 1034 | case glslang::ElfRg8: |
| 1035 | case glslang::ElfR16: |
| 1036 | case glslang::ElfR8: |
| 1037 | case glslang::ElfRgba16Snorm: |
| 1038 | case glslang::ElfRg16Snorm: |
| 1039 | case glslang::ElfRg8Snorm: |
| 1040 | case glslang::ElfR16Snorm: |
| 1041 | case glslang::ElfR8Snorm: |
| 1042 | |
| 1043 | case glslang::ElfRg32i: |
| 1044 | case glslang::ElfRg16i: |
| 1045 | case glslang::ElfRg8i: |
| 1046 | case glslang::ElfR16i: |
| 1047 | case glslang::ElfR8i: |
| 1048 | |
| 1049 | case glslang::ElfRgb10a2ui: |
| 1050 | case glslang::ElfRg32ui: |
| 1051 | case glslang::ElfRg16ui: |
| 1052 | case glslang::ElfRg8ui: |
| 1053 | case glslang::ElfR16ui: |
| 1054 | case glslang::ElfR8ui: |
| 1055 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 1056 | break; |
| 1057 | |
| 1058 | default: |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | // do the translation |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1063 | switch (type.getQualifier().getFormat()) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1064 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1065 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1066 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1067 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1068 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1069 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1070 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1071 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1072 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1073 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1074 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1075 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1076 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1077 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1078 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1079 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1080 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1081 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1082 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1083 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1084 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1085 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1086 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1087 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1088 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1089 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1090 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1091 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1092 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1093 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1094 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1095 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1096 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1097 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1098 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1099 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1100 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1101 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1102 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1103 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1104 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1108 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1109 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1110 | if (selectionNode.getFlatten()) |
| 1111 | return spv::SelectionControlFlattenMask; |
| 1112 | if (selectionNode.getDontFlatten()) |
| 1113 | return spv::SelectionControlDontFlattenMask; |
| 1114 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1115 | } |
| 1116 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1117 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1118 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1119 | if (switchNode.getFlatten()) |
| 1120 | return spv::SelectionControlFlattenMask; |
| 1121 | if (switchNode.getDontFlatten()) |
| 1122 | return spv::SelectionControlDontFlattenMask; |
| 1123 | return spv::SelectionControlMaskNone; |
| 1124 | } |
| 1125 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1126 | // return a non-0 dependency if the dependency argument must be set |
| 1127 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1128 | std::vector<unsigned int>& operands) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1129 | { |
| 1130 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1131 | |
| 1132 | if (loopNode.getDontUnroll()) |
| 1133 | control = control | spv::LoopControlDontUnrollMask; |
| 1134 | if (loopNode.getUnroll()) |
| 1135 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1136 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1137 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1138 | else if (loopNode.getLoopDependency() > 0) { |
| 1139 | control = control | spv::LoopControlDependencyLengthMask; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1140 | operands.push_back((unsigned int)loopNode.getLoopDependency()); |
| 1141 | } |
| 1142 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 1143 | if (loopNode.getMinIterations() > 0) { |
| 1144 | control = control | spv::LoopControlMinIterationsMask; |
| 1145 | operands.push_back(loopNode.getMinIterations()); |
| 1146 | } |
| 1147 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
| 1148 | control = control | spv::LoopControlMaxIterationsMask; |
| 1149 | operands.push_back(loopNode.getMaxIterations()); |
| 1150 | } |
| 1151 | if (loopNode.getIterationMultiple() > 1) { |
| 1152 | control = control | spv::LoopControlIterationMultipleMask; |
| 1153 | operands.push_back(loopNode.getIterationMultiple()); |
| 1154 | } |
| 1155 | if (loopNode.getPeelCount() > 0) { |
| 1156 | control = control | spv::LoopControlPeelCountMask; |
| 1157 | operands.push_back(loopNode.getPeelCount()); |
| 1158 | } |
| 1159 | if (loopNode.getPartialCount() > 0) { |
| 1160 | control = control | spv::LoopControlPartialCountMask; |
| 1161 | operands.push_back(loopNode.getPartialCount()); |
| 1162 | } |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1163 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1164 | |
| 1165 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1166 | } |
| 1167 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1168 | // Translate glslang type to SPIR-V storage class. |
| 1169 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1170 | { |
| 1171 | if (type.getQualifier().isPipeInput()) |
| 1172 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1173 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1174 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1175 | |
| 1176 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1177 | type.getQualifier().storage == glslang::EvqUniform) { |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1178 | if (type.isAtomic()) |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1179 | return spv::StorageClassAtomicCounter; |
| 1180 | if (type.containsOpaque()) |
| 1181 | return spv::StorageClassUniformConstant; |
| 1182 | } |
| 1183 | |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1184 | if (type.getQualifier().isUniformOrBuffer() && |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1185 | type.getQualifier().isShaderRecordNV()) { |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1186 | return spv::StorageClassShaderRecordBufferNV; |
| 1187 | } |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1188 | |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1189 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1190 | builder.addIncorporatedExtension(spv::E_SPV_KHR_storage_buffer_storage_class, spv::Spv_1_3); |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1191 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1195 | if (type.getQualifier().isPushConstant()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1196 | return spv::StorageClassPushConstant; |
| 1197 | if (type.getBasicType() == glslang::EbtBlock) |
| 1198 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1199 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1200 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1201 | |
| 1202 | switch (type.getQualifier().storage) { |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1203 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1204 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1205 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1206 | #ifndef GLSLANG_WEB |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1207 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1208 | case glslang::EvqPayloadNV: return spv::StorageClassRayPayloadNV; |
| 1209 | case glslang::EvqPayloadInNV: return spv::StorageClassIncomingRayPayloadNV; |
| 1210 | case glslang::EvqHitAttrNV: return spv::StorageClassHitAttributeNV; |
| 1211 | case glslang::EvqCallableDataNV: return spv::StorageClassCallableDataNV; |
| 1212 | case glslang::EvqCallableDataInNV: return spv::StorageClassIncomingCallableDataNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1213 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1214 | default: |
| 1215 | assert(0); |
| 1216 | break; |
| 1217 | } |
| 1218 | |
| 1219 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1220 | } |
| 1221 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1222 | // Add capabilities pertaining to how an array is indexed. |
| 1223 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1224 | const glslang::TType& indexType) |
| 1225 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1226 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1227 | if (indexType.getQualifier().isNonUniform()) { |
| 1228 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1229 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1230 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1231 | if (baseType.getQualifier().hasAttachment()) |
| 1232 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1233 | else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1234 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1235 | else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1236 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1237 | else if (baseType.isImage()) |
| 1238 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1239 | else if (baseType.isTexture()) |
| 1240 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1241 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1242 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1243 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1244 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1245 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1246 | } |
| 1247 | } else { |
| 1248 | // assume a dynamically uniform index |
| 1249 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1250 | if (baseType.getQualifier().hasAttachment()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1251 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1252 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1253 | } else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1254 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1255 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1256 | } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1257 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1258 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1259 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1260 | } |
| 1261 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1262 | #endif |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1263 | } |
| 1264 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1265 | // 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] | 1266 | // descriptor set. |
| 1267 | bool IsDescriptorResource(const glslang::TType& type) |
| 1268 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1269 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1270 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1271 | return type.getQualifier().isUniformOrBuffer() && |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1272 | ! type.getQualifier().isShaderRecordNV() && |
| 1273 | ! type.getQualifier().isPushConstant(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1274 | |
| 1275 | // non block... |
| 1276 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1277 | // if they are the global-scope-class, not the function parameter |
| 1278 | // (or local, if they ever exist) class. |
| 1279 | if (type.getBasicType() == glslang::EbtSampler) |
| 1280 | return type.getQualifier().isUniformOrBuffer(); |
| 1281 | |
| 1282 | // None of the above. |
| 1283 | return false; |
| 1284 | } |
| 1285 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1286 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1287 | { |
| 1288 | if (child.layoutMatrix == glslang::ElmNone) |
| 1289 | child.layoutMatrix = parent.layoutMatrix; |
| 1290 | |
| 1291 | if (parent.invariant) |
| 1292 | child.invariant = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1293 | if (parent.flat) |
| 1294 | child.flat = true; |
| 1295 | if (parent.centroid) |
| 1296 | child.centroid = true; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1297 | #ifndef GLSLANG_WEB |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1298 | if (parent.nopersp) |
| 1299 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1300 | if (parent.explicitInterp) |
| 1301 | child.explicitInterp = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1302 | if (parent.perPrimitiveNV) |
| 1303 | child.perPrimitiveNV = true; |
| 1304 | if (parent.perViewNV) |
| 1305 | child.perViewNV = true; |
| 1306 | if (parent.perTaskNV) |
| 1307 | child.perTaskNV = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1308 | if (parent.patch) |
| 1309 | child.patch = true; |
| 1310 | if (parent.sample) |
| 1311 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1312 | if (parent.coherent) |
| 1313 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1314 | if (parent.devicecoherent) |
| 1315 | child.devicecoherent = true; |
| 1316 | if (parent.queuefamilycoherent) |
| 1317 | child.queuefamilycoherent = true; |
| 1318 | if (parent.workgroupcoherent) |
| 1319 | child.workgroupcoherent = true; |
| 1320 | if (parent.subgroupcoherent) |
| 1321 | child.subgroupcoherent = true; |
| 1322 | if (parent.nonprivate) |
| 1323 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1324 | if (parent.volatil) |
| 1325 | child.volatil = true; |
| 1326 | if (parent.restrict) |
| 1327 | child.restrict = true; |
| 1328 | if (parent.readonly) |
| 1329 | child.readonly = true; |
| 1330 | if (parent.writeonly) |
| 1331 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1332 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1335 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1336 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1337 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1338 | // - struct members might inherit from a struct declaration |
| 1339 | // (note that non-block structs don't explicitly inherit, |
| 1340 | // only implicitly, meaning no decoration involved) |
| 1341 | // - affect decorations on the struct members |
| 1342 | // (note smooth does not, and expecting something like volatile |
| 1343 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1344 | // - 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] | 1345 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1348 | // |
| 1349 | // Implement the TGlslangToSpvTraverser class. |
| 1350 | // |
| 1351 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1352 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1353 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 1354 | : TIntermTraverser(true, false, true), |
| 1355 | options(options), |
| 1356 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1357 | sequenceDepth(0), logger(buildLogger), |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 1358 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1359 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 1360 | glslangIntermediate(glslangIntermediate), |
| 1361 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1362 | { |
| 1363 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1364 | |
| 1365 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1366 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1367 | glslangIntermediate->getVersion()); |
| 1368 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1369 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1370 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1371 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1372 | |
| 1373 | // Set the source shader's text. If for SPV version 1.0, include |
| 1374 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1375 | // Otherwise, emit those as actual instructions. |
| 1376 | std::string text; |
| 1377 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1378 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1379 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1380 | text.append("// OpModuleProcessed "); |
| 1381 | text.append(processes[p]); |
| 1382 | text.append("\n"); |
| 1383 | } else |
| 1384 | builder.addModuleProcessed(processes[p]); |
| 1385 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1386 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1387 | text.append("#line 1\n"); |
| 1388 | text.append(glslangIntermediate->getSourceText()); |
| 1389 | builder.setSourceText(text); |
Greg Fischer | d445bb2 | 2018-12-06 11:13:15 -0700 | [diff] [blame] | 1390 | // Pass name and text for all included files |
| 1391 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1392 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1393 | builder.addInclude(iItr->first, iItr->second); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1394 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1395 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1396 | |
| 1397 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1398 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1399 | |
| 1400 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1401 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
John Kessenich | 1ff0c18 | 2019-10-10 12:01:13 -0600 | [diff] [blame] | 1402 | builder.addIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, spv::Spv_1_5); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1403 | builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
| 1404 | }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1405 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1406 | memoryModel = spv::MemoryModelVulkanKHR; |
| 1407 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1408 | builder.addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1409 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1410 | builder.setMemoryModel(addressingModel, memoryModel); |
| 1411 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 1412 | if (glslangIntermediate->usingVariablePointers()) { |
| 1413 | builder.addCapability(spv::CapabilityVariablePointers); |
| 1414 | } |
| 1415 | |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1416 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1417 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1418 | |
| 1419 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1420 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1421 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1422 | builder.addSourceExtension(it->c_str()); |
| 1423 | |
| 1424 | // Add the top-level modes for this shader. |
| 1425 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1426 | if (glslangIntermediate->getXfbMode()) { |
| 1427 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1428 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1429 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1430 | |
| 1431 | unsigned int mode; |
| 1432 | switch (glslangIntermediate->getStage()) { |
| 1433 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1434 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1435 | break; |
| 1436 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1437 | case EShLangFragment: |
| 1438 | builder.addCapability(spv::CapabilityShader); |
| 1439 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1440 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
| 1441 | |
| 1442 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1443 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
| 1444 | else |
| 1445 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
| 1446 | |
| 1447 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1448 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1449 | |
| 1450 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1451 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1452 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1453 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1454 | } |
| 1455 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1456 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1457 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
| 1458 | |
| 1459 | #ifndef GLSLANG_WEB |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1460 | switch(glslangIntermediate->getDepth()) { |
| 1461 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1462 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
| 1463 | default: mode = spv::ExecutionModeMax; break; |
| 1464 | } |
| 1465 | if (mode != spv::ExecutionModeMax) |
| 1466 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1467 | switch (glslangIntermediate->getInterlockOrdering()) { |
| 1468 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; break; |
| 1469 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; break; |
| 1470 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; break; |
| 1471 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; break; |
| 1472 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; break; |
| 1473 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; break; |
| 1474 | default: mode = spv::ExecutionModeMax; break; |
| 1475 | } |
| 1476 | if (mode != spv::ExecutionModeMax) { |
| 1477 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1478 | if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
| 1479 | mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
| 1480 | builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
| 1481 | } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
| 1482 | mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
| 1483 | builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT); |
| 1484 | } else { |
| 1485 | builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT); |
| 1486 | } |
| 1487 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 1488 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1489 | #endif |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1490 | break; |
| 1491 | |
| 1492 | #ifndef GLSLANG_WEB |
| 1493 | case EShLangCompute: |
| 1494 | builder.addCapability(spv::CapabilityShader); |
| 1495 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1496 | glslangIntermediate->getLocalSize(1), |
| 1497 | glslangIntermediate->getLocalSize(2)); |
| 1498 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1499 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1500 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1501 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1502 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1503 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1504 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1505 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1506 | } |
| 1507 | break; |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1508 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1509 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1510 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1511 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1512 | glslang::TLayoutGeometry primitive; |
| 1513 | |
| 1514 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1515 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1516 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1517 | } else { |
| 1518 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1519 | } |
| 1520 | |
| 1521 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1522 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1523 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1524 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1525 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1526 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1527 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1528 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1529 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1530 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1531 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1532 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1533 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1534 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1535 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1536 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1537 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1538 | |
| 1539 | switch (glslangIntermediate->getVertexOrder()) { |
| 1540 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1541 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1542 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1543 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1544 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1545 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1546 | |
| 1547 | if (glslangIntermediate->getPointMode()) |
| 1548 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1549 | break; |
| 1550 | |
| 1551 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1552 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1553 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1554 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1555 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1556 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1557 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1558 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1559 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1560 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1561 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1562 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1563 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1564 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1565 | |
| 1566 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1567 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1568 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1569 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1570 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1571 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1572 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1573 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1574 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1575 | break; |
| 1576 | |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1577 | case EShLangRayGenNV: |
| 1578 | case EShLangIntersectNV: |
| 1579 | case EShLangAnyHitNV: |
| 1580 | case EShLangClosestHitNV: |
| 1581 | case EShLangMissNV: |
| 1582 | case EShLangCallableNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 1583 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1584 | builder.addExtension("SPV_NV_ray_tracing"); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1585 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1586 | case EShLangTaskNV: |
| 1587 | case EShLangMeshNV: |
| 1588 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1589 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1590 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1591 | glslangIntermediate->getLocalSize(1), |
| 1592 | glslangIntermediate->getLocalSize(2)); |
| 1593 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 1594 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1595 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, glslangIntermediate->getPrimitives()); |
| 1596 | |
| 1597 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1598 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1599 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1600 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1601 | default: mode = spv::ExecutionModeMax; break; |
| 1602 | } |
| 1603 | if (mode != spv::ExecutionModeMax) |
| 1604 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1605 | } |
| 1606 | break; |
| 1607 | #endif |
| 1608 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1609 | default: |
| 1610 | break; |
| 1611 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1612 | } |
| 1613 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1614 | // Finish creating SPV, after the traversal is complete. |
| 1615 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1616 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1617 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1618 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1619 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1620 | builder.leaveFunction(); |
| 1621 | } |
| 1622 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1623 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1624 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1625 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1626 | |
John Kessenich | 23d2775 | 2019-07-28 02:12:10 -0600 | [diff] [blame] | 1627 | #ifndef GLSLANG_WEB |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1628 | // Add capabilities, extensions, remove unneeded decorations, etc., |
| 1629 | // based on the resulting SPIR-V. |
| 1630 | builder.postProcess(); |
John Kessenich | 23d2775 | 2019-07-28 02:12:10 -0600 | [diff] [blame] | 1631 | #endif |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1634 | // Write the SPV into 'out'. |
| 1635 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1636 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1637 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | // |
| 1641 | // Implement the traversal functions. |
| 1642 | // |
| 1643 | // Return true from interior nodes to have the external traversal |
| 1644 | // continue on to children. Return false if children were |
| 1645 | // already processed. |
| 1646 | // |
| 1647 | |
| 1648 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1649 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1650 | // - uniform/input reads |
| 1651 | // - output writes |
| 1652 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1653 | // - something simple that degenerates into the last bullet |
| 1654 | // |
| 1655 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1656 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1657 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1658 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1659 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1660 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1661 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1662 | // Formal function parameters were mapped during makeFunctions(). |
| 1663 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1664 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1665 | if (builder.isPointer(id)) { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1666 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
John Kessenich | 7c7731e | 2019-01-04 16:47:06 +0700 | [diff] [blame] | 1667 | // Consider adding to the OpEntryPoint interface list. |
| 1668 | // Only looking at structures if they have at least one member. |
| 1669 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
| 1670 | spv::StorageClass sc = builder.getStorageClass(id); |
| 1671 | // Before SPIR-V 1.4, we only want to include Input and Output. |
| 1672 | // Starting with SPIR-V 1.4, we want all globals. |
| 1673 | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && sc != spv::StorageClassFunction) || |
| 1674 | (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1675 | iOSet.insert(id); |
John Kessenich | 7c7731e | 2019-01-04 16:47:06 +0700 | [diff] [blame] | 1676 | } |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1677 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1678 | |
| 1679 | // If the SPIR-V type is required to be different than the AST type, |
| 1680 | // translate now from the SPIR-V type to the AST type, for the consuming |
| 1681 | // operation. |
| 1682 | // Note this turns it from an l-value to an r-value. |
| 1683 | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
| 1684 | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
| 1685 | id = translateForcedType(id); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1689 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1690 | // Prepare to generate code for the access |
| 1691 | |
| 1692 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1693 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1694 | // followed by setting the base. |
| 1695 | builder.clearAccessChain(); |
| 1696 | |
| 1697 | // 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] | 1698 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1699 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1700 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1701 | // 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] | 1702 | // These are also pure R-values. |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1703 | // C) R-Values from type translation, see above call to translateForcedType() |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1704 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1705 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
| 1706 | !builder.isPointerType(builder.getTypeId(id))) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1707 | builder.setAccessChainRValue(id); |
| 1708 | else |
| 1709 | builder.setAccessChainLValue(id); |
| 1710 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1711 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1712 | #ifdef ENABLE_HLSL |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1713 | // Process linkage-only nodes for any special additional interface work. |
| 1714 | if (linkageOnly) { |
| 1715 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1716 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1717 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1718 | // of declaration. |
| 1719 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1720 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1721 | // Save possible originating buffers for counter buffers, keyed by |
| 1722 | // making the potential counter-buffer name. |
| 1723 | std::string keyName = symbol->getName().c_str(); |
| 1724 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1725 | counterOriginator[keyName] = symbol; |
| 1726 | } else { |
| 1727 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1728 | std::string keyName = symbol->getName().c_str(); |
| 1729 | auto it = counterOriginator.find(keyName); |
| 1730 | if (it != counterOriginator.end()) { |
| 1731 | id = getSymbolId(it->second); |
| 1732 | if (id != spv::NoResult) { |
| 1733 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1734 | if (counterId != spv::NoResult) { |
| 1735 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1736 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1737 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 1744 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1748 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1749 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1750 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1751 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1752 | if (node->getType().getQualifier().isSpecConstant()) |
| 1753 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1754 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1755 | // First, handle special cases |
| 1756 | switch (node->getOp()) { |
| 1757 | case glslang::EOpAssign: |
| 1758 | case glslang::EOpAddAssign: |
| 1759 | case glslang::EOpSubAssign: |
| 1760 | case glslang::EOpMulAssign: |
| 1761 | case glslang::EOpVectorTimesMatrixAssign: |
| 1762 | case glslang::EOpVectorTimesScalarAssign: |
| 1763 | case glslang::EOpMatrixTimesScalarAssign: |
| 1764 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1765 | case glslang::EOpDivAssign: |
| 1766 | case glslang::EOpModAssign: |
| 1767 | case glslang::EOpAndAssign: |
| 1768 | case glslang::EOpInclusiveOrAssign: |
| 1769 | case glslang::EOpExclusiveOrAssign: |
| 1770 | case glslang::EOpLeftShiftAssign: |
| 1771 | case glslang::EOpRightShiftAssign: |
| 1772 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1773 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1774 | // says to evaluate the left before the right. So, always, left |
| 1775 | // node then right node. |
| 1776 | { |
| 1777 | // get the left l-value, save it away |
| 1778 | builder.clearAccessChain(); |
| 1779 | node->getLeft()->traverse(this); |
| 1780 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1781 | |
| 1782 | // evaluate the right |
| 1783 | builder.clearAccessChain(); |
| 1784 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1785 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1786 | |
| 1787 | if (node->getOp() != glslang::EOpAssign) { |
| 1788 | // the left is also an r-value |
| 1789 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1790 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1791 | |
| 1792 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1793 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1794 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1795 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1796 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1797 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1798 | node->getType().getBasicType()); |
| 1799 | |
| 1800 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1801 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | // store the result |
| 1805 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1806 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1807 | |
| 1808 | // assignments are expressions having an rValue after they are evaluated... |
| 1809 | builder.clearAccessChain(); |
| 1810 | builder.setAccessChainRValue(rValue); |
| 1811 | } |
| 1812 | return false; |
| 1813 | case glslang::EOpIndexDirect: |
| 1814 | case glslang::EOpIndexDirectStruct: |
| 1815 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1816 | // Structure, array, matrix, or vector indirection with statically known index. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1817 | // Get the left part of the access chain. |
| 1818 | node->getLeft()->traverse(this); |
| 1819 | |
| 1820 | // Add the next element in the chain |
| 1821 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1822 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1823 | if (! node->getLeft()->getType().isArray() && |
| 1824 | node->getLeft()->getType().isVector() && |
| 1825 | node->getOp() == glslang::EOpIndexDirect) { |
| 1826 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1827 | // so short circuit the access-chain stuff with a swizzle. |
| 1828 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1829 | swizzle.push_back(glslangIndex); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1830 | int dummySize; |
| 1831 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1832 | TranslateCoherent(node->getLeft()->getType()), |
| 1833 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1834 | } else { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1835 | |
| 1836 | // Load through a block reference is performed with a dot operator that |
| 1837 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 1838 | // do a load and reset the access chain. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1839 | if (node->getLeft()->isReference() && |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1840 | !node->getLeft()->getType().isArray() && |
| 1841 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1842 | { |
| 1843 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 1844 | builder.clearAccessChain(); |
| 1845 | builder.setAccessChainLValue(left); |
| 1846 | } |
| 1847 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1848 | int spvIndex = glslangIndex; |
| 1849 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1850 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1851 | { |
| 1852 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1853 | // index remapping due to hidden members in anonymous blocks. |
| 1854 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1855 | assert(remapper.size() > 0); |
| 1856 | spvIndex = remapper[glslangIndex]; |
| 1857 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1858 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1859 | // normal case for indexing array or structure or block |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 1860 | builder.accessChainPush(builder.makeIntConstant(spvIndex), TranslateCoherent(node->getLeft()->getType()), node->getLeft()->getType().getBufferReferenceAlignment()); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1861 | |
| 1862 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1863 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1864 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1865 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1866 | } |
| 1867 | } |
| 1868 | return false; |
| 1869 | case glslang::EOpIndexIndirect: |
| 1870 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1871 | // Array, matrix, or vector indirection with variable index. |
| 1872 | // Will use native SPIR-V access-chain for and array indirection; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1873 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1874 | // Will use the access chain's 'component' for variable index into a vector. |
| 1875 | |
| 1876 | // This adapter is building access chains left to right. |
| 1877 | // Set up the access chain to the left. |
| 1878 | node->getLeft()->traverse(this); |
| 1879 | |
| 1880 | // save it so that computing the right side doesn't trash it |
| 1881 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1882 | |
| 1883 | // compute the next index in the chain |
| 1884 | builder.clearAccessChain(); |
| 1885 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1886 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1887 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1888 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1889 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1890 | // restore the saved access chain |
| 1891 | builder.setAccessChain(partial); |
| 1892 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1893 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 1894 | int dummySize; |
| 1895 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1896 | TranslateCoherent(node->getLeft()->getType()), |
| 1897 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
| 1898 | } else |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 1899 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), node->getLeft()->getType().getBufferReferenceAlignment()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1900 | } |
| 1901 | return false; |
| 1902 | case glslang::EOpVectorSwizzle: |
| 1903 | { |
| 1904 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1905 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1906 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1907 | int dummySize; |
| 1908 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1909 | TranslateCoherent(node->getLeft()->getType()), |
| 1910 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1911 | } |
| 1912 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1913 | case glslang::EOpMatrixSwizzle: |
| 1914 | logger->missingFunctionality("matrix swizzle"); |
| 1915 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1916 | case glslang::EOpLogicalOr: |
| 1917 | case glslang::EOpLogicalAnd: |
| 1918 | { |
| 1919 | |
| 1920 | // These may require short circuiting, but can sometimes be done as straight |
| 1921 | // binary operations. The right operand must be short circuited if it has |
| 1922 | // side effects, and should probably be if it is complex. |
| 1923 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1924 | break; // handle below as a normal binary operation |
| 1925 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1926 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1927 | builder.clearAccessChain(); |
| 1928 | builder.setAccessChainRValue(result); |
| 1929 | } |
| 1930 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1931 | default: |
| 1932 | break; |
| 1933 | } |
| 1934 | |
| 1935 | // Assume generic binary op... |
| 1936 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1937 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1938 | builder.clearAccessChain(); |
| 1939 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1940 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1941 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1942 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1943 | builder.clearAccessChain(); |
| 1944 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1945 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1946 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1947 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1948 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1949 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1950 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1951 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1952 | convertGlslangToSpvType(node->getType()), left, right, |
| 1953 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1954 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1955 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1956 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1957 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1958 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1959 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1960 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1961 | return false; |
| 1962 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1963 | } |
| 1964 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1965 | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
| 1966 | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
| 1967 | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
| 1968 | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(spv::BuiltIn builtIn, |
| 1969 | const glslang::TType& glslangType) |
| 1970 | { |
| 1971 | switch(builtIn) |
| 1972 | { |
| 1973 | case spv::BuiltInSubgroupEqMask: |
| 1974 | case spv::BuiltInSubgroupGeMask: |
| 1975 | case spv::BuiltInSubgroupGtMask: |
| 1976 | case spv::BuiltInSubgroupLeMask: |
| 1977 | case spv::BuiltInSubgroupLtMask: { |
| 1978 | // these require changing a 64-bit scaler -> a vector of 32-bit components |
| 1979 | if (glslangType.isVector()) |
| 1980 | break; |
| 1981 | std::pair<spv::Id, spv::Id> ret(builder.makeVectorType(builder.makeUintType(32), 4), |
| 1982 | builder.makeUintType(64)); |
| 1983 | return ret; |
| 1984 | } |
| 1985 | default: |
| 1986 | break; |
| 1987 | } |
| 1988 | |
| 1989 | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
| 1990 | return ret; |
| 1991 | } |
| 1992 | |
| 1993 | // For an object previously identified (see getForcedType() and forceType) |
| 1994 | // as needing type translations, do the translation needed for a load, turning |
| 1995 | // an L-value into in R-value. |
| 1996 | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
| 1997 | { |
| 1998 | const auto forceIt = forceType.find(object); |
| 1999 | if (forceIt == forceType.end()) |
| 2000 | return object; |
| 2001 | |
| 2002 | spv::Id desiredTypeId = forceIt->second; |
| 2003 | spv::Id objectTypeId = builder.getTypeId(object); |
| 2004 | assert(builder.isPointerType(objectTypeId)); |
| 2005 | objectTypeId = builder.getContainedTypeId(objectTypeId); |
| 2006 | if (builder.isVectorType(objectTypeId) && |
| 2007 | builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
| 2008 | if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
| 2009 | // handle 32-bit v.xy* -> 64-bit |
| 2010 | builder.clearAccessChain(); |
| 2011 | builder.setAccessChainLValue(object); |
| 2012 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 2013 | std::vector<spv::Id> components; |
| 2014 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
| 2015 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
| 2016 | |
| 2017 | spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
| 2018 | return builder.createUnaryOp(spv::OpBitcast, desiredTypeId, |
| 2019 | builder.createCompositeConstruct(vecType, components)); |
| 2020 | } else { |
| 2021 | logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar"); |
| 2022 | } |
| 2023 | } else { |
| 2024 | logger->missingFunctionality("forcing non 32-bit vector type"); |
| 2025 | } |
| 2026 | |
| 2027 | return object; |
| 2028 | } |
| 2029 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2030 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 2031 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2032 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2033 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 2034 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2035 | if (node->getType().getQualifier().isSpecConstant()) |
| 2036 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2037 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2038 | spv::Id result = spv::NoResult; |
| 2039 | |
| 2040 | // try texturing first |
| 2041 | result = createImageTextureFunctionCall(node); |
| 2042 | if (result != spv::NoResult) { |
| 2043 | builder.clearAccessChain(); |
| 2044 | builder.setAccessChainRValue(result); |
| 2045 | |
| 2046 | return false; // done with this node |
| 2047 | } |
| 2048 | |
| 2049 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2050 | |
| 2051 | if (node->getOp() == glslang::EOpArrayLength) { |
| 2052 | // Quite special; won't want to evaluate the operand. |
| 2053 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2054 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 2055 | // except for the last block membeor of an SSBO. |
| 2056 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 2057 | // size extracted. |
| 2058 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2059 | // Normal .length() would have been constant folded by the front-end. |
| 2060 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 2061 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2062 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2063 | spv::Id length; |
| 2064 | if (node->getOperand()->getType().isCoopMat()) { |
| 2065 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2066 | |
| 2067 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
| 2068 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2069 | |
| 2070 | length = builder.createCooperativeMatrixLength(typeId); |
| 2071 | } else { |
| 2072 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2073 | block->traverse(this); |
| 2074 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 2075 | length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
| 2076 | } |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2077 | |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2078 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 2079 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 2080 | // AST expectation of a signed result. |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2081 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
| 2082 | if (builder.isInSpecConstCodeGenMode()) { |
| 2083 | length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0)); |
| 2084 | } else { |
| 2085 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 2086 | } |
| 2087 | } |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2088 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2089 | builder.clearAccessChain(); |
| 2090 | builder.setAccessChainRValue(length); |
| 2091 | |
| 2092 | return false; |
| 2093 | } |
| 2094 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2095 | // Start by evaluating the operand |
| 2096 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2097 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2098 | // operate first on the swizzle base, then apply the swizzle. |
| 2099 | spv::Id invertedType = spv::NoType; |
| 2100 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 2101 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 2102 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 2103 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2104 | builder.clearAccessChain(); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2105 | TIntermNode *operandNode; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2106 | if (invertedType != spv::NoType) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2107 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2108 | else |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2109 | operandNode = node->getOperand(); |
| 2110 | |
| 2111 | operandNode->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 2112 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2113 | spv::Id operand = spv::NoResult; |
| 2114 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2115 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2116 | |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2117 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2118 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 2119 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2120 | node->getOp() == glslang::EOpAtomicCounter || |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2121 | node->getOp() == glslang::EOpInterpolateAtCentroid) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2122 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2123 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2124 | lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
| 2125 | } else |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2126 | #endif |
| 2127 | { |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2128 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2129 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2130 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2131 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2132 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2133 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2134 | |
| 2135 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2136 | if (! result) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2137 | result = createConversion(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2138 | |
| 2139 | // if not, then possibly an operation |
| 2140 | if (! result) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2141 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, node->getOperand()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2142 | |
| 2143 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2144 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2145 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2146 | decorations.addNonUniform(builder, result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2147 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2148 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2149 | builder.clearAccessChain(); |
| 2150 | builder.setAccessChainRValue(result); |
| 2151 | |
| 2152 | return false; // done with this node |
| 2153 | } |
| 2154 | |
| 2155 | // it must be a special case, check... |
| 2156 | switch (node->getOp()) { |
| 2157 | case glslang::EOpPostIncrement: |
| 2158 | case glslang::EOpPostDecrement: |
| 2159 | case glslang::EOpPreIncrement: |
| 2160 | case glslang::EOpPreDecrement: |
| 2161 | { |
| 2162 | // 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] | 2163 | spv::Id one = 0; |
| 2164 | if (node->getBasicType() == glslang::EbtFloat) |
| 2165 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2166 | #ifndef GLSLANG_WEB |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 2167 | else if (node->getBasicType() == glslang::EbtDouble) |
| 2168 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2169 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 2170 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2171 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 2172 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2173 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 2174 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2175 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 2176 | one = builder.makeInt64Constant(1); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2177 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2178 | else |
| 2179 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2180 | glslang::TOperator op; |
| 2181 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2182 | node->getOp() == glslang::EOpPostIncrement) |
| 2183 | op = glslang::EOpAdd; |
| 2184 | else |
| 2185 | op = glslang::EOpSub; |
| 2186 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2187 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2188 | convertGlslangToSpvType(node->getType()), operand, one, |
| 2189 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2190 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2191 | |
| 2192 | // The result of operation is always stored, but conditionally the |
| 2193 | // consumed result. The consumed result is always an r-value. |
| 2194 | builder.accessChainStore(result); |
| 2195 | builder.clearAccessChain(); |
| 2196 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2197 | node->getOp() == glslang::EOpPreDecrement) |
| 2198 | builder.setAccessChainRValue(result); |
| 2199 | else |
| 2200 | builder.setAccessChainRValue(operand); |
| 2201 | } |
| 2202 | |
| 2203 | return false; |
| 2204 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2205 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2206 | case glslang::EOpEmitStreamVertex: |
| 2207 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 2208 | return false; |
| 2209 | case glslang::EOpEndStreamPrimitive: |
| 2210 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 2211 | return false; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2212 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2213 | |
| 2214 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2215 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2216 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2217 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2218 | } |
| 2219 | |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2220 | // Construct a composite object, recursively copying members if their types don't match |
| 2221 | spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
| 2222 | { |
| 2223 | for (int c = 0; c < (int)constituents.size(); ++c) { |
| 2224 | spv::Id& constituent = constituents[c]; |
| 2225 | spv::Id lType = builder.getContainedTypeId(resultTypeId, c); |
| 2226 | spv::Id rType = builder.getTypeId(constituent); |
| 2227 | if (lType != rType) { |
| 2228 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 2229 | constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent); |
| 2230 | } else if (builder.isStructType(rType)) { |
| 2231 | std::vector<spv::Id> rTypeConstituents; |
| 2232 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2233 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2234 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, builder.getContainedTypeId(rType, i), i)); |
| 2235 | } |
| 2236 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2237 | } else { |
| 2238 | assert(builder.isArrayType(rType)); |
| 2239 | std::vector<spv::Id> rTypeConstituents; |
| 2240 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2241 | |
| 2242 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2243 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2244 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i)); |
| 2245 | } |
| 2246 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2247 | } |
| 2248 | } |
| 2249 | } |
| 2250 | return builder.createCompositeConstruct(resultTypeId, constituents); |
| 2251 | } |
| 2252 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2253 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2254 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 2255 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2256 | if (node->getType().getQualifier().isSpecConstant()) |
| 2257 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2258 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2259 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2260 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2261 | 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] | 2262 | |
| 2263 | // try texturing |
| 2264 | result = createImageTextureFunctionCall(node); |
| 2265 | if (result != spv::NoResult) { |
| 2266 | builder.clearAccessChain(); |
| 2267 | builder.setAccessChainRValue(result); |
| 2268 | |
| 2269 | return false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2270 | } |
| 2271 | #ifndef GLSLANG_WEB |
| 2272 | else if (node->getOp() == glslang::EOpImageStore || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2273 | node->getOp() == glslang::EOpImageStoreLod || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2274 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2275 | // "imageStore" is a special case, which has no result |
| 2276 | return false; |
| 2277 | } |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2278 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2279 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2280 | glslang::TOperator binOp = glslang::EOpNull; |
| 2281 | bool reduceComparison = true; |
| 2282 | bool isMatrix = false; |
| 2283 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2284 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2285 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2286 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2287 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2288 | assert(node->getOp()); |
| 2289 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 2290 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2291 | |
| 2292 | switch (node->getOp()) { |
| 2293 | case glslang::EOpSequence: |
| 2294 | { |
| 2295 | if (preVisit) |
| 2296 | ++sequenceDepth; |
| 2297 | else |
| 2298 | --sequenceDepth; |
| 2299 | |
| 2300 | if (sequenceDepth == 1) { |
| 2301 | // If this is the parent node of all the functions, we want to see them |
| 2302 | // early, so all call points have actual SPIR-V functions to reference. |
| 2303 | // In all cases, still let the traverser visit the children for us. |
| 2304 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 2305 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2306 | // 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] | 2307 | // anything else gets there, so visit out of order, doing them all now. |
| 2308 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2309 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2310 | // 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] | 2311 | // so do them manually. |
| 2312 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2313 | |
| 2314 | return false; |
| 2315 | } |
| 2316 | |
| 2317 | return true; |
| 2318 | } |
| 2319 | case glslang::EOpLinkerObjects: |
| 2320 | { |
| 2321 | if (visit == glslang::EvPreVisit) |
| 2322 | linkageOnly = true; |
| 2323 | else |
| 2324 | linkageOnly = false; |
| 2325 | |
| 2326 | return true; |
| 2327 | } |
| 2328 | case glslang::EOpComma: |
| 2329 | { |
| 2330 | // processing from left to right naturally leaves the right-most |
| 2331 | // lying around in the access chain |
| 2332 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2333 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2334 | glslangOperands[i]->traverse(this); |
| 2335 | |
| 2336 | return false; |
| 2337 | } |
| 2338 | case glslang::EOpFunction: |
| 2339 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2340 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2341 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2342 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2343 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2344 | } else { |
| 2345 | handleFunctionEntry(node); |
| 2346 | } |
| 2347 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2348 | if (inEntryPoint) |
| 2349 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2350 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2351 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | return true; |
| 2355 | case glslang::EOpParameters: |
| 2356 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2357 | // the body, so we still visited the function node's children, making this |
| 2358 | // child redundant. |
| 2359 | return false; |
| 2360 | case glslang::EOpFunctionCall: |
| 2361 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2362 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2363 | if (node->isUserDefined()) |
| 2364 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2365 | // 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] | 2366 | if (result) { |
| 2367 | builder.clearAccessChain(); |
| 2368 | builder.setAccessChainRValue(result); |
| 2369 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2370 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2371 | |
| 2372 | return false; |
| 2373 | } |
| 2374 | case glslang::EOpConstructMat2x2: |
| 2375 | case glslang::EOpConstructMat2x3: |
| 2376 | case glslang::EOpConstructMat2x4: |
| 2377 | case glslang::EOpConstructMat3x2: |
| 2378 | case glslang::EOpConstructMat3x3: |
| 2379 | case glslang::EOpConstructMat3x4: |
| 2380 | case glslang::EOpConstructMat4x2: |
| 2381 | case glslang::EOpConstructMat4x3: |
| 2382 | case glslang::EOpConstructMat4x4: |
| 2383 | case glslang::EOpConstructDMat2x2: |
| 2384 | case glslang::EOpConstructDMat2x3: |
| 2385 | case glslang::EOpConstructDMat2x4: |
| 2386 | case glslang::EOpConstructDMat3x2: |
| 2387 | case glslang::EOpConstructDMat3x3: |
| 2388 | case glslang::EOpConstructDMat3x4: |
| 2389 | case glslang::EOpConstructDMat4x2: |
| 2390 | case glslang::EOpConstructDMat4x3: |
| 2391 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2392 | case glslang::EOpConstructIMat2x2: |
| 2393 | case glslang::EOpConstructIMat2x3: |
| 2394 | case glslang::EOpConstructIMat2x4: |
| 2395 | case glslang::EOpConstructIMat3x2: |
| 2396 | case glslang::EOpConstructIMat3x3: |
| 2397 | case glslang::EOpConstructIMat3x4: |
| 2398 | case glslang::EOpConstructIMat4x2: |
| 2399 | case glslang::EOpConstructIMat4x3: |
| 2400 | case glslang::EOpConstructIMat4x4: |
| 2401 | case glslang::EOpConstructUMat2x2: |
| 2402 | case glslang::EOpConstructUMat2x3: |
| 2403 | case glslang::EOpConstructUMat2x4: |
| 2404 | case glslang::EOpConstructUMat3x2: |
| 2405 | case glslang::EOpConstructUMat3x3: |
| 2406 | case glslang::EOpConstructUMat3x4: |
| 2407 | case glslang::EOpConstructUMat4x2: |
| 2408 | case glslang::EOpConstructUMat4x3: |
| 2409 | case glslang::EOpConstructUMat4x4: |
| 2410 | case glslang::EOpConstructBMat2x2: |
| 2411 | case glslang::EOpConstructBMat2x3: |
| 2412 | case glslang::EOpConstructBMat2x4: |
| 2413 | case glslang::EOpConstructBMat3x2: |
| 2414 | case glslang::EOpConstructBMat3x3: |
| 2415 | case glslang::EOpConstructBMat3x4: |
| 2416 | case glslang::EOpConstructBMat4x2: |
| 2417 | case glslang::EOpConstructBMat4x3: |
| 2418 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2419 | case glslang::EOpConstructF16Mat2x2: |
| 2420 | case glslang::EOpConstructF16Mat2x3: |
| 2421 | case glslang::EOpConstructF16Mat2x4: |
| 2422 | case glslang::EOpConstructF16Mat3x2: |
| 2423 | case glslang::EOpConstructF16Mat3x3: |
| 2424 | case glslang::EOpConstructF16Mat3x4: |
| 2425 | case glslang::EOpConstructF16Mat4x2: |
| 2426 | case glslang::EOpConstructF16Mat4x3: |
| 2427 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2428 | isMatrix = true; |
| 2429 | // fall through |
| 2430 | case glslang::EOpConstructFloat: |
| 2431 | case glslang::EOpConstructVec2: |
| 2432 | case glslang::EOpConstructVec3: |
| 2433 | case glslang::EOpConstructVec4: |
| 2434 | case glslang::EOpConstructDouble: |
| 2435 | case glslang::EOpConstructDVec2: |
| 2436 | case glslang::EOpConstructDVec3: |
| 2437 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2438 | case glslang::EOpConstructFloat16: |
| 2439 | case glslang::EOpConstructF16Vec2: |
| 2440 | case glslang::EOpConstructF16Vec3: |
| 2441 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2442 | case glslang::EOpConstructBool: |
| 2443 | case glslang::EOpConstructBVec2: |
| 2444 | case glslang::EOpConstructBVec3: |
| 2445 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2446 | case glslang::EOpConstructInt8: |
| 2447 | case glslang::EOpConstructI8Vec2: |
| 2448 | case glslang::EOpConstructI8Vec3: |
| 2449 | case glslang::EOpConstructI8Vec4: |
| 2450 | case glslang::EOpConstructUint8: |
| 2451 | case glslang::EOpConstructU8Vec2: |
| 2452 | case glslang::EOpConstructU8Vec3: |
| 2453 | case glslang::EOpConstructU8Vec4: |
| 2454 | case glslang::EOpConstructInt16: |
| 2455 | case glslang::EOpConstructI16Vec2: |
| 2456 | case glslang::EOpConstructI16Vec3: |
| 2457 | case glslang::EOpConstructI16Vec4: |
| 2458 | case glslang::EOpConstructUint16: |
| 2459 | case glslang::EOpConstructU16Vec2: |
| 2460 | case glslang::EOpConstructU16Vec3: |
| 2461 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2462 | case glslang::EOpConstructInt: |
| 2463 | case glslang::EOpConstructIVec2: |
| 2464 | case glslang::EOpConstructIVec3: |
| 2465 | case glslang::EOpConstructIVec4: |
| 2466 | case glslang::EOpConstructUint: |
| 2467 | case glslang::EOpConstructUVec2: |
| 2468 | case glslang::EOpConstructUVec3: |
| 2469 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2470 | case glslang::EOpConstructInt64: |
| 2471 | case glslang::EOpConstructI64Vec2: |
| 2472 | case glslang::EOpConstructI64Vec3: |
| 2473 | case glslang::EOpConstructI64Vec4: |
| 2474 | case glslang::EOpConstructUint64: |
| 2475 | case glslang::EOpConstructU64Vec2: |
| 2476 | case glslang::EOpConstructU64Vec3: |
| 2477 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2478 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2479 | case glslang::EOpConstructTextureSampler: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2480 | case glslang::EOpConstructReference: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2481 | case glslang::EOpConstructCooperativeMatrix: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2482 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2483 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2484 | std::vector<spv::Id> arguments; |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2485 | translateArguments(*node, arguments, lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2486 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2487 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2488 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2489 | else if (node->getOp() == glslang::EOpConstructStruct || |
| 2490 | node->getOp() == glslang::EOpConstructCooperativeMatrix || |
| 2491 | node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2492 | std::vector<spv::Id> constituents; |
| 2493 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2494 | constituents.push_back(arguments[c]); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2495 | constructed = createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2496 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2497 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2498 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2499 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2500 | |
| 2501 | builder.clearAccessChain(); |
| 2502 | builder.setAccessChainRValue(constructed); |
| 2503 | |
| 2504 | return false; |
| 2505 | } |
| 2506 | |
| 2507 | // These six are component-wise compares with component-wise results. |
| 2508 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2509 | case glslang::EOpLessThan: |
| 2510 | case glslang::EOpGreaterThan: |
| 2511 | case glslang::EOpLessThanEqual: |
| 2512 | case glslang::EOpGreaterThanEqual: |
| 2513 | case glslang::EOpVectorEqual: |
| 2514 | case glslang::EOpVectorNotEqual: |
| 2515 | { |
| 2516 | // Map the operation to a binary |
| 2517 | binOp = node->getOp(); |
| 2518 | reduceComparison = false; |
| 2519 | switch (node->getOp()) { |
| 2520 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2521 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2522 | default: binOp = node->getOp(); break; |
| 2523 | } |
| 2524 | |
| 2525 | break; |
| 2526 | } |
| 2527 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2528 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2529 | binOp = glslang::EOpMul; |
| 2530 | break; |
| 2531 | case glslang::EOpOuterProduct: |
| 2532 | // two vectors multiplied to make a matrix |
| 2533 | binOp = glslang::EOpOuterProduct; |
| 2534 | break; |
| 2535 | case glslang::EOpDot: |
| 2536 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2537 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2538 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2539 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2540 | binOp = glslang::EOpMul; |
| 2541 | break; |
| 2542 | } |
| 2543 | case glslang::EOpMod: |
| 2544 | // when an aggregate, this is the floating-point mod built-in function, |
| 2545 | // which can be emitted by the one in createBinaryOperation() |
| 2546 | binOp = glslang::EOpMod; |
| 2547 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2548 | |
| 2549 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2550 | case glslang::EOpEmitVertex: |
| 2551 | case glslang::EOpEndPrimitive: |
| 2552 | case glslang::EOpBarrier: |
| 2553 | case glslang::EOpMemoryBarrier: |
| 2554 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2555 | case glslang::EOpMemoryBarrierBuffer: |
| 2556 | case glslang::EOpMemoryBarrierImage: |
| 2557 | case glslang::EOpMemoryBarrierShared: |
| 2558 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2559 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2560 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2561 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2562 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2563 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2564 | case glslang::EOpSubgroupBarrier: |
| 2565 | case glslang::EOpSubgroupMemoryBarrier: |
| 2566 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2567 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2568 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2569 | noReturnValue = true; |
| 2570 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2571 | break; |
| 2572 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2573 | case glslang::EOpAtomicStore: |
| 2574 | noReturnValue = true; |
| 2575 | // fallthrough |
| 2576 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2577 | case glslang::EOpAtomicAdd: |
| 2578 | case glslang::EOpAtomicMin: |
| 2579 | case glslang::EOpAtomicMax: |
| 2580 | case glslang::EOpAtomicAnd: |
| 2581 | case glslang::EOpAtomicOr: |
| 2582 | case glslang::EOpAtomicXor: |
| 2583 | case glslang::EOpAtomicExchange: |
| 2584 | case glslang::EOpAtomicCompSwap: |
| 2585 | atomic = true; |
| 2586 | break; |
| 2587 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2588 | case glslang::EOpAtomicCounterAdd: |
| 2589 | case glslang::EOpAtomicCounterSubtract: |
| 2590 | case glslang::EOpAtomicCounterMin: |
| 2591 | case glslang::EOpAtomicCounterMax: |
| 2592 | case glslang::EOpAtomicCounterAnd: |
| 2593 | case glslang::EOpAtomicCounterOr: |
| 2594 | case glslang::EOpAtomicCounterXor: |
| 2595 | case glslang::EOpAtomicCounterExchange: |
| 2596 | case glslang::EOpAtomicCounterCompSwap: |
| 2597 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2598 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2599 | atomic = true; |
| 2600 | break; |
| 2601 | |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 2602 | case glslang::EOpIgnoreIntersectionNV: |
| 2603 | case glslang::EOpTerminateRayNV: |
| 2604 | case glslang::EOpTraceNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 2605 | case glslang::EOpExecuteCallableNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2606 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2607 | noReturnValue = true; |
| 2608 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2609 | case glslang::EOpCooperativeMatrixLoad: |
| 2610 | case glslang::EOpCooperativeMatrixStore: |
| 2611 | noReturnValue = true; |
| 2612 | break; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 2613 | case glslang::EOpBeginInvocationInterlock: |
| 2614 | case glslang::EOpEndInvocationInterlock: |
| 2615 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 2616 | noReturnValue = true; |
| 2617 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2618 | #endif |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2619 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2620 | default: |
| 2621 | break; |
| 2622 | } |
| 2623 | |
| 2624 | // |
| 2625 | // See if it maps to a regular operation. |
| 2626 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2627 | if (binOp != glslang::EOpNull) { |
| 2628 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2629 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2630 | assert(left && right); |
| 2631 | |
| 2632 | builder.clearAccessChain(); |
| 2633 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2634 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2635 | |
| 2636 | builder.clearAccessChain(); |
| 2637 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2638 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2639 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2640 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2641 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2642 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2643 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2644 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2645 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2646 | left->getType().getBasicType(), reduceComparison); |
| 2647 | |
| 2648 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2649 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2650 | builder.clearAccessChain(); |
| 2651 | builder.setAccessChainRValue(result); |
| 2652 | |
| 2653 | return false; |
| 2654 | } |
| 2655 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2656 | // |
| 2657 | // Create the list of operands. |
| 2658 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2659 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2660 | std::vector<spv::Id> operands; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2661 | std::vector<spv::IdImmediate> memoryAccessOperands; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2662 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2663 | // special case l-value operands; there are just a few |
| 2664 | bool lvalue = false; |
| 2665 | switch (node->getOp()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2666 | case glslang::EOpModf: |
| 2667 | if (arg == 1) |
| 2668 | lvalue = true; |
| 2669 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2670 | #ifndef GLSLANG_WEB |
| 2671 | case glslang::EOpFrexp: |
| 2672 | if (arg == 1) |
| 2673 | lvalue = true; |
| 2674 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2675 | case glslang::EOpInterpolateAtSample: |
| 2676 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2677 | case glslang::EOpInterpolateAtVertex: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2678 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2679 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2680 | |
| 2681 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2682 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2683 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2684 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2685 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 2686 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2687 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2688 | case glslang::EOpAtomicAdd: |
| 2689 | case glslang::EOpAtomicMin: |
| 2690 | case glslang::EOpAtomicMax: |
| 2691 | case glslang::EOpAtomicAnd: |
| 2692 | case glslang::EOpAtomicOr: |
| 2693 | case glslang::EOpAtomicXor: |
| 2694 | case glslang::EOpAtomicExchange: |
| 2695 | case glslang::EOpAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2696 | case glslang::EOpAtomicLoad: |
| 2697 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2698 | case glslang::EOpAtomicCounterAdd: |
| 2699 | case glslang::EOpAtomicCounterSubtract: |
| 2700 | case glslang::EOpAtomicCounterMin: |
| 2701 | case glslang::EOpAtomicCounterMax: |
| 2702 | case glslang::EOpAtomicCounterAnd: |
| 2703 | case glslang::EOpAtomicCounterOr: |
| 2704 | case glslang::EOpAtomicCounterXor: |
| 2705 | case glslang::EOpAtomicCounterExchange: |
| 2706 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2707 | if (arg == 0) |
| 2708 | lvalue = true; |
| 2709 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2710 | case glslang::EOpAddCarry: |
| 2711 | case glslang::EOpSubBorrow: |
| 2712 | if (arg == 2) |
| 2713 | lvalue = true; |
| 2714 | break; |
| 2715 | case glslang::EOpUMulExtended: |
| 2716 | case glslang::EOpIMulExtended: |
| 2717 | if (arg >= 2) |
| 2718 | lvalue = true; |
| 2719 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2720 | case glslang::EOpCooperativeMatrixLoad: |
| 2721 | if (arg == 0 || arg == 1) |
| 2722 | lvalue = true; |
| 2723 | break; |
| 2724 | case glslang::EOpCooperativeMatrixStore: |
| 2725 | if (arg == 1) |
| 2726 | lvalue = true; |
| 2727 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2728 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2729 | default: |
| 2730 | break; |
| 2731 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2732 | builder.clearAccessChain(); |
| 2733 | if (invertedType != spv::NoType && arg == 0) |
| 2734 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2735 | else |
| 2736 | glslangOperands[arg]->traverse(this); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2737 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2738 | #ifndef GLSLANG_WEB |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2739 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
| 2740 | node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2741 | |
| 2742 | if (arg == 1) { |
| 2743 | // fold "element" parameter into the access chain |
| 2744 | spv::Builder::AccessChain save = builder.getAccessChain(); |
| 2745 | builder.clearAccessChain(); |
| 2746 | glslangOperands[2]->traverse(this); |
| 2747 | |
| 2748 | spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
| 2749 | |
| 2750 | builder.setAccessChain(save); |
| 2751 | |
| 2752 | // Point to the first element of the array. |
| 2753 | builder.accessChainPush(elementId, TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 2754 | glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2755 | |
| 2756 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 2757 | unsigned int alignment = builder.getAccessChain().alignment; |
| 2758 | |
| 2759 | int memoryAccess = TranslateMemoryAccess(coherentFlags); |
| 2760 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
| 2761 | memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
| 2762 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
| 2763 | memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
| 2764 | if (builder.getStorageClass(builder.getAccessChain().base) == spv::StorageClassPhysicalStorageBufferEXT) { |
| 2765 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
| 2766 | } |
| 2767 | |
| 2768 | memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
| 2769 | |
| 2770 | if (memoryAccess & spv::MemoryAccessAlignedMask) { |
| 2771 | memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
| 2772 | } |
| 2773 | |
| 2774 | if (memoryAccess & (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
| 2775 | memoryAccessOperands.push_back(spv::IdImmediate(true, builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
| 2776 | } |
| 2777 | } else if (arg == 2) { |
| 2778 | continue; |
| 2779 | } |
| 2780 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2781 | #endif |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2782 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2783 | if (lvalue) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2784 | operands.push_back(builder.accessChainGetLValue()); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2785 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2786 | lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
| 2787 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2788 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2789 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2790 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2791 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2792 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2793 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2794 | #ifndef GLSLANG_WEB |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2795 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
| 2796 | std::vector<spv::IdImmediate> idImmOps; |
| 2797 | |
| 2798 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 2799 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 2800 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 2801 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 2802 | // get the pointee type |
| 2803 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
| 2804 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2805 | // do the op |
| 2806 | spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
| 2807 | // store the result to the pointer (out param 'm') |
| 2808 | builder.createStore(result, operands[0]); |
| 2809 | result = 0; |
| 2810 | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2811 | std::vector<spv::IdImmediate> idImmOps; |
| 2812 | |
| 2813 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 2814 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
| 2815 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 2816 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 2817 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 2818 | |
| 2819 | builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps); |
| 2820 | result = 0; |
| 2821 | } else if (atomic) { |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2822 | // Handle all atomics |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2823 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), lvalueCoherentFlags); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2824 | } else |
| 2825 | #endif |
| 2826 | { |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2827 | // Pass through to generic operations. |
| 2828 | switch (glslangOperands.size()) { |
| 2829 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2830 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2831 | break; |
| 2832 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2833 | { |
| 2834 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2835 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2836 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2837 | result = createUnaryOperation( |
| 2838 | node->getOp(), decorations, |
| 2839 | resultType(), operands.front(), |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2840 | glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2841 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2842 | break; |
| 2843 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2844 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2845 | break; |
| 2846 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2847 | if (invertedType) |
| 2848 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | if (noReturnValue) |
| 2852 | return false; |
| 2853 | |
| 2854 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2855 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2856 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2857 | } else { |
| 2858 | builder.clearAccessChain(); |
| 2859 | builder.setAccessChainRValue(result); |
| 2860 | return false; |
| 2861 | } |
| 2862 | } |
| 2863 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2864 | // This path handles both if-then-else and ?: |
| 2865 | // The if-then-else has a node type of void, while |
| 2866 | // ?: has either a void or a non-void node type |
| 2867 | // |
| 2868 | // Leaving the result, when not void: |
| 2869 | // GLSL only has r-values as the result of a :?, but |
| 2870 | // if we have an l-value, that can be more efficient if it will |
| 2871 | // become the base of a complex r-value expression, because the |
| 2872 | // 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] | 2873 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 2874 | { |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2875 | // see if OpSelect can handle it |
| 2876 | const auto isOpSelectable = [&]() { |
| 2877 | if (node->getBasicType() == glslang::EbtVoid) |
| 2878 | return false; |
| 2879 | // OpSelect can do all other types starting with SPV 1.4 |
| 2880 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
| 2881 | // pre-1.4, only scalars and vectors can be handled |
| 2882 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
| 2883 | return false; |
| 2884 | } |
| 2885 | return true; |
| 2886 | }; |
| 2887 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2888 | // See if it simple and safe, or required, to execute both sides. |
| 2889 | // Crucially, side effects must be either semantically required or avoided, |
| 2890 | // and there are performance trade-offs. |
| 2891 | // Return true if required or a good idea (and safe) to execute both sides, |
| 2892 | // false otherwise. |
| 2893 | const auto bothSidesPolicy = [&]() -> bool { |
| 2894 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2895 | if (node->getTrueBlock() == nullptr || |
| 2896 | node->getFalseBlock() == nullptr) |
| 2897 | return false; |
| 2898 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2899 | // required? (unless we write additional code to look for side effects |
| 2900 | // and make performance trade-offs if none are present) |
| 2901 | if (!node->getShortCircuit()) |
| 2902 | return true; |
| 2903 | |
| 2904 | // if not required to execute both, decide based on performance/practicality... |
| 2905 | |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2906 | if (!isOpSelectable()) |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2907 | return false; |
| 2908 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2909 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 2910 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 2911 | |
| 2912 | // return true if a single operand to ? : is okay for OpSelect |
| 2913 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2914 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2915 | }; |
| 2916 | |
| 2917 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 2918 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 2919 | }; |
| 2920 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2921 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 2922 | // emit the condition before doing anything with selection |
| 2923 | node->getCondition()->traverse(this); |
| 2924 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 2925 | |
| 2926 | // Find a way of executing both sides and selecting the right result. |
| 2927 | const auto executeBothSides = [&]() -> void { |
| 2928 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2929 | node->getTrueBlock()->traverse(this); |
| 2930 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2931 | node->getFalseBlock()->traverse(this); |
| 2932 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2933 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2934 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2935 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2936 | // done if void |
| 2937 | if (node->getBasicType() == glslang::EbtVoid) |
| 2938 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 2939 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2940 | // emit code to select between trueValue and falseValue |
| 2941 | |
| 2942 | // see if OpSelect can handle it |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2943 | if (isOpSelectable()) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2944 | // Emit OpSelect for this selection. |
| 2945 | |
| 2946 | // smear condition to vector, if necessary (AST is always scalar) |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2947 | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
| 2948 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2949 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 2950 | builder.makeVectorType(builder.makeBoolType(), |
| 2951 | builder.getNumComponents(trueValue))); |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 2952 | } |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2953 | |
| 2954 | // OpSelect |
| 2955 | result = builder.createTriOp(spv::OpSelect, |
| 2956 | convertGlslangToSpvType(node->getType()), condition, |
| 2957 | trueValue, falseValue); |
| 2958 | |
| 2959 | builder.clearAccessChain(); |
| 2960 | builder.setAccessChainRValue(result); |
| 2961 | } else { |
| 2962 | // We need control flow to select the result. |
| 2963 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 2964 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2965 | |
| 2966 | // Selection control: |
| 2967 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2968 | |
| 2969 | // make an "if" based on the value created by the condition |
| 2970 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2971 | |
| 2972 | // emit the "then" statement |
| 2973 | builder.createStore(trueValue, result); |
| 2974 | ifBuilder.makeBeginElse(); |
| 2975 | // emit the "else" statement |
| 2976 | builder.createStore(falseValue, result); |
| 2977 | |
| 2978 | // finish off the control flow |
| 2979 | ifBuilder.makeEndIf(); |
| 2980 | |
| 2981 | builder.clearAccessChain(); |
| 2982 | builder.setAccessChainLValue(result); |
| 2983 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2984 | }; |
| 2985 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2986 | // Execute the one side needed, as per the condition |
| 2987 | const auto executeOneSide = [&]() { |
| 2988 | // Always emit control flow. |
| 2989 | if (node->getBasicType() != glslang::EbtVoid) |
| 2990 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2991 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2992 | // Selection control: |
| 2993 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2994 | |
| 2995 | // make an "if" based on the value created by the condition |
| 2996 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2997 | |
| 2998 | // emit the "then" statement |
| 2999 | if (node->getTrueBlock() != nullptr) { |
| 3000 | node->getTrueBlock()->traverse(this); |
| 3001 | if (result != spv::NoResult) |
| 3002 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 3003 | } |
| 3004 | |
| 3005 | if (node->getFalseBlock() != nullptr) { |
| 3006 | ifBuilder.makeBeginElse(); |
| 3007 | // emit the "else" statement |
| 3008 | node->getFalseBlock()->traverse(this); |
| 3009 | if (result != spv::NoResult) |
| 3010 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 3011 | } |
| 3012 | |
| 3013 | // finish off the control flow |
| 3014 | ifBuilder.makeEndIf(); |
| 3015 | |
| 3016 | if (result != spv::NoResult) { |
| 3017 | builder.clearAccessChain(); |
| 3018 | builder.setAccessChainLValue(result); |
| 3019 | } |
| 3020 | }; |
| 3021 | |
| 3022 | // Try for OpSelect (or a requirement to execute both sides) |
| 3023 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 3024 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 3025 | if (node->getType().getQualifier().isSpecConstant()) |
| 3026 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3027 | executeBothSides(); |
| 3028 | } else |
| 3029 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3030 | |
| 3031 | return false; |
| 3032 | } |
| 3033 | |
| 3034 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 3035 | { |
| 3036 | // emit and get the condition before doing anything with switch |
| 3037 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3038 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3039 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3040 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 3041 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3042 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3043 | // browse the children to sort out code segments |
| 3044 | int defaultSegment = -1; |
| 3045 | std::vector<TIntermNode*> codeSegments; |
| 3046 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 3047 | std::vector<int> caseValues; |
| 3048 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 3049 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 3050 | TIntermNode* child = *c; |
| 3051 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3052 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3053 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3054 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3055 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3056 | } else |
| 3057 | codeSegments.push_back(child); |
| 3058 | } |
| 3059 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3060 | // 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] | 3061 | // statements between the last case and the end of the switch statement |
| 3062 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 3063 | (int)codeSegments.size() == defaultSegment) |
| 3064 | codeSegments.push_back(nullptr); |
| 3065 | |
| 3066 | // make the switch statement |
| 3067 | 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] | 3068 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3069 | |
| 3070 | // emit all the code in the segments |
| 3071 | breakForLoop.push(false); |
| 3072 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 3073 | builder.nextSwitchSegment(segmentBlocks, s); |
| 3074 | if (codeSegments[s]) |
| 3075 | codeSegments[s]->traverse(this); |
| 3076 | else |
| 3077 | builder.addSwitchBreak(); |
| 3078 | } |
| 3079 | breakForLoop.pop(); |
| 3080 | |
| 3081 | builder.endSwitch(segmentBlocks); |
| 3082 | |
| 3083 | return false; |
| 3084 | } |
| 3085 | |
| 3086 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 3087 | { |
| 3088 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 3089 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3090 | |
| 3091 | builder.clearAccessChain(); |
| 3092 | builder.setAccessChainRValue(constant); |
| 3093 | } |
| 3094 | |
| 3095 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 3096 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3097 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3098 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3099 | |
| 3100 | // Loop control: |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3101 | std::vector<unsigned int> operands; |
| 3102 | const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3103 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3104 | // Spec requires back edges to target header blocks, and every header block |
| 3105 | // must dominate its merge block. Make a header block first to ensure these |
| 3106 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 3107 | // by a block-ending branch. But we don't want to put any other body/test |
| 3108 | // instructions in it, since the body/test may have arbitrary instructions, |
| 3109 | // including merges of its own. |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3110 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3111 | builder.setBuildPoint(&blocks.head); |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3112 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3113 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3114 | spv::Block& test = builder.makeNewBlock(); |
| 3115 | builder.createBranch(&test); |
| 3116 | |
| 3117 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3118 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3119 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3120 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 3121 | |
| 3122 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3123 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3124 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3125 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3126 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3127 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3128 | |
| 3129 | builder.setBuildPoint(&blocks.continue_target); |
| 3130 | if (node->getTerminal()) |
| 3131 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3132 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 3133 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3134 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3135 | builder.createBranch(&blocks.body); |
| 3136 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3137 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3138 | builder.setBuildPoint(&blocks.body); |
| 3139 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3140 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3141 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3142 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3143 | |
| 3144 | builder.setBuildPoint(&blocks.continue_target); |
| 3145 | if (node->getTerminal()) |
| 3146 | node->getTerminal()->traverse(this); |
| 3147 | if (node->getTest()) { |
| 3148 | node->getTest()->traverse(this); |
| 3149 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3150 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3151 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3152 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 3153 | // TODO: unless there was a break/return/discard instruction |
| 3154 | // somewhere in the body, this is an infinite loop, so we should |
| 3155 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3156 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3157 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3158 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3159 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3160 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3161 | return false; |
| 3162 | } |
| 3163 | |
| 3164 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 3165 | { |
| 3166 | if (node->getExpression()) |
| 3167 | node->getExpression()->traverse(this); |
| 3168 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3169 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3170 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3171 | switch (node->getFlowOp()) { |
| 3172 | case glslang::EOpKill: |
| 3173 | builder.makeDiscard(); |
| 3174 | break; |
| 3175 | case glslang::EOpBreak: |
| 3176 | if (breakForLoop.top()) |
| 3177 | builder.createLoopExit(); |
| 3178 | else |
| 3179 | builder.addSwitchBreak(); |
| 3180 | break; |
| 3181 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3182 | builder.createLoopContinue(); |
| 3183 | break; |
| 3184 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3185 | if (node->getExpression()) { |
| 3186 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 3187 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 3188 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 3189 | builder.clearAccessChain(); |
| 3190 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 3191 | builder.setAccessChainLValue(copyId); |
| 3192 | multiTypeStore(glslangReturnType, returnId); |
| 3193 | returnId = builder.createLoad(copyId); |
| 3194 | } |
| 3195 | builder.makeReturn(false, returnId); |
| 3196 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 3197 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3198 | |
| 3199 | builder.clearAccessChain(); |
| 3200 | break; |
| 3201 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3202 | #ifndef GLSLANG_WEB |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3203 | case glslang::EOpDemote: |
| 3204 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 3205 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 3206 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 3207 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3208 | #endif |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3209 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3210 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3211 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3212 | break; |
| 3213 | } |
| 3214 | |
| 3215 | return false; |
| 3216 | } |
| 3217 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3218 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3219 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3220 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3221 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3222 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 3223 | if (node->getQualifier().isConstant()) { |
Dan Sinclair | 12fcaa2 | 2018-11-13 09:17:44 -0500 | [diff] [blame] | 3224 | spv::Id result = createSpvConstant(*node); |
| 3225 | if (result != spv::NoResult) |
| 3226 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3230 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3231 | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
| 3232 | : forcedType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3233 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3234 | const bool contains16BitType = node->getType().contains16BitFloat() || |
| 3235 | node->getType().contains16BitInt(); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3236 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3237 | switch (storageClass) { |
| 3238 | case spv::StorageClassInput: |
| 3239 | case spv::StorageClassOutput: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3240 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3241 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3242 | break; |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3243 | case spv::StorageClassUniform: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3244 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3245 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 3246 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3247 | else |
| 3248 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 3249 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3250 | #ifndef GLSLANG_WEB |
| 3251 | case spv::StorageClassPushConstant: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3252 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3253 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 3254 | break; |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3255 | case spv::StorageClassStorageBuffer: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3256 | case spv::StorageClassPhysicalStorageBufferEXT: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3257 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3258 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 3259 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3260 | #endif |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3261 | default: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3262 | if (node->getType().contains16BitFloat()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3263 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3264 | if (node->getType().contains16BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3265 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3266 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3267 | } |
| 3268 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3269 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3270 | if (node->getType().contains8BitInt()) { |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3271 | if (storageClass == spv::StorageClassPushConstant) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3272 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3273 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 3274 | } else if (storageClass == spv::StorageClassUniform) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3275 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3276 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3277 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3278 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3279 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3280 | } else { |
| 3281 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3282 | } |
| 3283 | } |
| 3284 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3285 | const char* name = node->getName().c_str(); |
| 3286 | if (glslang::IsAnonymous(name)) |
| 3287 | name = ""; |
| 3288 | |
| 3289 | return builder.createVariable(storageClass, spvType, name); |
| 3290 | } |
| 3291 | |
| 3292 | // Return type Id of the sampled type. |
| 3293 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 3294 | { |
| 3295 | switch (sampler.type) { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3296 | case glslang::EbtInt: return builder.makeIntType(32); |
| 3297 | case glslang::EbtUint: return builder.makeUintType(32); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3298 | case glslang::EbtFloat: return builder.makeFloatType(32); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3299 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3300 | case glslang::EbtFloat16: |
| 3301 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 3302 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3303 | return builder.makeFloatType(16); |
| 3304 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3305 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3306 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3307 | return builder.makeFloatType(32); |
| 3308 | } |
| 3309 | } |
| 3310 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3311 | // If node is a swizzle operation, return the type that should be used if |
| 3312 | // the swizzle base is first consumed by another operation, before the swizzle |
| 3313 | // is applied. |
| 3314 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 3315 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3316 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3317 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 3318 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 3319 | else |
| 3320 | return spv::NoType; |
| 3321 | } |
| 3322 | |
| 3323 | // When inverting a swizzle with a parent op, this function |
| 3324 | // will apply the swizzle operation to a completed parent operation. |
| 3325 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 3326 | { |
| 3327 | std::vector<unsigned> swizzle; |
| 3328 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 3329 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 3330 | } |
| 3331 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3332 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 3333 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 3334 | { |
| 3335 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 3336 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 3337 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3338 | } |
| 3339 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3340 | // Convert from a glslang type to an SPV type, by calling into a |
| 3341 | // recursive version of this function. This establishes the inherited |
| 3342 | // layout state rooted from the top-level type. |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3343 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3344 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3345 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | // 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] | 3349 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3350 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3351 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3352 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 3353 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3354 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3355 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3356 | |
| 3357 | switch (type.getBasicType()) { |
| 3358 | case glslang::EbtVoid: |
| 3359 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3360 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3361 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3362 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3363 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 3364 | // a 32-bit int where non-0 means true. |
| 3365 | if (explicitLayout != glslang::ElpNone) |
| 3366 | spvType = builder.makeUintType(32); |
| 3367 | else |
| 3368 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3369 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3370 | case glslang::EbtInt: |
| 3371 | spvType = builder.makeIntType(32); |
| 3372 | break; |
| 3373 | case glslang::EbtUint: |
| 3374 | spvType = builder.makeUintType(32); |
| 3375 | break; |
| 3376 | case glslang::EbtFloat: |
| 3377 | spvType = builder.makeFloatType(32); |
| 3378 | break; |
| 3379 | #ifndef GLSLANG_WEB |
| 3380 | case glslang::EbtDouble: |
| 3381 | spvType = builder.makeFloatType(64); |
| 3382 | break; |
| 3383 | case glslang::EbtFloat16: |
| 3384 | spvType = builder.makeFloatType(16); |
| 3385 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3386 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3387 | spvType = builder.makeIntType(8); |
| 3388 | break; |
| 3389 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3390 | spvType = builder.makeUintType(8); |
| 3391 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3392 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3393 | spvType = builder.makeIntType(16); |
| 3394 | break; |
| 3395 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3396 | spvType = builder.makeUintType(16); |
| 3397 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3398 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3399 | spvType = builder.makeIntType(64); |
| 3400 | break; |
| 3401 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3402 | spvType = builder.makeUintType(64); |
| 3403 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3404 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 3405 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3406 | spvType = builder.makeUintType(32); |
| 3407 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3408 | case glslang::EbtAccStructNV: |
| 3409 | spvType = builder.makeAccelerationStructureNVType(); |
| 3410 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3411 | case glslang::EbtReference: |
| 3412 | { |
| 3413 | // Make the forward pointer, then recurse to convert the structure type, then |
| 3414 | // patch up the forward pointer with a real pointer type. |
| 3415 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 3416 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 3417 | forwardPointers[type.getReferentType()] = forwardId; |
| 3418 | } |
| 3419 | spvType = forwardPointers[type.getReferentType()]; |
| 3420 | if (!forwardReferenceOnly) { |
| 3421 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 3422 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 3423 | forwardPointers[type.getReferentType()], |
| 3424 | referentType); |
| 3425 | } |
| 3426 | } |
| 3427 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3428 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3429 | case glslang::EbtSampler: |
| 3430 | { |
| 3431 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3432 | if (sampler.isPureSampler()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3433 | spvType = builder.makeSamplerType(); |
| 3434 | } else { |
| 3435 | // an image is present, make its type |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3436 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), |
| 3437 | sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), |
| 3438 | sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type)); |
| 3439 | if (sampler.isCombined()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3440 | // already has both image and sampler, make the combined type |
| 3441 | spvType = builder.makeSampledImageType(spvType); |
| 3442 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3443 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3444 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3445 | break; |
| 3446 | case glslang::EbtStruct: |
| 3447 | case glslang::EbtBlock: |
| 3448 | { |
| 3449 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3450 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3451 | |
| 3452 | // Try to share structs for different layouts, but not yet for other |
| 3453 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3454 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3455 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3456 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3457 | break; |
| 3458 | |
| 3459 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3460 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3461 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 3462 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3463 | } |
| 3464 | break; |
| 3465 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3466 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3467 | break; |
| 3468 | } |
| 3469 | |
| 3470 | if (type.isMatrix()) |
| 3471 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3472 | else { |
| 3473 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3474 | if (type.getVectorSize() > 1) |
| 3475 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3476 | } |
| 3477 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3478 | if (type.isCoopMat()) { |
| 3479 | builder.addCapability(spv::CapabilityCooperativeMatrixNV); |
| 3480 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
| 3481 | if (type.getBasicType() == glslang::EbtFloat16) |
| 3482 | builder.addCapability(spv::CapabilityFloat16); |
Jeff Bolz | 387657e | 2019-08-22 20:28:00 -0500 | [diff] [blame] | 3483 | if (type.getBasicType() == glslang::EbtUint8 || |
| 3484 | type.getBasicType() == glslang::EbtInt8) { |
| 3485 | builder.addCapability(spv::CapabilityInt8); |
| 3486 | } |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3487 | |
| 3488 | spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1); |
| 3489 | spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2); |
| 3490 | spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3); |
| 3491 | |
| 3492 | spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols); |
| 3493 | } |
| 3494 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3495 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3496 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3497 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3498 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3499 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3500 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3501 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3502 | // Use a dummy glslang type for querying internal strides of |
| 3503 | // arrays of arrays, but using just a one-dimensional array. |
| 3504 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3505 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3506 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3507 | |
| 3508 | // Will compute the higher-order strides here, rather than making a whole |
| 3509 | // pile of types and doing repetitive recursion on their contents. |
| 3510 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3511 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3512 | |
| 3513 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3514 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3515 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3516 | if (stride > 0) |
| 3517 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3518 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3519 | } |
| 3520 | } else { |
| 3521 | // single-dimensional array, and don't yet have stride |
| 3522 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3523 | // 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] | 3524 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3525 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3526 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3527 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3528 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3529 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3530 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3531 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3532 | else { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3533 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3534 | if (!lastBufferBlockMember) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3535 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3536 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3537 | } |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3538 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3539 | #endif |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3540 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3541 | if (stride > 0) |
| 3542 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | return spvType; |
| 3546 | } |
| 3547 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3548 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3549 | // |
| 3550 | // Identify interface members that don't have their required extension turned on. |
| 3551 | // |
| 3552 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3553 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3554 | #ifndef GLSLANG_WEB |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3555 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3556 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3557 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3558 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3559 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3560 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3561 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3562 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3563 | |
| 3564 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3565 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3566 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3567 | return true; |
| 3568 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3569 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3570 | return true; |
| 3571 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3572 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3573 | return true; |
| 3574 | } |
| 3575 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3576 | |
| 3577 | return false; |
| 3578 | }; |
| 3579 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3580 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3581 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3582 | // Mutually recursive with convertGlslangToSpvType(). |
| 3583 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3584 | const glslang::TTypeList* glslangMembers, |
| 3585 | glslang::TLayoutPacking explicitLayout, |
| 3586 | const glslang::TQualifier& qualifier) |
| 3587 | { |
| 3588 | // Create a vector of struct types for SPIR-V to consume |
| 3589 | std::vector<spv::Id> spvMembers; |
| 3590 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3591 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3592 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3593 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3594 | if (glslangMember.hiddenMember()) { |
| 3595 | ++memberDelta; |
| 3596 | if (type.getBasicType() == glslang::EbtBlock) |
| 3597 | memberRemapper[glslangMembers][i] = -1; |
| 3598 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3599 | if (type.getBasicType() == glslang::EbtBlock) { |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 3600 | if (filterMember(glslangMember)) { |
| 3601 | memberDelta++; |
| 3602 | memberRemapper[glslangMembers][i] = -1; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3603 | continue; |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 3604 | } |
| 3605 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3606 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3607 | // modify just this child's view of the qualifier |
| 3608 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3609 | InheritQualifiers(memberQualifier, qualifier); |
| 3610 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3611 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3612 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3613 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3614 | |
| 3615 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3616 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3617 | i == (int)glslangMembers->size() - 1; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3618 | |
| 3619 | // Make forward pointers for any pointer members, and create a list of members to |
| 3620 | // convert to spirv types after creating the struct. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 3621 | if (glslangMember.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3622 | if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) { |
| 3623 | deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier)); |
| 3624 | } |
| 3625 | spvMembers.push_back( |
| 3626 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, true)); |
| 3627 | } else { |
| 3628 | spvMembers.push_back( |
| 3629 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, false)); |
| 3630 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | // Make the SPIR-V type |
| 3635 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3636 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3637 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3638 | |
| 3639 | // Decorate it |
| 3640 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3641 | |
John Kessenich | d72f488 | 2019-01-16 14:55:37 +0700 | [diff] [blame] | 3642 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3643 | auto it = deferredForwardPointers[i]; |
| 3644 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 3645 | } |
| 3646 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3647 | return spvType; |
| 3648 | } |
| 3649 | |
| 3650 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3651 | const glslang::TTypeList* glslangMembers, |
| 3652 | glslang::TLayoutPacking explicitLayout, |
| 3653 | const glslang::TQualifier& qualifier, |
| 3654 | spv::Id spvType) |
| 3655 | { |
| 3656 | // Name and decorate the non-hidden members |
| 3657 | int offset = -1; |
| 3658 | int locationOffset = 0; // for use within the members of this struct |
| 3659 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3660 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3661 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3662 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3663 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3664 | if (filterMember(glslangMember)) |
| 3665 | continue; |
| 3666 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3667 | |
| 3668 | // modify just this child's view of the qualifier |
| 3669 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3670 | InheritQualifiers(memberQualifier, qualifier); |
| 3671 | |
| 3672 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3673 | if (member < 0) |
| 3674 | continue; |
| 3675 | |
| 3676 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3677 | builder.addMemberDecoration(spvType, member, |
| 3678 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 3679 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 3680 | // Add interpolation and auxiliary storage decorations only to |
| 3681 | // top-level members of Input and Output storage classes |
| 3682 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 3683 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 3684 | if (type.getBasicType() == glslang::EbtBlock || |
| 3685 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 3686 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 3687 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3688 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3689 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 3690 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3691 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3692 | } |
| 3693 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3694 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3695 | #ifndef GLSLANG_WEB |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3696 | if (type.getBasicType() == glslang::EbtBlock && |
| 3697 | qualifier.storage == glslang::EvqBuffer) { |
| 3698 | // Add memory decorations only to top-level members of shader storage block |
| 3699 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3700 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3701 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3702 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 3703 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3704 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3705 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3706 | // Location assignment was already completed correctly by the front end, |
| 3707 | // just track whether a member needs to be decorated. |
| 3708 | // Ignore member locations if the container is an array, as that's |
| 3709 | // ill-specified and decisions have been made to not allow this. |
| 3710 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 3711 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3712 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3713 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 3714 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 3715 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 3716 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3717 | // component, XFB, others |
| 3718 | if (glslangMember.getQualifier().hasComponent()) |
| 3719 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 3720 | glslangMember.getQualifier().layoutComponent); |
| 3721 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 3722 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 3723 | glslangMember.getQualifier().layoutXfbOffset); |
| 3724 | else if (explicitLayout != glslang::ElpNone) { |
| 3725 | // figure out what to do with offset, which is accumulating |
| 3726 | int nextOffset; |
| 3727 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 3728 | if (offset >= 0) |
| 3729 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 3730 | offset = nextOffset; |
| 3731 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3732 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3733 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 3734 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 3735 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3736 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3737 | // built-in variable decorations |
| 3738 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 3739 | if (builtIn != spv::BuiltInMax) |
| 3740 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3741 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3742 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3743 | // nonuniform |
| 3744 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 3745 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3746 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 3747 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 3748 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 3749 | memberQualifier.semanticName); |
| 3750 | } |
| 3751 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3752 | if (builtIn == spv::BuiltInLayer) { |
| 3753 | // SPV_NV_viewport_array2 extension |
| 3754 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 3755 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 3756 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 3757 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3758 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3759 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 3760 | builder.addMemberDecoration(spvType, member, |
| 3761 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 3762 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 3763 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 3764 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3765 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3766 | } |
| 3767 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 3768 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 3769 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 3770 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 3771 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3772 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3776 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 3777 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3778 | } |
| 3779 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3780 | // Turn the expression forming the array size into an id. |
| 3781 | // This is not quite trivial, because of specialization constants. |
| 3782 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 3783 | // a specialization constant expression is. |
| 3784 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 3785 | { |
| 3786 | // First, see if this is sized with a node, meaning a specialization constant: |
| 3787 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 3788 | if (specNode != nullptr) { |
| 3789 | builder.clearAccessChain(); |
| 3790 | specNode->traverse(this); |
| 3791 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 3792 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3793 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3794 | // Otherwise, need a compile-time (front end) size, get it: |
| 3795 | int size = arraySizes.getDimSize(dim); |
| 3796 | assert(size > 0); |
| 3797 | return builder.makeUintConstant(size); |
| 3798 | } |
| 3799 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3800 | // Wrap the builder's accessChainLoad to: |
| 3801 | // - localize handling of RelaxedPrecision |
| 3802 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 3803 | // (avoids unnecessary work and possible type punning for structures) |
| 3804 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3805 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 3806 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3807 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3808 | |
| 3809 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3810 | coherentFlags |= TranslateCoherent(type); |
| 3811 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3812 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3813 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3814 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3815 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3816 | TranslateNonUniformDecoration(type.getQualifier()), |
| 3817 | nominalTypeId, |
| 3818 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3819 | TranslateMemoryScope(coherentFlags), |
| 3820 | alignment); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3821 | |
| 3822 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3823 | if (type.getBasicType() == glslang::EbtBool) { |
| 3824 | if (builder.isScalarType(nominalTypeId)) { |
| 3825 | // Conversion for bool |
| 3826 | spv::Id boolType = builder.makeBoolType(); |
| 3827 | if (nominalTypeId != boolType) |
| 3828 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 3829 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3830 | // Conversion for bvec |
| 3831 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3832 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 3833 | if (nominalTypeId != bvecType) |
| 3834 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 3835 | } |
| 3836 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3837 | |
| 3838 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3839 | } |
| 3840 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3841 | // Wrap the builder's accessChainStore to: |
| 3842 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3843 | // |
| 3844 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3845 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 3846 | { |
| 3847 | // Need to convert to abstract types when necessary |
| 3848 | if (type.getBasicType() == glslang::EbtBool) { |
| 3849 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 3850 | |
| 3851 | if (builder.isScalarType(nominalTypeId)) { |
| 3852 | // Conversion for bool |
| 3853 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3854 | if (nominalTypeId != boolType) { |
| 3855 | // keep these outside arguments, for determinant order-of-evaluation |
| 3856 | spv::Id one = builder.makeUintConstant(1); |
| 3857 | spv::Id zero = builder.makeUintConstant(0); |
| 3858 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 3859 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3860 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3861 | } else if (builder.isVectorType(nominalTypeId)) { |
| 3862 | // Conversion for bvec |
| 3863 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 3864 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3865 | if (nominalTypeId != bvecType) { |
| 3866 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 3867 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 3868 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 3869 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 3870 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 3871 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 3872 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3873 | } |
| 3874 | } |
| 3875 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3876 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3877 | coherentFlags |= TranslateCoherent(type); |
| 3878 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3879 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3880 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3881 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3882 | builder.accessChainStore(rvalue, |
| 3883 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerVisibleKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3884 | TranslateMemoryScope(coherentFlags), alignment); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 3885 | } |
| 3886 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3887 | // For storing when types match at the glslang level, but not might match at the |
| 3888 | // SPIR-V level. |
| 3889 | // |
| 3890 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3891 | // 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] | 3892 | // as in a member-decorated way. |
| 3893 | // |
| 3894 | // NOTE: This function can handle any store request; if it's not special it |
| 3895 | // simplifies to a simple OpStore. |
| 3896 | // |
| 3897 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 3898 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 3899 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3900 | // we only do the complex path here if it's an aggregate |
| 3901 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3902 | accessChainStore(type, rValue); |
| 3903 | return; |
| 3904 | } |
| 3905 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3906 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3907 | spv::Id rType = builder.getTypeId(rValue); |
| 3908 | spv::Id lValue = builder.accessChainGetLValue(); |
| 3909 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 3910 | if (lType == rType) { |
| 3911 | accessChainStore(type, rValue); |
| 3912 | return; |
| 3913 | } |
| 3914 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3915 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3916 | // where the two types were the same type in GLSL. This requires member |
| 3917 | // by member copy, recursively. |
| 3918 | |
John Kessenich | fbb6bdf | 2019-01-15 21:48:27 +0700 | [diff] [blame] | 3919 | // SPIR-V 1.4 added an instruction to do help do this. |
| 3920 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 3921 | // However, bool in uniform space is changed to int, so |
| 3922 | // OpCopyLogical does not work for that. |
| 3923 | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
| 3924 | bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0); |
| 3925 | bool lBool = builder.containsType(lType, spv::OpTypeBool, 0); |
| 3926 | if (lBool == rBool) { |
| 3927 | spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue); |
| 3928 | accessChainStore(type, logicalCopy); |
| 3929 | return; |
| 3930 | } |
| 3931 | } |
| 3932 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3933 | // If an array, copy element by element. |
| 3934 | if (type.isArray()) { |
| 3935 | glslang::TType glslangElementType(type, 0); |
| 3936 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 3937 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 3938 | // get the source member |
| 3939 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3940 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3941 | // set up the target storage |
| 3942 | builder.clearAccessChain(); |
| 3943 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3944 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), type.getBufferReferenceAlignment()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3945 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3946 | // store the member |
| 3947 | multiTypeStore(glslangElementType, elementRValue); |
| 3948 | } |
| 3949 | } else { |
| 3950 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3951 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3952 | // loop over structure members |
| 3953 | const glslang::TTypeList& members = *type.getStruct(); |
| 3954 | for (int m = 0; m < (int)members.size(); ++m) { |
| 3955 | const glslang::TType& glslangMemberType = *members[m].type; |
| 3956 | |
| 3957 | // get the source member |
| 3958 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 3959 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 3960 | |
| 3961 | // set up the target storage |
| 3962 | builder.clearAccessChain(); |
| 3963 | builder.setAccessChainLValue(lValue); |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 3964 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), type.getBufferReferenceAlignment()); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 3965 | |
| 3966 | // store the member |
| 3967 | multiTypeStore(glslangMemberType, memberRValue); |
| 3968 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3969 | } |
| 3970 | } |
| 3971 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3972 | // Decide whether or not this type should be |
| 3973 | // decorated with offsets and strides, and if so |
| 3974 | // whether std140 or std430 rules should be applied. |
| 3975 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3976 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3977 | // has to be a block |
| 3978 | if (type.getBasicType() != glslang::EbtBlock) |
| 3979 | return glslang::ElpNone; |
| 3980 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3981 | // has to be a uniform or buffer block or task in/out blocks |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3982 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3983 | type.getQualifier().storage != glslang::EvqBuffer && |
| 3984 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3985 | return glslang::ElpNone; |
| 3986 | |
| 3987 | // return the layout to use |
| 3988 | switch (type.getQualifier().layoutPacking) { |
| 3989 | case glslang::ElpStd140: |
| 3990 | case glslang::ElpStd430: |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 3991 | case glslang::ElpScalar: |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3992 | return type.getQualifier().layoutPacking; |
| 3993 | default: |
| 3994 | return glslang::ElpNone; |
| 3995 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3996 | } |
| 3997 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3998 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3999 | 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] | 4000 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4001 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4002 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4003 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 4004 | |
| 4005 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4006 | } |
| 4007 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4008 | // 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] | 4009 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 4010 | 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] | 4011 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4012 | glslang::TType elementType; |
| 4013 | elementType.shallowCopy(matrixType); |
| 4014 | elementType.clearArraySizes(); |
| 4015 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4016 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4017 | int stride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4018 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4019 | |
| 4020 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4021 | } |
| 4022 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4023 | // Given a member type of a struct, realign the current offset for it, and compute |
| 4024 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 4025 | // on the next call. |
| 4026 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 4027 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 4028 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4029 | 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] | 4030 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4031 | { |
| 4032 | // this will get a positive value when deemed necessary |
| 4033 | nextOffset = -1; |
| 4034 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4035 | // override anything in currentOffset with user-set offset |
| 4036 | if (memberType.getQualifier().hasOffset()) |
| 4037 | currentOffset = memberType.getQualifier().layoutOffset; |
| 4038 | |
| 4039 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 4040 | // in which case the following code does not matter. But, that's not quite right |
| 4041 | // once cross-compilation unit GLSL validation is done, as the original user |
| 4042 | // settings are needed in layoutOffset, and then the following will come into play. |
| 4043 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4044 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4045 | if (! memberType.getQualifier().hasOffset()) |
| 4046 | currentOffset = -1; |
| 4047 | |
| 4048 | return; |
| 4049 | } |
| 4050 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4051 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4052 | if (currentOffset < 0) |
| 4053 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4054 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4055 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 4056 | // but possibly not yet correctly aligned. |
| 4057 | |
| 4058 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4059 | int dummyStride; |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4060 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4061 | |
| 4062 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4063 | // TODO: make this consistent in early phases of code: |
| 4064 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 4065 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 4066 | // 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] | 4067 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4068 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4069 | int dummySize; |
| 4070 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 4071 | if (componentAlignment <= 4) |
| 4072 | memberAlignment = componentAlignment; |
| 4073 | } |
| 4074 | |
| 4075 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4076 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4077 | |
| 4078 | // Bump up to vec4 if there is a bad straddle |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4079 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4080 | glslang::RoundToPow2(currentOffset, 16); |
| 4081 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4082 | nextOffset = currentOffset + memberSize; |
| 4083 | } |
| 4084 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4085 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4086 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4087 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 4088 | switch (glslangBuiltIn) |
| 4089 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4090 | case glslang::EbvPointSize: |
| 4091 | #ifndef GLSLANG_WEB |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4092 | case glslang::EbvClipDistance: |
| 4093 | case glslang::EbvCullDistance: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4094 | case glslang::EbvViewportMaskNV: |
| 4095 | case glslang::EbvSecondaryPositionNV: |
| 4096 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 4097 | case glslang::EbvPositionPerViewNV: |
| 4098 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4099 | case glslang::EbvTaskCountNV: |
| 4100 | case glslang::EbvPrimitiveCountNV: |
| 4101 | case glslang::EbvPrimitiveIndicesNV: |
| 4102 | case glslang::EbvClipDistancePerViewNV: |
| 4103 | case glslang::EbvCullDistancePerViewNV: |
| 4104 | case glslang::EbvLayerPerViewNV: |
| 4105 | case glslang::EbvMeshViewCountNV: |
| 4106 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4107 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4108 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 4109 | // Alternately, we could just call this for any glslang built-in, since the |
| 4110 | // capability already guards against duplicates. |
| 4111 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 4112 | break; |
| 4113 | default: |
| 4114 | // Capabilities were already generated when the struct was declared. |
| 4115 | break; |
| 4116 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4117 | } |
| 4118 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4119 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4120 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 4121 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4122 | } |
| 4123 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4124 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4125 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 4126 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4127 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4128 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4129 | assert(qualifier == glslang::EvqIn || |
| 4130 | qualifier == glslang::EvqOut || |
| 4131 | qualifier == glslang::EvqInOut || |
| 4132 | qualifier == glslang::EvqConstReadOnly); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4133 | return qualifier != glslang::EvqConstReadOnly; |
| 4134 | } |
| 4135 | |
| 4136 | // Is parameter pass-by-original? |
| 4137 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 4138 | bool implicitThisParam) |
| 4139 | { |
| 4140 | if (implicitThisParam) // implicit this |
| 4141 | return true; |
| 4142 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4143 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4144 | return paramType.containsOpaque() || // sampler, etc. |
| 4145 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 4146 | } |
| 4147 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4148 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 4149 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4150 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4151 | 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] | 4152 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 4153 | if (paramPrecision != spv::NoPrecision) |
| 4154 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4155 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 4156 | if (type.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4157 | // Original and non-writable params pass the pointer directly and |
| 4158 | // use restrict/aliased, others are stored to a pointer in Function |
| 4159 | // memory and use RestrictPointer/AliasedPointer. |
| 4160 | if (originalParam(type.getQualifier().storage, type, false) || |
| 4161 | !writableParam(type.getQualifier().storage)) { |
| 4162 | decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrict : spv::DecorationAliased); |
| 4163 | } else { |
| 4164 | decorations.push_back(type.getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 4165 | } |
| 4166 | } |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4167 | }; |
| 4168 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4169 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4170 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4171 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4172 | continue; |
| 4173 | |
| 4174 | // 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] | 4175 | // 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] | 4176 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4177 | // 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] | 4178 | // function. What it is an address of varies: |
| 4179 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4180 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 4181 | // 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] | 4182 | // |
| 4183 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 4184 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4185 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 4186 | // 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] | 4187 | |
| 4188 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4189 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4190 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 4191 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4192 | #ifdef ENABLE_HLSL |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4193 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 4194 | glslangIntermediate->implicitThisName; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4195 | #else |
| 4196 | bool implicitThis = false; |
| 4197 | #endif |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4198 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4199 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4200 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4201 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4202 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4203 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 4204 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4205 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4206 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 4207 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4208 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4209 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4210 | paramTypes.push_back(typeId); |
| 4211 | } |
| 4212 | |
| 4213 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4214 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 4215 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4216 | glslFunction->getName().c_str(), paramTypes, |
| 4217 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4218 | if (implicitThis) |
| 4219 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4220 | |
| 4221 | // Track function to emit/call later |
| 4222 | functionMap[glslFunction->getName().c_str()] = function; |
| 4223 | |
| 4224 | // Set the parameter id's |
| 4225 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4226 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 4227 | // give a name too |
| 4228 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4229 | |
| 4230 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4231 | if (paramType.contains8BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4232 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4233 | if (paramType.contains16BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4234 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4235 | if (paramType.contains16BitFloat()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4236 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | // Process all the initializers, while skipping the functions and link objects |
| 4242 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 4243 | { |
| 4244 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 4245 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 4246 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 4247 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 4248 | |
| 4249 | // 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] | 4250 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4251 | initializer->traverse(this); |
| 4252 | } |
| 4253 | } |
| 4254 | } |
| 4255 | |
| 4256 | // Process all the functions, while skipping initializers. |
| 4257 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4258 | { |
| 4259 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4260 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 4261 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4262 | node->traverse(this); |
| 4263 | } |
| 4264 | } |
| 4265 | |
| 4266 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 4267 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4268 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4269 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4270 | currentFunction = functionMap[node->getName().c_str()]; |
| 4271 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4272 | builder.setBuildPoint(functionBlock); |
| 4273 | } |
| 4274 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4275 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4276 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4277 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4278 | |
| 4279 | glslang::TSampler sampler = {}; |
| 4280 | bool cubeCompare = false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4281 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4282 | bool f16ShadowCompare = false; |
| 4283 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4284 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4285 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 4286 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4287 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4288 | f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 4289 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4290 | } |
| 4291 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4292 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 4293 | builder.clearAccessChain(); |
| 4294 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4295 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4296 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4297 | // Special case l-value operands |
| 4298 | bool lvalue = false; |
| 4299 | switch (node.getOp()) { |
| 4300 | case glslang::EOpImageAtomicAdd: |
| 4301 | case glslang::EOpImageAtomicMin: |
| 4302 | case glslang::EOpImageAtomicMax: |
| 4303 | case glslang::EOpImageAtomicAnd: |
| 4304 | case glslang::EOpImageAtomicOr: |
| 4305 | case glslang::EOpImageAtomicXor: |
| 4306 | case glslang::EOpImageAtomicExchange: |
| 4307 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4308 | case glslang::EOpImageAtomicLoad: |
| 4309 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4310 | if (i == 0) |
| 4311 | lvalue = true; |
| 4312 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4313 | case glslang::EOpSparseImageLoad: |
| 4314 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 4315 | lvalue = true; |
| 4316 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4317 | case glslang::EOpSparseTexture: |
| 4318 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 4319 | lvalue = true; |
| 4320 | break; |
| 4321 | case glslang::EOpSparseTextureClamp: |
| 4322 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 4323 | lvalue = true; |
| 4324 | break; |
| 4325 | case glslang::EOpSparseTextureLod: |
| 4326 | case glslang::EOpSparseTextureOffset: |
| 4327 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 4328 | lvalue = true; |
| 4329 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4330 | case glslang::EOpSparseTextureFetch: |
| 4331 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 4332 | lvalue = true; |
| 4333 | break; |
| 4334 | case glslang::EOpSparseTextureFetchOffset: |
| 4335 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 4336 | lvalue = true; |
| 4337 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4338 | case glslang::EOpSparseTextureLodOffset: |
| 4339 | case glslang::EOpSparseTextureGrad: |
| 4340 | case glslang::EOpSparseTextureOffsetClamp: |
| 4341 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 4342 | lvalue = true; |
| 4343 | break; |
| 4344 | case glslang::EOpSparseTextureGradOffset: |
| 4345 | case glslang::EOpSparseTextureGradClamp: |
| 4346 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 4347 | lvalue = true; |
| 4348 | break; |
| 4349 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 4350 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 4351 | lvalue = true; |
| 4352 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4353 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4354 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 4355 | lvalue = true; |
| 4356 | break; |
| 4357 | case glslang::EOpSparseTextureGatherOffset: |
| 4358 | case glslang::EOpSparseTextureGatherOffsets: |
| 4359 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 4360 | lvalue = true; |
| 4361 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4362 | case glslang::EOpSparseTextureGatherLod: |
| 4363 | if (i == 3) |
| 4364 | lvalue = true; |
| 4365 | break; |
| 4366 | case glslang::EOpSparseTextureGatherLodOffset: |
| 4367 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 4368 | if (i == 4) |
| 4369 | lvalue = true; |
| 4370 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4371 | case glslang::EOpSparseImageLoadLod: |
| 4372 | if (i == 3) |
| 4373 | lvalue = true; |
| 4374 | break; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4375 | case glslang::EOpImageSampleFootprintNV: |
| 4376 | if (i == 4) |
| 4377 | lvalue = true; |
| 4378 | break; |
| 4379 | case glslang::EOpImageSampleFootprintClampNV: |
| 4380 | case glslang::EOpImageSampleFootprintLodNV: |
| 4381 | if (i == 5) |
| 4382 | lvalue = true; |
| 4383 | break; |
| 4384 | case glslang::EOpImageSampleFootprintGradNV: |
| 4385 | if (i == 6) |
| 4386 | lvalue = true; |
| 4387 | break; |
| 4388 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 4389 | if (i == 7) |
| 4390 | lvalue = true; |
| 4391 | break; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4392 | default: |
| 4393 | break; |
| 4394 | } |
| 4395 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4396 | if (lvalue) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4397 | arguments.push_back(builder.accessChainGetLValue()); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4398 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 4399 | lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
| 4400 | } else |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4401 | #endif |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4402 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4406 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4407 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4408 | builder.clearAccessChain(); |
| 4409 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4410 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4411 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4412 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4413 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 4414 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4415 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4416 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4417 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 4418 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4419 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4420 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4421 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4422 | const glslang::TType &imageType = node->getAsAggregate() |
| 4423 | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 4424 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4425 | const glslang::TSampler sampler = imageType.getSampler(); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4426 | #ifdef GLSLANG_WEB |
| 4427 | const bool f16ShadowCompare = false; |
| 4428 | #else |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4429 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4430 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 4431 | : false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4432 | #endif |
| 4433 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4434 | const auto signExtensionMask = [&]() { |
| 4435 | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
| 4436 | if (sampler.type == glslang::EbtUint) |
| 4437 | return spv::ImageOperandsZeroExtendMask; |
| 4438 | else if (sampler.type == glslang::EbtInt) |
| 4439 | return spv::ImageOperandsSignExtendMask; |
| 4440 | } |
| 4441 | return spv::ImageOperandsMaskNone; |
| 4442 | }; |
| 4443 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4444 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 4445 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4446 | std::vector<spv::Id> arguments; |
| 4447 | if (node->getAsAggregate()) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4448 | translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4449 | else |
| 4450 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 4451 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4452 | |
| 4453 | spv::Builder::TextureParameters params = { }; |
| 4454 | params.sampler = arguments[0]; |
| 4455 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4456 | glslang::TCrackedTextureOp cracked; |
| 4457 | node->crackTexture(sampler, cracked); |
| 4458 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4459 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4460 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4461 | // Check for queries |
| 4462 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4463 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 4464 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 4465 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4466 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4467 | switch (node->getOp()) { |
| 4468 | case glslang::EOpImageQuerySize: |
| 4469 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4470 | if (arguments.size() > 1) { |
| 4471 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4472 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4473 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4474 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4475 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4476 | case glslang::EOpImageQuerySamples: |
| 4477 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4478 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4479 | case glslang::EOpTextureQueryLod: |
| 4480 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4481 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4482 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4483 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4484 | case glslang::EOpSparseTexelsResident: |
| 4485 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4486 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4487 | default: |
| 4488 | assert(0); |
| 4489 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4490 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4491 | } |
| 4492 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4493 | int components = node->getType().getVectorSize(); |
| 4494 | |
| 4495 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 4496 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 4497 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 4498 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 4499 | // here around e.g. which ones return scalars or other types. |
| 4500 | components = 4; |
| 4501 | } |
| 4502 | |
| 4503 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 4504 | |
| 4505 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 4506 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4507 | // Check for image functions other than queries |
| 4508 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4509 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4510 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4511 | spv::IdImmediate image = { true, *(opIt++) }; |
| 4512 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4513 | |
| 4514 | // Handle subpass operations |
| 4515 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 4516 | // built-in function. |
| 4517 | if (cracked.subpass) { |
| 4518 | // add on the (0,0) coordinate |
| 4519 | spv::Id zero = builder.makeIntConstant(0); |
| 4520 | std::vector<spv::Id> comps; |
| 4521 | comps.push_back(zero); |
| 4522 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4523 | spv::IdImmediate coord = { true, |
| 4524 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 4525 | operands.push_back(coord); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4526 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
| 4527 | imageOperands.word = imageOperands.word | signExtensionMask(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4528 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4529 | imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
| 4530 | } |
| 4531 | if (imageOperands.word != spv::ImageOperandsMaskNone) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4532 | operands.push_back(imageOperands); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4533 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4534 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 4535 | operands.push_back(imageOperand); |
| 4536 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4537 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4538 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 4539 | builder.setPrecision(result, precision); |
| 4540 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4541 | } |
| 4542 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4543 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 4544 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4545 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4546 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4547 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4548 | mask = mask | spv::ImageOperandsSampleMask; |
| 4549 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4550 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4551 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4552 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4553 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4554 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4555 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4556 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4557 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4558 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4559 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4560 | operands.push_back(imageOperands); |
| 4561 | } |
| 4562 | if (mask & spv::ImageOperandsSampleMask) { |
| 4563 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4564 | operands.push_back(imageOperand); |
| 4565 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4566 | if (mask & spv::ImageOperandsLodMask) { |
| 4567 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4568 | operands.push_back(imageOperand); |
| 4569 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4570 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4571 | spv::IdImmediate imageOperand = { true, |
| 4572 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4573 | operands.push_back(imageOperand); |
| 4574 | } |
| 4575 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4576 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4577 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4578 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4579 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4580 | builder.setPrecision(result[0], precision); |
| 4581 | |
| 4582 | // If needed, add a conversion constructor to the proper size. |
| 4583 | if (components != node->getType().getVectorSize()) |
| 4584 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4585 | |
| 4586 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4587 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4588 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4589 | // Push the texel value before the operands |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4590 | if (sampler.isMultiSample() || cracked.lod) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4591 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4592 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4593 | } else { |
| 4594 | spv::IdImmediate texel = { true, *opIt }; |
| 4595 | operands.push_back(texel); |
| 4596 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4597 | |
| 4598 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4599 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4600 | mask = mask | spv::ImageOperandsSampleMask; |
| 4601 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4602 | if (cracked.lod) { |
| 4603 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4604 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4605 | mask = mask | spv::ImageOperandsLodMask; |
| 4606 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4607 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4608 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4609 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4610 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4611 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4612 | operands.push_back(imageOperands); |
| 4613 | } |
| 4614 | if (mask & spv::ImageOperandsSampleMask) { |
| 4615 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4616 | operands.push_back(imageOperand); |
| 4617 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4618 | if (mask & spv::ImageOperandsLodMask) { |
| 4619 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4620 | operands.push_back(imageOperand); |
| 4621 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4622 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4623 | spv::IdImmediate imageOperand = { true, |
| 4624 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4625 | operands.push_back(imageOperand); |
| 4626 | } |
| 4627 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4628 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4629 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4630 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4631 | return spv::NoResult; |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4632 | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
| 4633 | node->getOp() == glslang::EOpSparseImageLoadLod) { |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4634 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4635 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4636 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4637 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4638 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4639 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4640 | mask = mask | spv::ImageOperandsSampleMask; |
| 4641 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4642 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4643 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4644 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4645 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4646 | mask = mask | spv::ImageOperandsLodMask; |
| 4647 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4648 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4649 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4650 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4651 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4652 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4653 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4654 | } |
| 4655 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4656 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4657 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4658 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4659 | if (mask & spv::ImageOperandsLodMask) { |
| 4660 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4661 | operands.push_back(imageOperand); |
| 4662 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4663 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4664 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4665 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | // Create the return type that was a special structure |
| 4669 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4670 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4671 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 4672 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 4673 | |
| 4674 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 4675 | |
| 4676 | // Decode the return type |
| 4677 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 4678 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 4679 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 4680 | // Process image atomic operations |
| 4681 | |
| 4682 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 4683 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4684 | // For non-MS, the sample value should be 0 |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4685 | spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4686 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4687 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4688 | spv::Id resultTypeId; |
| 4689 | // imageAtomicStore has a void return type so base the pointer type on |
| 4690 | // the type of the value operand. |
| 4691 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 4692 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(operands[2].word)); |
| 4693 | } else { |
| 4694 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 4695 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4696 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4697 | |
| 4698 | std::vector<spv::Id> operands; |
| 4699 | operands.push_back(pointer); |
| 4700 | for (; opIt != arguments.end(); ++opIt) |
| 4701 | operands.push_back(*opIt); |
| 4702 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4703 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), lvalueCoherentFlags); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4704 | } |
| 4705 | } |
| 4706 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4707 | #ifndef GLSLANG_WEB |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4708 | // Check for fragment mask functions other than queries |
| 4709 | if (cracked.fragMask) { |
| 4710 | assert(sampler.ms); |
| 4711 | |
| 4712 | auto opIt = arguments.begin(); |
| 4713 | std::vector<spv::Id> operands; |
| 4714 | |
| 4715 | // Extract the image if necessary |
| 4716 | if (builder.isSampledImage(params.sampler)) |
| 4717 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4718 | |
| 4719 | operands.push_back(params.sampler); |
| 4720 | ++opIt; |
| 4721 | |
| 4722 | if (sampler.isSubpass()) { |
| 4723 | // add on the (0,0) coordinate |
| 4724 | spv::Id zero = builder.makeIntConstant(0); |
| 4725 | std::vector<spv::Id> comps; |
| 4726 | comps.push_back(zero); |
| 4727 | comps.push_back(zero); |
| 4728 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 4729 | } |
| 4730 | |
| 4731 | for (; opIt != arguments.end(); ++opIt) |
| 4732 | operands.push_back(*opIt); |
| 4733 | |
| 4734 | spv::Op fragMaskOp = spv::OpNop; |
| 4735 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 4736 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 4737 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 4738 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 4739 | |
| 4740 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 4741 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 4742 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 4743 | } |
| 4744 | #endif |
| 4745 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4746 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4747 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4748 | bool imageFootprint = node->isImageFootprint(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4749 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 4750 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4751 | // check for bias argument |
| 4752 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4753 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4754 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4755 | if (cracked.gather) |
| 4756 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4757 | |
| 4758 | if (f16ShadowCompare) |
| 4759 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4760 | if (cracked.offset) |
| 4761 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4762 | else if (cracked.offsets) |
| 4763 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4764 | if (cracked.grad) |
| 4765 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4766 | if (cracked.lodClamp) |
| 4767 | ++nonBiasArgCount; |
| 4768 | if (sparse) |
| 4769 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4770 | if (imageFootprint) |
| 4771 | //Following three extra arguments |
| 4772 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4773 | nonBiasArgCount += 3; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4774 | if ((int)arguments.size() > nonBiasArgCount) |
| 4775 | bias = true; |
| 4776 | } |
| 4777 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 4778 | // See if the sampler param should really be just the SPV image part |
| 4779 | if (cracked.fetch) { |
| 4780 | // a fetch needs to have the image extracted first |
| 4781 | if (builder.isSampledImage(params.sampler)) |
| 4782 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4783 | } |
| 4784 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4785 | #ifndef GLSLANG_WEB |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4786 | if (cracked.gather) { |
| 4787 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 4788 | if (bias || cracked.lod || |
| 4789 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 4790 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 4791 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4792 | } |
| 4793 | } |
| 4794 | #endif |
| 4795 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4796 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4797 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4798 | params.coords = arguments[1]; |
| 4799 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4800 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4801 | |
| 4802 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4803 | if (cubeCompare || f16ShadowCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4804 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4805 | ++extraArgs; |
| 4806 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4807 | params.Dref = arguments[2]; |
| 4808 | ++extraArgs; |
| 4809 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4810 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4811 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4812 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4813 | 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] | 4814 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4815 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 4816 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4817 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 4818 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4819 | |
| 4820 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4821 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4822 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4823 | ++extraArgs; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4824 | } else if (glslangIntermediate->getStage() != EShLangFragment && |
| 4825 | !(glslangIntermediate->getStage() == EShLangCompute && |
| 4826 | glslangIntermediate->hasLayoutDerivativeModeNone())) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 4827 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 4828 | noImplicitLod = true; |
| 4829 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4830 | |
| 4831 | // multisample |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4832 | if (sampler.isMultiSample()) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 4833 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4834 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4835 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4836 | |
| 4837 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4838 | if (cracked.grad) { |
| 4839 | params.gradX = arguments[2 + extraArgs]; |
| 4840 | params.gradY = arguments[3 + extraArgs]; |
| 4841 | extraArgs += 2; |
| 4842 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4843 | |
| 4844 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4845 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4846 | params.offset = arguments[2 + extraArgs]; |
| 4847 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4848 | } else if (cracked.offsets) { |
| 4849 | params.offsets = arguments[2 + extraArgs]; |
| 4850 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4851 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4852 | |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4853 | #ifndef GLSLANG_WEB |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4854 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4855 | if (cracked.lodClamp) { |
| 4856 | params.lodClamp = arguments[2 + extraArgs]; |
| 4857 | ++extraArgs; |
| 4858 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4859 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4860 | if (sparse) { |
| 4861 | params.texelOut = arguments[2 + extraArgs]; |
| 4862 | ++extraArgs; |
| 4863 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4864 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4865 | if (cracked.gather && ! sampler.shadow) { |
| 4866 | // default component is 0, if missing, otherwise an argument |
| 4867 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4868 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4869 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4870 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 4871 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4872 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4873 | spv::Id resultStruct = spv::NoResult; |
| 4874 | if (imageFootprint) { |
| 4875 | //Following three extra arguments |
| 4876 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 4877 | params.granularity = arguments[2 + extraArgs]; |
| 4878 | params.coarse = arguments[3 + extraArgs]; |
| 4879 | resultStruct = arguments[4 + extraArgs]; |
| 4880 | extraArgs += 3; |
| 4881 | } |
| 4882 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4883 | // bias |
| 4884 | if (bias) { |
| 4885 | params.bias = arguments[2 + extraArgs]; |
| 4886 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4887 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4888 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4889 | #ifndef GLSLANG_WEB |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4890 | if (imageFootprint) { |
| 4891 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 4892 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 4893 | |
| 4894 | |
| 4895 | //resultStructType(OpenGL type) contains 5 elements: |
| 4896 | //struct gl_TextureFootprint2DNV { |
| 4897 | // uvec2 anchor; |
| 4898 | // uvec2 offset; |
| 4899 | // uvec2 mask; |
| 4900 | // uint lod; |
| 4901 | // uint granularity; |
| 4902 | //}; |
| 4903 | //or |
| 4904 | //struct gl_TextureFootprint3DNV { |
| 4905 | // uvec3 anchor; |
| 4906 | // uvec3 offset; |
| 4907 | // uvec2 mask; |
| 4908 | // uint lod; |
| 4909 | // uint granularity; |
| 4910 | //}; |
| 4911 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 4912 | assert(builder.isStructType(resultStructType)); |
| 4913 | |
| 4914 | //resType (SPIR-V type) contains 6 elements: |
| 4915 | //Member 0 must be a Boolean type scalar(LOD), |
| 4916 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 4917 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 4918 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 4919 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 4920 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 4921 | std::vector<spv::Id> members; |
| 4922 | members.push_back(resultType()); |
| 4923 | for (int i = 0; i < 5; i++) { |
| 4924 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 4925 | } |
| 4926 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 4927 | |
| 4928 | //call ImageFootprintNV |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4929 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
| 4930 | cracked.gather, noImplicitLod, params, signExtensionMask()); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4931 | |
| 4932 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 4933 | for (int i = 0; i < 5; i++) { |
| 4934 | builder.clearAccessChain(); |
| 4935 | builder.setAccessChainLValue(resultStruct); |
| 4936 | |
| 4937 | //Accessing to a struct we created, no coherent flag is set |
| 4938 | spv::Builder::AccessChain::CoherentFlags flags; |
| 4939 | flags.clear(); |
| 4940 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4941 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4942 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), i+1)); |
| 4943 | } |
| 4944 | return builder.createCompositeExtract(res, resultType(), 0); |
| 4945 | } |
| 4946 | #endif |
| 4947 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4948 | // projective component (might not to move) |
| 4949 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 4950 | // are divided by the last component of P." |
| 4951 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 4952 | // unused components will appear after all used components." |
| 4953 | if (cracked.proj) { |
| 4954 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 4955 | int projTargetComp; |
| 4956 | switch (sampler.dim) { |
| 4957 | case glslang::Esd1D: projTargetComp = 1; break; |
| 4958 | case glslang::Esd2D: projTargetComp = 2; break; |
| 4959 | case glslang::EsdRect: projTargetComp = 2; break; |
| 4960 | default: projTargetComp = projSourceComp; break; |
| 4961 | } |
| 4962 | // copy the projective coordinate if we have to |
| 4963 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 4964 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 4965 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 4966 | projSourceComp); |
| 4967 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 4968 | builder.getTypeId(params.coords), projTargetComp); |
| 4969 | } |
| 4970 | } |
| 4971 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4972 | // nonprivate |
| 4973 | if (imageType.getQualifier().nonprivate) { |
| 4974 | params.nonprivate = true; |
| 4975 | } |
| 4976 | |
| 4977 | // volatile |
| 4978 | if (imageType.getQualifier().volatil) { |
| 4979 | params.volatil = true; |
| 4980 | } |
| 4981 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4982 | std::vector<spv::Id> result( 1, |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4983 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
| 4984 | noImplicitLod, params, signExtensionMask()) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 4985 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4986 | |
| 4987 | if (components != node->getType().getVectorSize()) |
| 4988 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4989 | |
| 4990 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 4994 | { |
| 4995 | // Grab the function's pointer from the previously created function |
| 4996 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 4997 | if (! function) |
| 4998 | return 0; |
| 4999 | |
| 5000 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 5001 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 5002 | |
| 5003 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 5004 | // |
| 5005 | // These imply we need a four step process: |
| 5006 | // 1. Evaluate the arguments |
| 5007 | // 2. Allocate and make copies of in, out, and inout arguments |
| 5008 | // 3. Make the call |
| 5009 | // 4. Copy back the results |
| 5010 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5011 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5012 | std::vector<spv::Builder::AccessChain> lValues; |
| 5013 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5014 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5015 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5016 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5017 | // build l-value |
| 5018 | builder.clearAccessChain(); |
| 5019 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5020 | // 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] | 5021 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 5022 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5023 | // save l-value |
| 5024 | lValues.push_back(builder.getAccessChain()); |
| 5025 | } else { |
| 5026 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5027 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5028 | } |
| 5029 | } |
| 5030 | |
| 5031 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 5032 | // copy the original into that space. |
| 5033 | // |
| 5034 | // Also, build up the list of actual arguments to pass in for the call |
| 5035 | int lValueCount = 0; |
| 5036 | int rValueCount = 0; |
| 5037 | std::vector<spv::Id> spvArgs; |
| 5038 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 5039 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5040 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 5041 | builder.setAccessChain(lValues[lValueCount]); |
| 5042 | arg = builder.accessChainGetLValue(); |
| 5043 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5044 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5045 | // need space to hold the copy |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5046 | arg = builder.createVariable(spv::StorageClassFunction, builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5047 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 5048 | // need to copy the input into output space |
| 5049 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5050 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 5051 | builder.clearAccessChain(); |
| 5052 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5053 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5054 | } |
| 5055 | ++lValueCount; |
| 5056 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5057 | // process r-value, which involves a copy for a type mismatch |
| 5058 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a])) { |
| 5059 | spv::Id argCopy = builder.createVariable(spv::StorageClassFunction, function->getParamType(a), "arg"); |
| 5060 | builder.clearAccessChain(); |
| 5061 | builder.setAccessChainLValue(argCopy); |
| 5062 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 5063 | arg = builder.createLoad(argCopy); |
| 5064 | } else |
| 5065 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5066 | ++rValueCount; |
| 5067 | } |
| 5068 | spvArgs.push_back(arg); |
| 5069 | } |
| 5070 | |
| 5071 | // 3. Make the call. |
| 5072 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5073 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5074 | |
| 5075 | // 4. Copy back out an "out" arguments. |
| 5076 | lValueCount = 0; |
| 5077 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5078 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5079 | ++lValueCount; |
| 5080 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5081 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 5082 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 5083 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5084 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5085 | } |
| 5086 | ++lValueCount; |
| 5087 | } |
| 5088 | } |
| 5089 | |
| 5090 | return result; |
| 5091 | } |
| 5092 | |
| 5093 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5094 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5095 | spv::Id typeId, spv::Id left, spv::Id right, |
| 5096 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 5097 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5098 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5099 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5100 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5101 | |
| 5102 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5103 | 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] | 5104 | bool comparison = false; |
| 5105 | |
| 5106 | switch (op) { |
| 5107 | case glslang::EOpAdd: |
| 5108 | case glslang::EOpAddAssign: |
| 5109 | if (isFloat) |
| 5110 | binOp = spv::OpFAdd; |
| 5111 | else |
| 5112 | binOp = spv::OpIAdd; |
| 5113 | break; |
| 5114 | case glslang::EOpSub: |
| 5115 | case glslang::EOpSubAssign: |
| 5116 | if (isFloat) |
| 5117 | binOp = spv::OpFSub; |
| 5118 | else |
| 5119 | binOp = spv::OpISub; |
| 5120 | break; |
| 5121 | case glslang::EOpMul: |
| 5122 | case glslang::EOpMulAssign: |
| 5123 | if (isFloat) |
| 5124 | binOp = spv::OpFMul; |
| 5125 | else |
| 5126 | binOp = spv::OpIMul; |
| 5127 | break; |
| 5128 | case glslang::EOpVectorTimesScalar: |
| 5129 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5130 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5131 | if (builder.isVector(right)) |
| 5132 | std::swap(left, right); |
| 5133 | assert(builder.isScalar(right)); |
| 5134 | needMatchingVectors = false; |
| 5135 | binOp = spv::OpVectorTimesScalar; |
t.jung | 697fdf0 | 2018-11-14 13:04:39 +0100 | [diff] [blame] | 5136 | } else if (isFloat) |
| 5137 | binOp = spv::OpFMul; |
| 5138 | else |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5139 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5140 | break; |
| 5141 | case glslang::EOpVectorTimesMatrix: |
| 5142 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5143 | binOp = spv::OpVectorTimesMatrix; |
| 5144 | break; |
| 5145 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5146 | binOp = spv::OpMatrixTimesVector; |
| 5147 | break; |
| 5148 | case glslang::EOpMatrixTimesScalar: |
| 5149 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5150 | binOp = spv::OpMatrixTimesScalar; |
| 5151 | break; |
| 5152 | case glslang::EOpMatrixTimesMatrix: |
| 5153 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5154 | binOp = spv::OpMatrixTimesMatrix; |
| 5155 | break; |
| 5156 | case glslang::EOpOuterProduct: |
| 5157 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5158 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5159 | break; |
| 5160 | |
| 5161 | case glslang::EOpDiv: |
| 5162 | case glslang::EOpDivAssign: |
| 5163 | if (isFloat) |
| 5164 | binOp = spv::OpFDiv; |
| 5165 | else if (isUnsigned) |
| 5166 | binOp = spv::OpUDiv; |
| 5167 | else |
| 5168 | binOp = spv::OpSDiv; |
| 5169 | break; |
| 5170 | case glslang::EOpMod: |
| 5171 | case glslang::EOpModAssign: |
| 5172 | if (isFloat) |
| 5173 | binOp = spv::OpFMod; |
| 5174 | else if (isUnsigned) |
| 5175 | binOp = spv::OpUMod; |
| 5176 | else |
| 5177 | binOp = spv::OpSMod; |
| 5178 | break; |
| 5179 | case glslang::EOpRightShift: |
| 5180 | case glslang::EOpRightShiftAssign: |
| 5181 | if (isUnsigned) |
| 5182 | binOp = spv::OpShiftRightLogical; |
| 5183 | else |
| 5184 | binOp = spv::OpShiftRightArithmetic; |
| 5185 | break; |
| 5186 | case glslang::EOpLeftShift: |
| 5187 | case glslang::EOpLeftShiftAssign: |
| 5188 | binOp = spv::OpShiftLeftLogical; |
| 5189 | break; |
| 5190 | case glslang::EOpAnd: |
| 5191 | case glslang::EOpAndAssign: |
| 5192 | binOp = spv::OpBitwiseAnd; |
| 5193 | break; |
| 5194 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5195 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5196 | binOp = spv::OpLogicalAnd; |
| 5197 | break; |
| 5198 | case glslang::EOpInclusiveOr: |
| 5199 | case glslang::EOpInclusiveOrAssign: |
| 5200 | binOp = spv::OpBitwiseOr; |
| 5201 | break; |
| 5202 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5203 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5204 | binOp = spv::OpLogicalOr; |
| 5205 | break; |
| 5206 | case glslang::EOpExclusiveOr: |
| 5207 | case glslang::EOpExclusiveOrAssign: |
| 5208 | binOp = spv::OpBitwiseXor; |
| 5209 | break; |
| 5210 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5211 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5212 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5213 | break; |
| 5214 | |
| 5215 | case glslang::EOpLessThan: |
| 5216 | case glslang::EOpGreaterThan: |
| 5217 | case glslang::EOpLessThanEqual: |
| 5218 | case glslang::EOpGreaterThanEqual: |
| 5219 | case glslang::EOpEqual: |
| 5220 | case glslang::EOpNotEqual: |
| 5221 | case glslang::EOpVectorEqual: |
| 5222 | case glslang::EOpVectorNotEqual: |
| 5223 | comparison = true; |
| 5224 | break; |
| 5225 | default: |
| 5226 | break; |
| 5227 | } |
| 5228 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5229 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5230 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5231 | assert(comparison == false); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5232 | if (builder.isMatrix(left) || builder.isMatrix(right) || |
| 5233 | builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5234 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5235 | |
| 5236 | // 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] | 5237 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5238 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5239 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5240 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5241 | decorations.addNoContraction(builder, result); |
| 5242 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5243 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | if (! comparison) |
| 5247 | return 0; |
| 5248 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5249 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5250 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 5251 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5252 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 5253 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5254 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5255 | return result; |
| 5256 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5257 | |
| 5258 | switch (op) { |
| 5259 | case glslang::EOpLessThan: |
| 5260 | if (isFloat) |
| 5261 | binOp = spv::OpFOrdLessThan; |
| 5262 | else if (isUnsigned) |
| 5263 | binOp = spv::OpULessThan; |
| 5264 | else |
| 5265 | binOp = spv::OpSLessThan; |
| 5266 | break; |
| 5267 | case glslang::EOpGreaterThan: |
| 5268 | if (isFloat) |
| 5269 | binOp = spv::OpFOrdGreaterThan; |
| 5270 | else if (isUnsigned) |
| 5271 | binOp = spv::OpUGreaterThan; |
| 5272 | else |
| 5273 | binOp = spv::OpSGreaterThan; |
| 5274 | break; |
| 5275 | case glslang::EOpLessThanEqual: |
| 5276 | if (isFloat) |
| 5277 | binOp = spv::OpFOrdLessThanEqual; |
| 5278 | else if (isUnsigned) |
| 5279 | binOp = spv::OpULessThanEqual; |
| 5280 | else |
| 5281 | binOp = spv::OpSLessThanEqual; |
| 5282 | break; |
| 5283 | case glslang::EOpGreaterThanEqual: |
| 5284 | if (isFloat) |
| 5285 | binOp = spv::OpFOrdGreaterThanEqual; |
| 5286 | else if (isUnsigned) |
| 5287 | binOp = spv::OpUGreaterThanEqual; |
| 5288 | else |
| 5289 | binOp = spv::OpSGreaterThanEqual; |
| 5290 | break; |
| 5291 | case glslang::EOpEqual: |
| 5292 | case glslang::EOpVectorEqual: |
| 5293 | if (isFloat) |
| 5294 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5295 | else if (isBool) |
| 5296 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5297 | else |
| 5298 | binOp = spv::OpIEqual; |
| 5299 | break; |
| 5300 | case glslang::EOpNotEqual: |
| 5301 | case glslang::EOpVectorNotEqual: |
| 5302 | if (isFloat) |
| 5303 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5304 | else if (isBool) |
| 5305 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5306 | else |
| 5307 | binOp = spv::OpINotEqual; |
| 5308 | break; |
| 5309 | default: |
| 5310 | break; |
| 5311 | } |
| 5312 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5313 | if (binOp != spv::OpNop) { |
| 5314 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5315 | decorations.addNoContraction(builder, result); |
| 5316 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5317 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5318 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5319 | |
| 5320 | return 0; |
| 5321 | } |
| 5322 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5323 | // |
| 5324 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 5325 | // These can be any of: |
| 5326 | // |
| 5327 | // matrix * scalar |
| 5328 | // scalar * matrix |
| 5329 | // matrix * matrix linear algebraic |
| 5330 | // matrix * vector |
| 5331 | // vector * matrix |
| 5332 | // matrix * matrix componentwise |
| 5333 | // matrix op matrix op in {+, -, /} |
| 5334 | // matrix op scalar op in {+, -, /} |
| 5335 | // scalar op matrix op in {+, -, /} |
| 5336 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5337 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5338 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5339 | { |
| 5340 | bool firstClass = true; |
| 5341 | |
| 5342 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 5343 | switch (op) { |
| 5344 | case spv::OpFDiv: |
| 5345 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 5346 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 5347 | spv::Id resultType = builder.getTypeId(right); |
| 5348 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5349 | op = spv::OpMatrixTimesScalar; |
| 5350 | } else |
| 5351 | firstClass = false; |
| 5352 | break; |
| 5353 | case spv::OpMatrixTimesScalar: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5354 | if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5355 | std::swap(left, right); |
| 5356 | assert(builder.isScalar(right)); |
| 5357 | break; |
| 5358 | case spv::OpVectorTimesMatrix: |
| 5359 | assert(builder.isVector(left)); |
| 5360 | assert(builder.isMatrix(right)); |
| 5361 | break; |
| 5362 | case spv::OpMatrixTimesVector: |
| 5363 | assert(builder.isMatrix(left)); |
| 5364 | assert(builder.isVector(right)); |
| 5365 | break; |
| 5366 | case spv::OpMatrixTimesMatrix: |
| 5367 | assert(builder.isMatrix(left)); |
| 5368 | assert(builder.isMatrix(right)); |
| 5369 | break; |
| 5370 | default: |
| 5371 | firstClass = false; |
| 5372 | break; |
| 5373 | } |
| 5374 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5375 | if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 5376 | firstClass = true; |
| 5377 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5378 | if (firstClass) { |
| 5379 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5380 | decorations.addNoContraction(builder, result); |
| 5381 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5382 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5383 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5384 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5385 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5386 | // The result type of all of them is the same type as the (a) matrix operand. |
| 5387 | // The algorithm is to: |
| 5388 | // - break the matrix(es) into vectors |
| 5389 | // - smear any scalar to a vector |
| 5390 | // - do vector operations |
| 5391 | // - make a matrix out the vector results |
| 5392 | switch (op) { |
| 5393 | case spv::OpFAdd: |
| 5394 | case spv::OpFSub: |
| 5395 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5396 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5397 | case spv::OpFMul: |
| 5398 | { |
| 5399 | // one time set up... |
| 5400 | bool leftMat = builder.isMatrix(left); |
| 5401 | bool rightMat = builder.isMatrix(right); |
| 5402 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 5403 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 5404 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 5405 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 5406 | std::vector<spv::Id> results; |
| 5407 | spv::Id smearVec = spv::NoResult; |
| 5408 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5409 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5410 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5411 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5412 | |
| 5413 | // do each vector op |
| 5414 | for (unsigned int c = 0; c < numCols; ++c) { |
| 5415 | std::vector<unsigned int> indexes; |
| 5416 | indexes.push_back(c); |
| 5417 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 5418 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5419 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5420 | decorations.addNoContraction(builder, result); |
| 5421 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5422 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5423 | } |
| 5424 | |
| 5425 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5426 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5427 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5428 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5429 | } |
| 5430 | default: |
| 5431 | assert(0); |
| 5432 | return spv::NoResult; |
| 5433 | } |
| 5434 | } |
| 5435 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5436 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 5437 | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5438 | { |
| 5439 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5440 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5441 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5442 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5443 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5444 | |
| 5445 | switch (op) { |
| 5446 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5447 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5448 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5449 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5450 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5451 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5452 | unaryOp = spv::OpSNegate; |
| 5453 | break; |
| 5454 | |
| 5455 | case glslang::EOpLogicalNot: |
| 5456 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5457 | unaryOp = spv::OpLogicalNot; |
| 5458 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5459 | case glslang::EOpBitwiseNot: |
| 5460 | unaryOp = spv::OpNot; |
| 5461 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5462 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5463 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5464 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5465 | break; |
| 5466 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5467 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5468 | break; |
| 5469 | case glslang::EOpTranspose: |
| 5470 | unaryOp = spv::OpTranspose; |
| 5471 | break; |
| 5472 | |
| 5473 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5474 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5475 | break; |
| 5476 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5477 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5478 | break; |
| 5479 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5480 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5481 | break; |
| 5482 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5483 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5484 | break; |
| 5485 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5486 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5487 | break; |
| 5488 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5489 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5490 | break; |
| 5491 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5492 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5493 | break; |
| 5494 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5495 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5496 | break; |
| 5497 | |
| 5498 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5499 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5500 | break; |
| 5501 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5502 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5503 | break; |
| 5504 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5505 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5506 | break; |
| 5507 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5508 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5509 | break; |
| 5510 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5511 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5512 | break; |
| 5513 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5514 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5515 | break; |
| 5516 | |
| 5517 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5518 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5519 | break; |
| 5520 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5521 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5522 | break; |
| 5523 | |
| 5524 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5525 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5526 | break; |
| 5527 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5528 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5529 | break; |
| 5530 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5531 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5532 | break; |
| 5533 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5534 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5535 | break; |
| 5536 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5537 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5538 | break; |
| 5539 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5540 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5541 | break; |
| 5542 | |
| 5543 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5544 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5545 | break; |
| 5546 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5547 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5548 | break; |
| 5549 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5550 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5551 | break; |
| 5552 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5553 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5554 | break; |
| 5555 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5556 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5557 | break; |
| 5558 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5559 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5560 | break; |
| 5561 | |
| 5562 | case glslang::EOpIsNan: |
| 5563 | unaryOp = spv::OpIsNan; |
| 5564 | break; |
| 5565 | case glslang::EOpIsInf: |
| 5566 | unaryOp = spv::OpIsInf; |
| 5567 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5568 | case glslang::EOpIsFinite: |
| 5569 | unaryOp = spv::OpIsFinite; |
| 5570 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5571 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5572 | case glslang::EOpFloatBitsToInt: |
| 5573 | case glslang::EOpFloatBitsToUint: |
| 5574 | case glslang::EOpIntBitsToFloat: |
| 5575 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5576 | case glslang::EOpDoubleBitsToInt64: |
| 5577 | case glslang::EOpDoubleBitsToUint64: |
| 5578 | case glslang::EOpInt64BitsToDouble: |
| 5579 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5580 | case glslang::EOpFloat16BitsToInt16: |
| 5581 | case glslang::EOpFloat16BitsToUint16: |
| 5582 | case glslang::EOpInt16BitsToFloat16: |
| 5583 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5584 | unaryOp = spv::OpBitcast; |
| 5585 | break; |
| 5586 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5587 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5588 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5589 | break; |
| 5590 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5591 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5592 | break; |
| 5593 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5594 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5595 | break; |
| 5596 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5597 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5598 | break; |
| 5599 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5600 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5601 | break; |
| 5602 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5603 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5604 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5605 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5606 | case glslang::EOpPackSnorm4x8: |
| 5607 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 5608 | break; |
| 5609 | case glslang::EOpUnpackSnorm4x8: |
| 5610 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5611 | break; |
| 5612 | case glslang::EOpPackUnorm4x8: |
| 5613 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 5614 | break; |
| 5615 | case glslang::EOpUnpackUnorm4x8: |
| 5616 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5617 | break; |
| 5618 | case glslang::EOpPackDouble2x32: |
| 5619 | libCall = spv::GLSLstd450PackDouble2x32; |
| 5620 | break; |
| 5621 | case glslang::EOpUnpackDouble2x32: |
| 5622 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5623 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5624 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5625 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5626 | case glslang::EOpPackInt2x32: |
| 5627 | case glslang::EOpUnpackInt2x32: |
| 5628 | case glslang::EOpPackUint2x32: |
| 5629 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5630 | case glslang::EOpPack16: |
| 5631 | case glslang::EOpPack32: |
| 5632 | case glslang::EOpPack64: |
| 5633 | case glslang::EOpUnpack32: |
| 5634 | case glslang::EOpUnpack16: |
| 5635 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5636 | case glslang::EOpPackInt2x16: |
| 5637 | case glslang::EOpUnpackInt2x16: |
| 5638 | case glslang::EOpPackUint2x16: |
| 5639 | case glslang::EOpUnpackUint2x16: |
| 5640 | case glslang::EOpPackInt4x16: |
| 5641 | case glslang::EOpUnpackInt4x16: |
| 5642 | case glslang::EOpPackUint4x16: |
| 5643 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5644 | case glslang::EOpPackFloat2x16: |
| 5645 | case glslang::EOpUnpackFloat2x16: |
| 5646 | unaryOp = spv::OpBitcast; |
| 5647 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5648 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5649 | case glslang::EOpDPdx: |
| 5650 | unaryOp = spv::OpDPdx; |
| 5651 | break; |
| 5652 | case glslang::EOpDPdy: |
| 5653 | unaryOp = spv::OpDPdy; |
| 5654 | break; |
| 5655 | case glslang::EOpFwidth: |
| 5656 | unaryOp = spv::OpFwidth; |
| 5657 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5658 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5659 | case glslang::EOpAny: |
| 5660 | unaryOp = spv::OpAny; |
| 5661 | break; |
| 5662 | case glslang::EOpAll: |
| 5663 | unaryOp = spv::OpAll; |
| 5664 | break; |
| 5665 | |
| 5666 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5667 | if (isFloat) |
| 5668 | libCall = spv::GLSLstd450FAbs; |
| 5669 | else |
| 5670 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5671 | break; |
| 5672 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5673 | if (isFloat) |
| 5674 | libCall = spv::GLSLstd450FSign; |
| 5675 | else |
| 5676 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5677 | break; |
| 5678 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5679 | #ifndef GLSLANG_WEB |
| 5680 | case glslang::EOpDPdxFine: |
| 5681 | unaryOp = spv::OpDPdxFine; |
| 5682 | break; |
| 5683 | case glslang::EOpDPdyFine: |
| 5684 | unaryOp = spv::OpDPdyFine; |
| 5685 | break; |
| 5686 | case glslang::EOpFwidthFine: |
| 5687 | unaryOp = spv::OpFwidthFine; |
| 5688 | break; |
| 5689 | case glslang::EOpDPdxCoarse: |
| 5690 | unaryOp = spv::OpDPdxCoarse; |
| 5691 | break; |
| 5692 | case glslang::EOpDPdyCoarse: |
| 5693 | unaryOp = spv::OpDPdyCoarse; |
| 5694 | break; |
| 5695 | case glslang::EOpFwidthCoarse: |
| 5696 | unaryOp = spv::OpFwidthCoarse; |
| 5697 | break; |
| 5698 | case glslang::EOpInterpolateAtCentroid: |
| 5699 | if (typeProxy == glslang::EbtFloat16) |
| 5700 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 5701 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 5702 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5703 | case glslang::EOpAtomicCounterIncrement: |
| 5704 | case glslang::EOpAtomicCounterDecrement: |
| 5705 | case glslang::EOpAtomicCounter: |
| 5706 | { |
| 5707 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 5708 | std::vector<spv::Id> operands; |
| 5709 | operands.push_back(operand); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 5710 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5711 | } |
| 5712 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5713 | case glslang::EOpBitFieldReverse: |
| 5714 | unaryOp = spv::OpBitReverse; |
| 5715 | break; |
| 5716 | case glslang::EOpBitCount: |
| 5717 | unaryOp = spv::OpBitCount; |
| 5718 | break; |
| 5719 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5720 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5721 | break; |
| 5722 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5723 | if (isUnsigned) |
| 5724 | libCall = spv::GLSLstd450FindUMsb; |
| 5725 | else |
| 5726 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5727 | break; |
| 5728 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5729 | case glslang::EOpBallot: |
| 5730 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5731 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5732 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5733 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5734 | case glslang::EOpMinInvocations: |
| 5735 | case glslang::EOpMaxInvocations: |
| 5736 | case glslang::EOpAddInvocations: |
| 5737 | case glslang::EOpMinInvocationsNonUniform: |
| 5738 | case glslang::EOpMaxInvocationsNonUniform: |
| 5739 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5740 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5741 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5742 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5743 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5744 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5745 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5746 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5747 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5748 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5749 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5750 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5751 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5752 | { |
| 5753 | std::vector<spv::Id> operands; |
| 5754 | operands.push_back(operand); |
| 5755 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 5756 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5757 | case glslang::EOpSubgroupAll: |
| 5758 | case glslang::EOpSubgroupAny: |
| 5759 | case glslang::EOpSubgroupAllEqual: |
| 5760 | case glslang::EOpSubgroupBroadcastFirst: |
| 5761 | case glslang::EOpSubgroupBallot: |
| 5762 | case glslang::EOpSubgroupInverseBallot: |
| 5763 | case glslang::EOpSubgroupBallotBitCount: |
| 5764 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5765 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5766 | case glslang::EOpSubgroupBallotFindLSB: |
| 5767 | case glslang::EOpSubgroupBallotFindMSB: |
| 5768 | case glslang::EOpSubgroupAdd: |
| 5769 | case glslang::EOpSubgroupMul: |
| 5770 | case glslang::EOpSubgroupMin: |
| 5771 | case glslang::EOpSubgroupMax: |
| 5772 | case glslang::EOpSubgroupAnd: |
| 5773 | case glslang::EOpSubgroupOr: |
| 5774 | case glslang::EOpSubgroupXor: |
| 5775 | case glslang::EOpSubgroupInclusiveAdd: |
| 5776 | case glslang::EOpSubgroupInclusiveMul: |
| 5777 | case glslang::EOpSubgroupInclusiveMin: |
| 5778 | case glslang::EOpSubgroupInclusiveMax: |
| 5779 | case glslang::EOpSubgroupInclusiveAnd: |
| 5780 | case glslang::EOpSubgroupInclusiveOr: |
| 5781 | case glslang::EOpSubgroupInclusiveXor: |
| 5782 | case glslang::EOpSubgroupExclusiveAdd: |
| 5783 | case glslang::EOpSubgroupExclusiveMul: |
| 5784 | case glslang::EOpSubgroupExclusiveMin: |
| 5785 | case glslang::EOpSubgroupExclusiveMax: |
| 5786 | case glslang::EOpSubgroupExclusiveAnd: |
| 5787 | case glslang::EOpSubgroupExclusiveOr: |
| 5788 | case glslang::EOpSubgroupExclusiveXor: |
| 5789 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5790 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5791 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 5792 | std::vector<spv::Id> operands; |
| 5793 | operands.push_back(operand); |
| 5794 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 5795 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5796 | case glslang::EOpMbcnt: |
| 5797 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5798 | libCall = spv::MbcntAMD; |
| 5799 | break; |
| 5800 | |
| 5801 | case glslang::EOpCubeFaceIndex: |
| 5802 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5803 | libCall = spv::CubeFaceIndexAMD; |
| 5804 | break; |
| 5805 | |
| 5806 | case glslang::EOpCubeFaceCoord: |
| 5807 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 5808 | libCall = spv::CubeFaceCoordAMD; |
| 5809 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5810 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 5811 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 5812 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 5813 | case glslang::EOpConstructReference: |
| 5814 | unaryOp = spv::OpBitcast; |
| 5815 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5816 | #endif |
Jeff Bolz | 88220d5 | 2019-05-08 10:24:46 -0500 | [diff] [blame] | 5817 | |
| 5818 | case glslang::EOpCopyObject: |
| 5819 | unaryOp = spv::OpCopyObject; |
| 5820 | break; |
| 5821 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5822 | default: |
| 5823 | return 0; |
| 5824 | } |
| 5825 | |
| 5826 | spv::Id id; |
| 5827 | if (libCall >= 0) { |
| 5828 | std::vector<spv::Id> args; |
| 5829 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5830 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5831 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5832 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 5833 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5834 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5835 | decorations.addNoContraction(builder, id); |
| 5836 | decorations.addNonUniform(builder, id); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5837 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5838 | } |
| 5839 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5840 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5841 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5842 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5843 | { |
| 5844 | // Handle unary operations vector by vector. |
| 5845 | // The result type is the same type as the original type. |
| 5846 | // The algorithm is to: |
| 5847 | // - break the matrix into vectors |
| 5848 | // - apply the operation to each vector |
| 5849 | // - make a matrix out the vector results |
| 5850 | |
| 5851 | // get the types sorted out |
| 5852 | int numCols = builder.getNumColumns(operand); |
| 5853 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5854 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 5855 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5856 | std::vector<spv::Id> results; |
| 5857 | |
| 5858 | // do each vector op |
| 5859 | for (int c = 0; c < numCols; ++c) { |
| 5860 | std::vector<unsigned int> indexes; |
| 5861 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 5862 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 5863 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5864 | decorations.addNoContraction(builder, destVec); |
| 5865 | decorations.addNonUniform(builder, destVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5866 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5870 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5871 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5872 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5873 | } |
| 5874 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5875 | // For converting integers where both the bitwidth and the signedness could |
| 5876 | // change, but only do the width change here. The caller is still responsible |
| 5877 | // for the signedness conversion. |
| 5878 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5879 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5880 | // Get the result type width, based on the type to convert to. |
| 5881 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5882 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5883 | case glslang::EOpConvInt16ToUint8: |
| 5884 | case glslang::EOpConvIntToUint8: |
| 5885 | case glslang::EOpConvInt64ToUint8: |
| 5886 | case glslang::EOpConvUint16ToInt8: |
| 5887 | case glslang::EOpConvUintToInt8: |
| 5888 | case glslang::EOpConvUint64ToInt8: |
| 5889 | width = 8; |
| 5890 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5891 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5892 | case glslang::EOpConvIntToUint16: |
| 5893 | case glslang::EOpConvInt64ToUint16: |
| 5894 | case glslang::EOpConvUint8ToInt16: |
| 5895 | case glslang::EOpConvUintToInt16: |
| 5896 | case glslang::EOpConvUint64ToInt16: |
| 5897 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5898 | break; |
| 5899 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5900 | case glslang::EOpConvInt16ToUint: |
| 5901 | case glslang::EOpConvInt64ToUint: |
| 5902 | case glslang::EOpConvUint8ToInt: |
| 5903 | case glslang::EOpConvUint16ToInt: |
| 5904 | case glslang::EOpConvUint64ToInt: |
| 5905 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5906 | break; |
| 5907 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5908 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5909 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5910 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5911 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5912 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5913 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5914 | break; |
| 5915 | |
| 5916 | default: |
| 5917 | assert(false && "Default missing"); |
| 5918 | break; |
| 5919 | } |
| 5920 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5921 | // Get the conversion operation and result type, |
| 5922 | // based on the target width, but the source type. |
| 5923 | spv::Id type = spv::NoType; |
| 5924 | spv::Op convOp = spv::OpNop; |
| 5925 | switch(op) { |
| 5926 | case glslang::EOpConvInt8ToUint16: |
| 5927 | case glslang::EOpConvInt8ToUint: |
| 5928 | case glslang::EOpConvInt8ToUint64: |
| 5929 | case glslang::EOpConvInt16ToUint8: |
| 5930 | case glslang::EOpConvInt16ToUint: |
| 5931 | case glslang::EOpConvInt16ToUint64: |
| 5932 | case glslang::EOpConvIntToUint8: |
| 5933 | case glslang::EOpConvIntToUint16: |
| 5934 | case glslang::EOpConvIntToUint64: |
| 5935 | case glslang::EOpConvInt64ToUint8: |
| 5936 | case glslang::EOpConvInt64ToUint16: |
| 5937 | case glslang::EOpConvInt64ToUint: |
| 5938 | convOp = spv::OpSConvert; |
| 5939 | type = builder.makeIntType(width); |
| 5940 | break; |
| 5941 | default: |
| 5942 | convOp = spv::OpUConvert; |
| 5943 | type = builder.makeUintType(width); |
| 5944 | break; |
| 5945 | } |
| 5946 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5947 | if (vectorSize > 0) |
| 5948 | type = builder.makeVectorType(type, vectorSize); |
| 5949 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 5950 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5951 | } |
| 5952 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5953 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 5954 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5955 | { |
| 5956 | spv::Op convOp = spv::OpNop; |
| 5957 | spv::Id zero = 0; |
| 5958 | spv::Id one = 0; |
| 5959 | |
| 5960 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 5961 | |
| 5962 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5963 | case glslang::EOpConvIntToBool: |
| 5964 | case glslang::EOpConvUintToBool: |
| 5965 | zero = builder.makeUintConstant(0); |
| 5966 | zero = makeSmearedConstant(zero, vectorSize); |
| 5967 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5968 | case glslang::EOpConvFloatToBool: |
| 5969 | zero = builder.makeFloatConstant(0.0F); |
| 5970 | zero = makeSmearedConstant(zero, vectorSize); |
| 5971 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5972 | case glslang::EOpConvBoolToFloat: |
| 5973 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5974 | zero = builder.makeFloatConstant(0.0F); |
| 5975 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5976 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5977 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5978 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5979 | case glslang::EOpConvBoolToInt64: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5980 | #ifndef GLSLANG_WEB |
| 5981 | if (op == glslang::EOpConvBoolToInt64) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5982 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5983 | one = builder.makeInt64Constant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5984 | } else |
| 5985 | #endif |
| 5986 | { |
| 5987 | zero = builder.makeIntConstant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5988 | one = builder.makeIntConstant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5989 | } |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5990 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5991 | convOp = spv::OpSelect; |
| 5992 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5993 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5994 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5995 | case glslang::EOpConvBoolToUint64: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5996 | #ifndef GLSLANG_WEB |
| 5997 | if (op == glslang::EOpConvBoolToUint64) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5998 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5999 | one = builder.makeUint64Constant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6000 | } else |
| 6001 | #endif |
| 6002 | { |
| 6003 | zero = builder.makeUintConstant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6004 | one = builder.makeUintConstant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6005 | } |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6006 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6007 | convOp = spv::OpSelect; |
| 6008 | break; |
| 6009 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6010 | case glslang::EOpConvInt8ToFloat16: |
| 6011 | case glslang::EOpConvInt8ToFloat: |
| 6012 | case glslang::EOpConvInt8ToDouble: |
| 6013 | case glslang::EOpConvInt16ToFloat16: |
| 6014 | case glslang::EOpConvInt16ToFloat: |
| 6015 | case glslang::EOpConvInt16ToDouble: |
| 6016 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6017 | case glslang::EOpConvIntToFloat: |
| 6018 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6019 | case glslang::EOpConvInt64ToFloat: |
| 6020 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6021 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6022 | convOp = spv::OpConvertSToF; |
| 6023 | break; |
| 6024 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6025 | case glslang::EOpConvUint8ToFloat16: |
| 6026 | case glslang::EOpConvUint8ToFloat: |
| 6027 | case glslang::EOpConvUint8ToDouble: |
| 6028 | case glslang::EOpConvUint16ToFloat16: |
| 6029 | case glslang::EOpConvUint16ToFloat: |
| 6030 | case glslang::EOpConvUint16ToDouble: |
| 6031 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6032 | case glslang::EOpConvUintToFloat: |
| 6033 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6034 | case glslang::EOpConvUint64ToFloat: |
| 6035 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6036 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6037 | convOp = spv::OpConvertUToF; |
| 6038 | break; |
| 6039 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6040 | case glslang::EOpConvFloat16ToInt8: |
| 6041 | case glslang::EOpConvFloatToInt8: |
| 6042 | case glslang::EOpConvDoubleToInt8: |
| 6043 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6044 | case glslang::EOpConvFloatToInt16: |
| 6045 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6046 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6047 | case glslang::EOpConvFloatToInt: |
| 6048 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6049 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6050 | case glslang::EOpConvFloatToInt64: |
| 6051 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6052 | convOp = spv::OpConvertFToS; |
| 6053 | break; |
| 6054 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6055 | case glslang::EOpConvUint8ToInt8: |
| 6056 | case glslang::EOpConvInt8ToUint8: |
| 6057 | case glslang::EOpConvUint16ToInt16: |
| 6058 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6059 | case glslang::EOpConvUintToInt: |
| 6060 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6061 | case glslang::EOpConvUint64ToInt64: |
| 6062 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6063 | if (builder.isInSpecConstCodeGenMode()) { |
| 6064 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6065 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6066 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 6067 | zero = builder.makeUint8Constant(0); |
| 6068 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6069 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6070 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 6071 | zero = builder.makeUint64Constant(0); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6072 | } else |
| 6073 | #endif |
| 6074 | { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6075 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6076 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 6077 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6078 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6079 | // generating for OpSpecConstantOp instruction. |
| 6080 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6081 | } |
| 6082 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6083 | convOp = spv::OpBitcast; |
| 6084 | break; |
| 6085 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6086 | case glslang::EOpConvFloat16ToUint8: |
| 6087 | case glslang::EOpConvFloatToUint8: |
| 6088 | case glslang::EOpConvDoubleToUint8: |
| 6089 | case glslang::EOpConvFloat16ToUint16: |
| 6090 | case glslang::EOpConvFloatToUint16: |
| 6091 | case glslang::EOpConvDoubleToUint16: |
| 6092 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6093 | case glslang::EOpConvFloatToUint: |
| 6094 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6095 | case glslang::EOpConvFloatToUint64: |
| 6096 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6097 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6098 | convOp = spv::OpConvertFToU; |
| 6099 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6100 | |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6101 | #ifndef GLSLANG_WEB |
| 6102 | case glslang::EOpConvInt8ToBool: |
| 6103 | case glslang::EOpConvUint8ToBool: |
| 6104 | zero = builder.makeUint8Constant(0); |
| 6105 | zero = makeSmearedConstant(zero, vectorSize); |
| 6106 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6107 | case glslang::EOpConvInt16ToBool: |
| 6108 | case glslang::EOpConvUint16ToBool: |
| 6109 | zero = builder.makeUint16Constant(0); |
| 6110 | zero = makeSmearedConstant(zero, vectorSize); |
| 6111 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6112 | case glslang::EOpConvInt64ToBool: |
| 6113 | case glslang::EOpConvUint64ToBool: |
| 6114 | zero = builder.makeUint64Constant(0); |
| 6115 | zero = makeSmearedConstant(zero, vectorSize); |
| 6116 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6117 | case glslang::EOpConvDoubleToBool: |
| 6118 | zero = builder.makeDoubleConstant(0.0); |
| 6119 | zero = makeSmearedConstant(zero, vectorSize); |
| 6120 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 6121 | case glslang::EOpConvFloat16ToBool: |
| 6122 | zero = builder.makeFloat16Constant(0.0F); |
| 6123 | zero = makeSmearedConstant(zero, vectorSize); |
| 6124 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 6125 | case glslang::EOpConvBoolToDouble: |
| 6126 | convOp = spv::OpSelect; |
| 6127 | zero = builder.makeDoubleConstant(0.0); |
| 6128 | one = builder.makeDoubleConstant(1.0); |
| 6129 | break; |
| 6130 | case glslang::EOpConvBoolToFloat16: |
| 6131 | convOp = spv::OpSelect; |
| 6132 | zero = builder.makeFloat16Constant(0.0F); |
| 6133 | one = builder.makeFloat16Constant(1.0F); |
| 6134 | break; |
| 6135 | case glslang::EOpConvBoolToInt8: |
| 6136 | zero = builder.makeInt8Constant(0); |
| 6137 | one = builder.makeInt8Constant(1); |
| 6138 | convOp = spv::OpSelect; |
| 6139 | break; |
| 6140 | case glslang::EOpConvBoolToUint8: |
| 6141 | zero = builder.makeUint8Constant(0); |
| 6142 | one = builder.makeUint8Constant(1); |
| 6143 | convOp = spv::OpSelect; |
| 6144 | break; |
| 6145 | case glslang::EOpConvBoolToInt16: |
| 6146 | zero = builder.makeInt16Constant(0); |
| 6147 | one = builder.makeInt16Constant(1); |
| 6148 | convOp = spv::OpSelect; |
| 6149 | break; |
| 6150 | case glslang::EOpConvBoolToUint16: |
| 6151 | zero = builder.makeUint16Constant(0); |
| 6152 | one = builder.makeUint16Constant(1); |
| 6153 | convOp = spv::OpSelect; |
| 6154 | break; |
| 6155 | case glslang::EOpConvDoubleToFloat: |
| 6156 | case glslang::EOpConvFloatToDouble: |
| 6157 | case glslang::EOpConvDoubleToFloat16: |
| 6158 | case glslang::EOpConvFloat16ToDouble: |
| 6159 | case glslang::EOpConvFloatToFloat16: |
| 6160 | case glslang::EOpConvFloat16ToFloat: |
| 6161 | convOp = spv::OpFConvert; |
| 6162 | if (builder.isMatrixType(destType)) |
| 6163 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
| 6164 | break; |
| 6165 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6166 | case glslang::EOpConvInt8ToInt16: |
| 6167 | case glslang::EOpConvInt8ToInt: |
| 6168 | case glslang::EOpConvInt8ToInt64: |
| 6169 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6170 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6171 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6172 | case glslang::EOpConvIntToInt8: |
| 6173 | case glslang::EOpConvIntToInt16: |
| 6174 | case glslang::EOpConvIntToInt64: |
| 6175 | case glslang::EOpConvInt64ToInt8: |
| 6176 | case glslang::EOpConvInt64ToInt16: |
| 6177 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6178 | convOp = spv::OpSConvert; |
| 6179 | break; |
| 6180 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6181 | case glslang::EOpConvUint8ToUint16: |
| 6182 | case glslang::EOpConvUint8ToUint: |
| 6183 | case glslang::EOpConvUint8ToUint64: |
| 6184 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6185 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6186 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6187 | case glslang::EOpConvUintToUint8: |
| 6188 | case glslang::EOpConvUintToUint16: |
| 6189 | case glslang::EOpConvUintToUint64: |
| 6190 | case glslang::EOpConvUint64ToUint8: |
| 6191 | case glslang::EOpConvUint64ToUint16: |
| 6192 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6193 | convOp = spv::OpUConvert; |
| 6194 | break; |
| 6195 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6196 | case glslang::EOpConvInt8ToUint16: |
| 6197 | case glslang::EOpConvInt8ToUint: |
| 6198 | case glslang::EOpConvInt8ToUint64: |
| 6199 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6200 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6201 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6202 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6203 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6204 | case glslang::EOpConvIntToUint64: |
| 6205 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6206 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6207 | case glslang::EOpConvInt64ToUint: |
| 6208 | case glslang::EOpConvUint8ToInt16: |
| 6209 | case glslang::EOpConvUint8ToInt: |
| 6210 | case glslang::EOpConvUint8ToInt64: |
| 6211 | case glslang::EOpConvUint16ToInt8: |
| 6212 | case glslang::EOpConvUint16ToInt: |
| 6213 | case glslang::EOpConvUint16ToInt64: |
| 6214 | case glslang::EOpConvUintToInt8: |
| 6215 | case glslang::EOpConvUintToInt16: |
| 6216 | case glslang::EOpConvUintToInt64: |
| 6217 | case glslang::EOpConvUint64ToInt8: |
| 6218 | case glslang::EOpConvUint64ToInt16: |
| 6219 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6220 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6221 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6222 | |
| 6223 | if (builder.isInSpecConstCodeGenMode()) { |
| 6224 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6225 | switch(op) { |
| 6226 | case glslang::EOpConvInt16ToUint8: |
| 6227 | case glslang::EOpConvIntToUint8: |
| 6228 | case glslang::EOpConvInt64ToUint8: |
| 6229 | case glslang::EOpConvUint16ToInt8: |
| 6230 | case glslang::EOpConvUintToInt8: |
| 6231 | case glslang::EOpConvUint64ToInt8: |
| 6232 | zero = builder.makeUint8Constant(0); |
| 6233 | break; |
| 6234 | case glslang::EOpConvInt8ToUint16: |
| 6235 | case glslang::EOpConvIntToUint16: |
| 6236 | case glslang::EOpConvInt64ToUint16: |
| 6237 | case glslang::EOpConvUint8ToInt16: |
| 6238 | case glslang::EOpConvUintToInt16: |
| 6239 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6240 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6241 | break; |
| 6242 | case glslang::EOpConvInt8ToUint: |
| 6243 | case glslang::EOpConvInt16ToUint: |
| 6244 | case glslang::EOpConvInt64ToUint: |
| 6245 | case glslang::EOpConvUint8ToInt: |
| 6246 | case glslang::EOpConvUint16ToInt: |
| 6247 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6248 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6249 | break; |
| 6250 | case glslang::EOpConvInt8ToUint64: |
| 6251 | case glslang::EOpConvInt16ToUint64: |
| 6252 | case glslang::EOpConvIntToUint64: |
| 6253 | case glslang::EOpConvUint8ToInt64: |
| 6254 | case glslang::EOpConvUint16ToInt64: |
| 6255 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6256 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6257 | break; |
| 6258 | default: |
| 6259 | assert(false && "Default missing"); |
| 6260 | break; |
| 6261 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6262 | zero = makeSmearedConstant(zero, vectorSize); |
| 6263 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6264 | // generating for OpSpecConstantOp instruction. |
| 6265 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6266 | } |
| 6267 | // For normal run-time conversion instruction, use OpBitcast. |
| 6268 | convOp = spv::OpBitcast; |
| 6269 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6270 | case glslang::EOpConvUint64ToPtr: |
| 6271 | convOp = spv::OpConvertUToPtr; |
| 6272 | break; |
| 6273 | case glslang::EOpConvPtrToUint64: |
| 6274 | convOp = spv::OpConvertPtrToU; |
| 6275 | break; |
John Kessenich | 90e402f | 2019-09-17 23:19:38 -0600 | [diff] [blame] | 6276 | case glslang::EOpConvPtrToUvec2: |
| 6277 | case glslang::EOpConvUvec2ToPtr: |
John Kessenich | ee8e9c1 | 2019-10-10 20:54:21 -0600 | [diff] [blame] | 6278 | if (builder.isVector(operand)) |
| 6279 | builder.promoteIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, |
| 6280 | spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5); |
John Kessenich | 90e402f | 2019-09-17 23:19:38 -0600 | [diff] [blame] | 6281 | convOp = spv::OpBitcast; |
| 6282 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6283 | #endif |
| 6284 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6285 | default: |
| 6286 | break; |
| 6287 | } |
| 6288 | |
| 6289 | spv::Id result = 0; |
| 6290 | if (convOp == spv::OpNop) |
| 6291 | return result; |
| 6292 | |
| 6293 | if (convOp == spv::OpSelect) { |
| 6294 | zero = makeSmearedConstant(zero, vectorSize); |
| 6295 | one = makeSmearedConstant(one, vectorSize); |
| 6296 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 6297 | } else |
| 6298 | result = builder.createUnaryOp(convOp, destType, operand); |
| 6299 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6300 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6301 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6302 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 6306 | { |
| 6307 | if (vectorSize == 0) |
| 6308 | return constant; |
| 6309 | |
| 6310 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 6311 | std::vector<spv::Id> components; |
| 6312 | for (int c = 0; c < vectorSize; ++c) |
| 6313 | components.push_back(constant); |
| 6314 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 6315 | } |
| 6316 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6317 | // For glslang ops that map to SPV atomic opCodes |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6318 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6319 | { |
| 6320 | spv::Op opCode = spv::OpNop; |
| 6321 | |
| 6322 | switch (op) { |
| 6323 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6324 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6325 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6326 | opCode = spv::OpAtomicIAdd; |
| 6327 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6328 | case glslang::EOpAtomicCounterSubtract: |
| 6329 | opCode = spv::OpAtomicISub; |
| 6330 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6331 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6332 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6333 | case glslang::EOpAtomicCounterMin: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6334 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6335 | break; |
| 6336 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6337 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6338 | case glslang::EOpAtomicCounterMax: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6339 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6340 | break; |
| 6341 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6342 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6343 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6344 | opCode = spv::OpAtomicAnd; |
| 6345 | break; |
| 6346 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6347 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6348 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6349 | opCode = spv::OpAtomicOr; |
| 6350 | break; |
| 6351 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6352 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6353 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6354 | opCode = spv::OpAtomicXor; |
| 6355 | break; |
| 6356 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6357 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6358 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6359 | opCode = spv::OpAtomicExchange; |
| 6360 | break; |
| 6361 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6362 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6363 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6364 | opCode = spv::OpAtomicCompareExchange; |
| 6365 | break; |
| 6366 | case glslang::EOpAtomicCounterIncrement: |
| 6367 | opCode = spv::OpAtomicIIncrement; |
| 6368 | break; |
| 6369 | case glslang::EOpAtomicCounterDecrement: |
| 6370 | opCode = spv::OpAtomicIDecrement; |
| 6371 | break; |
| 6372 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6373 | case glslang::EOpImageAtomicLoad: |
| 6374 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6375 | opCode = spv::OpAtomicLoad; |
| 6376 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6377 | case glslang::EOpAtomicStore: |
| 6378 | case glslang::EOpImageAtomicStore: |
| 6379 | opCode = spv::OpAtomicStore; |
| 6380 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6381 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6382 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6383 | break; |
| 6384 | } |
| 6385 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6386 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 6387 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 6388 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6389 | // Sort out the operands |
| 6390 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6391 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 6392 | // - compare-exchange swaps the value and comparator |
| 6393 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6394 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6395 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 6396 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 6397 | spv::Id scopeId; |
| 6398 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6399 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 6400 | } else { |
| 6401 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 6402 | } |
| 6403 | // semantics default to relaxed |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6404 | spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() ? |
| 6405 | spv::MemorySemanticsVolatileMask : |
| 6406 | spv::MemorySemanticsMaskNone); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6407 | spv::Id semanticsId2 = semanticsId; |
| 6408 | |
| 6409 | pointerId = operands[0]; |
| 6410 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 6411 | // no additional operands |
| 6412 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 6413 | compareId = operands[1]; |
| 6414 | valueId = operands[2]; |
| 6415 | if (operands.size() > 3) { |
| 6416 | scopeId = operands[3]; |
| 6417 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 6418 | semanticsId2 = builder.makeUintConstant(builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 6419 | } |
| 6420 | } else if (opCode == spv::OpAtomicLoad) { |
| 6421 | if (operands.size() > 1) { |
| 6422 | scopeId = operands[1]; |
| 6423 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 6424 | } |
| 6425 | } else { |
| 6426 | // atomic store or RMW |
| 6427 | valueId = operands[1]; |
| 6428 | if (operands.size() > 2) { |
| 6429 | scopeId = operands[2]; |
| 6430 | semanticsId = builder.makeUintConstant(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 6431 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 6432 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6433 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6434 | // Check for capabilities |
| 6435 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6436 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 6437 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 6438 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 6439 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6440 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6441 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6442 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6443 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 6444 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6445 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6446 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6447 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 6448 | spvAtomicOperands.push_back(pointerId); |
| 6449 | spvAtomicOperands.push_back(scopeId); |
| 6450 | spvAtomicOperands.push_back(semanticsId); |
| 6451 | if (opCode == spv::OpAtomicCompareExchange) { |
| 6452 | spvAtomicOperands.push_back(semanticsId2); |
| 6453 | spvAtomicOperands.push_back(valueId); |
| 6454 | spvAtomicOperands.push_back(compareId); |
| 6455 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 6456 | spvAtomicOperands.push_back(valueId); |
| 6457 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6458 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6459 | if (opCode == spv::OpAtomicStore) { |
| 6460 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 6461 | return 0; |
| 6462 | } else { |
| 6463 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 6464 | |
| 6465 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 6466 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 6467 | if (op == glslang::EOpAtomicCounterDecrement) |
| 6468 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 6469 | |
| 6470 | return resultId; |
| 6471 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6472 | } |
| 6473 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6474 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6475 | 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] | 6476 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6477 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6478 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6479 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6480 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6481 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6482 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 6483 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6484 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 6485 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6486 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 6487 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6488 | } else if (op == glslang::EOpAnyInvocation || |
| 6489 | op == glslang::EOpAllInvocations || |
| 6490 | op == glslang::EOpAllInvocationsEqual) { |
| 6491 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 6492 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6493 | } else { |
| 6494 | builder.addCapability(spv::CapabilityGroups); |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6495 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6496 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6497 | op == glslang::EOpAddInvocationsNonUniform || |
| 6498 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6499 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6500 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 6501 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 6502 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 6503 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6504 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6505 | |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6506 | switch (op) { |
| 6507 | case glslang::EOpMinInvocations: |
| 6508 | case glslang::EOpMaxInvocations: |
| 6509 | case glslang::EOpAddInvocations: |
| 6510 | case glslang::EOpMinInvocationsNonUniform: |
| 6511 | case glslang::EOpMaxInvocationsNonUniform: |
| 6512 | case glslang::EOpAddInvocationsNonUniform: |
| 6513 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6514 | break; |
| 6515 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6516 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6517 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6518 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6519 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6520 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6521 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6522 | break; |
| 6523 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6524 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6525 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6526 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6527 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6528 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6529 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6530 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 6531 | default: |
| 6532 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6533 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6534 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6535 | spvGroupOperands.push_back(scope); |
| 6536 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6537 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6538 | spvGroupOperands.push_back(groupOp); |
| 6539 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6540 | } |
| 6541 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6542 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6543 | spv::IdImmediate op = { true, *opIt }; |
| 6544 | spvGroupOperands.push_back(op); |
| 6545 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6546 | |
| 6547 | switch (op) { |
| 6548 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6549 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6550 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6551 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6552 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6553 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6554 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6555 | opCode = spv::OpSubgroupAllEqualKHR; |
| 6556 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6557 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6558 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6559 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6560 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6561 | break; |
| 6562 | case glslang::EOpReadFirstInvocation: |
| 6563 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6564 | break; |
| 6565 | case glslang::EOpBallot: |
| 6566 | { |
| 6567 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6568 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6569 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6570 | // |
| 6571 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6572 | // |
| 6573 | spv::Id uintType = builder.makeUintType(32); |
| 6574 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6575 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6576 | |
| 6577 | std::vector<spv::Id> components; |
| 6578 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6579 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6580 | |
| 6581 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6582 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6583 | builder.createCompositeConstruct(uvec2Type, components)); |
| 6584 | } |
| 6585 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6586 | case glslang::EOpMinInvocations: |
| 6587 | case glslang::EOpMaxInvocations: |
| 6588 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6589 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6590 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6591 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6592 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6593 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6594 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6595 | if (op == glslang::EOpMinInvocations || |
| 6596 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 6597 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6598 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6599 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6600 | else { |
| 6601 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6602 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6603 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6604 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6605 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6606 | } else if (op == glslang::EOpMaxInvocations || |
| 6607 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 6608 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6609 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6610 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6611 | else { |
| 6612 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6613 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6614 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6615 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6616 | } |
| 6617 | } else { |
| 6618 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6619 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6620 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6621 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6622 | } |
| 6623 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6624 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6625 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6626 | |
| 6627 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6628 | case glslang::EOpMinInvocationsNonUniform: |
| 6629 | case glslang::EOpMaxInvocationsNonUniform: |
| 6630 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6631 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6632 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6633 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6634 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6635 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6636 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6637 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6638 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6639 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6640 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6641 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6642 | else { |
| 6643 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6644 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6645 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6646 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6647 | } |
| 6648 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6649 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 6650 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6651 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6652 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6653 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6654 | else { |
| 6655 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6656 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6657 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6658 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6659 | } |
| 6660 | } |
| 6661 | else { |
| 6662 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6663 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6664 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6665 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6666 | } |
| 6667 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6668 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6669 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6670 | |
| 6671 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6672 | default: |
| 6673 | logger->missingFunctionality("invocation operation"); |
| 6674 | return spv::NoResult; |
| 6675 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6676 | |
| 6677 | assert(opCode != spv::OpNop); |
| 6678 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6679 | } |
| 6680 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6681 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6682 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 6683 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6684 | { |
| 6685 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 6686 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6687 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6688 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6689 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 6690 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 6691 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
| 6692 | |
| 6693 | // Handle group invocation operations scalar by scalar. |
| 6694 | // The result type is the same type as the original type. |
| 6695 | // The algorithm is to: |
| 6696 | // - break the vector into scalars |
| 6697 | // - apply the operation to each scalar |
| 6698 | // - make a vector out the scalar results |
| 6699 | |
| 6700 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6701 | int numComponents = builder.getNumComponents(operands[0]); |
| 6702 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6703 | std::vector<spv::Id> results; |
| 6704 | |
| 6705 | // do each scalar op |
| 6706 | for (int comp = 0; comp < numComponents; ++comp) { |
| 6707 | std::vector<unsigned int> indexes; |
| 6708 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6709 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 6710 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6711 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 6712 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6713 | spv::IdImmediate operand = { true, operands[1] }; |
| 6714 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6715 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6716 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6717 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6718 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6719 | spv::IdImmediate operand = { true, operands[1] }; |
| 6720 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6721 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6722 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6723 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6724 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6725 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6726 | spvGroupOperands.push_back(scalar); |
| 6727 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6728 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6729 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6730 | } |
| 6731 | |
| 6732 | // put the pieces together |
| 6733 | return builder.createCompositeConstruct(typeId, results); |
| 6734 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6735 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6736 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6737 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 6738 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6739 | { |
| 6740 | // Add the required capabilities. |
| 6741 | switch (op) { |
| 6742 | case glslang::EOpSubgroupElect: |
| 6743 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6744 | break; |
| 6745 | case glslang::EOpSubgroupAll: |
| 6746 | case glslang::EOpSubgroupAny: |
| 6747 | case glslang::EOpSubgroupAllEqual: |
| 6748 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6749 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 6750 | break; |
| 6751 | case glslang::EOpSubgroupBroadcast: |
| 6752 | case glslang::EOpSubgroupBroadcastFirst: |
| 6753 | case glslang::EOpSubgroupBallot: |
| 6754 | case glslang::EOpSubgroupInverseBallot: |
| 6755 | case glslang::EOpSubgroupBallotBitExtract: |
| 6756 | case glslang::EOpSubgroupBallotBitCount: |
| 6757 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6758 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6759 | case glslang::EOpSubgroupBallotFindLSB: |
| 6760 | case glslang::EOpSubgroupBallotFindMSB: |
| 6761 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6762 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 6763 | break; |
| 6764 | case glslang::EOpSubgroupShuffle: |
| 6765 | case glslang::EOpSubgroupShuffleXor: |
| 6766 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6767 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 6768 | break; |
| 6769 | case glslang::EOpSubgroupShuffleUp: |
| 6770 | case glslang::EOpSubgroupShuffleDown: |
| 6771 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6772 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 6773 | break; |
| 6774 | case glslang::EOpSubgroupAdd: |
| 6775 | case glslang::EOpSubgroupMul: |
| 6776 | case glslang::EOpSubgroupMin: |
| 6777 | case glslang::EOpSubgroupMax: |
| 6778 | case glslang::EOpSubgroupAnd: |
| 6779 | case glslang::EOpSubgroupOr: |
| 6780 | case glslang::EOpSubgroupXor: |
| 6781 | case glslang::EOpSubgroupInclusiveAdd: |
| 6782 | case glslang::EOpSubgroupInclusiveMul: |
| 6783 | case glslang::EOpSubgroupInclusiveMin: |
| 6784 | case glslang::EOpSubgroupInclusiveMax: |
| 6785 | case glslang::EOpSubgroupInclusiveAnd: |
| 6786 | case glslang::EOpSubgroupInclusiveOr: |
| 6787 | case glslang::EOpSubgroupInclusiveXor: |
| 6788 | case glslang::EOpSubgroupExclusiveAdd: |
| 6789 | case glslang::EOpSubgroupExclusiveMul: |
| 6790 | case glslang::EOpSubgroupExclusiveMin: |
| 6791 | case glslang::EOpSubgroupExclusiveMax: |
| 6792 | case glslang::EOpSubgroupExclusiveAnd: |
| 6793 | case glslang::EOpSubgroupExclusiveOr: |
| 6794 | case glslang::EOpSubgroupExclusiveXor: |
| 6795 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6796 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 6797 | break; |
| 6798 | case glslang::EOpSubgroupClusteredAdd: |
| 6799 | case glslang::EOpSubgroupClusteredMul: |
| 6800 | case glslang::EOpSubgroupClusteredMin: |
| 6801 | case glslang::EOpSubgroupClusteredMax: |
| 6802 | case glslang::EOpSubgroupClusteredAnd: |
| 6803 | case glslang::EOpSubgroupClusteredOr: |
| 6804 | case glslang::EOpSubgroupClusteredXor: |
| 6805 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6806 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 6807 | break; |
| 6808 | case glslang::EOpSubgroupQuadBroadcast: |
| 6809 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6810 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6811 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 6812 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 6813 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 6814 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6815 | case glslang::EOpSubgroupPartitionedAdd: |
| 6816 | case glslang::EOpSubgroupPartitionedMul: |
| 6817 | case glslang::EOpSubgroupPartitionedMin: |
| 6818 | case glslang::EOpSubgroupPartitionedMax: |
| 6819 | case glslang::EOpSubgroupPartitionedAnd: |
| 6820 | case glslang::EOpSubgroupPartitionedOr: |
| 6821 | case glslang::EOpSubgroupPartitionedXor: |
| 6822 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6823 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6824 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6825 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6826 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6827 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6828 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6829 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 6830 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 6831 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 6832 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 6833 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 6834 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 6835 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 6836 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 6837 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 6838 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6839 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6840 | } |
| 6841 | |
Jeff Bolz | c5b669e | 2019-09-08 08:49:18 -0500 | [diff] [blame] | 6842 | |
| 6843 | const bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6844 | const bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6845 | const bool isBool = typeProxy == glslang::EbtBool; |
| 6846 | |
| 6847 | spv::Op opCode = spv::OpNop; |
| 6848 | |
| 6849 | // Figure out which opcode to use. |
| 6850 | switch (op) { |
| 6851 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 6852 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 6853 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 6854 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 6855 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 6856 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 6857 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 6858 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 6859 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 6860 | case glslang::EOpSubgroupBallotBitCount: |
| 6861 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6862 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 6863 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 6864 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 6865 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 6866 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 6867 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 6868 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 6869 | case glslang::EOpSubgroupAdd: |
| 6870 | case glslang::EOpSubgroupInclusiveAdd: |
| 6871 | case glslang::EOpSubgroupExclusiveAdd: |
| 6872 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6873 | case glslang::EOpSubgroupPartitionedAdd: |
| 6874 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 6875 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6876 | if (isFloat) { |
| 6877 | opCode = spv::OpGroupNonUniformFAdd; |
| 6878 | } else { |
| 6879 | opCode = spv::OpGroupNonUniformIAdd; |
| 6880 | } |
| 6881 | break; |
| 6882 | case glslang::EOpSubgroupMul: |
| 6883 | case glslang::EOpSubgroupInclusiveMul: |
| 6884 | case glslang::EOpSubgroupExclusiveMul: |
| 6885 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6886 | case glslang::EOpSubgroupPartitionedMul: |
| 6887 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 6888 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6889 | if (isFloat) { |
| 6890 | opCode = spv::OpGroupNonUniformFMul; |
| 6891 | } else { |
| 6892 | opCode = spv::OpGroupNonUniformIMul; |
| 6893 | } |
| 6894 | break; |
| 6895 | case glslang::EOpSubgroupMin: |
| 6896 | case glslang::EOpSubgroupInclusiveMin: |
| 6897 | case glslang::EOpSubgroupExclusiveMin: |
| 6898 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6899 | case glslang::EOpSubgroupPartitionedMin: |
| 6900 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 6901 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6902 | if (isFloat) { |
| 6903 | opCode = spv::OpGroupNonUniformFMin; |
| 6904 | } else if (isUnsigned) { |
| 6905 | opCode = spv::OpGroupNonUniformUMin; |
| 6906 | } else { |
| 6907 | opCode = spv::OpGroupNonUniformSMin; |
| 6908 | } |
| 6909 | break; |
| 6910 | case glslang::EOpSubgroupMax: |
| 6911 | case glslang::EOpSubgroupInclusiveMax: |
| 6912 | case glslang::EOpSubgroupExclusiveMax: |
| 6913 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6914 | case glslang::EOpSubgroupPartitionedMax: |
| 6915 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 6916 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6917 | if (isFloat) { |
| 6918 | opCode = spv::OpGroupNonUniformFMax; |
| 6919 | } else if (isUnsigned) { |
| 6920 | opCode = spv::OpGroupNonUniformUMax; |
| 6921 | } else { |
| 6922 | opCode = spv::OpGroupNonUniformSMax; |
| 6923 | } |
| 6924 | break; |
| 6925 | case glslang::EOpSubgroupAnd: |
| 6926 | case glslang::EOpSubgroupInclusiveAnd: |
| 6927 | case glslang::EOpSubgroupExclusiveAnd: |
| 6928 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6929 | case glslang::EOpSubgroupPartitionedAnd: |
| 6930 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 6931 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6932 | if (isBool) { |
| 6933 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 6934 | } else { |
| 6935 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 6936 | } |
| 6937 | break; |
| 6938 | case glslang::EOpSubgroupOr: |
| 6939 | case glslang::EOpSubgroupInclusiveOr: |
| 6940 | case glslang::EOpSubgroupExclusiveOr: |
| 6941 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6942 | case glslang::EOpSubgroupPartitionedOr: |
| 6943 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 6944 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6945 | if (isBool) { |
| 6946 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 6947 | } else { |
| 6948 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 6949 | } |
| 6950 | break; |
| 6951 | case glslang::EOpSubgroupXor: |
| 6952 | case glslang::EOpSubgroupInclusiveXor: |
| 6953 | case glslang::EOpSubgroupExclusiveXor: |
| 6954 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6955 | case glslang::EOpSubgroupPartitionedXor: |
| 6956 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 6957 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6958 | if (isBool) { |
| 6959 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 6960 | } else { |
| 6961 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 6962 | } |
| 6963 | break; |
| 6964 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 6965 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6966 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6967 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 6968 | default: assert(0 && "Unhandled subgroup operation!"); |
| 6969 | } |
| 6970 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6971 | // get the right Group Operation |
| 6972 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6973 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6974 | default: |
| 6975 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6976 | case glslang::EOpSubgroupBallotBitCount: |
| 6977 | case glslang::EOpSubgroupAdd: |
| 6978 | case glslang::EOpSubgroupMul: |
| 6979 | case glslang::EOpSubgroupMin: |
| 6980 | case glslang::EOpSubgroupMax: |
| 6981 | case glslang::EOpSubgroupAnd: |
| 6982 | case glslang::EOpSubgroupOr: |
| 6983 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6984 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6985 | break; |
| 6986 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6987 | case glslang::EOpSubgroupInclusiveAdd: |
| 6988 | case glslang::EOpSubgroupInclusiveMul: |
| 6989 | case glslang::EOpSubgroupInclusiveMin: |
| 6990 | case glslang::EOpSubgroupInclusiveMax: |
| 6991 | case glslang::EOpSubgroupInclusiveAnd: |
| 6992 | case glslang::EOpSubgroupInclusiveOr: |
| 6993 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6994 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6995 | break; |
| 6996 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6997 | case glslang::EOpSubgroupExclusiveAdd: |
| 6998 | case glslang::EOpSubgroupExclusiveMul: |
| 6999 | case glslang::EOpSubgroupExclusiveMin: |
| 7000 | case glslang::EOpSubgroupExclusiveMax: |
| 7001 | case glslang::EOpSubgroupExclusiveAnd: |
| 7002 | case glslang::EOpSubgroupExclusiveOr: |
| 7003 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7004 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7005 | break; |
| 7006 | case glslang::EOpSubgroupClusteredAdd: |
| 7007 | case glslang::EOpSubgroupClusteredMul: |
| 7008 | case glslang::EOpSubgroupClusteredMin: |
| 7009 | case glslang::EOpSubgroupClusteredMax: |
| 7010 | case glslang::EOpSubgroupClusteredAnd: |
| 7011 | case glslang::EOpSubgroupClusteredOr: |
| 7012 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7013 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7014 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7015 | case glslang::EOpSubgroupPartitionedAdd: |
| 7016 | case glslang::EOpSubgroupPartitionedMul: |
| 7017 | case glslang::EOpSubgroupPartitionedMin: |
| 7018 | case glslang::EOpSubgroupPartitionedMax: |
| 7019 | case glslang::EOpSubgroupPartitionedAnd: |
| 7020 | case glslang::EOpSubgroupPartitionedOr: |
| 7021 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7022 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7023 | break; |
| 7024 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7025 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7026 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7027 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7028 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7029 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7030 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7031 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7032 | break; |
| 7033 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7034 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7035 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7036 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7037 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7038 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7039 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7040 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7041 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7042 | } |
| 7043 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7044 | // build the instruction |
| 7045 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 7046 | |
| 7047 | // Every operation begins with the Execution Scope operand. |
| 7048 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7049 | spvGroupOperands.push_back(executionScope); |
| 7050 | |
| 7051 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 7052 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7053 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7054 | spvGroupOperands.push_back(groupOperand); |
| 7055 | } |
| 7056 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7057 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7058 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 7059 | spv::IdImmediate operand = { true, *opIt }; |
| 7060 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7061 | } |
| 7062 | |
| 7063 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7064 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7065 | switch (op) { |
| 7066 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7067 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 7068 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 7069 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 7070 | } |
| 7071 | if (directionId != spv::NoResult) { |
| 7072 | spv::IdImmediate direction = { true, directionId }; |
| 7073 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7074 | } |
| 7075 | |
| 7076 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 7077 | } |
| 7078 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7079 | 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] | 7080 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7081 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7082 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7083 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7084 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7085 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7086 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 7087 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7088 | spv::Id typeId0 = 0; |
| 7089 | if (consumedOperands > 0) |
| 7090 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7091 | spv::Id typeId1 = 0; |
| 7092 | if (consumedOperands > 1) |
| 7093 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7094 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7095 | |
| 7096 | switch (op) { |
| 7097 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7098 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7099 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7100 | else if (isUnsigned) |
| 7101 | libCall = spv::GLSLstd450UMin; |
| 7102 | else |
| 7103 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7104 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7105 | break; |
| 7106 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7107 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7108 | break; |
| 7109 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7110 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7111 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7112 | else if (isUnsigned) |
| 7113 | libCall = spv::GLSLstd450UMax; |
| 7114 | else |
| 7115 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7116 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7117 | break; |
| 7118 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7119 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7120 | break; |
| 7121 | case glslang::EOpDot: |
| 7122 | opCode = spv::OpDot; |
| 7123 | break; |
| 7124 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7125 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7126 | break; |
| 7127 | |
| 7128 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7129 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7130 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7131 | else if (isUnsigned) |
| 7132 | libCall = spv::GLSLstd450UClamp; |
| 7133 | else |
| 7134 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7135 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 7136 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7137 | break; |
| 7138 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7139 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 7140 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7141 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7142 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7143 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7144 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7145 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7146 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7147 | break; |
| 7148 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7149 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7150 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7151 | break; |
| 7152 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7153 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7154 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 7155 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7156 | break; |
| 7157 | |
| 7158 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7159 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7160 | break; |
| 7161 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7162 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7163 | break; |
| 7164 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7165 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7166 | break; |
| 7167 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7168 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7169 | break; |
| 7170 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7171 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7172 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7173 | #ifndef GLSLANG_WEB |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7174 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7175 | if (typeProxy == glslang::EbtFloat16) |
| 7176 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7177 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 7178 | break; |
| 7179 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7180 | if (typeProxy == glslang::EbtFloat16) |
| 7181 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7182 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 7183 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7184 | case glslang::EOpAddCarry: |
| 7185 | opCode = spv::OpIAddCarry; |
| 7186 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7187 | consumedOperands = 2; |
| 7188 | break; |
| 7189 | case glslang::EOpSubBorrow: |
| 7190 | opCode = spv::OpISubBorrow; |
| 7191 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7192 | consumedOperands = 2; |
| 7193 | break; |
| 7194 | case glslang::EOpUMulExtended: |
| 7195 | opCode = spv::OpUMulExtended; |
| 7196 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7197 | consumedOperands = 2; |
| 7198 | break; |
| 7199 | case glslang::EOpIMulExtended: |
| 7200 | opCode = spv::OpSMulExtended; |
| 7201 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7202 | consumedOperands = 2; |
| 7203 | break; |
| 7204 | case glslang::EOpBitfieldExtract: |
| 7205 | if (isUnsigned) |
| 7206 | opCode = spv::OpBitFieldUExtract; |
| 7207 | else |
| 7208 | opCode = spv::OpBitFieldSExtract; |
| 7209 | break; |
| 7210 | case glslang::EOpBitfieldInsert: |
| 7211 | opCode = spv::OpBitFieldInsert; |
| 7212 | break; |
| 7213 | |
| 7214 | case glslang::EOpFma: |
| 7215 | libCall = spv::GLSLstd450Fma; |
| 7216 | break; |
| 7217 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7218 | { |
| 7219 | libCall = spv::GLSLstd450FrexpStruct; |
| 7220 | assert(builder.isPointerType(typeId1)); |
| 7221 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7222 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 7223 | if (width == 16) |
| 7224 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 7225 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7226 | if (builder.getNumComponents(operands[0]) == 1) |
| 7227 | frexpIntType = builder.makeIntegerType(width, true); |
| 7228 | else |
| 7229 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 7230 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 7231 | consumedOperands = 1; |
| 7232 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7233 | break; |
| 7234 | case glslang::EOpLdexp: |
| 7235 | libCall = spv::GLSLstd450Ldexp; |
| 7236 | break; |
| 7237 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7238 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7239 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7240 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7241 | case glslang::EOpSubgroupBroadcast: |
| 7242 | case glslang::EOpSubgroupBallotBitExtract: |
| 7243 | case glslang::EOpSubgroupShuffle: |
| 7244 | case glslang::EOpSubgroupShuffleXor: |
| 7245 | case glslang::EOpSubgroupShuffleUp: |
| 7246 | case glslang::EOpSubgroupShuffleDown: |
| 7247 | case glslang::EOpSubgroupClusteredAdd: |
| 7248 | case glslang::EOpSubgroupClusteredMul: |
| 7249 | case glslang::EOpSubgroupClusteredMin: |
| 7250 | case glslang::EOpSubgroupClusteredMax: |
| 7251 | case glslang::EOpSubgroupClusteredAnd: |
| 7252 | case glslang::EOpSubgroupClusteredOr: |
| 7253 | case glslang::EOpSubgroupClusteredXor: |
| 7254 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7255 | case glslang::EOpSubgroupPartitionedAdd: |
| 7256 | case glslang::EOpSubgroupPartitionedMul: |
| 7257 | case glslang::EOpSubgroupPartitionedMin: |
| 7258 | case glslang::EOpSubgroupPartitionedMax: |
| 7259 | case glslang::EOpSubgroupPartitionedAnd: |
| 7260 | case glslang::EOpSubgroupPartitionedOr: |
| 7261 | case glslang::EOpSubgroupPartitionedXor: |
| 7262 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7263 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7264 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7265 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7266 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7267 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7268 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7269 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7270 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7271 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7272 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7273 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7274 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7275 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7276 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 7277 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7278 | case glslang::EOpSwizzleInvocations: |
| 7279 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7280 | libCall = spv::SwizzleInvocationsAMD; |
| 7281 | break; |
| 7282 | case glslang::EOpSwizzleInvocationsMasked: |
| 7283 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7284 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 7285 | break; |
| 7286 | case glslang::EOpWriteInvocation: |
| 7287 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7288 | libCall = spv::WriteInvocationAMD; |
| 7289 | break; |
| 7290 | |
| 7291 | case glslang::EOpMin3: |
| 7292 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7293 | if (isFloat) |
| 7294 | libCall = spv::FMin3AMD; |
| 7295 | else { |
| 7296 | if (isUnsigned) |
| 7297 | libCall = spv::UMin3AMD; |
| 7298 | else |
| 7299 | libCall = spv::SMin3AMD; |
| 7300 | } |
| 7301 | break; |
| 7302 | case glslang::EOpMax3: |
| 7303 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7304 | if (isFloat) |
| 7305 | libCall = spv::FMax3AMD; |
| 7306 | else { |
| 7307 | if (isUnsigned) |
| 7308 | libCall = spv::UMax3AMD; |
| 7309 | else |
| 7310 | libCall = spv::SMax3AMD; |
| 7311 | } |
| 7312 | break; |
| 7313 | case glslang::EOpMid3: |
| 7314 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7315 | if (isFloat) |
| 7316 | libCall = spv::FMid3AMD; |
| 7317 | else { |
| 7318 | if (isUnsigned) |
| 7319 | libCall = spv::UMid3AMD; |
| 7320 | else |
| 7321 | libCall = spv::SMid3AMD; |
| 7322 | } |
| 7323 | break; |
| 7324 | |
| 7325 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7326 | if (typeProxy == glslang::EbtFloat16) |
| 7327 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7328 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 7329 | libCall = spv::InterpolateAtVertexAMD; |
| 7330 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7331 | case glslang::EOpBarrier: |
| 7332 | { |
| 7333 | // This is for the extended controlBarrier function, with four operands. |
| 7334 | // The unextended barrier() goes through createNoArgOperation. |
| 7335 | assert(operands.size() == 4); |
| 7336 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 7337 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 7338 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 7339 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 7340 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7341 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7342 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7343 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7344 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7345 | } |
| 7346 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || memoryScope == spv::ScopeDevice)) { |
| 7347 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7348 | } |
| 7349 | return 0; |
| 7350 | } |
| 7351 | break; |
| 7352 | case glslang::EOpMemoryBarrier: |
| 7353 | { |
| 7354 | // This is for the extended memoryBarrier function, with three operands. |
| 7355 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 7356 | assert(operands.size() == 3); |
| 7357 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 7358 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 7359 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 7360 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7361 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7362 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7363 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7364 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7365 | } |
| 7366 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 7367 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7368 | } |
| 7369 | return 0; |
| 7370 | } |
| 7371 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7372 | |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7373 | case glslang::EOpReportIntersectionNV: |
| 7374 | { |
| 7375 | typeId = builder.makeBoolType(); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7376 | opCode = spv::OpReportIntersectionNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7377 | } |
| 7378 | break; |
| 7379 | case glslang::EOpTraceNV: |
| 7380 | { |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7381 | builder.createNoResultOp(spv::OpTraceNV, operands); |
| 7382 | return 0; |
| 7383 | } |
| 7384 | break; |
| 7385 | case glslang::EOpExecuteCallableNV: |
| 7386 | { |
| 7387 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7388 | return 0; |
| 7389 | } |
| 7390 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7391 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 7392 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 7393 | return 0; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 7394 | case glslang::EOpCooperativeMatrixMulAdd: |
| 7395 | opCode = spv::OpCooperativeMatrixMulAddNV; |
| 7396 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7397 | #endif // GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7398 | default: |
| 7399 | return 0; |
| 7400 | } |
| 7401 | |
| 7402 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7403 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 7404 | // Use an extended instruction from the standard library. |
| 7405 | // Construct the call arguments, without modifying the original operands vector. |
| 7406 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 7407 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7408 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 7409 | } else if (opCode == spv::OpDot && !isFloat) { |
| 7410 | // int dot(int, int) |
| 7411 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 7412 | const int componentCount = builder.getNumComponents(operands[0]); |
| 7413 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 7414 | builder.setPrecision(mulOp, precision); |
| 7415 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 7416 | for (int i = 1; i < componentCount; ++i) { |
| 7417 | builder.setPrecision(id, precision); |
| 7418 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(operands[0], typeId, i)); |
| 7419 | } |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7420 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7421 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7422 | case 0: |
| 7423 | // should all be handled by visitAggregate and createNoArgOperation |
| 7424 | assert(0); |
| 7425 | return 0; |
| 7426 | case 1: |
| 7427 | // should all be handled by createUnaryOperation |
| 7428 | assert(0); |
| 7429 | return 0; |
| 7430 | case 2: |
| 7431 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 7432 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7433 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7434 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 7435 | assert(consumedOperands == operands.size()); |
| 7436 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7437 | break; |
| 7438 | } |
| 7439 | } |
| 7440 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7441 | #ifndef GLSLANG_WEB |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7442 | // Decode the return types that were structures |
| 7443 | switch (op) { |
| 7444 | case glslang::EOpAddCarry: |
| 7445 | case glslang::EOpSubBorrow: |
| 7446 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7447 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7448 | break; |
| 7449 | case glslang::EOpUMulExtended: |
| 7450 | case glslang::EOpIMulExtended: |
| 7451 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 7452 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7453 | break; |
| 7454 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7455 | { |
| 7456 | assert(operands.size() == 2); |
| 7457 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 7458 | // "exp" is floating-point type (from HLSL intrinsic) |
| 7459 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 7460 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 7461 | builder.createStore(member1, operands[1]); |
| 7462 | } else |
| 7463 | // "exp" is integer type (from GLSL built-in function) |
| 7464 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 7465 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7466 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7467 | break; |
| 7468 | default: |
| 7469 | break; |
| 7470 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7471 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7472 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 7473 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7474 | } |
| 7475 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7476 | // Intrinsics with no arguments (or no return value, and no precision). |
| 7477 | 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] | 7478 | { |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7479 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7480 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 7481 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7482 | |
| 7483 | switch (op) { |
| 7484 | case glslang::EOpEmitVertex: |
| 7485 | builder.createNoResultOp(spv::OpEmitVertex); |
| 7486 | return 0; |
| 7487 | case glslang::EOpEndPrimitive: |
| 7488 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 7489 | return 0; |
| 7490 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7491 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7492 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7493 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7494 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7495 | spv::MemorySemanticsAcquireReleaseMask); |
| 7496 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7497 | } else { |
| 7498 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 7499 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7500 | } else { |
| 7501 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7502 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7503 | spv::MemorySemanticsAcquireReleaseMask); |
| 7504 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7505 | return 0; |
| 7506 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7507 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 7508 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7509 | return 0; |
| 7510 | case glslang::EOpMemoryBarrierAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7511 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 7512 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7513 | return 0; |
| 7514 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7515 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 7516 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7517 | return 0; |
| 7518 | case glslang::EOpMemoryBarrierImage: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7519 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 7520 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7521 | return 0; |
| 7522 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7523 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7524 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7525 | return 0; |
| 7526 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7527 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 7528 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7529 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7530 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7531 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7532 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7533 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7534 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7535 | case glslang::EOpDeviceMemoryBarrier: |
| 7536 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7537 | spv::MemorySemanticsImageMemoryMask | |
| 7538 | spv::MemorySemanticsAcquireReleaseMask); |
| 7539 | return 0; |
| 7540 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 7541 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7542 | spv::MemorySemanticsImageMemoryMask | |
| 7543 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7544 | return 0; |
| 7545 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7546 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7547 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7548 | return 0; |
| 7549 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7550 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7551 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7552 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7553 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7554 | case glslang::EOpSubgroupBarrier: |
| 7555 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7556 | spv::MemorySemanticsAcquireReleaseMask); |
| 7557 | return spv::NoResult; |
| 7558 | case glslang::EOpSubgroupMemoryBarrier: |
| 7559 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 7560 | spv::MemorySemanticsAcquireReleaseMask); |
| 7561 | return spv::NoResult; |
| 7562 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 7563 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 7564 | spv::MemorySemanticsAcquireReleaseMask); |
| 7565 | return spv::NoResult; |
| 7566 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 7567 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 7568 | spv::MemorySemanticsAcquireReleaseMask); |
| 7569 | return spv::NoResult; |
| 7570 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 7571 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7572 | spv::MemorySemanticsAcquireReleaseMask); |
| 7573 | return spv::NoResult; |
| 7574 | case glslang::EOpSubgroupElect: { |
| 7575 | std::vector<spv::Id> operands; |
| 7576 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 7577 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7578 | case glslang::EOpTime: |
| 7579 | { |
| 7580 | std::vector<spv::Id> args; // Dummy arguments |
| 7581 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 7582 | return builder.setPrecision(id, precision); |
| 7583 | } |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7584 | case glslang::EOpIgnoreIntersectionNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7585 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7586 | return 0; |
| 7587 | case glslang::EOpTerminateRayNV: |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7588 | builder.createNoResultOp(spv::OpTerminateRayNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7589 | return 0; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 7590 | |
| 7591 | case glslang::EOpBeginInvocationInterlock: |
| 7592 | builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
| 7593 | return 0; |
| 7594 | case glslang::EOpEndInvocationInterlock: |
| 7595 | builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
| 7596 | return 0; |
| 7597 | |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 7598 | case glslang::EOpIsHelperInvocation: |
| 7599 | { |
| 7600 | std::vector<spv::Id> args; // Dummy arguments |
Rex Xu | bb7307b | 2019-07-15 14:57:20 +0800 | [diff] [blame] | 7601 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 7602 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 7603 | return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args); |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 7604 | } |
| 7605 | |
amhagan | 91fb009 | 2019-07-10 21:14:38 -0400 | [diff] [blame] | 7606 | case glslang::EOpReadClockSubgroupKHR: { |
| 7607 | std::vector<spv::Id> args; |
| 7608 | args.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 7609 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 7610 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 7611 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 7612 | } |
| 7613 | |
| 7614 | case glslang::EOpReadClockDeviceKHR: { |
| 7615 | std::vector<spv::Id> args; |
| 7616 | args.push_back(builder.makeUintConstant(spv::ScopeDevice)); |
| 7617 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 7618 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 7619 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 7620 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7621 | default: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7622 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7623 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7624 | #endif |
| 7625 | |
| 7626 | logger->missingFunctionality("unknown operation with no arguments"); |
| 7627 | |
| 7628 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7629 | } |
| 7630 | |
| 7631 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 7632 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 7633 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7634 | spv::Id id; |
| 7635 | if (symbolValues.end() != iter) { |
| 7636 | id = iter->second; |
| 7637 | return id; |
| 7638 | } |
| 7639 | |
| 7640 | // it was not found, create it |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7641 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
| 7642 | auto forcedType = getForcedType(builtIn, symbol->getType()); |
| 7643 | id = createSpvVariable(symbol, forcedType.first); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7644 | symbolValues[symbol->getId()] = id; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 7645 | if (forcedType.second != spv::NoType) |
| 7646 | forceType[id] = forcedType.second; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7647 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 7648 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7649 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 7650 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 7651 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7652 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7653 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7654 | if (symbol->getQualifier().hasComponent()) |
| 7655 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 7656 | if (symbol->getQualifier().hasIndex()) |
| 7657 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7658 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7659 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7660 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 7661 | // atomic counters use this: |
| 7662 | if (symbol->getQualifier().hasOffset()) |
| 7663 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7664 | } |
| 7665 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 7666 | if (symbol->getQualifier().hasLocation()) |
| 7667 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7668 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 7669 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7670 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7671 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7672 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7673 | if (symbol->getQualifier().hasSet()) |
| 7674 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7675 | else if (IsDescriptorResource(symbol->getType())) { |
| 7676 | // default to 0 |
| 7677 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 7678 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7679 | if (symbol->getQualifier().hasBinding()) |
| 7680 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
Jeff Bolz | 0a93cfb | 2018-12-11 20:53:59 -0600 | [diff] [blame] | 7681 | else if (IsDescriptorResource(symbol->getType())) { |
| 7682 | // default to 0 |
| 7683 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 7684 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7685 | if (symbol->getQualifier().hasAttachment()) |
| 7686 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7687 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 7688 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7689 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7690 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 7691 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 7692 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 7693 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 7694 | } |
| 7695 | if (symbol->getQualifier().hasXfbOffset()) |
| 7696 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7697 | } |
| 7698 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7699 | // add built-in variable decoration |
| 7700 | if (builtIn != spv::BuiltInMax) { |
| 7701 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
| 7702 | } |
| 7703 | |
| 7704 | #ifndef GLSLANG_WEB |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7705 | if (symbol->getType().isImage()) { |
| 7706 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7707 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7708 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7709 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 7710 | } |
| 7711 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 7712 | // nonuniform |
| 7713 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 7714 | |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7715 | if (builtIn == spv::BuiltInSampleMask) { |
| 7716 | spv::Decoration decoration; |
| 7717 | // GL_NV_sample_mask_override_coverage extension |
| 7718 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7719 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7720 | else |
| 7721 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7722 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7723 | if (decoration != spv::DecorationMax) { |
Jason Macnak | dbd4c3c | 2019-07-12 14:33:02 -0700 | [diff] [blame] | 7724 | builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7725 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 7726 | } |
| 7727 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7728 | else if (builtIn == spv::BuiltInLayer) { |
| 7729 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7730 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7731 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7732 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 7733 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 7734 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 7735 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7736 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 7737 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7738 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 7739 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 7740 | } |
| 7741 | } |
| 7742 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7743 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7744 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 7745 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 7746 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 7747 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 7748 | if (symbol->getQualifier().pervertexNV) { |
| 7749 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 7750 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 7751 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 7752 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 7753 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7754 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 7755 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 7756 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 7757 | symbol->getType().getQualifier().semanticName); |
| 7758 | } |
| 7759 | |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 7760 | if (symbol->isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7761 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 7762 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7763 | #endif |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7764 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7765 | return id; |
| 7766 | } |
| 7767 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7768 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7769 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 7770 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 7771 | { |
| 7772 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7773 | if (qualifier.perPrimitiveNV) { |
| 7774 | // Need to add capability/extension for fragment shader. |
| 7775 | // Mesh shader already adds this by default. |
| 7776 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7777 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7778 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7779 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7780 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7781 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7782 | if (qualifier.perViewNV) |
| 7783 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 7784 | if (qualifier.perTaskNV) |
| 7785 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 7786 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7787 | if (qualifier.perPrimitiveNV) { |
| 7788 | // Need to add capability/extension for fragment shader. |
| 7789 | // Mesh shader already adds this by default. |
| 7790 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 7791 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 7792 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 7793 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7794 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 7795 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7796 | if (qualifier.perViewNV) |
| 7797 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 7798 | if (qualifier.perTaskNV) |
| 7799 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 7800 | } |
| 7801 | } |
| 7802 | #endif |
| 7803 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7804 | // 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] | 7805 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7806 | // |
| 7807 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 7808 | // |
| 7809 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 7810 | // regular constants, which themselves are trees that createSpvConstant() |
| 7811 | // recursively walks. So, this function walks the "top" of the tree: |
| 7812 | // - emit specialization constant-building instructions for specConstant |
| 7813 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7814 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7815 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 7816 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7817 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7818 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7819 | if (! node.getQualifier().specConstant) { |
| 7820 | // hand off to the non-spec-constant path |
| 7821 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 7822 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7823 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7824 | nextConst, false); |
| 7825 | } |
| 7826 | |
| 7827 | // We now know we have a specialization constant to build |
| 7828 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7829 | #ifndef GLSLANG_WEB |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 7830 | // 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] | 7831 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 7832 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 7833 | std::vector<spv::Id> dimConstId; |
| 7834 | for (int dim = 0; dim < 3; ++dim) { |
| 7835 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 7836 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 7837 | if (specConst) { |
| 7838 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 7839 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 7840 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7841 | } |
| 7842 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 7843 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 7844 | #endif |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7845 | |
| 7846 | // An AST node labelled as specialization constant should be a symbol node. |
| 7847 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 7848 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7849 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7850 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 7851 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 7852 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 7853 | // will set the builder into spec constant op instruction generating mode. |
| 7854 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7855 | result = accessChainLoad(sub_tree->getType()); |
| 7856 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7857 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7858 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7859 | } else { |
| 7860 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 7861 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7862 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 7863 | builder.addName(result, sn->getName().c_str()); |
| 7864 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7865 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7866 | |
| 7867 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 7868 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 7869 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 7870 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7871 | } |
| 7872 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7873 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 7874 | // build the aggregate SPIR-V constant. |
| 7875 | // |
| 7876 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 7877 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 7878 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7879 | 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] | 7880 | { |
| 7881 | // vector of constants for SPIR-V |
| 7882 | std::vector<spv::Id> spvConsts; |
| 7883 | |
| 7884 | // Type is used for struct and array constants |
| 7885 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 7886 | |
| 7887 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7888 | glslang::TType elementType(glslangType, 0); |
| 7889 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7890 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7891 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 7892 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7893 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7894 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 7895 | } else if (glslangType.isCoopMat()) { |
| 7896 | glslang::TType componentType(glslangType.getBasicType()); |
| 7897 | spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 7898 | } else if (glslangType.isStruct()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7899 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 7900 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 7901 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 7902 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7903 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 7904 | bool zero = nextConst >= consts.size(); |
| 7905 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 7906 | case glslang::EbtInt: |
| 7907 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 7908 | break; |
| 7909 | case glslang::EbtUint: |
| 7910 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 7911 | break; |
| 7912 | case glslang::EbtFloat: |
| 7913 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7914 | break; |
| 7915 | case glslang::EbtBool: |
| 7916 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 7917 | break; |
| 7918 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7919 | case glslang::EbtInt8: |
| 7920 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 7921 | break; |
| 7922 | case glslang::EbtUint8: |
| 7923 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 7924 | break; |
| 7925 | case glslang::EbtInt16: |
| 7926 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 7927 | break; |
| 7928 | case glslang::EbtUint16: |
| 7929 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 7930 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7931 | case glslang::EbtInt64: |
| 7932 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 7933 | break; |
| 7934 | case glslang::EbtUint64: |
| 7935 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 7936 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7937 | case glslang::EbtDouble: |
| 7938 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 7939 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7940 | case glslang::EbtFloat16: |
| 7941 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 7942 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 7943 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7944 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7945 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7946 | break; |
| 7947 | } |
| 7948 | ++nextConst; |
| 7949 | } |
| 7950 | } else { |
| 7951 | // we have a non-aggregate (scalar) constant |
| 7952 | bool zero = nextConst >= consts.size(); |
| 7953 | spv::Id scalar = 0; |
| 7954 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 7955 | case glslang::EbtInt: |
| 7956 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
| 7957 | break; |
| 7958 | case glslang::EbtUint: |
| 7959 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
| 7960 | break; |
| 7961 | case glslang::EbtFloat: |
| 7962 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 7963 | break; |
| 7964 | case glslang::EbtBool: |
| 7965 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
| 7966 | break; |
| 7967 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7968 | case glslang::EbtInt8: |
| 7969 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 7970 | break; |
| 7971 | case glslang::EbtUint8: |
| 7972 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 7973 | break; |
| 7974 | case glslang::EbtInt16: |
| 7975 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 7976 | break; |
| 7977 | case glslang::EbtUint16: |
| 7978 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 7979 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 7980 | case glslang::EbtInt64: |
| 7981 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 7982 | break; |
| 7983 | case glslang::EbtUint64: |
| 7984 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7985 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7986 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7987 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7988 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 7989 | case glslang::EbtFloat16: |
| 7990 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 7991 | break; |
Jeff Bolz | 3fd1232 | 2019-03-05 23:27:09 -0600 | [diff] [blame] | 7992 | case glslang::EbtReference: |
| 7993 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 7994 | scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar); |
| 7995 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 7996 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7997 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7998 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7999 | break; |
| 8000 | } |
| 8001 | ++nextConst; |
| 8002 | return scalar; |
| 8003 | } |
| 8004 | |
| 8005 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 8006 | } |
| 8007 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8008 | // Return true if the node is a constant or symbol whose reading has no |
| 8009 | // non-trivial observable cost or effect. |
| 8010 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 8011 | { |
| 8012 | // don't know what this is |
| 8013 | if (node == nullptr) |
| 8014 | return false; |
| 8015 | |
| 8016 | // a constant is safe |
| 8017 | if (node->getAsConstantUnion() != nullptr) |
| 8018 | return true; |
| 8019 | |
| 8020 | // not a symbol means non-trivial |
| 8021 | if (node->getAsSymbolNode() == nullptr) |
| 8022 | return false; |
| 8023 | |
| 8024 | // a symbol, depends on what's being read |
| 8025 | switch (node->getType().getQualifier().storage) { |
| 8026 | case glslang::EvqTemporary: |
| 8027 | case glslang::EvqGlobal: |
| 8028 | case glslang::EvqIn: |
| 8029 | case glslang::EvqInOut: |
| 8030 | case glslang::EvqConst: |
| 8031 | case glslang::EvqConstReadOnly: |
| 8032 | case glslang::EvqUniform: |
| 8033 | return true; |
| 8034 | default: |
| 8035 | return false; |
| 8036 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 8037 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8038 | |
| 8039 | // 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] | 8040 | // 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] | 8041 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8042 | // Return true if trivial. |
| 8043 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 8044 | { |
| 8045 | if (node == nullptr) |
| 8046 | return false; |
| 8047 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 8048 | // count non scalars as trivial, as well as anything coming from HLSL |
| 8049 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 8050 | return true; |
| 8051 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8052 | // symbols and constants are trivial |
| 8053 | if (isTrivialLeaf(node)) |
| 8054 | return true; |
| 8055 | |
| 8056 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 8057 | |
| 8058 | // not a simple operation |
| 8059 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 8060 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 8061 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 8062 | return false; |
| 8063 | |
| 8064 | // not on leaf nodes |
| 8065 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 8066 | return false; |
| 8067 | |
| 8068 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 8069 | return false; |
| 8070 | } |
| 8071 | |
| 8072 | switch (node->getAsOperator()->getOp()) { |
| 8073 | case glslang::EOpLogicalNot: |
| 8074 | case glslang::EOpConvIntToBool: |
| 8075 | case glslang::EOpConvUintToBool: |
| 8076 | case glslang::EOpConvFloatToBool: |
| 8077 | case glslang::EOpConvDoubleToBool: |
| 8078 | case glslang::EOpEqual: |
| 8079 | case glslang::EOpNotEqual: |
| 8080 | case glslang::EOpLessThan: |
| 8081 | case glslang::EOpGreaterThan: |
| 8082 | case glslang::EOpLessThanEqual: |
| 8083 | case glslang::EOpGreaterThanEqual: |
| 8084 | case glslang::EOpIndexDirect: |
| 8085 | case glslang::EOpIndexDirectStruct: |
| 8086 | case glslang::EOpLogicalXor: |
| 8087 | case glslang::EOpAny: |
| 8088 | case glslang::EOpAll: |
| 8089 | return true; |
| 8090 | default: |
| 8091 | return false; |
| 8092 | } |
| 8093 | } |
| 8094 | |
| 8095 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 8096 | // the left side is true (for &&) or false (for ||). |
| 8097 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 8098 | { |
| 8099 | spv::Id boolTypeId = builder.makeBoolType(); |
| 8100 | |
| 8101 | // emit left operand |
| 8102 | builder.clearAccessChain(); |
| 8103 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8104 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8105 | |
| 8106 | // Operands to accumulate OpPhi operands |
| 8107 | std::vector<spv::Id> phiOperands; |
| 8108 | // accumulate left operand's phi information |
| 8109 | phiOperands.push_back(leftId); |
| 8110 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8111 | |
| 8112 | // Make the two kinds of operation symmetric with a "!" |
| 8113 | // || => emit "if (! left) result = right" |
| 8114 | // && => emit "if ( left) result = right" |
| 8115 | // |
| 8116 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 8117 | // to 'builder' to have an "else" without an "then" |
| 8118 | if (op == glslang::EOpLogicalOr) |
| 8119 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 8120 | |
| 8121 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 8122 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8123 | |
| 8124 | // emit right operand as the "then" part of the "if" |
| 8125 | builder.clearAccessChain(); |
| 8126 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8127 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8128 | |
| 8129 | // accumulate left operand's phi information |
| 8130 | phiOperands.push_back(rightId); |
| 8131 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8132 | |
| 8133 | // finish the "if" |
| 8134 | ifBuilder.makeEndIf(); |
| 8135 | |
| 8136 | // phi together the two results |
| 8137 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 8138 | } |
| 8139 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8140 | #ifndef GLSLANG_WEB |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8141 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 8142 | // Import this set if it has not been imported yet. |
| 8143 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 8144 | { |
| 8145 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 8146 | return extBuiltinMap[name]; |
| 8147 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 8148 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8149 | spv::Id extBuiltins = builder.import(name); |
| 8150 | extBuiltinMap[name] = extBuiltins; |
| 8151 | return extBuiltins; |
| 8152 | } |
| 8153 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 8154 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8155 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8156 | }; // end anonymous namespace |
| 8157 | |
| 8158 | namespace glslang { |
| 8159 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8160 | void GetSpirvVersion(std::string& version) |
| 8161 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 8162 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 8163 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8164 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8165 | version = buf; |
| 8166 | } |
| 8167 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8168 | // For low-order part of the generator's magic number. Bump up |
| 8169 | // when there is a change in the style (e.g., if SSA form changes, |
| 8170 | // or a different instruction sequence to do something gets used). |
| 8171 | int GetSpirvGeneratorVersion() |
| 8172 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 8173 | // return 1; // start |
| 8174 | // 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] | 8175 | // 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] | 8176 | // 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] | 8177 | // 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] | 8178 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 8179 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
| 8180 | 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] | 8181 | } |
| 8182 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8183 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8184 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8185 | { |
| 8186 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8187 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8188 | if (out.fail()) |
| 8189 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8190 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 8191 | unsigned int word = spirv[i]; |
| 8192 | out.write((const char*)&word, 4); |
| 8193 | } |
| 8194 | out.close(); |
| 8195 | } |
| 8196 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8197 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 8198 | 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] | 8199 | { |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8200 | #ifndef GLSLANG_WEB |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8201 | std::ofstream out; |
| 8202 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8203 | if (out.fail()) |
| 8204 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 8205 | out << "\t// " << |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8206 | GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL << |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 8207 | std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8208 | if (varName != nullptr) { |
| 8209 | out << "\t #pragma once" << std::endl; |
| 8210 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 8211 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8212 | const int WORDS_PER_LINE = 8; |
| 8213 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 8214 | out << "\t"; |
| 8215 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 8216 | const unsigned int word = spirv[i + j]; |
| 8217 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 8218 | if (i + j + 1 < (int)spirv.size()) { |
| 8219 | out << ","; |
| 8220 | } |
| 8221 | } |
| 8222 | out << std::endl; |
| 8223 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8224 | if (varName != nullptr) { |
| 8225 | out << "};"; |
| 8226 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8227 | out.close(); |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8228 | #endif |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8229 | } |
| 8230 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8231 | // |
| 8232 | // Set up the glslang traversal |
| 8233 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8234 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8235 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8236 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8237 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8238 | } |
| 8239 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8240 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8241 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8242 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8243 | TIntermNode* root = intermediate.getTreeRoot(); |
| 8244 | |
| 8245 | if (root == 0) |
| 8246 | return; |
| 8247 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8248 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8249 | if (options == nullptr) |
| 8250 | options = &defaultOptions; |
| 8251 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8252 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8253 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 8254 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8255 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 8256 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8257 | it.dumpSpv(spirv); |
| 8258 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 8259 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8260 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 8261 | // eg. forward and remove memory writes of opaque types. |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8262 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
| 8263 | if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) && !options->disableOptimizer) { |
John Kessenich | e7df8e0 | 2018-08-22 17:12:46 -0600 | [diff] [blame] | 8264 | SpirvToolsLegalize(intermediate, spirv, logger, options); |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8265 | prelegalization = false; |
| 8266 | } |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8267 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8268 | if (options->validate) |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8269 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8270 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8271 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8272 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8273 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8274 | #endif |
| 8275 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8276 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8277 | } |
| 8278 | |
| 8279 | }; // end namespace glslang |