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 | 56364b6 | 2020-03-01 04:51:40 -0700 | [diff] [blame] | 3 | // Copyright (C) 2015-2020 Google, Inc. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4 | // Copyright (C) 2017 ARM Limited. |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 5 | // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 7 | // All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 9 | // Redistribution and use in source and binary forms, with or without |
| 10 | // modification, are permitted provided that the following conditions |
| 11 | // are met: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 12 | // |
| 13 | // Redistributions of source code must retain the above copyright |
| 14 | // notice, this list of conditions and the following disclaimer. |
| 15 | // |
| 16 | // Redistributions in binary form must reproduce the above |
| 17 | // copyright notice, this list of conditions and the following |
| 18 | // disclaimer in the documentation and/or other materials provided |
| 19 | // with the distribution. |
| 20 | // |
| 21 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 22 | // contributors may be used to endorse or promote products derived |
| 23 | // from this software without specific prior written permission. |
| 24 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 29 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | // POSSIBILITY OF SUCH DAMAGE. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 37 | |
| 38 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 39 | // Visit the nodes in the glslang intermediate tree representation to |
| 40 | // translate them to SPIR-V. |
| 41 | // |
| 42 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 43 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 44 | #include "GlslangToSpv.h" |
| 45 | #include "SpvBuilder.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 46 | namespace spv { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 47 | #include "GLSL.std.450.h" |
| 48 | #include "GLSL.ext.KHR.h" |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 49 | #include "GLSL.ext.EXT.h" |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 50 | #include "GLSL.ext.AMD.h" |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 51 | #include "GLSL.ext.NV.h" |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 52 | #include "NonSemanticDebugPrintf.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 53 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 54 | |
| 55 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 56 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 57 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 58 | #include "../glslang/Include/Common.h" |
Ben Clayton | fbe9a23 | 2020-06-17 11:17:19 +0100 | [diff] [blame] | 59 | |
| 60 | // Build-time generated includes |
| 61 | #include "glslang/build_info.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 62 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 63 | #include <fstream> |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 64 | #include <iomanip> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 65 | #include <list> |
| 66 | #include <map> |
| 67 | #include <stack> |
| 68 | #include <string> |
| 69 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 70 | |
| 71 | namespace { |
| 72 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 73 | namespace { |
| 74 | class SpecConstantOpModeGuard { |
| 75 | public: |
| 76 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 77 | : builder_(builder) { |
| 78 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 79 | } |
| 80 | ~SpecConstantOpModeGuard() { |
| 81 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 82 | : builder_->setToNormalCodeGenMode(); |
| 83 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 84 | void turnOnSpecConstantOpMode() { |
| 85 | builder_->setToSpecConstCodeGenMode(); |
| 86 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | spv::Builder* builder_; |
| 90 | bool previous_flag_; |
| 91 | }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 92 | |
| 93 | struct OpDecorations { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 94 | public: |
| 95 | OpDecorations(spv::Decoration precision, spv::Decoration noContraction, spv::Decoration nonUniform) : |
| 96 | precision(precision) |
| 97 | #ifndef GLSLANG_WEB |
| 98 | , |
| 99 | noContraction(noContraction), |
| 100 | nonUniform(nonUniform) |
| 101 | #endif |
| 102 | { } |
| 103 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 104 | spv::Decoration precision; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 105 | |
| 106 | #ifdef GLSLANG_WEB |
Ryan Harrison | 7c9accb | 2019-10-11 11:00:57 -0400 | [diff] [blame] | 107 | void addNoContraction(spv::Builder&, spv::Id) const { } |
| 108 | void addNonUniform(spv::Builder&, spv::Id) const { } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 109 | #else |
Ryan Harrison | 7c9accb | 2019-10-11 11:00:57 -0400 | [diff] [blame] | 110 | void addNoContraction(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, noContraction); } |
| 111 | void addNonUniform(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, nonUniform); } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 112 | protected: |
| 113 | spv::Decoration noContraction; |
| 114 | spv::Decoration nonUniform; |
| 115 | #endif |
| 116 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // namespace |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 120 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 121 | // |
| 122 | // The main holder of information for translating glslang to SPIR-V. |
| 123 | // |
| 124 | // Derives from the AST walking base class. |
| 125 | // |
| 126 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 127 | public: |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 128 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 129 | glslang::SpvOptions& options); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 130 | virtual ~TGlslangToSpvTraverser() { } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 131 | |
| 132 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 133 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 134 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 135 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 136 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 137 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 138 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 139 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 140 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 141 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 142 | void finishSpv(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 143 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 144 | |
| 145 | protected: |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 146 | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
| 147 | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
| 148 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 149 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 150 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 151 | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 152 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 153 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 154 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 155 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 156 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 157 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 158 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 159 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 160 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 161 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 162 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 163 | spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 164 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 165 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 166 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 167 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 168 | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 169 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 170 | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 171 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 172 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 173 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 174 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 175 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 176 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 177 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 178 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 179 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 180 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 181 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 182 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 183 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 184 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 185 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 186 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 187 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 188 | bool writableParam(glslang::TStorageQualifier) const; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 189 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 190 | void makeFunctions(const glslang::TIntermSequence&); |
| 191 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 192 | void visitFunctions(const glslang::TIntermSequence&); |
| 193 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 194 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 195 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 196 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 197 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 198 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 199 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 200 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 201 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 202 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 203 | spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 204 | glslang::TBasicType typeProxy, |
| 205 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 206 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 207 | glslang::TBasicType typeProxy); |
| 208 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 209 | glslang::TBasicType typeProxy); |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 210 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 211 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 212 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
| 213 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 214 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
| 215 | spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
| 216 | glslang::TBasicType typeProxy); |
| 217 | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 218 | spv::Id typeId, std::vector<spv::Id>& operands); |
| 219 | spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
| 220 | glslang::TBasicType typeProxy); |
| 221 | spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
| 222 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 223 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 224 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 225 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 226 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 227 | spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, |
| 228 | int& nextConst, bool specConstant); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 229 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 230 | bool isTrivial(const glslang::TIntermTyped* node); |
| 231 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 232 | spv::Id getExtBuiltins(const char* name); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 233 | std::pair<spv::Id, spv::Id> getForcedType(glslang::TBuiltInVariable builtIn, const glslang::TType&); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 234 | spv::Id translateForcedType(spv::Id object); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 235 | spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 236 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 237 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 238 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 239 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 240 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 241 | int sequenceDepth; |
| 242 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 243 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 244 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 245 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 246 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 247 | bool inEntryPoint; |
| 248 | bool entryPointTerminated; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 249 | bool linkageOnly; // true when visiting the set of objects in the AST present only for |
| 250 | // establishing interface, whether or not they were statically used |
John Kessenich | 59420fd | 2015-12-21 11:45:34 -0700 | [diff] [blame] | 251 | 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] | 252 | const glslang::TIntermediate* glslangIntermediate; |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 253 | 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] | 254 | spv::Id stdBuiltins; |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 255 | spv::Id nonSemanticDebugPrintf; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 256 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 257 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 258 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 259 | std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, |
| 260 | // rather than a pointer |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 261 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 262 | 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] | 263 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 264 | std::unordered_map<int, std::vector<int>> memberRemapper; |
| 265 | // for mapping glslang symbol struct to symbol Id |
| 266 | std::unordered_map<const glslang::TTypeList*, int> glslangTypeToIdMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 267 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 268 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 269 | // Map pointee types for EbtReference to their forward pointers |
| 270 | std::map<const glslang::TType *, spv::Id> forwardPointers; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 271 | // Type forcing, for when SPIR-V wants a different type than the AST, |
| 272 | // requiring local translation to and from SPIR-V type on every access. |
| 273 | // Maps <builtin-variable-id -> AST-required-type-id> |
| 274 | std::unordered_map<spv::Id, spv::Id> forceType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | // |
| 278 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 279 | // |
| 280 | |
| 281 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 282 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 283 | { |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 284 | #ifdef GLSLANG_WEB |
| 285 | return spv::SourceLanguageESSL; |
Shahbaz Youssefi | 1ef2e25 | 2020-07-03 15:42:53 -0400 | [diff] [blame] | 286 | #elif defined(GLSLANG_ANGLE) |
| 287 | return spv::SourceLanguageGLSL; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 288 | #endif |
| 289 | |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 290 | switch (source) { |
| 291 | case glslang::EShSourceGlsl: |
| 292 | switch (profile) { |
| 293 | case ENoProfile: |
| 294 | case ECoreProfile: |
| 295 | case ECompatibilityProfile: |
| 296 | return spv::SourceLanguageGLSL; |
| 297 | case EEsProfile: |
| 298 | return spv::SourceLanguageESSL; |
| 299 | default: |
| 300 | return spv::SourceLanguageUnknown; |
| 301 | } |
| 302 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 303 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 304 | default: |
| 305 | return spv::SourceLanguageUnknown; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // Translate glslang language (stage) to SPIR-V execution model. |
| 310 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 311 | { |
| 312 | switch (stage) { |
| 313 | case EShLangVertex: return spv::ExecutionModelVertex; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 314 | case EShLangFragment: return spv::ExecutionModelFragment; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 315 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
John Kessenich | 51ed01c | 2019-10-10 11:40:11 -0600 | [diff] [blame] | 316 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 317 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 318 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 319 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 320 | case EShLangRayGen: return spv::ExecutionModelRayGenerationKHR; |
| 321 | case EShLangIntersect: return spv::ExecutionModelIntersectionKHR; |
| 322 | case EShLangAnyHit: return spv::ExecutionModelAnyHitKHR; |
| 323 | case EShLangClosestHit: return spv::ExecutionModelClosestHitKHR; |
| 324 | case EShLangMiss: return spv::ExecutionModelMissKHR; |
| 325 | case EShLangCallable: return spv::ExecutionModelCallableKHR; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 326 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 327 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 328 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 329 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 330 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 331 | return spv::ExecutionModelFragment; |
| 332 | } |
| 333 | } |
| 334 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 335 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 336 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 337 | { |
| 338 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 339 | case glslang::Esd1D: return spv::Dim1D; |
| 340 | case glslang::Esd2D: return spv::Dim2D; |
| 341 | case glslang::Esd3D: return spv::Dim3D; |
| 342 | case glslang::EsdCube: return spv::DimCube; |
| 343 | case glslang::EsdRect: return spv::DimRect; |
| 344 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 345 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 346 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 347 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 348 | return spv::Dim2D; |
| 349 | } |
| 350 | } |
| 351 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 352 | // Translate glslang precision to SPIR-V precision decorations. |
| 353 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 354 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 355 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 356 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 357 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 358 | default: |
| 359 | return spv::NoPrecision; |
| 360 | } |
| 361 | } |
| 362 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 363 | // Translate glslang type to SPIR-V precision decorations. |
| 364 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 365 | { |
| 366 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 367 | } |
| 368 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 369 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 370 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 371 | { |
| 372 | if (type.getBasicType() == glslang::EbtBlock) { |
| 373 | switch (type.getQualifier().storage) { |
| 374 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 375 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 376 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 377 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 378 | #ifndef GLSLANG_WEB |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 379 | case glslang::EvqPayload: return spv::DecorationBlock; |
| 380 | case glslang::EvqPayloadIn: return spv::DecorationBlock; |
| 381 | case glslang::EvqHitAttr: return spv::DecorationBlock; |
| 382 | case glslang::EvqCallableData: return spv::DecorationBlock; |
| 383 | case glslang::EvqCallableDataIn: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 384 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 385 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 386 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 387 | break; |
| 388 | } |
| 389 | } |
| 390 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 391 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 392 | } |
| 393 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 394 | // Translate glslang type to SPIR-V memory decorations. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 395 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, |
| 396 | bool useVulkanMemoryModel) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 397 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 398 | if (!useVulkanMemoryModel) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 399 | if (qualifier.isCoherent()) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 400 | memory.push_back(spv::DecorationCoherent); |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 401 | if (qualifier.isVolatile()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 402 | memory.push_back(spv::DecorationVolatile); |
| 403 | memory.push_back(spv::DecorationCoherent); |
| 404 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 405 | } |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 406 | if (qualifier.isRestrict()) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 407 | memory.push_back(spv::DecorationRestrict); |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 408 | if (qualifier.isReadOnly()) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 409 | memory.push_back(spv::DecorationNonWritable); |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 410 | if (qualifier.isWriteOnly()) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 411 | memory.push_back(spv::DecorationNonReadable); |
| 412 | } |
| 413 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 414 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 415 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 416 | { |
| 417 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 418 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 419 | case glslang::ElmRowMajor: |
| 420 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 421 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 422 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 423 | default: |
| 424 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 425 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 426 | } |
| 427 | } else { |
| 428 | switch (type.getBasicType()) { |
| 429 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 430 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 431 | break; |
| 432 | case glslang::EbtBlock: |
| 433 | switch (type.getQualifier().storage) { |
| 434 | case glslang::EvqUniform: |
| 435 | case glslang::EvqBuffer: |
| 436 | switch (type.getQualifier().layoutPacking) { |
| 437 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 438 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 439 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 440 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 441 | } |
| 442 | case glslang::EvqVaryingIn: |
| 443 | case glslang::EvqVaryingOut: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 444 | if (type.getQualifier().isTaskMemory()) { |
| 445 | switch (type.getQualifier().layoutPacking) { |
| 446 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 447 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 448 | default: break; |
| 449 | } |
| 450 | } else { |
| 451 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 452 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 453 | return spv::DecorationMax; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 454 | #ifndef GLSLANG_WEB |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 455 | case glslang::EvqPayload: |
| 456 | case glslang::EvqPayloadIn: |
| 457 | case glslang::EvqHitAttr: |
| 458 | case glslang::EvqCallableData: |
| 459 | case glslang::EvqCallableDataIn: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 460 | return spv::DecorationMax; |
| 461 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 462 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 463 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 464 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 471 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 472 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 473 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 474 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 475 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 476 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 477 | return spv::DecorationMax; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 478 | else if (qualifier.isNonPerspective()) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 479 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 480 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 481 | return spv::DecorationFlat; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 482 | else if (qualifier.isExplicitInterpolation()) { |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 483 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 484 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 485 | } |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 486 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 487 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 491 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 492 | // should be applied. |
| 493 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 494 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 495 | if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 496 | return spv::DecorationCentroid; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 497 | #ifndef GLSLANG_WEB |
| 498 | else if (qualifier.patch) |
| 499 | return spv::DecorationPatch; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 500 | else if (qualifier.sample) { |
| 501 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 502 | return spv::DecorationSample; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 503 | } |
| 504 | #endif |
| 505 | |
| 506 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 507 | } |
| 508 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 509 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 510 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 511 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 512 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 513 | return spv::DecorationInvariant; |
| 514 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 515 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 516 | } |
| 517 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 518 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 519 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 520 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 521 | #ifndef GLSLANG_WEB |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 522 | if (qualifier.isNoContraction()) |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 523 | return spv::DecorationNoContraction; |
| 524 | else |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 525 | #endif |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 526 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 527 | } |
| 528 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 529 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 530 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 531 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 532 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 533 | if (qualifier.isNonUniform()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 534 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 535 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 536 | return spv::DecorationNonUniformEXT; |
| 537 | } else |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 538 | #endif |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 539 | return spv::DecorationMax; |
| 540 | } |
| 541 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 542 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess( |
| 543 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 544 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 545 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 546 | |
| 547 | #ifndef GLSLANG_WEB |
| 548 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) |
| 549 | return mask; |
| 550 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 551 | if (coherentFlags.isVolatile() || coherentFlags.anyCoherent()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 552 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 553 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 554 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 555 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 556 | if (coherentFlags.nonprivate) { |
| 557 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 558 | } |
| 559 | if (coherentFlags.volatil) { |
| 560 | mask = mask | spv::MemoryAccessVolatileMask; |
| 561 | } |
| 562 | if (mask != spv::MemoryAccessMaskNone) { |
| 563 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 564 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 565 | #endif |
| 566 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 567 | return mask; |
| 568 | } |
| 569 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 570 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands( |
| 571 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 572 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 573 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 574 | |
| 575 | #ifndef GLSLANG_WEB |
| 576 | if (!glslangIntermediate->usingVulkanMemoryModel()) |
| 577 | return mask; |
| 578 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 579 | if (coherentFlags.volatil || |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 580 | coherentFlags.anyCoherent()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 581 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 582 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 583 | } |
| 584 | if (coherentFlags.nonprivate) { |
| 585 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 586 | } |
| 587 | if (coherentFlags.volatil) { |
| 588 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 589 | } |
| 590 | if (mask != spv::ImageOperandsMaskNone) { |
| 591 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 592 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 593 | #endif |
| 594 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 595 | return mask; |
| 596 | } |
| 597 | |
| 598 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 599 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 600 | spv::Builder::AccessChain::CoherentFlags flags = {}; |
| 601 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 602 | flags.coherent = type.getQualifier().coherent; |
| 603 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 604 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 605 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 606 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 607 | type.getQualifier().storage == glslang::EvqShared; |
| 608 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 609 | flags.shadercallcoherent = type.getQualifier().shadercallcoherent; |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 610 | flags.volatil = type.getQualifier().volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 611 | // *coherent variables are implicitly nonprivate in GLSL |
| 612 | flags.nonprivate = type.getQualifier().nonprivate || |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 613 | flags.anyCoherent() || |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 614 | flags.volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 615 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 616 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 617 | return flags; |
| 618 | } |
| 619 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 620 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope( |
| 621 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 622 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 623 | spv::Scope scope = spv::ScopeMax; |
| 624 | |
| 625 | #ifndef GLSLANG_WEB |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 626 | if (coherentFlags.volatil || coherentFlags.coherent) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 627 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 628 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 629 | } else if (coherentFlags.devicecoherent) { |
| 630 | scope = spv::ScopeDevice; |
| 631 | } else if (coherentFlags.queuefamilycoherent) { |
| 632 | scope = spv::ScopeQueueFamilyKHR; |
| 633 | } else if (coherentFlags.workgroupcoherent) { |
| 634 | scope = spv::ScopeWorkgroup; |
| 635 | } else if (coherentFlags.subgroupcoherent) { |
| 636 | scope = spv::ScopeSubgroup; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 637 | } else if (coherentFlags.shadercallcoherent) { |
| 638 | scope = spv::ScopeShaderCallKHR; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 639 | } |
| 640 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 641 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 642 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 643 | #endif |
| 644 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 645 | return scope; |
| 646 | } |
| 647 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 648 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 649 | // associated capabilities when required. For some built-in variables, a capability |
| 650 | // is generated only when using the variable in an executable instruction, but not when |
| 651 | // just declaring a struct member variable with it. This is true for PointSize, |
| 652 | // ClipDistance, and CullDistance. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 653 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, |
| 654 | bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 655 | { |
| 656 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 657 | case glslang::EbvPointSize: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 658 | #ifndef GLSLANG_WEB |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 659 | // Defer adding the capability until the built-in is actually used. |
| 660 | if (! memberDeclaration) { |
| 661 | switch (glslangIntermediate->getStage()) { |
| 662 | case EShLangGeometry: |
| 663 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 664 | break; |
| 665 | case EShLangTessControl: |
| 666 | case EShLangTessEvaluation: |
| 667 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 668 | break; |
| 669 | default: |
| 670 | break; |
| 671 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 672 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 673 | #endif |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 674 | return spv::BuiltInPointSize; |
| 675 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 676 | case glslang::EbvPosition: return spv::BuiltInPosition; |
| 677 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 678 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
| 679 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 680 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
| 681 | |
| 682 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 683 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 684 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
| 685 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 686 | |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 687 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 688 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 689 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 690 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 691 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 692 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
| 693 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 694 | #ifndef GLSLANG_WEB |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 695 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 696 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 697 | // They are now generated when used, rather than here when declared. |
| 698 | // Potentially, the specification should be more clear what the minimum |
| 699 | // use needed is to trigger the capability. |
| 700 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 701 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 702 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 703 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 704 | return spv::BuiltInClipDistance; |
| 705 | |
| 706 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 707 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 708 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 709 | return spv::BuiltInCullDistance; |
| 710 | |
| 711 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 712 | builder.addCapability(spv::CapabilityMultiViewport); |
| 713 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 714 | glslangIntermediate->getStage() == EShLangTessControl || |
| 715 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 716 | |
Sidney Just | 56350ca | 2020-11-02 11:27:40 -0800 | [diff] [blame] | 717 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
| 718 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
| 719 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
| 720 | } |
| 721 | else |
| 722 | builder.addCapability(spv::CapabilityShaderViewportIndex); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 723 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 724 | return spv::BuiltInViewportIndex; |
| 725 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 726 | case glslang::EbvSampleId: |
| 727 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 728 | return spv::BuiltInSampleId; |
| 729 | |
| 730 | case glslang::EbvSamplePosition: |
| 731 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 732 | return spv::BuiltInSamplePosition; |
| 733 | |
| 734 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 735 | return spv::BuiltInSampleMask; |
| 736 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 737 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 738 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 739 | return spv::BuiltInLayer; |
| 740 | } |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 741 | builder.addCapability(spv::CapabilityGeometry); |
| 742 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 743 | glslangIntermediate->getStage() == EShLangTessControl || |
| 744 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 745 | |
Sidney Just | 56350ca | 2020-11-02 11:27:40 -0800 | [diff] [blame] | 746 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
| 747 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
| 748 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
| 749 | } else |
| 750 | builder.addCapability(spv::CapabilityShaderLayer); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 751 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 752 | return spv::BuiltInLayer; |
| 753 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 754 | case glslang::EbvBaseVertex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 755 | 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] | 756 | builder.addCapability(spv::CapabilityDrawParameters); |
| 757 | return spv::BuiltInBaseVertex; |
| 758 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 759 | case glslang::EbvBaseInstance: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 760 | 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] | 761 | builder.addCapability(spv::CapabilityDrawParameters); |
| 762 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 763 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 764 | case glslang::EbvDrawId: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 765 | 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] | 766 | builder.addCapability(spv::CapabilityDrawParameters); |
| 767 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 768 | |
| 769 | case glslang::EbvPrimitiveId: |
| 770 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 771 | builder.addCapability(spv::CapabilityGeometry); |
| 772 | return spv::BuiltInPrimitiveId; |
| 773 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 774 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 775 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 776 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 777 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 778 | |
Chow | a315b56 | 2020-07-02 15:50:36 +0800 | [diff] [blame] | 779 | case glslang::EbvShadingRateKHR: |
| 780 | builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
| 781 | builder.addCapability(spv::CapabilityFragmentShadingRateKHR); |
| 782 | return spv::BuiltInShadingRateKHR; |
| 783 | |
| 784 | case glslang::EbvPrimitiveShadingRateKHR: |
| 785 | builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
| 786 | builder.addCapability(spv::CapabilityFragmentShadingRateKHR); |
| 787 | return spv::BuiltInPrimitiveShadingRateKHR; |
| 788 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 789 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 790 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 791 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 792 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 793 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 794 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 795 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 796 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 797 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 798 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 799 | return spv::BuiltInSubgroupSize; |
| 800 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 801 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 802 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 803 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 804 | return spv::BuiltInSubgroupLocalInvocationId; |
| 805 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 806 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 807 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 808 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 809 | return spv::BuiltInSubgroupEqMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 810 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 811 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 812 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 813 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 814 | return spv::BuiltInSubgroupGeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 815 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 816 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 817 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 818 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 819 | return spv::BuiltInSubgroupGtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 820 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 821 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 822 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 823 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 824 | return spv::BuiltInSubgroupLeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 825 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 826 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 827 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 828 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 829 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 830 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 831 | case glslang::EbvNumSubgroups: |
| 832 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 833 | return spv::BuiltInNumSubgroups; |
| 834 | |
| 835 | case glslang::EbvSubgroupID: |
| 836 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 837 | return spv::BuiltInSubgroupId; |
| 838 | |
| 839 | case glslang::EbvSubgroupSize2: |
| 840 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 841 | return spv::BuiltInSubgroupSize; |
| 842 | |
| 843 | case glslang::EbvSubgroupInvocation2: |
| 844 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 845 | return spv::BuiltInSubgroupLocalInvocationId; |
| 846 | |
| 847 | case glslang::EbvSubgroupEqMask2: |
| 848 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 849 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 850 | return spv::BuiltInSubgroupEqMask; |
| 851 | |
| 852 | case glslang::EbvSubgroupGeMask2: |
| 853 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 854 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 855 | return spv::BuiltInSubgroupGeMask; |
| 856 | |
| 857 | case glslang::EbvSubgroupGtMask2: |
| 858 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 859 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 860 | return spv::BuiltInSubgroupGtMask; |
| 861 | |
| 862 | case glslang::EbvSubgroupLeMask2: |
| 863 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 864 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 865 | return spv::BuiltInSubgroupLeMask; |
| 866 | |
| 867 | case glslang::EbvSubgroupLtMask2: |
| 868 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 869 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 870 | return spv::BuiltInSubgroupLtMask; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 871 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 872 | case glslang::EbvBaryCoordNoPersp: |
| 873 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 874 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 875 | |
| 876 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 877 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 878 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 879 | |
| 880 | case glslang::EbvBaryCoordNoPerspSample: |
| 881 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 882 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 883 | |
| 884 | case glslang::EbvBaryCoordSmooth: |
| 885 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 886 | return spv::BuiltInBaryCoordSmoothAMD; |
| 887 | |
| 888 | case glslang::EbvBaryCoordSmoothCentroid: |
| 889 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 890 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 891 | |
| 892 | case glslang::EbvBaryCoordSmoothSample: |
| 893 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 894 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 895 | |
| 896 | case glslang::EbvBaryCoordPullModel: |
| 897 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 898 | return spv::BuiltInBaryCoordPullModelAMD; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 899 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 900 | case glslang::EbvDeviceIndex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 901 | builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 902 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 903 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 904 | |
| 905 | case glslang::EbvViewIndex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 906 | builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 907 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 908 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 909 | |
Daniel Koch | 5154db5 | 2018-11-26 10:01:58 -0500 | [diff] [blame] | 910 | case glslang::EbvFragSizeEXT: |
| 911 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 912 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 913 | return spv::BuiltInFragSizeEXT; |
| 914 | |
| 915 | case glslang::EbvFragInvocationCountEXT: |
| 916 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 917 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 918 | return spv::BuiltInFragInvocationCountEXT; |
| 919 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 920 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 921 | if (!memberDeclaration) { |
| 922 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 923 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 924 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 925 | return spv::BuiltInViewportMaskNV; |
| 926 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 927 | if (!memberDeclaration) { |
| 928 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 929 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 930 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 931 | return spv::BuiltInSecondaryPositionNV; |
| 932 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 933 | if (!memberDeclaration) { |
| 934 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 935 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 936 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 937 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 938 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 939 | if (!memberDeclaration) { |
| 940 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 941 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 942 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 943 | return spv::BuiltInPositionPerViewNV; |
| 944 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 945 | if (!memberDeclaration) { |
| 946 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 947 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 948 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 949 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 950 | case glslang::EbvFragFullyCoveredNV: |
| 951 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 952 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 953 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 954 | case glslang::EbvFragmentSizeNV: |
| 955 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 956 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 957 | return spv::BuiltInFragmentSizeNV; |
| 958 | case glslang::EbvInvocationsPerPixelNV: |
| 959 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 960 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 961 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 962 | |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 963 | // ray tracing |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 964 | case glslang::EbvLaunchId: |
| 965 | return spv::BuiltInLaunchIdKHR; |
| 966 | case glslang::EbvLaunchSize: |
| 967 | return spv::BuiltInLaunchSizeKHR; |
| 968 | case glslang::EbvWorldRayOrigin: |
| 969 | return spv::BuiltInWorldRayOriginKHR; |
| 970 | case glslang::EbvWorldRayDirection: |
| 971 | return spv::BuiltInWorldRayDirectionKHR; |
| 972 | case glslang::EbvObjectRayOrigin: |
| 973 | return spv::BuiltInObjectRayOriginKHR; |
| 974 | case glslang::EbvObjectRayDirection: |
| 975 | return spv::BuiltInObjectRayDirectionKHR; |
| 976 | case glslang::EbvRayTmin: |
| 977 | return spv::BuiltInRayTminKHR; |
| 978 | case glslang::EbvRayTmax: |
| 979 | return spv::BuiltInRayTmaxKHR; |
| 980 | case glslang::EbvInstanceCustomIndex: |
| 981 | return spv::BuiltInInstanceCustomIndexKHR; |
| 982 | case glslang::EbvHitT: |
| 983 | return spv::BuiltInHitTKHR; |
| 984 | case glslang::EbvHitKind: |
| 985 | return spv::BuiltInHitKindKHR; |
| 986 | case glslang::EbvObjectToWorld: |
| 987 | case glslang::EbvObjectToWorld3x4: |
| 988 | return spv::BuiltInObjectToWorldKHR; |
| 989 | case glslang::EbvWorldToObject: |
| 990 | case glslang::EbvWorldToObject3x4: |
| 991 | return spv::BuiltInWorldToObjectKHR; |
| 992 | case glslang::EbvIncomingRayFlags: |
| 993 | return spv::BuiltInIncomingRayFlagsKHR; |
| 994 | case glslang::EbvGeometryIndex: |
| 995 | return spv::BuiltInRayGeometryIndexKHR; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 996 | |
| 997 | // barycentrics |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 998 | case glslang::EbvBaryCoordNV: |
| 999 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 1000 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 1001 | return spv::BuiltInBaryCoordNV; |
| 1002 | case glslang::EbvBaryCoordNoPerspNV: |
| 1003 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 1004 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 1005 | return spv::BuiltInBaryCoordNoPerspNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1006 | |
| 1007 | // mesh shaders |
| 1008 | case glslang::EbvTaskCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1009 | return spv::BuiltInTaskCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1010 | case glslang::EbvPrimitiveCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1011 | return spv::BuiltInPrimitiveCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1012 | case glslang::EbvPrimitiveIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1013 | return spv::BuiltInPrimitiveIndicesNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1014 | case glslang::EbvClipDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1015 | return spv::BuiltInClipDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1016 | case glslang::EbvCullDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1017 | return spv::BuiltInCullDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1018 | case glslang::EbvLayerPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1019 | return spv::BuiltInLayerPerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1020 | case glslang::EbvMeshViewCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1021 | return spv::BuiltInMeshViewCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1022 | case glslang::EbvMeshViewIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1023 | return spv::BuiltInMeshViewIndicesNV; |
Daniel Koch | 2cb2f19 | 2019-06-04 08:43:32 -0400 | [diff] [blame] | 1024 | |
| 1025 | // sm builtins |
| 1026 | case glslang::EbvWarpsPerSM: |
| 1027 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1028 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1029 | return spv::BuiltInWarpsPerSMNV; |
| 1030 | case glslang::EbvSMCount: |
| 1031 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1032 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1033 | return spv::BuiltInSMCountNV; |
| 1034 | case glslang::EbvWarpID: |
| 1035 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1036 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1037 | return spv::BuiltInWarpIDNV; |
| 1038 | case glslang::EbvSMID: |
| 1039 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1040 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1041 | return spv::BuiltInSMIDNV; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1042 | #endif |
| 1043 | |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 1044 | default: |
| 1045 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1049 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1050 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1051 | { |
| 1052 | assert(type.getBasicType() == glslang::EbtSampler); |
| 1053 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1054 | #ifdef GLSLANG_WEB |
| 1055 | return spv::ImageFormatUnknown; |
| 1056 | #endif |
| 1057 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1058 | // Check for capabilities |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1059 | switch (type.getQualifier().getFormat()) { |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1060 | case glslang::ElfRg32f: |
| 1061 | case glslang::ElfRg16f: |
| 1062 | case glslang::ElfR11fG11fB10f: |
| 1063 | case glslang::ElfR16f: |
| 1064 | case glslang::ElfRgba16: |
| 1065 | case glslang::ElfRgb10A2: |
| 1066 | case glslang::ElfRg16: |
| 1067 | case glslang::ElfRg8: |
| 1068 | case glslang::ElfR16: |
| 1069 | case glslang::ElfR8: |
| 1070 | case glslang::ElfRgba16Snorm: |
| 1071 | case glslang::ElfRg16Snorm: |
| 1072 | case glslang::ElfRg8Snorm: |
| 1073 | case glslang::ElfR16Snorm: |
| 1074 | case glslang::ElfR8Snorm: |
| 1075 | |
| 1076 | case glslang::ElfRg32i: |
| 1077 | case glslang::ElfRg16i: |
| 1078 | case glslang::ElfRg8i: |
| 1079 | case glslang::ElfR16i: |
| 1080 | case glslang::ElfR8i: |
| 1081 | |
| 1082 | case glslang::ElfRgb10a2ui: |
| 1083 | case glslang::ElfRg32ui: |
| 1084 | case glslang::ElfRg16ui: |
| 1085 | case glslang::ElfRg8ui: |
| 1086 | case glslang::ElfR16ui: |
| 1087 | case glslang::ElfR8ui: |
| 1088 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 1089 | break; |
| 1090 | |
Tobski | 8c1a3a0 | 2020-11-04 16:24:23 +0000 | [diff] [blame] | 1091 | case glslang::ElfR64ui: |
| 1092 | case glslang::ElfR64i: |
| 1093 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 1094 | builder.addCapability(spv::CapabilityInt64ImageEXT); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1095 | default: |
| 1096 | break; |
| 1097 | } |
| 1098 | |
| 1099 | // do the translation |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1100 | switch (type.getQualifier().getFormat()) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1101 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1102 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1103 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1104 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1105 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1106 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1107 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1108 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1109 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1110 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1111 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1112 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1113 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1114 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1115 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1116 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1117 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1118 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1119 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1120 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1121 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1122 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1123 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1124 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1125 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1126 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1127 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1128 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1129 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1130 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1131 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1132 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1133 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1134 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1135 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1136 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1137 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1138 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1139 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1140 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
Tobski | 8c1a3a0 | 2020-11-04 16:24:23 +0000 | [diff] [blame] | 1141 | case glslang::ElfR64ui: return spv::ImageFormatR64ui; |
| 1142 | case glslang::ElfR64i: return spv::ImageFormatR64i; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1143 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1147 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl( |
| 1148 | const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1149 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1150 | if (selectionNode.getFlatten()) |
| 1151 | return spv::SelectionControlFlattenMask; |
| 1152 | if (selectionNode.getDontFlatten()) |
| 1153 | return spv::SelectionControlDontFlattenMask; |
| 1154 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1155 | } |
| 1156 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1157 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) |
| 1158 | const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1159 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1160 | if (switchNode.getFlatten()) |
| 1161 | return spv::SelectionControlFlattenMask; |
| 1162 | if (switchNode.getDontFlatten()) |
| 1163 | return spv::SelectionControlDontFlattenMask; |
| 1164 | return spv::SelectionControlMaskNone; |
| 1165 | } |
| 1166 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1167 | // return a non-0 dependency if the dependency argument must be set |
| 1168 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1169 | std::vector<unsigned int>& operands) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1170 | { |
| 1171 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1172 | |
| 1173 | if (loopNode.getDontUnroll()) |
| 1174 | control = control | spv::LoopControlDontUnrollMask; |
| 1175 | if (loopNode.getUnroll()) |
| 1176 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1177 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1178 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1179 | else if (loopNode.getLoopDependency() > 0) { |
| 1180 | control = control | spv::LoopControlDependencyLengthMask; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1181 | operands.push_back((unsigned int)loopNode.getLoopDependency()); |
| 1182 | } |
| 1183 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 1184 | if (loopNode.getMinIterations() > 0) { |
| 1185 | control = control | spv::LoopControlMinIterationsMask; |
| 1186 | operands.push_back(loopNode.getMinIterations()); |
| 1187 | } |
| 1188 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
| 1189 | control = control | spv::LoopControlMaxIterationsMask; |
| 1190 | operands.push_back(loopNode.getMaxIterations()); |
| 1191 | } |
| 1192 | if (loopNode.getIterationMultiple() > 1) { |
| 1193 | control = control | spv::LoopControlIterationMultipleMask; |
| 1194 | operands.push_back(loopNode.getIterationMultiple()); |
| 1195 | } |
| 1196 | if (loopNode.getPeelCount() > 0) { |
| 1197 | control = control | spv::LoopControlPeelCountMask; |
| 1198 | operands.push_back(loopNode.getPeelCount()); |
| 1199 | } |
| 1200 | if (loopNode.getPartialCount() > 0) { |
| 1201 | control = control | spv::LoopControlPartialCountMask; |
| 1202 | operands.push_back(loopNode.getPartialCount()); |
| 1203 | } |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1204 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1205 | |
| 1206 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1207 | } |
| 1208 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1209 | // Translate glslang type to SPIR-V storage class. |
| 1210 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1211 | { |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 1212 | if (type.getBasicType() == glslang::EbtRayQuery) |
Neslisah Torosdagli | 054b5e3 | 2020-03-26 12:24:31 -0400 | [diff] [blame] | 1213 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1214 | if (type.getQualifier().isPipeInput()) |
| 1215 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1216 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1217 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1218 | |
| 1219 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1220 | type.getQualifier().storage == glslang::EvqUniform) { |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1221 | if (type.isAtomic()) |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1222 | return spv::StorageClassAtomicCounter; |
| 1223 | if (type.containsOpaque()) |
| 1224 | return spv::StorageClassUniformConstant; |
| 1225 | } |
| 1226 | |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1227 | if (type.getQualifier().isUniformOrBuffer() && |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1228 | type.getQualifier().isShaderRecord()) { |
| 1229 | return spv::StorageClassShaderRecordBufferKHR; |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1230 | } |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1231 | |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1232 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1233 | 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] | 1234 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1238 | if (type.getQualifier().isPushConstant()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1239 | return spv::StorageClassPushConstant; |
| 1240 | if (type.getBasicType() == glslang::EbtBlock) |
| 1241 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1242 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1243 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1244 | |
| 1245 | switch (type.getQualifier().storage) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 1246 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1247 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1248 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1249 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 1250 | #ifndef GLSLANG_WEB |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1251 | case glslang::EvqPayload: return spv::StorageClassRayPayloadKHR; |
| 1252 | case glslang::EvqPayloadIn: return spv::StorageClassIncomingRayPayloadKHR; |
| 1253 | case glslang::EvqHitAttr: return spv::StorageClassHitAttributeKHR; |
| 1254 | case glslang::EvqCallableData: return spv::StorageClassCallableDataKHR; |
| 1255 | case glslang::EvqCallableDataIn: return spv::StorageClassIncomingCallableDataKHR; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1256 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1257 | default: |
| 1258 | assert(0); |
| 1259 | break; |
| 1260 | } |
| 1261 | |
| 1262 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1263 | } |
| 1264 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1265 | // Add capabilities pertaining to how an array is indexed. |
| 1266 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1267 | const glslang::TType& indexType) |
| 1268 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1269 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1270 | if (indexType.getQualifier().isNonUniform()) { |
| 1271 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1272 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1273 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1274 | if (baseType.getQualifier().hasAttachment()) |
| 1275 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1276 | else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1277 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1278 | else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1279 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1280 | else if (baseType.isImage()) |
| 1281 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1282 | else if (baseType.isTexture()) |
| 1283 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1284 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1285 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1286 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1287 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1288 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1289 | } |
| 1290 | } else { |
| 1291 | // assume a dynamically uniform index |
| 1292 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1293 | if (baseType.getQualifier().hasAttachment()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1294 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1295 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1296 | } else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1297 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1298 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1299 | } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1300 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1301 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1302 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1303 | } |
| 1304 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1305 | #endif |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1306 | } |
| 1307 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1308 | // 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] | 1309 | // descriptor set. |
| 1310 | bool IsDescriptorResource(const glslang::TType& type) |
| 1311 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1312 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1313 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1314 | return type.getQualifier().isUniformOrBuffer() && |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1315 | ! type.getQualifier().isShaderRecord() && |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1316 | ! type.getQualifier().isPushConstant(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1317 | |
| 1318 | // non block... |
| 1319 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1320 | // if they are the global-scope-class, not the function parameter |
| 1321 | // (or local, if they ever exist) class. |
alelenv | 999d4fd | 2020-06-01 23:24:41 -0700 | [diff] [blame] | 1322 | if (type.getBasicType() == glslang::EbtSampler || |
| 1323 | type.getBasicType() == glslang::EbtAccStruct) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1324 | return type.getQualifier().isUniformOrBuffer(); |
| 1325 | |
| 1326 | // None of the above. |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1330 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1331 | { |
| 1332 | if (child.layoutMatrix == glslang::ElmNone) |
| 1333 | child.layoutMatrix = parent.layoutMatrix; |
| 1334 | |
| 1335 | if (parent.invariant) |
| 1336 | child.invariant = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1337 | if (parent.flat) |
| 1338 | child.flat = true; |
| 1339 | if (parent.centroid) |
| 1340 | child.centroid = true; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1341 | #ifndef GLSLANG_WEB |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1342 | if (parent.nopersp) |
| 1343 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1344 | if (parent.explicitInterp) |
| 1345 | child.explicitInterp = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1346 | if (parent.perPrimitiveNV) |
| 1347 | child.perPrimitiveNV = true; |
| 1348 | if (parent.perViewNV) |
| 1349 | child.perViewNV = true; |
| 1350 | if (parent.perTaskNV) |
| 1351 | child.perTaskNV = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1352 | if (parent.patch) |
| 1353 | child.patch = true; |
| 1354 | if (parent.sample) |
| 1355 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1356 | if (parent.coherent) |
| 1357 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1358 | if (parent.devicecoherent) |
| 1359 | child.devicecoherent = true; |
| 1360 | if (parent.queuefamilycoherent) |
| 1361 | child.queuefamilycoherent = true; |
| 1362 | if (parent.workgroupcoherent) |
| 1363 | child.workgroupcoherent = true; |
| 1364 | if (parent.subgroupcoherent) |
| 1365 | child.subgroupcoherent = true; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1366 | if (parent.shadercallcoherent) |
| 1367 | child.shadercallcoherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1368 | if (parent.nonprivate) |
| 1369 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1370 | if (parent.volatil) |
| 1371 | child.volatil = true; |
| 1372 | if (parent.restrict) |
| 1373 | child.restrict = true; |
| 1374 | if (parent.readonly) |
| 1375 | child.readonly = true; |
| 1376 | if (parent.writeonly) |
| 1377 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1378 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1381 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1382 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1383 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1384 | // - struct members might inherit from a struct declaration |
| 1385 | // (note that non-block structs don't explicitly inherit, |
| 1386 | // only implicitly, meaning no decoration involved) |
| 1387 | // - affect decorations on the struct members |
| 1388 | // (note smooth does not, and expecting something like volatile |
| 1389 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1390 | // - 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] | 1391 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1394 | // |
| 1395 | // Implement the TGlslangToSpvTraverser class. |
| 1396 | // |
| 1397 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1398 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, |
| 1399 | const glslang::TIntermediate* glslangIntermediate, |
| 1400 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) : |
| 1401 | TIntermTraverser(true, false, true), |
| 1402 | options(options), |
| 1403 | shaderEntry(nullptr), currentFunction(nullptr), |
| 1404 | sequenceDepth(0), logger(buildLogger), |
| 1405 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
| 1406 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
| 1407 | glslangIntermediate(glslangIntermediate), |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 1408 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()), |
| 1409 | nonSemanticDebugPrintf(0) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1410 | { |
| 1411 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1412 | |
| 1413 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1414 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1415 | glslangIntermediate->getVersion()); |
| 1416 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1417 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1418 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1419 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1420 | |
| 1421 | // Set the source shader's text. If for SPV version 1.0, include |
| 1422 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1423 | // Otherwise, emit those as actual instructions. |
| 1424 | std::string text; |
| 1425 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1426 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1427 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1428 | text.append("// OpModuleProcessed "); |
| 1429 | text.append(processes[p]); |
| 1430 | text.append("\n"); |
| 1431 | } else |
| 1432 | builder.addModuleProcessed(processes[p]); |
| 1433 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1434 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1435 | text.append("#line 1\n"); |
| 1436 | text.append(glslangIntermediate->getSourceText()); |
| 1437 | builder.setSourceText(text); |
Greg Fischer | d445bb2 | 2018-12-06 11:13:15 -0700 | [diff] [blame] | 1438 | // Pass name and text for all included files |
| 1439 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1440 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1441 | builder.addInclude(iItr->first, iItr->second); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1442 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1443 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1444 | |
| 1445 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1446 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1447 | |
| 1448 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1449 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
John Kessenich | 1ff0c18 | 2019-10-10 12:01:13 -0600 | [diff] [blame] | 1450 | 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] | 1451 | builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1452 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1453 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1454 | memoryModel = spv::MemoryModelVulkanKHR; |
| 1455 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1456 | 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] | 1457 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1458 | builder.setMemoryModel(addressingModel, memoryModel); |
| 1459 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 1460 | if (glslangIntermediate->usingVariablePointers()) { |
| 1461 | builder.addCapability(spv::CapabilityVariablePointers); |
| 1462 | } |
| 1463 | |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1464 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1465 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1466 | |
| 1467 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1468 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1469 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
johnkslang | 0138472 | 2020-08-14 08:40:06 -0600 | [diff] [blame] | 1470 | builder.addSourceExtension(it->c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1471 | |
| 1472 | // Add the top-level modes for this shader. |
| 1473 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1474 | if (glslangIntermediate->getXfbMode()) { |
| 1475 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1476 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1477 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1478 | |
alelenv | 59216d5 | 2020-05-21 04:38:41 -0700 | [diff] [blame] | 1479 | if (glslangIntermediate->getLayoutPrimitiveCulling()) { |
alelenv | 75de196 | 2020-04-08 21:09:20 -0700 | [diff] [blame] | 1480 | builder.addCapability(spv::CapabilityRayTraversalPrimitiveCullingProvisionalKHR); |
| 1481 | } |
| 1482 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1483 | unsigned int mode; |
| 1484 | switch (glslangIntermediate->getStage()) { |
| 1485 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1486 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1487 | break; |
| 1488 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1489 | case EShLangFragment: |
| 1490 | builder.addCapability(spv::CapabilityShader); |
| 1491 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1492 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
| 1493 | |
| 1494 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1495 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
| 1496 | else |
| 1497 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
| 1498 | |
| 1499 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1500 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1501 | |
| 1502 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1503 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1504 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1505 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1506 | } |
| 1507 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1508 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1509 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
| 1510 | |
| 1511 | #ifndef GLSLANG_WEB |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 1512 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1513 | switch(glslangIntermediate->getDepth()) { |
| 1514 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1515 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
| 1516 | default: mode = spv::ExecutionModeMax; break; |
| 1517 | } |
| 1518 | if (mode != spv::ExecutionModeMax) |
| 1519 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1520 | switch (glslangIntermediate->getInterlockOrdering()) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 1521 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; |
| 1522 | break; |
| 1523 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; |
| 1524 | break; |
| 1525 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; |
| 1526 | break; |
| 1527 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; |
| 1528 | break; |
| 1529 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; |
| 1530 | break; |
| 1531 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; |
| 1532 | break; |
| 1533 | default: mode = spv::ExecutionModeMax; |
| 1534 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1535 | } |
| 1536 | if (mode != spv::ExecutionModeMax) { |
| 1537 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1538 | if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
| 1539 | mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
| 1540 | builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
| 1541 | } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
| 1542 | mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
| 1543 | builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT); |
| 1544 | } else { |
| 1545 | builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT); |
| 1546 | } |
| 1547 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 1548 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1549 | #endif |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 1550 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1551 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1552 | case EShLangCompute: |
| 1553 | builder.addCapability(spv::CapabilityShader); |
| 1554 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1555 | glslangIntermediate->getLocalSize(1), |
| 1556 | glslangIntermediate->getLocalSize(2)); |
| 1557 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1558 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1559 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1560 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1561 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1562 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1563 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1564 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1565 | } |
| 1566 | break; |
John Kessenich | 51ed01c | 2019-10-10 11:40:11 -0600 | [diff] [blame] | 1567 | #ifndef GLSLANG_WEB |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1568 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1569 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1570 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1571 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1572 | glslang::TLayoutGeometry primitive; |
| 1573 | |
| 1574 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1575 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1576 | glslangIntermediate->getVertices()); |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1577 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1578 | } else { |
| 1579 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1580 | } |
| 1581 | |
| 1582 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1583 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1584 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1585 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1586 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1587 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1588 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1589 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1590 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1591 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1592 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1593 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1594 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1595 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1596 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1597 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1598 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1599 | |
| 1600 | switch (glslangIntermediate->getVertexOrder()) { |
| 1601 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1602 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1603 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1604 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1605 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1606 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1607 | |
| 1608 | if (glslangIntermediate->getPointMode()) |
| 1609 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1610 | break; |
| 1611 | |
| 1612 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1613 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1614 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1615 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1616 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1617 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1618 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1619 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1620 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1621 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1622 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1623 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1624 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1625 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1626 | |
| 1627 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1628 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1629 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1630 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1631 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1632 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1633 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1634 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1635 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1636 | break; |
| 1637 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1638 | case EShLangRayGen: |
| 1639 | case EShLangIntersect: |
| 1640 | case EShLangAnyHit: |
| 1641 | case EShLangClosestHit: |
| 1642 | case EShLangMiss: |
| 1643 | case EShLangCallable: |
| 1644 | { |
| 1645 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 1646 | if (extensions.find("GL_NV_ray_tracing") == extensions.end()) { |
| 1647 | builder.addCapability(spv::CapabilityRayTracingProvisionalKHR); |
| 1648 | builder.addExtension("SPV_KHR_ray_tracing"); |
| 1649 | } |
| 1650 | else { |
| 1651 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1652 | builder.addExtension("SPV_NV_ray_tracing"); |
| 1653 | } |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1654 | break; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1655 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1656 | case EShLangTaskNV: |
| 1657 | case EShLangMeshNV: |
| 1658 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1659 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1660 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1661 | glslangIntermediate->getLocalSize(1), |
| 1662 | glslangIntermediate->getLocalSize(2)); |
| 1663 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1664 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1665 | glslangIntermediate->getVertices()); |
| 1666 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, |
| 1667 | glslangIntermediate->getPrimitives()); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1668 | |
| 1669 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1670 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1671 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1672 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1673 | default: mode = spv::ExecutionModeMax; break; |
| 1674 | } |
| 1675 | if (mode != spv::ExecutionModeMax) |
| 1676 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1677 | } |
| 1678 | break; |
| 1679 | #endif |
| 1680 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1681 | default: |
| 1682 | break; |
| 1683 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1684 | } |
| 1685 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1686 | // Finish creating SPV, after the traversal is complete. |
| 1687 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1688 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1689 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1690 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1691 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1692 | builder.leaveFunction(); |
| 1693 | } |
| 1694 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1695 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1696 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1697 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1698 | |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 1699 | // Add capabilities, extensions, remove unneeded decorations, etc., |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1700 | // based on the resulting SPIR-V. |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 1701 | // Note: WebGPU code generation must have the opportunity to aggressively |
| 1702 | // prune unreachable merge blocks and continue targets. |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1703 | builder.postProcess(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1706 | // Write the SPV into 'out'. |
| 1707 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1708 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1709 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | // |
| 1713 | // Implement the traversal functions. |
| 1714 | // |
| 1715 | // Return true from interior nodes to have the external traversal |
| 1716 | // continue on to children. Return false if children were |
| 1717 | // already processed. |
| 1718 | // |
| 1719 | |
| 1720 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1721 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1722 | // - uniform/input reads |
| 1723 | // - output writes |
| 1724 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1725 | // - something simple that degenerates into the last bullet |
| 1726 | // |
| 1727 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1728 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1729 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1730 | if (symbol->getType().isStruct()) |
| 1731 | glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId(); |
| 1732 | |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1733 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1734 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1735 | |
Rex Xu | f6e0fe8 | 2020-10-23 22:54:35 +0800 | [diff] [blame] | 1736 | #ifdef ENABLE_HLSL |
| 1737 | // Skip symbol handling if it is string-typed |
| 1738 | if (symbol->getBasicType() == glslang::EbtString) |
| 1739 | return; |
| 1740 | #endif |
| 1741 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1742 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1743 | // Formal function parameters were mapped during makeFunctions(). |
| 1744 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1745 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1746 | if (builder.isPointer(id)) { |
John Kessenich | c30d335 | 2020-06-10 07:15:24 -0600 | [diff] [blame] | 1747 | if (!symbol->getType().getQualifier().isParamInput() && |
| 1748 | !symbol->getType().getQualifier().isParamOutput()) { |
| 1749 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1750 | // Consider adding to the OpEntryPoint interface list. |
| 1751 | // Only looking at structures if they have at least one member. |
| 1752 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
| 1753 | spv::StorageClass sc = builder.getStorageClass(id); |
| 1754 | // Before SPIR-V 1.4, we only want to include Input and Output. |
| 1755 | // Starting with SPIR-V 1.4, we want all globals. |
John Kessenich | 4e13c90 | 2020-07-13 00:35:58 -0600 | [diff] [blame] | 1756 | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(id)) || |
John Kessenich | c30d335 | 2020-06-10 07:15:24 -0600 | [diff] [blame] | 1757 | (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
| 1758 | iOSet.insert(id); |
| 1759 | } |
John Kessenich | 7c7731e | 2019-01-04 16:47:06 +0700 | [diff] [blame] | 1760 | } |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1761 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1762 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1763 | // If the SPIR-V type is required to be different than the AST type |
| 1764 | // (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices), |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1765 | // translate now from the SPIR-V type to the AST type, for the consuming |
| 1766 | // operation. |
| 1767 | // Note this turns it from an l-value to an r-value. |
| 1768 | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
| 1769 | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
| 1770 | id = translateForcedType(id); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1774 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1775 | // Prepare to generate code for the access |
| 1776 | |
| 1777 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1778 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1779 | // followed by setting the base. |
| 1780 | builder.clearAccessChain(); |
| 1781 | |
| 1782 | // 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] | 1783 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1784 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1785 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1786 | // 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] | 1787 | // These are also pure R-values. |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1788 | // C) R-Values from type translation, see above call to translateForcedType() |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1789 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1790 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
| 1791 | !builder.isPointerType(builder.getTypeId(id))) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1792 | builder.setAccessChainRValue(id); |
| 1793 | else |
| 1794 | builder.setAccessChainLValue(id); |
| 1795 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1796 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1797 | #ifdef ENABLE_HLSL |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1798 | // Process linkage-only nodes for any special additional interface work. |
| 1799 | if (linkageOnly) { |
| 1800 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1801 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1802 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1803 | // of declaration. |
| 1804 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1805 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1806 | // Save possible originating buffers for counter buffers, keyed by |
| 1807 | // making the potential counter-buffer name. |
| 1808 | std::string keyName = symbol->getName().c_str(); |
| 1809 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1810 | counterOriginator[keyName] = symbol; |
| 1811 | } else { |
| 1812 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1813 | std::string keyName = symbol->getName().c_str(); |
| 1814 | auto it = counterOriginator.find(keyName); |
| 1815 | if (it != counterOriginator.end()) { |
| 1816 | id = getSymbolId(it->second); |
| 1817 | if (id != spv::NoResult) { |
| 1818 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1819 | if (counterId != spv::NoResult) { |
| 1820 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1821 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1822 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 1829 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1833 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1834 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1835 | if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) { |
| 1836 | glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId(); |
| 1837 | } |
| 1838 | if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) { |
| 1839 | glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId(); |
| 1840 | } |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1841 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1842 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1843 | if (node->getType().getQualifier().isSpecConstant()) |
| 1844 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1845 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1846 | // First, handle special cases |
| 1847 | switch (node->getOp()) { |
| 1848 | case glslang::EOpAssign: |
| 1849 | case glslang::EOpAddAssign: |
| 1850 | case glslang::EOpSubAssign: |
| 1851 | case glslang::EOpMulAssign: |
| 1852 | case glslang::EOpVectorTimesMatrixAssign: |
| 1853 | case glslang::EOpVectorTimesScalarAssign: |
| 1854 | case glslang::EOpMatrixTimesScalarAssign: |
| 1855 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1856 | case glslang::EOpDivAssign: |
| 1857 | case glslang::EOpModAssign: |
| 1858 | case glslang::EOpAndAssign: |
| 1859 | case glslang::EOpInclusiveOrAssign: |
| 1860 | case glslang::EOpExclusiveOrAssign: |
| 1861 | case glslang::EOpLeftShiftAssign: |
| 1862 | case glslang::EOpRightShiftAssign: |
| 1863 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1864 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1865 | // says to evaluate the left before the right. So, always, left |
| 1866 | // node then right node. |
| 1867 | { |
| 1868 | // get the left l-value, save it away |
| 1869 | builder.clearAccessChain(); |
| 1870 | node->getLeft()->traverse(this); |
| 1871 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1872 | |
| 1873 | // evaluate the right |
| 1874 | builder.clearAccessChain(); |
| 1875 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1876 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1877 | |
| 1878 | if (node->getOp() != glslang::EOpAssign) { |
| 1879 | // the left is also an r-value |
| 1880 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1881 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1882 | |
| 1883 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1884 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1885 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1886 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1887 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1888 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1889 | node->getType().getBasicType()); |
| 1890 | |
| 1891 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1892 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | // store the result |
| 1896 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1897 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1898 | |
| 1899 | // assignments are expressions having an rValue after they are evaluated... |
| 1900 | builder.clearAccessChain(); |
| 1901 | builder.setAccessChainRValue(rValue); |
| 1902 | } |
| 1903 | return false; |
| 1904 | case glslang::EOpIndexDirect: |
| 1905 | case glslang::EOpIndexDirectStruct: |
| 1906 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1907 | // Structure, array, matrix, or vector indirection with statically known index. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1908 | // Get the left part of the access chain. |
| 1909 | node->getLeft()->traverse(this); |
| 1910 | |
| 1911 | // Add the next element in the chain |
| 1912 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1913 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1914 | if (! node->getLeft()->getType().isArray() && |
| 1915 | node->getLeft()->getType().isVector() && |
| 1916 | node->getOp() == glslang::EOpIndexDirect) { |
| 1917 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1918 | // so short circuit the access-chain stuff with a swizzle. |
| 1919 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1920 | swizzle.push_back(glslangIndex); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1921 | int dummySize; |
| 1922 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1923 | TranslateCoherent(node->getLeft()->getType()), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1924 | glslangIntermediate->getBaseAlignmentScalar( |
| 1925 | node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1926 | } else { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1927 | |
| 1928 | // Load through a block reference is performed with a dot operator that |
| 1929 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 1930 | // do a load and reset the access chain. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1931 | if (node->getLeft()->isReference() && |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1932 | !node->getLeft()->getType().isArray() && |
| 1933 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1934 | { |
| 1935 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 1936 | builder.clearAccessChain(); |
| 1937 | builder.setAccessChainLValue(left); |
| 1938 | } |
| 1939 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1940 | int spvIndex = glslangIndex; |
| 1941 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1942 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1943 | { |
| 1944 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1945 | // index remapping due to hidden members in anonymous blocks. |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1946 | int glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()]; |
| 1947 | if (memberRemapper.find(glslangId) != memberRemapper.end()) { |
| 1948 | std::vector<int>& remapper = memberRemapper[glslangId]; |
| 1949 | assert(remapper.size() > 0); |
| 1950 | spvIndex = remapper[glslangIndex]; |
| 1951 | } |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1952 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1953 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1954 | // normal case for indexing array or structure or block |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1955 | builder.accessChainPush(builder.makeIntConstant(spvIndex), |
| 1956 | TranslateCoherent(node->getLeft()->getType()), |
| 1957 | node->getLeft()->getType().getBufferReferenceAlignment()); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1958 | |
| 1959 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1960 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1961 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1962 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | return false; |
| 1966 | case glslang::EOpIndexIndirect: |
| 1967 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1968 | // Array, matrix, or vector indirection with variable index. |
| 1969 | // Will use native SPIR-V access-chain for and array indirection; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1970 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1971 | // Will use the access chain's 'component' for variable index into a vector. |
| 1972 | |
| 1973 | // This adapter is building access chains left to right. |
| 1974 | // Set up the access chain to the left. |
| 1975 | node->getLeft()->traverse(this); |
| 1976 | |
| 1977 | // save it so that computing the right side doesn't trash it |
| 1978 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1979 | |
| 1980 | // compute the next index in the chain |
| 1981 | builder.clearAccessChain(); |
| 1982 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1983 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1984 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1985 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1986 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1987 | // restore the saved access chain |
| 1988 | builder.setAccessChain(partial); |
| 1989 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1990 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 1991 | int dummySize; |
| 1992 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1993 | TranslateCoherent(node->getLeft()->getType()), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1994 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 1995 | dummySize)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1996 | } else |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1997 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), |
| 1998 | node->getLeft()->getType().getBufferReferenceAlignment()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1999 | } |
| 2000 | return false; |
| 2001 | case glslang::EOpVectorSwizzle: |
| 2002 | { |
| 2003 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2004 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2005 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2006 | int dummySize; |
| 2007 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 2008 | TranslateCoherent(node->getLeft()->getType()), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2009 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 2010 | dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2011 | } |
| 2012 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 2013 | case glslang::EOpMatrixSwizzle: |
| 2014 | logger->missingFunctionality("matrix swizzle"); |
| 2015 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 2016 | case glslang::EOpLogicalOr: |
| 2017 | case glslang::EOpLogicalAnd: |
| 2018 | { |
| 2019 | |
| 2020 | // These may require short circuiting, but can sometimes be done as straight |
| 2021 | // binary operations. The right operand must be short circuited if it has |
| 2022 | // side effects, and should probably be if it is complex. |
| 2023 | if (isTrivial(node->getRight()->getAsTyped())) |
| 2024 | break; // handle below as a normal binary operation |
| 2025 | // otherwise, we need to do dynamic short circuiting on the right operand |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2026 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), |
| 2027 | *node->getRight()->getAsTyped()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 2028 | builder.clearAccessChain(); |
| 2029 | builder.setAccessChainRValue(result); |
| 2030 | } |
| 2031 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2032 | default: |
| 2033 | break; |
| 2034 | } |
| 2035 | |
| 2036 | // Assume generic binary op... |
| 2037 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2038 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2039 | builder.clearAccessChain(); |
| 2040 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2041 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2042 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2043 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2044 | builder.clearAccessChain(); |
| 2045 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2046 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2047 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2048 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2049 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2050 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2051 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2052 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2053 | convertGlslangToSpvType(node->getType()), left, right, |
| 2054 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2055 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2056 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2057 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2058 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2059 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2060 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2061 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2062 | return false; |
| 2063 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2064 | } |
| 2065 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2066 | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
| 2067 | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
| 2068 | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2069 | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn, |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2070 | const glslang::TType& glslangType) |
| 2071 | { |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2072 | switch(glslangBuiltIn) |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2073 | { |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2074 | case glslang::EbvSubGroupEqMask: |
| 2075 | case glslang::EbvSubGroupGeMask: |
| 2076 | case glslang::EbvSubGroupGtMask: |
| 2077 | case glslang::EbvSubGroupLeMask: |
| 2078 | case glslang::EbvSubGroupLtMask: { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2079 | // these require changing a 64-bit scaler -> a vector of 32-bit components |
| 2080 | if (glslangType.isVector()) |
| 2081 | break; |
| 2082 | std::pair<spv::Id, spv::Id> ret(builder.makeVectorType(builder.makeUintType(32), 4), |
| 2083 | builder.makeUintType(64)); |
| 2084 | return ret; |
| 2085 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2086 | // There are no SPIR-V builtins defined for these and map onto original non-transposed |
| 2087 | // builtins. During visitBinary we insert a transpose |
| 2088 | case glslang::EbvWorldToObject3x4: |
| 2089 | case glslang::EbvObjectToWorld3x4: { |
John Kessenich | f80ef74 | 2020-07-14 01:44:35 -0600 | [diff] [blame] | 2090 | spv::Id mat43 = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 2091 | spv::Id mat34 = builder.makeMatrixType(builder.makeFloatType(32), 3, 4); |
| 2092 | std::pair<spv::Id, spv::Id> ret(mat43, mat34); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2093 | return ret; |
| 2094 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2095 | default: |
| 2096 | break; |
| 2097 | } |
| 2098 | |
| 2099 | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
| 2100 | return ret; |
| 2101 | } |
| 2102 | |
| 2103 | // For an object previously identified (see getForcedType() and forceType) |
| 2104 | // as needing type translations, do the translation needed for a load, turning |
| 2105 | // an L-value into in R-value. |
| 2106 | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
| 2107 | { |
| 2108 | const auto forceIt = forceType.find(object); |
| 2109 | if (forceIt == forceType.end()) |
| 2110 | return object; |
| 2111 | |
| 2112 | spv::Id desiredTypeId = forceIt->second; |
| 2113 | spv::Id objectTypeId = builder.getTypeId(object); |
| 2114 | assert(builder.isPointerType(objectTypeId)); |
| 2115 | objectTypeId = builder.getContainedTypeId(objectTypeId); |
| 2116 | if (builder.isVectorType(objectTypeId) && |
| 2117 | builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
| 2118 | if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
| 2119 | // handle 32-bit v.xy* -> 64-bit |
| 2120 | builder.clearAccessChain(); |
| 2121 | builder.setAccessChainLValue(object); |
| 2122 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 2123 | std::vector<spv::Id> components; |
| 2124 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
| 2125 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
| 2126 | |
| 2127 | spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
| 2128 | return builder.createUnaryOp(spv::OpBitcast, desiredTypeId, |
| 2129 | builder.createCompositeConstruct(vecType, components)); |
| 2130 | } else { |
| 2131 | logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar"); |
| 2132 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2133 | } else if (builder.isMatrixType(objectTypeId)) { |
| 2134 | // There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject |
| 2135 | // and we insert a transpose after loading the original non-transposed builtins |
| 2136 | builder.clearAccessChain(); |
| 2137 | builder.setAccessChainLValue(object); |
| 2138 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 2139 | return builder.createUnaryOp(spv::OpTranspose, desiredTypeId, object); |
| 2140 | |
| 2141 | } else { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2142 | logger->missingFunctionality("forcing non 32-bit vector type"); |
| 2143 | } |
| 2144 | |
| 2145 | return object; |
| 2146 | } |
| 2147 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2148 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 2149 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2150 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2151 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 2152 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2153 | if (node->getType().getQualifier().isSpecConstant()) |
| 2154 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2155 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2156 | spv::Id result = spv::NoResult; |
| 2157 | |
| 2158 | // try texturing first |
| 2159 | result = createImageTextureFunctionCall(node); |
| 2160 | if (result != spv::NoResult) { |
| 2161 | builder.clearAccessChain(); |
| 2162 | builder.setAccessChainRValue(result); |
| 2163 | |
| 2164 | return false; // done with this node |
| 2165 | } |
| 2166 | |
| 2167 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2168 | |
| 2169 | if (node->getOp() == glslang::EOpArrayLength) { |
| 2170 | // Quite special; won't want to evaluate the operand. |
| 2171 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2172 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 2173 | // except for the last block membeor of an SSBO. |
| 2174 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 2175 | // size extracted. |
| 2176 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2177 | // Normal .length() would have been constant folded by the front-end. |
| 2178 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 2179 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2180 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2181 | spv::Id length; |
| 2182 | if (node->getOperand()->getType().isCoopMat()) { |
| 2183 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2184 | |
| 2185 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
| 2186 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2187 | |
| 2188 | length = builder.createCooperativeMatrixLength(typeId); |
| 2189 | } else { |
| 2190 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2191 | block->traverse(this); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2192 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion() |
| 2193 | ->getConstArray()[0].getUConst(); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2194 | length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
| 2195 | } |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2196 | |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2197 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 2198 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 2199 | // AST expectation of a signed result. |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2200 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
| 2201 | if (builder.isInSpecConstCodeGenMode()) { |
| 2202 | length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0)); |
| 2203 | } else { |
| 2204 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 2205 | } |
| 2206 | } |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2207 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2208 | builder.clearAccessChain(); |
| 2209 | builder.setAccessChainRValue(length); |
| 2210 | |
| 2211 | return false; |
| 2212 | } |
| 2213 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2214 | // Start by evaluating the operand |
| 2215 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2216 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2217 | // operate first on the swizzle base, then apply the swizzle. |
| 2218 | spv::Id invertedType = spv::NoType; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2219 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2220 | invertedType : convertGlslangToSpvType(node->getType()); }; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2221 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 2222 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 2223 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2224 | builder.clearAccessChain(); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2225 | TIntermNode *operandNode; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2226 | if (invertedType != spv::NoType) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2227 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2228 | else |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2229 | operandNode = node->getOperand(); |
| 2230 | |
| 2231 | operandNode->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 2232 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2233 | spv::Id operand = spv::NoResult; |
| 2234 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2235 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2236 | |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2237 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2238 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 2239 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2240 | node->getOp() == glslang::EOpAtomicCounter || |
Neslisah Torosdagli | 7d37a68 | 2020-03-26 10:52:33 -0400 | [diff] [blame] | 2241 | node->getOp() == glslang::EOpInterpolateAtCentroid || |
| 2242 | node->getOp() == glslang::EOpRayQueryProceed || |
| 2243 | node->getOp() == glslang::EOpRayQueryGetRayTMin || |
| 2244 | node->getOp() == glslang::EOpRayQueryGetRayFlags || |
| 2245 | node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin || |
| 2246 | node->getOp() == glslang::EOpRayQueryGetWorldRayDirection || |
| 2247 | node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque || |
| 2248 | node->getOp() == glslang::EOpRayQueryTerminate || |
| 2249 | node->getOp() == glslang::EOpRayQueryConfirmIntersection) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2250 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2251 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2252 | lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
| 2253 | } else |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2254 | #endif |
| 2255 | { |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2256 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2257 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2258 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2259 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2260 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2261 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2262 | |
| 2263 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2264 | if (! result) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2265 | result = createConversion(node->getOp(), decorations, resultType(), operand, |
| 2266 | node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2267 | |
| 2268 | // if not, then possibly an operation |
| 2269 | if (! result) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2270 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, |
| 2271 | node->getOperand()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2272 | |
| 2273 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2274 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2275 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2276 | decorations.addNonUniform(builder, result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2277 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2278 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2279 | builder.clearAccessChain(); |
| 2280 | builder.setAccessChainRValue(result); |
| 2281 | |
| 2282 | return false; // done with this node |
| 2283 | } |
| 2284 | |
| 2285 | // it must be a special case, check... |
| 2286 | switch (node->getOp()) { |
| 2287 | case glslang::EOpPostIncrement: |
| 2288 | case glslang::EOpPostDecrement: |
| 2289 | case glslang::EOpPreIncrement: |
| 2290 | case glslang::EOpPreDecrement: |
| 2291 | { |
| 2292 | // 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] | 2293 | spv::Id one = 0; |
| 2294 | if (node->getBasicType() == glslang::EbtFloat) |
| 2295 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2296 | #ifndef GLSLANG_WEB |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 2297 | else if (node->getBasicType() == glslang::EbtDouble) |
| 2298 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2299 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 2300 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2301 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 2302 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2303 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 2304 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2305 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 2306 | one = builder.makeInt64Constant(1); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2307 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2308 | else |
| 2309 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2310 | glslang::TOperator op; |
| 2311 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2312 | node->getOp() == glslang::EOpPostIncrement) |
| 2313 | op = glslang::EOpAdd; |
| 2314 | else |
| 2315 | op = glslang::EOpSub; |
| 2316 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2317 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2318 | convertGlslangToSpvType(node->getType()), operand, one, |
| 2319 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2320 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2321 | |
| 2322 | // The result of operation is always stored, but conditionally the |
| 2323 | // consumed result. The consumed result is always an r-value. |
Bas Nieuwenhuizen | de949a2 | 2020-08-24 23:27:26 +0200 | [diff] [blame] | 2324 | builder.accessChainStore(result, |
| 2325 | TranslateNonUniformDecoration(node->getOperand()->getType().getQualifier())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2326 | builder.clearAccessChain(); |
| 2327 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2328 | node->getOp() == glslang::EOpPreDecrement) |
| 2329 | builder.setAccessChainRValue(result); |
| 2330 | else |
| 2331 | builder.setAccessChainRValue(operand); |
| 2332 | } |
| 2333 | |
| 2334 | return false; |
| 2335 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2336 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2337 | case glslang::EOpEmitStreamVertex: |
| 2338 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 2339 | return false; |
| 2340 | case glslang::EOpEndStreamPrimitive: |
| 2341 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 2342 | return false; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2343 | case glslang::EOpRayQueryTerminate: |
| 2344 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operand); |
| 2345 | return false; |
| 2346 | case glslang::EOpRayQueryConfirmIntersection: |
| 2347 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operand); |
| 2348 | return false; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2349 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2350 | |
| 2351 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2352 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2353 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2354 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2355 | } |
| 2356 | |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2357 | // Construct a composite object, recursively copying members if their types don't match |
| 2358 | spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
| 2359 | { |
| 2360 | for (int c = 0; c < (int)constituents.size(); ++c) { |
| 2361 | spv::Id& constituent = constituents[c]; |
| 2362 | spv::Id lType = builder.getContainedTypeId(resultTypeId, c); |
| 2363 | spv::Id rType = builder.getTypeId(constituent); |
| 2364 | if (lType != rType) { |
| 2365 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 2366 | constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent); |
| 2367 | } else if (builder.isStructType(rType)) { |
| 2368 | std::vector<spv::Id> rTypeConstituents; |
| 2369 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2370 | for (int i = 0; i < numrTypeConstituents; ++i) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2371 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, |
| 2372 | builder.getContainedTypeId(rType, i), i)); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2373 | } |
| 2374 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2375 | } else { |
| 2376 | assert(builder.isArrayType(rType)); |
| 2377 | std::vector<spv::Id> rTypeConstituents; |
| 2378 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2379 | |
| 2380 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2381 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2382 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i)); |
| 2383 | } |
| 2384 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2385 | } |
| 2386 | } |
| 2387 | } |
| 2388 | return builder.createCompositeConstruct(resultTypeId, constituents); |
| 2389 | } |
| 2390 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2391 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2392 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 2393 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2394 | if (node->getType().getQualifier().isSpecConstant()) |
| 2395 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2396 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2397 | spv::Id result = spv::NoResult; |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 2398 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2399 | std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for |
| 2400 | // SPIR-V, for an out parameter |
Bas Nieuwenhuizen | de949a2 | 2020-08-24 23:27:26 +0200 | [diff] [blame] | 2401 | std::vector<glslang::TQualifier> complexLValueQualifiers; |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 2402 | std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2403 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2404 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2405 | invertedType : |
| 2406 | convertGlslangToSpvType(node->getType()); }; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2407 | |
| 2408 | // try texturing |
| 2409 | result = createImageTextureFunctionCall(node); |
| 2410 | if (result != spv::NoResult) { |
| 2411 | builder.clearAccessChain(); |
| 2412 | builder.setAccessChainRValue(result); |
| 2413 | |
| 2414 | return false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2415 | } |
| 2416 | #ifndef GLSLANG_WEB |
| 2417 | else if (node->getOp() == glslang::EOpImageStore || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2418 | node->getOp() == glslang::EOpImageStoreLod || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2419 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2420 | // "imageStore" is a special case, which has no result |
| 2421 | return false; |
| 2422 | } |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2423 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2424 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2425 | glslang::TOperator binOp = glslang::EOpNull; |
| 2426 | bool reduceComparison = true; |
| 2427 | bool isMatrix = false; |
| 2428 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2429 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2430 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2431 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2432 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2433 | assert(node->getOp()); |
| 2434 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 2435 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2436 | |
| 2437 | switch (node->getOp()) { |
| 2438 | case glslang::EOpSequence: |
| 2439 | { |
| 2440 | if (preVisit) |
| 2441 | ++sequenceDepth; |
| 2442 | else |
| 2443 | --sequenceDepth; |
| 2444 | |
| 2445 | if (sequenceDepth == 1) { |
| 2446 | // If this is the parent node of all the functions, we want to see them |
| 2447 | // early, so all call points have actual SPIR-V functions to reference. |
| 2448 | // In all cases, still let the traverser visit the children for us. |
| 2449 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 2450 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2451 | // 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] | 2452 | // anything else gets there, so visit out of order, doing them all now. |
| 2453 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2454 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2455 | // 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] | 2456 | // so do them manually. |
| 2457 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2458 | |
| 2459 | return false; |
| 2460 | } |
| 2461 | |
| 2462 | return true; |
| 2463 | } |
| 2464 | case glslang::EOpLinkerObjects: |
| 2465 | { |
| 2466 | if (visit == glslang::EvPreVisit) |
| 2467 | linkageOnly = true; |
| 2468 | else |
| 2469 | linkageOnly = false; |
| 2470 | |
| 2471 | return true; |
| 2472 | } |
| 2473 | case glslang::EOpComma: |
| 2474 | { |
| 2475 | // processing from left to right naturally leaves the right-most |
| 2476 | // lying around in the access chain |
| 2477 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2478 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2479 | glslangOperands[i]->traverse(this); |
| 2480 | |
| 2481 | return false; |
| 2482 | } |
| 2483 | case glslang::EOpFunction: |
| 2484 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2485 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2486 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2487 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2488 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2489 | } else { |
| 2490 | handleFunctionEntry(node); |
| 2491 | } |
| 2492 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2493 | if (inEntryPoint) |
| 2494 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2495 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2496 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | return true; |
| 2500 | case glslang::EOpParameters: |
| 2501 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2502 | // the body, so we still visited the function node's children, making this |
| 2503 | // child redundant. |
| 2504 | return false; |
| 2505 | case glslang::EOpFunctionCall: |
| 2506 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2507 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2508 | if (node->isUserDefined()) |
| 2509 | result = handleUserFunctionCall(node); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2510 | if (result) { |
| 2511 | builder.clearAccessChain(); |
| 2512 | builder.setAccessChainRValue(result); |
| 2513 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2514 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2515 | |
| 2516 | return false; |
| 2517 | } |
| 2518 | case glslang::EOpConstructMat2x2: |
| 2519 | case glslang::EOpConstructMat2x3: |
| 2520 | case glslang::EOpConstructMat2x4: |
| 2521 | case glslang::EOpConstructMat3x2: |
| 2522 | case glslang::EOpConstructMat3x3: |
| 2523 | case glslang::EOpConstructMat3x4: |
| 2524 | case glslang::EOpConstructMat4x2: |
| 2525 | case glslang::EOpConstructMat4x3: |
| 2526 | case glslang::EOpConstructMat4x4: |
| 2527 | case glslang::EOpConstructDMat2x2: |
| 2528 | case glslang::EOpConstructDMat2x3: |
| 2529 | case glslang::EOpConstructDMat2x4: |
| 2530 | case glslang::EOpConstructDMat3x2: |
| 2531 | case glslang::EOpConstructDMat3x3: |
| 2532 | case glslang::EOpConstructDMat3x4: |
| 2533 | case glslang::EOpConstructDMat4x2: |
| 2534 | case glslang::EOpConstructDMat4x3: |
| 2535 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2536 | case glslang::EOpConstructIMat2x2: |
| 2537 | case glslang::EOpConstructIMat2x3: |
| 2538 | case glslang::EOpConstructIMat2x4: |
| 2539 | case glslang::EOpConstructIMat3x2: |
| 2540 | case glslang::EOpConstructIMat3x3: |
| 2541 | case glslang::EOpConstructIMat3x4: |
| 2542 | case glslang::EOpConstructIMat4x2: |
| 2543 | case glslang::EOpConstructIMat4x3: |
| 2544 | case glslang::EOpConstructIMat4x4: |
| 2545 | case glslang::EOpConstructUMat2x2: |
| 2546 | case glslang::EOpConstructUMat2x3: |
| 2547 | case glslang::EOpConstructUMat2x4: |
| 2548 | case glslang::EOpConstructUMat3x2: |
| 2549 | case glslang::EOpConstructUMat3x3: |
| 2550 | case glslang::EOpConstructUMat3x4: |
| 2551 | case glslang::EOpConstructUMat4x2: |
| 2552 | case glslang::EOpConstructUMat4x3: |
| 2553 | case glslang::EOpConstructUMat4x4: |
| 2554 | case glslang::EOpConstructBMat2x2: |
| 2555 | case glslang::EOpConstructBMat2x3: |
| 2556 | case glslang::EOpConstructBMat2x4: |
| 2557 | case glslang::EOpConstructBMat3x2: |
| 2558 | case glslang::EOpConstructBMat3x3: |
| 2559 | case glslang::EOpConstructBMat3x4: |
| 2560 | case glslang::EOpConstructBMat4x2: |
| 2561 | case glslang::EOpConstructBMat4x3: |
| 2562 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2563 | case glslang::EOpConstructF16Mat2x2: |
| 2564 | case glslang::EOpConstructF16Mat2x3: |
| 2565 | case glslang::EOpConstructF16Mat2x4: |
| 2566 | case glslang::EOpConstructF16Mat3x2: |
| 2567 | case glslang::EOpConstructF16Mat3x3: |
| 2568 | case glslang::EOpConstructF16Mat3x4: |
| 2569 | case glslang::EOpConstructF16Mat4x2: |
| 2570 | case glslang::EOpConstructF16Mat4x3: |
| 2571 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2572 | isMatrix = true; |
| 2573 | // fall through |
| 2574 | case glslang::EOpConstructFloat: |
| 2575 | case glslang::EOpConstructVec2: |
| 2576 | case glslang::EOpConstructVec3: |
| 2577 | case glslang::EOpConstructVec4: |
| 2578 | case glslang::EOpConstructDouble: |
| 2579 | case glslang::EOpConstructDVec2: |
| 2580 | case glslang::EOpConstructDVec3: |
| 2581 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2582 | case glslang::EOpConstructFloat16: |
| 2583 | case glslang::EOpConstructF16Vec2: |
| 2584 | case glslang::EOpConstructF16Vec3: |
| 2585 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2586 | case glslang::EOpConstructBool: |
| 2587 | case glslang::EOpConstructBVec2: |
| 2588 | case glslang::EOpConstructBVec3: |
| 2589 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2590 | case glslang::EOpConstructInt8: |
| 2591 | case glslang::EOpConstructI8Vec2: |
| 2592 | case glslang::EOpConstructI8Vec3: |
| 2593 | case glslang::EOpConstructI8Vec4: |
| 2594 | case glslang::EOpConstructUint8: |
| 2595 | case glslang::EOpConstructU8Vec2: |
| 2596 | case glslang::EOpConstructU8Vec3: |
| 2597 | case glslang::EOpConstructU8Vec4: |
| 2598 | case glslang::EOpConstructInt16: |
| 2599 | case glslang::EOpConstructI16Vec2: |
| 2600 | case glslang::EOpConstructI16Vec3: |
| 2601 | case glslang::EOpConstructI16Vec4: |
| 2602 | case glslang::EOpConstructUint16: |
| 2603 | case glslang::EOpConstructU16Vec2: |
| 2604 | case glslang::EOpConstructU16Vec3: |
| 2605 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2606 | case glslang::EOpConstructInt: |
| 2607 | case glslang::EOpConstructIVec2: |
| 2608 | case glslang::EOpConstructIVec3: |
| 2609 | case glslang::EOpConstructIVec4: |
| 2610 | case glslang::EOpConstructUint: |
| 2611 | case glslang::EOpConstructUVec2: |
| 2612 | case glslang::EOpConstructUVec3: |
| 2613 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2614 | case glslang::EOpConstructInt64: |
| 2615 | case glslang::EOpConstructI64Vec2: |
| 2616 | case glslang::EOpConstructI64Vec3: |
| 2617 | case glslang::EOpConstructI64Vec4: |
| 2618 | case glslang::EOpConstructUint64: |
| 2619 | case glslang::EOpConstructU64Vec2: |
| 2620 | case glslang::EOpConstructU64Vec3: |
| 2621 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2622 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2623 | case glslang::EOpConstructTextureSampler: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2624 | case glslang::EOpConstructReference: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2625 | case glslang::EOpConstructCooperativeMatrix: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2626 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2627 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2628 | std::vector<spv::Id> arguments; |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2629 | translateArguments(*node, arguments, lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2630 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2631 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2632 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2633 | else if (node->getOp() == glslang::EOpConstructStruct || |
| 2634 | node->getOp() == glslang::EOpConstructCooperativeMatrix || |
| 2635 | node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2636 | std::vector<spv::Id> constituents; |
| 2637 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2638 | constituents.push_back(arguments[c]); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2639 | constructed = createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2640 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2641 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2642 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2643 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2644 | |
Bas Nieuwenhuizen | c9ffeec | 2020-09-03 03:09:39 +0200 | [diff] [blame] | 2645 | if (node->getType().getQualifier().isNonUniform()) { |
| 2646 | builder.addDecoration(constructed, spv::DecorationNonUniformEXT); |
| 2647 | } |
| 2648 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2649 | builder.clearAccessChain(); |
| 2650 | builder.setAccessChainRValue(constructed); |
| 2651 | |
| 2652 | return false; |
| 2653 | } |
| 2654 | |
| 2655 | // These six are component-wise compares with component-wise results. |
| 2656 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2657 | case glslang::EOpLessThan: |
| 2658 | case glslang::EOpGreaterThan: |
| 2659 | case glslang::EOpLessThanEqual: |
| 2660 | case glslang::EOpGreaterThanEqual: |
| 2661 | case glslang::EOpVectorEqual: |
| 2662 | case glslang::EOpVectorNotEqual: |
| 2663 | { |
| 2664 | // Map the operation to a binary |
| 2665 | binOp = node->getOp(); |
| 2666 | reduceComparison = false; |
| 2667 | switch (node->getOp()) { |
| 2668 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2669 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2670 | default: binOp = node->getOp(); break; |
| 2671 | } |
| 2672 | |
| 2673 | break; |
| 2674 | } |
| 2675 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2676 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2677 | binOp = glslang::EOpMul; |
| 2678 | break; |
| 2679 | case glslang::EOpOuterProduct: |
| 2680 | // two vectors multiplied to make a matrix |
| 2681 | binOp = glslang::EOpOuterProduct; |
| 2682 | break; |
| 2683 | case glslang::EOpDot: |
| 2684 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2685 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2686 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2687 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2688 | binOp = glslang::EOpMul; |
| 2689 | break; |
| 2690 | } |
| 2691 | case glslang::EOpMod: |
| 2692 | // when an aggregate, this is the floating-point mod built-in function, |
| 2693 | // which can be emitted by the one in createBinaryOperation() |
| 2694 | binOp = glslang::EOpMod; |
| 2695 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2696 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2697 | case glslang::EOpEmitVertex: |
| 2698 | case glslang::EOpEndPrimitive: |
| 2699 | case glslang::EOpBarrier: |
| 2700 | case glslang::EOpMemoryBarrier: |
| 2701 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2702 | case glslang::EOpMemoryBarrierBuffer: |
| 2703 | case glslang::EOpMemoryBarrierImage: |
| 2704 | case glslang::EOpMemoryBarrierShared: |
| 2705 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2706 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2707 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2708 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2709 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2710 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2711 | case glslang::EOpSubgroupBarrier: |
| 2712 | case glslang::EOpSubgroupMemoryBarrier: |
| 2713 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2714 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2715 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2716 | noReturnValue = true; |
| 2717 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2718 | break; |
| 2719 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2720 | case glslang::EOpAtomicAdd: |
| 2721 | case glslang::EOpAtomicMin: |
| 2722 | case glslang::EOpAtomicMax: |
| 2723 | case glslang::EOpAtomicAnd: |
| 2724 | case glslang::EOpAtomicOr: |
| 2725 | case glslang::EOpAtomicXor: |
| 2726 | case glslang::EOpAtomicExchange: |
| 2727 | case glslang::EOpAtomicCompSwap: |
| 2728 | atomic = true; |
| 2729 | break; |
| 2730 | |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2731 | #ifndef GLSLANG_WEB |
| 2732 | case glslang::EOpAtomicStore: |
| 2733 | noReturnValue = true; |
| 2734 | // fallthrough |
| 2735 | case glslang::EOpAtomicLoad: |
| 2736 | atomic = true; |
| 2737 | break; |
| 2738 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2739 | case glslang::EOpAtomicCounterAdd: |
| 2740 | case glslang::EOpAtomicCounterSubtract: |
| 2741 | case glslang::EOpAtomicCounterMin: |
| 2742 | case glslang::EOpAtomicCounterMax: |
| 2743 | case glslang::EOpAtomicCounterAnd: |
| 2744 | case glslang::EOpAtomicCounterOr: |
| 2745 | case glslang::EOpAtomicCounterXor: |
| 2746 | case glslang::EOpAtomicCounterExchange: |
| 2747 | case glslang::EOpAtomicCounterCompSwap: |
| 2748 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2749 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2750 | atomic = true; |
| 2751 | break; |
| 2752 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 2753 | case glslang::EOpAbsDifference: |
| 2754 | case glslang::EOpAddSaturate: |
| 2755 | case glslang::EOpSubSaturate: |
| 2756 | case glslang::EOpAverage: |
| 2757 | case glslang::EOpAverageRounded: |
| 2758 | case glslang::EOpMul32x16: |
| 2759 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 2760 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 2761 | binOp = node->getOp(); |
| 2762 | break; |
| 2763 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2764 | case glslang::EOpIgnoreIntersection: |
| 2765 | case glslang::EOpTerminateRay: |
| 2766 | case glslang::EOpTrace: |
| 2767 | case glslang::EOpExecuteCallable: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2768 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2769 | noReturnValue = true; |
| 2770 | break; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2771 | case glslang::EOpRayQueryInitialize: |
| 2772 | case glslang::EOpRayQueryTerminate: |
| 2773 | case glslang::EOpRayQueryGenerateIntersection: |
| 2774 | case glslang::EOpRayQueryConfirmIntersection: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2775 | builder.addExtension("SPV_KHR_ray_query"); |
| 2776 | builder.addCapability(spv::CapabilityRayQueryProvisionalKHR); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2777 | noReturnValue = true; |
| 2778 | break; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2779 | case glslang::EOpRayQueryProceed: |
| 2780 | case glslang::EOpRayQueryGetIntersectionType: |
| 2781 | case glslang::EOpRayQueryGetRayTMin: |
| 2782 | case glslang::EOpRayQueryGetRayFlags: |
| 2783 | case glslang::EOpRayQueryGetIntersectionT: |
| 2784 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 2785 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 2786 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 2787 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 2788 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 2789 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 2790 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 2791 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 2792 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 2793 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 2794 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 2795 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 2796 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 2797 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 2798 | builder.addExtension("SPV_KHR_ray_query"); |
| 2799 | builder.addCapability(spv::CapabilityRayQueryProvisionalKHR); |
| 2800 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2801 | case glslang::EOpCooperativeMatrixLoad: |
| 2802 | case glslang::EOpCooperativeMatrixStore: |
| 2803 | noReturnValue = true; |
| 2804 | break; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 2805 | case glslang::EOpBeginInvocationInterlock: |
| 2806 | case glslang::EOpEndInvocationInterlock: |
| 2807 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 2808 | noReturnValue = true; |
| 2809 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2810 | #endif |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2811 | |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 2812 | case glslang::EOpDebugPrintf: |
| 2813 | noReturnValue = true; |
| 2814 | break; |
| 2815 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2816 | default: |
| 2817 | break; |
| 2818 | } |
| 2819 | |
| 2820 | // |
| 2821 | // See if it maps to a regular operation. |
| 2822 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2823 | if (binOp != glslang::EOpNull) { |
| 2824 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2825 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2826 | assert(left && right); |
| 2827 | |
| 2828 | builder.clearAccessChain(); |
| 2829 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2830 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2831 | |
| 2832 | builder.clearAccessChain(); |
| 2833 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2834 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2835 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2836 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2837 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2838 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2839 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2840 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2841 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2842 | left->getType().getBasicType(), reduceComparison); |
| 2843 | |
| 2844 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2845 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2846 | builder.clearAccessChain(); |
| 2847 | builder.setAccessChainRValue(result); |
| 2848 | |
| 2849 | return false; |
| 2850 | } |
| 2851 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2852 | // |
| 2853 | // Create the list of operands. |
| 2854 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2855 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2856 | std::vector<spv::Id> operands; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2857 | std::vector<spv::IdImmediate> memoryAccessOperands; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2858 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2859 | // special case l-value operands; there are just a few |
| 2860 | bool lvalue = false; |
| 2861 | switch (node->getOp()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2862 | case glslang::EOpModf: |
| 2863 | if (arg == 1) |
| 2864 | lvalue = true; |
| 2865 | break; |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2866 | |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2867 | case glslang::EOpRayQueryInitialize: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2868 | case glslang::EOpRayQueryTerminate: |
| 2869 | case glslang::EOpRayQueryConfirmIntersection: |
| 2870 | case glslang::EOpRayQueryProceed: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2871 | case glslang::EOpRayQueryGenerateIntersection: |
| 2872 | case glslang::EOpRayQueryGetIntersectionType: |
| 2873 | case glslang::EOpRayQueryGetIntersectionT: |
| 2874 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 2875 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 2876 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 2877 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 2878 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 2879 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 2880 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 2881 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 2882 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 2883 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 2884 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 2885 | if (arg == 0) |
| 2886 | lvalue = true; |
| 2887 | break; |
| 2888 | |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2889 | case glslang::EOpAtomicAdd: |
| 2890 | case glslang::EOpAtomicMin: |
| 2891 | case glslang::EOpAtomicMax: |
| 2892 | case glslang::EOpAtomicAnd: |
| 2893 | case glslang::EOpAtomicOr: |
| 2894 | case glslang::EOpAtomicXor: |
| 2895 | case glslang::EOpAtomicExchange: |
| 2896 | case glslang::EOpAtomicCompSwap: |
| 2897 | if (arg == 0) |
| 2898 | lvalue = true; |
| 2899 | break; |
| 2900 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2901 | #ifndef GLSLANG_WEB |
| 2902 | case glslang::EOpFrexp: |
| 2903 | if (arg == 1) |
| 2904 | lvalue = true; |
| 2905 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2906 | case glslang::EOpInterpolateAtSample: |
| 2907 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2908 | case glslang::EOpInterpolateAtVertex: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2909 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2910 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2911 | |
| 2912 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2913 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2914 | // That is, we transform |
| 2915 | // |
| 2916 | // interpolate(v.zy) -> interpolate(v).zy |
| 2917 | // |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2918 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2919 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2920 | invertedType = convertGlslangToSpvType( |
| 2921 | glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2922 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2923 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2924 | case glslang::EOpAtomicLoad: |
| 2925 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2926 | case glslang::EOpAtomicCounterAdd: |
| 2927 | case glslang::EOpAtomicCounterSubtract: |
| 2928 | case glslang::EOpAtomicCounterMin: |
| 2929 | case glslang::EOpAtomicCounterMax: |
| 2930 | case glslang::EOpAtomicCounterAnd: |
| 2931 | case glslang::EOpAtomicCounterOr: |
| 2932 | case glslang::EOpAtomicCounterXor: |
| 2933 | case glslang::EOpAtomicCounterExchange: |
| 2934 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2935 | if (arg == 0) |
| 2936 | lvalue = true; |
| 2937 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2938 | case glslang::EOpAddCarry: |
| 2939 | case glslang::EOpSubBorrow: |
| 2940 | if (arg == 2) |
| 2941 | lvalue = true; |
| 2942 | break; |
| 2943 | case glslang::EOpUMulExtended: |
| 2944 | case glslang::EOpIMulExtended: |
| 2945 | if (arg >= 2) |
| 2946 | lvalue = true; |
| 2947 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2948 | case glslang::EOpCooperativeMatrixLoad: |
| 2949 | if (arg == 0 || arg == 1) |
| 2950 | lvalue = true; |
| 2951 | break; |
| 2952 | case glslang::EOpCooperativeMatrixStore: |
| 2953 | if (arg == 1) |
| 2954 | lvalue = true; |
| 2955 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2956 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2957 | default: |
| 2958 | break; |
| 2959 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2960 | builder.clearAccessChain(); |
| 2961 | if (invertedType != spv::NoType && arg == 0) |
| 2962 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2963 | else |
| 2964 | glslangOperands[arg]->traverse(this); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2965 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2966 | #ifndef GLSLANG_WEB |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2967 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
| 2968 | node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2969 | |
| 2970 | if (arg == 1) { |
| 2971 | // fold "element" parameter into the access chain |
| 2972 | spv::Builder::AccessChain save = builder.getAccessChain(); |
| 2973 | builder.clearAccessChain(); |
| 2974 | glslangOperands[2]->traverse(this); |
| 2975 | |
| 2976 | spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
| 2977 | |
| 2978 | builder.setAccessChain(save); |
| 2979 | |
| 2980 | // Point to the first element of the array. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2981 | builder.accessChainPush(elementId, |
| 2982 | TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
| 2983 | glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2984 | |
| 2985 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 2986 | unsigned int alignment = builder.getAccessChain().alignment; |
| 2987 | |
| 2988 | int memoryAccess = TranslateMemoryAccess(coherentFlags); |
| 2989 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
| 2990 | memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
| 2991 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
| 2992 | memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2993 | if (builder.getStorageClass(builder.getAccessChain().base) == |
| 2994 | spv::StorageClassPhysicalStorageBufferEXT) { |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2995 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
| 2996 | } |
| 2997 | |
| 2998 | memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
| 2999 | |
| 3000 | if (memoryAccess & spv::MemoryAccessAlignedMask) { |
| 3001 | memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
| 3002 | } |
| 3003 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3004 | if (memoryAccess & |
| 3005 | (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
| 3006 | memoryAccessOperands.push_back(spv::IdImmediate(true, |
| 3007 | builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3008 | } |
| 3009 | } else if (arg == 2) { |
| 3010 | continue; |
| 3011 | } |
| 3012 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3013 | #endif |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3014 | |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3015 | // for l-values, pass the address, for r-values, pass the value |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 3016 | if (lvalue) { |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3017 | if (invertedType == spv::NoType && !builder.isSpvLvalue()) { |
| 3018 | // SPIR-V cannot represent an l-value containing a swizzle that doesn't |
| 3019 | // reduce to a simple access chain. So, we need a temporary vector to |
| 3020 | // receive the result, and must later swizzle that into the original |
| 3021 | // l-value. |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3022 | complexLvalues.push_back(builder.getAccessChain()); |
Bas Nieuwenhuizen | de949a2 | 2020-08-24 23:27:26 +0200 | [diff] [blame] | 3023 | complexLValueQualifiers.push_back(glslangOperands[arg]->getAsTyped()->getType().getQualifier()); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3024 | temporaryLvalues.push_back(builder.createVariable( |
| 3025 | spv::NoPrecision, spv::StorageClassFunction, |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3026 | builder.accessChainGetInferredType(), "swizzleTemp")); |
| 3027 | operands.push_back(temporaryLvalues.back()); |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3028 | } else { |
| 3029 | operands.push_back(builder.accessChainGetLValue()); |
| 3030 | } |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 3031 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 3032 | lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
| 3033 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3034 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 3035 | glslang::TOperator glslangOp = node->getOp(); |
| 3036 | if (arg == 1 && |
| 3037 | (glslangOp == glslang::EOpRayQueryGetIntersectionType || |
| 3038 | glslangOp == glslang::EOpRayQueryGetIntersectionT || |
| 3039 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex || |
| 3040 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId || |
| 3041 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset || |
| 3042 | glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex || |
| 3043 | glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex || |
| 3044 | glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics || |
| 3045 | glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace || |
| 3046 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection || |
| 3047 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin || |
| 3048 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld || |
| 3049 | glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject |
| 3050 | )) { |
| 3051 | bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst(); |
| 3052 | operands.push_back(builder.makeIntConstant(cond ? 1 : 0)); |
| 3053 | } |
| 3054 | else { |
| 3055 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
| 3056 | } |
| 3057 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3058 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3059 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3060 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3061 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3062 | #ifndef GLSLANG_WEB |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3063 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
| 3064 | std::vector<spv::IdImmediate> idImmOps; |
| 3065 | |
| 3066 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3067 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3068 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3069 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3070 | // get the pointee type |
| 3071 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
| 3072 | assert(builder.isCooperativeMatrixType(typeId)); |
| 3073 | // do the op |
| 3074 | spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
| 3075 | // store the result to the pointer (out param 'm') |
| 3076 | builder.createStore(result, operands[0]); |
| 3077 | result = 0; |
| 3078 | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 3079 | std::vector<spv::IdImmediate> idImmOps; |
| 3080 | |
| 3081 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3082 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
| 3083 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3084 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3085 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3086 | |
| 3087 | builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps); |
| 3088 | result = 0; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3089 | } else |
| 3090 | #endif |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 3091 | if (atomic) { |
| 3092 | // Handle all atomics |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3093 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), |
| 3094 | lvalueCoherentFlags); |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 3095 | } else if (node->getOp() == glslang::EOpDebugPrintf) { |
| 3096 | if (!nonSemanticDebugPrintf) { |
| 3097 | nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf"); |
| 3098 | } |
| 3099 | result = builder.createBuiltinCall(builder.makeVoidType(), nonSemanticDebugPrintf, spv::NonSemanticDebugPrintfDebugPrintf, operands); |
| 3100 | builder.addExtension(spv::E_SPV_KHR_non_semantic_info); |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 3101 | } else { |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3102 | // Pass through to generic operations. |
| 3103 | switch (glslangOperands.size()) { |
| 3104 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3105 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3106 | break; |
| 3107 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3108 | { |
| 3109 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3110 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 3111 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3112 | result = createUnaryOperation( |
| 3113 | node->getOp(), decorations, |
| 3114 | resultType(), operands.front(), |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 3115 | glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3116 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3117 | break; |
| 3118 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3119 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3120 | break; |
| 3121 | } |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3122 | |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3123 | if (invertedType != spv::NoResult) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3124 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3125 | |
| 3126 | for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) { |
| 3127 | builder.setAccessChain(complexLvalues[i]); |
Bas Nieuwenhuizen | de949a2 | 2020-08-24 23:27:26 +0200 | [diff] [blame] | 3128 | builder.accessChainStore(builder.createLoad(temporaryLvalues[i], spv::NoPrecision), TranslateNonUniformDecoration(complexLValueQualifiers[i])); |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3129 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | if (noReturnValue) |
| 3133 | return false; |
| 3134 | |
| 3135 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 3136 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 3137 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3138 | } else { |
| 3139 | builder.clearAccessChain(); |
| 3140 | builder.setAccessChainRValue(result); |
| 3141 | return false; |
| 3142 | } |
| 3143 | } |
| 3144 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3145 | // This path handles both if-then-else and ?: |
| 3146 | // The if-then-else has a node type of void, while |
| 3147 | // ?: has either a void or a non-void node type |
| 3148 | // |
| 3149 | // Leaving the result, when not void: |
| 3150 | // GLSL only has r-values as the result of a :?, but |
| 3151 | // if we have an l-value, that can be more efficient if it will |
| 3152 | // become the base of a complex r-value expression, because the |
| 3153 | // 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] | 3154 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 3155 | { |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3156 | // see if OpSelect can handle it |
| 3157 | const auto isOpSelectable = [&]() { |
| 3158 | if (node->getBasicType() == glslang::EbtVoid) |
| 3159 | return false; |
| 3160 | // OpSelect can do all other types starting with SPV 1.4 |
| 3161 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
| 3162 | // pre-1.4, only scalars and vectors can be handled |
| 3163 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
| 3164 | return false; |
| 3165 | } |
| 3166 | return true; |
| 3167 | }; |
| 3168 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3169 | // See if it simple and safe, or required, to execute both sides. |
| 3170 | // Crucially, side effects must be either semantically required or avoided, |
| 3171 | // and there are performance trade-offs. |
| 3172 | // Return true if required or a good idea (and safe) to execute both sides, |
| 3173 | // false otherwise. |
| 3174 | const auto bothSidesPolicy = [&]() -> bool { |
| 3175 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3176 | if (node->getTrueBlock() == nullptr || |
| 3177 | node->getFalseBlock() == nullptr) |
| 3178 | return false; |
| 3179 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3180 | // required? (unless we write additional code to look for side effects |
| 3181 | // and make performance trade-offs if none are present) |
| 3182 | if (!node->getShortCircuit()) |
| 3183 | return true; |
| 3184 | |
| 3185 | // if not required to execute both, decide based on performance/practicality... |
| 3186 | |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3187 | if (!isOpSelectable()) |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3188 | return false; |
| 3189 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3190 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 3191 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 3192 | |
| 3193 | // return true if a single operand to ? : is okay for OpSelect |
| 3194 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 3195 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3196 | }; |
| 3197 | |
| 3198 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 3199 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 3200 | }; |
| 3201 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3202 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 3203 | // emit the condition before doing anything with selection |
| 3204 | node->getCondition()->traverse(this); |
| 3205 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 3206 | |
| 3207 | // Find a way of executing both sides and selecting the right result. |
| 3208 | const auto executeBothSides = [&]() -> void { |
| 3209 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3210 | node->getTrueBlock()->traverse(this); |
| 3211 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3212 | node->getFalseBlock()->traverse(this); |
| 3213 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3214 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3215 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3216 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3217 | // done if void |
| 3218 | if (node->getBasicType() == glslang::EbtVoid) |
| 3219 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 3220 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3221 | // emit code to select between trueValue and falseValue |
| 3222 | |
| 3223 | // see if OpSelect can handle it |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3224 | if (isOpSelectable()) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3225 | // Emit OpSelect for this selection. |
| 3226 | |
| 3227 | // smear condition to vector, if necessary (AST is always scalar) |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3228 | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
| 3229 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3230 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 3231 | builder.makeVectorType(builder.makeBoolType(), |
| 3232 | builder.getNumComponents(trueValue))); |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3233 | } |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3234 | |
| 3235 | // OpSelect |
| 3236 | result = builder.createTriOp(spv::OpSelect, |
| 3237 | convertGlslangToSpvType(node->getType()), condition, |
| 3238 | trueValue, falseValue); |
| 3239 | |
| 3240 | builder.clearAccessChain(); |
| 3241 | builder.setAccessChainRValue(result); |
| 3242 | } else { |
| 3243 | // We need control flow to select the result. |
| 3244 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3245 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), |
| 3246 | spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3247 | |
| 3248 | // Selection control: |
| 3249 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3250 | |
| 3251 | // make an "if" based on the value created by the condition |
| 3252 | spv::Builder::If ifBuilder(condition, control, builder); |
| 3253 | |
| 3254 | // emit the "then" statement |
| 3255 | builder.createStore(trueValue, result); |
| 3256 | ifBuilder.makeBeginElse(); |
| 3257 | // emit the "else" statement |
| 3258 | builder.createStore(falseValue, result); |
| 3259 | |
| 3260 | // finish off the control flow |
| 3261 | ifBuilder.makeEndIf(); |
| 3262 | |
| 3263 | builder.clearAccessChain(); |
| 3264 | builder.setAccessChainLValue(result); |
| 3265 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3266 | }; |
| 3267 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3268 | // Execute the one side needed, as per the condition |
| 3269 | const auto executeOneSide = [&]() { |
| 3270 | // Always emit control flow. |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3271 | if (node->getBasicType() != glslang::EbtVoid) { |
| 3272 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), spv::StorageClassFunction, |
| 3273 | convertGlslangToSpvType(node->getType())); |
| 3274 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3275 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3276 | // Selection control: |
| 3277 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3278 | |
| 3279 | // make an "if" based on the value created by the condition |
| 3280 | spv::Builder::If ifBuilder(condition, control, builder); |
| 3281 | |
| 3282 | // emit the "then" statement |
| 3283 | if (node->getTrueBlock() != nullptr) { |
| 3284 | node->getTrueBlock()->traverse(this); |
| 3285 | if (result != spv::NoResult) |
| 3286 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 3287 | } |
| 3288 | |
| 3289 | if (node->getFalseBlock() != nullptr) { |
| 3290 | ifBuilder.makeBeginElse(); |
| 3291 | // emit the "else" statement |
| 3292 | node->getFalseBlock()->traverse(this); |
| 3293 | if (result != spv::NoResult) |
| 3294 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 3295 | } |
| 3296 | |
| 3297 | // finish off the control flow |
| 3298 | ifBuilder.makeEndIf(); |
| 3299 | |
| 3300 | if (result != spv::NoResult) { |
| 3301 | builder.clearAccessChain(); |
| 3302 | builder.setAccessChainLValue(result); |
| 3303 | } |
| 3304 | }; |
| 3305 | |
| 3306 | // Try for OpSelect (or a requirement to execute both sides) |
| 3307 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 3308 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 3309 | if (node->getType().getQualifier().isSpecConstant()) |
| 3310 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3311 | executeBothSides(); |
| 3312 | } else |
| 3313 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3314 | |
| 3315 | return false; |
| 3316 | } |
| 3317 | |
| 3318 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 3319 | { |
| 3320 | // emit and get the condition before doing anything with switch |
| 3321 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3322 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3323 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3324 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 3325 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3326 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3327 | // browse the children to sort out code segments |
| 3328 | int defaultSegment = -1; |
| 3329 | std::vector<TIntermNode*> codeSegments; |
| 3330 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 3331 | std::vector<int> caseValues; |
| 3332 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 3333 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 3334 | TIntermNode* child = *c; |
| 3335 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3336 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3337 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3338 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3339 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion() |
| 3340 | ->getConstArray()[0].getIConst()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3341 | } else |
| 3342 | codeSegments.push_back(child); |
| 3343 | } |
| 3344 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3345 | // 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] | 3346 | // statements between the last case and the end of the switch statement |
| 3347 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 3348 | (int)codeSegments.size() == defaultSegment) |
| 3349 | codeSegments.push_back(nullptr); |
| 3350 | |
| 3351 | // make the switch statement |
| 3352 | std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3353 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, |
| 3354 | segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3355 | |
| 3356 | // emit all the code in the segments |
| 3357 | breakForLoop.push(false); |
| 3358 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 3359 | builder.nextSwitchSegment(segmentBlocks, s); |
| 3360 | if (codeSegments[s]) |
| 3361 | codeSegments[s]->traverse(this); |
| 3362 | else |
| 3363 | builder.addSwitchBreak(); |
| 3364 | } |
| 3365 | breakForLoop.pop(); |
| 3366 | |
| 3367 | builder.endSwitch(segmentBlocks); |
| 3368 | |
| 3369 | return false; |
| 3370 | } |
| 3371 | |
| 3372 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 3373 | { |
| 3374 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 3375 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3376 | |
| 3377 | builder.clearAccessChain(); |
| 3378 | builder.setAccessChainRValue(constant); |
| 3379 | } |
| 3380 | |
| 3381 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 3382 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3383 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3384 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3385 | |
| 3386 | // Loop control: |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3387 | std::vector<unsigned int> operands; |
| 3388 | const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3389 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3390 | // Spec requires back edges to target header blocks, and every header block |
| 3391 | // must dominate its merge block. Make a header block first to ensure these |
| 3392 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 3393 | // by a block-ending branch. But we don't want to put any other body/test |
| 3394 | // instructions in it, since the body/test may have arbitrary instructions, |
| 3395 | // including merges of its own. |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3396 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3397 | builder.setBuildPoint(&blocks.head); |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3398 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3399 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3400 | spv::Block& test = builder.makeNewBlock(); |
| 3401 | builder.createBranch(&test); |
| 3402 | |
| 3403 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3404 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3405 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3406 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 3407 | |
| 3408 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3409 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3410 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3411 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3412 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3413 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3414 | |
| 3415 | builder.setBuildPoint(&blocks.continue_target); |
| 3416 | if (node->getTerminal()) |
| 3417 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3418 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 3419 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3420 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3421 | builder.createBranch(&blocks.body); |
| 3422 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3423 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3424 | builder.setBuildPoint(&blocks.body); |
| 3425 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3426 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3427 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3428 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3429 | |
| 3430 | builder.setBuildPoint(&blocks.continue_target); |
| 3431 | if (node->getTerminal()) |
| 3432 | node->getTerminal()->traverse(this); |
| 3433 | if (node->getTest()) { |
| 3434 | node->getTest()->traverse(this); |
| 3435 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3436 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3437 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3438 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 3439 | // TODO: unless there was a break/return/discard instruction |
| 3440 | // somewhere in the body, this is an infinite loop, so we should |
| 3441 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3442 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3443 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3444 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3445 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3446 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3447 | return false; |
| 3448 | } |
| 3449 | |
| 3450 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 3451 | { |
| 3452 | if (node->getExpression()) |
| 3453 | node->getExpression()->traverse(this); |
| 3454 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3455 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3456 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3457 | switch (node->getFlowOp()) { |
| 3458 | case glslang::EOpKill: |
| 3459 | builder.makeDiscard(); |
| 3460 | break; |
| 3461 | case glslang::EOpBreak: |
| 3462 | if (breakForLoop.top()) |
| 3463 | builder.createLoopExit(); |
| 3464 | else |
| 3465 | builder.addSwitchBreak(); |
| 3466 | break; |
| 3467 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3468 | builder.createLoopContinue(); |
| 3469 | break; |
| 3470 | case glslang::EOpReturn: |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3471 | if (node->getExpression() != nullptr) { |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3472 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 3473 | spv::Id returnId = accessChainLoad(glslangReturnType); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3474 | if (builder.getTypeId(returnId) != currentFunction->getReturnType() || |
| 3475 | TranslatePrecisionDecoration(glslangReturnType) != currentFunction->getReturnPrecision()) { |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3476 | builder.clearAccessChain(); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3477 | spv::Id copyId = builder.createVariable(currentFunction->getReturnPrecision(), |
| 3478 | spv::StorageClassFunction, currentFunction->getReturnType()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3479 | builder.setAccessChainLValue(copyId); |
| 3480 | multiTypeStore(glslangReturnType, returnId); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3481 | returnId = builder.createLoad(copyId, currentFunction->getReturnPrecision()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3482 | } |
| 3483 | builder.makeReturn(false, returnId); |
| 3484 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 3485 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3486 | |
| 3487 | builder.clearAccessChain(); |
| 3488 | break; |
| 3489 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3490 | #ifndef GLSLANG_WEB |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3491 | case glslang::EOpDemote: |
| 3492 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 3493 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 3494 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 3495 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3496 | #endif |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3497 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3498 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3499 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3500 | break; |
| 3501 | } |
| 3502 | |
| 3503 | return false; |
| 3504 | } |
| 3505 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3506 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3507 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3508 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3509 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3510 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 3511 | if (node->getQualifier().isConstant()) { |
Dan Sinclair | 12fcaa2 | 2018-11-13 09:17:44 -0500 | [diff] [blame] | 3512 | spv::Id result = createSpvConstant(*node); |
| 3513 | if (result != spv::NoResult) |
| 3514 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3518 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3519 | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
| 3520 | : forcedType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3521 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3522 | const bool contains16BitType = node->getType().contains16BitFloat() || |
| 3523 | node->getType().contains16BitInt(); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3524 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3525 | switch (storageClass) { |
| 3526 | case spv::StorageClassInput: |
| 3527 | case spv::StorageClassOutput: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3528 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3529 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3530 | break; |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3531 | case spv::StorageClassUniform: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3532 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3533 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 3534 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3535 | else |
| 3536 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 3537 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3538 | #ifndef GLSLANG_WEB |
| 3539 | case spv::StorageClassPushConstant: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3540 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3541 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 3542 | break; |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3543 | case spv::StorageClassStorageBuffer: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3544 | case spv::StorageClassPhysicalStorageBufferEXT: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3545 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3546 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 3547 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3548 | #endif |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3549 | default: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3550 | if (node->getType().contains16BitFloat()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3551 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3552 | if (node->getType().contains16BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3553 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3554 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3555 | } |
| 3556 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3557 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3558 | if (node->getType().contains8BitInt()) { |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3559 | if (storageClass == spv::StorageClassPushConstant) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3560 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3561 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 3562 | } else if (storageClass == spv::StorageClassUniform) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3563 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3564 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3565 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3566 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3567 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3568 | } else { |
| 3569 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3570 | } |
| 3571 | } |
| 3572 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3573 | const char* name = node->getName().c_str(); |
| 3574 | if (glslang::IsAnonymous(name)) |
| 3575 | name = ""; |
| 3576 | |
Alejandro Piñeiro | ff6dcca | 2020-06-04 09:39:31 +0200 | [diff] [blame] | 3577 | spv::Id initializer = spv::NoResult; |
| 3578 | |
| 3579 | if (node->getType().getQualifier().storage == glslang::EvqUniform && |
| 3580 | !node->getConstArray().empty()) { |
| 3581 | int nextConst = 0; |
| 3582 | initializer = createSpvConstantFromConstUnionArray(node->getType(), |
| 3583 | node->getConstArray(), |
| 3584 | nextConst, |
| 3585 | false /* specConst */); |
| 3586 | } |
| 3587 | |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3588 | return builder.createVariable(spv::NoPrecision, storageClass, spvType, name, initializer); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | // Return type Id of the sampled type. |
| 3592 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 3593 | { |
| 3594 | switch (sampler.type) { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3595 | case glslang::EbtInt: return builder.makeIntType(32); |
| 3596 | case glslang::EbtUint: return builder.makeUintType(32); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3597 | case glslang::EbtFloat: return builder.makeFloatType(32); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3598 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3599 | case glslang::EbtFloat16: |
| 3600 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 3601 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3602 | return builder.makeFloatType(16); |
Tobski | 8c1a3a0 | 2020-11-04 16:24:23 +0000 | [diff] [blame] | 3603 | case glslang::EbtInt64: return builder.makeIntType(64); |
| 3604 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 3605 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3606 | case glslang::EbtUint64: return builder.makeUintType(64); |
| 3607 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 3608 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3609 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3610 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3611 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3612 | return builder.makeFloatType(32); |
| 3613 | } |
| 3614 | } |
| 3615 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3616 | // If node is a swizzle operation, return the type that should be used if |
| 3617 | // the swizzle base is first consumed by another operation, before the swizzle |
| 3618 | // is applied. |
| 3619 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 3620 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3621 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3622 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 3623 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 3624 | else |
| 3625 | return spv::NoType; |
| 3626 | } |
| 3627 | |
| 3628 | // When inverting a swizzle with a parent op, this function |
| 3629 | // will apply the swizzle operation to a completed parent operation. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3630 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, |
| 3631 | spv::Id parentResult) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3632 | { |
| 3633 | std::vector<unsigned> swizzle; |
| 3634 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 3635 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 3636 | } |
| 3637 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3638 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 3639 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 3640 | { |
| 3641 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 3642 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 3643 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3644 | } |
| 3645 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3646 | // Convert from a glslang type to an SPV type, by calling into a |
| 3647 | // recursive version of this function. This establishes the inherited |
| 3648 | // layout state rooted from the top-level type. |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3649 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3650 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3651 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | // 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] | 3655 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3656 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3657 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3658 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 3659 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3660 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3661 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3662 | |
| 3663 | switch (type.getBasicType()) { |
| 3664 | case glslang::EbtVoid: |
| 3665 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3666 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3667 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3668 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3669 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 3670 | // a 32-bit int where non-0 means true. |
| 3671 | if (explicitLayout != glslang::ElpNone) |
| 3672 | spvType = builder.makeUintType(32); |
| 3673 | else |
| 3674 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3675 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3676 | case glslang::EbtInt: |
| 3677 | spvType = builder.makeIntType(32); |
| 3678 | break; |
| 3679 | case glslang::EbtUint: |
| 3680 | spvType = builder.makeUintType(32); |
| 3681 | break; |
| 3682 | case glslang::EbtFloat: |
| 3683 | spvType = builder.makeFloatType(32); |
| 3684 | break; |
| 3685 | #ifndef GLSLANG_WEB |
| 3686 | case glslang::EbtDouble: |
| 3687 | spvType = builder.makeFloatType(64); |
| 3688 | break; |
| 3689 | case glslang::EbtFloat16: |
| 3690 | spvType = builder.makeFloatType(16); |
| 3691 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3692 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3693 | spvType = builder.makeIntType(8); |
| 3694 | break; |
| 3695 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3696 | spvType = builder.makeUintType(8); |
| 3697 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3698 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3699 | spvType = builder.makeIntType(16); |
| 3700 | break; |
| 3701 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3702 | spvType = builder.makeUintType(16); |
| 3703 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3704 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3705 | spvType = builder.makeIntType(64); |
| 3706 | break; |
| 3707 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3708 | spvType = builder.makeUintType(64); |
| 3709 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3710 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 3711 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3712 | spvType = builder.makeUintType(32); |
| 3713 | break; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 3714 | case glslang::EbtAccStruct: |
| 3715 | spvType = builder.makeAccelerationStructureType(); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3716 | break; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 3717 | case glslang::EbtRayQuery: |
| 3718 | spvType = builder.makeRayQueryType(); |
| 3719 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3720 | case glslang::EbtReference: |
| 3721 | { |
| 3722 | // Make the forward pointer, then recurse to convert the structure type, then |
| 3723 | // patch up the forward pointer with a real pointer type. |
| 3724 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 3725 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 3726 | forwardPointers[type.getReferentType()] = forwardId; |
| 3727 | } |
| 3728 | spvType = forwardPointers[type.getReferentType()]; |
| 3729 | if (!forwardReferenceOnly) { |
| 3730 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 3731 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 3732 | forwardPointers[type.getReferentType()], |
| 3733 | referentType); |
| 3734 | } |
| 3735 | } |
| 3736 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3737 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3738 | case glslang::EbtSampler: |
| 3739 | { |
| 3740 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3741 | if (sampler.isPureSampler()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3742 | spvType = builder.makeSamplerType(); |
| 3743 | } else { |
| 3744 | // an image is present, make its type |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3745 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), |
| 3746 | sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), |
| 3747 | sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type)); |
| 3748 | if (sampler.isCombined()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3749 | // already has both image and sampler, make the combined type |
| 3750 | spvType = builder.makeSampledImageType(spvType); |
| 3751 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3752 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3753 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3754 | break; |
| 3755 | case glslang::EbtStruct: |
| 3756 | case glslang::EbtBlock: |
| 3757 | { |
| 3758 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3759 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3760 | |
| 3761 | // Try to share structs for different layouts, but not yet for other |
| 3762 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3763 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3764 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3765 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3766 | break; |
| 3767 | |
| 3768 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3769 | if (type.getBasicType() == glslang::EbtBlock) |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3770 | memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(glslangMembers->size()); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3771 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3772 | } |
| 3773 | break; |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 3774 | case glslang::EbtString: |
| 3775 | // no type used for OpString |
| 3776 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3777 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3778 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3779 | break; |
| 3780 | } |
| 3781 | |
| 3782 | if (type.isMatrix()) |
| 3783 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3784 | else { |
| 3785 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3786 | if (type.getVectorSize() > 1) |
| 3787 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3788 | } |
| 3789 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3790 | if (type.isCoopMat()) { |
| 3791 | builder.addCapability(spv::CapabilityCooperativeMatrixNV); |
| 3792 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
| 3793 | if (type.getBasicType() == glslang::EbtFloat16) |
| 3794 | builder.addCapability(spv::CapabilityFloat16); |
Jeff Bolz | 387657e | 2019-08-22 20:28:00 -0500 | [diff] [blame] | 3795 | if (type.getBasicType() == glslang::EbtUint8 || |
| 3796 | type.getBasicType() == glslang::EbtInt8) { |
| 3797 | builder.addCapability(spv::CapabilityInt8); |
| 3798 | } |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3799 | |
| 3800 | spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1); |
| 3801 | spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2); |
| 3802 | spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3); |
| 3803 | |
| 3804 | spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols); |
| 3805 | } |
| 3806 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3807 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3808 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3809 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3810 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3811 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3812 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3813 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3814 | // Use a dummy glslang type for querying internal strides of |
| 3815 | // arrays of arrays, but using just a one-dimensional array. |
| 3816 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3817 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3818 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3819 | |
| 3820 | // Will compute the higher-order strides here, rather than making a whole |
| 3821 | // pile of types and doing repetitive recursion on their contents. |
| 3822 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3823 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3824 | |
| 3825 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3826 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3827 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3828 | if (stride > 0) |
| 3829 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3830 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3831 | } |
| 3832 | } else { |
| 3833 | // single-dimensional array, and don't yet have stride |
| 3834 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3835 | // 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] | 3836 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3837 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3838 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3839 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3840 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3841 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3842 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3843 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3844 | else { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3845 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3846 | if (!lastBufferBlockMember) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3847 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3848 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3849 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3850 | #endif |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 3851 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3852 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3853 | if (stride > 0) |
| 3854 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | return spvType; |
| 3858 | } |
| 3859 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3860 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3861 | // |
| 3862 | // Identify interface members that don't have their required extension turned on. |
| 3863 | // |
| 3864 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3865 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3866 | #ifndef GLSLANG_WEB |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3867 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3868 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3869 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3870 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3871 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3872 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3873 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3874 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3875 | |
| 3876 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3877 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3878 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3879 | return true; |
| 3880 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3881 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3882 | return true; |
| 3883 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3884 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3885 | return true; |
| 3886 | } |
| 3887 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3888 | |
| 3889 | return false; |
| 3890 | }; |
| 3891 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3892 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3893 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3894 | // Mutually recursive with convertGlslangToSpvType(). |
| 3895 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3896 | const glslang::TTypeList* glslangMembers, |
| 3897 | glslang::TLayoutPacking explicitLayout, |
| 3898 | const glslang::TQualifier& qualifier) |
| 3899 | { |
| 3900 | // Create a vector of struct types for SPIR-V to consume |
| 3901 | std::vector<spv::Id> spvMembers; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3902 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, |
| 3903 | // except sometimes for blocks |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3904 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3905 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3906 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3907 | if (glslangMember.hiddenMember()) { |
| 3908 | ++memberDelta; |
| 3909 | if (type.getBasicType() == glslang::EbtBlock) |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3910 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3911 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3912 | if (type.getBasicType() == glslang::EbtBlock) { |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 3913 | if (filterMember(glslangMember)) { |
| 3914 | memberDelta++; |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3915 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3916 | continue; |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 3917 | } |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3918 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3919 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3920 | // modify just this child's view of the qualifier |
| 3921 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3922 | InheritQualifiers(memberQualifier, qualifier); |
| 3923 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3924 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3925 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3926 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3927 | |
| 3928 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3929 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3930 | i == (int)glslangMembers->size() - 1; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3931 | |
| 3932 | // Make forward pointers for any pointer members, and create a list of members to |
| 3933 | // convert to spirv types after creating the struct. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 3934 | if (glslangMember.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3935 | if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) { |
| 3936 | deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier)); |
| 3937 | } |
| 3938 | spvMembers.push_back( |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3939 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 3940 | true)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3941 | } else { |
| 3942 | spvMembers.push_back( |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3943 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 3944 | false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3945 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3946 | } |
| 3947 | } |
| 3948 | |
| 3949 | // Make the SPIR-V type |
| 3950 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3951 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3952 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3953 | |
| 3954 | // Decorate it |
| 3955 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3956 | |
John Kessenich | d72f488 | 2019-01-16 14:55:37 +0700 | [diff] [blame] | 3957 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3958 | auto it = deferredForwardPointers[i]; |
| 3959 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 3960 | } |
| 3961 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3962 | return spvType; |
| 3963 | } |
| 3964 | |
| 3965 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3966 | const glslang::TTypeList* glslangMembers, |
| 3967 | glslang::TLayoutPacking explicitLayout, |
| 3968 | const glslang::TQualifier& qualifier, |
| 3969 | spv::Id spvType) |
| 3970 | { |
| 3971 | // Name and decorate the non-hidden members |
| 3972 | int offset = -1; |
| 3973 | int locationOffset = 0; // for use within the members of this struct |
Chow | 478b232 | 2020-11-04 04:34:19 +0800 | [diff] [blame] | 3974 | bool memberLocationInvalid = type.isArrayOfArrays() || |
| 3975 | (type.isArray() && (type.getQualifier().isArrayedIo(glslangIntermediate->getStage()) == false)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3976 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3977 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3978 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3979 | if (type.getBasicType() == glslang::EbtBlock) { |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3980 | member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3981 | if (filterMember(glslangMember)) |
| 3982 | continue; |
| 3983 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3984 | |
| 3985 | // modify just this child's view of the qualifier |
| 3986 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3987 | InheritQualifiers(memberQualifier, qualifier); |
| 3988 | |
| 3989 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3990 | if (member < 0) |
| 3991 | continue; |
| 3992 | |
| 3993 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3994 | builder.addMemberDecoration(spvType, member, |
| 3995 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 3996 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 3997 | // Add interpolation and auxiliary storage decorations only to |
| 3998 | // top-level members of Input and Output storage classes |
| 3999 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 4000 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 4001 | if (type.getBasicType() == glslang::EbtBlock || |
| 4002 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 4003 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 4004 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4005 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4006 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 4007 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4008 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4009 | } |
| 4010 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4011 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4012 | #ifndef GLSLANG_WEB |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4013 | if (type.getBasicType() == glslang::EbtBlock && |
| 4014 | qualifier.storage == glslang::EvqBuffer) { |
| 4015 | // Add memory decorations only to top-level members of shader storage block |
| 4016 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4017 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4018 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 4019 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 4020 | } |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 4021 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4022 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4023 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4024 | // Location assignment was already completed correctly by the front end, |
| 4025 | // just track whether a member needs to be decorated. |
| 4026 | // Ignore member locations if the container is an array, as that's |
| 4027 | // ill-specified and decisions have been made to not allow this. |
Chow | 478b232 | 2020-11-04 04:34:19 +0800 | [diff] [blame] | 4028 | if (!memberLocationInvalid && memberQualifier.hasLocation()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4029 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4030 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4031 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 4032 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 4033 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 4034 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4035 | // component, XFB, others |
| 4036 | if (glslangMember.getQualifier().hasComponent()) |
| 4037 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 4038 | glslangMember.getQualifier().layoutComponent); |
| 4039 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 4040 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 4041 | glslangMember.getQualifier().layoutXfbOffset); |
| 4042 | else if (explicitLayout != glslang::ElpNone) { |
| 4043 | // figure out what to do with offset, which is accumulating |
| 4044 | int nextOffset; |
| 4045 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 4046 | if (offset >= 0) |
| 4047 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 4048 | offset = nextOffset; |
| 4049 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4050 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4051 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 4052 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 4053 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4054 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4055 | // built-in variable decorations |
| 4056 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 4057 | if (builtIn != spv::BuiltInMax) |
| 4058 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4059 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4060 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4061 | // nonuniform |
| 4062 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 4063 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4064 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 4065 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 4066 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 4067 | memberQualifier.semanticName); |
| 4068 | } |
| 4069 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4070 | if (builtIn == spv::BuiltInLayer) { |
| 4071 | // SPV_NV_viewport_array2 extension |
| 4072 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 4073 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 4074 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 4075 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4076 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4077 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 4078 | builder.addMemberDecoration(spvType, member, |
| 4079 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 4080 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 4081 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 4082 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 4083 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4084 | } |
| 4085 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 4086 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 4087 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 4088 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 4089 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4090 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4094 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 4095 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4096 | } |
| 4097 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4098 | // Turn the expression forming the array size into an id. |
| 4099 | // This is not quite trivial, because of specialization constants. |
| 4100 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 4101 | // a specialization constant expression is. |
| 4102 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 4103 | { |
| 4104 | // First, see if this is sized with a node, meaning a specialization constant: |
| 4105 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 4106 | if (specNode != nullptr) { |
| 4107 | builder.clearAccessChain(); |
| 4108 | specNode->traverse(this); |
| 4109 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 4110 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4111 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4112 | // Otherwise, need a compile-time (front end) size, get it: |
| 4113 | int size = arraySizes.getDimSize(dim); |
| 4114 | assert(size > 0); |
| 4115 | return builder.makeUintConstant(size); |
| 4116 | } |
| 4117 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4118 | // Wrap the builder's accessChainLoad to: |
| 4119 | // - localize handling of RelaxedPrecision |
| 4120 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 4121 | // (avoids unnecessary work and possible type punning for structures) |
| 4122 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4123 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 4124 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4125 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4126 | |
| 4127 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4128 | coherentFlags |= TranslateCoherent(type); |
| 4129 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4130 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 4131 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4132 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4133 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4134 | TranslateNonUniformDecoration(type.getQualifier()), |
| 4135 | nominalTypeId, |
| 4136 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
| 4137 | TranslateMemoryScope(coherentFlags), |
| 4138 | alignment); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4139 | |
| 4140 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4141 | if (type.getBasicType() == glslang::EbtBool) { |
| 4142 | if (builder.isScalarType(nominalTypeId)) { |
| 4143 | // Conversion for bool |
| 4144 | spv::Id boolType = builder.makeBoolType(); |
| 4145 | if (nominalTypeId != boolType) |
| 4146 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 4147 | } else if (builder.isVectorType(nominalTypeId)) { |
| 4148 | // Conversion for bvec |
| 4149 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 4150 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 4151 | if (nominalTypeId != bvecType) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4152 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, |
| 4153 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4154 | } |
| 4155 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4156 | |
| 4157 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4158 | } |
| 4159 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4160 | // Wrap the builder's accessChainStore to: |
| 4161 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4162 | // |
| 4163 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4164 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 4165 | { |
| 4166 | // Need to convert to abstract types when necessary |
| 4167 | if (type.getBasicType() == glslang::EbtBool) { |
| 4168 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 4169 | |
| 4170 | if (builder.isScalarType(nominalTypeId)) { |
| 4171 | // Conversion for bool |
| 4172 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4173 | if (nominalTypeId != boolType) { |
| 4174 | // keep these outside arguments, for determinant order-of-evaluation |
| 4175 | spv::Id one = builder.makeUintConstant(1); |
| 4176 | spv::Id zero = builder.makeUintConstant(0); |
| 4177 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 4178 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 4179 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4180 | } else if (builder.isVectorType(nominalTypeId)) { |
| 4181 | // Conversion for bvec |
| 4182 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 4183 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4184 | if (nominalTypeId != bvecType) { |
| 4185 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 4186 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 4187 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 4188 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4189 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 4190 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 4191 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4192 | } |
| 4193 | } |
| 4194 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4195 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4196 | coherentFlags |= TranslateCoherent(type); |
| 4197 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4198 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 4199 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4200 | |
Bas Nieuwenhuizen | de949a2 | 2020-08-24 23:27:26 +0200 | [diff] [blame] | 4201 | builder.accessChainStore(rvalue, TranslateNonUniformDecoration(type.getQualifier()), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4202 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & |
| 4203 | ~spv::MemoryAccessMakePointerVisibleKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4204 | TranslateMemoryScope(coherentFlags), alignment); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4205 | } |
| 4206 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4207 | // For storing when types match at the glslang level, but not might match at the |
| 4208 | // SPIR-V level. |
| 4209 | // |
| 4210 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4211 | // 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] | 4212 | // as in a member-decorated way. |
| 4213 | // |
| 4214 | // NOTE: This function can handle any store request; if it's not special it |
| 4215 | // simplifies to a simple OpStore. |
| 4216 | // |
| 4217 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 4218 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 4219 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4220 | // we only do the complex path here if it's an aggregate |
| 4221 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4222 | accessChainStore(type, rValue); |
| 4223 | return; |
| 4224 | } |
| 4225 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4226 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4227 | spv::Id rType = builder.getTypeId(rValue); |
| 4228 | spv::Id lValue = builder.accessChainGetLValue(); |
| 4229 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 4230 | if (lType == rType) { |
| 4231 | accessChainStore(type, rValue); |
| 4232 | return; |
| 4233 | } |
| 4234 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4235 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4236 | // where the two types were the same type in GLSL. This requires member |
| 4237 | // by member copy, recursively. |
| 4238 | |
John Kessenich | fbb6bdf | 2019-01-15 21:48:27 +0700 | [diff] [blame] | 4239 | // SPIR-V 1.4 added an instruction to do help do this. |
| 4240 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 4241 | // However, bool in uniform space is changed to int, so |
| 4242 | // OpCopyLogical does not work for that. |
| 4243 | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
| 4244 | bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0); |
| 4245 | bool lBool = builder.containsType(lType, spv::OpTypeBool, 0); |
| 4246 | if (lBool == rBool) { |
| 4247 | spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue); |
| 4248 | accessChainStore(type, logicalCopy); |
| 4249 | return; |
| 4250 | } |
| 4251 | } |
| 4252 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4253 | // If an array, copy element by element. |
| 4254 | if (type.isArray()) { |
| 4255 | glslang::TType glslangElementType(type, 0); |
| 4256 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 4257 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 4258 | // get the source member |
| 4259 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4260 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4261 | // set up the target storage |
| 4262 | builder.clearAccessChain(); |
| 4263 | builder.setAccessChainLValue(lValue); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4264 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), |
| 4265 | type.getBufferReferenceAlignment()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4266 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4267 | // store the member |
| 4268 | multiTypeStore(glslangElementType, elementRValue); |
| 4269 | } |
| 4270 | } else { |
| 4271 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4272 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4273 | // loop over structure members |
| 4274 | const glslang::TTypeList& members = *type.getStruct(); |
| 4275 | for (int m = 0; m < (int)members.size(); ++m) { |
| 4276 | const glslang::TType& glslangMemberType = *members[m].type; |
| 4277 | |
| 4278 | // get the source member |
| 4279 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 4280 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 4281 | |
| 4282 | // set up the target storage |
| 4283 | builder.clearAccessChain(); |
| 4284 | builder.setAccessChainLValue(lValue); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4285 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), |
| 4286 | type.getBufferReferenceAlignment()); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4287 | |
| 4288 | // store the member |
| 4289 | multiTypeStore(glslangMemberType, memberRValue); |
| 4290 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4291 | } |
| 4292 | } |
| 4293 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4294 | // Decide whether or not this type should be |
| 4295 | // decorated with offsets and strides, and if so |
| 4296 | // whether std140 or std430 rules should be applied. |
| 4297 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 4298 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4299 | // has to be a block |
| 4300 | if (type.getBasicType() != glslang::EbtBlock) |
| 4301 | return glslang::ElpNone; |
| 4302 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4303 | // 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] | 4304 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4305 | type.getQualifier().storage != glslang::EvqBuffer && |
| 4306 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4307 | return glslang::ElpNone; |
| 4308 | |
| 4309 | // return the layout to use |
| 4310 | switch (type.getQualifier().layoutPacking) { |
| 4311 | case glslang::ElpStd140: |
| 4312 | case glslang::ElpStd430: |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4313 | case glslang::ElpScalar: |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4314 | return type.getQualifier().layoutPacking; |
| 4315 | default: |
| 4316 | return glslang::ElpNone; |
| 4317 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 4318 | } |
| 4319 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4320 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4321 | int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, |
| 4322 | glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4323 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4324 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4325 | int stride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4326 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, |
| 4327 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 4328 | |
| 4329 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4330 | } |
| 4331 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4332 | // 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] | 4333 | // when used as a member of an interface block |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4334 | int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, |
| 4335 | glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4336 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4337 | glslang::TType elementType; |
| 4338 | elementType.shallowCopy(matrixType); |
| 4339 | elementType.clearArraySizes(); |
| 4340 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4341 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4342 | int stride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4343 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, |
| 4344 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4345 | |
| 4346 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4347 | } |
| 4348 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4349 | // Given a member type of a struct, realign the current offset for it, and compute |
| 4350 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 4351 | // on the next call. |
| 4352 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 4353 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 4354 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4355 | void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, |
| 4356 | int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4357 | { |
| 4358 | // this will get a positive value when deemed necessary |
| 4359 | nextOffset = -1; |
| 4360 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4361 | // override anything in currentOffset with user-set offset |
| 4362 | if (memberType.getQualifier().hasOffset()) |
| 4363 | currentOffset = memberType.getQualifier().layoutOffset; |
| 4364 | |
| 4365 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 4366 | // in which case the following code does not matter. But, that's not quite right |
| 4367 | // once cross-compilation unit GLSL validation is done, as the original user |
| 4368 | // settings are needed in layoutOffset, and then the following will come into play. |
| 4369 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4370 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4371 | if (! memberType.getQualifier().hasOffset()) |
| 4372 | currentOffset = -1; |
| 4373 | |
| 4374 | return; |
| 4375 | } |
| 4376 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4377 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4378 | if (currentOffset < 0) |
| 4379 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4380 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4381 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 4382 | // but possibly not yet correctly aligned. |
| 4383 | |
| 4384 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4385 | int dummyStride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4386 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, |
| 4387 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4388 | |
| 4389 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4390 | // TODO: make this consistent in early phases of code: |
| 4391 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 4392 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 4393 | // 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] | 4394 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4395 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4396 | int dummySize; |
| 4397 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 4398 | if (componentAlignment <= 4) |
| 4399 | memberAlignment = componentAlignment; |
| 4400 | } |
| 4401 | |
| 4402 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4403 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4404 | |
| 4405 | // Bump up to vec4 if there is a bad straddle |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4406 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, |
| 4407 | currentOffset)) |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4408 | glslang::RoundToPow2(currentOffset, 16); |
| 4409 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4410 | nextOffset = currentOffset + memberSize; |
| 4411 | } |
| 4412 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4413 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4414 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4415 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 4416 | switch (glslangBuiltIn) |
| 4417 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4418 | case glslang::EbvPointSize: |
| 4419 | #ifndef GLSLANG_WEB |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4420 | case glslang::EbvClipDistance: |
| 4421 | case glslang::EbvCullDistance: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4422 | case glslang::EbvViewportMaskNV: |
| 4423 | case glslang::EbvSecondaryPositionNV: |
| 4424 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 4425 | case glslang::EbvPositionPerViewNV: |
| 4426 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4427 | case glslang::EbvTaskCountNV: |
| 4428 | case glslang::EbvPrimitiveCountNV: |
| 4429 | case glslang::EbvPrimitiveIndicesNV: |
| 4430 | case glslang::EbvClipDistancePerViewNV: |
| 4431 | case glslang::EbvCullDistancePerViewNV: |
| 4432 | case glslang::EbvLayerPerViewNV: |
| 4433 | case glslang::EbvMeshViewCountNV: |
| 4434 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4435 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4436 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 4437 | // Alternately, we could just call this for any glslang built-in, since the |
| 4438 | // capability already guards against duplicates. |
| 4439 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 4440 | break; |
| 4441 | default: |
| 4442 | // Capabilities were already generated when the struct was declared. |
| 4443 | break; |
| 4444 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4445 | } |
| 4446 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4447 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4448 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 4449 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4450 | } |
| 4451 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4452 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4453 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 4454 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4455 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4456 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4457 | assert(qualifier == glslang::EvqIn || |
| 4458 | qualifier == glslang::EvqOut || |
| 4459 | qualifier == glslang::EvqInOut || |
rdb | d8edfd8 | 2020-06-02 08:30:07 +0200 | [diff] [blame] | 4460 | qualifier == glslang::EvqUniform || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4461 | qualifier == glslang::EvqConstReadOnly); |
rdb | d8edfd8 | 2020-06-02 08:30:07 +0200 | [diff] [blame] | 4462 | return qualifier != glslang::EvqConstReadOnly && |
| 4463 | qualifier != glslang::EvqUniform; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | // Is parameter pass-by-original? |
| 4467 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 4468 | bool implicitThisParam) |
| 4469 | { |
| 4470 | if (implicitThisParam) // implicit this |
| 4471 | return true; |
| 4472 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4473 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4474 | return paramType.containsOpaque() || // sampler, etc. |
| 4475 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 4476 | } |
| 4477 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4478 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 4479 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4480 | { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4481 | const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, |
| 4482 | bool useVulkanMemoryModel) { |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4483 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 4484 | if (paramPrecision != spv::NoPrecision) |
| 4485 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4486 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 4487 | if (type.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4488 | // Original and non-writable params pass the pointer directly and |
| 4489 | // use restrict/aliased, others are stored to a pointer in Function |
| 4490 | // memory and use RestrictPointer/AliasedPointer. |
| 4491 | if (originalParam(type.getQualifier().storage, type, false) || |
| 4492 | !writableParam(type.getQualifier().storage)) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 4493 | decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrict : |
| 4494 | spv::DecorationAliased); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4495 | } else { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 4496 | decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrictPointerEXT : |
| 4497 | spv::DecorationAliasedPointerEXT); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4498 | } |
| 4499 | } |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4500 | }; |
| 4501 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4502 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4503 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4504 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4505 | continue; |
| 4506 | |
| 4507 | // 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] | 4508 | // 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] | 4509 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4510 | // 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] | 4511 | // function. What it is an address of varies: |
| 4512 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4513 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 4514 | // 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] | 4515 | // |
| 4516 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 4517 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4518 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 4519 | // 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] | 4520 | |
| 4521 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4522 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4523 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 4524 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4525 | #ifdef ENABLE_HLSL |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4526 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 4527 | glslangIntermediate->implicitThisName; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4528 | #else |
| 4529 | bool implicitThis = false; |
| 4530 | #endif |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4531 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4532 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4533 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4534 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4535 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4536 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 4537 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4538 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4539 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 4540 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4541 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4542 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4543 | paramTypes.push_back(typeId); |
| 4544 | } |
| 4545 | |
| 4546 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4547 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 4548 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4549 | glslFunction->getName().c_str(), paramTypes, |
| 4550 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4551 | if (implicitThis) |
| 4552 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4553 | |
| 4554 | // Track function to emit/call later |
| 4555 | functionMap[glslFunction->getName().c_str()] = function; |
| 4556 | |
| 4557 | // Set the parameter id's |
| 4558 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4559 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 4560 | // give a name too |
| 4561 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4562 | |
| 4563 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4564 | if (paramType.contains8BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4565 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4566 | if (paramType.contains16BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4567 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4568 | if (paramType.contains16BitFloat()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4569 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4570 | } |
| 4571 | } |
| 4572 | } |
| 4573 | |
| 4574 | // Process all the initializers, while skipping the functions and link objects |
| 4575 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 4576 | { |
| 4577 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 4578 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 4579 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4580 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != |
| 4581 | glslang::EOpLinkerObjects) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4582 | |
| 4583 | // 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] | 4584 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4585 | initializer->traverse(this); |
| 4586 | } |
| 4587 | } |
| 4588 | } |
| 4589 | |
| 4590 | // Process all the functions, while skipping initializers. |
| 4591 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4592 | { |
| 4593 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4594 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 4595 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4596 | node->traverse(this); |
| 4597 | } |
| 4598 | } |
| 4599 | |
| 4600 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 4601 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4602 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4603 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4604 | currentFunction = functionMap[node->getName().c_str()]; |
| 4605 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4606 | builder.setBuildPoint(functionBlock); |
| 4607 | } |
| 4608 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4609 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 4610 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4611 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4612 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4613 | |
| 4614 | glslang::TSampler sampler = {}; |
| 4615 | bool cubeCompare = false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4616 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4617 | bool f16ShadowCompare = false; |
| 4618 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4619 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4620 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 4621 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4622 | #ifndef GLSLANG_WEB |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4623 | f16ShadowCompare = sampler.shadow && |
| 4624 | glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4625 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4626 | } |
| 4627 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4628 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 4629 | builder.clearAccessChain(); |
| 4630 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4631 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4632 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4633 | // Special case l-value operands |
| 4634 | bool lvalue = false; |
| 4635 | switch (node.getOp()) { |
| 4636 | case glslang::EOpImageAtomicAdd: |
| 4637 | case glslang::EOpImageAtomicMin: |
| 4638 | case glslang::EOpImageAtomicMax: |
| 4639 | case glslang::EOpImageAtomicAnd: |
| 4640 | case glslang::EOpImageAtomicOr: |
| 4641 | case glslang::EOpImageAtomicXor: |
| 4642 | case glslang::EOpImageAtomicExchange: |
| 4643 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4644 | case glslang::EOpImageAtomicLoad: |
| 4645 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4646 | if (i == 0) |
| 4647 | lvalue = true; |
| 4648 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4649 | case glslang::EOpSparseImageLoad: |
| 4650 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 4651 | lvalue = true; |
| 4652 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4653 | case glslang::EOpSparseTexture: |
| 4654 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 4655 | lvalue = true; |
| 4656 | break; |
| 4657 | case glslang::EOpSparseTextureClamp: |
| 4658 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 4659 | lvalue = true; |
| 4660 | break; |
| 4661 | case glslang::EOpSparseTextureLod: |
| 4662 | case glslang::EOpSparseTextureOffset: |
| 4663 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 4664 | lvalue = true; |
| 4665 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4666 | case glslang::EOpSparseTextureFetch: |
| 4667 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 4668 | lvalue = true; |
| 4669 | break; |
| 4670 | case glslang::EOpSparseTextureFetchOffset: |
| 4671 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 4672 | lvalue = true; |
| 4673 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4674 | case glslang::EOpSparseTextureLodOffset: |
| 4675 | case glslang::EOpSparseTextureGrad: |
| 4676 | case glslang::EOpSparseTextureOffsetClamp: |
| 4677 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 4678 | lvalue = true; |
| 4679 | break; |
| 4680 | case glslang::EOpSparseTextureGradOffset: |
| 4681 | case glslang::EOpSparseTextureGradClamp: |
| 4682 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 4683 | lvalue = true; |
| 4684 | break; |
| 4685 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 4686 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 4687 | lvalue = true; |
| 4688 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4689 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4690 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 4691 | lvalue = true; |
| 4692 | break; |
| 4693 | case glslang::EOpSparseTextureGatherOffset: |
| 4694 | case glslang::EOpSparseTextureGatherOffsets: |
| 4695 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 4696 | lvalue = true; |
| 4697 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4698 | case glslang::EOpSparseTextureGatherLod: |
| 4699 | if (i == 3) |
| 4700 | lvalue = true; |
| 4701 | break; |
| 4702 | case glslang::EOpSparseTextureGatherLodOffset: |
| 4703 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 4704 | if (i == 4) |
| 4705 | lvalue = true; |
| 4706 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4707 | case glslang::EOpSparseImageLoadLod: |
| 4708 | if (i == 3) |
| 4709 | lvalue = true; |
| 4710 | break; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4711 | case glslang::EOpImageSampleFootprintNV: |
| 4712 | if (i == 4) |
| 4713 | lvalue = true; |
| 4714 | break; |
| 4715 | case glslang::EOpImageSampleFootprintClampNV: |
| 4716 | case glslang::EOpImageSampleFootprintLodNV: |
| 4717 | if (i == 5) |
| 4718 | lvalue = true; |
| 4719 | break; |
| 4720 | case glslang::EOpImageSampleFootprintGradNV: |
| 4721 | if (i == 6) |
| 4722 | lvalue = true; |
| 4723 | break; |
| 4724 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 4725 | if (i == 7) |
| 4726 | lvalue = true; |
| 4727 | break; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4728 | default: |
| 4729 | break; |
| 4730 | } |
| 4731 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4732 | if (lvalue) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4733 | arguments.push_back(builder.accessChainGetLValue()); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4734 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 4735 | lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
| 4736 | } else |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4737 | #endif |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4738 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4739 | } |
| 4740 | } |
| 4741 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4742 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4743 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4744 | builder.clearAccessChain(); |
| 4745 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4746 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4747 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4748 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4749 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 4750 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4751 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4752 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4753 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 4754 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4755 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4756 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4757 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4758 | const glslang::TType &imageType = node->getAsAggregate() |
| 4759 | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 4760 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4761 | const glslang::TSampler sampler = imageType.getSampler(); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4762 | #ifdef GLSLANG_WEB |
| 4763 | const bool f16ShadowCompare = false; |
| 4764 | #else |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4765 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4766 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 4767 | : false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4768 | #endif |
| 4769 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4770 | const auto signExtensionMask = [&]() { |
| 4771 | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
| 4772 | if (sampler.type == glslang::EbtUint) |
| 4773 | return spv::ImageOperandsZeroExtendMask; |
| 4774 | else if (sampler.type == glslang::EbtInt) |
| 4775 | return spv::ImageOperandsSignExtendMask; |
| 4776 | } |
| 4777 | return spv::ImageOperandsMaskNone; |
| 4778 | }; |
| 4779 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4780 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 4781 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4782 | std::vector<spv::Id> arguments; |
| 4783 | if (node->getAsAggregate()) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4784 | translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4785 | else |
| 4786 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | 12c155f | 2020-06-30 07:52:05 -0600 | [diff] [blame] | 4787 | spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4788 | |
| 4789 | spv::Builder::TextureParameters params = { }; |
| 4790 | params.sampler = arguments[0]; |
| 4791 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4792 | glslang::TCrackedTextureOp cracked; |
| 4793 | node->crackTexture(sampler, cracked); |
| 4794 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4795 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4796 | |
Bas Nieuwenhuizen | 5806431 | 2020-09-03 03:04:13 +0200 | [diff] [blame] | 4797 | if (builder.isSampledImage(params.sampler) && |
| 4798 | ((cracked.query && node->getOp() != glslang::EOpTextureQueryLod) || cracked.fragMask || cracked.fetch)) { |
| 4799 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4800 | if (imageType.getQualifier().isNonUniform()) { |
| 4801 | builder.addDecoration(params.sampler, spv::DecorationNonUniformEXT); |
| 4802 | } |
| 4803 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4804 | // Check for queries |
| 4805 | if (cracked.query) { |
| 4806 | switch (node->getOp()) { |
| 4807 | case glslang::EOpImageQuerySize: |
| 4808 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4809 | if (arguments.size() > 1) { |
| 4810 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4811 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4812 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4813 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4814 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4815 | case glslang::EOpImageQuerySamples: |
| 4816 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4817 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4818 | case glslang::EOpTextureQueryLod: |
| 4819 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4820 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4821 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4822 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4823 | case glslang::EOpSparseTexelsResident: |
| 4824 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4825 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4826 | default: |
| 4827 | assert(0); |
| 4828 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4829 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4830 | } |
| 4831 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4832 | int components = node->getType().getVectorSize(); |
| 4833 | |
| 4834 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 4835 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 4836 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 4837 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 4838 | // here around e.g. which ones return scalars or other types. |
| 4839 | components = 4; |
| 4840 | } |
| 4841 | |
| 4842 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 4843 | |
| 4844 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 4845 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4846 | // Check for image functions other than queries |
| 4847 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4848 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4849 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4850 | spv::IdImmediate image = { true, *(opIt++) }; |
| 4851 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4852 | |
| 4853 | // Handle subpass operations |
| 4854 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 4855 | // built-in function. |
| 4856 | if (cracked.subpass) { |
| 4857 | // add on the (0,0) coordinate |
| 4858 | spv::Id zero = builder.makeIntConstant(0); |
| 4859 | std::vector<spv::Id> comps; |
| 4860 | comps.push_back(zero); |
| 4861 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4862 | spv::IdImmediate coord = { true, |
| 4863 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 4864 | operands.push_back(coord); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4865 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
| 4866 | imageOperands.word = imageOperands.word | signExtensionMask(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4867 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4868 | imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
| 4869 | } |
| 4870 | if (imageOperands.word != spv::ImageOperandsMaskNone) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4871 | operands.push_back(imageOperands); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4872 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4873 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 4874 | operands.push_back(imageOperand); |
| 4875 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4876 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4877 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 4878 | builder.setPrecision(result, precision); |
| 4879 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4880 | } |
| 4881 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4882 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 4883 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4884 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4885 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4886 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4887 | mask = mask | spv::ImageOperandsSampleMask; |
| 4888 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4889 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4890 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4891 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4892 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4893 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4894 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4895 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4896 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4897 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4898 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4899 | operands.push_back(imageOperands); |
| 4900 | } |
| 4901 | if (mask & spv::ImageOperandsSampleMask) { |
| 4902 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4903 | operands.push_back(imageOperand); |
| 4904 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4905 | if (mask & spv::ImageOperandsLodMask) { |
| 4906 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4907 | operands.push_back(imageOperand); |
| 4908 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4909 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4910 | spv::IdImmediate imageOperand = { true, |
| 4911 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4912 | operands.push_back(imageOperand); |
| 4913 | } |
| 4914 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4915 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4916 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4917 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4918 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4919 | builder.setPrecision(result[0], precision); |
| 4920 | |
| 4921 | // If needed, add a conversion constructor to the proper size. |
| 4922 | if (components != node->getType().getVectorSize()) |
| 4923 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4924 | |
| 4925 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4926 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4927 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4928 | // Push the texel value before the operands |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4929 | if (sampler.isMultiSample() || cracked.lod) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4930 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4931 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4932 | } else { |
| 4933 | spv::IdImmediate texel = { true, *opIt }; |
| 4934 | operands.push_back(texel); |
| 4935 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4936 | |
| 4937 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4938 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4939 | mask = mask | spv::ImageOperandsSampleMask; |
| 4940 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4941 | if (cracked.lod) { |
| 4942 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4943 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4944 | mask = mask | spv::ImageOperandsLodMask; |
| 4945 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4946 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4947 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4948 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4949 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4950 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4951 | operands.push_back(imageOperands); |
| 4952 | } |
| 4953 | if (mask & spv::ImageOperandsSampleMask) { |
| 4954 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4955 | operands.push_back(imageOperand); |
| 4956 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4957 | if (mask & spv::ImageOperandsLodMask) { |
| 4958 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4959 | operands.push_back(imageOperand); |
| 4960 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4961 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4962 | spv::IdImmediate imageOperand = { true, |
| 4963 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4964 | operands.push_back(imageOperand); |
| 4965 | } |
| 4966 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4967 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4968 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4969 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4970 | return spv::NoResult; |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4971 | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
| 4972 | node->getOp() == glslang::EOpSparseImageLoadLod) { |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4973 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4974 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4975 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4976 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4977 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4978 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4979 | mask = mask | spv::ImageOperandsSampleMask; |
| 4980 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4981 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4982 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4983 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4984 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4985 | mask = mask | spv::ImageOperandsLodMask; |
| 4986 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4987 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4988 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4989 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4990 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4991 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4992 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4993 | } |
| 4994 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4995 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4996 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4997 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4998 | if (mask & spv::ImageOperandsLodMask) { |
| 4999 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5000 | operands.push_back(imageOperand); |
| 5001 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5002 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5003 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope( |
| 5004 | TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5005 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | // Create the return type that was a special structure |
| 5009 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 5010 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 5011 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 5012 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 5013 | |
| 5014 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 5015 | |
| 5016 | // Decode the return type |
| 5017 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 5018 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 5019 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 5020 | // Process image atomic operations |
| 5021 | |
| 5022 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 5023 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5024 | // For non-MS, the sample value should be 0 |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5025 | spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5026 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5027 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5028 | spv::Id resultTypeId; |
| 5029 | // imageAtomicStore has a void return type so base the pointer type on |
| 5030 | // the type of the value operand. |
| 5031 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fb18b6d | 2020-02-22 22:04:31 +0800 | [diff] [blame] | 5032 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(*opIt)); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5033 | } else { |
| 5034 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 5035 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 5036 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Jeff Bolz | 39ffdaf | 2020-03-09 10:48:12 -0500 | [diff] [blame] | 5037 | if (imageType.getQualifier().nonUniform) { |
| 5038 | builder.addDecoration(pointer, spv::DecorationNonUniformEXT); |
| 5039 | } |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5040 | |
| 5041 | std::vector<spv::Id> operands; |
| 5042 | operands.push_back(pointer); |
| 5043 | for (; opIt != arguments.end(); ++opIt) |
| 5044 | operands.push_back(*opIt); |
| 5045 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5046 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), |
| 5047 | lvalueCoherentFlags); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5048 | } |
| 5049 | } |
| 5050 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5051 | #ifndef GLSLANG_WEB |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 5052 | // Check for fragment mask functions other than queries |
| 5053 | if (cracked.fragMask) { |
| 5054 | assert(sampler.ms); |
| 5055 | |
| 5056 | auto opIt = arguments.begin(); |
| 5057 | std::vector<spv::Id> operands; |
| 5058 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 5059 | operands.push_back(params.sampler); |
| 5060 | ++opIt; |
| 5061 | |
| 5062 | if (sampler.isSubpass()) { |
| 5063 | // add on the (0,0) coordinate |
| 5064 | spv::Id zero = builder.makeIntConstant(0); |
| 5065 | std::vector<spv::Id> comps; |
| 5066 | comps.push_back(zero); |
| 5067 | comps.push_back(zero); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5068 | operands.push_back(builder.makeCompositeConstant( |
| 5069 | builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | for (; opIt != arguments.end(); ++opIt) |
| 5073 | operands.push_back(*opIt); |
| 5074 | |
| 5075 | spv::Op fragMaskOp = spv::OpNop; |
| 5076 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 5077 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 5078 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 5079 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 5080 | |
| 5081 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 5082 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 5083 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 5084 | } |
| 5085 | #endif |
| 5086 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5087 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5088 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5089 | bool imageFootprint = node->isImageFootprint(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5090 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 5091 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5092 | // check for bias argument |
| 5093 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5094 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5095 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5096 | if (cracked.gather) |
| 5097 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 5098 | |
| 5099 | if (f16ShadowCompare) |
| 5100 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5101 | if (cracked.offset) |
| 5102 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5103 | else if (cracked.offsets) |
| 5104 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5105 | if (cracked.grad) |
| 5106 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5107 | if (cracked.lodClamp) |
| 5108 | ++nonBiasArgCount; |
| 5109 | if (sparse) |
| 5110 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5111 | if (imageFootprint) |
| 5112 | //Following three extra arguments |
| 5113 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 5114 | nonBiasArgCount += 3; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5115 | if ((int)arguments.size() > nonBiasArgCount) |
| 5116 | bias = true; |
| 5117 | } |
| 5118 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5119 | #ifndef GLSLANG_WEB |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5120 | if (cracked.gather) { |
| 5121 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 5122 | if (bias || cracked.lod || |
| 5123 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 5124 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 5125 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5126 | } |
| 5127 | } |
| 5128 | #endif |
| 5129 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5130 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5131 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5132 | params.coords = arguments[1]; |
| 5133 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 5134 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5135 | |
| 5136 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 5137 | if (cubeCompare || f16ShadowCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5138 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5139 | ++extraArgs; |
| 5140 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5141 | params.Dref = arguments[2]; |
| 5142 | ++extraArgs; |
| 5143 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5144 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5145 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5146 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5147 | 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] | 5148 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5149 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 5150 | indexes.push_back(dRefComp); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5151 | params.Dref = builder.createCompositeExtract(params.coords, |
| 5152 | builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5153 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5154 | |
| 5155 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5156 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 5157 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5158 | ++extraArgs; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5159 | } else if (glslangIntermediate->getStage() != EShLangFragment && |
| 5160 | !(glslangIntermediate->getStage() == EShLangCompute && |
| 5161 | glslangIntermediate->hasLayoutDerivativeModeNone())) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 5162 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 5163 | noImplicitLod = true; |
| 5164 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5165 | |
| 5166 | // multisample |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5167 | if (sampler.isMultiSample()) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 5168 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5169 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5170 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5171 | |
| 5172 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5173 | if (cracked.grad) { |
| 5174 | params.gradX = arguments[2 + extraArgs]; |
| 5175 | params.gradY = arguments[3 + extraArgs]; |
| 5176 | extraArgs += 2; |
| 5177 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5178 | |
| 5179 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5180 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5181 | params.offset = arguments[2 + extraArgs]; |
| 5182 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5183 | } else if (cracked.offsets) { |
| 5184 | params.offsets = arguments[2 + extraArgs]; |
| 5185 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5186 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5187 | |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5188 | #ifndef GLSLANG_WEB |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5189 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5190 | if (cracked.lodClamp) { |
| 5191 | params.lodClamp = arguments[2 + extraArgs]; |
| 5192 | ++extraArgs; |
| 5193 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5194 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5195 | if (sparse) { |
| 5196 | params.texelOut = arguments[2 + extraArgs]; |
| 5197 | ++extraArgs; |
| 5198 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5199 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5200 | if (cracked.gather && ! sampler.shadow) { |
| 5201 | // default component is 0, if missing, otherwise an argument |
| 5202 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5203 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5204 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5205 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5206 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5207 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5208 | spv::Id resultStruct = spv::NoResult; |
| 5209 | if (imageFootprint) { |
| 5210 | //Following three extra arguments |
| 5211 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 5212 | params.granularity = arguments[2 + extraArgs]; |
| 5213 | params.coarse = arguments[3 + extraArgs]; |
| 5214 | resultStruct = arguments[4 + extraArgs]; |
| 5215 | extraArgs += 3; |
| 5216 | } |
| 5217 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5218 | // bias |
| 5219 | if (bias) { |
| 5220 | params.bias = arguments[2 + extraArgs]; |
| 5221 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5222 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5223 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5224 | #ifndef GLSLANG_WEB |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5225 | if (imageFootprint) { |
| 5226 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 5227 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 5228 | |
| 5229 | |
| 5230 | //resultStructType(OpenGL type) contains 5 elements: |
| 5231 | //struct gl_TextureFootprint2DNV { |
| 5232 | // uvec2 anchor; |
| 5233 | // uvec2 offset; |
| 5234 | // uvec2 mask; |
| 5235 | // uint lod; |
| 5236 | // uint granularity; |
| 5237 | //}; |
| 5238 | //or |
| 5239 | //struct gl_TextureFootprint3DNV { |
| 5240 | // uvec3 anchor; |
| 5241 | // uvec3 offset; |
| 5242 | // uvec2 mask; |
| 5243 | // uint lod; |
| 5244 | // uint granularity; |
| 5245 | //}; |
| 5246 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 5247 | assert(builder.isStructType(resultStructType)); |
| 5248 | |
| 5249 | //resType (SPIR-V type) contains 6 elements: |
| 5250 | //Member 0 must be a Boolean type scalar(LOD), |
| 5251 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 5252 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 5253 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 5254 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 5255 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 5256 | std::vector<spv::Id> members; |
| 5257 | members.push_back(resultType()); |
| 5258 | for (int i = 0; i < 5; i++) { |
| 5259 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 5260 | } |
| 5261 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 5262 | |
| 5263 | //call ImageFootprintNV |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5264 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
| 5265 | cracked.gather, noImplicitLod, params, signExtensionMask()); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5266 | |
| 5267 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 5268 | for (int i = 0; i < 5; i++) { |
| 5269 | builder.clearAccessChain(); |
| 5270 | builder.setAccessChainLValue(resultStruct); |
| 5271 | |
| 5272 | //Accessing to a struct we created, no coherent flag is set |
| 5273 | spv::Builder::AccessChain::CoherentFlags flags; |
| 5274 | flags.clear(); |
| 5275 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 5276 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5277 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), |
Bas Nieuwenhuizen | de949a2 | 2020-08-24 23:27:26 +0200 | [diff] [blame] | 5278 | i+1), TranslateNonUniformDecoration(imageType.getQualifier())); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5279 | } |
| 5280 | return builder.createCompositeExtract(res, resultType(), 0); |
| 5281 | } |
| 5282 | #endif |
| 5283 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5284 | // projective component (might not to move) |
| 5285 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 5286 | // are divided by the last component of P." |
| 5287 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 5288 | // unused components will appear after all used components." |
| 5289 | if (cracked.proj) { |
| 5290 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 5291 | int projTargetComp; |
| 5292 | switch (sampler.dim) { |
| 5293 | case glslang::Esd1D: projTargetComp = 1; break; |
| 5294 | case glslang::Esd2D: projTargetComp = 2; break; |
| 5295 | case glslang::EsdRect: projTargetComp = 2; break; |
| 5296 | default: projTargetComp = projSourceComp; break; |
| 5297 | } |
| 5298 | // copy the projective coordinate if we have to |
| 5299 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 5300 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5301 | builder.getScalarTypeId(builder.getTypeId(params.coords)), projSourceComp); |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5302 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5303 | builder.getTypeId(params.coords), projTargetComp); |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5304 | } |
| 5305 | } |
| 5306 | |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 5307 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5308 | // nonprivate |
| 5309 | if (imageType.getQualifier().nonprivate) { |
| 5310 | params.nonprivate = true; |
| 5311 | } |
| 5312 | |
| 5313 | // volatile |
| 5314 | if (imageType.getQualifier().volatil) { |
| 5315 | params.volatil = true; |
| 5316 | } |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 5317 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5318 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 5319 | std::vector<spv::Id> result( 1, |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5320 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
| 5321 | noImplicitLod, params, signExtensionMask()) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 5322 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 5323 | |
| 5324 | if (components != node->getType().getVectorSize()) |
| 5325 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 5326 | |
| 5327 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 5331 | { |
| 5332 | // Grab the function's pointer from the previously created function |
| 5333 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 5334 | if (! function) |
| 5335 | return 0; |
| 5336 | |
| 5337 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 5338 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 5339 | |
| 5340 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 5341 | // |
| 5342 | // These imply we need a four step process: |
| 5343 | // 1. Evaluate the arguments |
| 5344 | // 2. Allocate and make copies of in, out, and inout arguments |
| 5345 | // 3. Make the call |
| 5346 | // 4. Copy back the results |
| 5347 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5348 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5349 | std::vector<spv::Builder::AccessChain> lValues; |
| 5350 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5351 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5352 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5353 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5354 | // build l-value |
| 5355 | builder.clearAccessChain(); |
| 5356 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5357 | // 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] | 5358 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 5359 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5360 | // save l-value |
| 5361 | lValues.push_back(builder.getAccessChain()); |
| 5362 | } else { |
| 5363 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5364 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5365 | } |
| 5366 | } |
| 5367 | |
| 5368 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 5369 | // copy the original into that space. |
| 5370 | // |
| 5371 | // Also, build up the list of actual arguments to pass in for the call |
| 5372 | int lValueCount = 0; |
| 5373 | int rValueCount = 0; |
| 5374 | std::vector<spv::Id> spvArgs; |
| 5375 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 5376 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5377 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 5378 | builder.setAccessChain(lValues[lValueCount]); |
| 5379 | arg = builder.accessChainGetLValue(); |
| 5380 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5381 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5382 | // need space to hold the copy |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5383 | arg = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5384 | builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5385 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 5386 | // need to copy the input into output space |
| 5387 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5388 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 5389 | builder.clearAccessChain(); |
| 5390 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5391 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5392 | } |
| 5393 | ++lValueCount; |
| 5394 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5395 | // process r-value, which involves a copy for a type mismatch |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 5396 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a]) || |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5397 | TranslatePrecisionDecoration(*argTypes[a]) != function->getParamPrecision(a)) |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 5398 | { |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5399 | spv::Id argCopy = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, function->getParamType(a), "arg"); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5400 | builder.clearAccessChain(); |
| 5401 | builder.setAccessChainLValue(argCopy); |
| 5402 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5403 | arg = builder.createLoad(argCopy, function->getParamPrecision(a)); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5404 | } else |
| 5405 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5406 | ++rValueCount; |
| 5407 | } |
| 5408 | spvArgs.push_back(arg); |
| 5409 | } |
| 5410 | |
| 5411 | // 3. Make the call. |
| 5412 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5413 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5414 | |
| 5415 | // 4. Copy back out an "out" arguments. |
| 5416 | lValueCount = 0; |
| 5417 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5418 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5419 | ++lValueCount; |
| 5420 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5421 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5422 | spv::Id copy = builder.createLoad(spvArgs[a], spv::NoPrecision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5423 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5424 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5425 | } |
| 5426 | ++lValueCount; |
| 5427 | } |
| 5428 | } |
| 5429 | |
| 5430 | return result; |
| 5431 | } |
| 5432 | |
| 5433 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5434 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5435 | spv::Id typeId, spv::Id left, spv::Id right, |
| 5436 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 5437 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5438 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5439 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5440 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5441 | |
| 5442 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5443 | 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] | 5444 | bool comparison = false; |
| 5445 | |
| 5446 | switch (op) { |
| 5447 | case glslang::EOpAdd: |
| 5448 | case glslang::EOpAddAssign: |
| 5449 | if (isFloat) |
| 5450 | binOp = spv::OpFAdd; |
| 5451 | else |
| 5452 | binOp = spv::OpIAdd; |
| 5453 | break; |
| 5454 | case glslang::EOpSub: |
| 5455 | case glslang::EOpSubAssign: |
| 5456 | if (isFloat) |
| 5457 | binOp = spv::OpFSub; |
| 5458 | else |
| 5459 | binOp = spv::OpISub; |
| 5460 | break; |
| 5461 | case glslang::EOpMul: |
| 5462 | case glslang::EOpMulAssign: |
| 5463 | if (isFloat) |
| 5464 | binOp = spv::OpFMul; |
| 5465 | else |
| 5466 | binOp = spv::OpIMul; |
| 5467 | break; |
| 5468 | case glslang::EOpVectorTimesScalar: |
| 5469 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5470 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5471 | if (builder.isVector(right)) |
| 5472 | std::swap(left, right); |
| 5473 | assert(builder.isScalar(right)); |
| 5474 | needMatchingVectors = false; |
| 5475 | binOp = spv::OpVectorTimesScalar; |
t.jung | 697fdf0 | 2018-11-14 13:04:39 +0100 | [diff] [blame] | 5476 | } else if (isFloat) |
| 5477 | binOp = spv::OpFMul; |
| 5478 | else |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5479 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5480 | break; |
| 5481 | case glslang::EOpVectorTimesMatrix: |
| 5482 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5483 | binOp = spv::OpVectorTimesMatrix; |
| 5484 | break; |
| 5485 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5486 | binOp = spv::OpMatrixTimesVector; |
| 5487 | break; |
| 5488 | case glslang::EOpMatrixTimesScalar: |
| 5489 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5490 | binOp = spv::OpMatrixTimesScalar; |
| 5491 | break; |
| 5492 | case glslang::EOpMatrixTimesMatrix: |
| 5493 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5494 | binOp = spv::OpMatrixTimesMatrix; |
| 5495 | break; |
| 5496 | case glslang::EOpOuterProduct: |
| 5497 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5498 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5499 | break; |
| 5500 | |
| 5501 | case glslang::EOpDiv: |
| 5502 | case glslang::EOpDivAssign: |
| 5503 | if (isFloat) |
| 5504 | binOp = spv::OpFDiv; |
| 5505 | else if (isUnsigned) |
| 5506 | binOp = spv::OpUDiv; |
| 5507 | else |
| 5508 | binOp = spv::OpSDiv; |
| 5509 | break; |
| 5510 | case glslang::EOpMod: |
| 5511 | case glslang::EOpModAssign: |
| 5512 | if (isFloat) |
| 5513 | binOp = spv::OpFMod; |
| 5514 | else if (isUnsigned) |
| 5515 | binOp = spv::OpUMod; |
| 5516 | else |
| 5517 | binOp = spv::OpSMod; |
| 5518 | break; |
| 5519 | case glslang::EOpRightShift: |
| 5520 | case glslang::EOpRightShiftAssign: |
| 5521 | if (isUnsigned) |
| 5522 | binOp = spv::OpShiftRightLogical; |
| 5523 | else |
| 5524 | binOp = spv::OpShiftRightArithmetic; |
| 5525 | break; |
| 5526 | case glslang::EOpLeftShift: |
| 5527 | case glslang::EOpLeftShiftAssign: |
| 5528 | binOp = spv::OpShiftLeftLogical; |
| 5529 | break; |
| 5530 | case glslang::EOpAnd: |
| 5531 | case glslang::EOpAndAssign: |
| 5532 | binOp = spv::OpBitwiseAnd; |
| 5533 | break; |
| 5534 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5535 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5536 | binOp = spv::OpLogicalAnd; |
| 5537 | break; |
| 5538 | case glslang::EOpInclusiveOr: |
| 5539 | case glslang::EOpInclusiveOrAssign: |
| 5540 | binOp = spv::OpBitwiseOr; |
| 5541 | break; |
| 5542 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5543 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5544 | binOp = spv::OpLogicalOr; |
| 5545 | break; |
| 5546 | case glslang::EOpExclusiveOr: |
| 5547 | case glslang::EOpExclusiveOrAssign: |
| 5548 | binOp = spv::OpBitwiseXor; |
| 5549 | break; |
| 5550 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5551 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5552 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5553 | break; |
| 5554 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 5555 | case glslang::EOpAbsDifference: |
| 5556 | binOp = isUnsigned ? spv::OpAbsUSubINTEL : spv::OpAbsISubINTEL; |
| 5557 | break; |
| 5558 | |
| 5559 | case glslang::EOpAddSaturate: |
| 5560 | binOp = isUnsigned ? spv::OpUAddSatINTEL : spv::OpIAddSatINTEL; |
| 5561 | break; |
| 5562 | |
| 5563 | case glslang::EOpSubSaturate: |
| 5564 | binOp = isUnsigned ? spv::OpUSubSatINTEL : spv::OpISubSatINTEL; |
| 5565 | break; |
| 5566 | |
| 5567 | case glslang::EOpAverage: |
| 5568 | binOp = isUnsigned ? spv::OpUAverageINTEL : spv::OpIAverageINTEL; |
| 5569 | break; |
| 5570 | |
| 5571 | case glslang::EOpAverageRounded: |
| 5572 | binOp = isUnsigned ? spv::OpUAverageRoundedINTEL : spv::OpIAverageRoundedINTEL; |
| 5573 | break; |
| 5574 | |
| 5575 | case glslang::EOpMul32x16: |
| 5576 | binOp = isUnsigned ? spv::OpUMul32x16INTEL : spv::OpIMul32x16INTEL; |
| 5577 | break; |
| 5578 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5579 | case glslang::EOpLessThan: |
| 5580 | case glslang::EOpGreaterThan: |
| 5581 | case glslang::EOpLessThanEqual: |
| 5582 | case glslang::EOpGreaterThanEqual: |
| 5583 | case glslang::EOpEqual: |
| 5584 | case glslang::EOpNotEqual: |
| 5585 | case glslang::EOpVectorEqual: |
| 5586 | case glslang::EOpVectorNotEqual: |
| 5587 | comparison = true; |
| 5588 | break; |
| 5589 | default: |
| 5590 | break; |
| 5591 | } |
| 5592 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5593 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5594 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5595 | assert(comparison == false); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5596 | if (builder.isMatrix(left) || builder.isMatrix(right) || |
| 5597 | builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5598 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5599 | |
| 5600 | // 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] | 5601 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5602 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5603 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5604 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5605 | decorations.addNoContraction(builder, result); |
| 5606 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5607 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | if (! comparison) |
| 5611 | return 0; |
| 5612 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5613 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5614 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 5615 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5616 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 5617 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5618 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5619 | return result; |
| 5620 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5621 | |
| 5622 | switch (op) { |
| 5623 | case glslang::EOpLessThan: |
| 5624 | if (isFloat) |
| 5625 | binOp = spv::OpFOrdLessThan; |
| 5626 | else if (isUnsigned) |
| 5627 | binOp = spv::OpULessThan; |
| 5628 | else |
| 5629 | binOp = spv::OpSLessThan; |
| 5630 | break; |
| 5631 | case glslang::EOpGreaterThan: |
| 5632 | if (isFloat) |
| 5633 | binOp = spv::OpFOrdGreaterThan; |
| 5634 | else if (isUnsigned) |
| 5635 | binOp = spv::OpUGreaterThan; |
| 5636 | else |
| 5637 | binOp = spv::OpSGreaterThan; |
| 5638 | break; |
| 5639 | case glslang::EOpLessThanEqual: |
| 5640 | if (isFloat) |
| 5641 | binOp = spv::OpFOrdLessThanEqual; |
| 5642 | else if (isUnsigned) |
| 5643 | binOp = spv::OpULessThanEqual; |
| 5644 | else |
| 5645 | binOp = spv::OpSLessThanEqual; |
| 5646 | break; |
| 5647 | case glslang::EOpGreaterThanEqual: |
| 5648 | if (isFloat) |
| 5649 | binOp = spv::OpFOrdGreaterThanEqual; |
| 5650 | else if (isUnsigned) |
| 5651 | binOp = spv::OpUGreaterThanEqual; |
| 5652 | else |
| 5653 | binOp = spv::OpSGreaterThanEqual; |
| 5654 | break; |
| 5655 | case glslang::EOpEqual: |
| 5656 | case glslang::EOpVectorEqual: |
| 5657 | if (isFloat) |
| 5658 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5659 | else if (isBool) |
| 5660 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5661 | else |
| 5662 | binOp = spv::OpIEqual; |
| 5663 | break; |
| 5664 | case glslang::EOpNotEqual: |
| 5665 | case glslang::EOpVectorNotEqual: |
| 5666 | if (isFloat) |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 5667 | binOp = spv::OpFUnordNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5668 | else if (isBool) |
| 5669 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5670 | else |
| 5671 | binOp = spv::OpINotEqual; |
| 5672 | break; |
| 5673 | default: |
| 5674 | break; |
| 5675 | } |
| 5676 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5677 | if (binOp != spv::OpNop) { |
| 5678 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5679 | decorations.addNoContraction(builder, result); |
| 5680 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5681 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5682 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5683 | |
| 5684 | return 0; |
| 5685 | } |
| 5686 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5687 | // |
| 5688 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 5689 | // These can be any of: |
| 5690 | // |
| 5691 | // matrix * scalar |
| 5692 | // scalar * matrix |
| 5693 | // matrix * matrix linear algebraic |
| 5694 | // matrix * vector |
| 5695 | // vector * matrix |
| 5696 | // matrix * matrix componentwise |
| 5697 | // matrix op matrix op in {+, -, /} |
| 5698 | // matrix op scalar op in {+, -, /} |
| 5699 | // scalar op matrix op in {+, -, /} |
| 5700 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5701 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5702 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5703 | { |
| 5704 | bool firstClass = true; |
| 5705 | |
| 5706 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 5707 | switch (op) { |
| 5708 | case spv::OpFDiv: |
| 5709 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 5710 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 5711 | spv::Id resultType = builder.getTypeId(right); |
| 5712 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5713 | op = spv::OpMatrixTimesScalar; |
| 5714 | } else |
| 5715 | firstClass = false; |
| 5716 | break; |
| 5717 | case spv::OpMatrixTimesScalar: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5718 | if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5719 | std::swap(left, right); |
| 5720 | assert(builder.isScalar(right)); |
| 5721 | break; |
| 5722 | case spv::OpVectorTimesMatrix: |
| 5723 | assert(builder.isVector(left)); |
| 5724 | assert(builder.isMatrix(right)); |
| 5725 | break; |
| 5726 | case spv::OpMatrixTimesVector: |
| 5727 | assert(builder.isMatrix(left)); |
| 5728 | assert(builder.isVector(right)); |
| 5729 | break; |
| 5730 | case spv::OpMatrixTimesMatrix: |
| 5731 | assert(builder.isMatrix(left)); |
| 5732 | assert(builder.isMatrix(right)); |
| 5733 | break; |
| 5734 | default: |
| 5735 | firstClass = false; |
| 5736 | break; |
| 5737 | } |
| 5738 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5739 | if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 5740 | firstClass = true; |
| 5741 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5742 | if (firstClass) { |
| 5743 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5744 | decorations.addNoContraction(builder, result); |
| 5745 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5746 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5747 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5748 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5749 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5750 | // The result type of all of them is the same type as the (a) matrix operand. |
| 5751 | // The algorithm is to: |
| 5752 | // - break the matrix(es) into vectors |
| 5753 | // - smear any scalar to a vector |
| 5754 | // - do vector operations |
| 5755 | // - make a matrix out the vector results |
| 5756 | switch (op) { |
| 5757 | case spv::OpFAdd: |
| 5758 | case spv::OpFSub: |
| 5759 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5760 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5761 | case spv::OpFMul: |
| 5762 | { |
| 5763 | // one time set up... |
| 5764 | bool leftMat = builder.isMatrix(left); |
| 5765 | bool rightMat = builder.isMatrix(right); |
| 5766 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 5767 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 5768 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 5769 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 5770 | std::vector<spv::Id> results; |
| 5771 | spv::Id smearVec = spv::NoResult; |
| 5772 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5773 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5774 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5775 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5776 | |
| 5777 | // do each vector op |
| 5778 | for (unsigned int c = 0; c < numCols; ++c) { |
| 5779 | std::vector<unsigned int> indexes; |
| 5780 | indexes.push_back(c); |
| 5781 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 5782 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5783 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5784 | decorations.addNoContraction(builder, result); |
| 5785 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5786 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5790 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5791 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5792 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5793 | } |
| 5794 | default: |
| 5795 | assert(0); |
| 5796 | return spv::NoResult; |
| 5797 | } |
| 5798 | } |
| 5799 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5800 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5801 | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5802 | { |
| 5803 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5804 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5805 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5806 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5807 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5808 | |
| 5809 | switch (op) { |
| 5810 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5811 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5812 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5813 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5814 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5815 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5816 | unaryOp = spv::OpSNegate; |
| 5817 | break; |
| 5818 | |
| 5819 | case glslang::EOpLogicalNot: |
| 5820 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5821 | unaryOp = spv::OpLogicalNot; |
| 5822 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5823 | case glslang::EOpBitwiseNot: |
| 5824 | unaryOp = spv::OpNot; |
| 5825 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5826 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5827 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5828 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5829 | break; |
| 5830 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5831 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5832 | break; |
| 5833 | case glslang::EOpTranspose: |
| 5834 | unaryOp = spv::OpTranspose; |
| 5835 | break; |
| 5836 | |
| 5837 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5838 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5839 | break; |
| 5840 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5841 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5842 | break; |
| 5843 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5844 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5845 | break; |
| 5846 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5847 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5848 | break; |
| 5849 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5850 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5851 | break; |
| 5852 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5853 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5854 | break; |
| 5855 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5856 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5857 | break; |
| 5858 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5859 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5860 | break; |
| 5861 | |
| 5862 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5863 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5864 | break; |
| 5865 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5866 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5867 | break; |
| 5868 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5869 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5870 | break; |
| 5871 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5872 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5873 | break; |
| 5874 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5875 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5876 | break; |
| 5877 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5878 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5879 | break; |
| 5880 | |
| 5881 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5882 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5883 | break; |
| 5884 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5885 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5886 | break; |
| 5887 | |
| 5888 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5889 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5890 | break; |
| 5891 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5892 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5893 | break; |
| 5894 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5895 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5896 | break; |
| 5897 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5898 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5899 | break; |
| 5900 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5901 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5902 | break; |
| 5903 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5904 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5905 | break; |
| 5906 | |
| 5907 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5908 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5909 | break; |
| 5910 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5911 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5912 | break; |
| 5913 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5914 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5915 | break; |
| 5916 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5917 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5918 | break; |
| 5919 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5920 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5921 | break; |
| 5922 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5923 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5924 | break; |
| 5925 | |
| 5926 | case glslang::EOpIsNan: |
| 5927 | unaryOp = spv::OpIsNan; |
| 5928 | break; |
| 5929 | case glslang::EOpIsInf: |
| 5930 | unaryOp = spv::OpIsInf; |
| 5931 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5932 | case glslang::EOpIsFinite: |
| 5933 | unaryOp = spv::OpIsFinite; |
| 5934 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5935 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5936 | case glslang::EOpFloatBitsToInt: |
| 5937 | case glslang::EOpFloatBitsToUint: |
| 5938 | case glslang::EOpIntBitsToFloat: |
| 5939 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5940 | case glslang::EOpDoubleBitsToInt64: |
| 5941 | case glslang::EOpDoubleBitsToUint64: |
| 5942 | case glslang::EOpInt64BitsToDouble: |
| 5943 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5944 | case glslang::EOpFloat16BitsToInt16: |
| 5945 | case glslang::EOpFloat16BitsToUint16: |
| 5946 | case glslang::EOpInt16BitsToFloat16: |
| 5947 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5948 | unaryOp = spv::OpBitcast; |
| 5949 | break; |
| 5950 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5951 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5952 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5953 | break; |
| 5954 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5955 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5956 | break; |
| 5957 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5958 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5959 | break; |
| 5960 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5961 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5962 | break; |
| 5963 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5964 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5965 | break; |
| 5966 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5967 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5968 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5969 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5970 | case glslang::EOpPackSnorm4x8: |
| 5971 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 5972 | break; |
| 5973 | case glslang::EOpUnpackSnorm4x8: |
| 5974 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5975 | break; |
| 5976 | case glslang::EOpPackUnorm4x8: |
| 5977 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 5978 | break; |
| 5979 | case glslang::EOpUnpackUnorm4x8: |
| 5980 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5981 | break; |
| 5982 | case glslang::EOpPackDouble2x32: |
| 5983 | libCall = spv::GLSLstd450PackDouble2x32; |
| 5984 | break; |
| 5985 | case glslang::EOpUnpackDouble2x32: |
| 5986 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5987 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5988 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5989 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5990 | case glslang::EOpPackInt2x32: |
| 5991 | case glslang::EOpUnpackInt2x32: |
| 5992 | case glslang::EOpPackUint2x32: |
| 5993 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5994 | case glslang::EOpPack16: |
| 5995 | case glslang::EOpPack32: |
| 5996 | case glslang::EOpPack64: |
| 5997 | case glslang::EOpUnpack32: |
| 5998 | case glslang::EOpUnpack16: |
| 5999 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6000 | case glslang::EOpPackInt2x16: |
| 6001 | case glslang::EOpUnpackInt2x16: |
| 6002 | case glslang::EOpPackUint2x16: |
| 6003 | case glslang::EOpUnpackUint2x16: |
| 6004 | case glslang::EOpPackInt4x16: |
| 6005 | case glslang::EOpUnpackInt4x16: |
| 6006 | case glslang::EOpPackUint4x16: |
| 6007 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6008 | case glslang::EOpPackFloat2x16: |
| 6009 | case glslang::EOpUnpackFloat2x16: |
| 6010 | unaryOp = spv::OpBitcast; |
| 6011 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6012 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6013 | case glslang::EOpDPdx: |
| 6014 | unaryOp = spv::OpDPdx; |
| 6015 | break; |
| 6016 | case glslang::EOpDPdy: |
| 6017 | unaryOp = spv::OpDPdy; |
| 6018 | break; |
| 6019 | case glslang::EOpFwidth: |
| 6020 | unaryOp = spv::OpFwidth; |
| 6021 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6022 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6023 | case glslang::EOpAny: |
| 6024 | unaryOp = spv::OpAny; |
| 6025 | break; |
| 6026 | case glslang::EOpAll: |
| 6027 | unaryOp = spv::OpAll; |
| 6028 | break; |
| 6029 | |
| 6030 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6031 | if (isFloat) |
| 6032 | libCall = spv::GLSLstd450FAbs; |
| 6033 | else |
| 6034 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6035 | break; |
| 6036 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6037 | if (isFloat) |
| 6038 | libCall = spv::GLSLstd450FSign; |
| 6039 | else |
| 6040 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6041 | break; |
| 6042 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6043 | #ifndef GLSLANG_WEB |
| 6044 | case glslang::EOpDPdxFine: |
| 6045 | unaryOp = spv::OpDPdxFine; |
| 6046 | break; |
| 6047 | case glslang::EOpDPdyFine: |
| 6048 | unaryOp = spv::OpDPdyFine; |
| 6049 | break; |
| 6050 | case glslang::EOpFwidthFine: |
| 6051 | unaryOp = spv::OpFwidthFine; |
| 6052 | break; |
| 6053 | case glslang::EOpDPdxCoarse: |
| 6054 | unaryOp = spv::OpDPdxCoarse; |
| 6055 | break; |
| 6056 | case glslang::EOpDPdyCoarse: |
| 6057 | unaryOp = spv::OpDPdyCoarse; |
| 6058 | break; |
| 6059 | case glslang::EOpFwidthCoarse: |
| 6060 | unaryOp = spv::OpFwidthCoarse; |
| 6061 | break; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 6062 | case glslang::EOpRayQueryProceed: |
| 6063 | unaryOp = spv::OpRayQueryProceedKHR; |
| 6064 | break; |
| 6065 | case glslang::EOpRayQueryGetRayTMin: |
| 6066 | unaryOp = spv::OpRayQueryGetRayTMinKHR; |
| 6067 | break; |
| 6068 | case glslang::EOpRayQueryGetRayFlags: |
| 6069 | unaryOp = spv::OpRayQueryGetRayFlagsKHR; |
| 6070 | break; |
| 6071 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 6072 | unaryOp = spv::OpRayQueryGetWorldRayOriginKHR; |
| 6073 | break; |
| 6074 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 6075 | unaryOp = spv::OpRayQueryGetWorldRayDirectionKHR; |
| 6076 | break; |
| 6077 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 6078 | unaryOp = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
| 6079 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6080 | case glslang::EOpInterpolateAtCentroid: |
| 6081 | if (typeProxy == glslang::EbtFloat16) |
| 6082 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6083 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 6084 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6085 | case glslang::EOpAtomicCounterIncrement: |
| 6086 | case glslang::EOpAtomicCounterDecrement: |
| 6087 | case glslang::EOpAtomicCounter: |
| 6088 | { |
| 6089 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 6090 | std::vector<spv::Id> operands; |
| 6091 | operands.push_back(operand); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6092 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6093 | } |
| 6094 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6095 | case glslang::EOpBitFieldReverse: |
| 6096 | unaryOp = spv::OpBitReverse; |
| 6097 | break; |
| 6098 | case glslang::EOpBitCount: |
| 6099 | unaryOp = spv::OpBitCount; |
| 6100 | break; |
| 6101 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6102 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6103 | break; |
| 6104 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6105 | if (isUnsigned) |
| 6106 | libCall = spv::GLSLstd450FindUMsb; |
| 6107 | else |
| 6108 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6109 | break; |
| 6110 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 6111 | case glslang::EOpCountLeadingZeros: |
| 6112 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 6113 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 6114 | unaryOp = spv::OpUCountLeadingZerosINTEL; |
| 6115 | break; |
| 6116 | |
| 6117 | case glslang::EOpCountTrailingZeros: |
| 6118 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 6119 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 6120 | unaryOp = spv::OpUCountTrailingZerosINTEL; |
| 6121 | break; |
| 6122 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6123 | case glslang::EOpBallot: |
| 6124 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6125 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6126 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6127 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6128 | case glslang::EOpMinInvocations: |
| 6129 | case glslang::EOpMaxInvocations: |
| 6130 | case glslang::EOpAddInvocations: |
| 6131 | case glslang::EOpMinInvocationsNonUniform: |
| 6132 | case glslang::EOpMaxInvocationsNonUniform: |
| 6133 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6134 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6135 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6136 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6137 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6138 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6139 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6140 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6141 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6142 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6143 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6144 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6145 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6146 | { |
| 6147 | std::vector<spv::Id> operands; |
| 6148 | operands.push_back(operand); |
| 6149 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 6150 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6151 | case glslang::EOpSubgroupAll: |
| 6152 | case glslang::EOpSubgroupAny: |
| 6153 | case glslang::EOpSubgroupAllEqual: |
| 6154 | case glslang::EOpSubgroupBroadcastFirst: |
| 6155 | case glslang::EOpSubgroupBallot: |
| 6156 | case glslang::EOpSubgroupInverseBallot: |
| 6157 | case glslang::EOpSubgroupBallotBitCount: |
| 6158 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6159 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6160 | case glslang::EOpSubgroupBallotFindLSB: |
| 6161 | case glslang::EOpSubgroupBallotFindMSB: |
| 6162 | case glslang::EOpSubgroupAdd: |
| 6163 | case glslang::EOpSubgroupMul: |
| 6164 | case glslang::EOpSubgroupMin: |
| 6165 | case glslang::EOpSubgroupMax: |
| 6166 | case glslang::EOpSubgroupAnd: |
| 6167 | case glslang::EOpSubgroupOr: |
| 6168 | case glslang::EOpSubgroupXor: |
| 6169 | case glslang::EOpSubgroupInclusiveAdd: |
| 6170 | case glslang::EOpSubgroupInclusiveMul: |
| 6171 | case glslang::EOpSubgroupInclusiveMin: |
| 6172 | case glslang::EOpSubgroupInclusiveMax: |
| 6173 | case glslang::EOpSubgroupInclusiveAnd: |
| 6174 | case glslang::EOpSubgroupInclusiveOr: |
| 6175 | case glslang::EOpSubgroupInclusiveXor: |
| 6176 | case glslang::EOpSubgroupExclusiveAdd: |
| 6177 | case glslang::EOpSubgroupExclusiveMul: |
| 6178 | case glslang::EOpSubgroupExclusiveMin: |
| 6179 | case glslang::EOpSubgroupExclusiveMax: |
| 6180 | case glslang::EOpSubgroupExclusiveAnd: |
| 6181 | case glslang::EOpSubgroupExclusiveOr: |
| 6182 | case glslang::EOpSubgroupExclusiveXor: |
| 6183 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6184 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6185 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 6186 | std::vector<spv::Id> operands; |
| 6187 | operands.push_back(operand); |
| 6188 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 6189 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6190 | case glslang::EOpMbcnt: |
| 6191 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6192 | libCall = spv::MbcntAMD; |
| 6193 | break; |
| 6194 | |
| 6195 | case glslang::EOpCubeFaceIndex: |
| 6196 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 6197 | libCall = spv::CubeFaceIndexAMD; |
| 6198 | break; |
| 6199 | |
| 6200 | case glslang::EOpCubeFaceCoord: |
| 6201 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 6202 | libCall = spv::CubeFaceCoordAMD; |
| 6203 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6204 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6205 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 6206 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6207 | case glslang::EOpConstructReference: |
| 6208 | unaryOp = spv::OpBitcast; |
| 6209 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6210 | #endif |
Jeff Bolz | 88220d5 | 2019-05-08 10:24:46 -0500 | [diff] [blame] | 6211 | |
| 6212 | case glslang::EOpCopyObject: |
| 6213 | unaryOp = spv::OpCopyObject; |
| 6214 | break; |
| 6215 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6216 | default: |
| 6217 | return 0; |
| 6218 | } |
| 6219 | |
| 6220 | spv::Id id; |
| 6221 | if (libCall >= 0) { |
| 6222 | std::vector<spv::Id> args; |
| 6223 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6224 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6225 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6226 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6227 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6228 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6229 | decorations.addNoContraction(builder, id); |
| 6230 | decorations.addNonUniform(builder, id); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6231 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6232 | } |
| 6233 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6234 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6235 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 6236 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6237 | { |
| 6238 | // Handle unary operations vector by vector. |
| 6239 | // The result type is the same type as the original type. |
| 6240 | // The algorithm is to: |
| 6241 | // - break the matrix into vectors |
| 6242 | // - apply the operation to each vector |
| 6243 | // - make a matrix out the vector results |
| 6244 | |
| 6245 | // get the types sorted out |
| 6246 | int numCols = builder.getNumColumns(operand); |
| 6247 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 6248 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 6249 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6250 | std::vector<spv::Id> results; |
| 6251 | |
| 6252 | // do each vector op |
| 6253 | for (int c = 0; c < numCols; ++c) { |
| 6254 | std::vector<unsigned int> indexes; |
| 6255 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 6256 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 6257 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6258 | decorations.addNoContraction(builder, destVec); |
| 6259 | decorations.addNonUniform(builder, destVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6260 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6261 | } |
| 6262 | |
| 6263 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6264 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6265 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6266 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6267 | } |
| 6268 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6269 | // For converting integers where both the bitwidth and the signedness could |
| 6270 | // change, but only do the width change here. The caller is still responsible |
| 6271 | // for the signedness conversion. |
| 6272 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6273 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6274 | // Get the result type width, based on the type to convert to. |
| 6275 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6276 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6277 | case glslang::EOpConvInt16ToUint8: |
| 6278 | case glslang::EOpConvIntToUint8: |
| 6279 | case glslang::EOpConvInt64ToUint8: |
| 6280 | case glslang::EOpConvUint16ToInt8: |
| 6281 | case glslang::EOpConvUintToInt8: |
| 6282 | case glslang::EOpConvUint64ToInt8: |
| 6283 | width = 8; |
| 6284 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6285 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6286 | case glslang::EOpConvIntToUint16: |
| 6287 | case glslang::EOpConvInt64ToUint16: |
| 6288 | case glslang::EOpConvUint8ToInt16: |
| 6289 | case glslang::EOpConvUintToInt16: |
| 6290 | case glslang::EOpConvUint64ToInt16: |
| 6291 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6292 | break; |
| 6293 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6294 | case glslang::EOpConvInt16ToUint: |
| 6295 | case glslang::EOpConvInt64ToUint: |
| 6296 | case glslang::EOpConvUint8ToInt: |
| 6297 | case glslang::EOpConvUint16ToInt: |
| 6298 | case glslang::EOpConvUint64ToInt: |
| 6299 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6300 | break; |
| 6301 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6302 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6303 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6304 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6305 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6306 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6307 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6308 | break; |
| 6309 | |
| 6310 | default: |
| 6311 | assert(false && "Default missing"); |
| 6312 | break; |
| 6313 | } |
| 6314 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6315 | // Get the conversion operation and result type, |
| 6316 | // based on the target width, but the source type. |
| 6317 | spv::Id type = spv::NoType; |
| 6318 | spv::Op convOp = spv::OpNop; |
| 6319 | switch(op) { |
| 6320 | case glslang::EOpConvInt8ToUint16: |
| 6321 | case glslang::EOpConvInt8ToUint: |
| 6322 | case glslang::EOpConvInt8ToUint64: |
| 6323 | case glslang::EOpConvInt16ToUint8: |
| 6324 | case glslang::EOpConvInt16ToUint: |
| 6325 | case glslang::EOpConvInt16ToUint64: |
| 6326 | case glslang::EOpConvIntToUint8: |
| 6327 | case glslang::EOpConvIntToUint16: |
| 6328 | case glslang::EOpConvIntToUint64: |
| 6329 | case glslang::EOpConvInt64ToUint8: |
| 6330 | case glslang::EOpConvInt64ToUint16: |
| 6331 | case glslang::EOpConvInt64ToUint: |
| 6332 | convOp = spv::OpSConvert; |
| 6333 | type = builder.makeIntType(width); |
| 6334 | break; |
| 6335 | default: |
| 6336 | convOp = spv::OpUConvert; |
| 6337 | type = builder.makeUintType(width); |
| 6338 | break; |
| 6339 | } |
| 6340 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6341 | if (vectorSize > 0) |
| 6342 | type = builder.makeVectorType(type, vectorSize); |
| 6343 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6344 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6345 | } |
| 6346 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6347 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 6348 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6349 | { |
| 6350 | spv::Op convOp = spv::OpNop; |
| 6351 | spv::Id zero = 0; |
| 6352 | spv::Id one = 0; |
| 6353 | |
| 6354 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 6355 | |
| 6356 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6357 | case glslang::EOpConvIntToBool: |
| 6358 | case glslang::EOpConvUintToBool: |
| 6359 | zero = builder.makeUintConstant(0); |
| 6360 | zero = makeSmearedConstant(zero, vectorSize); |
| 6361 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6362 | case glslang::EOpConvFloatToBool: |
| 6363 | zero = builder.makeFloatConstant(0.0F); |
| 6364 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6365 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6366 | case glslang::EOpConvBoolToFloat: |
| 6367 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6368 | zero = builder.makeFloatConstant(0.0F); |
| 6369 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6370 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6371 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6372 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6373 | case glslang::EOpConvBoolToInt64: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6374 | #ifndef GLSLANG_WEB |
| 6375 | if (op == glslang::EOpConvBoolToInt64) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6376 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6377 | one = builder.makeInt64Constant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6378 | } else |
| 6379 | #endif |
| 6380 | { |
| 6381 | zero = builder.makeIntConstant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6382 | one = builder.makeIntConstant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6383 | } |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6384 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6385 | convOp = spv::OpSelect; |
| 6386 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6387 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6388 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6389 | case glslang::EOpConvBoolToUint64: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6390 | #ifndef GLSLANG_WEB |
| 6391 | if (op == glslang::EOpConvBoolToUint64) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6392 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6393 | one = builder.makeUint64Constant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6394 | } else |
| 6395 | #endif |
| 6396 | { |
| 6397 | zero = builder.makeUintConstant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6398 | one = builder.makeUintConstant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6399 | } |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6400 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6401 | convOp = spv::OpSelect; |
| 6402 | break; |
| 6403 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6404 | case glslang::EOpConvInt8ToFloat16: |
| 6405 | case glslang::EOpConvInt8ToFloat: |
| 6406 | case glslang::EOpConvInt8ToDouble: |
| 6407 | case glslang::EOpConvInt16ToFloat16: |
| 6408 | case glslang::EOpConvInt16ToFloat: |
| 6409 | case glslang::EOpConvInt16ToDouble: |
| 6410 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6411 | case glslang::EOpConvIntToFloat: |
| 6412 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6413 | case glslang::EOpConvInt64ToFloat: |
| 6414 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6415 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6416 | convOp = spv::OpConvertSToF; |
| 6417 | break; |
| 6418 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6419 | case glslang::EOpConvUint8ToFloat16: |
| 6420 | case glslang::EOpConvUint8ToFloat: |
| 6421 | case glslang::EOpConvUint8ToDouble: |
| 6422 | case glslang::EOpConvUint16ToFloat16: |
| 6423 | case glslang::EOpConvUint16ToFloat: |
| 6424 | case glslang::EOpConvUint16ToDouble: |
| 6425 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6426 | case glslang::EOpConvUintToFloat: |
| 6427 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6428 | case glslang::EOpConvUint64ToFloat: |
| 6429 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6430 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6431 | convOp = spv::OpConvertUToF; |
| 6432 | break; |
| 6433 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6434 | case glslang::EOpConvFloat16ToInt8: |
| 6435 | case glslang::EOpConvFloatToInt8: |
| 6436 | case glslang::EOpConvDoubleToInt8: |
| 6437 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6438 | case glslang::EOpConvFloatToInt16: |
| 6439 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6440 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6441 | case glslang::EOpConvFloatToInt: |
| 6442 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6443 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6444 | case glslang::EOpConvFloatToInt64: |
| 6445 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6446 | convOp = spv::OpConvertFToS; |
| 6447 | break; |
| 6448 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6449 | case glslang::EOpConvUint8ToInt8: |
| 6450 | case glslang::EOpConvInt8ToUint8: |
| 6451 | case glslang::EOpConvUint16ToInt16: |
| 6452 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6453 | case glslang::EOpConvUintToInt: |
| 6454 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6455 | case glslang::EOpConvUint64ToInt64: |
| 6456 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6457 | if (builder.isInSpecConstCodeGenMode()) { |
| 6458 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6459 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6460 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 6461 | zero = builder.makeUint8Constant(0); |
| 6462 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6463 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6464 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 6465 | zero = builder.makeUint64Constant(0); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6466 | } else |
| 6467 | #endif |
| 6468 | { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6469 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6470 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 6471 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6472 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6473 | // generating for OpSpecConstantOp instruction. |
| 6474 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6475 | } |
| 6476 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6477 | convOp = spv::OpBitcast; |
| 6478 | break; |
| 6479 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6480 | case glslang::EOpConvFloat16ToUint8: |
| 6481 | case glslang::EOpConvFloatToUint8: |
| 6482 | case glslang::EOpConvDoubleToUint8: |
| 6483 | case glslang::EOpConvFloat16ToUint16: |
| 6484 | case glslang::EOpConvFloatToUint16: |
| 6485 | case glslang::EOpConvDoubleToUint16: |
| 6486 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6487 | case glslang::EOpConvFloatToUint: |
| 6488 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6489 | case glslang::EOpConvFloatToUint64: |
| 6490 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6491 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6492 | convOp = spv::OpConvertFToU; |
| 6493 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6494 | |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6495 | #ifndef GLSLANG_WEB |
| 6496 | case glslang::EOpConvInt8ToBool: |
| 6497 | case glslang::EOpConvUint8ToBool: |
| 6498 | zero = builder.makeUint8Constant(0); |
| 6499 | zero = makeSmearedConstant(zero, vectorSize); |
| 6500 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6501 | case glslang::EOpConvInt16ToBool: |
| 6502 | case glslang::EOpConvUint16ToBool: |
| 6503 | zero = builder.makeUint16Constant(0); |
| 6504 | zero = makeSmearedConstant(zero, vectorSize); |
| 6505 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6506 | case glslang::EOpConvInt64ToBool: |
| 6507 | case glslang::EOpConvUint64ToBool: |
| 6508 | zero = builder.makeUint64Constant(0); |
| 6509 | zero = makeSmearedConstant(zero, vectorSize); |
| 6510 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6511 | case glslang::EOpConvDoubleToBool: |
| 6512 | zero = builder.makeDoubleConstant(0.0); |
| 6513 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6514 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6515 | case glslang::EOpConvFloat16ToBool: |
| 6516 | zero = builder.makeFloat16Constant(0.0F); |
| 6517 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6518 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6519 | case glslang::EOpConvBoolToDouble: |
| 6520 | convOp = spv::OpSelect; |
| 6521 | zero = builder.makeDoubleConstant(0.0); |
| 6522 | one = builder.makeDoubleConstant(1.0); |
| 6523 | break; |
| 6524 | case glslang::EOpConvBoolToFloat16: |
| 6525 | convOp = spv::OpSelect; |
| 6526 | zero = builder.makeFloat16Constant(0.0F); |
| 6527 | one = builder.makeFloat16Constant(1.0F); |
| 6528 | break; |
| 6529 | case glslang::EOpConvBoolToInt8: |
| 6530 | zero = builder.makeInt8Constant(0); |
| 6531 | one = builder.makeInt8Constant(1); |
| 6532 | convOp = spv::OpSelect; |
| 6533 | break; |
| 6534 | case glslang::EOpConvBoolToUint8: |
| 6535 | zero = builder.makeUint8Constant(0); |
| 6536 | one = builder.makeUint8Constant(1); |
| 6537 | convOp = spv::OpSelect; |
| 6538 | break; |
| 6539 | case glslang::EOpConvBoolToInt16: |
| 6540 | zero = builder.makeInt16Constant(0); |
| 6541 | one = builder.makeInt16Constant(1); |
| 6542 | convOp = spv::OpSelect; |
| 6543 | break; |
| 6544 | case glslang::EOpConvBoolToUint16: |
| 6545 | zero = builder.makeUint16Constant(0); |
| 6546 | one = builder.makeUint16Constant(1); |
| 6547 | convOp = spv::OpSelect; |
| 6548 | break; |
| 6549 | case glslang::EOpConvDoubleToFloat: |
| 6550 | case glslang::EOpConvFloatToDouble: |
| 6551 | case glslang::EOpConvDoubleToFloat16: |
| 6552 | case glslang::EOpConvFloat16ToDouble: |
| 6553 | case glslang::EOpConvFloatToFloat16: |
| 6554 | case glslang::EOpConvFloat16ToFloat: |
| 6555 | convOp = spv::OpFConvert; |
| 6556 | if (builder.isMatrixType(destType)) |
| 6557 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
| 6558 | break; |
| 6559 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6560 | case glslang::EOpConvInt8ToInt16: |
| 6561 | case glslang::EOpConvInt8ToInt: |
| 6562 | case glslang::EOpConvInt8ToInt64: |
| 6563 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6564 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6565 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6566 | case glslang::EOpConvIntToInt8: |
| 6567 | case glslang::EOpConvIntToInt16: |
| 6568 | case glslang::EOpConvIntToInt64: |
| 6569 | case glslang::EOpConvInt64ToInt8: |
| 6570 | case glslang::EOpConvInt64ToInt16: |
| 6571 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6572 | convOp = spv::OpSConvert; |
| 6573 | break; |
| 6574 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6575 | case glslang::EOpConvUint8ToUint16: |
| 6576 | case glslang::EOpConvUint8ToUint: |
| 6577 | case glslang::EOpConvUint8ToUint64: |
| 6578 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6579 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6580 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6581 | case glslang::EOpConvUintToUint8: |
| 6582 | case glslang::EOpConvUintToUint16: |
| 6583 | case glslang::EOpConvUintToUint64: |
| 6584 | case glslang::EOpConvUint64ToUint8: |
| 6585 | case glslang::EOpConvUint64ToUint16: |
| 6586 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6587 | convOp = spv::OpUConvert; |
| 6588 | break; |
| 6589 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6590 | case glslang::EOpConvInt8ToUint16: |
| 6591 | case glslang::EOpConvInt8ToUint: |
| 6592 | case glslang::EOpConvInt8ToUint64: |
| 6593 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6594 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6595 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6596 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6597 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6598 | case glslang::EOpConvIntToUint64: |
| 6599 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6600 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6601 | case glslang::EOpConvInt64ToUint: |
| 6602 | case glslang::EOpConvUint8ToInt16: |
| 6603 | case glslang::EOpConvUint8ToInt: |
| 6604 | case glslang::EOpConvUint8ToInt64: |
| 6605 | case glslang::EOpConvUint16ToInt8: |
| 6606 | case glslang::EOpConvUint16ToInt: |
| 6607 | case glslang::EOpConvUint16ToInt64: |
| 6608 | case glslang::EOpConvUintToInt8: |
| 6609 | case glslang::EOpConvUintToInt16: |
| 6610 | case glslang::EOpConvUintToInt64: |
| 6611 | case glslang::EOpConvUint64ToInt8: |
| 6612 | case glslang::EOpConvUint64ToInt16: |
| 6613 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6614 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6615 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6616 | |
| 6617 | if (builder.isInSpecConstCodeGenMode()) { |
| 6618 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6619 | switch(op) { |
| 6620 | case glslang::EOpConvInt16ToUint8: |
| 6621 | case glslang::EOpConvIntToUint8: |
| 6622 | case glslang::EOpConvInt64ToUint8: |
| 6623 | case glslang::EOpConvUint16ToInt8: |
| 6624 | case glslang::EOpConvUintToInt8: |
| 6625 | case glslang::EOpConvUint64ToInt8: |
| 6626 | zero = builder.makeUint8Constant(0); |
| 6627 | break; |
| 6628 | case glslang::EOpConvInt8ToUint16: |
| 6629 | case glslang::EOpConvIntToUint16: |
| 6630 | case glslang::EOpConvInt64ToUint16: |
| 6631 | case glslang::EOpConvUint8ToInt16: |
| 6632 | case glslang::EOpConvUintToInt16: |
| 6633 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6634 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6635 | break; |
| 6636 | case glslang::EOpConvInt8ToUint: |
| 6637 | case glslang::EOpConvInt16ToUint: |
| 6638 | case glslang::EOpConvInt64ToUint: |
| 6639 | case glslang::EOpConvUint8ToInt: |
| 6640 | case glslang::EOpConvUint16ToInt: |
| 6641 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6642 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6643 | break; |
| 6644 | case glslang::EOpConvInt8ToUint64: |
| 6645 | case glslang::EOpConvInt16ToUint64: |
| 6646 | case glslang::EOpConvIntToUint64: |
| 6647 | case glslang::EOpConvUint8ToInt64: |
| 6648 | case glslang::EOpConvUint16ToInt64: |
| 6649 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6650 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6651 | break; |
| 6652 | default: |
| 6653 | assert(false && "Default missing"); |
| 6654 | break; |
| 6655 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6656 | zero = makeSmearedConstant(zero, vectorSize); |
| 6657 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6658 | // generating for OpSpecConstantOp instruction. |
| 6659 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6660 | } |
| 6661 | // For normal run-time conversion instruction, use OpBitcast. |
| 6662 | convOp = spv::OpBitcast; |
| 6663 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6664 | case glslang::EOpConvUint64ToPtr: |
| 6665 | convOp = spv::OpConvertUToPtr; |
| 6666 | break; |
| 6667 | case glslang::EOpConvPtrToUint64: |
| 6668 | convOp = spv::OpConvertPtrToU; |
| 6669 | break; |
John Kessenich | 90e402f | 2019-09-17 23:19:38 -0600 | [diff] [blame] | 6670 | case glslang::EOpConvPtrToUvec2: |
| 6671 | case glslang::EOpConvUvec2ToPtr: |
John Kessenich | ee8e9c1 | 2019-10-10 20:54:21 -0600 | [diff] [blame] | 6672 | if (builder.isVector(operand)) |
| 6673 | builder.promoteIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, |
| 6674 | spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5); |
John Kessenich | 90e402f | 2019-09-17 23:19:38 -0600 | [diff] [blame] | 6675 | convOp = spv::OpBitcast; |
| 6676 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6677 | #endif |
| 6678 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6679 | default: |
| 6680 | break; |
| 6681 | } |
| 6682 | |
| 6683 | spv::Id result = 0; |
| 6684 | if (convOp == spv::OpNop) |
| 6685 | return result; |
| 6686 | |
| 6687 | if (convOp == spv::OpSelect) { |
| 6688 | zero = makeSmearedConstant(zero, vectorSize); |
| 6689 | one = makeSmearedConstant(one, vectorSize); |
| 6690 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 6691 | } else |
| 6692 | result = builder.createUnaryOp(convOp, destType, operand); |
| 6693 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6694 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6695 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6696 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6697 | } |
| 6698 | |
| 6699 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 6700 | { |
| 6701 | if (vectorSize == 0) |
| 6702 | return constant; |
| 6703 | |
| 6704 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 6705 | std::vector<spv::Id> components; |
| 6706 | for (int c = 0; c < vectorSize; ++c) |
| 6707 | components.push_back(constant); |
| 6708 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 6709 | } |
| 6710 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6711 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6712 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, |
| 6713 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 6714 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6715 | { |
| 6716 | spv::Op opCode = spv::OpNop; |
| 6717 | |
| 6718 | switch (op) { |
| 6719 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6720 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6721 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6722 | opCode = spv::OpAtomicIAdd; |
Vikram Kushwaha | 79b9392 | 2020-07-19 15:45:01 -0700 | [diff] [blame] | 6723 | if (typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
| 6724 | opCode = spv::OpAtomicFAddEXT; |
| 6725 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_add); |
| 6726 | if (typeProxy == glslang::EbtFloat) |
| 6727 | builder.addCapability(spv::CapabilityAtomicFloat32AddEXT); |
| 6728 | else |
| 6729 | builder.addCapability(spv::CapabilityAtomicFloat64AddEXT); |
| 6730 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6731 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6732 | case glslang::EOpAtomicCounterSubtract: |
| 6733 | opCode = spv::OpAtomicISub; |
| 6734 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6735 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6736 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6737 | case glslang::EOpAtomicCounterMin: |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6738 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? |
| 6739 | spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6740 | break; |
| 6741 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6742 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6743 | case glslang::EOpAtomicCounterMax: |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6744 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? |
| 6745 | spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6746 | break; |
| 6747 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6748 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6749 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6750 | opCode = spv::OpAtomicAnd; |
| 6751 | break; |
| 6752 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6753 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6754 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6755 | opCode = spv::OpAtomicOr; |
| 6756 | break; |
| 6757 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6758 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6759 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6760 | opCode = spv::OpAtomicXor; |
| 6761 | break; |
| 6762 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6763 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6764 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6765 | opCode = spv::OpAtomicExchange; |
| 6766 | break; |
| 6767 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6768 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6769 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6770 | opCode = spv::OpAtomicCompareExchange; |
| 6771 | break; |
| 6772 | case glslang::EOpAtomicCounterIncrement: |
| 6773 | opCode = spv::OpAtomicIIncrement; |
| 6774 | break; |
| 6775 | case glslang::EOpAtomicCounterDecrement: |
| 6776 | opCode = spv::OpAtomicIDecrement; |
| 6777 | break; |
| 6778 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6779 | case glslang::EOpImageAtomicLoad: |
| 6780 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6781 | opCode = spv::OpAtomicLoad; |
| 6782 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6783 | case glslang::EOpAtomicStore: |
| 6784 | case glslang::EOpImageAtomicStore: |
| 6785 | opCode = spv::OpAtomicStore; |
| 6786 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6787 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6788 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6789 | break; |
| 6790 | } |
| 6791 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6792 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 6793 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 6794 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6795 | // Sort out the operands |
| 6796 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6797 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 6798 | // - compare-exchange swaps the value and comparator |
| 6799 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6800 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6801 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 6802 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 6803 | spv::Id scopeId; |
| 6804 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6805 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 6806 | } else { |
| 6807 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 6808 | } |
| 6809 | // semantics default to relaxed |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6810 | spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() && |
| 6811 | glslangIntermediate->usingVulkanMemoryModel() ? |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6812 | spv::MemorySemanticsVolatileMask : |
| 6813 | spv::MemorySemanticsMaskNone); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6814 | spv::Id semanticsId2 = semanticsId; |
| 6815 | |
| 6816 | pointerId = operands[0]; |
| 6817 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 6818 | // no additional operands |
| 6819 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 6820 | compareId = operands[1]; |
| 6821 | valueId = operands[2]; |
| 6822 | if (operands.size() > 3) { |
| 6823 | scopeId = operands[3]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6824 | semanticsId = builder.makeUintConstant( |
| 6825 | builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 6826 | semanticsId2 = builder.makeUintConstant( |
| 6827 | builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6828 | } |
| 6829 | } else if (opCode == spv::OpAtomicLoad) { |
| 6830 | if (operands.size() > 1) { |
| 6831 | scopeId = operands[1]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6832 | semanticsId = builder.makeUintConstant( |
| 6833 | builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6834 | } |
| 6835 | } else { |
| 6836 | // atomic store or RMW |
| 6837 | valueId = operands[1]; |
| 6838 | if (operands.size() > 2) { |
| 6839 | scopeId = operands[2]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6840 | semanticsId = builder.makeUintConstant |
| 6841 | (builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6842 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 6843 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6844 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6845 | // Check for capabilities |
| 6846 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6847 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 6848 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 6849 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 6850 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6851 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6852 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6853 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6854 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 6855 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6856 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6857 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6858 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 6859 | spvAtomicOperands.push_back(pointerId); |
| 6860 | spvAtomicOperands.push_back(scopeId); |
| 6861 | spvAtomicOperands.push_back(semanticsId); |
| 6862 | if (opCode == spv::OpAtomicCompareExchange) { |
| 6863 | spvAtomicOperands.push_back(semanticsId2); |
| 6864 | spvAtomicOperands.push_back(valueId); |
| 6865 | spvAtomicOperands.push_back(compareId); |
| 6866 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 6867 | spvAtomicOperands.push_back(valueId); |
| 6868 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6869 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6870 | if (opCode == spv::OpAtomicStore) { |
| 6871 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 6872 | return 0; |
| 6873 | } else { |
| 6874 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 6875 | |
| 6876 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 6877 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 6878 | if (op == glslang::EOpAtomicCounterDecrement) |
| 6879 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 6880 | |
| 6881 | return resultId; |
| 6882 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6883 | } |
| 6884 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6885 | // Create group invocation operations. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6886 | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, |
| 6887 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6888 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6889 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6890 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6891 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6892 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6893 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6894 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 6895 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6896 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 6897 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6898 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 6899 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6900 | } else if (op == glslang::EOpAnyInvocation || |
| 6901 | op == glslang::EOpAllInvocations || |
| 6902 | op == glslang::EOpAllInvocationsEqual) { |
| 6903 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 6904 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6905 | } else { |
| 6906 | builder.addCapability(spv::CapabilityGroups); |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6907 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6908 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6909 | op == glslang::EOpAddInvocationsNonUniform || |
| 6910 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6911 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6912 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 6913 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 6914 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 6915 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6916 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6917 | |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6918 | switch (op) { |
| 6919 | case glslang::EOpMinInvocations: |
| 6920 | case glslang::EOpMaxInvocations: |
| 6921 | case glslang::EOpAddInvocations: |
| 6922 | case glslang::EOpMinInvocationsNonUniform: |
| 6923 | case glslang::EOpMaxInvocationsNonUniform: |
| 6924 | case glslang::EOpAddInvocationsNonUniform: |
| 6925 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6926 | break; |
| 6927 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6928 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6929 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6930 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6931 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6932 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6933 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6934 | break; |
| 6935 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6936 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6937 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6938 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6939 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6940 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6941 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6942 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 6943 | default: |
| 6944 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6945 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6946 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6947 | spvGroupOperands.push_back(scope); |
| 6948 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6949 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6950 | spvGroupOperands.push_back(groupOp); |
| 6951 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6952 | } |
| 6953 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6954 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6955 | spv::IdImmediate op = { true, *opIt }; |
| 6956 | spvGroupOperands.push_back(op); |
| 6957 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6958 | |
| 6959 | switch (op) { |
| 6960 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6961 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6962 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6963 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6964 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6965 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6966 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6967 | opCode = spv::OpSubgroupAllEqualKHR; |
| 6968 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6969 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6970 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6971 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6972 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6973 | break; |
| 6974 | case glslang::EOpReadFirstInvocation: |
| 6975 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6976 | break; |
| 6977 | case glslang::EOpBallot: |
| 6978 | { |
| 6979 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6980 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6981 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6982 | // |
| 6983 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6984 | // |
| 6985 | spv::Id uintType = builder.makeUintType(32); |
| 6986 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6987 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6988 | |
| 6989 | std::vector<spv::Id> components; |
| 6990 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6991 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6992 | |
| 6993 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6994 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6995 | builder.createCompositeConstruct(uvec2Type, components)); |
| 6996 | } |
| 6997 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6998 | case glslang::EOpMinInvocations: |
| 6999 | case glslang::EOpMaxInvocations: |
| 7000 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7001 | case glslang::EOpMinInvocationsInclusiveScan: |
| 7002 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 7003 | case glslang::EOpAddInvocationsInclusiveScan: |
| 7004 | case glslang::EOpMinInvocationsExclusiveScan: |
| 7005 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 7006 | case glslang::EOpAddInvocationsExclusiveScan: |
| 7007 | if (op == glslang::EOpMinInvocations || |
| 7008 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 7009 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7010 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7011 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7012 | else { |
| 7013 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7014 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7015 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7016 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7017 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7018 | } else if (op == glslang::EOpMaxInvocations || |
| 7019 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 7020 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7021 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7022 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7023 | else { |
| 7024 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7025 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7026 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7027 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7028 | } |
| 7029 | } else { |
| 7030 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7031 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7032 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7033 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7034 | } |
| 7035 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7036 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7037 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7038 | |
| 7039 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7040 | case glslang::EOpMinInvocationsNonUniform: |
| 7041 | case glslang::EOpMaxInvocationsNonUniform: |
| 7042 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7043 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 7044 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 7045 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 7046 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 7047 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 7048 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 7049 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 7050 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 7051 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7052 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7053 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7054 | else { |
| 7055 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7056 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7057 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7058 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7059 | } |
| 7060 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7061 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 7062 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 7063 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7064 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7065 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7066 | else { |
| 7067 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7068 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7069 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7070 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7071 | } |
| 7072 | } |
| 7073 | else { |
| 7074 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7075 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7076 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7077 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7078 | } |
| 7079 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7080 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7081 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7082 | |
| 7083 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7084 | default: |
| 7085 | logger->missingFunctionality("invocation operation"); |
| 7086 | return spv::NoResult; |
| 7087 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7088 | |
| 7089 | assert(opCode != spv::OpNop); |
| 7090 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7091 | } |
| 7092 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7093 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7094 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 7095 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7096 | { |
| 7097 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 7098 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7099 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7100 | op == spv::OpSubgroupReadInvocationKHR || |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7101 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || |
| 7102 | op == spv::OpGroupSMinNonUniformAMD || |
| 7103 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || |
| 7104 | op == spv::OpGroupSMaxNonUniformAMD || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7105 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
| 7106 | |
| 7107 | // Handle group invocation operations scalar by scalar. |
| 7108 | // The result type is the same type as the original type. |
| 7109 | // The algorithm is to: |
| 7110 | // - break the vector into scalars |
| 7111 | // - apply the operation to each scalar |
| 7112 | // - make a vector out the scalar results |
| 7113 | |
| 7114 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7115 | int numComponents = builder.getNumComponents(operands[0]); |
| 7116 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7117 | std::vector<spv::Id> results; |
| 7118 | |
| 7119 | // do each scalar op |
| 7120 | for (int comp = 0; comp < numComponents; ++comp) { |
| 7121 | std::vector<unsigned int> indexes; |
| 7122 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7123 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 7124 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7125 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 7126 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7127 | spv::IdImmediate operand = { true, operands[1] }; |
| 7128 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7129 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7130 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7131 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7132 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7133 | spv::IdImmediate operand = { true, operands[1] }; |
| 7134 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7135 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7136 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7137 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7138 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7139 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7140 | spvGroupOperands.push_back(scalar); |
| 7141 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7142 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7143 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7144 | } |
| 7145 | |
| 7146 | // put the pieces together |
| 7147 | return builder.createCompositeConstruct(typeId, results); |
| 7148 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7149 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7150 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7151 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 7152 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7153 | { |
| 7154 | // Add the required capabilities. |
| 7155 | switch (op) { |
| 7156 | case glslang::EOpSubgroupElect: |
| 7157 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7158 | break; |
| 7159 | case glslang::EOpSubgroupAll: |
| 7160 | case glslang::EOpSubgroupAny: |
| 7161 | case glslang::EOpSubgroupAllEqual: |
| 7162 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7163 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 7164 | break; |
| 7165 | case glslang::EOpSubgroupBroadcast: |
| 7166 | case glslang::EOpSubgroupBroadcastFirst: |
| 7167 | case glslang::EOpSubgroupBallot: |
| 7168 | case glslang::EOpSubgroupInverseBallot: |
| 7169 | case glslang::EOpSubgroupBallotBitExtract: |
| 7170 | case glslang::EOpSubgroupBallotBitCount: |
| 7171 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7172 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7173 | case glslang::EOpSubgroupBallotFindLSB: |
| 7174 | case glslang::EOpSubgroupBallotFindMSB: |
| 7175 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7176 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 7177 | break; |
| 7178 | case glslang::EOpSubgroupShuffle: |
| 7179 | case glslang::EOpSubgroupShuffleXor: |
| 7180 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7181 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 7182 | break; |
| 7183 | case glslang::EOpSubgroupShuffleUp: |
| 7184 | case glslang::EOpSubgroupShuffleDown: |
| 7185 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7186 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 7187 | break; |
| 7188 | case glslang::EOpSubgroupAdd: |
| 7189 | case glslang::EOpSubgroupMul: |
| 7190 | case glslang::EOpSubgroupMin: |
| 7191 | case glslang::EOpSubgroupMax: |
| 7192 | case glslang::EOpSubgroupAnd: |
| 7193 | case glslang::EOpSubgroupOr: |
| 7194 | case glslang::EOpSubgroupXor: |
| 7195 | case glslang::EOpSubgroupInclusiveAdd: |
| 7196 | case glslang::EOpSubgroupInclusiveMul: |
| 7197 | case glslang::EOpSubgroupInclusiveMin: |
| 7198 | case glslang::EOpSubgroupInclusiveMax: |
| 7199 | case glslang::EOpSubgroupInclusiveAnd: |
| 7200 | case glslang::EOpSubgroupInclusiveOr: |
| 7201 | case glslang::EOpSubgroupInclusiveXor: |
| 7202 | case glslang::EOpSubgroupExclusiveAdd: |
| 7203 | case glslang::EOpSubgroupExclusiveMul: |
| 7204 | case glslang::EOpSubgroupExclusiveMin: |
| 7205 | case glslang::EOpSubgroupExclusiveMax: |
| 7206 | case glslang::EOpSubgroupExclusiveAnd: |
| 7207 | case glslang::EOpSubgroupExclusiveOr: |
| 7208 | case glslang::EOpSubgroupExclusiveXor: |
| 7209 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7210 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 7211 | break; |
| 7212 | case glslang::EOpSubgroupClusteredAdd: |
| 7213 | case glslang::EOpSubgroupClusteredMul: |
| 7214 | case glslang::EOpSubgroupClusteredMin: |
| 7215 | case glslang::EOpSubgroupClusteredMax: |
| 7216 | case glslang::EOpSubgroupClusteredAnd: |
| 7217 | case glslang::EOpSubgroupClusteredOr: |
| 7218 | case glslang::EOpSubgroupClusteredXor: |
| 7219 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7220 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 7221 | break; |
| 7222 | case glslang::EOpSubgroupQuadBroadcast: |
| 7223 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7224 | case glslang::EOpSubgroupQuadSwapVertical: |
| 7225 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 7226 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7227 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 7228 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7229 | case glslang::EOpSubgroupPartitionedAdd: |
| 7230 | case glslang::EOpSubgroupPartitionedMul: |
| 7231 | case glslang::EOpSubgroupPartitionedMin: |
| 7232 | case glslang::EOpSubgroupPartitionedMax: |
| 7233 | case glslang::EOpSubgroupPartitionedAnd: |
| 7234 | case glslang::EOpSubgroupPartitionedOr: |
| 7235 | case glslang::EOpSubgroupPartitionedXor: |
| 7236 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7237 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7238 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7239 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7240 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7241 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7242 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7243 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7244 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7245 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7246 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7247 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7248 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7249 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 7250 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 7251 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 7252 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7253 | default: assert(0 && "Unhandled subgroup operation!"); |
| 7254 | } |
| 7255 | |
Jeff Bolz | c5b669e | 2019-09-08 08:49:18 -0500 | [diff] [blame] | 7256 | |
| 7257 | const bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7258 | const bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7259 | const bool isBool = typeProxy == glslang::EbtBool; |
| 7260 | |
| 7261 | spv::Op opCode = spv::OpNop; |
| 7262 | |
| 7263 | // Figure out which opcode to use. |
| 7264 | switch (op) { |
| 7265 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 7266 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 7267 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 7268 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 7269 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 7270 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 7271 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 7272 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 7273 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 7274 | case glslang::EOpSubgroupBallotBitCount: |
| 7275 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7276 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 7277 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 7278 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 7279 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 7280 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 7281 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 7282 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 7283 | case glslang::EOpSubgroupAdd: |
| 7284 | case glslang::EOpSubgroupInclusiveAdd: |
| 7285 | case glslang::EOpSubgroupExclusiveAdd: |
| 7286 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7287 | case glslang::EOpSubgroupPartitionedAdd: |
| 7288 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7289 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7290 | if (isFloat) { |
| 7291 | opCode = spv::OpGroupNonUniformFAdd; |
| 7292 | } else { |
| 7293 | opCode = spv::OpGroupNonUniformIAdd; |
| 7294 | } |
| 7295 | break; |
| 7296 | case glslang::EOpSubgroupMul: |
| 7297 | case glslang::EOpSubgroupInclusiveMul: |
| 7298 | case glslang::EOpSubgroupExclusiveMul: |
| 7299 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7300 | case glslang::EOpSubgroupPartitionedMul: |
| 7301 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7302 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7303 | if (isFloat) { |
| 7304 | opCode = spv::OpGroupNonUniformFMul; |
| 7305 | } else { |
| 7306 | opCode = spv::OpGroupNonUniformIMul; |
| 7307 | } |
| 7308 | break; |
| 7309 | case glslang::EOpSubgroupMin: |
| 7310 | case glslang::EOpSubgroupInclusiveMin: |
| 7311 | case glslang::EOpSubgroupExclusiveMin: |
| 7312 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7313 | case glslang::EOpSubgroupPartitionedMin: |
| 7314 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7315 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7316 | if (isFloat) { |
| 7317 | opCode = spv::OpGroupNonUniformFMin; |
| 7318 | } else if (isUnsigned) { |
| 7319 | opCode = spv::OpGroupNonUniformUMin; |
| 7320 | } else { |
| 7321 | opCode = spv::OpGroupNonUniformSMin; |
| 7322 | } |
| 7323 | break; |
| 7324 | case glslang::EOpSubgroupMax: |
| 7325 | case glslang::EOpSubgroupInclusiveMax: |
| 7326 | case glslang::EOpSubgroupExclusiveMax: |
| 7327 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7328 | case glslang::EOpSubgroupPartitionedMax: |
| 7329 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7330 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7331 | if (isFloat) { |
| 7332 | opCode = spv::OpGroupNonUniformFMax; |
| 7333 | } else if (isUnsigned) { |
| 7334 | opCode = spv::OpGroupNonUniformUMax; |
| 7335 | } else { |
| 7336 | opCode = spv::OpGroupNonUniformSMax; |
| 7337 | } |
| 7338 | break; |
| 7339 | case glslang::EOpSubgroupAnd: |
| 7340 | case glslang::EOpSubgroupInclusiveAnd: |
| 7341 | case glslang::EOpSubgroupExclusiveAnd: |
| 7342 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7343 | case glslang::EOpSubgroupPartitionedAnd: |
| 7344 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7345 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7346 | if (isBool) { |
| 7347 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 7348 | } else { |
| 7349 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 7350 | } |
| 7351 | break; |
| 7352 | case glslang::EOpSubgroupOr: |
| 7353 | case glslang::EOpSubgroupInclusiveOr: |
| 7354 | case glslang::EOpSubgroupExclusiveOr: |
| 7355 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7356 | case glslang::EOpSubgroupPartitionedOr: |
| 7357 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7358 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7359 | if (isBool) { |
| 7360 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 7361 | } else { |
| 7362 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 7363 | } |
| 7364 | break; |
| 7365 | case glslang::EOpSubgroupXor: |
| 7366 | case glslang::EOpSubgroupInclusiveXor: |
| 7367 | case glslang::EOpSubgroupExclusiveXor: |
| 7368 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7369 | case glslang::EOpSubgroupPartitionedXor: |
| 7370 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7371 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7372 | if (isBool) { |
| 7373 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 7374 | } else { |
| 7375 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 7376 | } |
| 7377 | break; |
| 7378 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 7379 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7380 | case glslang::EOpSubgroupQuadSwapVertical: |
| 7381 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 7382 | default: assert(0 && "Unhandled subgroup operation!"); |
| 7383 | } |
| 7384 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7385 | // get the right Group Operation |
| 7386 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7387 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7388 | default: |
| 7389 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7390 | case glslang::EOpSubgroupBallotBitCount: |
| 7391 | case glslang::EOpSubgroupAdd: |
| 7392 | case glslang::EOpSubgroupMul: |
| 7393 | case glslang::EOpSubgroupMin: |
| 7394 | case glslang::EOpSubgroupMax: |
| 7395 | case glslang::EOpSubgroupAnd: |
| 7396 | case glslang::EOpSubgroupOr: |
| 7397 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7398 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7399 | break; |
| 7400 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7401 | case glslang::EOpSubgroupInclusiveAdd: |
| 7402 | case glslang::EOpSubgroupInclusiveMul: |
| 7403 | case glslang::EOpSubgroupInclusiveMin: |
| 7404 | case glslang::EOpSubgroupInclusiveMax: |
| 7405 | case glslang::EOpSubgroupInclusiveAnd: |
| 7406 | case glslang::EOpSubgroupInclusiveOr: |
| 7407 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7408 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7409 | break; |
| 7410 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7411 | case glslang::EOpSubgroupExclusiveAdd: |
| 7412 | case glslang::EOpSubgroupExclusiveMul: |
| 7413 | case glslang::EOpSubgroupExclusiveMin: |
| 7414 | case glslang::EOpSubgroupExclusiveMax: |
| 7415 | case glslang::EOpSubgroupExclusiveAnd: |
| 7416 | case glslang::EOpSubgroupExclusiveOr: |
| 7417 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7418 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7419 | break; |
| 7420 | case glslang::EOpSubgroupClusteredAdd: |
| 7421 | case glslang::EOpSubgroupClusteredMul: |
| 7422 | case glslang::EOpSubgroupClusteredMin: |
| 7423 | case glslang::EOpSubgroupClusteredMax: |
| 7424 | case glslang::EOpSubgroupClusteredAnd: |
| 7425 | case glslang::EOpSubgroupClusteredOr: |
| 7426 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7427 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7428 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7429 | case glslang::EOpSubgroupPartitionedAdd: |
| 7430 | case glslang::EOpSubgroupPartitionedMul: |
| 7431 | case glslang::EOpSubgroupPartitionedMin: |
| 7432 | case glslang::EOpSubgroupPartitionedMax: |
| 7433 | case glslang::EOpSubgroupPartitionedAnd: |
| 7434 | case glslang::EOpSubgroupPartitionedOr: |
| 7435 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7436 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7437 | break; |
| 7438 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7439 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7440 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7441 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7442 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7443 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7444 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7445 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7446 | break; |
| 7447 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7448 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7449 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7450 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7451 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7452 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7453 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7454 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7455 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7456 | } |
| 7457 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7458 | // build the instruction |
| 7459 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 7460 | |
| 7461 | // Every operation begins with the Execution Scope operand. |
| 7462 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7463 | spvGroupOperands.push_back(executionScope); |
| 7464 | |
| 7465 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 7466 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7467 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7468 | spvGroupOperands.push_back(groupOperand); |
| 7469 | } |
| 7470 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7471 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7472 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 7473 | spv::IdImmediate operand = { true, *opIt }; |
| 7474 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7475 | } |
| 7476 | |
| 7477 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7478 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7479 | switch (op) { |
| 7480 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7481 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 7482 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 7483 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 7484 | } |
| 7485 | if (directionId != spv::NoResult) { |
| 7486 | spv::IdImmediate direction = { true, directionId }; |
| 7487 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7488 | } |
| 7489 | |
| 7490 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 7491 | } |
| 7492 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7493 | spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, |
| 7494 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7495 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7496 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7497 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7498 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7499 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7500 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7501 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 7502 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7503 | spv::Id typeId0 = 0; |
| 7504 | if (consumedOperands > 0) |
| 7505 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7506 | spv::Id typeId1 = 0; |
| 7507 | if (consumedOperands > 1) |
| 7508 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7509 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7510 | |
| 7511 | switch (op) { |
| 7512 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7513 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7514 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7515 | else if (isUnsigned) |
| 7516 | libCall = spv::GLSLstd450UMin; |
| 7517 | else |
| 7518 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7519 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7520 | break; |
| 7521 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7522 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7523 | break; |
| 7524 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7525 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7526 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7527 | else if (isUnsigned) |
| 7528 | libCall = spv::GLSLstd450UMax; |
| 7529 | else |
| 7530 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7531 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7532 | break; |
| 7533 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7534 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7535 | break; |
| 7536 | case glslang::EOpDot: |
| 7537 | opCode = spv::OpDot; |
| 7538 | break; |
| 7539 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7540 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7541 | break; |
| 7542 | |
| 7543 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7544 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7545 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7546 | else if (isUnsigned) |
| 7547 | libCall = spv::GLSLstd450UClamp; |
| 7548 | else |
| 7549 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7550 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 7551 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7552 | break; |
| 7553 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7554 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 7555 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7556 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7557 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7558 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7559 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7560 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7561 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7562 | break; |
| 7563 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7564 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7565 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7566 | break; |
| 7567 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7568 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7569 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 7570 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7571 | break; |
| 7572 | |
| 7573 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7574 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7575 | break; |
| 7576 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7577 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7578 | break; |
| 7579 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7580 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7581 | break; |
| 7582 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7583 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7584 | break; |
| 7585 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7586 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7587 | break; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7588 | case glslang::EOpBarrier: |
| 7589 | { |
| 7590 | // This is for the extended controlBarrier function, with four operands. |
| 7591 | // The unextended barrier() goes through createNoArgOperation. |
| 7592 | assert(operands.size() == 4); |
| 7593 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 7594 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 7595 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7596 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, |
| 7597 | (spv::MemorySemanticsMask)semantics); |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7598 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7599 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7600 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7601 | spv::MemorySemanticsVolatileMask)) { |
| 7602 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7603 | } |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7604 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || |
| 7605 | memoryScope == spv::ScopeDevice)) { |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7606 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7607 | } |
| 7608 | return 0; |
| 7609 | } |
| 7610 | break; |
| 7611 | case glslang::EOpMemoryBarrier: |
| 7612 | { |
| 7613 | // This is for the extended memoryBarrier function, with three operands. |
| 7614 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 7615 | assert(operands.size() == 3); |
| 7616 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 7617 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 7618 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 7619 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7620 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7621 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7622 | spv::MemorySemanticsVolatileMask)) { |
| 7623 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7624 | } |
| 7625 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 7626 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7627 | } |
| 7628 | return 0; |
| 7629 | } |
| 7630 | break; |
| 7631 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7632 | #ifndef GLSLANG_WEB |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7633 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7634 | if (typeProxy == glslang::EbtFloat16) |
| 7635 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7636 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 7637 | break; |
| 7638 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7639 | if (typeProxy == glslang::EbtFloat16) |
| 7640 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7641 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 7642 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7643 | case glslang::EOpAddCarry: |
| 7644 | opCode = spv::OpIAddCarry; |
| 7645 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7646 | consumedOperands = 2; |
| 7647 | break; |
| 7648 | case glslang::EOpSubBorrow: |
| 7649 | opCode = spv::OpISubBorrow; |
| 7650 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7651 | consumedOperands = 2; |
| 7652 | break; |
| 7653 | case glslang::EOpUMulExtended: |
| 7654 | opCode = spv::OpUMulExtended; |
| 7655 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7656 | consumedOperands = 2; |
| 7657 | break; |
| 7658 | case glslang::EOpIMulExtended: |
| 7659 | opCode = spv::OpSMulExtended; |
| 7660 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7661 | consumedOperands = 2; |
| 7662 | break; |
| 7663 | case glslang::EOpBitfieldExtract: |
| 7664 | if (isUnsigned) |
| 7665 | opCode = spv::OpBitFieldUExtract; |
| 7666 | else |
| 7667 | opCode = spv::OpBitFieldSExtract; |
| 7668 | break; |
| 7669 | case glslang::EOpBitfieldInsert: |
| 7670 | opCode = spv::OpBitFieldInsert; |
| 7671 | break; |
| 7672 | |
| 7673 | case glslang::EOpFma: |
| 7674 | libCall = spv::GLSLstd450Fma; |
| 7675 | break; |
| 7676 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7677 | { |
| 7678 | libCall = spv::GLSLstd450FrexpStruct; |
| 7679 | assert(builder.isPointerType(typeId1)); |
| 7680 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7681 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 7682 | if (width == 16) |
| 7683 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 7684 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7685 | if (builder.getNumComponents(operands[0]) == 1) |
| 7686 | frexpIntType = builder.makeIntegerType(width, true); |
| 7687 | else |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7688 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), |
| 7689 | builder.getNumComponents(operands[0])); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7690 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 7691 | consumedOperands = 1; |
| 7692 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7693 | break; |
| 7694 | case glslang::EOpLdexp: |
| 7695 | libCall = spv::GLSLstd450Ldexp; |
| 7696 | break; |
| 7697 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7698 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7699 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7700 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7701 | case glslang::EOpSubgroupBroadcast: |
| 7702 | case glslang::EOpSubgroupBallotBitExtract: |
| 7703 | case glslang::EOpSubgroupShuffle: |
| 7704 | case glslang::EOpSubgroupShuffleXor: |
| 7705 | case glslang::EOpSubgroupShuffleUp: |
| 7706 | case glslang::EOpSubgroupShuffleDown: |
| 7707 | case glslang::EOpSubgroupClusteredAdd: |
| 7708 | case glslang::EOpSubgroupClusteredMul: |
| 7709 | case glslang::EOpSubgroupClusteredMin: |
| 7710 | case glslang::EOpSubgroupClusteredMax: |
| 7711 | case glslang::EOpSubgroupClusteredAnd: |
| 7712 | case glslang::EOpSubgroupClusteredOr: |
| 7713 | case glslang::EOpSubgroupClusteredXor: |
| 7714 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7715 | case glslang::EOpSubgroupPartitionedAdd: |
| 7716 | case glslang::EOpSubgroupPartitionedMul: |
| 7717 | case glslang::EOpSubgroupPartitionedMin: |
| 7718 | case glslang::EOpSubgroupPartitionedMax: |
| 7719 | case glslang::EOpSubgroupPartitionedAnd: |
| 7720 | case glslang::EOpSubgroupPartitionedOr: |
| 7721 | case glslang::EOpSubgroupPartitionedXor: |
| 7722 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7723 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7724 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7725 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7726 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7727 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7728 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7729 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7730 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7731 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7732 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7733 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7734 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7735 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7736 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 7737 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7738 | case glslang::EOpSwizzleInvocations: |
| 7739 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7740 | libCall = spv::SwizzleInvocationsAMD; |
| 7741 | break; |
| 7742 | case glslang::EOpSwizzleInvocationsMasked: |
| 7743 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7744 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 7745 | break; |
| 7746 | case glslang::EOpWriteInvocation: |
| 7747 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7748 | libCall = spv::WriteInvocationAMD; |
| 7749 | break; |
| 7750 | |
| 7751 | case glslang::EOpMin3: |
| 7752 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7753 | if (isFloat) |
| 7754 | libCall = spv::FMin3AMD; |
| 7755 | else { |
| 7756 | if (isUnsigned) |
| 7757 | libCall = spv::UMin3AMD; |
| 7758 | else |
| 7759 | libCall = spv::SMin3AMD; |
| 7760 | } |
| 7761 | break; |
| 7762 | case glslang::EOpMax3: |
| 7763 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7764 | if (isFloat) |
| 7765 | libCall = spv::FMax3AMD; |
| 7766 | else { |
| 7767 | if (isUnsigned) |
| 7768 | libCall = spv::UMax3AMD; |
| 7769 | else |
| 7770 | libCall = spv::SMax3AMD; |
| 7771 | } |
| 7772 | break; |
| 7773 | case glslang::EOpMid3: |
| 7774 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7775 | if (isFloat) |
| 7776 | libCall = spv::FMid3AMD; |
| 7777 | else { |
| 7778 | if (isUnsigned) |
| 7779 | libCall = spv::UMid3AMD; |
| 7780 | else |
| 7781 | libCall = spv::SMid3AMD; |
| 7782 | } |
| 7783 | break; |
| 7784 | |
| 7785 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7786 | if (typeProxy == glslang::EbtFloat16) |
| 7787 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7788 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 7789 | libCall = spv::InterpolateAtVertexAMD; |
| 7790 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7791 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7792 | case glslang::EOpReportIntersection: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7793 | typeId = builder.makeBoolType(); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7794 | opCode = spv::OpReportIntersectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7795 | break; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7796 | case glslang::EOpTrace: |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7797 | builder.createNoResultOp(spv::OpTraceRayKHR, operands); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7798 | return 0; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7799 | case glslang::EOpExecuteCallable: |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7800 | builder.createNoResultOp(spv::OpExecuteCallableKHR, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7801 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7802 | |
| 7803 | case glslang::EOpRayQueryInitialize: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7804 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR, operands); |
| 7805 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7806 | case glslang::EOpRayQueryTerminate: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7807 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operands); |
| 7808 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7809 | case glslang::EOpRayQueryGenerateIntersection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7810 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR, operands); |
| 7811 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7812 | case glslang::EOpRayQueryConfirmIntersection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7813 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operands); |
| 7814 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7815 | case glslang::EOpRayQueryProceed: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7816 | typeId = builder.makeBoolType(); |
| 7817 | opCode = spv::OpRayQueryProceedKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7818 | break; |
| 7819 | case glslang::EOpRayQueryGetIntersectionType: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 7820 | typeId = builder.makeUintType(32); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7821 | opCode = spv::OpRayQueryGetIntersectionTypeKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7822 | break; |
| 7823 | case glslang::EOpRayQueryGetRayTMin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7824 | typeId = builder.makeFloatType(32); |
| 7825 | opCode = spv::OpRayQueryGetRayTMinKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7826 | break; |
| 7827 | case glslang::EOpRayQueryGetRayFlags: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7828 | typeId = builder.makeIntType(32); |
| 7829 | opCode = spv::OpRayQueryGetRayFlagsKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7830 | break; |
| 7831 | case glslang::EOpRayQueryGetIntersectionT: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 7832 | typeId = builder.makeFloatType(32); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7833 | opCode = spv::OpRayQueryGetIntersectionTKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7834 | break; |
| 7835 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7836 | typeId = builder.makeIntType(32); |
| 7837 | opCode = spv::OpRayQueryGetIntersectionInstanceCustomIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7838 | break; |
| 7839 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7840 | typeId = builder.makeIntType(32); |
| 7841 | opCode = spv::OpRayQueryGetIntersectionInstanceIdKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7842 | break; |
| 7843 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7844 | typeId = builder.makeIntType(32); |
| 7845 | opCode = spv::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7846 | break; |
| 7847 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7848 | typeId = builder.makeIntType(32); |
| 7849 | opCode = spv::OpRayQueryGetIntersectionGeometryIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7850 | break; |
| 7851 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7852 | typeId = builder.makeIntType(32); |
| 7853 | opCode = spv::OpRayQueryGetIntersectionPrimitiveIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7854 | break; |
| 7855 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7856 | typeId = builder.makeVectorType(builder.makeFloatType(32), 2); |
| 7857 | opCode = spv::OpRayQueryGetIntersectionBarycentricsKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7858 | break; |
| 7859 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7860 | typeId = builder.makeBoolType(); |
| 7861 | opCode = spv::OpRayQueryGetIntersectionFrontFaceKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7862 | break; |
| 7863 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7864 | typeId = builder.makeBoolType(); |
| 7865 | opCode = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7866 | break; |
| 7867 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7868 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7869 | opCode = spv::OpRayQueryGetIntersectionObjectRayDirectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7870 | break; |
| 7871 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7872 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7873 | opCode = spv::OpRayQueryGetIntersectionObjectRayOriginKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7874 | break; |
| 7875 | case glslang::EOpRayQueryGetWorldRayDirection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7876 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7877 | opCode = spv::OpRayQueryGetWorldRayDirectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7878 | break; |
| 7879 | case glslang::EOpRayQueryGetWorldRayOrigin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7880 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7881 | opCode = spv::OpRayQueryGetWorldRayOriginKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7882 | break; |
| 7883 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7884 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7885 | opCode = spv::OpRayQueryGetIntersectionObjectToWorldKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7886 | break; |
| 7887 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7888 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7889 | opCode = spv::OpRayQueryGetIntersectionWorldToObjectKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7890 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7891 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 7892 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 7893 | return 0; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 7894 | case glslang::EOpCooperativeMatrixMulAdd: |
| 7895 | opCode = spv::OpCooperativeMatrixMulAddNV; |
| 7896 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7897 | #endif // GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7898 | default: |
| 7899 | return 0; |
| 7900 | } |
| 7901 | |
| 7902 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7903 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 7904 | // Use an extended instruction from the standard library. |
| 7905 | // Construct the call arguments, without modifying the original operands vector. |
| 7906 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 7907 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7908 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 7909 | } else if (opCode == spv::OpDot && !isFloat) { |
| 7910 | // int dot(int, int) |
| 7911 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 7912 | const int componentCount = builder.getNumComponents(operands[0]); |
| 7913 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 7914 | builder.setPrecision(mulOp, precision); |
| 7915 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 7916 | for (int i = 1; i < componentCount; ++i) { |
| 7917 | builder.setPrecision(id, precision); |
John Kessenich | 5de15a2 | 2019-12-26 10:56:54 -0700 | [diff] [blame] | 7918 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(mulOp, typeId, i)); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 7919 | } |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7920 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7921 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7922 | case 0: |
| 7923 | // should all be handled by visitAggregate and createNoArgOperation |
| 7924 | assert(0); |
| 7925 | return 0; |
| 7926 | case 1: |
| 7927 | // should all be handled by createUnaryOperation |
| 7928 | assert(0); |
| 7929 | return 0; |
| 7930 | case 2: |
| 7931 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 7932 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7933 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7934 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 7935 | assert(consumedOperands == operands.size()); |
| 7936 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7937 | break; |
| 7938 | } |
| 7939 | } |
| 7940 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7941 | #ifndef GLSLANG_WEB |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7942 | // Decode the return types that were structures |
| 7943 | switch (op) { |
| 7944 | case glslang::EOpAddCarry: |
| 7945 | case glslang::EOpSubBorrow: |
| 7946 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7947 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7948 | break; |
| 7949 | case glslang::EOpUMulExtended: |
| 7950 | case glslang::EOpIMulExtended: |
| 7951 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 7952 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7953 | break; |
| 7954 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7955 | { |
| 7956 | assert(operands.size() == 2); |
| 7957 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 7958 | // "exp" is floating-point type (from HLSL intrinsic) |
| 7959 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 7960 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 7961 | builder.createStore(member1, operands[1]); |
| 7962 | } else |
| 7963 | // "exp" is integer type (from GLSL built-in function) |
| 7964 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 7965 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7966 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7967 | break; |
| 7968 | default: |
| 7969 | break; |
| 7970 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7971 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7972 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 7973 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7974 | } |
| 7975 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7976 | // Intrinsics with no arguments (or no return value, and no precision). |
| 7977 | 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] | 7978 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7979 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7980 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? |
| 7981 | spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7982 | |
| 7983 | switch (op) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7984 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7985 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7986 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7987 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7988 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7989 | spv::MemorySemanticsAcquireReleaseMask); |
| 7990 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7991 | } else { |
| 7992 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 7993 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7994 | } else { |
| 7995 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7996 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7997 | spv::MemorySemanticsAcquireReleaseMask); |
| 7998 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7999 | return 0; |
| 8000 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 8001 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 8002 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8003 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8004 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 8005 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 8006 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8007 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8008 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 8009 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8010 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8011 | return 0; |
| 8012 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 8013 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 8014 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8015 | return 0; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 8016 | #ifndef GLSLANG_WEB |
| 8017 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 8018 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 8019 | spv::MemorySemanticsAcquireReleaseMask); |
| 8020 | return 0; |
| 8021 | case glslang::EOpMemoryBarrierImage: |
| 8022 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 8023 | spv::MemorySemanticsAcquireReleaseMask); |
| 8024 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8025 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8026 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 8027 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8028 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8029 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8030 | case glslang::EOpDeviceMemoryBarrier: |
| 8031 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 8032 | spv::MemorySemanticsImageMemoryMask | |
| 8033 | spv::MemorySemanticsAcquireReleaseMask); |
| 8034 | return 0; |
| 8035 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 8036 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 8037 | spv::MemorySemanticsImageMemoryMask | |
| 8038 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8039 | return 0; |
| 8040 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8041 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8042 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8043 | return 0; |
| 8044 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8045 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 8046 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 8047 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8048 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8049 | case glslang::EOpSubgroupBarrier: |
| 8050 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 8051 | spv::MemorySemanticsAcquireReleaseMask); |
| 8052 | return spv::NoResult; |
| 8053 | case glslang::EOpSubgroupMemoryBarrier: |
| 8054 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 8055 | spv::MemorySemanticsAcquireReleaseMask); |
| 8056 | return spv::NoResult; |
| 8057 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 8058 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 8059 | spv::MemorySemanticsAcquireReleaseMask); |
| 8060 | return spv::NoResult; |
| 8061 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 8062 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 8063 | spv::MemorySemanticsAcquireReleaseMask); |
| 8064 | return spv::NoResult; |
| 8065 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 8066 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8067 | spv::MemorySemanticsAcquireReleaseMask); |
| 8068 | return spv::NoResult; |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 8069 | |
| 8070 | case glslang::EOpEmitVertex: |
| 8071 | builder.createNoResultOp(spv::OpEmitVertex); |
| 8072 | return 0; |
| 8073 | case glslang::EOpEndPrimitive: |
| 8074 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 8075 | return 0; |
| 8076 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8077 | case glslang::EOpSubgroupElect: { |
| 8078 | std::vector<spv::Id> operands; |
| 8079 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 8080 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8081 | case glslang::EOpTime: |
| 8082 | { |
| 8083 | std::vector<spv::Id> args; // Dummy arguments |
| 8084 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 8085 | return builder.setPrecision(id, precision); |
| 8086 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 8087 | case glslang::EOpIgnoreIntersection: |
| 8088 | builder.createNoResultOp(spv::OpIgnoreIntersectionKHR); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 8089 | return 0; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 8090 | case glslang::EOpTerminateRay: |
| 8091 | builder.createNoResultOp(spv::OpTerminateRayKHR); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 8092 | return 0; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8093 | case glslang::EOpRayQueryInitialize: |
| 8094 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR); |
| 8095 | return 0; |
| 8096 | case glslang::EOpRayQueryTerminate: |
| 8097 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR); |
| 8098 | return 0; |
| 8099 | case glslang::EOpRayQueryGenerateIntersection: |
| 8100 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR); |
| 8101 | return 0; |
| 8102 | case glslang::EOpRayQueryConfirmIntersection: |
| 8103 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR); |
| 8104 | return 0; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 8105 | case glslang::EOpBeginInvocationInterlock: |
| 8106 | builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
| 8107 | return 0; |
| 8108 | case glslang::EOpEndInvocationInterlock: |
| 8109 | builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
| 8110 | return 0; |
| 8111 | |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 8112 | case glslang::EOpIsHelperInvocation: |
| 8113 | { |
| 8114 | std::vector<spv::Id> args; // Dummy arguments |
Rex Xu | bb7307b | 2019-07-15 14:57:20 +0800 | [diff] [blame] | 8115 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 8116 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 8117 | return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args); |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 8118 | } |
| 8119 | |
amhagan | 91fb009 | 2019-07-10 21:14:38 -0400 | [diff] [blame] | 8120 | case glslang::EOpReadClockSubgroupKHR: { |
| 8121 | std::vector<spv::Id> args; |
| 8122 | args.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 8123 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 8124 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 8125 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 8126 | } |
| 8127 | |
| 8128 | case glslang::EOpReadClockDeviceKHR: { |
| 8129 | std::vector<spv::Id> args; |
| 8130 | args.push_back(builder.makeUintConstant(spv::ScopeDevice)); |
| 8131 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 8132 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 8133 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 8134 | } |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 8135 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8136 | default: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8137 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8138 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8139 | |
| 8140 | logger->missingFunctionality("unknown operation with no arguments"); |
| 8141 | |
| 8142 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8143 | } |
| 8144 | |
| 8145 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 8146 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 8147 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8148 | spv::Id id; |
| 8149 | if (symbolValues.end() != iter) { |
| 8150 | id = iter->second; |
| 8151 | return id; |
| 8152 | } |
| 8153 | |
| 8154 | // it was not found, create it |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8155 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 8156 | auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8157 | id = createSpvVariable(symbol, forcedType.first); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8158 | symbolValues[symbol->getId()] = id; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8159 | if (forcedType.second != spv::NoType) |
| 8160 | forceType[id] = forcedType.second; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8161 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 8162 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8163 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 8164 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 8165 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8166 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8167 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8168 | if (symbol->getQualifier().hasComponent()) |
| 8169 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 8170 | if (symbol->getQualifier().hasIndex()) |
| 8171 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8172 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8173 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8174 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 8175 | // atomic counters use this: |
| 8176 | if (symbol->getQualifier().hasOffset()) |
| 8177 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8178 | } |
| 8179 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 8180 | if (symbol->getQualifier().hasLocation()) |
| 8181 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8182 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 8183 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8184 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8185 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8186 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8187 | if (symbol->getQualifier().hasSet()) |
| 8188 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8189 | else if (IsDescriptorResource(symbol->getType())) { |
| 8190 | // default to 0 |
| 8191 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 8192 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8193 | if (symbol->getQualifier().hasBinding()) |
| 8194 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
Jeff Bolz | 0a93cfb | 2018-12-11 20:53:59 -0600 | [diff] [blame] | 8195 | else if (IsDescriptorResource(symbol->getType())) { |
| 8196 | // default to 0 |
| 8197 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 8198 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8199 | if (symbol->getQualifier().hasAttachment()) |
| 8200 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8201 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8202 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 8203 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8204 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 8205 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 8206 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 8207 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 8208 | } |
| 8209 | if (symbol->getQualifier().hasXfbOffset()) |
| 8210 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8211 | } |
| 8212 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8213 | // add built-in variable decoration |
| 8214 | if (builtIn != spv::BuiltInMax) { |
| 8215 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
| 8216 | } |
| 8217 | |
| 8218 | #ifndef GLSLANG_WEB |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8219 | if (symbol->getType().isImage()) { |
| 8220 | std::vector<spv::Decoration> memory; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8221 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, |
| 8222 | glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8223 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8224 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8225 | } |
| 8226 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 8227 | // nonuniform |
| 8228 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 8229 | |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8230 | if (builtIn == spv::BuiltInSampleMask) { |
| 8231 | spv::Decoration decoration; |
| 8232 | // GL_NV_sample_mask_override_coverage extension |
| 8233 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8234 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8235 | else |
| 8236 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8237 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8238 | if (decoration != spv::DecorationMax) { |
Jason Macnak | dbd4c3c | 2019-07-12 14:33:02 -0700 | [diff] [blame] | 8239 | builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8240 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 8241 | } |
| 8242 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8243 | else if (builtIn == spv::BuiltInLayer) { |
| 8244 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 8245 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8246 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8247 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 8248 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 8249 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 8250 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8251 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 8252 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8253 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 8254 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 8255 | } |
| 8256 | } |
| 8257 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 8258 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8259 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8260 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 8261 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 8262 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 8263 | if (symbol->getQualifier().pervertexNV) { |
| 8264 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 8265 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 8266 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 8267 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8268 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8269 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 8270 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 8271 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 8272 | symbol->getType().getQualifier().semanticName); |
| 8273 | } |
| 8274 | |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 8275 | if (symbol->isReference()) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8276 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? |
| 8277 | spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8278 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8279 | #endif |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8280 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8281 | return id; |
| 8282 | } |
| 8283 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8284 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8285 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 8286 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 8287 | { |
| 8288 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8289 | if (qualifier.perPrimitiveNV) { |
| 8290 | // Need to add capability/extension for fragment shader. |
| 8291 | // Mesh shader already adds this by default. |
| 8292 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 8293 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 8294 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 8295 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8296 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8297 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8298 | if (qualifier.perViewNV) |
| 8299 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 8300 | if (qualifier.perTaskNV) |
| 8301 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 8302 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8303 | if (qualifier.perPrimitiveNV) { |
| 8304 | // Need to add capability/extension for fragment shader. |
| 8305 | // Mesh shader already adds this by default. |
| 8306 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 8307 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 8308 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 8309 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8310 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8311 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8312 | if (qualifier.perViewNV) |
| 8313 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 8314 | if (qualifier.perTaskNV) |
| 8315 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 8316 | } |
| 8317 | } |
| 8318 | #endif |
| 8319 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8320 | // 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] | 8321 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8322 | // |
| 8323 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 8324 | // |
| 8325 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 8326 | // regular constants, which themselves are trees that createSpvConstant() |
| 8327 | // recursively walks. So, this function walks the "top" of the tree: |
| 8328 | // - emit specialization constant-building instructions for specConstant |
| 8329 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8330 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8331 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 8332 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8333 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8334 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8335 | if (! node.getQualifier().specConstant) { |
| 8336 | // hand off to the non-spec-constant path |
| 8337 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 8338 | int nextConst = 0; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8339 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? |
| 8340 | node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
| 8341 | nextConst, false); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8342 | } |
| 8343 | |
| 8344 | // We now know we have a specialization constant to build |
| 8345 | |
Ricardo Garcia | 232ba0d | 2020-06-03 15:52:55 +0200 | [diff] [blame] | 8346 | // Extra capabilities may be needed. |
| 8347 | if (node.getType().contains8BitInt()) |
| 8348 | builder.addCapability(spv::CapabilityInt8); |
| 8349 | if (node.getType().contains16BitFloat()) |
| 8350 | builder.addCapability(spv::CapabilityFloat16); |
| 8351 | if (node.getType().contains16BitInt()) |
| 8352 | builder.addCapability(spv::CapabilityInt16); |
| 8353 | if (node.getType().contains64BitInt()) |
| 8354 | builder.addCapability(spv::CapabilityInt64); |
| 8355 | if (node.getType().containsDouble()) |
| 8356 | builder.addCapability(spv::CapabilityFloat64); |
| 8357 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 8358 | // 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] | 8359 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 8360 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 8361 | std::vector<spv::Id> dimConstId; |
| 8362 | for (int dim = 0; dim < 3; ++dim) { |
| 8363 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 8364 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8365 | if (specConst) { |
| 8366 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 8367 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 8368 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8369 | } |
| 8370 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 8371 | } |
| 8372 | |
| 8373 | // An AST node labelled as specialization constant should be a symbol node. |
| 8374 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 8375 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8376 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8377 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 8378 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 8379 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 8380 | // will set the builder into spec constant op instruction generating mode. |
| 8381 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8382 | result = accessChainLoad(sub_tree->getType()); |
| 8383 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8384 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8385 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 8386 | } else { |
| 8387 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 8388 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8389 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8390 | builder.addName(result, sn->getName().c_str()); |
| 8391 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8392 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8393 | |
| 8394 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 8395 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8396 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8397 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8398 | } |
| 8399 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8400 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 8401 | // build the aggregate SPIR-V constant. |
| 8402 | // |
| 8403 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 8404 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 8405 | // |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8406 | spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, |
| 8407 | const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8408 | { |
| 8409 | // vector of constants for SPIR-V |
| 8410 | std::vector<spv::Id> spvConsts; |
| 8411 | |
| 8412 | // Type is used for struct and array constants |
| 8413 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 8414 | |
| 8415 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 8416 | glslang::TType elementType(glslangType, 0); |
| 8417 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8418 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8419 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 8420 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8421 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8422 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 8423 | } else if (glslangType.isCoopMat()) { |
| 8424 | glslang::TType componentType(glslangType.getBasicType()); |
| 8425 | spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8426 | } else if (glslangType.isStruct()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8427 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 8428 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8429 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 8430 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8431 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 8432 | bool zero = nextConst >= consts.size(); |
| 8433 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8434 | case glslang::EbtInt: |
| 8435 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 8436 | break; |
| 8437 | case glslang::EbtUint: |
| 8438 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 8439 | break; |
| 8440 | case glslang::EbtFloat: |
| 8441 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 8442 | break; |
| 8443 | case glslang::EbtBool: |
| 8444 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 8445 | break; |
| 8446 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8447 | case glslang::EbtInt8: |
| 8448 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 8449 | break; |
| 8450 | case glslang::EbtUint8: |
| 8451 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 8452 | break; |
| 8453 | case glslang::EbtInt16: |
| 8454 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 8455 | break; |
| 8456 | case glslang::EbtUint16: |
| 8457 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 8458 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 8459 | case glslang::EbtInt64: |
| 8460 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 8461 | break; |
| 8462 | case glslang::EbtUint64: |
| 8463 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 8464 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8465 | case glslang::EbtDouble: |
| 8466 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 8467 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 8468 | case glslang::EbtFloat16: |
| 8469 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 8470 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8471 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8472 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8473 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8474 | break; |
| 8475 | } |
| 8476 | ++nextConst; |
| 8477 | } |
| 8478 | } else { |
| 8479 | // we have a non-aggregate (scalar) constant |
| 8480 | bool zero = nextConst >= consts.size(); |
| 8481 | spv::Id scalar = 0; |
| 8482 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8483 | case glslang::EbtInt: |
| 8484 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
| 8485 | break; |
| 8486 | case glslang::EbtUint: |
| 8487 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
| 8488 | break; |
| 8489 | case glslang::EbtFloat: |
| 8490 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8491 | break; |
| 8492 | case glslang::EbtBool: |
| 8493 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
| 8494 | break; |
| 8495 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8496 | case glslang::EbtInt8: |
| 8497 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 8498 | break; |
| 8499 | case glslang::EbtUint8: |
| 8500 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 8501 | break; |
| 8502 | case glslang::EbtInt16: |
| 8503 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 8504 | break; |
| 8505 | case glslang::EbtUint16: |
| 8506 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 8507 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 8508 | case glslang::EbtInt64: |
| 8509 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 8510 | break; |
| 8511 | case glslang::EbtUint64: |
| 8512 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8513 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8514 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8515 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8516 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 8517 | case glslang::EbtFloat16: |
| 8518 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8519 | break; |
Jeff Bolz | 3fd1232 | 2019-03-05 23:27:09 -0600 | [diff] [blame] | 8520 | case glslang::EbtReference: |
| 8521 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8522 | scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar); |
| 8523 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8524 | #endif |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 8525 | case glslang::EbtString: |
| 8526 | scalar = builder.getStringId(consts[nextConst].getSConst()->c_str()); |
| 8527 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8528 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8529 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8530 | break; |
| 8531 | } |
| 8532 | ++nextConst; |
| 8533 | return scalar; |
| 8534 | } |
| 8535 | |
| 8536 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 8537 | } |
| 8538 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8539 | // Return true if the node is a constant or symbol whose reading has no |
| 8540 | // non-trivial observable cost or effect. |
| 8541 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 8542 | { |
| 8543 | // don't know what this is |
| 8544 | if (node == nullptr) |
| 8545 | return false; |
| 8546 | |
| 8547 | // a constant is safe |
| 8548 | if (node->getAsConstantUnion() != nullptr) |
| 8549 | return true; |
| 8550 | |
| 8551 | // not a symbol means non-trivial |
| 8552 | if (node->getAsSymbolNode() == nullptr) |
| 8553 | return false; |
| 8554 | |
| 8555 | // a symbol, depends on what's being read |
| 8556 | switch (node->getType().getQualifier().storage) { |
| 8557 | case glslang::EvqTemporary: |
| 8558 | case glslang::EvqGlobal: |
| 8559 | case glslang::EvqIn: |
| 8560 | case glslang::EvqInOut: |
| 8561 | case glslang::EvqConst: |
| 8562 | case glslang::EvqConstReadOnly: |
| 8563 | case glslang::EvqUniform: |
| 8564 | return true; |
| 8565 | default: |
| 8566 | return false; |
| 8567 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 8568 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8569 | |
| 8570 | // 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] | 8571 | // 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] | 8572 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8573 | // Return true if trivial. |
| 8574 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 8575 | { |
| 8576 | if (node == nullptr) |
| 8577 | return false; |
| 8578 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 8579 | // count non scalars as trivial, as well as anything coming from HLSL |
| 8580 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 8581 | return true; |
| 8582 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8583 | // symbols and constants are trivial |
| 8584 | if (isTrivialLeaf(node)) |
| 8585 | return true; |
| 8586 | |
| 8587 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 8588 | |
| 8589 | // not a simple operation |
| 8590 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 8591 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 8592 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 8593 | return false; |
| 8594 | |
| 8595 | // not on leaf nodes |
| 8596 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 8597 | return false; |
| 8598 | |
| 8599 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 8600 | return false; |
| 8601 | } |
| 8602 | |
| 8603 | switch (node->getAsOperator()->getOp()) { |
| 8604 | case glslang::EOpLogicalNot: |
| 8605 | case glslang::EOpConvIntToBool: |
| 8606 | case glslang::EOpConvUintToBool: |
| 8607 | case glslang::EOpConvFloatToBool: |
| 8608 | case glslang::EOpConvDoubleToBool: |
| 8609 | case glslang::EOpEqual: |
| 8610 | case glslang::EOpNotEqual: |
| 8611 | case glslang::EOpLessThan: |
| 8612 | case glslang::EOpGreaterThan: |
| 8613 | case glslang::EOpLessThanEqual: |
| 8614 | case glslang::EOpGreaterThanEqual: |
| 8615 | case glslang::EOpIndexDirect: |
| 8616 | case glslang::EOpIndexDirectStruct: |
| 8617 | case glslang::EOpLogicalXor: |
| 8618 | case glslang::EOpAny: |
| 8619 | case glslang::EOpAll: |
| 8620 | return true; |
| 8621 | default: |
| 8622 | return false; |
| 8623 | } |
| 8624 | } |
| 8625 | |
| 8626 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 8627 | // the left side is true (for &&) or false (for ||). |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8628 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, |
| 8629 | glslang::TIntermTyped& right) |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8630 | { |
| 8631 | spv::Id boolTypeId = builder.makeBoolType(); |
| 8632 | |
| 8633 | // emit left operand |
| 8634 | builder.clearAccessChain(); |
| 8635 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8636 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8637 | |
| 8638 | // Operands to accumulate OpPhi operands |
| 8639 | std::vector<spv::Id> phiOperands; |
| 8640 | // accumulate left operand's phi information |
| 8641 | phiOperands.push_back(leftId); |
| 8642 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8643 | |
| 8644 | // Make the two kinds of operation symmetric with a "!" |
| 8645 | // || => emit "if (! left) result = right" |
| 8646 | // && => emit "if ( left) result = right" |
| 8647 | // |
| 8648 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 8649 | // to 'builder' to have an "else" without an "then" |
| 8650 | if (op == glslang::EOpLogicalOr) |
| 8651 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 8652 | |
| 8653 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 8654 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8655 | |
| 8656 | // emit right operand as the "then" part of the "if" |
| 8657 | builder.clearAccessChain(); |
| 8658 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8659 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8660 | |
| 8661 | // accumulate left operand's phi information |
| 8662 | phiOperands.push_back(rightId); |
| 8663 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8664 | |
| 8665 | // finish the "if" |
| 8666 | ifBuilder.makeEndIf(); |
| 8667 | |
| 8668 | // phi together the two results |
| 8669 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 8670 | } |
| 8671 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8672 | #ifndef GLSLANG_WEB |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8673 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 8674 | // Import this set if it has not been imported yet. |
| 8675 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 8676 | { |
| 8677 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 8678 | return extBuiltinMap[name]; |
| 8679 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 8680 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8681 | spv::Id extBuiltins = builder.import(name); |
| 8682 | extBuiltinMap[name] = extBuiltins; |
| 8683 | return extBuiltins; |
| 8684 | } |
| 8685 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 8686 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8687 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8688 | }; // end anonymous namespace |
| 8689 | |
| 8690 | namespace glslang { |
| 8691 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8692 | void GetSpirvVersion(std::string& version) |
| 8693 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 8694 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 8695 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8696 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8697 | version = buf; |
| 8698 | } |
| 8699 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8700 | // For low-order part of the generator's magic number. Bump up |
| 8701 | // when there is a change in the style (e.g., if SSA form changes, |
| 8702 | // or a different instruction sequence to do something gets used). |
| 8703 | int GetSpirvGeneratorVersion() |
| 8704 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 8705 | // return 1; // start |
| 8706 | // 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] | 8707 | // 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] | 8708 | // 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] | 8709 | // 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] | 8710 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 8711 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
John Kessenich | 31c3370 | 2019-11-02 21:26:40 -0600 | [diff] [blame] | 8712 | // return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent |
John Kessenich | 3641ff7 | 2020-06-10 07:38:31 -0600 | [diff] [blame] | 8713 | // return 8; // switch to new dead block eliminator; use OpUnreachable |
Graeme Leese | 060882f | 2020-06-22 11:03:46 +0100 | [diff] [blame] | 8714 | // return 9; // don't include opaque function parameters in OpEntryPoint global's operand list |
| 8715 | return 10; // Generate OpFUnordNotEqual for != comparisons |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8716 | } |
| 8717 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8718 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8719 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8720 | { |
| 8721 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8722 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8723 | if (out.fail()) |
| 8724 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8725 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 8726 | unsigned int word = spirv[i]; |
| 8727 | out.write((const char*)&word, 4); |
| 8728 | } |
| 8729 | out.close(); |
| 8730 | } |
| 8731 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8732 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 8733 | 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] | 8734 | { |
Shahbaz Youssefi | 1ef2e25 | 2020-07-03 15:42:53 -0400 | [diff] [blame] | 8735 | #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE) |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8736 | std::ofstream out; |
| 8737 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8738 | if (out.fail()) |
| 8739 | printf("ERROR: Failed to open file: %s\n", baseName); |
Ben Clayton | fbe9a23 | 2020-06-17 11:17:19 +0100 | [diff] [blame] | 8740 | out << "\t// " << |
| 8741 | GetSpirvGeneratorVersion() << |
| 8742 | GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH << |
| 8743 | GLSLANG_VERSION_FLAVOR << std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8744 | if (varName != nullptr) { |
| 8745 | out << "\t #pragma once" << std::endl; |
| 8746 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 8747 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8748 | const int WORDS_PER_LINE = 8; |
| 8749 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 8750 | out << "\t"; |
| 8751 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 8752 | const unsigned int word = spirv[i + j]; |
| 8753 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 8754 | if (i + j + 1 < (int)spirv.size()) { |
| 8755 | out << ","; |
| 8756 | } |
| 8757 | } |
| 8758 | out << std::endl; |
| 8759 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8760 | if (varName != nullptr) { |
| 8761 | out << "};"; |
johnkslang | f881f08 | 2020-08-04 02:13:50 -0600 | [diff] [blame] | 8762 | out << std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8763 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8764 | out.close(); |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8765 | #endif |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8766 | } |
| 8767 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8768 | // |
| 8769 | // Set up the glslang traversal |
| 8770 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8771 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8772 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8773 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8774 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8775 | } |
| 8776 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8777 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8778 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8779 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8780 | TIntermNode* root = intermediate.getTreeRoot(); |
| 8781 | |
| 8782 | if (root == 0) |
| 8783 | return; |
| 8784 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8785 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8786 | if (options == nullptr) |
| 8787 | options = &defaultOptions; |
| 8788 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8789 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8790 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 8791 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8792 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 8793 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8794 | it.dumpSpv(spirv); |
| 8795 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 8796 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8797 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 8798 | // eg. forward and remove memory writes of opaque types. |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8799 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
Shahbaz Youssefi | d52dce5 | 2020-06-17 12:47:44 -0400 | [diff] [blame] | 8800 | if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) { |
| 8801 | SpirvToolsTransform(intermediate, spirv, logger, options); |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8802 | prelegalization = false; |
| 8803 | } |
Shahbaz Youssefi | d52dce5 | 2020-06-17 12:47:44 -0400 | [diff] [blame] | 8804 | else if (options->stripDebugInfo) { |
| 8805 | // Strip debug info even if optimization is disabled. |
| 8806 | SpirvToolsStripDebugInfo(intermediate, spirv, logger); |
| 8807 | } |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8808 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8809 | if (options->validate) |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8810 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8811 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8812 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8813 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8814 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8815 | #endif |
| 8816 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8817 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8818 | } |
| 8819 | |
| 8820 | }; // end namespace glslang |