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 | 8c49d15 | 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 | |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 717 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 718 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 719 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 720 | return spv::BuiltInViewportIndex; |
| 721 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 722 | case glslang::EbvSampleId: |
| 723 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 724 | return spv::BuiltInSampleId; |
| 725 | |
| 726 | case glslang::EbvSamplePosition: |
| 727 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 728 | return spv::BuiltInSamplePosition; |
| 729 | |
| 730 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 731 | return spv::BuiltInSampleMask; |
| 732 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 733 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 734 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 735 | return spv::BuiltInLayer; |
| 736 | } |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 737 | builder.addCapability(spv::CapabilityGeometry); |
| 738 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 739 | glslangIntermediate->getStage() == EShLangTessControl || |
| 740 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 741 | |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 742 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 743 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 744 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 745 | return spv::BuiltInLayer; |
| 746 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 747 | case glslang::EbvBaseVertex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 748 | 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] | 749 | builder.addCapability(spv::CapabilityDrawParameters); |
| 750 | return spv::BuiltInBaseVertex; |
| 751 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 752 | case glslang::EbvBaseInstance: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 753 | 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] | 754 | builder.addCapability(spv::CapabilityDrawParameters); |
| 755 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 756 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 757 | case glslang::EbvDrawId: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 758 | 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] | 759 | builder.addCapability(spv::CapabilityDrawParameters); |
| 760 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 761 | |
| 762 | case glslang::EbvPrimitiveId: |
| 763 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 764 | builder.addCapability(spv::CapabilityGeometry); |
| 765 | return spv::BuiltInPrimitiveId; |
| 766 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 767 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 768 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 769 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 770 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 771 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 772 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 773 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 774 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 775 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 776 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 777 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 778 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 779 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 780 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 781 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 782 | return spv::BuiltInSubgroupSize; |
| 783 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 784 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 785 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 786 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 787 | return spv::BuiltInSubgroupLocalInvocationId; |
| 788 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 789 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 790 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 791 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 792 | return spv::BuiltInSubgroupEqMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 793 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 794 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 795 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 796 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 797 | return spv::BuiltInSubgroupGeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 798 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 799 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 800 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 801 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 802 | return spv::BuiltInSubgroupGtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 803 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 804 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 805 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 806 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 807 | return spv::BuiltInSubgroupLeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 808 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 809 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 810 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 811 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 812 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 813 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 814 | case glslang::EbvNumSubgroups: |
| 815 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 816 | return spv::BuiltInNumSubgroups; |
| 817 | |
| 818 | case glslang::EbvSubgroupID: |
| 819 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 820 | return spv::BuiltInSubgroupId; |
| 821 | |
| 822 | case glslang::EbvSubgroupSize2: |
| 823 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 824 | return spv::BuiltInSubgroupSize; |
| 825 | |
| 826 | case glslang::EbvSubgroupInvocation2: |
| 827 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 828 | return spv::BuiltInSubgroupLocalInvocationId; |
| 829 | |
| 830 | case glslang::EbvSubgroupEqMask2: |
| 831 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 832 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 833 | return spv::BuiltInSubgroupEqMask; |
| 834 | |
| 835 | case glslang::EbvSubgroupGeMask2: |
| 836 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 837 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 838 | return spv::BuiltInSubgroupGeMask; |
| 839 | |
| 840 | case glslang::EbvSubgroupGtMask2: |
| 841 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 842 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 843 | return spv::BuiltInSubgroupGtMask; |
| 844 | |
| 845 | case glslang::EbvSubgroupLeMask2: |
| 846 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 847 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 848 | return spv::BuiltInSubgroupLeMask; |
| 849 | |
| 850 | case glslang::EbvSubgroupLtMask2: |
| 851 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 852 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 853 | return spv::BuiltInSubgroupLtMask; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 854 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 855 | case glslang::EbvBaryCoordNoPersp: |
| 856 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 857 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 858 | |
| 859 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 860 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 861 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 862 | |
| 863 | case glslang::EbvBaryCoordNoPerspSample: |
| 864 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 865 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 866 | |
| 867 | case glslang::EbvBaryCoordSmooth: |
| 868 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 869 | return spv::BuiltInBaryCoordSmoothAMD; |
| 870 | |
| 871 | case glslang::EbvBaryCoordSmoothCentroid: |
| 872 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 873 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 874 | |
| 875 | case glslang::EbvBaryCoordSmoothSample: |
| 876 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 877 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 878 | |
| 879 | case glslang::EbvBaryCoordPullModel: |
| 880 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 881 | return spv::BuiltInBaryCoordPullModelAMD; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 882 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 883 | case glslang::EbvDeviceIndex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 884 | builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 885 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 886 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 887 | |
| 888 | case glslang::EbvViewIndex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 889 | builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 890 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 891 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 892 | |
Daniel Koch | 5154db5 | 2018-11-26 10:01:58 -0500 | [diff] [blame] | 893 | case glslang::EbvFragSizeEXT: |
| 894 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 895 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 896 | return spv::BuiltInFragSizeEXT; |
| 897 | |
| 898 | case glslang::EbvFragInvocationCountEXT: |
| 899 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 900 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 901 | return spv::BuiltInFragInvocationCountEXT; |
| 902 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 903 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 904 | if (!memberDeclaration) { |
| 905 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 906 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 907 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 908 | return spv::BuiltInViewportMaskNV; |
| 909 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 910 | if (!memberDeclaration) { |
| 911 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 912 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 913 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 914 | return spv::BuiltInSecondaryPositionNV; |
| 915 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 916 | if (!memberDeclaration) { |
| 917 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 918 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 919 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 920 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 921 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 922 | if (!memberDeclaration) { |
| 923 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 924 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 925 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 926 | return spv::BuiltInPositionPerViewNV; |
| 927 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 928 | if (!memberDeclaration) { |
| 929 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 930 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 931 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 932 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 933 | case glslang::EbvFragFullyCoveredNV: |
| 934 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 935 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 936 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 937 | case glslang::EbvFragmentSizeNV: |
| 938 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 939 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 940 | return spv::BuiltInFragmentSizeNV; |
| 941 | case glslang::EbvInvocationsPerPixelNV: |
| 942 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 943 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 944 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 945 | |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 946 | // ray tracing |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 947 | case glslang::EbvLaunchId: |
| 948 | return spv::BuiltInLaunchIdKHR; |
| 949 | case glslang::EbvLaunchSize: |
| 950 | return spv::BuiltInLaunchSizeKHR; |
| 951 | case glslang::EbvWorldRayOrigin: |
| 952 | return spv::BuiltInWorldRayOriginKHR; |
| 953 | case glslang::EbvWorldRayDirection: |
| 954 | return spv::BuiltInWorldRayDirectionKHR; |
| 955 | case glslang::EbvObjectRayOrigin: |
| 956 | return spv::BuiltInObjectRayOriginKHR; |
| 957 | case glslang::EbvObjectRayDirection: |
| 958 | return spv::BuiltInObjectRayDirectionKHR; |
| 959 | case glslang::EbvRayTmin: |
| 960 | return spv::BuiltInRayTminKHR; |
| 961 | case glslang::EbvRayTmax: |
| 962 | return spv::BuiltInRayTmaxKHR; |
| 963 | case glslang::EbvInstanceCustomIndex: |
| 964 | return spv::BuiltInInstanceCustomIndexKHR; |
| 965 | case glslang::EbvHitT: |
| 966 | return spv::BuiltInHitTKHR; |
| 967 | case glslang::EbvHitKind: |
| 968 | return spv::BuiltInHitKindKHR; |
| 969 | case glslang::EbvObjectToWorld: |
| 970 | case glslang::EbvObjectToWorld3x4: |
| 971 | return spv::BuiltInObjectToWorldKHR; |
| 972 | case glslang::EbvWorldToObject: |
| 973 | case glslang::EbvWorldToObject3x4: |
| 974 | return spv::BuiltInWorldToObjectKHR; |
| 975 | case glslang::EbvIncomingRayFlags: |
| 976 | return spv::BuiltInIncomingRayFlagsKHR; |
| 977 | case glslang::EbvGeometryIndex: |
| 978 | return spv::BuiltInRayGeometryIndexKHR; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 979 | |
| 980 | // barycentrics |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 981 | case glslang::EbvBaryCoordNV: |
| 982 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 983 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 984 | return spv::BuiltInBaryCoordNV; |
| 985 | case glslang::EbvBaryCoordNoPerspNV: |
| 986 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 987 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 988 | return spv::BuiltInBaryCoordNoPerspNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 989 | |
| 990 | // mesh shaders |
| 991 | case glslang::EbvTaskCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 992 | return spv::BuiltInTaskCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 993 | case glslang::EbvPrimitiveCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 994 | return spv::BuiltInPrimitiveCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 995 | case glslang::EbvPrimitiveIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 996 | return spv::BuiltInPrimitiveIndicesNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 997 | case glslang::EbvClipDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 998 | return spv::BuiltInClipDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 999 | case glslang::EbvCullDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1000 | return spv::BuiltInCullDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1001 | case glslang::EbvLayerPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1002 | return spv::BuiltInLayerPerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1003 | case glslang::EbvMeshViewCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1004 | return spv::BuiltInMeshViewCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1005 | case glslang::EbvMeshViewIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1006 | return spv::BuiltInMeshViewIndicesNV; |
Daniel Koch | 2cb2f19 | 2019-06-04 08:43:32 -0400 | [diff] [blame] | 1007 | |
| 1008 | // sm builtins |
| 1009 | case glslang::EbvWarpsPerSM: |
| 1010 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1011 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1012 | return spv::BuiltInWarpsPerSMNV; |
| 1013 | case glslang::EbvSMCount: |
| 1014 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1015 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1016 | return spv::BuiltInSMCountNV; |
| 1017 | case glslang::EbvWarpID: |
| 1018 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1019 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1020 | return spv::BuiltInWarpIDNV; |
| 1021 | case glslang::EbvSMID: |
| 1022 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1023 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1024 | return spv::BuiltInSMIDNV; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1025 | #endif |
| 1026 | |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 1027 | default: |
| 1028 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1032 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1033 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1034 | { |
| 1035 | assert(type.getBasicType() == glslang::EbtSampler); |
| 1036 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1037 | #ifdef GLSLANG_WEB |
| 1038 | return spv::ImageFormatUnknown; |
| 1039 | #endif |
| 1040 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1041 | // Check for capabilities |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1042 | switch (type.getQualifier().getFormat()) { |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1043 | case glslang::ElfRg32f: |
| 1044 | case glslang::ElfRg16f: |
| 1045 | case glslang::ElfR11fG11fB10f: |
| 1046 | case glslang::ElfR16f: |
| 1047 | case glslang::ElfRgba16: |
| 1048 | case glslang::ElfRgb10A2: |
| 1049 | case glslang::ElfRg16: |
| 1050 | case glslang::ElfRg8: |
| 1051 | case glslang::ElfR16: |
| 1052 | case glslang::ElfR8: |
| 1053 | case glslang::ElfRgba16Snorm: |
| 1054 | case glslang::ElfRg16Snorm: |
| 1055 | case glslang::ElfRg8Snorm: |
| 1056 | case glslang::ElfR16Snorm: |
| 1057 | case glslang::ElfR8Snorm: |
| 1058 | |
| 1059 | case glslang::ElfRg32i: |
| 1060 | case glslang::ElfRg16i: |
| 1061 | case glslang::ElfRg8i: |
| 1062 | case glslang::ElfR16i: |
| 1063 | case glslang::ElfR8i: |
| 1064 | |
| 1065 | case glslang::ElfRgb10a2ui: |
| 1066 | case glslang::ElfRg32ui: |
| 1067 | case glslang::ElfRg16ui: |
| 1068 | case glslang::ElfRg8ui: |
| 1069 | case glslang::ElfR16ui: |
| 1070 | case glslang::ElfR8ui: |
| 1071 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 1072 | break; |
| 1073 | |
| 1074 | default: |
| 1075 | break; |
| 1076 | } |
| 1077 | |
| 1078 | // do the translation |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1079 | switch (type.getQualifier().getFormat()) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1080 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1081 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1082 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1083 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1084 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1085 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1086 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1087 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1088 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1089 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1090 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1091 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1092 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1093 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1094 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1095 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1096 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1097 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1098 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1099 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1100 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1101 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1102 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1103 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1104 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1105 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1106 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1107 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1108 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1109 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1110 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1111 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1112 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1113 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1114 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1115 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1116 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1117 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1118 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1119 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1120 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1124 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl( |
| 1125 | const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1126 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1127 | if (selectionNode.getFlatten()) |
| 1128 | return spv::SelectionControlFlattenMask; |
| 1129 | if (selectionNode.getDontFlatten()) |
| 1130 | return spv::SelectionControlDontFlattenMask; |
| 1131 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1132 | } |
| 1133 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1134 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) |
| 1135 | const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1136 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1137 | if (switchNode.getFlatten()) |
| 1138 | return spv::SelectionControlFlattenMask; |
| 1139 | if (switchNode.getDontFlatten()) |
| 1140 | return spv::SelectionControlDontFlattenMask; |
| 1141 | return spv::SelectionControlMaskNone; |
| 1142 | } |
| 1143 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1144 | // return a non-0 dependency if the dependency argument must be set |
| 1145 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1146 | std::vector<unsigned int>& operands) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1147 | { |
| 1148 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1149 | |
| 1150 | if (loopNode.getDontUnroll()) |
| 1151 | control = control | spv::LoopControlDontUnrollMask; |
| 1152 | if (loopNode.getUnroll()) |
| 1153 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1154 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1155 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1156 | else if (loopNode.getLoopDependency() > 0) { |
| 1157 | control = control | spv::LoopControlDependencyLengthMask; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1158 | operands.push_back((unsigned int)loopNode.getLoopDependency()); |
| 1159 | } |
| 1160 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 1161 | if (loopNode.getMinIterations() > 0) { |
| 1162 | control = control | spv::LoopControlMinIterationsMask; |
| 1163 | operands.push_back(loopNode.getMinIterations()); |
| 1164 | } |
| 1165 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
| 1166 | control = control | spv::LoopControlMaxIterationsMask; |
| 1167 | operands.push_back(loopNode.getMaxIterations()); |
| 1168 | } |
| 1169 | if (loopNode.getIterationMultiple() > 1) { |
| 1170 | control = control | spv::LoopControlIterationMultipleMask; |
| 1171 | operands.push_back(loopNode.getIterationMultiple()); |
| 1172 | } |
| 1173 | if (loopNode.getPeelCount() > 0) { |
| 1174 | control = control | spv::LoopControlPeelCountMask; |
| 1175 | operands.push_back(loopNode.getPeelCount()); |
| 1176 | } |
| 1177 | if (loopNode.getPartialCount() > 0) { |
| 1178 | control = control | spv::LoopControlPartialCountMask; |
| 1179 | operands.push_back(loopNode.getPartialCount()); |
| 1180 | } |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1181 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1182 | |
| 1183 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1184 | } |
| 1185 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1186 | // Translate glslang type to SPIR-V storage class. |
| 1187 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1188 | { |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 1189 | if (type.getBasicType() == glslang::EbtRayQuery) |
Neslisah Torosdagli | 054b5e3 | 2020-03-26 12:24:31 -0400 | [diff] [blame] | 1190 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1191 | if (type.getQualifier().isPipeInput()) |
| 1192 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1193 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1194 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1195 | |
| 1196 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1197 | type.getQualifier().storage == glslang::EvqUniform) { |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1198 | if (type.isAtomic()) |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1199 | return spv::StorageClassAtomicCounter; |
| 1200 | if (type.containsOpaque()) |
| 1201 | return spv::StorageClassUniformConstant; |
| 1202 | } |
| 1203 | |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1204 | if (type.getQualifier().isUniformOrBuffer() && |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1205 | type.getQualifier().isShaderRecord()) { |
| 1206 | return spv::StorageClassShaderRecordBufferKHR; |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1207 | } |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1208 | |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1209 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1210 | 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] | 1211 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1215 | if (type.getQualifier().isPushConstant()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1216 | return spv::StorageClassPushConstant; |
| 1217 | if (type.getBasicType() == glslang::EbtBlock) |
| 1218 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1219 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1220 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1221 | |
| 1222 | switch (type.getQualifier().storage) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 1223 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1224 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1225 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1226 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 1227 | #ifndef GLSLANG_WEB |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1228 | case glslang::EvqPayload: return spv::StorageClassRayPayloadKHR; |
| 1229 | case glslang::EvqPayloadIn: return spv::StorageClassIncomingRayPayloadKHR; |
| 1230 | case glslang::EvqHitAttr: return spv::StorageClassHitAttributeKHR; |
| 1231 | case glslang::EvqCallableData: return spv::StorageClassCallableDataKHR; |
| 1232 | case glslang::EvqCallableDataIn: return spv::StorageClassIncomingCallableDataKHR; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1233 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1234 | default: |
| 1235 | assert(0); |
| 1236 | break; |
| 1237 | } |
| 1238 | |
| 1239 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1240 | } |
| 1241 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1242 | // Add capabilities pertaining to how an array is indexed. |
| 1243 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1244 | const glslang::TType& indexType) |
| 1245 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1246 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1247 | if (indexType.getQualifier().isNonUniform()) { |
| 1248 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1249 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1250 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1251 | if (baseType.getQualifier().hasAttachment()) |
| 1252 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1253 | else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1254 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1255 | else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1256 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1257 | else if (baseType.isImage()) |
| 1258 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1259 | else if (baseType.isTexture()) |
| 1260 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1261 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1262 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1263 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1264 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1265 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1266 | } |
| 1267 | } else { |
| 1268 | // assume a dynamically uniform index |
| 1269 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1270 | if (baseType.getQualifier().hasAttachment()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1271 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1272 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1273 | } else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1274 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1275 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1276 | } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1277 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1278 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1279 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1280 | } |
| 1281 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1282 | #endif |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1283 | } |
| 1284 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1285 | // 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] | 1286 | // descriptor set. |
| 1287 | bool IsDescriptorResource(const glslang::TType& type) |
| 1288 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1289 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1290 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1291 | return type.getQualifier().isUniformOrBuffer() && |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1292 | ! type.getQualifier().isShaderRecord() && |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1293 | ! type.getQualifier().isPushConstant(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1294 | |
| 1295 | // non block... |
| 1296 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1297 | // if they are the global-scope-class, not the function parameter |
| 1298 | // (or local, if they ever exist) class. |
alelenv | 999d4fd | 2020-06-01 23:24:41 -0700 | [diff] [blame] | 1299 | if (type.getBasicType() == glslang::EbtSampler || |
| 1300 | type.getBasicType() == glslang::EbtAccStruct) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1301 | return type.getQualifier().isUniformOrBuffer(); |
| 1302 | |
| 1303 | // None of the above. |
| 1304 | return false; |
| 1305 | } |
| 1306 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1307 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1308 | { |
| 1309 | if (child.layoutMatrix == glslang::ElmNone) |
| 1310 | child.layoutMatrix = parent.layoutMatrix; |
| 1311 | |
| 1312 | if (parent.invariant) |
| 1313 | child.invariant = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1314 | if (parent.flat) |
| 1315 | child.flat = true; |
| 1316 | if (parent.centroid) |
| 1317 | child.centroid = true; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1318 | #ifndef GLSLANG_WEB |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1319 | if (parent.nopersp) |
| 1320 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1321 | if (parent.explicitInterp) |
| 1322 | child.explicitInterp = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1323 | if (parent.perPrimitiveNV) |
| 1324 | child.perPrimitiveNV = true; |
| 1325 | if (parent.perViewNV) |
| 1326 | child.perViewNV = true; |
| 1327 | if (parent.perTaskNV) |
| 1328 | child.perTaskNV = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1329 | if (parent.patch) |
| 1330 | child.patch = true; |
| 1331 | if (parent.sample) |
| 1332 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1333 | if (parent.coherent) |
| 1334 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1335 | if (parent.devicecoherent) |
| 1336 | child.devicecoherent = true; |
| 1337 | if (parent.queuefamilycoherent) |
| 1338 | child.queuefamilycoherent = true; |
| 1339 | if (parent.workgroupcoherent) |
| 1340 | child.workgroupcoherent = true; |
| 1341 | if (parent.subgroupcoherent) |
| 1342 | child.subgroupcoherent = true; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1343 | if (parent.shadercallcoherent) |
| 1344 | child.shadercallcoherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1345 | if (parent.nonprivate) |
| 1346 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1347 | if (parent.volatil) |
| 1348 | child.volatil = true; |
| 1349 | if (parent.restrict) |
| 1350 | child.restrict = true; |
| 1351 | if (parent.readonly) |
| 1352 | child.readonly = true; |
| 1353 | if (parent.writeonly) |
| 1354 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1355 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1358 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1359 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1360 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1361 | // - struct members might inherit from a struct declaration |
| 1362 | // (note that non-block structs don't explicitly inherit, |
| 1363 | // only implicitly, meaning no decoration involved) |
| 1364 | // - affect decorations on the struct members |
| 1365 | // (note smooth does not, and expecting something like volatile |
| 1366 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1367 | // - 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] | 1368 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1371 | // |
| 1372 | // Implement the TGlslangToSpvTraverser class. |
| 1373 | // |
| 1374 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1375 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, |
| 1376 | const glslang::TIntermediate* glslangIntermediate, |
| 1377 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) : |
| 1378 | TIntermTraverser(true, false, true), |
| 1379 | options(options), |
| 1380 | shaderEntry(nullptr), currentFunction(nullptr), |
| 1381 | sequenceDepth(0), logger(buildLogger), |
| 1382 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
| 1383 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
| 1384 | glslangIntermediate(glslangIntermediate), |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 1385 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()), |
| 1386 | nonSemanticDebugPrintf(0) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1387 | { |
| 1388 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1389 | |
| 1390 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1391 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1392 | glslangIntermediate->getVersion()); |
| 1393 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1394 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1395 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1396 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1397 | |
| 1398 | // Set the source shader's text. If for SPV version 1.0, include |
| 1399 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1400 | // Otherwise, emit those as actual instructions. |
| 1401 | std::string text; |
| 1402 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1403 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1404 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1405 | text.append("// OpModuleProcessed "); |
| 1406 | text.append(processes[p]); |
| 1407 | text.append("\n"); |
| 1408 | } else |
| 1409 | builder.addModuleProcessed(processes[p]); |
| 1410 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1411 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1412 | text.append("#line 1\n"); |
| 1413 | text.append(glslangIntermediate->getSourceText()); |
| 1414 | builder.setSourceText(text); |
Greg Fischer | d445bb2 | 2018-12-06 11:13:15 -0700 | [diff] [blame] | 1415 | // Pass name and text for all included files |
| 1416 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1417 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1418 | builder.addInclude(iItr->first, iItr->second); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1419 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1420 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1421 | |
| 1422 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1423 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1424 | |
| 1425 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1426 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
John Kessenich | 1ff0c18 | 2019-10-10 12:01:13 -0600 | [diff] [blame] | 1427 | 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] | 1428 | builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1429 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1430 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1431 | memoryModel = spv::MemoryModelVulkanKHR; |
| 1432 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1433 | 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] | 1434 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1435 | builder.setMemoryModel(addressingModel, memoryModel); |
| 1436 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 1437 | if (glslangIntermediate->usingVariablePointers()) { |
| 1438 | builder.addCapability(spv::CapabilityVariablePointers); |
| 1439 | } |
| 1440 | |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1441 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1442 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1443 | |
| 1444 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1445 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1446 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
Pankaj Mistry | 2a8ead2 | 2020-04-26 17:52:31 -0700 | [diff] [blame] | 1447 | builder.addSourceExtension(it->first.c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1448 | |
| 1449 | // Add the top-level modes for this shader. |
| 1450 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1451 | if (glslangIntermediate->getXfbMode()) { |
| 1452 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1453 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1454 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1455 | |
alelenv | 59216d5 | 2020-05-21 04:38:41 -0700 | [diff] [blame] | 1456 | if (glslangIntermediate->getLayoutPrimitiveCulling()) { |
alelenv | 75de196 | 2020-04-08 21:09:20 -0700 | [diff] [blame] | 1457 | builder.addCapability(spv::CapabilityRayTraversalPrimitiveCullingProvisionalKHR); |
| 1458 | } |
| 1459 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1460 | unsigned int mode; |
| 1461 | switch (glslangIntermediate->getStage()) { |
| 1462 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1463 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1464 | break; |
| 1465 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1466 | case EShLangFragment: |
| 1467 | builder.addCapability(spv::CapabilityShader); |
| 1468 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1469 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
| 1470 | |
| 1471 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1472 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
| 1473 | else |
| 1474 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
| 1475 | |
| 1476 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1477 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1478 | |
| 1479 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1480 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1481 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1482 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1483 | } |
| 1484 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1485 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1486 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
| 1487 | |
| 1488 | #ifndef GLSLANG_WEB |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 1489 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1490 | switch(glslangIntermediate->getDepth()) { |
| 1491 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1492 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
| 1493 | default: mode = spv::ExecutionModeMax; break; |
| 1494 | } |
| 1495 | if (mode != spv::ExecutionModeMax) |
| 1496 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1497 | switch (glslangIntermediate->getInterlockOrdering()) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 1498 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; |
| 1499 | break; |
| 1500 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; |
| 1501 | break; |
| 1502 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; |
| 1503 | break; |
| 1504 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; |
| 1505 | break; |
| 1506 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; |
| 1507 | break; |
| 1508 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; |
| 1509 | break; |
| 1510 | default: mode = spv::ExecutionModeMax; |
| 1511 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1512 | } |
| 1513 | if (mode != spv::ExecutionModeMax) { |
| 1514 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1515 | if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
| 1516 | mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
| 1517 | builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
| 1518 | } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
| 1519 | mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
| 1520 | builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT); |
| 1521 | } else { |
| 1522 | builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT); |
| 1523 | } |
| 1524 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 1525 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1526 | #endif |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 1527 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1528 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1529 | case EShLangCompute: |
| 1530 | builder.addCapability(spv::CapabilityShader); |
| 1531 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1532 | glslangIntermediate->getLocalSize(1), |
| 1533 | glslangIntermediate->getLocalSize(2)); |
| 1534 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1535 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1536 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1537 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1538 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1539 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1540 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1541 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1542 | } |
| 1543 | break; |
John Kessenich | 51ed01c | 2019-10-10 11:40:11 -0600 | [diff] [blame] | 1544 | #ifndef GLSLANG_WEB |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1545 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1546 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1547 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1548 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1549 | glslang::TLayoutGeometry primitive; |
| 1550 | |
| 1551 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1552 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1553 | glslangIntermediate->getVertices()); |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1554 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1555 | } else { |
| 1556 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1557 | } |
| 1558 | |
| 1559 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1560 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1561 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1562 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1563 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1564 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1565 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1566 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1567 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1568 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1569 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1570 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1571 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1572 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1573 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1574 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1575 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1576 | |
| 1577 | switch (glslangIntermediate->getVertexOrder()) { |
| 1578 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1579 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1580 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1581 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1582 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1583 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1584 | |
| 1585 | if (glslangIntermediate->getPointMode()) |
| 1586 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1587 | break; |
| 1588 | |
| 1589 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1590 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1591 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1592 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1593 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1594 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1595 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1596 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1597 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1598 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1599 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1600 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1601 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1602 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1603 | |
| 1604 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1605 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1606 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1607 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1608 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1609 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1610 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1611 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1612 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1613 | break; |
| 1614 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1615 | case EShLangRayGen: |
| 1616 | case EShLangIntersect: |
| 1617 | case EShLangAnyHit: |
| 1618 | case EShLangClosestHit: |
| 1619 | case EShLangMiss: |
| 1620 | case EShLangCallable: |
| 1621 | { |
| 1622 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 1623 | if (extensions.find("GL_NV_ray_tracing") == extensions.end()) { |
| 1624 | builder.addCapability(spv::CapabilityRayTracingProvisionalKHR); |
| 1625 | builder.addExtension("SPV_KHR_ray_tracing"); |
| 1626 | } |
| 1627 | else { |
| 1628 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1629 | builder.addExtension("SPV_NV_ray_tracing"); |
| 1630 | } |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1631 | break; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1632 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1633 | case EShLangTaskNV: |
| 1634 | case EShLangMeshNV: |
| 1635 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1636 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1637 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1638 | glslangIntermediate->getLocalSize(1), |
| 1639 | glslangIntermediate->getLocalSize(2)); |
| 1640 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1641 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1642 | glslangIntermediate->getVertices()); |
| 1643 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, |
| 1644 | glslangIntermediate->getPrimitives()); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1645 | |
| 1646 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1647 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1648 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1649 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1650 | default: mode = spv::ExecutionModeMax; break; |
| 1651 | } |
| 1652 | if (mode != spv::ExecutionModeMax) |
| 1653 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1654 | } |
| 1655 | break; |
| 1656 | #endif |
| 1657 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1658 | default: |
| 1659 | break; |
| 1660 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1661 | } |
| 1662 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1663 | // Finish creating SPV, after the traversal is complete. |
| 1664 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1665 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1666 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1667 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1668 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1669 | builder.leaveFunction(); |
| 1670 | } |
| 1671 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1672 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1673 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1674 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1675 | |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 1676 | // Add capabilities, extensions, remove unneeded decorations, etc., |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1677 | // based on the resulting SPIR-V. |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 1678 | // Note: WebGPU code generation must have the opportunity to aggressively |
| 1679 | // prune unreachable merge blocks and continue targets. |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1680 | builder.postProcess(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1683 | // Write the SPV into 'out'. |
| 1684 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1685 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1686 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | // |
| 1690 | // Implement the traversal functions. |
| 1691 | // |
| 1692 | // Return true from interior nodes to have the external traversal |
| 1693 | // continue on to children. Return false if children were |
| 1694 | // already processed. |
| 1695 | // |
| 1696 | |
| 1697 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1698 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1699 | // - uniform/input reads |
| 1700 | // - output writes |
| 1701 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1702 | // - something simple that degenerates into the last bullet |
| 1703 | // |
| 1704 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1705 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1706 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1707 | if (symbol->getType().isStruct()) |
| 1708 | glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId(); |
| 1709 | |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1710 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1711 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1712 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1713 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1714 | // Formal function parameters were mapped during makeFunctions(). |
| 1715 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1716 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1717 | if (builder.isPointer(id)) { |
John Kessenich | c30d335 | 2020-06-10 07:15:24 -0600 | [diff] [blame] | 1718 | if (!symbol->getType().getQualifier().isParamInput() && |
| 1719 | !symbol->getType().getQualifier().isParamOutput()) { |
| 1720 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1721 | // Consider adding to the OpEntryPoint interface list. |
| 1722 | // Only looking at structures if they have at least one member. |
| 1723 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
| 1724 | spv::StorageClass sc = builder.getStorageClass(id); |
| 1725 | // Before SPIR-V 1.4, we only want to include Input and Output. |
| 1726 | // Starting with SPIR-V 1.4, we want all globals. |
| 1727 | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && sc != spv::StorageClassFunction) || |
| 1728 | (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
| 1729 | iOSet.insert(id); |
| 1730 | } |
John Kessenich | 7c7731e | 2019-01-04 16:47:06 +0700 | [diff] [blame] | 1731 | } |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1732 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1733 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1734 | // If the SPIR-V type is required to be different than the AST type |
| 1735 | // (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices), |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1736 | // translate now from the SPIR-V type to the AST type, for the consuming |
| 1737 | // operation. |
| 1738 | // Note this turns it from an l-value to an r-value. |
| 1739 | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
| 1740 | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
| 1741 | id = translateForcedType(id); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1745 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1746 | // Prepare to generate code for the access |
| 1747 | |
| 1748 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1749 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1750 | // followed by setting the base. |
| 1751 | builder.clearAccessChain(); |
| 1752 | |
| 1753 | // 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] | 1754 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1755 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1756 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1757 | // 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] | 1758 | // These are also pure R-values. |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1759 | // C) R-Values from type translation, see above call to translateForcedType() |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1760 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1761 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
| 1762 | !builder.isPointerType(builder.getTypeId(id))) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1763 | builder.setAccessChainRValue(id); |
| 1764 | else |
| 1765 | builder.setAccessChainLValue(id); |
| 1766 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1767 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1768 | #ifdef ENABLE_HLSL |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1769 | // Process linkage-only nodes for any special additional interface work. |
| 1770 | if (linkageOnly) { |
| 1771 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1772 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1773 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1774 | // of declaration. |
| 1775 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1776 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1777 | // Save possible originating buffers for counter buffers, keyed by |
| 1778 | // making the potential counter-buffer name. |
| 1779 | std::string keyName = symbol->getName().c_str(); |
| 1780 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1781 | counterOriginator[keyName] = symbol; |
| 1782 | } else { |
| 1783 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1784 | std::string keyName = symbol->getName().c_str(); |
| 1785 | auto it = counterOriginator.find(keyName); |
| 1786 | if (it != counterOriginator.end()) { |
| 1787 | id = getSymbolId(it->second); |
| 1788 | if (id != spv::NoResult) { |
| 1789 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1790 | if (counterId != spv::NoResult) { |
| 1791 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1792 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1793 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | } |
| 1799 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 1800 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1804 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1805 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1806 | if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) { |
| 1807 | glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId(); |
| 1808 | } |
| 1809 | if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) { |
| 1810 | glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId(); |
| 1811 | } |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1812 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1813 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1814 | if (node->getType().getQualifier().isSpecConstant()) |
| 1815 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1816 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1817 | // First, handle special cases |
| 1818 | switch (node->getOp()) { |
| 1819 | case glslang::EOpAssign: |
| 1820 | case glslang::EOpAddAssign: |
| 1821 | case glslang::EOpSubAssign: |
| 1822 | case glslang::EOpMulAssign: |
| 1823 | case glslang::EOpVectorTimesMatrixAssign: |
| 1824 | case glslang::EOpVectorTimesScalarAssign: |
| 1825 | case glslang::EOpMatrixTimesScalarAssign: |
| 1826 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1827 | case glslang::EOpDivAssign: |
| 1828 | case glslang::EOpModAssign: |
| 1829 | case glslang::EOpAndAssign: |
| 1830 | case glslang::EOpInclusiveOrAssign: |
| 1831 | case glslang::EOpExclusiveOrAssign: |
| 1832 | case glslang::EOpLeftShiftAssign: |
| 1833 | case glslang::EOpRightShiftAssign: |
| 1834 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1835 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1836 | // says to evaluate the left before the right. So, always, left |
| 1837 | // node then right node. |
| 1838 | { |
| 1839 | // get the left l-value, save it away |
| 1840 | builder.clearAccessChain(); |
| 1841 | node->getLeft()->traverse(this); |
| 1842 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1843 | |
| 1844 | // evaluate the right |
| 1845 | builder.clearAccessChain(); |
| 1846 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1847 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1848 | |
| 1849 | if (node->getOp() != glslang::EOpAssign) { |
| 1850 | // the left is also an r-value |
| 1851 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1852 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1853 | |
| 1854 | // do the operation |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1855 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1856 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1857 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1858 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1859 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1860 | node->getType().getBasicType()); |
| 1861 | |
| 1862 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1863 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | // store the result |
| 1867 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1868 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1869 | |
| 1870 | // assignments are expressions having an rValue after they are evaluated... |
| 1871 | builder.clearAccessChain(); |
| 1872 | builder.setAccessChainRValue(rValue); |
| 1873 | } |
| 1874 | return false; |
| 1875 | case glslang::EOpIndexDirect: |
| 1876 | case glslang::EOpIndexDirectStruct: |
| 1877 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1878 | // Structure, array, matrix, or vector indirection with statically known index. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1879 | // Get the left part of the access chain. |
| 1880 | node->getLeft()->traverse(this); |
| 1881 | |
| 1882 | // Add the next element in the chain |
| 1883 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1884 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1885 | if (! node->getLeft()->getType().isArray() && |
| 1886 | node->getLeft()->getType().isVector() && |
| 1887 | node->getOp() == glslang::EOpIndexDirect) { |
| 1888 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1889 | // so short circuit the access-chain stuff with a swizzle. |
| 1890 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1891 | swizzle.push_back(glslangIndex); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1892 | int dummySize; |
| 1893 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1894 | TranslateCoherent(node->getLeft()->getType()), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1895 | glslangIntermediate->getBaseAlignmentScalar( |
| 1896 | node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1897 | } else { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1898 | |
| 1899 | // Load through a block reference is performed with a dot operator that |
| 1900 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 1901 | // do a load and reset the access chain. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1902 | if (node->getLeft()->isReference() && |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1903 | !node->getLeft()->getType().isArray() && |
| 1904 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1905 | { |
| 1906 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 1907 | builder.clearAccessChain(); |
| 1908 | builder.setAccessChainLValue(left); |
| 1909 | } |
| 1910 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1911 | int spvIndex = glslangIndex; |
| 1912 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1913 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1914 | { |
| 1915 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1916 | // index remapping due to hidden members in anonymous blocks. |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1917 | int glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()]; |
| 1918 | if (memberRemapper.find(glslangId) != memberRemapper.end()) { |
| 1919 | std::vector<int>& remapper = memberRemapper[glslangId]; |
| 1920 | assert(remapper.size() > 0); |
| 1921 | spvIndex = remapper[glslangIndex]; |
| 1922 | } |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1923 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1924 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1925 | // normal case for indexing array or structure or block |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1926 | builder.accessChainPush(builder.makeIntConstant(spvIndex), |
| 1927 | TranslateCoherent(node->getLeft()->getType()), |
| 1928 | node->getLeft()->getType().getBufferReferenceAlignment()); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1929 | |
| 1930 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1931 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1932 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1933 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | return false; |
| 1937 | case glslang::EOpIndexIndirect: |
| 1938 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1939 | // Array, matrix, or vector indirection with variable index. |
| 1940 | // Will use native SPIR-V access-chain for and array indirection; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1941 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1942 | // Will use the access chain's 'component' for variable index into a vector. |
| 1943 | |
| 1944 | // This adapter is building access chains left to right. |
| 1945 | // Set up the access chain to the left. |
| 1946 | node->getLeft()->traverse(this); |
| 1947 | |
| 1948 | // save it so that computing the right side doesn't trash it |
| 1949 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1950 | |
| 1951 | // compute the next index in the chain |
| 1952 | builder.clearAccessChain(); |
| 1953 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1954 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1955 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1956 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1957 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1958 | // restore the saved access chain |
| 1959 | builder.setAccessChain(partial); |
| 1960 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1961 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 1962 | int dummySize; |
| 1963 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1964 | TranslateCoherent(node->getLeft()->getType()), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1965 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 1966 | dummySize)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1967 | } else |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1968 | builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), |
| 1969 | node->getLeft()->getType().getBufferReferenceAlignment()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1970 | } |
| 1971 | return false; |
| 1972 | case glslang::EOpVectorSwizzle: |
| 1973 | { |
| 1974 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1975 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1976 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1977 | int dummySize; |
| 1978 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1979 | TranslateCoherent(node->getLeft()->getType()), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1980 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 1981 | dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1982 | } |
| 1983 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1984 | case glslang::EOpMatrixSwizzle: |
| 1985 | logger->missingFunctionality("matrix swizzle"); |
| 1986 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1987 | case glslang::EOpLogicalOr: |
| 1988 | case glslang::EOpLogicalAnd: |
| 1989 | { |
| 1990 | |
| 1991 | // These may require short circuiting, but can sometimes be done as straight |
| 1992 | // binary operations. The right operand must be short circuited if it has |
| 1993 | // side effects, and should probably be if it is complex. |
| 1994 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1995 | break; // handle below as a normal binary operation |
| 1996 | // otherwise, we need to do dynamic short circuiting on the right operand |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1997 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), |
| 1998 | *node->getRight()->getAsTyped()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1999 | builder.clearAccessChain(); |
| 2000 | builder.setAccessChainRValue(result); |
| 2001 | } |
| 2002 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2003 | default: |
| 2004 | break; |
| 2005 | } |
| 2006 | |
| 2007 | // Assume generic binary op... |
| 2008 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2009 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2010 | builder.clearAccessChain(); |
| 2011 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2012 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2013 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2014 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2015 | builder.clearAccessChain(); |
| 2016 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2017 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2018 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2019 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2020 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2021 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2022 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2023 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2024 | convertGlslangToSpvType(node->getType()), left, right, |
| 2025 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2026 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2027 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2028 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2029 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2030 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2031 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2032 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2033 | return false; |
| 2034 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2035 | } |
| 2036 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2037 | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
| 2038 | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
| 2039 | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2040 | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn, |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2041 | const glslang::TType& glslangType) |
| 2042 | { |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2043 | switch(glslangBuiltIn) |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2044 | { |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2045 | case glslang::EbvSubGroupEqMask: |
| 2046 | case glslang::EbvSubGroupGeMask: |
| 2047 | case glslang::EbvSubGroupGtMask: |
| 2048 | case glslang::EbvSubGroupLeMask: |
| 2049 | case glslang::EbvSubGroupLtMask: { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2050 | // these require changing a 64-bit scaler -> a vector of 32-bit components |
| 2051 | if (glslangType.isVector()) |
| 2052 | break; |
| 2053 | std::pair<spv::Id, spv::Id> ret(builder.makeVectorType(builder.makeUintType(32), 4), |
| 2054 | builder.makeUintType(64)); |
| 2055 | return ret; |
| 2056 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2057 | // There are no SPIR-V builtins defined for these and map onto original non-transposed |
| 2058 | // builtins. During visitBinary we insert a transpose |
| 2059 | case glslang::EbvWorldToObject3x4: |
| 2060 | case glslang::EbvObjectToWorld3x4: { |
| 2061 | std::pair<spv::Id, spv::Id> ret(builder.makeMatrixType(builder.makeFloatType(32), 4, 3), |
| 2062 | builder.makeMatrixType(builder.makeFloatType(32), 3, 4) |
| 2063 | ); |
| 2064 | return ret; |
| 2065 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2066 | default: |
| 2067 | break; |
| 2068 | } |
| 2069 | |
| 2070 | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
| 2071 | return ret; |
| 2072 | } |
| 2073 | |
| 2074 | // For an object previously identified (see getForcedType() and forceType) |
| 2075 | // as needing type translations, do the translation needed for a load, turning |
| 2076 | // an L-value into in R-value. |
| 2077 | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
| 2078 | { |
| 2079 | const auto forceIt = forceType.find(object); |
| 2080 | if (forceIt == forceType.end()) |
| 2081 | return object; |
| 2082 | |
| 2083 | spv::Id desiredTypeId = forceIt->second; |
| 2084 | spv::Id objectTypeId = builder.getTypeId(object); |
| 2085 | assert(builder.isPointerType(objectTypeId)); |
| 2086 | objectTypeId = builder.getContainedTypeId(objectTypeId); |
| 2087 | if (builder.isVectorType(objectTypeId) && |
| 2088 | builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
| 2089 | if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
| 2090 | // handle 32-bit v.xy* -> 64-bit |
| 2091 | builder.clearAccessChain(); |
| 2092 | builder.setAccessChainLValue(object); |
| 2093 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 2094 | std::vector<spv::Id> components; |
| 2095 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
| 2096 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
| 2097 | |
| 2098 | spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
| 2099 | return builder.createUnaryOp(spv::OpBitcast, desiredTypeId, |
| 2100 | builder.createCompositeConstruct(vecType, components)); |
| 2101 | } else { |
| 2102 | logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar"); |
| 2103 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2104 | } else if (builder.isMatrixType(objectTypeId)) { |
| 2105 | // There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject |
| 2106 | // and we insert a transpose after loading the original non-transposed builtins |
| 2107 | builder.clearAccessChain(); |
| 2108 | builder.setAccessChainLValue(object); |
| 2109 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 2110 | return builder.createUnaryOp(spv::OpTranspose, desiredTypeId, object); |
| 2111 | |
| 2112 | } else { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2113 | logger->missingFunctionality("forcing non 32-bit vector type"); |
| 2114 | } |
| 2115 | |
| 2116 | return object; |
| 2117 | } |
| 2118 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2119 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 2120 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2121 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2122 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 2123 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2124 | if (node->getType().getQualifier().isSpecConstant()) |
| 2125 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2126 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2127 | spv::Id result = spv::NoResult; |
| 2128 | |
| 2129 | // try texturing first |
| 2130 | result = createImageTextureFunctionCall(node); |
| 2131 | if (result != spv::NoResult) { |
| 2132 | builder.clearAccessChain(); |
| 2133 | builder.setAccessChainRValue(result); |
| 2134 | |
| 2135 | return false; // done with this node |
| 2136 | } |
| 2137 | |
| 2138 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2139 | |
| 2140 | if (node->getOp() == glslang::EOpArrayLength) { |
| 2141 | // Quite special; won't want to evaluate the operand. |
| 2142 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2143 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 2144 | // except for the last block membeor of an SSBO. |
| 2145 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 2146 | // size extracted. |
| 2147 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2148 | // Normal .length() would have been constant folded by the front-end. |
| 2149 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 2150 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2151 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2152 | spv::Id length; |
| 2153 | if (node->getOperand()->getType().isCoopMat()) { |
| 2154 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2155 | |
| 2156 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
| 2157 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2158 | |
| 2159 | length = builder.createCooperativeMatrixLength(typeId); |
| 2160 | } else { |
| 2161 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2162 | block->traverse(this); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2163 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion() |
| 2164 | ->getConstArray()[0].getUConst(); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2165 | length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
| 2166 | } |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2167 | |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2168 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 2169 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 2170 | // AST expectation of a signed result. |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2171 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
| 2172 | if (builder.isInSpecConstCodeGenMode()) { |
| 2173 | length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0)); |
| 2174 | } else { |
| 2175 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 2176 | } |
| 2177 | } |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2178 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2179 | builder.clearAccessChain(); |
| 2180 | builder.setAccessChainRValue(length); |
| 2181 | |
| 2182 | return false; |
| 2183 | } |
| 2184 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2185 | // Start by evaluating the operand |
| 2186 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2187 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2188 | // operate first on the swizzle base, then apply the swizzle. |
| 2189 | spv::Id invertedType = spv::NoType; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2190 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2191 | invertedType : convertGlslangToSpvType(node->getType()); }; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2192 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 2193 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 2194 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2195 | builder.clearAccessChain(); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2196 | TIntermNode *operandNode; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2197 | if (invertedType != spv::NoType) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2198 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2199 | else |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2200 | operandNode = node->getOperand(); |
| 2201 | |
| 2202 | operandNode->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 2203 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2204 | spv::Id operand = spv::NoResult; |
| 2205 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2206 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2207 | |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2208 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2209 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 2210 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2211 | node->getOp() == glslang::EOpAtomicCounter || |
Neslisah Torosdagli | 7d37a68 | 2020-03-26 10:52:33 -0400 | [diff] [blame] | 2212 | node->getOp() == glslang::EOpInterpolateAtCentroid || |
| 2213 | node->getOp() == glslang::EOpRayQueryProceed || |
| 2214 | node->getOp() == glslang::EOpRayQueryGetRayTMin || |
| 2215 | node->getOp() == glslang::EOpRayQueryGetRayFlags || |
| 2216 | node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin || |
| 2217 | node->getOp() == glslang::EOpRayQueryGetWorldRayDirection || |
| 2218 | node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque || |
| 2219 | node->getOp() == glslang::EOpRayQueryTerminate || |
| 2220 | node->getOp() == glslang::EOpRayQueryConfirmIntersection) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2221 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2222 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2223 | lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
| 2224 | } else |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2225 | #endif |
| 2226 | { |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2227 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2228 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2229 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2230 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2231 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2232 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2233 | |
| 2234 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2235 | if (! result) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2236 | result = createConversion(node->getOp(), decorations, resultType(), operand, |
| 2237 | node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2238 | |
| 2239 | // if not, then possibly an operation |
| 2240 | if (! result) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2241 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, |
| 2242 | node->getOperand()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2243 | |
| 2244 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2245 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2246 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2247 | decorations.addNonUniform(builder, result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2248 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2249 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2250 | builder.clearAccessChain(); |
| 2251 | builder.setAccessChainRValue(result); |
| 2252 | |
| 2253 | return false; // done with this node |
| 2254 | } |
| 2255 | |
| 2256 | // it must be a special case, check... |
| 2257 | switch (node->getOp()) { |
| 2258 | case glslang::EOpPostIncrement: |
| 2259 | case glslang::EOpPostDecrement: |
| 2260 | case glslang::EOpPreIncrement: |
| 2261 | case glslang::EOpPreDecrement: |
| 2262 | { |
| 2263 | // 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] | 2264 | spv::Id one = 0; |
| 2265 | if (node->getBasicType() == glslang::EbtFloat) |
| 2266 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2267 | #ifndef GLSLANG_WEB |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 2268 | else if (node->getBasicType() == glslang::EbtDouble) |
| 2269 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2270 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 2271 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2272 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 2273 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2274 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 2275 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2276 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 2277 | one = builder.makeInt64Constant(1); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2278 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2279 | else |
| 2280 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2281 | glslang::TOperator op; |
| 2282 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2283 | node->getOp() == glslang::EOpPostIncrement) |
| 2284 | op = glslang::EOpAdd; |
| 2285 | else |
| 2286 | op = glslang::EOpSub; |
| 2287 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2288 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2289 | convertGlslangToSpvType(node->getType()), operand, one, |
| 2290 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2291 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2292 | |
| 2293 | // The result of operation is always stored, but conditionally the |
| 2294 | // consumed result. The consumed result is always an r-value. |
| 2295 | builder.accessChainStore(result); |
| 2296 | builder.clearAccessChain(); |
| 2297 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2298 | node->getOp() == glslang::EOpPreDecrement) |
| 2299 | builder.setAccessChainRValue(result); |
| 2300 | else |
| 2301 | builder.setAccessChainRValue(operand); |
| 2302 | } |
| 2303 | |
| 2304 | return false; |
| 2305 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2306 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2307 | case glslang::EOpEmitStreamVertex: |
| 2308 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 2309 | return false; |
| 2310 | case glslang::EOpEndStreamPrimitive: |
| 2311 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 2312 | return false; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2313 | case glslang::EOpRayQueryTerminate: |
| 2314 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operand); |
| 2315 | return false; |
| 2316 | case glslang::EOpRayQueryConfirmIntersection: |
| 2317 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operand); |
| 2318 | return false; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2319 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2320 | |
| 2321 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2322 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2323 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2324 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2325 | } |
| 2326 | |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2327 | // Construct a composite object, recursively copying members if their types don't match |
| 2328 | spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
| 2329 | { |
| 2330 | for (int c = 0; c < (int)constituents.size(); ++c) { |
| 2331 | spv::Id& constituent = constituents[c]; |
| 2332 | spv::Id lType = builder.getContainedTypeId(resultTypeId, c); |
| 2333 | spv::Id rType = builder.getTypeId(constituent); |
| 2334 | if (lType != rType) { |
| 2335 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 2336 | constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent); |
| 2337 | } else if (builder.isStructType(rType)) { |
| 2338 | std::vector<spv::Id> rTypeConstituents; |
| 2339 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2340 | for (int i = 0; i < numrTypeConstituents; ++i) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2341 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, |
| 2342 | builder.getContainedTypeId(rType, i), i)); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2343 | } |
| 2344 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2345 | } else { |
| 2346 | assert(builder.isArrayType(rType)); |
| 2347 | std::vector<spv::Id> rTypeConstituents; |
| 2348 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2349 | |
| 2350 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2351 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2352 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i)); |
| 2353 | } |
| 2354 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2355 | } |
| 2356 | } |
| 2357 | } |
| 2358 | return builder.createCompositeConstruct(resultTypeId, constituents); |
| 2359 | } |
| 2360 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2361 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2362 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 2363 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2364 | if (node->getType().getQualifier().isSpecConstant()) |
| 2365 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2366 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2367 | spv::Id result = spv::NoResult; |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 2368 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2369 | std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for |
| 2370 | // SPIR-V, for an out parameter |
| 2371 | std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2372 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2373 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2374 | invertedType : |
| 2375 | convertGlslangToSpvType(node->getType()); }; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2376 | |
| 2377 | // try texturing |
| 2378 | result = createImageTextureFunctionCall(node); |
| 2379 | if (result != spv::NoResult) { |
| 2380 | builder.clearAccessChain(); |
| 2381 | builder.setAccessChainRValue(result); |
| 2382 | |
| 2383 | return false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2384 | } |
| 2385 | #ifndef GLSLANG_WEB |
| 2386 | else if (node->getOp() == glslang::EOpImageStore || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2387 | node->getOp() == glslang::EOpImageStoreLod || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2388 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2389 | // "imageStore" is a special case, which has no result |
| 2390 | return false; |
| 2391 | } |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2392 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2393 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2394 | glslang::TOperator binOp = glslang::EOpNull; |
| 2395 | bool reduceComparison = true; |
| 2396 | bool isMatrix = false; |
| 2397 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2398 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2399 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2400 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2401 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2402 | assert(node->getOp()); |
| 2403 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 2404 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2405 | |
| 2406 | switch (node->getOp()) { |
| 2407 | case glslang::EOpSequence: |
| 2408 | { |
| 2409 | if (preVisit) |
| 2410 | ++sequenceDepth; |
| 2411 | else |
| 2412 | --sequenceDepth; |
| 2413 | |
| 2414 | if (sequenceDepth == 1) { |
| 2415 | // If this is the parent node of all the functions, we want to see them |
| 2416 | // early, so all call points have actual SPIR-V functions to reference. |
| 2417 | // In all cases, still let the traverser visit the children for us. |
| 2418 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 2419 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2420 | // 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] | 2421 | // anything else gets there, so visit out of order, doing them all now. |
| 2422 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2423 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2424 | // 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] | 2425 | // so do them manually. |
| 2426 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2427 | |
| 2428 | return false; |
| 2429 | } |
| 2430 | |
| 2431 | return true; |
| 2432 | } |
| 2433 | case glslang::EOpLinkerObjects: |
| 2434 | { |
| 2435 | if (visit == glslang::EvPreVisit) |
| 2436 | linkageOnly = true; |
| 2437 | else |
| 2438 | linkageOnly = false; |
| 2439 | |
| 2440 | return true; |
| 2441 | } |
| 2442 | case glslang::EOpComma: |
| 2443 | { |
| 2444 | // processing from left to right naturally leaves the right-most |
| 2445 | // lying around in the access chain |
| 2446 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2447 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2448 | glslangOperands[i]->traverse(this); |
| 2449 | |
| 2450 | return false; |
| 2451 | } |
| 2452 | case glslang::EOpFunction: |
| 2453 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2454 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2455 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2456 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2457 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2458 | } else { |
| 2459 | handleFunctionEntry(node); |
| 2460 | } |
| 2461 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2462 | if (inEntryPoint) |
| 2463 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2464 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2465 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | return true; |
| 2469 | case glslang::EOpParameters: |
| 2470 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2471 | // the body, so we still visited the function node's children, making this |
| 2472 | // child redundant. |
| 2473 | return false; |
| 2474 | case glslang::EOpFunctionCall: |
| 2475 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2476 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2477 | if (node->isUserDefined()) |
| 2478 | result = handleUserFunctionCall(node); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2479 | if (result) { |
| 2480 | builder.clearAccessChain(); |
| 2481 | builder.setAccessChainRValue(result); |
| 2482 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2483 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2484 | |
| 2485 | return false; |
| 2486 | } |
| 2487 | case glslang::EOpConstructMat2x2: |
| 2488 | case glslang::EOpConstructMat2x3: |
| 2489 | case glslang::EOpConstructMat2x4: |
| 2490 | case glslang::EOpConstructMat3x2: |
| 2491 | case glslang::EOpConstructMat3x3: |
| 2492 | case glslang::EOpConstructMat3x4: |
| 2493 | case glslang::EOpConstructMat4x2: |
| 2494 | case glslang::EOpConstructMat4x3: |
| 2495 | case glslang::EOpConstructMat4x4: |
| 2496 | case glslang::EOpConstructDMat2x2: |
| 2497 | case glslang::EOpConstructDMat2x3: |
| 2498 | case glslang::EOpConstructDMat2x4: |
| 2499 | case glslang::EOpConstructDMat3x2: |
| 2500 | case glslang::EOpConstructDMat3x3: |
| 2501 | case glslang::EOpConstructDMat3x4: |
| 2502 | case glslang::EOpConstructDMat4x2: |
| 2503 | case glslang::EOpConstructDMat4x3: |
| 2504 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2505 | case glslang::EOpConstructIMat2x2: |
| 2506 | case glslang::EOpConstructIMat2x3: |
| 2507 | case glslang::EOpConstructIMat2x4: |
| 2508 | case glslang::EOpConstructIMat3x2: |
| 2509 | case glslang::EOpConstructIMat3x3: |
| 2510 | case glslang::EOpConstructIMat3x4: |
| 2511 | case glslang::EOpConstructIMat4x2: |
| 2512 | case glslang::EOpConstructIMat4x3: |
| 2513 | case glslang::EOpConstructIMat4x4: |
| 2514 | case glslang::EOpConstructUMat2x2: |
| 2515 | case glslang::EOpConstructUMat2x3: |
| 2516 | case glslang::EOpConstructUMat2x4: |
| 2517 | case glslang::EOpConstructUMat3x2: |
| 2518 | case glslang::EOpConstructUMat3x3: |
| 2519 | case glslang::EOpConstructUMat3x4: |
| 2520 | case glslang::EOpConstructUMat4x2: |
| 2521 | case glslang::EOpConstructUMat4x3: |
| 2522 | case glslang::EOpConstructUMat4x4: |
| 2523 | case glslang::EOpConstructBMat2x2: |
| 2524 | case glslang::EOpConstructBMat2x3: |
| 2525 | case glslang::EOpConstructBMat2x4: |
| 2526 | case glslang::EOpConstructBMat3x2: |
| 2527 | case glslang::EOpConstructBMat3x3: |
| 2528 | case glslang::EOpConstructBMat3x4: |
| 2529 | case glslang::EOpConstructBMat4x2: |
| 2530 | case glslang::EOpConstructBMat4x3: |
| 2531 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2532 | case glslang::EOpConstructF16Mat2x2: |
| 2533 | case glslang::EOpConstructF16Mat2x3: |
| 2534 | case glslang::EOpConstructF16Mat2x4: |
| 2535 | case glslang::EOpConstructF16Mat3x2: |
| 2536 | case glslang::EOpConstructF16Mat3x3: |
| 2537 | case glslang::EOpConstructF16Mat3x4: |
| 2538 | case glslang::EOpConstructF16Mat4x2: |
| 2539 | case glslang::EOpConstructF16Mat4x3: |
| 2540 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2541 | isMatrix = true; |
| 2542 | // fall through |
| 2543 | case glslang::EOpConstructFloat: |
| 2544 | case glslang::EOpConstructVec2: |
| 2545 | case glslang::EOpConstructVec3: |
| 2546 | case glslang::EOpConstructVec4: |
| 2547 | case glslang::EOpConstructDouble: |
| 2548 | case glslang::EOpConstructDVec2: |
| 2549 | case glslang::EOpConstructDVec3: |
| 2550 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2551 | case glslang::EOpConstructFloat16: |
| 2552 | case glslang::EOpConstructF16Vec2: |
| 2553 | case glslang::EOpConstructF16Vec3: |
| 2554 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2555 | case glslang::EOpConstructBool: |
| 2556 | case glslang::EOpConstructBVec2: |
| 2557 | case glslang::EOpConstructBVec3: |
| 2558 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2559 | case glslang::EOpConstructInt8: |
| 2560 | case glslang::EOpConstructI8Vec2: |
| 2561 | case glslang::EOpConstructI8Vec3: |
| 2562 | case glslang::EOpConstructI8Vec4: |
| 2563 | case glslang::EOpConstructUint8: |
| 2564 | case glslang::EOpConstructU8Vec2: |
| 2565 | case glslang::EOpConstructU8Vec3: |
| 2566 | case glslang::EOpConstructU8Vec4: |
| 2567 | case glslang::EOpConstructInt16: |
| 2568 | case glslang::EOpConstructI16Vec2: |
| 2569 | case glslang::EOpConstructI16Vec3: |
| 2570 | case glslang::EOpConstructI16Vec4: |
| 2571 | case glslang::EOpConstructUint16: |
| 2572 | case glslang::EOpConstructU16Vec2: |
| 2573 | case glslang::EOpConstructU16Vec3: |
| 2574 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2575 | case glslang::EOpConstructInt: |
| 2576 | case glslang::EOpConstructIVec2: |
| 2577 | case glslang::EOpConstructIVec3: |
| 2578 | case glslang::EOpConstructIVec4: |
| 2579 | case glslang::EOpConstructUint: |
| 2580 | case glslang::EOpConstructUVec2: |
| 2581 | case glslang::EOpConstructUVec3: |
| 2582 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2583 | case glslang::EOpConstructInt64: |
| 2584 | case glslang::EOpConstructI64Vec2: |
| 2585 | case glslang::EOpConstructI64Vec3: |
| 2586 | case glslang::EOpConstructI64Vec4: |
| 2587 | case glslang::EOpConstructUint64: |
| 2588 | case glslang::EOpConstructU64Vec2: |
| 2589 | case glslang::EOpConstructU64Vec3: |
| 2590 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2591 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2592 | case glslang::EOpConstructTextureSampler: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2593 | case glslang::EOpConstructReference: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2594 | case glslang::EOpConstructCooperativeMatrix: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2595 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2596 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2597 | std::vector<spv::Id> arguments; |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2598 | translateArguments(*node, arguments, lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2599 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2600 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2601 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2602 | else if (node->getOp() == glslang::EOpConstructStruct || |
| 2603 | node->getOp() == glslang::EOpConstructCooperativeMatrix || |
| 2604 | node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2605 | std::vector<spv::Id> constituents; |
| 2606 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2607 | constituents.push_back(arguments[c]); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2608 | constructed = createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2609 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2610 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2611 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2612 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2613 | |
| 2614 | builder.clearAccessChain(); |
| 2615 | builder.setAccessChainRValue(constructed); |
| 2616 | |
| 2617 | return false; |
| 2618 | } |
| 2619 | |
| 2620 | // These six are component-wise compares with component-wise results. |
| 2621 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2622 | case glslang::EOpLessThan: |
| 2623 | case glslang::EOpGreaterThan: |
| 2624 | case glslang::EOpLessThanEqual: |
| 2625 | case glslang::EOpGreaterThanEqual: |
| 2626 | case glslang::EOpVectorEqual: |
| 2627 | case glslang::EOpVectorNotEqual: |
| 2628 | { |
| 2629 | // Map the operation to a binary |
| 2630 | binOp = node->getOp(); |
| 2631 | reduceComparison = false; |
| 2632 | switch (node->getOp()) { |
| 2633 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2634 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2635 | default: binOp = node->getOp(); break; |
| 2636 | } |
| 2637 | |
| 2638 | break; |
| 2639 | } |
| 2640 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2641 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2642 | binOp = glslang::EOpMul; |
| 2643 | break; |
| 2644 | case glslang::EOpOuterProduct: |
| 2645 | // two vectors multiplied to make a matrix |
| 2646 | binOp = glslang::EOpOuterProduct; |
| 2647 | break; |
| 2648 | case glslang::EOpDot: |
| 2649 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2650 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2651 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2652 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2653 | binOp = glslang::EOpMul; |
| 2654 | break; |
| 2655 | } |
| 2656 | case glslang::EOpMod: |
| 2657 | // when an aggregate, this is the floating-point mod built-in function, |
| 2658 | // which can be emitted by the one in createBinaryOperation() |
| 2659 | binOp = glslang::EOpMod; |
| 2660 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2661 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2662 | case glslang::EOpEmitVertex: |
| 2663 | case glslang::EOpEndPrimitive: |
| 2664 | case glslang::EOpBarrier: |
| 2665 | case glslang::EOpMemoryBarrier: |
| 2666 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2667 | case glslang::EOpMemoryBarrierBuffer: |
| 2668 | case glslang::EOpMemoryBarrierImage: |
| 2669 | case glslang::EOpMemoryBarrierShared: |
| 2670 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2671 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2672 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2673 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2674 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2675 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2676 | case glslang::EOpSubgroupBarrier: |
| 2677 | case glslang::EOpSubgroupMemoryBarrier: |
| 2678 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2679 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2680 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2681 | noReturnValue = true; |
| 2682 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2683 | break; |
| 2684 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2685 | case glslang::EOpAtomicAdd: |
| 2686 | case glslang::EOpAtomicMin: |
| 2687 | case glslang::EOpAtomicMax: |
| 2688 | case glslang::EOpAtomicAnd: |
| 2689 | case glslang::EOpAtomicOr: |
| 2690 | case glslang::EOpAtomicXor: |
| 2691 | case glslang::EOpAtomicExchange: |
| 2692 | case glslang::EOpAtomicCompSwap: |
| 2693 | atomic = true; |
| 2694 | break; |
| 2695 | |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2696 | #ifndef GLSLANG_WEB |
| 2697 | case glslang::EOpAtomicStore: |
| 2698 | noReturnValue = true; |
| 2699 | // fallthrough |
| 2700 | case glslang::EOpAtomicLoad: |
| 2701 | atomic = true; |
| 2702 | break; |
| 2703 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2704 | case glslang::EOpAtomicCounterAdd: |
| 2705 | case glslang::EOpAtomicCounterSubtract: |
| 2706 | case glslang::EOpAtomicCounterMin: |
| 2707 | case glslang::EOpAtomicCounterMax: |
| 2708 | case glslang::EOpAtomicCounterAnd: |
| 2709 | case glslang::EOpAtomicCounterOr: |
| 2710 | case glslang::EOpAtomicCounterXor: |
| 2711 | case glslang::EOpAtomicCounterExchange: |
| 2712 | case glslang::EOpAtomicCounterCompSwap: |
| 2713 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2714 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2715 | atomic = true; |
| 2716 | break; |
| 2717 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 2718 | case glslang::EOpAbsDifference: |
| 2719 | case glslang::EOpAddSaturate: |
| 2720 | case glslang::EOpSubSaturate: |
| 2721 | case glslang::EOpAverage: |
| 2722 | case glslang::EOpAverageRounded: |
| 2723 | case glslang::EOpMul32x16: |
| 2724 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 2725 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 2726 | binOp = node->getOp(); |
| 2727 | break; |
| 2728 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2729 | case glslang::EOpIgnoreIntersection: |
| 2730 | case glslang::EOpTerminateRay: |
| 2731 | case glslang::EOpTrace: |
| 2732 | case glslang::EOpExecuteCallable: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2733 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2734 | noReturnValue = true; |
| 2735 | break; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2736 | case glslang::EOpRayQueryInitialize: |
| 2737 | case glslang::EOpRayQueryTerminate: |
| 2738 | case glslang::EOpRayQueryGenerateIntersection: |
| 2739 | case glslang::EOpRayQueryConfirmIntersection: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2740 | builder.addExtension("SPV_KHR_ray_query"); |
| 2741 | builder.addCapability(spv::CapabilityRayQueryProvisionalKHR); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2742 | noReturnValue = true; |
| 2743 | break; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2744 | case glslang::EOpRayQueryProceed: |
| 2745 | case glslang::EOpRayQueryGetIntersectionType: |
| 2746 | case glslang::EOpRayQueryGetRayTMin: |
| 2747 | case glslang::EOpRayQueryGetRayFlags: |
| 2748 | case glslang::EOpRayQueryGetIntersectionT: |
| 2749 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 2750 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 2751 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 2752 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 2753 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 2754 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 2755 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 2756 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 2757 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 2758 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 2759 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 2760 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 2761 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 2762 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 2763 | builder.addExtension("SPV_KHR_ray_query"); |
| 2764 | builder.addCapability(spv::CapabilityRayQueryProvisionalKHR); |
| 2765 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2766 | case glslang::EOpCooperativeMatrixLoad: |
| 2767 | case glslang::EOpCooperativeMatrixStore: |
| 2768 | noReturnValue = true; |
| 2769 | break; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 2770 | case glslang::EOpBeginInvocationInterlock: |
| 2771 | case glslang::EOpEndInvocationInterlock: |
| 2772 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 2773 | noReturnValue = true; |
| 2774 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2775 | #endif |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2776 | |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 2777 | case glslang::EOpDebugPrintf: |
| 2778 | noReturnValue = true; |
| 2779 | break; |
| 2780 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2781 | default: |
| 2782 | break; |
| 2783 | } |
| 2784 | |
| 2785 | // |
| 2786 | // See if it maps to a regular operation. |
| 2787 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2788 | if (binOp != glslang::EOpNull) { |
| 2789 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2790 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2791 | assert(left && right); |
| 2792 | |
| 2793 | builder.clearAccessChain(); |
| 2794 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2795 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2796 | |
| 2797 | builder.clearAccessChain(); |
| 2798 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2799 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2800 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2801 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2802 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2803 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2804 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2805 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2806 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2807 | left->getType().getBasicType(), reduceComparison); |
| 2808 | |
| 2809 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2810 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2811 | builder.clearAccessChain(); |
| 2812 | builder.setAccessChainRValue(result); |
| 2813 | |
| 2814 | return false; |
| 2815 | } |
| 2816 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2817 | // |
| 2818 | // Create the list of operands. |
| 2819 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2820 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2821 | std::vector<spv::Id> operands; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2822 | std::vector<spv::IdImmediate> memoryAccessOperands; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2823 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2824 | // special case l-value operands; there are just a few |
| 2825 | bool lvalue = false; |
| 2826 | switch (node->getOp()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2827 | case glslang::EOpModf: |
| 2828 | if (arg == 1) |
| 2829 | lvalue = true; |
| 2830 | break; |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2831 | |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2832 | case glslang::EOpRayQueryInitialize: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2833 | case glslang::EOpRayQueryTerminate: |
| 2834 | case glslang::EOpRayQueryConfirmIntersection: |
| 2835 | case glslang::EOpRayQueryProceed: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2836 | case glslang::EOpRayQueryGenerateIntersection: |
| 2837 | case glslang::EOpRayQueryGetIntersectionType: |
| 2838 | case glslang::EOpRayQueryGetIntersectionT: |
| 2839 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 2840 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 2841 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 2842 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 2843 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 2844 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 2845 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 2846 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 2847 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 2848 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 2849 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 2850 | if (arg == 0) |
| 2851 | lvalue = true; |
| 2852 | break; |
| 2853 | |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2854 | case glslang::EOpAtomicAdd: |
| 2855 | case glslang::EOpAtomicMin: |
| 2856 | case glslang::EOpAtomicMax: |
| 2857 | case glslang::EOpAtomicAnd: |
| 2858 | case glslang::EOpAtomicOr: |
| 2859 | case glslang::EOpAtomicXor: |
| 2860 | case glslang::EOpAtomicExchange: |
| 2861 | case glslang::EOpAtomicCompSwap: |
| 2862 | if (arg == 0) |
| 2863 | lvalue = true; |
| 2864 | break; |
| 2865 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2866 | #ifndef GLSLANG_WEB |
| 2867 | case glslang::EOpFrexp: |
| 2868 | if (arg == 1) |
| 2869 | lvalue = true; |
| 2870 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2871 | case glslang::EOpInterpolateAtSample: |
| 2872 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2873 | case glslang::EOpInterpolateAtVertex: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2874 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2875 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2876 | |
| 2877 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2878 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2879 | // That is, we transform |
| 2880 | // |
| 2881 | // interpolate(v.zy) -> interpolate(v).zy |
| 2882 | // |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2883 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2884 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2885 | invertedType = convertGlslangToSpvType( |
| 2886 | glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2887 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2888 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2889 | case glslang::EOpAtomicLoad: |
| 2890 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2891 | case glslang::EOpAtomicCounterAdd: |
| 2892 | case glslang::EOpAtomicCounterSubtract: |
| 2893 | case glslang::EOpAtomicCounterMin: |
| 2894 | case glslang::EOpAtomicCounterMax: |
| 2895 | case glslang::EOpAtomicCounterAnd: |
| 2896 | case glslang::EOpAtomicCounterOr: |
| 2897 | case glslang::EOpAtomicCounterXor: |
| 2898 | case glslang::EOpAtomicCounterExchange: |
| 2899 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 2900 | if (arg == 0) |
| 2901 | lvalue = true; |
| 2902 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2903 | case glslang::EOpAddCarry: |
| 2904 | case glslang::EOpSubBorrow: |
| 2905 | if (arg == 2) |
| 2906 | lvalue = true; |
| 2907 | break; |
| 2908 | case glslang::EOpUMulExtended: |
| 2909 | case glslang::EOpIMulExtended: |
| 2910 | if (arg >= 2) |
| 2911 | lvalue = true; |
| 2912 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2913 | case glslang::EOpCooperativeMatrixLoad: |
| 2914 | if (arg == 0 || arg == 1) |
| 2915 | lvalue = true; |
| 2916 | break; |
| 2917 | case glslang::EOpCooperativeMatrixStore: |
| 2918 | if (arg == 1) |
| 2919 | lvalue = true; |
| 2920 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2921 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2922 | default: |
| 2923 | break; |
| 2924 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2925 | builder.clearAccessChain(); |
| 2926 | if (invertedType != spv::NoType && arg == 0) |
| 2927 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2928 | else |
| 2929 | glslangOperands[arg]->traverse(this); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2930 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2931 | #ifndef GLSLANG_WEB |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2932 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
| 2933 | node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2934 | |
| 2935 | if (arg == 1) { |
| 2936 | // fold "element" parameter into the access chain |
| 2937 | spv::Builder::AccessChain save = builder.getAccessChain(); |
| 2938 | builder.clearAccessChain(); |
| 2939 | glslangOperands[2]->traverse(this); |
| 2940 | |
| 2941 | spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
| 2942 | |
| 2943 | builder.setAccessChain(save); |
| 2944 | |
| 2945 | // Point to the first element of the array. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2946 | builder.accessChainPush(elementId, |
| 2947 | TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
| 2948 | glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2949 | |
| 2950 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 2951 | unsigned int alignment = builder.getAccessChain().alignment; |
| 2952 | |
| 2953 | int memoryAccess = TranslateMemoryAccess(coherentFlags); |
| 2954 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
| 2955 | memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
| 2956 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
| 2957 | memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2958 | if (builder.getStorageClass(builder.getAccessChain().base) == |
| 2959 | spv::StorageClassPhysicalStorageBufferEXT) { |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2960 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
| 2961 | } |
| 2962 | |
| 2963 | memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
| 2964 | |
| 2965 | if (memoryAccess & spv::MemoryAccessAlignedMask) { |
| 2966 | memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
| 2967 | } |
| 2968 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2969 | if (memoryAccess & |
| 2970 | (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
| 2971 | memoryAccessOperands.push_back(spv::IdImmediate(true, |
| 2972 | builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2973 | } |
| 2974 | } else if (arg == 2) { |
| 2975 | continue; |
| 2976 | } |
| 2977 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2978 | #endif |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2979 | |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2980 | // for l-values, pass the address, for r-values, pass the value |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2981 | if (lvalue) { |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2982 | if (invertedType == spv::NoType && !builder.isSpvLvalue()) { |
| 2983 | // SPIR-V cannot represent an l-value containing a swizzle that doesn't |
| 2984 | // reduce to a simple access chain. So, we need a temporary vector to |
| 2985 | // receive the result, and must later swizzle that into the original |
| 2986 | // l-value. |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 2987 | complexLvalues.push_back(builder.getAccessChain()); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 2988 | temporaryLvalues.push_back(builder.createVariable( |
| 2989 | spv::NoPrecision, spv::StorageClassFunction, |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 2990 | builder.accessChainGetInferredType(), "swizzleTemp")); |
| 2991 | operands.push_back(temporaryLvalues.back()); |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2992 | } else { |
| 2993 | operands.push_back(builder.accessChainGetLValue()); |
| 2994 | } |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2995 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2996 | lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
| 2997 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2998 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2999 | glslang::TOperator glslangOp = node->getOp(); |
| 3000 | if (arg == 1 && |
| 3001 | (glslangOp == glslang::EOpRayQueryGetIntersectionType || |
| 3002 | glslangOp == glslang::EOpRayQueryGetIntersectionT || |
| 3003 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex || |
| 3004 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId || |
| 3005 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset || |
| 3006 | glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex || |
| 3007 | glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex || |
| 3008 | glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics || |
| 3009 | glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace || |
| 3010 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection || |
| 3011 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin || |
| 3012 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld || |
| 3013 | glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject |
| 3014 | )) { |
| 3015 | bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst(); |
| 3016 | operands.push_back(builder.makeIntConstant(cond ? 1 : 0)); |
| 3017 | } |
| 3018 | else { |
| 3019 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
| 3020 | } |
| 3021 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3022 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3023 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3024 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3025 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3026 | #ifndef GLSLANG_WEB |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3027 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
| 3028 | std::vector<spv::IdImmediate> idImmOps; |
| 3029 | |
| 3030 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3031 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3032 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3033 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3034 | // get the pointee type |
| 3035 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
| 3036 | assert(builder.isCooperativeMatrixType(typeId)); |
| 3037 | // do the op |
| 3038 | spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
| 3039 | // store the result to the pointer (out param 'm') |
| 3040 | builder.createStore(result, operands[0]); |
| 3041 | result = 0; |
| 3042 | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 3043 | std::vector<spv::IdImmediate> idImmOps; |
| 3044 | |
| 3045 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3046 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
| 3047 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3048 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3049 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3050 | |
| 3051 | builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps); |
| 3052 | result = 0; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3053 | } else |
| 3054 | #endif |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 3055 | if (atomic) { |
| 3056 | // Handle all atomics |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3057 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), |
| 3058 | lvalueCoherentFlags); |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 3059 | } else if (node->getOp() == glslang::EOpDebugPrintf) { |
| 3060 | if (!nonSemanticDebugPrintf) { |
| 3061 | nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf"); |
| 3062 | } |
| 3063 | result = builder.createBuiltinCall(builder.makeVoidType(), nonSemanticDebugPrintf, spv::NonSemanticDebugPrintfDebugPrintf, operands); |
| 3064 | builder.addExtension(spv::E_SPV_KHR_non_semantic_info); |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 3065 | } else { |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3066 | // Pass through to generic operations. |
| 3067 | switch (glslangOperands.size()) { |
| 3068 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3069 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3070 | break; |
| 3071 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3072 | { |
| 3073 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3074 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 3075 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3076 | result = createUnaryOperation( |
| 3077 | node->getOp(), decorations, |
| 3078 | resultType(), operands.front(), |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 3079 | glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3080 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3081 | break; |
| 3082 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3083 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3084 | break; |
| 3085 | } |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3086 | |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3087 | if (invertedType != spv::NoResult) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3088 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3089 | |
| 3090 | for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) { |
| 3091 | builder.setAccessChain(complexLvalues[i]); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3092 | builder.accessChainStore(builder.createLoad(temporaryLvalues[i], spv::NoPrecision)); |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3093 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | if (noReturnValue) |
| 3097 | return false; |
| 3098 | |
| 3099 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 3100 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 3101 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3102 | } else { |
| 3103 | builder.clearAccessChain(); |
| 3104 | builder.setAccessChainRValue(result); |
| 3105 | return false; |
| 3106 | } |
| 3107 | } |
| 3108 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3109 | // This path handles both if-then-else and ?: |
| 3110 | // The if-then-else has a node type of void, while |
| 3111 | // ?: has either a void or a non-void node type |
| 3112 | // |
| 3113 | // Leaving the result, when not void: |
| 3114 | // GLSL only has r-values as the result of a :?, but |
| 3115 | // if we have an l-value, that can be more efficient if it will |
| 3116 | // become the base of a complex r-value expression, because the |
| 3117 | // 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] | 3118 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 3119 | { |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3120 | // see if OpSelect can handle it |
| 3121 | const auto isOpSelectable = [&]() { |
| 3122 | if (node->getBasicType() == glslang::EbtVoid) |
| 3123 | return false; |
| 3124 | // OpSelect can do all other types starting with SPV 1.4 |
| 3125 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
| 3126 | // pre-1.4, only scalars and vectors can be handled |
| 3127 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
| 3128 | return false; |
| 3129 | } |
| 3130 | return true; |
| 3131 | }; |
| 3132 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3133 | // See if it simple and safe, or required, to execute both sides. |
| 3134 | // Crucially, side effects must be either semantically required or avoided, |
| 3135 | // and there are performance trade-offs. |
| 3136 | // Return true if required or a good idea (and safe) to execute both sides, |
| 3137 | // false otherwise. |
| 3138 | const auto bothSidesPolicy = [&]() -> bool { |
| 3139 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3140 | if (node->getTrueBlock() == nullptr || |
| 3141 | node->getFalseBlock() == nullptr) |
| 3142 | return false; |
| 3143 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3144 | // required? (unless we write additional code to look for side effects |
| 3145 | // and make performance trade-offs if none are present) |
| 3146 | if (!node->getShortCircuit()) |
| 3147 | return true; |
| 3148 | |
| 3149 | // if not required to execute both, decide based on performance/practicality... |
| 3150 | |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3151 | if (!isOpSelectable()) |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3152 | return false; |
| 3153 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3154 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 3155 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 3156 | |
| 3157 | // return true if a single operand to ? : is okay for OpSelect |
| 3158 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 3159 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3160 | }; |
| 3161 | |
| 3162 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 3163 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 3164 | }; |
| 3165 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3166 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 3167 | // emit the condition before doing anything with selection |
| 3168 | node->getCondition()->traverse(this); |
| 3169 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 3170 | |
| 3171 | // Find a way of executing both sides and selecting the right result. |
| 3172 | const auto executeBothSides = [&]() -> void { |
| 3173 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3174 | node->getTrueBlock()->traverse(this); |
| 3175 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3176 | node->getFalseBlock()->traverse(this); |
| 3177 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3178 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3179 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3180 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3181 | // done if void |
| 3182 | if (node->getBasicType() == glslang::EbtVoid) |
| 3183 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 3184 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3185 | // emit code to select between trueValue and falseValue |
| 3186 | |
| 3187 | // see if OpSelect can handle it |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3188 | if (isOpSelectable()) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3189 | // Emit OpSelect for this selection. |
| 3190 | |
| 3191 | // smear condition to vector, if necessary (AST is always scalar) |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3192 | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
| 3193 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3194 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 3195 | builder.makeVectorType(builder.makeBoolType(), |
| 3196 | builder.getNumComponents(trueValue))); |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3197 | } |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3198 | |
| 3199 | // OpSelect |
| 3200 | result = builder.createTriOp(spv::OpSelect, |
| 3201 | convertGlslangToSpvType(node->getType()), condition, |
| 3202 | trueValue, falseValue); |
| 3203 | |
| 3204 | builder.clearAccessChain(); |
| 3205 | builder.setAccessChainRValue(result); |
| 3206 | } else { |
| 3207 | // We need control flow to select the result. |
| 3208 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3209 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), |
| 3210 | spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3211 | |
| 3212 | // Selection control: |
| 3213 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3214 | |
| 3215 | // make an "if" based on the value created by the condition |
| 3216 | spv::Builder::If ifBuilder(condition, control, builder); |
| 3217 | |
| 3218 | // emit the "then" statement |
| 3219 | builder.createStore(trueValue, result); |
| 3220 | ifBuilder.makeBeginElse(); |
| 3221 | // emit the "else" statement |
| 3222 | builder.createStore(falseValue, result); |
| 3223 | |
| 3224 | // finish off the control flow |
| 3225 | ifBuilder.makeEndIf(); |
| 3226 | |
| 3227 | builder.clearAccessChain(); |
| 3228 | builder.setAccessChainLValue(result); |
| 3229 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3230 | }; |
| 3231 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3232 | // Execute the one side needed, as per the condition |
| 3233 | const auto executeOneSide = [&]() { |
| 3234 | // Always emit control flow. |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3235 | if (node->getBasicType() != glslang::EbtVoid) { |
| 3236 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), spv::StorageClassFunction, |
| 3237 | convertGlslangToSpvType(node->getType())); |
| 3238 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3239 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3240 | // Selection control: |
| 3241 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3242 | |
| 3243 | // make an "if" based on the value created by the condition |
| 3244 | spv::Builder::If ifBuilder(condition, control, builder); |
| 3245 | |
| 3246 | // emit the "then" statement |
| 3247 | if (node->getTrueBlock() != nullptr) { |
| 3248 | node->getTrueBlock()->traverse(this); |
| 3249 | if (result != spv::NoResult) |
| 3250 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 3251 | } |
| 3252 | |
| 3253 | if (node->getFalseBlock() != nullptr) { |
| 3254 | ifBuilder.makeBeginElse(); |
| 3255 | // emit the "else" statement |
| 3256 | node->getFalseBlock()->traverse(this); |
| 3257 | if (result != spv::NoResult) |
| 3258 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 3259 | } |
| 3260 | |
| 3261 | // finish off the control flow |
| 3262 | ifBuilder.makeEndIf(); |
| 3263 | |
| 3264 | if (result != spv::NoResult) { |
| 3265 | builder.clearAccessChain(); |
| 3266 | builder.setAccessChainLValue(result); |
| 3267 | } |
| 3268 | }; |
| 3269 | |
| 3270 | // Try for OpSelect (or a requirement to execute both sides) |
| 3271 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 3272 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 3273 | if (node->getType().getQualifier().isSpecConstant()) |
| 3274 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3275 | executeBothSides(); |
| 3276 | } else |
| 3277 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3278 | |
| 3279 | return false; |
| 3280 | } |
| 3281 | |
| 3282 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 3283 | { |
| 3284 | // emit and get the condition before doing anything with switch |
| 3285 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3286 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3287 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3288 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 3289 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3290 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3291 | // browse the children to sort out code segments |
| 3292 | int defaultSegment = -1; |
| 3293 | std::vector<TIntermNode*> codeSegments; |
| 3294 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 3295 | std::vector<int> caseValues; |
| 3296 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 3297 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 3298 | TIntermNode* child = *c; |
| 3299 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3300 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3301 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3302 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3303 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion() |
| 3304 | ->getConstArray()[0].getIConst()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3305 | } else |
| 3306 | codeSegments.push_back(child); |
| 3307 | } |
| 3308 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3309 | // 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] | 3310 | // statements between the last case and the end of the switch statement |
| 3311 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 3312 | (int)codeSegments.size() == defaultSegment) |
| 3313 | codeSegments.push_back(nullptr); |
| 3314 | |
| 3315 | // make the switch statement |
| 3316 | 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] | 3317 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, |
| 3318 | segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3319 | |
| 3320 | // emit all the code in the segments |
| 3321 | breakForLoop.push(false); |
| 3322 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 3323 | builder.nextSwitchSegment(segmentBlocks, s); |
| 3324 | if (codeSegments[s]) |
| 3325 | codeSegments[s]->traverse(this); |
| 3326 | else |
| 3327 | builder.addSwitchBreak(); |
| 3328 | } |
| 3329 | breakForLoop.pop(); |
| 3330 | |
| 3331 | builder.endSwitch(segmentBlocks); |
| 3332 | |
| 3333 | return false; |
| 3334 | } |
| 3335 | |
| 3336 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 3337 | { |
| 3338 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 3339 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3340 | |
| 3341 | builder.clearAccessChain(); |
| 3342 | builder.setAccessChainRValue(constant); |
| 3343 | } |
| 3344 | |
| 3345 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 3346 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3347 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3348 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3349 | |
| 3350 | // Loop control: |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3351 | std::vector<unsigned int> operands; |
| 3352 | const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3353 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3354 | // Spec requires back edges to target header blocks, and every header block |
| 3355 | // must dominate its merge block. Make a header block first to ensure these |
| 3356 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 3357 | // by a block-ending branch. But we don't want to put any other body/test |
| 3358 | // instructions in it, since the body/test may have arbitrary instructions, |
| 3359 | // including merges of its own. |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3360 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3361 | builder.setBuildPoint(&blocks.head); |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3362 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3363 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3364 | spv::Block& test = builder.makeNewBlock(); |
| 3365 | builder.createBranch(&test); |
| 3366 | |
| 3367 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3368 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3369 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3370 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 3371 | |
| 3372 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3373 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3374 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3375 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3376 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3377 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3378 | |
| 3379 | builder.setBuildPoint(&blocks.continue_target); |
| 3380 | if (node->getTerminal()) |
| 3381 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3382 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 3383 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3384 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3385 | builder.createBranch(&blocks.body); |
| 3386 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3387 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3388 | builder.setBuildPoint(&blocks.body); |
| 3389 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3390 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3391 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3392 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3393 | |
| 3394 | builder.setBuildPoint(&blocks.continue_target); |
| 3395 | if (node->getTerminal()) |
| 3396 | node->getTerminal()->traverse(this); |
| 3397 | if (node->getTest()) { |
| 3398 | node->getTest()->traverse(this); |
| 3399 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3400 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3401 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3402 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 3403 | // TODO: unless there was a break/return/discard instruction |
| 3404 | // somewhere in the body, this is an infinite loop, so we should |
| 3405 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3406 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3407 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3408 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3409 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3410 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3411 | return false; |
| 3412 | } |
| 3413 | |
| 3414 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 3415 | { |
| 3416 | if (node->getExpression()) |
| 3417 | node->getExpression()->traverse(this); |
| 3418 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3419 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3420 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3421 | switch (node->getFlowOp()) { |
| 3422 | case glslang::EOpKill: |
| 3423 | builder.makeDiscard(); |
| 3424 | break; |
| 3425 | case glslang::EOpBreak: |
| 3426 | if (breakForLoop.top()) |
| 3427 | builder.createLoopExit(); |
| 3428 | else |
| 3429 | builder.addSwitchBreak(); |
| 3430 | break; |
| 3431 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3432 | builder.createLoopContinue(); |
| 3433 | break; |
| 3434 | case glslang::EOpReturn: |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3435 | if (node->getExpression() != nullptr) { |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3436 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 3437 | spv::Id returnId = accessChainLoad(glslangReturnType); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3438 | if (builder.getTypeId(returnId) != currentFunction->getReturnType() || |
| 3439 | TranslatePrecisionDecoration(glslangReturnType) != currentFunction->getReturnPrecision()) { |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3440 | builder.clearAccessChain(); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3441 | spv::Id copyId = builder.createVariable(currentFunction->getReturnPrecision(), |
| 3442 | spv::StorageClassFunction, currentFunction->getReturnType()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3443 | builder.setAccessChainLValue(copyId); |
| 3444 | multiTypeStore(glslangReturnType, returnId); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3445 | returnId = builder.createLoad(copyId, currentFunction->getReturnPrecision()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3446 | } |
| 3447 | builder.makeReturn(false, returnId); |
| 3448 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 3449 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3450 | |
| 3451 | builder.clearAccessChain(); |
| 3452 | break; |
| 3453 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3454 | #ifndef GLSLANG_WEB |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3455 | case glslang::EOpDemote: |
| 3456 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 3457 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 3458 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 3459 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3460 | #endif |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3461 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3462 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3463 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3464 | break; |
| 3465 | } |
| 3466 | |
| 3467 | return false; |
| 3468 | } |
| 3469 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3470 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3471 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3472 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3473 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3474 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 3475 | if (node->getQualifier().isConstant()) { |
Dan Sinclair | 12fcaa2 | 2018-11-13 09:17:44 -0500 | [diff] [blame] | 3476 | spv::Id result = createSpvConstant(*node); |
| 3477 | if (result != spv::NoResult) |
| 3478 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3482 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3483 | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
| 3484 | : forcedType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3485 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3486 | const bool contains16BitType = node->getType().contains16BitFloat() || |
| 3487 | node->getType().contains16BitInt(); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3488 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3489 | switch (storageClass) { |
| 3490 | case spv::StorageClassInput: |
| 3491 | case spv::StorageClassOutput: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3492 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3493 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3494 | break; |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3495 | case spv::StorageClassUniform: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3496 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3497 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 3498 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3499 | else |
| 3500 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 3501 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3502 | #ifndef GLSLANG_WEB |
| 3503 | case spv::StorageClassPushConstant: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3504 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3505 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 3506 | break; |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3507 | case spv::StorageClassStorageBuffer: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3508 | case spv::StorageClassPhysicalStorageBufferEXT: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3509 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3510 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 3511 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3512 | #endif |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3513 | default: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3514 | if (node->getType().contains16BitFloat()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3515 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3516 | if (node->getType().contains16BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3517 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3518 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3519 | } |
| 3520 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3521 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3522 | if (node->getType().contains8BitInt()) { |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3523 | if (storageClass == spv::StorageClassPushConstant) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3524 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3525 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 3526 | } else if (storageClass == spv::StorageClassUniform) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3527 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3528 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3529 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3530 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3531 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3532 | } else { |
| 3533 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3537 | const char* name = node->getName().c_str(); |
| 3538 | if (glslang::IsAnonymous(name)) |
| 3539 | name = ""; |
| 3540 | |
Alejandro Piñeiro | ff6dcca | 2020-06-04 09:39:31 +0200 | [diff] [blame] | 3541 | spv::Id initializer = spv::NoResult; |
| 3542 | |
| 3543 | if (node->getType().getQualifier().storage == glslang::EvqUniform && |
| 3544 | !node->getConstArray().empty()) { |
| 3545 | int nextConst = 0; |
| 3546 | initializer = createSpvConstantFromConstUnionArray(node->getType(), |
| 3547 | node->getConstArray(), |
| 3548 | nextConst, |
| 3549 | false /* specConst */); |
| 3550 | } |
| 3551 | |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3552 | return builder.createVariable(spv::NoPrecision, storageClass, spvType, name, initializer); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | // Return type Id of the sampled type. |
| 3556 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 3557 | { |
| 3558 | switch (sampler.type) { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3559 | case glslang::EbtInt: return builder.makeIntType(32); |
| 3560 | case glslang::EbtUint: return builder.makeUintType(32); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3561 | case glslang::EbtFloat: return builder.makeFloatType(32); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3562 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3563 | case glslang::EbtFloat16: |
| 3564 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 3565 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3566 | return builder.makeFloatType(16); |
| 3567 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3568 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3569 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3570 | return builder.makeFloatType(32); |
| 3571 | } |
| 3572 | } |
| 3573 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3574 | // If node is a swizzle operation, return the type that should be used if |
| 3575 | // the swizzle base is first consumed by another operation, before the swizzle |
| 3576 | // is applied. |
| 3577 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 3578 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3579 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3580 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 3581 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 3582 | else |
| 3583 | return spv::NoType; |
| 3584 | } |
| 3585 | |
| 3586 | // When inverting a swizzle with a parent op, this function |
| 3587 | // will apply the swizzle operation to a completed parent operation. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3588 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, |
| 3589 | spv::Id parentResult) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3590 | { |
| 3591 | std::vector<unsigned> swizzle; |
| 3592 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 3593 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 3594 | } |
| 3595 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3596 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 3597 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 3598 | { |
| 3599 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 3600 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 3601 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3602 | } |
| 3603 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3604 | // Convert from a glslang type to an SPV type, by calling into a |
| 3605 | // recursive version of this function. This establishes the inherited |
| 3606 | // layout state rooted from the top-level type. |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3607 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3608 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3609 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | // 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] | 3613 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3614 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3615 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3616 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 3617 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3618 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3619 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3620 | |
| 3621 | switch (type.getBasicType()) { |
| 3622 | case glslang::EbtVoid: |
| 3623 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3624 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3625 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3626 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3627 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 3628 | // a 32-bit int where non-0 means true. |
| 3629 | if (explicitLayout != glslang::ElpNone) |
| 3630 | spvType = builder.makeUintType(32); |
| 3631 | else |
| 3632 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3633 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3634 | case glslang::EbtInt: |
| 3635 | spvType = builder.makeIntType(32); |
| 3636 | break; |
| 3637 | case glslang::EbtUint: |
| 3638 | spvType = builder.makeUintType(32); |
| 3639 | break; |
| 3640 | case glslang::EbtFloat: |
| 3641 | spvType = builder.makeFloatType(32); |
| 3642 | break; |
| 3643 | #ifndef GLSLANG_WEB |
| 3644 | case glslang::EbtDouble: |
| 3645 | spvType = builder.makeFloatType(64); |
| 3646 | break; |
| 3647 | case glslang::EbtFloat16: |
| 3648 | spvType = builder.makeFloatType(16); |
| 3649 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3650 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3651 | spvType = builder.makeIntType(8); |
| 3652 | break; |
| 3653 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3654 | spvType = builder.makeUintType(8); |
| 3655 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3656 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3657 | spvType = builder.makeIntType(16); |
| 3658 | break; |
| 3659 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3660 | spvType = builder.makeUintType(16); |
| 3661 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3662 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3663 | spvType = builder.makeIntType(64); |
| 3664 | break; |
| 3665 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3666 | spvType = builder.makeUintType(64); |
| 3667 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3668 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 3669 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3670 | spvType = builder.makeUintType(32); |
| 3671 | break; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 3672 | case glslang::EbtAccStruct: |
| 3673 | spvType = builder.makeAccelerationStructureType(); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3674 | break; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 3675 | case glslang::EbtRayQuery: |
| 3676 | spvType = builder.makeRayQueryType(); |
| 3677 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3678 | case glslang::EbtReference: |
| 3679 | { |
| 3680 | // Make the forward pointer, then recurse to convert the structure type, then |
| 3681 | // patch up the forward pointer with a real pointer type. |
| 3682 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 3683 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 3684 | forwardPointers[type.getReferentType()] = forwardId; |
| 3685 | } |
| 3686 | spvType = forwardPointers[type.getReferentType()]; |
| 3687 | if (!forwardReferenceOnly) { |
| 3688 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 3689 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 3690 | forwardPointers[type.getReferentType()], |
| 3691 | referentType); |
| 3692 | } |
| 3693 | } |
| 3694 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3695 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3696 | case glslang::EbtSampler: |
| 3697 | { |
| 3698 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3699 | if (sampler.isPureSampler()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3700 | spvType = builder.makeSamplerType(); |
| 3701 | } else { |
| 3702 | // an image is present, make its type |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3703 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), |
| 3704 | sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), |
| 3705 | sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type)); |
| 3706 | if (sampler.isCombined()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3707 | // already has both image and sampler, make the combined type |
| 3708 | spvType = builder.makeSampledImageType(spvType); |
| 3709 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3710 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3711 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3712 | break; |
| 3713 | case glslang::EbtStruct: |
| 3714 | case glslang::EbtBlock: |
| 3715 | { |
| 3716 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3717 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3718 | |
| 3719 | // Try to share structs for different layouts, but not yet for other |
| 3720 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3721 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3722 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3723 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3724 | break; |
| 3725 | |
| 3726 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3727 | if (type.getBasicType() == glslang::EbtBlock) |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3728 | memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(glslangMembers->size()); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3729 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3730 | } |
| 3731 | break; |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 3732 | case glslang::EbtString: |
| 3733 | // no type used for OpString |
| 3734 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3735 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3736 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3737 | break; |
| 3738 | } |
| 3739 | |
| 3740 | if (type.isMatrix()) |
| 3741 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3742 | else { |
| 3743 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3744 | if (type.getVectorSize() > 1) |
| 3745 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3746 | } |
| 3747 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3748 | if (type.isCoopMat()) { |
| 3749 | builder.addCapability(spv::CapabilityCooperativeMatrixNV); |
| 3750 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
| 3751 | if (type.getBasicType() == glslang::EbtFloat16) |
| 3752 | builder.addCapability(spv::CapabilityFloat16); |
Jeff Bolz | 387657e | 2019-08-22 20:28:00 -0500 | [diff] [blame] | 3753 | if (type.getBasicType() == glslang::EbtUint8 || |
| 3754 | type.getBasicType() == glslang::EbtInt8) { |
| 3755 | builder.addCapability(spv::CapabilityInt8); |
| 3756 | } |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3757 | |
| 3758 | spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1); |
| 3759 | spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2); |
| 3760 | spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3); |
| 3761 | |
| 3762 | spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols); |
| 3763 | } |
| 3764 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3765 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3766 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3767 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3768 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3769 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3770 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3771 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3772 | // Use a dummy glslang type for querying internal strides of |
| 3773 | // arrays of arrays, but using just a one-dimensional array. |
| 3774 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3775 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3776 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3777 | |
| 3778 | // Will compute the higher-order strides here, rather than making a whole |
| 3779 | // pile of types and doing repetitive recursion on their contents. |
| 3780 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3781 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3782 | |
| 3783 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3784 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3785 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3786 | if (stride > 0) |
| 3787 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3788 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3789 | } |
| 3790 | } else { |
| 3791 | // single-dimensional array, and don't yet have stride |
| 3792 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3793 | // 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] | 3794 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3795 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3796 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3797 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3798 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3799 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3800 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3801 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3802 | else { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3803 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3804 | if (!lastBufferBlockMember) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3805 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3806 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3807 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3808 | #endif |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 3809 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3810 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3811 | if (stride > 0) |
| 3812 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | return spvType; |
| 3816 | } |
| 3817 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3818 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3819 | // |
| 3820 | // Identify interface members that don't have their required extension turned on. |
| 3821 | // |
| 3822 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3823 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3824 | #ifndef GLSLANG_WEB |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3825 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3826 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3827 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3828 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3829 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3830 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3831 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3832 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3833 | |
| 3834 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3835 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3836 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3837 | return true; |
| 3838 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3839 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3840 | return true; |
| 3841 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3842 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3843 | return true; |
| 3844 | } |
| 3845 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3846 | |
| 3847 | return false; |
| 3848 | }; |
| 3849 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3850 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3851 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3852 | // Mutually recursive with convertGlslangToSpvType(). |
| 3853 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3854 | const glslang::TTypeList* glslangMembers, |
| 3855 | glslang::TLayoutPacking explicitLayout, |
| 3856 | const glslang::TQualifier& qualifier) |
| 3857 | { |
| 3858 | // Create a vector of struct types for SPIR-V to consume |
| 3859 | std::vector<spv::Id> spvMembers; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3860 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, |
| 3861 | // except sometimes for blocks |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3862 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3863 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3864 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3865 | if (glslangMember.hiddenMember()) { |
| 3866 | ++memberDelta; |
| 3867 | if (type.getBasicType() == glslang::EbtBlock) |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3868 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3869 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3870 | if (type.getBasicType() == glslang::EbtBlock) { |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 3871 | if (filterMember(glslangMember)) { |
| 3872 | memberDelta++; |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3873 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3874 | continue; |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 3875 | } |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3876 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3877 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3878 | // modify just this child's view of the qualifier |
| 3879 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3880 | InheritQualifiers(memberQualifier, qualifier); |
| 3881 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3882 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3883 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 3884 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3885 | |
| 3886 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3887 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3888 | i == (int)glslangMembers->size() - 1; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3889 | |
| 3890 | // Make forward pointers for any pointer members, and create a list of members to |
| 3891 | // convert to spirv types after creating the struct. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 3892 | if (glslangMember.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3893 | if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) { |
| 3894 | deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier)); |
| 3895 | } |
| 3896 | spvMembers.push_back( |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3897 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 3898 | true)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3899 | } else { |
| 3900 | spvMembers.push_back( |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3901 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 3902 | false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3903 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | // Make the SPIR-V type |
| 3908 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3909 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3910 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3911 | |
| 3912 | // Decorate it |
| 3913 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3914 | |
John Kessenich | d72f488 | 2019-01-16 14:55:37 +0700 | [diff] [blame] | 3915 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3916 | auto it = deferredForwardPointers[i]; |
| 3917 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 3918 | } |
| 3919 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3920 | return spvType; |
| 3921 | } |
| 3922 | |
| 3923 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3924 | const glslang::TTypeList* glslangMembers, |
| 3925 | glslang::TLayoutPacking explicitLayout, |
| 3926 | const glslang::TQualifier& qualifier, |
| 3927 | spv::Id spvType) |
| 3928 | { |
| 3929 | // Name and decorate the non-hidden members |
| 3930 | int offset = -1; |
| 3931 | int locationOffset = 0; // for use within the members of this struct |
| 3932 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3933 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3934 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3935 | if (type.getBasicType() == glslang::EbtBlock) { |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3936 | member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3937 | if (filterMember(glslangMember)) |
| 3938 | continue; |
| 3939 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3940 | |
| 3941 | // modify just this child's view of the qualifier |
| 3942 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3943 | InheritQualifiers(memberQualifier, qualifier); |
| 3944 | |
| 3945 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3946 | if (member < 0) |
| 3947 | continue; |
| 3948 | |
| 3949 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3950 | builder.addMemberDecoration(spvType, member, |
| 3951 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 3952 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 3953 | // Add interpolation and auxiliary storage decorations only to |
| 3954 | // top-level members of Input and Output storage classes |
| 3955 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 3956 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 3957 | if (type.getBasicType() == glslang::EbtBlock || |
| 3958 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 3959 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 3960 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3961 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3962 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 3963 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3964 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3965 | } |
| 3966 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3967 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3968 | #ifndef GLSLANG_WEB |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3969 | if (type.getBasicType() == glslang::EbtBlock && |
| 3970 | qualifier.storage == glslang::EvqBuffer) { |
| 3971 | // Add memory decorations only to top-level members of shader storage block |
| 3972 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 3973 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3974 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3975 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 3976 | } |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 3977 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3978 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3979 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3980 | // Location assignment was already completed correctly by the front end, |
| 3981 | // just track whether a member needs to be decorated. |
| 3982 | // Ignore member locations if the container is an array, as that's |
| 3983 | // ill-specified and decisions have been made to not allow this. |
| 3984 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 3985 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3986 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3987 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 3988 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 3989 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 3990 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 3991 | // component, XFB, others |
| 3992 | if (glslangMember.getQualifier().hasComponent()) |
| 3993 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 3994 | glslangMember.getQualifier().layoutComponent); |
| 3995 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 3996 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 3997 | glslangMember.getQualifier().layoutXfbOffset); |
| 3998 | else if (explicitLayout != glslang::ElpNone) { |
| 3999 | // figure out what to do with offset, which is accumulating |
| 4000 | int nextOffset; |
| 4001 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 4002 | if (offset >= 0) |
| 4003 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 4004 | offset = nextOffset; |
| 4005 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4006 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4007 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 4008 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 4009 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4010 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4011 | // built-in variable decorations |
| 4012 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 4013 | if (builtIn != spv::BuiltInMax) |
| 4014 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4015 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4016 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4017 | // nonuniform |
| 4018 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 4019 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4020 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 4021 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 4022 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 4023 | memberQualifier.semanticName); |
| 4024 | } |
| 4025 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4026 | if (builtIn == spv::BuiltInLayer) { |
| 4027 | // SPV_NV_viewport_array2 extension |
| 4028 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 4029 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 4030 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 4031 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4032 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4033 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 4034 | builder.addMemberDecoration(spvType, member, |
| 4035 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 4036 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 4037 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 4038 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 4039 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4040 | } |
| 4041 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 4042 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 4043 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 4044 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 4045 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4046 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4050 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 4051 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4052 | } |
| 4053 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4054 | // Turn the expression forming the array size into an id. |
| 4055 | // This is not quite trivial, because of specialization constants. |
| 4056 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 4057 | // a specialization constant expression is. |
| 4058 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 4059 | { |
| 4060 | // First, see if this is sized with a node, meaning a specialization constant: |
| 4061 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 4062 | if (specNode != nullptr) { |
| 4063 | builder.clearAccessChain(); |
| 4064 | specNode->traverse(this); |
| 4065 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 4066 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4067 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4068 | // Otherwise, need a compile-time (front end) size, get it: |
| 4069 | int size = arraySizes.getDimSize(dim); |
| 4070 | assert(size > 0); |
| 4071 | return builder.makeUintConstant(size); |
| 4072 | } |
| 4073 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4074 | // Wrap the builder's accessChainLoad to: |
| 4075 | // - localize handling of RelaxedPrecision |
| 4076 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 4077 | // (avoids unnecessary work and possible type punning for structures) |
| 4078 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4079 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 4080 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4081 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4082 | |
| 4083 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4084 | coherentFlags |= TranslateCoherent(type); |
| 4085 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4086 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 4087 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4088 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4089 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4090 | TranslateNonUniformDecoration(type.getQualifier()), |
| 4091 | nominalTypeId, |
| 4092 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
| 4093 | TranslateMemoryScope(coherentFlags), |
| 4094 | alignment); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4095 | |
| 4096 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4097 | if (type.getBasicType() == glslang::EbtBool) { |
| 4098 | if (builder.isScalarType(nominalTypeId)) { |
| 4099 | // Conversion for bool |
| 4100 | spv::Id boolType = builder.makeBoolType(); |
| 4101 | if (nominalTypeId != boolType) |
| 4102 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 4103 | } else if (builder.isVectorType(nominalTypeId)) { |
| 4104 | // Conversion for bvec |
| 4105 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 4106 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 4107 | if (nominalTypeId != bvecType) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4108 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, |
| 4109 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4110 | } |
| 4111 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4112 | |
| 4113 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4114 | } |
| 4115 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4116 | // Wrap the builder's accessChainStore to: |
| 4117 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4118 | // |
| 4119 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4120 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 4121 | { |
| 4122 | // Need to convert to abstract types when necessary |
| 4123 | if (type.getBasicType() == glslang::EbtBool) { |
| 4124 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 4125 | |
| 4126 | if (builder.isScalarType(nominalTypeId)) { |
| 4127 | // Conversion for bool |
| 4128 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4129 | if (nominalTypeId != boolType) { |
| 4130 | // keep these outside arguments, for determinant order-of-evaluation |
| 4131 | spv::Id one = builder.makeUintConstant(1); |
| 4132 | spv::Id zero = builder.makeUintConstant(0); |
| 4133 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 4134 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 4135 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4136 | } else if (builder.isVectorType(nominalTypeId)) { |
| 4137 | // Conversion for bvec |
| 4138 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 4139 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4140 | if (nominalTypeId != bvecType) { |
| 4141 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 4142 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 4143 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 4144 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4145 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 4146 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 4147 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4148 | } |
| 4149 | } |
| 4150 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4151 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4152 | coherentFlags |= TranslateCoherent(type); |
| 4153 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4154 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 4155 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4156 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4157 | builder.accessChainStore(rvalue, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4158 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & |
| 4159 | ~spv::MemoryAccessMakePointerVisibleKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4160 | TranslateMemoryScope(coherentFlags), alignment); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4161 | } |
| 4162 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4163 | // For storing when types match at the glslang level, but not might match at the |
| 4164 | // SPIR-V level. |
| 4165 | // |
| 4166 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4167 | // 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] | 4168 | // as in a member-decorated way. |
| 4169 | // |
| 4170 | // NOTE: This function can handle any store request; if it's not special it |
| 4171 | // simplifies to a simple OpStore. |
| 4172 | // |
| 4173 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 4174 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 4175 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4176 | // we only do the complex path here if it's an aggregate |
| 4177 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4178 | accessChainStore(type, rValue); |
| 4179 | return; |
| 4180 | } |
| 4181 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4182 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4183 | spv::Id rType = builder.getTypeId(rValue); |
| 4184 | spv::Id lValue = builder.accessChainGetLValue(); |
| 4185 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 4186 | if (lType == rType) { |
| 4187 | accessChainStore(type, rValue); |
| 4188 | return; |
| 4189 | } |
| 4190 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4191 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4192 | // where the two types were the same type in GLSL. This requires member |
| 4193 | // by member copy, recursively. |
| 4194 | |
John Kessenich | fbb6bdf | 2019-01-15 21:48:27 +0700 | [diff] [blame] | 4195 | // SPIR-V 1.4 added an instruction to do help do this. |
| 4196 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 4197 | // However, bool in uniform space is changed to int, so |
| 4198 | // OpCopyLogical does not work for that. |
| 4199 | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
| 4200 | bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0); |
| 4201 | bool lBool = builder.containsType(lType, spv::OpTypeBool, 0); |
| 4202 | if (lBool == rBool) { |
| 4203 | spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue); |
| 4204 | accessChainStore(type, logicalCopy); |
| 4205 | return; |
| 4206 | } |
| 4207 | } |
| 4208 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4209 | // If an array, copy element by element. |
| 4210 | if (type.isArray()) { |
| 4211 | glslang::TType glslangElementType(type, 0); |
| 4212 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 4213 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 4214 | // get the source member |
| 4215 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4216 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4217 | // set up the target storage |
| 4218 | builder.clearAccessChain(); |
| 4219 | builder.setAccessChainLValue(lValue); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4220 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), |
| 4221 | type.getBufferReferenceAlignment()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4222 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4223 | // store the member |
| 4224 | multiTypeStore(glslangElementType, elementRValue); |
| 4225 | } |
| 4226 | } else { |
| 4227 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4228 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4229 | // loop over structure members |
| 4230 | const glslang::TTypeList& members = *type.getStruct(); |
| 4231 | for (int m = 0; m < (int)members.size(); ++m) { |
| 4232 | const glslang::TType& glslangMemberType = *members[m].type; |
| 4233 | |
| 4234 | // get the source member |
| 4235 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 4236 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 4237 | |
| 4238 | // set up the target storage |
| 4239 | builder.clearAccessChain(); |
| 4240 | builder.setAccessChainLValue(lValue); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4241 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), |
| 4242 | type.getBufferReferenceAlignment()); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4243 | |
| 4244 | // store the member |
| 4245 | multiTypeStore(glslangMemberType, memberRValue); |
| 4246 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4247 | } |
| 4248 | } |
| 4249 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4250 | // Decide whether or not this type should be |
| 4251 | // decorated with offsets and strides, and if so |
| 4252 | // whether std140 or std430 rules should be applied. |
| 4253 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 4254 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4255 | // has to be a block |
| 4256 | if (type.getBasicType() != glslang::EbtBlock) |
| 4257 | return glslang::ElpNone; |
| 4258 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4259 | // 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] | 4260 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4261 | type.getQualifier().storage != glslang::EvqBuffer && |
| 4262 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4263 | return glslang::ElpNone; |
| 4264 | |
| 4265 | // return the layout to use |
| 4266 | switch (type.getQualifier().layoutPacking) { |
| 4267 | case glslang::ElpStd140: |
| 4268 | case glslang::ElpStd430: |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4269 | case glslang::ElpScalar: |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4270 | return type.getQualifier().layoutPacking; |
| 4271 | default: |
| 4272 | return glslang::ElpNone; |
| 4273 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 4274 | } |
| 4275 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4276 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4277 | int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, |
| 4278 | glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4279 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4280 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4281 | int stride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4282 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, |
| 4283 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 4284 | |
| 4285 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4286 | } |
| 4287 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4288 | // 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] | 4289 | // when used as a member of an interface block |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4290 | int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, |
| 4291 | glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4292 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4293 | glslang::TType elementType; |
| 4294 | elementType.shallowCopy(matrixType); |
| 4295 | elementType.clearArraySizes(); |
| 4296 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4297 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4298 | int stride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4299 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, |
| 4300 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4301 | |
| 4302 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4303 | } |
| 4304 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4305 | // Given a member type of a struct, realign the current offset for it, and compute |
| 4306 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 4307 | // on the next call. |
| 4308 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 4309 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 4310 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4311 | void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, |
| 4312 | int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4313 | { |
| 4314 | // this will get a positive value when deemed necessary |
| 4315 | nextOffset = -1; |
| 4316 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4317 | // override anything in currentOffset with user-set offset |
| 4318 | if (memberType.getQualifier().hasOffset()) |
| 4319 | currentOffset = memberType.getQualifier().layoutOffset; |
| 4320 | |
| 4321 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 4322 | // in which case the following code does not matter. But, that's not quite right |
| 4323 | // once cross-compilation unit GLSL validation is done, as the original user |
| 4324 | // settings are needed in layoutOffset, and then the following will come into play. |
| 4325 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4326 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4327 | if (! memberType.getQualifier().hasOffset()) |
| 4328 | currentOffset = -1; |
| 4329 | |
| 4330 | return; |
| 4331 | } |
| 4332 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4333 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4334 | if (currentOffset < 0) |
| 4335 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4336 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4337 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 4338 | // but possibly not yet correctly aligned. |
| 4339 | |
| 4340 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4341 | int dummyStride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4342 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, |
| 4343 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4344 | |
| 4345 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4346 | // TODO: make this consistent in early phases of code: |
| 4347 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 4348 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 4349 | // 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] | 4350 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4351 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4352 | int dummySize; |
| 4353 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 4354 | if (componentAlignment <= 4) |
| 4355 | memberAlignment = componentAlignment; |
| 4356 | } |
| 4357 | |
| 4358 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4359 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4360 | |
| 4361 | // Bump up to vec4 if there is a bad straddle |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4362 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, |
| 4363 | currentOffset)) |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4364 | glslang::RoundToPow2(currentOffset, 16); |
| 4365 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4366 | nextOffset = currentOffset + memberSize; |
| 4367 | } |
| 4368 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4369 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4370 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4371 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 4372 | switch (glslangBuiltIn) |
| 4373 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4374 | case glslang::EbvPointSize: |
| 4375 | #ifndef GLSLANG_WEB |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4376 | case glslang::EbvClipDistance: |
| 4377 | case glslang::EbvCullDistance: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4378 | case glslang::EbvViewportMaskNV: |
| 4379 | case glslang::EbvSecondaryPositionNV: |
| 4380 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 4381 | case glslang::EbvPositionPerViewNV: |
| 4382 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4383 | case glslang::EbvTaskCountNV: |
| 4384 | case glslang::EbvPrimitiveCountNV: |
| 4385 | case glslang::EbvPrimitiveIndicesNV: |
| 4386 | case glslang::EbvClipDistancePerViewNV: |
| 4387 | case glslang::EbvCullDistancePerViewNV: |
| 4388 | case glslang::EbvLayerPerViewNV: |
| 4389 | case glslang::EbvMeshViewCountNV: |
| 4390 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4391 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4392 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 4393 | // Alternately, we could just call this for any glslang built-in, since the |
| 4394 | // capability already guards against duplicates. |
| 4395 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 4396 | break; |
| 4397 | default: |
| 4398 | // Capabilities were already generated when the struct was declared. |
| 4399 | break; |
| 4400 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4401 | } |
| 4402 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4403 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4404 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 4405 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4406 | } |
| 4407 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4408 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4409 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 4410 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4411 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4412 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4413 | assert(qualifier == glslang::EvqIn || |
| 4414 | qualifier == glslang::EvqOut || |
| 4415 | qualifier == glslang::EvqInOut || |
rdb | d8edfd8 | 2020-06-02 08:30:07 +0200 | [diff] [blame] | 4416 | qualifier == glslang::EvqUniform || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4417 | qualifier == glslang::EvqConstReadOnly); |
rdb | d8edfd8 | 2020-06-02 08:30:07 +0200 | [diff] [blame] | 4418 | return qualifier != glslang::EvqConstReadOnly && |
| 4419 | qualifier != glslang::EvqUniform; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | // Is parameter pass-by-original? |
| 4423 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 4424 | bool implicitThisParam) |
| 4425 | { |
| 4426 | if (implicitThisParam) // implicit this |
| 4427 | return true; |
| 4428 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4429 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4430 | return paramType.containsOpaque() || // sampler, etc. |
| 4431 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 4432 | } |
| 4433 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4434 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 4435 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4436 | { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4437 | const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, |
| 4438 | bool useVulkanMemoryModel) { |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4439 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 4440 | if (paramPrecision != spv::NoPrecision) |
| 4441 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4442 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 4443 | if (type.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4444 | // Original and non-writable params pass the pointer directly and |
| 4445 | // use restrict/aliased, others are stored to a pointer in Function |
| 4446 | // memory and use RestrictPointer/AliasedPointer. |
| 4447 | if (originalParam(type.getQualifier().storage, type, false) || |
| 4448 | !writableParam(type.getQualifier().storage)) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 4449 | decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrict : |
| 4450 | spv::DecorationAliased); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4451 | } else { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 4452 | decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrictPointerEXT : |
| 4453 | spv::DecorationAliasedPointerEXT); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4454 | } |
| 4455 | } |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4456 | }; |
| 4457 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4458 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4459 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4460 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4461 | continue; |
| 4462 | |
| 4463 | // 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] | 4464 | // 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] | 4465 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4466 | // 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] | 4467 | // function. What it is an address of varies: |
| 4468 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4469 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 4470 | // 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] | 4471 | // |
| 4472 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 4473 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4474 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 4475 | // 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] | 4476 | |
| 4477 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4478 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4479 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 4480 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4481 | #ifdef ENABLE_HLSL |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4482 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 4483 | glslangIntermediate->implicitThisName; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4484 | #else |
| 4485 | bool implicitThis = false; |
| 4486 | #endif |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4487 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4488 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4489 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4490 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4491 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4492 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 4493 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4494 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4495 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 4496 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4497 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4498 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4499 | paramTypes.push_back(typeId); |
| 4500 | } |
| 4501 | |
| 4502 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4503 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 4504 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4505 | glslFunction->getName().c_str(), paramTypes, |
| 4506 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4507 | if (implicitThis) |
| 4508 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4509 | |
| 4510 | // Track function to emit/call later |
| 4511 | functionMap[glslFunction->getName().c_str()] = function; |
| 4512 | |
| 4513 | // Set the parameter id's |
| 4514 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4515 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 4516 | // give a name too |
| 4517 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4518 | |
| 4519 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4520 | if (paramType.contains8BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4521 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4522 | if (paramType.contains16BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4523 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4524 | if (paramType.contains16BitFloat()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4525 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4526 | } |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | // Process all the initializers, while skipping the functions and link objects |
| 4531 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 4532 | { |
| 4533 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 4534 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 4535 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4536 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != |
| 4537 | glslang::EOpLinkerObjects) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4538 | |
| 4539 | // 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] | 4540 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4541 | initializer->traverse(this); |
| 4542 | } |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | // Process all the functions, while skipping initializers. |
| 4547 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4548 | { |
| 4549 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4550 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 4551 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4552 | node->traverse(this); |
| 4553 | } |
| 4554 | } |
| 4555 | |
| 4556 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 4557 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4558 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4559 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4560 | currentFunction = functionMap[node->getName().c_str()]; |
| 4561 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4562 | builder.setBuildPoint(functionBlock); |
| 4563 | } |
| 4564 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4565 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 4566 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4567 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4568 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4569 | |
| 4570 | glslang::TSampler sampler = {}; |
| 4571 | bool cubeCompare = false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4572 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4573 | bool f16ShadowCompare = false; |
| 4574 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4575 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4576 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 4577 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4578 | #ifndef GLSLANG_WEB |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4579 | f16ShadowCompare = sampler.shadow && |
| 4580 | glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4581 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4582 | } |
| 4583 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4584 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 4585 | builder.clearAccessChain(); |
| 4586 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4587 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4588 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4589 | // Special case l-value operands |
| 4590 | bool lvalue = false; |
| 4591 | switch (node.getOp()) { |
| 4592 | case glslang::EOpImageAtomicAdd: |
| 4593 | case glslang::EOpImageAtomicMin: |
| 4594 | case glslang::EOpImageAtomicMax: |
| 4595 | case glslang::EOpImageAtomicAnd: |
| 4596 | case glslang::EOpImageAtomicOr: |
| 4597 | case glslang::EOpImageAtomicXor: |
| 4598 | case glslang::EOpImageAtomicExchange: |
| 4599 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4600 | case glslang::EOpImageAtomicLoad: |
| 4601 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4602 | if (i == 0) |
| 4603 | lvalue = true; |
| 4604 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4605 | case glslang::EOpSparseImageLoad: |
| 4606 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 4607 | lvalue = true; |
| 4608 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4609 | case glslang::EOpSparseTexture: |
| 4610 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 4611 | lvalue = true; |
| 4612 | break; |
| 4613 | case glslang::EOpSparseTextureClamp: |
| 4614 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 4615 | lvalue = true; |
| 4616 | break; |
| 4617 | case glslang::EOpSparseTextureLod: |
| 4618 | case glslang::EOpSparseTextureOffset: |
| 4619 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 4620 | lvalue = true; |
| 4621 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4622 | case glslang::EOpSparseTextureFetch: |
| 4623 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 4624 | lvalue = true; |
| 4625 | break; |
| 4626 | case glslang::EOpSparseTextureFetchOffset: |
| 4627 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 4628 | lvalue = true; |
| 4629 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4630 | case glslang::EOpSparseTextureLodOffset: |
| 4631 | case glslang::EOpSparseTextureGrad: |
| 4632 | case glslang::EOpSparseTextureOffsetClamp: |
| 4633 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 4634 | lvalue = true; |
| 4635 | break; |
| 4636 | case glslang::EOpSparseTextureGradOffset: |
| 4637 | case glslang::EOpSparseTextureGradClamp: |
| 4638 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 4639 | lvalue = true; |
| 4640 | break; |
| 4641 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 4642 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 4643 | lvalue = true; |
| 4644 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4645 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4646 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 4647 | lvalue = true; |
| 4648 | break; |
| 4649 | case glslang::EOpSparseTextureGatherOffset: |
| 4650 | case glslang::EOpSparseTextureGatherOffsets: |
| 4651 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 4652 | lvalue = true; |
| 4653 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4654 | case glslang::EOpSparseTextureGatherLod: |
| 4655 | if (i == 3) |
| 4656 | lvalue = true; |
| 4657 | break; |
| 4658 | case glslang::EOpSparseTextureGatherLodOffset: |
| 4659 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 4660 | if (i == 4) |
| 4661 | lvalue = true; |
| 4662 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4663 | case glslang::EOpSparseImageLoadLod: |
| 4664 | if (i == 3) |
| 4665 | lvalue = true; |
| 4666 | break; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4667 | case glslang::EOpImageSampleFootprintNV: |
| 4668 | if (i == 4) |
| 4669 | lvalue = true; |
| 4670 | break; |
| 4671 | case glslang::EOpImageSampleFootprintClampNV: |
| 4672 | case glslang::EOpImageSampleFootprintLodNV: |
| 4673 | if (i == 5) |
| 4674 | lvalue = true; |
| 4675 | break; |
| 4676 | case glslang::EOpImageSampleFootprintGradNV: |
| 4677 | if (i == 6) |
| 4678 | lvalue = true; |
| 4679 | break; |
| 4680 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 4681 | if (i == 7) |
| 4682 | lvalue = true; |
| 4683 | break; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4684 | default: |
| 4685 | break; |
| 4686 | } |
| 4687 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4688 | if (lvalue) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4689 | arguments.push_back(builder.accessChainGetLValue()); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4690 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 4691 | lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
| 4692 | } else |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4693 | #endif |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4694 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4695 | } |
| 4696 | } |
| 4697 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4698 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4699 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4700 | builder.clearAccessChain(); |
| 4701 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4702 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4703 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4704 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4705 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 4706 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4707 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4708 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4709 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 4710 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4711 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4712 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4713 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4714 | const glslang::TType &imageType = node->getAsAggregate() |
| 4715 | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 4716 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4717 | const glslang::TSampler sampler = imageType.getSampler(); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4718 | #ifdef GLSLANG_WEB |
| 4719 | const bool f16ShadowCompare = false; |
| 4720 | #else |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4721 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4722 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 4723 | : false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4724 | #endif |
| 4725 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4726 | const auto signExtensionMask = [&]() { |
| 4727 | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
| 4728 | if (sampler.type == glslang::EbtUint) |
| 4729 | return spv::ImageOperandsZeroExtendMask; |
| 4730 | else if (sampler.type == glslang::EbtInt) |
| 4731 | return spv::ImageOperandsSignExtendMask; |
| 4732 | } |
| 4733 | return spv::ImageOperandsMaskNone; |
| 4734 | }; |
| 4735 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4736 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 4737 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4738 | std::vector<spv::Id> arguments; |
| 4739 | if (node->getAsAggregate()) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4740 | translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4741 | else |
| 4742 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | 12c155f | 2020-06-30 07:52:05 -0600 | [diff] [blame] | 4743 | spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4744 | |
| 4745 | spv::Builder::TextureParameters params = { }; |
| 4746 | params.sampler = arguments[0]; |
| 4747 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4748 | glslang::TCrackedTextureOp cracked; |
| 4749 | node->crackTexture(sampler, cracked); |
| 4750 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4751 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4752 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4753 | // Check for queries |
| 4754 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4755 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 4756 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 4757 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 4758 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4759 | switch (node->getOp()) { |
| 4760 | case glslang::EOpImageQuerySize: |
| 4761 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4762 | if (arguments.size() > 1) { |
| 4763 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4764 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4765 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4766 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4767 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4768 | case glslang::EOpImageQuerySamples: |
| 4769 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4770 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4771 | case glslang::EOpTextureQueryLod: |
| 4772 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4773 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4774 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4775 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4776 | case glslang::EOpSparseTexelsResident: |
| 4777 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4778 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4779 | default: |
| 4780 | assert(0); |
| 4781 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4782 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4783 | } |
| 4784 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4785 | int components = node->getType().getVectorSize(); |
| 4786 | |
| 4787 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 4788 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 4789 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 4790 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 4791 | // here around e.g. which ones return scalars or other types. |
| 4792 | components = 4; |
| 4793 | } |
| 4794 | |
| 4795 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 4796 | |
| 4797 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 4798 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4799 | // Check for image functions other than queries |
| 4800 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4801 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4802 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4803 | spv::IdImmediate image = { true, *(opIt++) }; |
| 4804 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4805 | |
| 4806 | // Handle subpass operations |
| 4807 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 4808 | // built-in function. |
| 4809 | if (cracked.subpass) { |
| 4810 | // add on the (0,0) coordinate |
| 4811 | spv::Id zero = builder.makeIntConstant(0); |
| 4812 | std::vector<spv::Id> comps; |
| 4813 | comps.push_back(zero); |
| 4814 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4815 | spv::IdImmediate coord = { true, |
| 4816 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 4817 | operands.push_back(coord); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4818 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
| 4819 | imageOperands.word = imageOperands.word | signExtensionMask(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4820 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4821 | imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
| 4822 | } |
| 4823 | if (imageOperands.word != spv::ImageOperandsMaskNone) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4824 | operands.push_back(imageOperands); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4825 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4826 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 4827 | operands.push_back(imageOperand); |
| 4828 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4829 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4830 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 4831 | builder.setPrecision(result, precision); |
| 4832 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4833 | } |
| 4834 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4835 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 4836 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4837 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4838 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4839 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4840 | mask = mask | spv::ImageOperandsSampleMask; |
| 4841 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4842 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4843 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4844 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4845 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4846 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4847 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4848 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4849 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4850 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4851 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4852 | operands.push_back(imageOperands); |
| 4853 | } |
| 4854 | if (mask & spv::ImageOperandsSampleMask) { |
| 4855 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4856 | operands.push_back(imageOperand); |
| 4857 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4858 | if (mask & spv::ImageOperandsLodMask) { |
| 4859 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4860 | operands.push_back(imageOperand); |
| 4861 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4862 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4863 | spv::IdImmediate imageOperand = { true, |
| 4864 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4865 | operands.push_back(imageOperand); |
| 4866 | } |
| 4867 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4868 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4869 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 4870 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4871 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4872 | builder.setPrecision(result[0], precision); |
| 4873 | |
| 4874 | // If needed, add a conversion constructor to the proper size. |
| 4875 | if (components != node->getType().getVectorSize()) |
| 4876 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4877 | |
| 4878 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4879 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4880 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4881 | // Push the texel value before the operands |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4882 | if (sampler.isMultiSample() || cracked.lod) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4883 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4884 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4885 | } else { |
| 4886 | spv::IdImmediate texel = { true, *opIt }; |
| 4887 | operands.push_back(texel); |
| 4888 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4889 | |
| 4890 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4891 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4892 | mask = mask | spv::ImageOperandsSampleMask; |
| 4893 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4894 | if (cracked.lod) { |
| 4895 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4896 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4897 | mask = mask | spv::ImageOperandsLodMask; |
| 4898 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4899 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4900 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4901 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4902 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4903 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4904 | operands.push_back(imageOperands); |
| 4905 | } |
| 4906 | if (mask & spv::ImageOperandsSampleMask) { |
| 4907 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4908 | operands.push_back(imageOperand); |
| 4909 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4910 | if (mask & spv::ImageOperandsLodMask) { |
| 4911 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4912 | operands.push_back(imageOperand); |
| 4913 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4914 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4915 | spv::IdImmediate imageOperand = { true, |
| 4916 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4917 | operands.push_back(imageOperand); |
| 4918 | } |
| 4919 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4920 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4921 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 4922 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4923 | return spv::NoResult; |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4924 | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
| 4925 | node->getOp() == glslang::EOpSparseImageLoadLod) { |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4926 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4927 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4928 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4929 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4930 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4931 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4932 | mask = mask | spv::ImageOperandsSampleMask; |
| 4933 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4934 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4935 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4936 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4937 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4938 | mask = mask | spv::ImageOperandsLodMask; |
| 4939 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4940 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4941 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4942 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 4943 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4944 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4945 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4946 | } |
| 4947 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4948 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4949 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4950 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4951 | if (mask & spv::ImageOperandsLodMask) { |
| 4952 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4953 | operands.push_back(imageOperand); |
| 4954 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4955 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4956 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope( |
| 4957 | TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4958 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | // Create the return type that was a special structure |
| 4962 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 4963 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4964 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 4965 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 4966 | |
| 4967 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 4968 | |
| 4969 | // Decode the return type |
| 4970 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 4971 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 4972 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 4973 | // Process image atomic operations |
| 4974 | |
| 4975 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 4976 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4977 | // For non-MS, the sample value should be 0 |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 4978 | spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 4979 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4980 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4981 | spv::Id resultTypeId; |
| 4982 | // imageAtomicStore has a void return type so base the pointer type on |
| 4983 | // the type of the value operand. |
| 4984 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fb18b6d | 2020-02-22 22:04:31 +0800 | [diff] [blame] | 4985 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(*opIt)); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4986 | } else { |
| 4987 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 4988 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 4989 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Jeff Bolz | 39ffdaf | 2020-03-09 10:48:12 -0500 | [diff] [blame] | 4990 | if (imageType.getQualifier().nonUniform) { |
| 4991 | builder.addDecoration(pointer, spv::DecorationNonUniformEXT); |
| 4992 | } |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4993 | |
| 4994 | std::vector<spv::Id> operands; |
| 4995 | operands.push_back(pointer); |
| 4996 | for (; opIt != arguments.end(); ++opIt) |
| 4997 | operands.push_back(*opIt); |
| 4998 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4999 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), |
| 5000 | lvalueCoherentFlags); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5004 | #ifndef GLSLANG_WEB |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 5005 | // Check for fragment mask functions other than queries |
| 5006 | if (cracked.fragMask) { |
| 5007 | assert(sampler.ms); |
| 5008 | |
| 5009 | auto opIt = arguments.begin(); |
| 5010 | std::vector<spv::Id> operands; |
| 5011 | |
| 5012 | // Extract the image if necessary |
| 5013 | if (builder.isSampledImage(params.sampler)) |
| 5014 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 5015 | |
| 5016 | operands.push_back(params.sampler); |
| 5017 | ++opIt; |
| 5018 | |
| 5019 | if (sampler.isSubpass()) { |
| 5020 | // add on the (0,0) coordinate |
| 5021 | spv::Id zero = builder.makeIntConstant(0); |
| 5022 | std::vector<spv::Id> comps; |
| 5023 | comps.push_back(zero); |
| 5024 | comps.push_back(zero); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5025 | operands.push_back(builder.makeCompositeConstant( |
| 5026 | builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | for (; opIt != arguments.end(); ++opIt) |
| 5030 | operands.push_back(*opIt); |
| 5031 | |
| 5032 | spv::Op fragMaskOp = spv::OpNop; |
| 5033 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 5034 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 5035 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 5036 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 5037 | |
| 5038 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 5039 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 5040 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 5041 | } |
| 5042 | #endif |
| 5043 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5044 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5045 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5046 | bool imageFootprint = node->isImageFootprint(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5047 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 5048 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5049 | // check for bias argument |
| 5050 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5051 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5052 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5053 | if (cracked.gather) |
| 5054 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 5055 | |
| 5056 | if (f16ShadowCompare) |
| 5057 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5058 | if (cracked.offset) |
| 5059 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5060 | else if (cracked.offsets) |
| 5061 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5062 | if (cracked.grad) |
| 5063 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5064 | if (cracked.lodClamp) |
| 5065 | ++nonBiasArgCount; |
| 5066 | if (sparse) |
| 5067 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5068 | if (imageFootprint) |
| 5069 | //Following three extra arguments |
| 5070 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 5071 | nonBiasArgCount += 3; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5072 | if ((int)arguments.size() > nonBiasArgCount) |
| 5073 | bias = true; |
| 5074 | } |
| 5075 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 5076 | // See if the sampler param should really be just the SPV image part |
| 5077 | if (cracked.fetch) { |
| 5078 | // a fetch needs to have the image extracted first |
| 5079 | if (builder.isSampledImage(params.sampler)) |
| 5080 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 5081 | } |
| 5082 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5083 | #ifndef GLSLANG_WEB |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5084 | if (cracked.gather) { |
| 5085 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 5086 | if (bias || cracked.lod || |
| 5087 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 5088 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 5089 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5090 | } |
| 5091 | } |
| 5092 | #endif |
| 5093 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5094 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5095 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5096 | params.coords = arguments[1]; |
| 5097 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 5098 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5099 | |
| 5100 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 5101 | if (cubeCompare || f16ShadowCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5102 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5103 | ++extraArgs; |
| 5104 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5105 | params.Dref = arguments[2]; |
| 5106 | ++extraArgs; |
| 5107 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5108 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5109 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5110 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5111 | 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] | 5112 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5113 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 5114 | indexes.push_back(dRefComp); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5115 | params.Dref = builder.createCompositeExtract(params.coords, |
| 5116 | builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5117 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5118 | |
| 5119 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5120 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 5121 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5122 | ++extraArgs; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5123 | } else if (glslangIntermediate->getStage() != EShLangFragment && |
| 5124 | !(glslangIntermediate->getStage() == EShLangCompute && |
| 5125 | glslangIntermediate->hasLayoutDerivativeModeNone())) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 5126 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 5127 | noImplicitLod = true; |
| 5128 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5129 | |
| 5130 | // multisample |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5131 | if (sampler.isMultiSample()) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 5132 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5133 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5134 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5135 | |
| 5136 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5137 | if (cracked.grad) { |
| 5138 | params.gradX = arguments[2 + extraArgs]; |
| 5139 | params.gradY = arguments[3 + extraArgs]; |
| 5140 | extraArgs += 2; |
| 5141 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5142 | |
| 5143 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5144 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5145 | params.offset = arguments[2 + extraArgs]; |
| 5146 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5147 | } else if (cracked.offsets) { |
| 5148 | params.offsets = arguments[2 + extraArgs]; |
| 5149 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5150 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5151 | |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5152 | #ifndef GLSLANG_WEB |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5153 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5154 | if (cracked.lodClamp) { |
| 5155 | params.lodClamp = arguments[2 + extraArgs]; |
| 5156 | ++extraArgs; |
| 5157 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5158 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5159 | if (sparse) { |
| 5160 | params.texelOut = arguments[2 + extraArgs]; |
| 5161 | ++extraArgs; |
| 5162 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5163 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5164 | if (cracked.gather && ! sampler.shadow) { |
| 5165 | // default component is 0, if missing, otherwise an argument |
| 5166 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5167 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5168 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5169 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5170 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5171 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5172 | spv::Id resultStruct = spv::NoResult; |
| 5173 | if (imageFootprint) { |
| 5174 | //Following three extra arguments |
| 5175 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 5176 | params.granularity = arguments[2 + extraArgs]; |
| 5177 | params.coarse = arguments[3 + extraArgs]; |
| 5178 | resultStruct = arguments[4 + extraArgs]; |
| 5179 | extraArgs += 3; |
| 5180 | } |
| 5181 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5182 | // bias |
| 5183 | if (bias) { |
| 5184 | params.bias = arguments[2 + extraArgs]; |
| 5185 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5186 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5187 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5188 | #ifndef GLSLANG_WEB |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5189 | if (imageFootprint) { |
| 5190 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 5191 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 5192 | |
| 5193 | |
| 5194 | //resultStructType(OpenGL type) contains 5 elements: |
| 5195 | //struct gl_TextureFootprint2DNV { |
| 5196 | // uvec2 anchor; |
| 5197 | // uvec2 offset; |
| 5198 | // uvec2 mask; |
| 5199 | // uint lod; |
| 5200 | // uint granularity; |
| 5201 | //}; |
| 5202 | //or |
| 5203 | //struct gl_TextureFootprint3DNV { |
| 5204 | // uvec3 anchor; |
| 5205 | // uvec3 offset; |
| 5206 | // uvec2 mask; |
| 5207 | // uint lod; |
| 5208 | // uint granularity; |
| 5209 | //}; |
| 5210 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 5211 | assert(builder.isStructType(resultStructType)); |
| 5212 | |
| 5213 | //resType (SPIR-V type) contains 6 elements: |
| 5214 | //Member 0 must be a Boolean type scalar(LOD), |
| 5215 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 5216 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 5217 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 5218 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 5219 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 5220 | std::vector<spv::Id> members; |
| 5221 | members.push_back(resultType()); |
| 5222 | for (int i = 0; i < 5; i++) { |
| 5223 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 5224 | } |
| 5225 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 5226 | |
| 5227 | //call ImageFootprintNV |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5228 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
| 5229 | cracked.gather, noImplicitLod, params, signExtensionMask()); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5230 | |
| 5231 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 5232 | for (int i = 0; i < 5; i++) { |
| 5233 | builder.clearAccessChain(); |
| 5234 | builder.setAccessChainLValue(resultStruct); |
| 5235 | |
| 5236 | //Accessing to a struct we created, no coherent flag is set |
| 5237 | spv::Builder::AccessChain::CoherentFlags flags; |
| 5238 | flags.clear(); |
| 5239 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 5240 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5241 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), |
| 5242 | i+1)); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5243 | } |
| 5244 | return builder.createCompositeExtract(res, resultType(), 0); |
| 5245 | } |
| 5246 | #endif |
| 5247 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5248 | // projective component (might not to move) |
| 5249 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 5250 | // are divided by the last component of P." |
| 5251 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 5252 | // unused components will appear after all used components." |
| 5253 | if (cracked.proj) { |
| 5254 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 5255 | int projTargetComp; |
| 5256 | switch (sampler.dim) { |
| 5257 | case glslang::Esd1D: projTargetComp = 1; break; |
| 5258 | case glslang::Esd2D: projTargetComp = 2; break; |
| 5259 | case glslang::EsdRect: projTargetComp = 2; break; |
| 5260 | default: projTargetComp = projSourceComp; break; |
| 5261 | } |
| 5262 | // copy the projective coordinate if we have to |
| 5263 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 5264 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5265 | builder.getScalarTypeId(builder.getTypeId(params.coords)), projSourceComp); |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5266 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5267 | builder.getTypeId(params.coords), projTargetComp); |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5268 | } |
| 5269 | } |
| 5270 | |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 5271 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5272 | // nonprivate |
| 5273 | if (imageType.getQualifier().nonprivate) { |
| 5274 | params.nonprivate = true; |
| 5275 | } |
| 5276 | |
| 5277 | // volatile |
| 5278 | if (imageType.getQualifier().volatil) { |
| 5279 | params.volatil = true; |
| 5280 | } |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 5281 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5282 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 5283 | std::vector<spv::Id> result( 1, |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5284 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
| 5285 | noImplicitLod, params, signExtensionMask()) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 5286 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 5287 | |
| 5288 | if (components != node->getType().getVectorSize()) |
| 5289 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 5290 | |
| 5291 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 5295 | { |
| 5296 | // Grab the function's pointer from the previously created function |
| 5297 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 5298 | if (! function) |
| 5299 | return 0; |
| 5300 | |
| 5301 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 5302 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 5303 | |
| 5304 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 5305 | // |
| 5306 | // These imply we need a four step process: |
| 5307 | // 1. Evaluate the arguments |
| 5308 | // 2. Allocate and make copies of in, out, and inout arguments |
| 5309 | // 3. Make the call |
| 5310 | // 4. Copy back the results |
| 5311 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5312 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5313 | std::vector<spv::Builder::AccessChain> lValues; |
| 5314 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5315 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5316 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5317 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5318 | // build l-value |
| 5319 | builder.clearAccessChain(); |
| 5320 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5321 | // 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] | 5322 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 5323 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5324 | // save l-value |
| 5325 | lValues.push_back(builder.getAccessChain()); |
| 5326 | } else { |
| 5327 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5328 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 5333 | // copy the original into that space. |
| 5334 | // |
| 5335 | // Also, build up the list of actual arguments to pass in for the call |
| 5336 | int lValueCount = 0; |
| 5337 | int rValueCount = 0; |
| 5338 | std::vector<spv::Id> spvArgs; |
| 5339 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 5340 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5341 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 5342 | builder.setAccessChain(lValues[lValueCount]); |
| 5343 | arg = builder.accessChainGetLValue(); |
| 5344 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5345 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5346 | // need space to hold the copy |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5347 | arg = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5348 | builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5349 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 5350 | // need to copy the input into output space |
| 5351 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5352 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 5353 | builder.clearAccessChain(); |
| 5354 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5355 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5356 | } |
| 5357 | ++lValueCount; |
| 5358 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5359 | // process r-value, which involves a copy for a type mismatch |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 5360 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a]) || |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5361 | TranslatePrecisionDecoration(*argTypes[a]) != function->getParamPrecision(a)) |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 5362 | { |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5363 | 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] | 5364 | builder.clearAccessChain(); |
| 5365 | builder.setAccessChainLValue(argCopy); |
| 5366 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5367 | arg = builder.createLoad(argCopy, function->getParamPrecision(a)); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5368 | } else |
| 5369 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5370 | ++rValueCount; |
| 5371 | } |
| 5372 | spvArgs.push_back(arg); |
| 5373 | } |
| 5374 | |
| 5375 | // 3. Make the call. |
| 5376 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5377 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5378 | |
| 5379 | // 4. Copy back out an "out" arguments. |
| 5380 | lValueCount = 0; |
| 5381 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5382 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5383 | ++lValueCount; |
| 5384 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5385 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5386 | spv::Id copy = builder.createLoad(spvArgs[a], spv::NoPrecision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5387 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5388 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5389 | } |
| 5390 | ++lValueCount; |
| 5391 | } |
| 5392 | } |
| 5393 | |
| 5394 | return result; |
| 5395 | } |
| 5396 | |
| 5397 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5398 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5399 | spv::Id typeId, spv::Id left, spv::Id right, |
| 5400 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 5401 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5402 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5403 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5404 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5405 | |
| 5406 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5407 | 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] | 5408 | bool comparison = false; |
| 5409 | |
| 5410 | switch (op) { |
| 5411 | case glslang::EOpAdd: |
| 5412 | case glslang::EOpAddAssign: |
| 5413 | if (isFloat) |
| 5414 | binOp = spv::OpFAdd; |
| 5415 | else |
| 5416 | binOp = spv::OpIAdd; |
| 5417 | break; |
| 5418 | case glslang::EOpSub: |
| 5419 | case glslang::EOpSubAssign: |
| 5420 | if (isFloat) |
| 5421 | binOp = spv::OpFSub; |
| 5422 | else |
| 5423 | binOp = spv::OpISub; |
| 5424 | break; |
| 5425 | case glslang::EOpMul: |
| 5426 | case glslang::EOpMulAssign: |
| 5427 | if (isFloat) |
| 5428 | binOp = spv::OpFMul; |
| 5429 | else |
| 5430 | binOp = spv::OpIMul; |
| 5431 | break; |
| 5432 | case glslang::EOpVectorTimesScalar: |
| 5433 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5434 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5435 | if (builder.isVector(right)) |
| 5436 | std::swap(left, right); |
| 5437 | assert(builder.isScalar(right)); |
| 5438 | needMatchingVectors = false; |
| 5439 | binOp = spv::OpVectorTimesScalar; |
t.jung | 697fdf0 | 2018-11-14 13:04:39 +0100 | [diff] [blame] | 5440 | } else if (isFloat) |
| 5441 | binOp = spv::OpFMul; |
| 5442 | else |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5443 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5444 | break; |
| 5445 | case glslang::EOpVectorTimesMatrix: |
| 5446 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5447 | binOp = spv::OpVectorTimesMatrix; |
| 5448 | break; |
| 5449 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5450 | binOp = spv::OpMatrixTimesVector; |
| 5451 | break; |
| 5452 | case glslang::EOpMatrixTimesScalar: |
| 5453 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5454 | binOp = spv::OpMatrixTimesScalar; |
| 5455 | break; |
| 5456 | case glslang::EOpMatrixTimesMatrix: |
| 5457 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5458 | binOp = spv::OpMatrixTimesMatrix; |
| 5459 | break; |
| 5460 | case glslang::EOpOuterProduct: |
| 5461 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5462 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5463 | break; |
| 5464 | |
| 5465 | case glslang::EOpDiv: |
| 5466 | case glslang::EOpDivAssign: |
| 5467 | if (isFloat) |
| 5468 | binOp = spv::OpFDiv; |
| 5469 | else if (isUnsigned) |
| 5470 | binOp = spv::OpUDiv; |
| 5471 | else |
| 5472 | binOp = spv::OpSDiv; |
| 5473 | break; |
| 5474 | case glslang::EOpMod: |
| 5475 | case glslang::EOpModAssign: |
| 5476 | if (isFloat) |
| 5477 | binOp = spv::OpFMod; |
| 5478 | else if (isUnsigned) |
| 5479 | binOp = spv::OpUMod; |
| 5480 | else |
| 5481 | binOp = spv::OpSMod; |
| 5482 | break; |
| 5483 | case glslang::EOpRightShift: |
| 5484 | case glslang::EOpRightShiftAssign: |
| 5485 | if (isUnsigned) |
| 5486 | binOp = spv::OpShiftRightLogical; |
| 5487 | else |
| 5488 | binOp = spv::OpShiftRightArithmetic; |
| 5489 | break; |
| 5490 | case glslang::EOpLeftShift: |
| 5491 | case glslang::EOpLeftShiftAssign: |
| 5492 | binOp = spv::OpShiftLeftLogical; |
| 5493 | break; |
| 5494 | case glslang::EOpAnd: |
| 5495 | case glslang::EOpAndAssign: |
| 5496 | binOp = spv::OpBitwiseAnd; |
| 5497 | break; |
| 5498 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5499 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5500 | binOp = spv::OpLogicalAnd; |
| 5501 | break; |
| 5502 | case glslang::EOpInclusiveOr: |
| 5503 | case glslang::EOpInclusiveOrAssign: |
| 5504 | binOp = spv::OpBitwiseOr; |
| 5505 | break; |
| 5506 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5507 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5508 | binOp = spv::OpLogicalOr; |
| 5509 | break; |
| 5510 | case glslang::EOpExclusiveOr: |
| 5511 | case glslang::EOpExclusiveOrAssign: |
| 5512 | binOp = spv::OpBitwiseXor; |
| 5513 | break; |
| 5514 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5515 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5516 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5517 | break; |
| 5518 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 5519 | case glslang::EOpAbsDifference: |
| 5520 | binOp = isUnsigned ? spv::OpAbsUSubINTEL : spv::OpAbsISubINTEL; |
| 5521 | break; |
| 5522 | |
| 5523 | case glslang::EOpAddSaturate: |
| 5524 | binOp = isUnsigned ? spv::OpUAddSatINTEL : spv::OpIAddSatINTEL; |
| 5525 | break; |
| 5526 | |
| 5527 | case glslang::EOpSubSaturate: |
| 5528 | binOp = isUnsigned ? spv::OpUSubSatINTEL : spv::OpISubSatINTEL; |
| 5529 | break; |
| 5530 | |
| 5531 | case glslang::EOpAverage: |
| 5532 | binOp = isUnsigned ? spv::OpUAverageINTEL : spv::OpIAverageINTEL; |
| 5533 | break; |
| 5534 | |
| 5535 | case glslang::EOpAverageRounded: |
| 5536 | binOp = isUnsigned ? spv::OpUAverageRoundedINTEL : spv::OpIAverageRoundedINTEL; |
| 5537 | break; |
| 5538 | |
| 5539 | case glslang::EOpMul32x16: |
| 5540 | binOp = isUnsigned ? spv::OpUMul32x16INTEL : spv::OpIMul32x16INTEL; |
| 5541 | break; |
| 5542 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5543 | case glslang::EOpLessThan: |
| 5544 | case glslang::EOpGreaterThan: |
| 5545 | case glslang::EOpLessThanEqual: |
| 5546 | case glslang::EOpGreaterThanEqual: |
| 5547 | case glslang::EOpEqual: |
| 5548 | case glslang::EOpNotEqual: |
| 5549 | case glslang::EOpVectorEqual: |
| 5550 | case glslang::EOpVectorNotEqual: |
| 5551 | comparison = true; |
| 5552 | break; |
| 5553 | default: |
| 5554 | break; |
| 5555 | } |
| 5556 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5557 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5558 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5559 | assert(comparison == false); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5560 | if (builder.isMatrix(left) || builder.isMatrix(right) || |
| 5561 | builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5562 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5563 | |
| 5564 | // 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] | 5565 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5566 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5567 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5568 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5569 | decorations.addNoContraction(builder, result); |
| 5570 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5571 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | if (! comparison) |
| 5575 | return 0; |
| 5576 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5577 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5578 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 5579 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5580 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 5581 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5582 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5583 | return result; |
| 5584 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5585 | |
| 5586 | switch (op) { |
| 5587 | case glslang::EOpLessThan: |
| 5588 | if (isFloat) |
| 5589 | binOp = spv::OpFOrdLessThan; |
| 5590 | else if (isUnsigned) |
| 5591 | binOp = spv::OpULessThan; |
| 5592 | else |
| 5593 | binOp = spv::OpSLessThan; |
| 5594 | break; |
| 5595 | case glslang::EOpGreaterThan: |
| 5596 | if (isFloat) |
| 5597 | binOp = spv::OpFOrdGreaterThan; |
| 5598 | else if (isUnsigned) |
| 5599 | binOp = spv::OpUGreaterThan; |
| 5600 | else |
| 5601 | binOp = spv::OpSGreaterThan; |
| 5602 | break; |
| 5603 | case glslang::EOpLessThanEqual: |
| 5604 | if (isFloat) |
| 5605 | binOp = spv::OpFOrdLessThanEqual; |
| 5606 | else if (isUnsigned) |
| 5607 | binOp = spv::OpULessThanEqual; |
| 5608 | else |
| 5609 | binOp = spv::OpSLessThanEqual; |
| 5610 | break; |
| 5611 | case glslang::EOpGreaterThanEqual: |
| 5612 | if (isFloat) |
| 5613 | binOp = spv::OpFOrdGreaterThanEqual; |
| 5614 | else if (isUnsigned) |
| 5615 | binOp = spv::OpUGreaterThanEqual; |
| 5616 | else |
| 5617 | binOp = spv::OpSGreaterThanEqual; |
| 5618 | break; |
| 5619 | case glslang::EOpEqual: |
| 5620 | case glslang::EOpVectorEqual: |
| 5621 | if (isFloat) |
| 5622 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5623 | else if (isBool) |
| 5624 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5625 | else |
| 5626 | binOp = spv::OpIEqual; |
| 5627 | break; |
| 5628 | case glslang::EOpNotEqual: |
| 5629 | case glslang::EOpVectorNotEqual: |
| 5630 | if (isFloat) |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 5631 | binOp = spv::OpFUnordNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5632 | else if (isBool) |
| 5633 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5634 | else |
| 5635 | binOp = spv::OpINotEqual; |
| 5636 | break; |
| 5637 | default: |
| 5638 | break; |
| 5639 | } |
| 5640 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5641 | if (binOp != spv::OpNop) { |
| 5642 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5643 | decorations.addNoContraction(builder, result); |
| 5644 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5645 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5646 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5647 | |
| 5648 | return 0; |
| 5649 | } |
| 5650 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5651 | // |
| 5652 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 5653 | // These can be any of: |
| 5654 | // |
| 5655 | // matrix * scalar |
| 5656 | // scalar * matrix |
| 5657 | // matrix * matrix linear algebraic |
| 5658 | // matrix * vector |
| 5659 | // vector * matrix |
| 5660 | // matrix * matrix componentwise |
| 5661 | // matrix op matrix op in {+, -, /} |
| 5662 | // matrix op scalar op in {+, -, /} |
| 5663 | // scalar op matrix op in {+, -, /} |
| 5664 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5665 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5666 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5667 | { |
| 5668 | bool firstClass = true; |
| 5669 | |
| 5670 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 5671 | switch (op) { |
| 5672 | case spv::OpFDiv: |
| 5673 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 5674 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 5675 | spv::Id resultType = builder.getTypeId(right); |
| 5676 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5677 | op = spv::OpMatrixTimesScalar; |
| 5678 | } else |
| 5679 | firstClass = false; |
| 5680 | break; |
| 5681 | case spv::OpMatrixTimesScalar: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5682 | if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5683 | std::swap(left, right); |
| 5684 | assert(builder.isScalar(right)); |
| 5685 | break; |
| 5686 | case spv::OpVectorTimesMatrix: |
| 5687 | assert(builder.isVector(left)); |
| 5688 | assert(builder.isMatrix(right)); |
| 5689 | break; |
| 5690 | case spv::OpMatrixTimesVector: |
| 5691 | assert(builder.isMatrix(left)); |
| 5692 | assert(builder.isVector(right)); |
| 5693 | break; |
| 5694 | case spv::OpMatrixTimesMatrix: |
| 5695 | assert(builder.isMatrix(left)); |
| 5696 | assert(builder.isMatrix(right)); |
| 5697 | break; |
| 5698 | default: |
| 5699 | firstClass = false; |
| 5700 | break; |
| 5701 | } |
| 5702 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5703 | if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 5704 | firstClass = true; |
| 5705 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5706 | if (firstClass) { |
| 5707 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5708 | decorations.addNoContraction(builder, result); |
| 5709 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5710 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5711 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5712 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5713 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5714 | // The result type of all of them is the same type as the (a) matrix operand. |
| 5715 | // The algorithm is to: |
| 5716 | // - break the matrix(es) into vectors |
| 5717 | // - smear any scalar to a vector |
| 5718 | // - do vector operations |
| 5719 | // - make a matrix out the vector results |
| 5720 | switch (op) { |
| 5721 | case spv::OpFAdd: |
| 5722 | case spv::OpFSub: |
| 5723 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5724 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5725 | case spv::OpFMul: |
| 5726 | { |
| 5727 | // one time set up... |
| 5728 | bool leftMat = builder.isMatrix(left); |
| 5729 | bool rightMat = builder.isMatrix(right); |
| 5730 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 5731 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 5732 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 5733 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 5734 | std::vector<spv::Id> results; |
| 5735 | spv::Id smearVec = spv::NoResult; |
| 5736 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5737 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5738 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5739 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5740 | |
| 5741 | // do each vector op |
| 5742 | for (unsigned int c = 0; c < numCols; ++c) { |
| 5743 | std::vector<unsigned int> indexes; |
| 5744 | indexes.push_back(c); |
| 5745 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 5746 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5747 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5748 | decorations.addNoContraction(builder, result); |
| 5749 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5750 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5751 | } |
| 5752 | |
| 5753 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5754 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5755 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5756 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5757 | } |
| 5758 | default: |
| 5759 | assert(0); |
| 5760 | return spv::NoResult; |
| 5761 | } |
| 5762 | } |
| 5763 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5764 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5765 | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5766 | { |
| 5767 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5768 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5769 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5770 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5771 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5772 | |
| 5773 | switch (op) { |
| 5774 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5775 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5776 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5777 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5778 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5779 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5780 | unaryOp = spv::OpSNegate; |
| 5781 | break; |
| 5782 | |
| 5783 | case glslang::EOpLogicalNot: |
| 5784 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5785 | unaryOp = spv::OpLogicalNot; |
| 5786 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5787 | case glslang::EOpBitwiseNot: |
| 5788 | unaryOp = spv::OpNot; |
| 5789 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5790 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5791 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5792 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5793 | break; |
| 5794 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5795 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5796 | break; |
| 5797 | case glslang::EOpTranspose: |
| 5798 | unaryOp = spv::OpTranspose; |
| 5799 | break; |
| 5800 | |
| 5801 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5802 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5803 | break; |
| 5804 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5805 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5806 | break; |
| 5807 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5808 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5809 | break; |
| 5810 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5811 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5812 | break; |
| 5813 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5814 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5815 | break; |
| 5816 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5817 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5818 | break; |
| 5819 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5820 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5821 | break; |
| 5822 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5823 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5824 | break; |
| 5825 | |
| 5826 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5827 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5828 | break; |
| 5829 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5830 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5831 | break; |
| 5832 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5833 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5834 | break; |
| 5835 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5836 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5837 | break; |
| 5838 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5839 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5840 | break; |
| 5841 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5842 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5843 | break; |
| 5844 | |
| 5845 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5846 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5847 | break; |
| 5848 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5849 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5850 | break; |
| 5851 | |
| 5852 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5853 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5854 | break; |
| 5855 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5856 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5857 | break; |
| 5858 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5859 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5860 | break; |
| 5861 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5862 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5863 | break; |
| 5864 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5865 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5866 | break; |
| 5867 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5868 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5869 | break; |
| 5870 | |
| 5871 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5872 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5873 | break; |
| 5874 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5875 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5876 | break; |
| 5877 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5878 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5879 | break; |
| 5880 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5881 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5882 | break; |
| 5883 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5884 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5885 | break; |
| 5886 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5887 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5888 | break; |
| 5889 | |
| 5890 | case glslang::EOpIsNan: |
| 5891 | unaryOp = spv::OpIsNan; |
| 5892 | break; |
| 5893 | case glslang::EOpIsInf: |
| 5894 | unaryOp = spv::OpIsInf; |
| 5895 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5896 | case glslang::EOpIsFinite: |
| 5897 | unaryOp = spv::OpIsFinite; |
| 5898 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5899 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5900 | case glslang::EOpFloatBitsToInt: |
| 5901 | case glslang::EOpFloatBitsToUint: |
| 5902 | case glslang::EOpIntBitsToFloat: |
| 5903 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5904 | case glslang::EOpDoubleBitsToInt64: |
| 5905 | case glslang::EOpDoubleBitsToUint64: |
| 5906 | case glslang::EOpInt64BitsToDouble: |
| 5907 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5908 | case glslang::EOpFloat16BitsToInt16: |
| 5909 | case glslang::EOpFloat16BitsToUint16: |
| 5910 | case glslang::EOpInt16BitsToFloat16: |
| 5911 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 5912 | unaryOp = spv::OpBitcast; |
| 5913 | break; |
| 5914 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5915 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5916 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5917 | break; |
| 5918 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5919 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5920 | break; |
| 5921 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5922 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5923 | break; |
| 5924 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5925 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5926 | break; |
| 5927 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5928 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5929 | break; |
| 5930 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5931 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5932 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5933 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5934 | case glslang::EOpPackSnorm4x8: |
| 5935 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 5936 | break; |
| 5937 | case glslang::EOpUnpackSnorm4x8: |
| 5938 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5939 | break; |
| 5940 | case glslang::EOpPackUnorm4x8: |
| 5941 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 5942 | break; |
| 5943 | case glslang::EOpUnpackUnorm4x8: |
| 5944 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5945 | break; |
| 5946 | case glslang::EOpPackDouble2x32: |
| 5947 | libCall = spv::GLSLstd450PackDouble2x32; |
| 5948 | break; |
| 5949 | case glslang::EOpUnpackDouble2x32: |
| 5950 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5951 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5952 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5953 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5954 | case glslang::EOpPackInt2x32: |
| 5955 | case glslang::EOpUnpackInt2x32: |
| 5956 | case glslang::EOpPackUint2x32: |
| 5957 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5958 | case glslang::EOpPack16: |
| 5959 | case glslang::EOpPack32: |
| 5960 | case glslang::EOpPack64: |
| 5961 | case glslang::EOpUnpack32: |
| 5962 | case glslang::EOpUnpack16: |
| 5963 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5964 | case glslang::EOpPackInt2x16: |
| 5965 | case glslang::EOpUnpackInt2x16: |
| 5966 | case glslang::EOpPackUint2x16: |
| 5967 | case glslang::EOpUnpackUint2x16: |
| 5968 | case glslang::EOpPackInt4x16: |
| 5969 | case glslang::EOpUnpackInt4x16: |
| 5970 | case glslang::EOpPackUint4x16: |
| 5971 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5972 | case glslang::EOpPackFloat2x16: |
| 5973 | case glslang::EOpUnpackFloat2x16: |
| 5974 | unaryOp = spv::OpBitcast; |
| 5975 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5976 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5977 | case glslang::EOpDPdx: |
| 5978 | unaryOp = spv::OpDPdx; |
| 5979 | break; |
| 5980 | case glslang::EOpDPdy: |
| 5981 | unaryOp = spv::OpDPdy; |
| 5982 | break; |
| 5983 | case glslang::EOpFwidth: |
| 5984 | unaryOp = spv::OpFwidth; |
| 5985 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5986 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5987 | case glslang::EOpAny: |
| 5988 | unaryOp = spv::OpAny; |
| 5989 | break; |
| 5990 | case glslang::EOpAll: |
| 5991 | unaryOp = spv::OpAll; |
| 5992 | break; |
| 5993 | |
| 5994 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5995 | if (isFloat) |
| 5996 | libCall = spv::GLSLstd450FAbs; |
| 5997 | else |
| 5998 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5999 | break; |
| 6000 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6001 | if (isFloat) |
| 6002 | libCall = spv::GLSLstd450FSign; |
| 6003 | else |
| 6004 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6005 | break; |
| 6006 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6007 | #ifndef GLSLANG_WEB |
| 6008 | case glslang::EOpDPdxFine: |
| 6009 | unaryOp = spv::OpDPdxFine; |
| 6010 | break; |
| 6011 | case glslang::EOpDPdyFine: |
| 6012 | unaryOp = spv::OpDPdyFine; |
| 6013 | break; |
| 6014 | case glslang::EOpFwidthFine: |
| 6015 | unaryOp = spv::OpFwidthFine; |
| 6016 | break; |
| 6017 | case glslang::EOpDPdxCoarse: |
| 6018 | unaryOp = spv::OpDPdxCoarse; |
| 6019 | break; |
| 6020 | case glslang::EOpDPdyCoarse: |
| 6021 | unaryOp = spv::OpDPdyCoarse; |
| 6022 | break; |
| 6023 | case glslang::EOpFwidthCoarse: |
| 6024 | unaryOp = spv::OpFwidthCoarse; |
| 6025 | break; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 6026 | case glslang::EOpRayQueryProceed: |
| 6027 | unaryOp = spv::OpRayQueryProceedKHR; |
| 6028 | break; |
| 6029 | case glslang::EOpRayQueryGetRayTMin: |
| 6030 | unaryOp = spv::OpRayQueryGetRayTMinKHR; |
| 6031 | break; |
| 6032 | case glslang::EOpRayQueryGetRayFlags: |
| 6033 | unaryOp = spv::OpRayQueryGetRayFlagsKHR; |
| 6034 | break; |
| 6035 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 6036 | unaryOp = spv::OpRayQueryGetWorldRayOriginKHR; |
| 6037 | break; |
| 6038 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 6039 | unaryOp = spv::OpRayQueryGetWorldRayDirectionKHR; |
| 6040 | break; |
| 6041 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 6042 | unaryOp = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
| 6043 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6044 | case glslang::EOpInterpolateAtCentroid: |
| 6045 | if (typeProxy == glslang::EbtFloat16) |
| 6046 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6047 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 6048 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6049 | case glslang::EOpAtomicCounterIncrement: |
| 6050 | case glslang::EOpAtomicCounterDecrement: |
| 6051 | case glslang::EOpAtomicCounter: |
| 6052 | { |
| 6053 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 6054 | std::vector<spv::Id> operands; |
| 6055 | operands.push_back(operand); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6056 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6057 | } |
| 6058 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6059 | case glslang::EOpBitFieldReverse: |
| 6060 | unaryOp = spv::OpBitReverse; |
| 6061 | break; |
| 6062 | case glslang::EOpBitCount: |
| 6063 | unaryOp = spv::OpBitCount; |
| 6064 | break; |
| 6065 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6066 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6067 | break; |
| 6068 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6069 | if (isUnsigned) |
| 6070 | libCall = spv::GLSLstd450FindUMsb; |
| 6071 | else |
| 6072 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6073 | break; |
| 6074 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 6075 | case glslang::EOpCountLeadingZeros: |
| 6076 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 6077 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 6078 | unaryOp = spv::OpUCountLeadingZerosINTEL; |
| 6079 | break; |
| 6080 | |
| 6081 | case glslang::EOpCountTrailingZeros: |
| 6082 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 6083 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 6084 | unaryOp = spv::OpUCountTrailingZerosINTEL; |
| 6085 | break; |
| 6086 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6087 | case glslang::EOpBallot: |
| 6088 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6089 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6090 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6091 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6092 | case glslang::EOpMinInvocations: |
| 6093 | case glslang::EOpMaxInvocations: |
| 6094 | case glslang::EOpAddInvocations: |
| 6095 | case glslang::EOpMinInvocationsNonUniform: |
| 6096 | case glslang::EOpMaxInvocationsNonUniform: |
| 6097 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6098 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6099 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6100 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6101 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6102 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6103 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6104 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6105 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6106 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6107 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6108 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6109 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6110 | { |
| 6111 | std::vector<spv::Id> operands; |
| 6112 | operands.push_back(operand); |
| 6113 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 6114 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6115 | case glslang::EOpSubgroupAll: |
| 6116 | case glslang::EOpSubgroupAny: |
| 6117 | case glslang::EOpSubgroupAllEqual: |
| 6118 | case glslang::EOpSubgroupBroadcastFirst: |
| 6119 | case glslang::EOpSubgroupBallot: |
| 6120 | case glslang::EOpSubgroupInverseBallot: |
| 6121 | case glslang::EOpSubgroupBallotBitCount: |
| 6122 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6123 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6124 | case glslang::EOpSubgroupBallotFindLSB: |
| 6125 | case glslang::EOpSubgroupBallotFindMSB: |
| 6126 | case glslang::EOpSubgroupAdd: |
| 6127 | case glslang::EOpSubgroupMul: |
| 6128 | case glslang::EOpSubgroupMin: |
| 6129 | case glslang::EOpSubgroupMax: |
| 6130 | case glslang::EOpSubgroupAnd: |
| 6131 | case glslang::EOpSubgroupOr: |
| 6132 | case glslang::EOpSubgroupXor: |
| 6133 | case glslang::EOpSubgroupInclusiveAdd: |
| 6134 | case glslang::EOpSubgroupInclusiveMul: |
| 6135 | case glslang::EOpSubgroupInclusiveMin: |
| 6136 | case glslang::EOpSubgroupInclusiveMax: |
| 6137 | case glslang::EOpSubgroupInclusiveAnd: |
| 6138 | case glslang::EOpSubgroupInclusiveOr: |
| 6139 | case glslang::EOpSubgroupInclusiveXor: |
| 6140 | case glslang::EOpSubgroupExclusiveAdd: |
| 6141 | case glslang::EOpSubgroupExclusiveMul: |
| 6142 | case glslang::EOpSubgroupExclusiveMin: |
| 6143 | case glslang::EOpSubgroupExclusiveMax: |
| 6144 | case glslang::EOpSubgroupExclusiveAnd: |
| 6145 | case glslang::EOpSubgroupExclusiveOr: |
| 6146 | case glslang::EOpSubgroupExclusiveXor: |
| 6147 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6148 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6149 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 6150 | std::vector<spv::Id> operands; |
| 6151 | operands.push_back(operand); |
| 6152 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 6153 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6154 | case glslang::EOpMbcnt: |
| 6155 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6156 | libCall = spv::MbcntAMD; |
| 6157 | break; |
| 6158 | |
| 6159 | case glslang::EOpCubeFaceIndex: |
| 6160 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 6161 | libCall = spv::CubeFaceIndexAMD; |
| 6162 | break; |
| 6163 | |
| 6164 | case glslang::EOpCubeFaceCoord: |
| 6165 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 6166 | libCall = spv::CubeFaceCoordAMD; |
| 6167 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6168 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6169 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 6170 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6171 | case glslang::EOpConstructReference: |
| 6172 | unaryOp = spv::OpBitcast; |
| 6173 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6174 | #endif |
Jeff Bolz | 88220d5 | 2019-05-08 10:24:46 -0500 | [diff] [blame] | 6175 | |
| 6176 | case glslang::EOpCopyObject: |
| 6177 | unaryOp = spv::OpCopyObject; |
| 6178 | break; |
| 6179 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6180 | default: |
| 6181 | return 0; |
| 6182 | } |
| 6183 | |
| 6184 | spv::Id id; |
| 6185 | if (libCall >= 0) { |
| 6186 | std::vector<spv::Id> args; |
| 6187 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6188 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6189 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6190 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6191 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6192 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6193 | decorations.addNoContraction(builder, id); |
| 6194 | decorations.addNonUniform(builder, id); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6195 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6196 | } |
| 6197 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6198 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6199 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 6200 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6201 | { |
| 6202 | // Handle unary operations vector by vector. |
| 6203 | // The result type is the same type as the original type. |
| 6204 | // The algorithm is to: |
| 6205 | // - break the matrix into vectors |
| 6206 | // - apply the operation to each vector |
| 6207 | // - make a matrix out the vector results |
| 6208 | |
| 6209 | // get the types sorted out |
| 6210 | int numCols = builder.getNumColumns(operand); |
| 6211 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 6212 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 6213 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6214 | std::vector<spv::Id> results; |
| 6215 | |
| 6216 | // do each vector op |
| 6217 | for (int c = 0; c < numCols; ++c) { |
| 6218 | std::vector<unsigned int> indexes; |
| 6219 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 6220 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 6221 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6222 | decorations.addNoContraction(builder, destVec); |
| 6223 | decorations.addNonUniform(builder, destVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6224 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6225 | } |
| 6226 | |
| 6227 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6228 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6229 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6230 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6231 | } |
| 6232 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6233 | // For converting integers where both the bitwidth and the signedness could |
| 6234 | // change, but only do the width change here. The caller is still responsible |
| 6235 | // for the signedness conversion. |
| 6236 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6237 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6238 | // Get the result type width, based on the type to convert to. |
| 6239 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6240 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6241 | case glslang::EOpConvInt16ToUint8: |
| 6242 | case glslang::EOpConvIntToUint8: |
| 6243 | case glslang::EOpConvInt64ToUint8: |
| 6244 | case glslang::EOpConvUint16ToInt8: |
| 6245 | case glslang::EOpConvUintToInt8: |
| 6246 | case glslang::EOpConvUint64ToInt8: |
| 6247 | width = 8; |
| 6248 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6249 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6250 | case glslang::EOpConvIntToUint16: |
| 6251 | case glslang::EOpConvInt64ToUint16: |
| 6252 | case glslang::EOpConvUint8ToInt16: |
| 6253 | case glslang::EOpConvUintToInt16: |
| 6254 | case glslang::EOpConvUint64ToInt16: |
| 6255 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6256 | break; |
| 6257 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6258 | case glslang::EOpConvInt16ToUint: |
| 6259 | case glslang::EOpConvInt64ToUint: |
| 6260 | case glslang::EOpConvUint8ToInt: |
| 6261 | case glslang::EOpConvUint16ToInt: |
| 6262 | case glslang::EOpConvUint64ToInt: |
| 6263 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6264 | break; |
| 6265 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6266 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6267 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6268 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6269 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6270 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6271 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6272 | break; |
| 6273 | |
| 6274 | default: |
| 6275 | assert(false && "Default missing"); |
| 6276 | break; |
| 6277 | } |
| 6278 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6279 | // Get the conversion operation and result type, |
| 6280 | // based on the target width, but the source type. |
| 6281 | spv::Id type = spv::NoType; |
| 6282 | spv::Op convOp = spv::OpNop; |
| 6283 | switch(op) { |
| 6284 | case glslang::EOpConvInt8ToUint16: |
| 6285 | case glslang::EOpConvInt8ToUint: |
| 6286 | case glslang::EOpConvInt8ToUint64: |
| 6287 | case glslang::EOpConvInt16ToUint8: |
| 6288 | case glslang::EOpConvInt16ToUint: |
| 6289 | case glslang::EOpConvInt16ToUint64: |
| 6290 | case glslang::EOpConvIntToUint8: |
| 6291 | case glslang::EOpConvIntToUint16: |
| 6292 | case glslang::EOpConvIntToUint64: |
| 6293 | case glslang::EOpConvInt64ToUint8: |
| 6294 | case glslang::EOpConvInt64ToUint16: |
| 6295 | case glslang::EOpConvInt64ToUint: |
| 6296 | convOp = spv::OpSConvert; |
| 6297 | type = builder.makeIntType(width); |
| 6298 | break; |
| 6299 | default: |
| 6300 | convOp = spv::OpUConvert; |
| 6301 | type = builder.makeUintType(width); |
| 6302 | break; |
| 6303 | } |
| 6304 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6305 | if (vectorSize > 0) |
| 6306 | type = builder.makeVectorType(type, vectorSize); |
| 6307 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6308 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6309 | } |
| 6310 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6311 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 6312 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6313 | { |
| 6314 | spv::Op convOp = spv::OpNop; |
| 6315 | spv::Id zero = 0; |
| 6316 | spv::Id one = 0; |
| 6317 | |
| 6318 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 6319 | |
| 6320 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6321 | case glslang::EOpConvIntToBool: |
| 6322 | case glslang::EOpConvUintToBool: |
| 6323 | zero = builder.makeUintConstant(0); |
| 6324 | zero = makeSmearedConstant(zero, vectorSize); |
| 6325 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6326 | case glslang::EOpConvFloatToBool: |
| 6327 | zero = builder.makeFloatConstant(0.0F); |
| 6328 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6329 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6330 | case glslang::EOpConvBoolToFloat: |
| 6331 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6332 | zero = builder.makeFloatConstant(0.0F); |
| 6333 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6334 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6335 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6336 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6337 | case glslang::EOpConvBoolToInt64: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6338 | #ifndef GLSLANG_WEB |
| 6339 | if (op == glslang::EOpConvBoolToInt64) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6340 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6341 | one = builder.makeInt64Constant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6342 | } else |
| 6343 | #endif |
| 6344 | { |
| 6345 | zero = builder.makeIntConstant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6346 | one = builder.makeIntConstant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6347 | } |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6348 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6349 | convOp = spv::OpSelect; |
| 6350 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6351 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6352 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6353 | case glslang::EOpConvBoolToUint64: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6354 | #ifndef GLSLANG_WEB |
| 6355 | if (op == glslang::EOpConvBoolToUint64) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6356 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6357 | one = builder.makeUint64Constant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6358 | } else |
| 6359 | #endif |
| 6360 | { |
| 6361 | zero = builder.makeUintConstant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6362 | one = builder.makeUintConstant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6363 | } |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6364 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6365 | convOp = spv::OpSelect; |
| 6366 | break; |
| 6367 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6368 | case glslang::EOpConvInt8ToFloat16: |
| 6369 | case glslang::EOpConvInt8ToFloat: |
| 6370 | case glslang::EOpConvInt8ToDouble: |
| 6371 | case glslang::EOpConvInt16ToFloat16: |
| 6372 | case glslang::EOpConvInt16ToFloat: |
| 6373 | case glslang::EOpConvInt16ToDouble: |
| 6374 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6375 | case glslang::EOpConvIntToFloat: |
| 6376 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6377 | case glslang::EOpConvInt64ToFloat: |
| 6378 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6379 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6380 | convOp = spv::OpConvertSToF; |
| 6381 | break; |
| 6382 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6383 | case glslang::EOpConvUint8ToFloat16: |
| 6384 | case glslang::EOpConvUint8ToFloat: |
| 6385 | case glslang::EOpConvUint8ToDouble: |
| 6386 | case glslang::EOpConvUint16ToFloat16: |
| 6387 | case glslang::EOpConvUint16ToFloat: |
| 6388 | case glslang::EOpConvUint16ToDouble: |
| 6389 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6390 | case glslang::EOpConvUintToFloat: |
| 6391 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6392 | case glslang::EOpConvUint64ToFloat: |
| 6393 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6394 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6395 | convOp = spv::OpConvertUToF; |
| 6396 | break; |
| 6397 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6398 | case glslang::EOpConvFloat16ToInt8: |
| 6399 | case glslang::EOpConvFloatToInt8: |
| 6400 | case glslang::EOpConvDoubleToInt8: |
| 6401 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6402 | case glslang::EOpConvFloatToInt16: |
| 6403 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6404 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6405 | case glslang::EOpConvFloatToInt: |
| 6406 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6407 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6408 | case glslang::EOpConvFloatToInt64: |
| 6409 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6410 | convOp = spv::OpConvertFToS; |
| 6411 | break; |
| 6412 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6413 | case glslang::EOpConvUint8ToInt8: |
| 6414 | case glslang::EOpConvInt8ToUint8: |
| 6415 | case glslang::EOpConvUint16ToInt16: |
| 6416 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6417 | case glslang::EOpConvUintToInt: |
| 6418 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6419 | case glslang::EOpConvUint64ToInt64: |
| 6420 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6421 | if (builder.isInSpecConstCodeGenMode()) { |
| 6422 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6423 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6424 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 6425 | zero = builder.makeUint8Constant(0); |
| 6426 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6427 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6428 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 6429 | zero = builder.makeUint64Constant(0); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6430 | } else |
| 6431 | #endif |
| 6432 | { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6433 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6434 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 6435 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6436 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6437 | // generating for OpSpecConstantOp instruction. |
| 6438 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6439 | } |
| 6440 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6441 | convOp = spv::OpBitcast; |
| 6442 | break; |
| 6443 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6444 | case glslang::EOpConvFloat16ToUint8: |
| 6445 | case glslang::EOpConvFloatToUint8: |
| 6446 | case glslang::EOpConvDoubleToUint8: |
| 6447 | case glslang::EOpConvFloat16ToUint16: |
| 6448 | case glslang::EOpConvFloatToUint16: |
| 6449 | case glslang::EOpConvDoubleToUint16: |
| 6450 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6451 | case glslang::EOpConvFloatToUint: |
| 6452 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6453 | case glslang::EOpConvFloatToUint64: |
| 6454 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6455 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6456 | convOp = spv::OpConvertFToU; |
| 6457 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6458 | |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6459 | #ifndef GLSLANG_WEB |
| 6460 | case glslang::EOpConvInt8ToBool: |
| 6461 | case glslang::EOpConvUint8ToBool: |
| 6462 | zero = builder.makeUint8Constant(0); |
| 6463 | zero = makeSmearedConstant(zero, vectorSize); |
| 6464 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6465 | case glslang::EOpConvInt16ToBool: |
| 6466 | case glslang::EOpConvUint16ToBool: |
| 6467 | zero = builder.makeUint16Constant(0); |
| 6468 | zero = makeSmearedConstant(zero, vectorSize); |
| 6469 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6470 | case glslang::EOpConvInt64ToBool: |
| 6471 | case glslang::EOpConvUint64ToBool: |
| 6472 | zero = builder.makeUint64Constant(0); |
| 6473 | zero = makeSmearedConstant(zero, vectorSize); |
| 6474 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6475 | case glslang::EOpConvDoubleToBool: |
| 6476 | zero = builder.makeDoubleConstant(0.0); |
| 6477 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6478 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6479 | case glslang::EOpConvFloat16ToBool: |
| 6480 | zero = builder.makeFloat16Constant(0.0F); |
| 6481 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6482 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6483 | case glslang::EOpConvBoolToDouble: |
| 6484 | convOp = spv::OpSelect; |
| 6485 | zero = builder.makeDoubleConstant(0.0); |
| 6486 | one = builder.makeDoubleConstant(1.0); |
| 6487 | break; |
| 6488 | case glslang::EOpConvBoolToFloat16: |
| 6489 | convOp = spv::OpSelect; |
| 6490 | zero = builder.makeFloat16Constant(0.0F); |
| 6491 | one = builder.makeFloat16Constant(1.0F); |
| 6492 | break; |
| 6493 | case glslang::EOpConvBoolToInt8: |
| 6494 | zero = builder.makeInt8Constant(0); |
| 6495 | one = builder.makeInt8Constant(1); |
| 6496 | convOp = spv::OpSelect; |
| 6497 | break; |
| 6498 | case glslang::EOpConvBoolToUint8: |
| 6499 | zero = builder.makeUint8Constant(0); |
| 6500 | one = builder.makeUint8Constant(1); |
| 6501 | convOp = spv::OpSelect; |
| 6502 | break; |
| 6503 | case glslang::EOpConvBoolToInt16: |
| 6504 | zero = builder.makeInt16Constant(0); |
| 6505 | one = builder.makeInt16Constant(1); |
| 6506 | convOp = spv::OpSelect; |
| 6507 | break; |
| 6508 | case glslang::EOpConvBoolToUint16: |
| 6509 | zero = builder.makeUint16Constant(0); |
| 6510 | one = builder.makeUint16Constant(1); |
| 6511 | convOp = spv::OpSelect; |
| 6512 | break; |
| 6513 | case glslang::EOpConvDoubleToFloat: |
| 6514 | case glslang::EOpConvFloatToDouble: |
| 6515 | case glslang::EOpConvDoubleToFloat16: |
| 6516 | case glslang::EOpConvFloat16ToDouble: |
| 6517 | case glslang::EOpConvFloatToFloat16: |
| 6518 | case glslang::EOpConvFloat16ToFloat: |
| 6519 | convOp = spv::OpFConvert; |
| 6520 | if (builder.isMatrixType(destType)) |
| 6521 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
| 6522 | break; |
| 6523 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6524 | case glslang::EOpConvInt8ToInt16: |
| 6525 | case glslang::EOpConvInt8ToInt: |
| 6526 | case glslang::EOpConvInt8ToInt64: |
| 6527 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6528 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6529 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6530 | case glslang::EOpConvIntToInt8: |
| 6531 | case glslang::EOpConvIntToInt16: |
| 6532 | case glslang::EOpConvIntToInt64: |
| 6533 | case glslang::EOpConvInt64ToInt8: |
| 6534 | case glslang::EOpConvInt64ToInt16: |
| 6535 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6536 | convOp = spv::OpSConvert; |
| 6537 | break; |
| 6538 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6539 | case glslang::EOpConvUint8ToUint16: |
| 6540 | case glslang::EOpConvUint8ToUint: |
| 6541 | case glslang::EOpConvUint8ToUint64: |
| 6542 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6543 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6544 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6545 | case glslang::EOpConvUintToUint8: |
| 6546 | case glslang::EOpConvUintToUint16: |
| 6547 | case glslang::EOpConvUintToUint64: |
| 6548 | case glslang::EOpConvUint64ToUint8: |
| 6549 | case glslang::EOpConvUint64ToUint16: |
| 6550 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6551 | convOp = spv::OpUConvert; |
| 6552 | break; |
| 6553 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6554 | case glslang::EOpConvInt8ToUint16: |
| 6555 | case glslang::EOpConvInt8ToUint: |
| 6556 | case glslang::EOpConvInt8ToUint64: |
| 6557 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6558 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6559 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6560 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6561 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6562 | case glslang::EOpConvIntToUint64: |
| 6563 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6564 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6565 | case glslang::EOpConvInt64ToUint: |
| 6566 | case glslang::EOpConvUint8ToInt16: |
| 6567 | case glslang::EOpConvUint8ToInt: |
| 6568 | case glslang::EOpConvUint8ToInt64: |
| 6569 | case glslang::EOpConvUint16ToInt8: |
| 6570 | case glslang::EOpConvUint16ToInt: |
| 6571 | case glslang::EOpConvUint16ToInt64: |
| 6572 | case glslang::EOpConvUintToInt8: |
| 6573 | case glslang::EOpConvUintToInt16: |
| 6574 | case glslang::EOpConvUintToInt64: |
| 6575 | case glslang::EOpConvUint64ToInt8: |
| 6576 | case glslang::EOpConvUint64ToInt16: |
| 6577 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6578 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6579 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6580 | |
| 6581 | if (builder.isInSpecConstCodeGenMode()) { |
| 6582 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6583 | switch(op) { |
| 6584 | case glslang::EOpConvInt16ToUint8: |
| 6585 | case glslang::EOpConvIntToUint8: |
| 6586 | case glslang::EOpConvInt64ToUint8: |
| 6587 | case glslang::EOpConvUint16ToInt8: |
| 6588 | case glslang::EOpConvUintToInt8: |
| 6589 | case glslang::EOpConvUint64ToInt8: |
| 6590 | zero = builder.makeUint8Constant(0); |
| 6591 | break; |
| 6592 | case glslang::EOpConvInt8ToUint16: |
| 6593 | case glslang::EOpConvIntToUint16: |
| 6594 | case glslang::EOpConvInt64ToUint16: |
| 6595 | case glslang::EOpConvUint8ToInt16: |
| 6596 | case glslang::EOpConvUintToInt16: |
| 6597 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6598 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6599 | break; |
| 6600 | case glslang::EOpConvInt8ToUint: |
| 6601 | case glslang::EOpConvInt16ToUint: |
| 6602 | case glslang::EOpConvInt64ToUint: |
| 6603 | case glslang::EOpConvUint8ToInt: |
| 6604 | case glslang::EOpConvUint16ToInt: |
| 6605 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6606 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6607 | break; |
| 6608 | case glslang::EOpConvInt8ToUint64: |
| 6609 | case glslang::EOpConvInt16ToUint64: |
| 6610 | case glslang::EOpConvIntToUint64: |
| 6611 | case glslang::EOpConvUint8ToInt64: |
| 6612 | case glslang::EOpConvUint16ToInt64: |
| 6613 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6614 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6615 | break; |
| 6616 | default: |
| 6617 | assert(false && "Default missing"); |
| 6618 | break; |
| 6619 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6620 | zero = makeSmearedConstant(zero, vectorSize); |
| 6621 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6622 | // generating for OpSpecConstantOp instruction. |
| 6623 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6624 | } |
| 6625 | // For normal run-time conversion instruction, use OpBitcast. |
| 6626 | convOp = spv::OpBitcast; |
| 6627 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6628 | case glslang::EOpConvUint64ToPtr: |
| 6629 | convOp = spv::OpConvertUToPtr; |
| 6630 | break; |
| 6631 | case glslang::EOpConvPtrToUint64: |
| 6632 | convOp = spv::OpConvertPtrToU; |
| 6633 | break; |
John Kessenich | 90e402f | 2019-09-17 23:19:38 -0600 | [diff] [blame] | 6634 | case glslang::EOpConvPtrToUvec2: |
| 6635 | case glslang::EOpConvUvec2ToPtr: |
John Kessenich | ee8e9c1 | 2019-10-10 20:54:21 -0600 | [diff] [blame] | 6636 | if (builder.isVector(operand)) |
| 6637 | builder.promoteIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, |
| 6638 | spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5); |
John Kessenich | 90e402f | 2019-09-17 23:19:38 -0600 | [diff] [blame] | 6639 | convOp = spv::OpBitcast; |
| 6640 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6641 | #endif |
| 6642 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6643 | default: |
| 6644 | break; |
| 6645 | } |
| 6646 | |
| 6647 | spv::Id result = 0; |
| 6648 | if (convOp == spv::OpNop) |
| 6649 | return result; |
| 6650 | |
| 6651 | if (convOp == spv::OpSelect) { |
| 6652 | zero = makeSmearedConstant(zero, vectorSize); |
| 6653 | one = makeSmearedConstant(one, vectorSize); |
| 6654 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 6655 | } else |
| 6656 | result = builder.createUnaryOp(convOp, destType, operand); |
| 6657 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6658 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6659 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6660 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 6664 | { |
| 6665 | if (vectorSize == 0) |
| 6666 | return constant; |
| 6667 | |
| 6668 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 6669 | std::vector<spv::Id> components; |
| 6670 | for (int c = 0; c < vectorSize; ++c) |
| 6671 | components.push_back(constant); |
| 6672 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 6673 | } |
| 6674 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6675 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6676 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, |
| 6677 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 6678 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6679 | { |
| 6680 | spv::Op opCode = spv::OpNop; |
| 6681 | |
| 6682 | switch (op) { |
| 6683 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6684 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6685 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6686 | opCode = spv::OpAtomicIAdd; |
| 6687 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6688 | case glslang::EOpAtomicCounterSubtract: |
| 6689 | opCode = spv::OpAtomicISub; |
| 6690 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6691 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6692 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6693 | case glslang::EOpAtomicCounterMin: |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6694 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? |
| 6695 | spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6696 | break; |
| 6697 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6698 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6699 | case glslang::EOpAtomicCounterMax: |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6700 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? |
| 6701 | spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6702 | break; |
| 6703 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6704 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6705 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6706 | opCode = spv::OpAtomicAnd; |
| 6707 | break; |
| 6708 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6709 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6710 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6711 | opCode = spv::OpAtomicOr; |
| 6712 | break; |
| 6713 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6714 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6715 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6716 | opCode = spv::OpAtomicXor; |
| 6717 | break; |
| 6718 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6719 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6720 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6721 | opCode = spv::OpAtomicExchange; |
| 6722 | break; |
| 6723 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6724 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6725 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6726 | opCode = spv::OpAtomicCompareExchange; |
| 6727 | break; |
| 6728 | case glslang::EOpAtomicCounterIncrement: |
| 6729 | opCode = spv::OpAtomicIIncrement; |
| 6730 | break; |
| 6731 | case glslang::EOpAtomicCounterDecrement: |
| 6732 | opCode = spv::OpAtomicIDecrement; |
| 6733 | break; |
| 6734 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6735 | case glslang::EOpImageAtomicLoad: |
| 6736 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6737 | opCode = spv::OpAtomicLoad; |
| 6738 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6739 | case glslang::EOpAtomicStore: |
| 6740 | case glslang::EOpImageAtomicStore: |
| 6741 | opCode = spv::OpAtomicStore; |
| 6742 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6743 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6744 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6745 | break; |
| 6746 | } |
| 6747 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6748 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 6749 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 6750 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6751 | // Sort out the operands |
| 6752 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6753 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 6754 | // - compare-exchange swaps the value and comparator |
| 6755 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6756 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6757 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 6758 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 6759 | spv::Id scopeId; |
| 6760 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6761 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 6762 | } else { |
| 6763 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 6764 | } |
| 6765 | // semantics default to relaxed |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6766 | spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() && |
| 6767 | glslangIntermediate->usingVulkanMemoryModel() ? |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6768 | spv::MemorySemanticsVolatileMask : |
| 6769 | spv::MemorySemanticsMaskNone); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6770 | spv::Id semanticsId2 = semanticsId; |
| 6771 | |
| 6772 | pointerId = operands[0]; |
| 6773 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 6774 | // no additional operands |
| 6775 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 6776 | compareId = operands[1]; |
| 6777 | valueId = operands[2]; |
| 6778 | if (operands.size() > 3) { |
| 6779 | scopeId = operands[3]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6780 | semanticsId = builder.makeUintConstant( |
| 6781 | builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 6782 | semanticsId2 = builder.makeUintConstant( |
| 6783 | builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6784 | } |
| 6785 | } else if (opCode == spv::OpAtomicLoad) { |
| 6786 | if (operands.size() > 1) { |
| 6787 | scopeId = operands[1]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6788 | semanticsId = builder.makeUintConstant( |
| 6789 | builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6790 | } |
| 6791 | } else { |
| 6792 | // atomic store or RMW |
| 6793 | valueId = operands[1]; |
| 6794 | if (operands.size() > 2) { |
| 6795 | scopeId = operands[2]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6796 | semanticsId = builder.makeUintConstant |
| 6797 | (builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6798 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 6799 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6800 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6801 | // Check for capabilities |
| 6802 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6803 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 6804 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 6805 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 6806 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6807 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6808 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6809 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6810 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 6811 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6812 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6813 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6814 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 6815 | spvAtomicOperands.push_back(pointerId); |
| 6816 | spvAtomicOperands.push_back(scopeId); |
| 6817 | spvAtomicOperands.push_back(semanticsId); |
| 6818 | if (opCode == spv::OpAtomicCompareExchange) { |
| 6819 | spvAtomicOperands.push_back(semanticsId2); |
| 6820 | spvAtomicOperands.push_back(valueId); |
| 6821 | spvAtomicOperands.push_back(compareId); |
| 6822 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 6823 | spvAtomicOperands.push_back(valueId); |
| 6824 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6825 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6826 | if (opCode == spv::OpAtomicStore) { |
| 6827 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 6828 | return 0; |
| 6829 | } else { |
| 6830 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 6831 | |
| 6832 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 6833 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 6834 | if (op == glslang::EOpAtomicCounterDecrement) |
| 6835 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 6836 | |
| 6837 | return resultId; |
| 6838 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6839 | } |
| 6840 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6841 | // Create group invocation operations. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6842 | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, |
| 6843 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6844 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6845 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6846 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6847 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6848 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6849 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6850 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 6851 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6852 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 6853 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6854 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 6855 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6856 | } else if (op == glslang::EOpAnyInvocation || |
| 6857 | op == glslang::EOpAllInvocations || |
| 6858 | op == glslang::EOpAllInvocationsEqual) { |
| 6859 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 6860 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6861 | } else { |
| 6862 | builder.addCapability(spv::CapabilityGroups); |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6863 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 6864 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6865 | op == glslang::EOpAddInvocationsNonUniform || |
| 6866 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6867 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6868 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 6869 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 6870 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 6871 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 6872 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6873 | |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6874 | switch (op) { |
| 6875 | case glslang::EOpMinInvocations: |
| 6876 | case glslang::EOpMaxInvocations: |
| 6877 | case glslang::EOpAddInvocations: |
| 6878 | case glslang::EOpMinInvocationsNonUniform: |
| 6879 | case glslang::EOpMaxInvocationsNonUniform: |
| 6880 | case glslang::EOpAddInvocationsNonUniform: |
| 6881 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6882 | break; |
| 6883 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6884 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6885 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6886 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6887 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6888 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6889 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6890 | break; |
| 6891 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6892 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6893 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6894 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6895 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6896 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6897 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6898 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 6899 | default: |
| 6900 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6901 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6902 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6903 | spvGroupOperands.push_back(scope); |
| 6904 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 6905 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6906 | spvGroupOperands.push_back(groupOp); |
| 6907 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6908 | } |
| 6909 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 6910 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6911 | spv::IdImmediate op = { true, *opIt }; |
| 6912 | spvGroupOperands.push_back(op); |
| 6913 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6914 | |
| 6915 | switch (op) { |
| 6916 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6917 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6918 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6919 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6920 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6921 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6922 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 6923 | opCode = spv::OpSubgroupAllEqualKHR; |
| 6924 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6925 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 6926 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 6927 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6928 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6929 | break; |
| 6930 | case glslang::EOpReadFirstInvocation: |
| 6931 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6932 | break; |
| 6933 | case glslang::EOpBallot: |
| 6934 | { |
| 6935 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6936 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6937 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6938 | // |
| 6939 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6940 | // |
| 6941 | spv::Id uintType = builder.makeUintType(32); |
| 6942 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6943 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6944 | |
| 6945 | std::vector<spv::Id> components; |
| 6946 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6947 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6948 | |
| 6949 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6950 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6951 | builder.createCompositeConstruct(uvec2Type, components)); |
| 6952 | } |
| 6953 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6954 | case glslang::EOpMinInvocations: |
| 6955 | case glslang::EOpMaxInvocations: |
| 6956 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6957 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6958 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6959 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6960 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6961 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6962 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6963 | if (op == glslang::EOpMinInvocations || |
| 6964 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 6965 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6966 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6967 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6968 | else { |
| 6969 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6970 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6971 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6972 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6973 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6974 | } else if (op == glslang::EOpMaxInvocations || |
| 6975 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 6976 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6977 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6978 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6979 | else { |
| 6980 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6981 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6982 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6983 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6984 | } |
| 6985 | } else { |
| 6986 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6987 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6988 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6989 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6990 | } |
| 6991 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 6992 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6993 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6994 | |
| 6995 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6996 | case glslang::EOpMinInvocationsNonUniform: |
| 6997 | case glslang::EOpMaxInvocationsNonUniform: |
| 6998 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6999 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 7000 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 7001 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 7002 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 7003 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 7004 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 7005 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 7006 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 7007 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7008 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7009 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7010 | else { |
| 7011 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7012 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7013 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7014 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7015 | } |
| 7016 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7017 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 7018 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 7019 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7020 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7021 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7022 | else { |
| 7023 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7024 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7025 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7026 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7027 | } |
| 7028 | } |
| 7029 | else { |
| 7030 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7031 | opCode = spv::OpGroupFAddNonUniformAMD; |
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::OpGroupIAddNonUniformAMD; |
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; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7040 | default: |
| 7041 | logger->missingFunctionality("invocation operation"); |
| 7042 | return spv::NoResult; |
| 7043 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7044 | |
| 7045 | assert(opCode != spv::OpNop); |
| 7046 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7047 | } |
| 7048 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7049 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7050 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 7051 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7052 | { |
| 7053 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 7054 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7055 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7056 | op == spv::OpSubgroupReadInvocationKHR || |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7057 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || |
| 7058 | op == spv::OpGroupSMinNonUniformAMD || |
| 7059 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || |
| 7060 | op == spv::OpGroupSMaxNonUniformAMD || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7061 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
| 7062 | |
| 7063 | // Handle group invocation operations scalar by scalar. |
| 7064 | // The result type is the same type as the original type. |
| 7065 | // The algorithm is to: |
| 7066 | // - break the vector into scalars |
| 7067 | // - apply the operation to each scalar |
| 7068 | // - make a vector out the scalar results |
| 7069 | |
| 7070 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7071 | int numComponents = builder.getNumComponents(operands[0]); |
| 7072 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7073 | std::vector<spv::Id> results; |
| 7074 | |
| 7075 | // do each scalar op |
| 7076 | for (int comp = 0; comp < numComponents; ++comp) { |
| 7077 | std::vector<unsigned int> indexes; |
| 7078 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7079 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 7080 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7081 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 7082 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7083 | spv::IdImmediate operand = { true, operands[1] }; |
| 7084 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7085 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7086 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7087 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7088 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7089 | spv::IdImmediate operand = { true, operands[1] }; |
| 7090 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7091 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7092 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7093 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7094 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7095 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7096 | spvGroupOperands.push_back(scalar); |
| 7097 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7098 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7099 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7100 | } |
| 7101 | |
| 7102 | // put the pieces together |
| 7103 | return builder.createCompositeConstruct(typeId, results); |
| 7104 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7105 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7106 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7107 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 7108 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7109 | { |
| 7110 | // Add the required capabilities. |
| 7111 | switch (op) { |
| 7112 | case glslang::EOpSubgroupElect: |
| 7113 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7114 | break; |
| 7115 | case glslang::EOpSubgroupAll: |
| 7116 | case glslang::EOpSubgroupAny: |
| 7117 | case glslang::EOpSubgroupAllEqual: |
| 7118 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7119 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 7120 | break; |
| 7121 | case glslang::EOpSubgroupBroadcast: |
| 7122 | case glslang::EOpSubgroupBroadcastFirst: |
| 7123 | case glslang::EOpSubgroupBallot: |
| 7124 | case glslang::EOpSubgroupInverseBallot: |
| 7125 | case glslang::EOpSubgroupBallotBitExtract: |
| 7126 | case glslang::EOpSubgroupBallotBitCount: |
| 7127 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7128 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7129 | case glslang::EOpSubgroupBallotFindLSB: |
| 7130 | case glslang::EOpSubgroupBallotFindMSB: |
| 7131 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7132 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 7133 | break; |
| 7134 | case glslang::EOpSubgroupShuffle: |
| 7135 | case glslang::EOpSubgroupShuffleXor: |
| 7136 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7137 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 7138 | break; |
| 7139 | case glslang::EOpSubgroupShuffleUp: |
| 7140 | case glslang::EOpSubgroupShuffleDown: |
| 7141 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7142 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 7143 | break; |
| 7144 | case glslang::EOpSubgroupAdd: |
| 7145 | case glslang::EOpSubgroupMul: |
| 7146 | case glslang::EOpSubgroupMin: |
| 7147 | case glslang::EOpSubgroupMax: |
| 7148 | case glslang::EOpSubgroupAnd: |
| 7149 | case glslang::EOpSubgroupOr: |
| 7150 | case glslang::EOpSubgroupXor: |
| 7151 | case glslang::EOpSubgroupInclusiveAdd: |
| 7152 | case glslang::EOpSubgroupInclusiveMul: |
| 7153 | case glslang::EOpSubgroupInclusiveMin: |
| 7154 | case glslang::EOpSubgroupInclusiveMax: |
| 7155 | case glslang::EOpSubgroupInclusiveAnd: |
| 7156 | case glslang::EOpSubgroupInclusiveOr: |
| 7157 | case glslang::EOpSubgroupInclusiveXor: |
| 7158 | case glslang::EOpSubgroupExclusiveAdd: |
| 7159 | case glslang::EOpSubgroupExclusiveMul: |
| 7160 | case glslang::EOpSubgroupExclusiveMin: |
| 7161 | case glslang::EOpSubgroupExclusiveMax: |
| 7162 | case glslang::EOpSubgroupExclusiveAnd: |
| 7163 | case glslang::EOpSubgroupExclusiveOr: |
| 7164 | case glslang::EOpSubgroupExclusiveXor: |
| 7165 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7166 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 7167 | break; |
| 7168 | case glslang::EOpSubgroupClusteredAdd: |
| 7169 | case glslang::EOpSubgroupClusteredMul: |
| 7170 | case glslang::EOpSubgroupClusteredMin: |
| 7171 | case glslang::EOpSubgroupClusteredMax: |
| 7172 | case glslang::EOpSubgroupClusteredAnd: |
| 7173 | case glslang::EOpSubgroupClusteredOr: |
| 7174 | case glslang::EOpSubgroupClusteredXor: |
| 7175 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7176 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 7177 | break; |
| 7178 | case glslang::EOpSubgroupQuadBroadcast: |
| 7179 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7180 | case glslang::EOpSubgroupQuadSwapVertical: |
| 7181 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 7182 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7183 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 7184 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7185 | case glslang::EOpSubgroupPartitionedAdd: |
| 7186 | case glslang::EOpSubgroupPartitionedMul: |
| 7187 | case glslang::EOpSubgroupPartitionedMin: |
| 7188 | case glslang::EOpSubgroupPartitionedMax: |
| 7189 | case glslang::EOpSubgroupPartitionedAnd: |
| 7190 | case glslang::EOpSubgroupPartitionedOr: |
| 7191 | case glslang::EOpSubgroupPartitionedXor: |
| 7192 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7193 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7194 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7195 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7196 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7197 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7198 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7199 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7200 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7201 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7202 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7203 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7204 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7205 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 7206 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 7207 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 7208 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7209 | default: assert(0 && "Unhandled subgroup operation!"); |
| 7210 | } |
| 7211 | |
Jeff Bolz | c5b669e | 2019-09-08 08:49:18 -0500 | [diff] [blame] | 7212 | |
| 7213 | const bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7214 | const bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7215 | const bool isBool = typeProxy == glslang::EbtBool; |
| 7216 | |
| 7217 | spv::Op opCode = spv::OpNop; |
| 7218 | |
| 7219 | // Figure out which opcode to use. |
| 7220 | switch (op) { |
| 7221 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 7222 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 7223 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 7224 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 7225 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 7226 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 7227 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 7228 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 7229 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 7230 | case glslang::EOpSubgroupBallotBitCount: |
| 7231 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7232 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 7233 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 7234 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 7235 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 7236 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 7237 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 7238 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 7239 | case glslang::EOpSubgroupAdd: |
| 7240 | case glslang::EOpSubgroupInclusiveAdd: |
| 7241 | case glslang::EOpSubgroupExclusiveAdd: |
| 7242 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7243 | case glslang::EOpSubgroupPartitionedAdd: |
| 7244 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7245 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7246 | if (isFloat) { |
| 7247 | opCode = spv::OpGroupNonUniformFAdd; |
| 7248 | } else { |
| 7249 | opCode = spv::OpGroupNonUniformIAdd; |
| 7250 | } |
| 7251 | break; |
| 7252 | case glslang::EOpSubgroupMul: |
| 7253 | case glslang::EOpSubgroupInclusiveMul: |
| 7254 | case glslang::EOpSubgroupExclusiveMul: |
| 7255 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7256 | case glslang::EOpSubgroupPartitionedMul: |
| 7257 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7258 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7259 | if (isFloat) { |
| 7260 | opCode = spv::OpGroupNonUniformFMul; |
| 7261 | } else { |
| 7262 | opCode = spv::OpGroupNonUniformIMul; |
| 7263 | } |
| 7264 | break; |
| 7265 | case glslang::EOpSubgroupMin: |
| 7266 | case glslang::EOpSubgroupInclusiveMin: |
| 7267 | case glslang::EOpSubgroupExclusiveMin: |
| 7268 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7269 | case glslang::EOpSubgroupPartitionedMin: |
| 7270 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7271 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7272 | if (isFloat) { |
| 7273 | opCode = spv::OpGroupNonUniformFMin; |
| 7274 | } else if (isUnsigned) { |
| 7275 | opCode = spv::OpGroupNonUniformUMin; |
| 7276 | } else { |
| 7277 | opCode = spv::OpGroupNonUniformSMin; |
| 7278 | } |
| 7279 | break; |
| 7280 | case glslang::EOpSubgroupMax: |
| 7281 | case glslang::EOpSubgroupInclusiveMax: |
| 7282 | case glslang::EOpSubgroupExclusiveMax: |
| 7283 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7284 | case glslang::EOpSubgroupPartitionedMax: |
| 7285 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7286 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7287 | if (isFloat) { |
| 7288 | opCode = spv::OpGroupNonUniformFMax; |
| 7289 | } else if (isUnsigned) { |
| 7290 | opCode = spv::OpGroupNonUniformUMax; |
| 7291 | } else { |
| 7292 | opCode = spv::OpGroupNonUniformSMax; |
| 7293 | } |
| 7294 | break; |
| 7295 | case glslang::EOpSubgroupAnd: |
| 7296 | case glslang::EOpSubgroupInclusiveAnd: |
| 7297 | case glslang::EOpSubgroupExclusiveAnd: |
| 7298 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7299 | case glslang::EOpSubgroupPartitionedAnd: |
| 7300 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7301 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7302 | if (isBool) { |
| 7303 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 7304 | } else { |
| 7305 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 7306 | } |
| 7307 | break; |
| 7308 | case glslang::EOpSubgroupOr: |
| 7309 | case glslang::EOpSubgroupInclusiveOr: |
| 7310 | case glslang::EOpSubgroupExclusiveOr: |
| 7311 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7312 | case glslang::EOpSubgroupPartitionedOr: |
| 7313 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7314 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7315 | if (isBool) { |
| 7316 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 7317 | } else { |
| 7318 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 7319 | } |
| 7320 | break; |
| 7321 | case glslang::EOpSubgroupXor: |
| 7322 | case glslang::EOpSubgroupInclusiveXor: |
| 7323 | case glslang::EOpSubgroupExclusiveXor: |
| 7324 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7325 | case glslang::EOpSubgroupPartitionedXor: |
| 7326 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7327 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7328 | if (isBool) { |
| 7329 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 7330 | } else { |
| 7331 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 7332 | } |
| 7333 | break; |
| 7334 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 7335 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7336 | case glslang::EOpSubgroupQuadSwapVertical: |
| 7337 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 7338 | default: assert(0 && "Unhandled subgroup operation!"); |
| 7339 | } |
| 7340 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7341 | // get the right Group Operation |
| 7342 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7343 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7344 | default: |
| 7345 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7346 | case glslang::EOpSubgroupBallotBitCount: |
| 7347 | case glslang::EOpSubgroupAdd: |
| 7348 | case glslang::EOpSubgroupMul: |
| 7349 | case glslang::EOpSubgroupMin: |
| 7350 | case glslang::EOpSubgroupMax: |
| 7351 | case glslang::EOpSubgroupAnd: |
| 7352 | case glslang::EOpSubgroupOr: |
| 7353 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7354 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7355 | break; |
| 7356 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7357 | case glslang::EOpSubgroupInclusiveAdd: |
| 7358 | case glslang::EOpSubgroupInclusiveMul: |
| 7359 | case glslang::EOpSubgroupInclusiveMin: |
| 7360 | case glslang::EOpSubgroupInclusiveMax: |
| 7361 | case glslang::EOpSubgroupInclusiveAnd: |
| 7362 | case glslang::EOpSubgroupInclusiveOr: |
| 7363 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7364 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7365 | break; |
| 7366 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7367 | case glslang::EOpSubgroupExclusiveAdd: |
| 7368 | case glslang::EOpSubgroupExclusiveMul: |
| 7369 | case glslang::EOpSubgroupExclusiveMin: |
| 7370 | case glslang::EOpSubgroupExclusiveMax: |
| 7371 | case glslang::EOpSubgroupExclusiveAnd: |
| 7372 | case glslang::EOpSubgroupExclusiveOr: |
| 7373 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7374 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7375 | break; |
| 7376 | case glslang::EOpSubgroupClusteredAdd: |
| 7377 | case glslang::EOpSubgroupClusteredMul: |
| 7378 | case glslang::EOpSubgroupClusteredMin: |
| 7379 | case glslang::EOpSubgroupClusteredMax: |
| 7380 | case glslang::EOpSubgroupClusteredAnd: |
| 7381 | case glslang::EOpSubgroupClusteredOr: |
| 7382 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7383 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7384 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7385 | case glslang::EOpSubgroupPartitionedAdd: |
| 7386 | case glslang::EOpSubgroupPartitionedMul: |
| 7387 | case glslang::EOpSubgroupPartitionedMin: |
| 7388 | case glslang::EOpSubgroupPartitionedMax: |
| 7389 | case glslang::EOpSubgroupPartitionedAnd: |
| 7390 | case glslang::EOpSubgroupPartitionedOr: |
| 7391 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7392 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7393 | break; |
| 7394 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7395 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7396 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7397 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7398 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7399 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7400 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7401 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7402 | break; |
| 7403 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7404 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7405 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7406 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7407 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7408 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7409 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7410 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7411 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7412 | } |
| 7413 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7414 | // build the instruction |
| 7415 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 7416 | |
| 7417 | // Every operation begins with the Execution Scope operand. |
| 7418 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7419 | spvGroupOperands.push_back(executionScope); |
| 7420 | |
| 7421 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 7422 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7423 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7424 | spvGroupOperands.push_back(groupOperand); |
| 7425 | } |
| 7426 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7427 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7428 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 7429 | spv::IdImmediate operand = { true, *opIt }; |
| 7430 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7431 | } |
| 7432 | |
| 7433 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7434 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7435 | switch (op) { |
| 7436 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7437 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 7438 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 7439 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 7440 | } |
| 7441 | if (directionId != spv::NoResult) { |
| 7442 | spv::IdImmediate direction = { true, directionId }; |
| 7443 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7444 | } |
| 7445 | |
| 7446 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 7447 | } |
| 7448 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7449 | spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, |
| 7450 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7451 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7452 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7453 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7454 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7455 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7456 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7457 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 7458 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7459 | spv::Id typeId0 = 0; |
| 7460 | if (consumedOperands > 0) |
| 7461 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7462 | spv::Id typeId1 = 0; |
| 7463 | if (consumedOperands > 1) |
| 7464 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7465 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7466 | |
| 7467 | switch (op) { |
| 7468 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7469 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7470 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7471 | else if (isUnsigned) |
| 7472 | libCall = spv::GLSLstd450UMin; |
| 7473 | else |
| 7474 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7475 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7476 | break; |
| 7477 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7478 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7479 | break; |
| 7480 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7481 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7482 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7483 | else if (isUnsigned) |
| 7484 | libCall = spv::GLSLstd450UMax; |
| 7485 | else |
| 7486 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7487 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7488 | break; |
| 7489 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7490 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7491 | break; |
| 7492 | case glslang::EOpDot: |
| 7493 | opCode = spv::OpDot; |
| 7494 | break; |
| 7495 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7496 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7497 | break; |
| 7498 | |
| 7499 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7500 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7501 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7502 | else if (isUnsigned) |
| 7503 | libCall = spv::GLSLstd450UClamp; |
| 7504 | else |
| 7505 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7506 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 7507 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7508 | break; |
| 7509 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7510 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 7511 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7512 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7513 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7514 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7515 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7516 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7517 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7518 | break; |
| 7519 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7520 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7521 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7522 | break; |
| 7523 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7524 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7525 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 7526 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7527 | break; |
| 7528 | |
| 7529 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7530 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7531 | break; |
| 7532 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7533 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7534 | break; |
| 7535 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7536 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7537 | break; |
| 7538 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7539 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7540 | break; |
| 7541 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7542 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7543 | break; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7544 | case glslang::EOpBarrier: |
| 7545 | { |
| 7546 | // This is for the extended controlBarrier function, with four operands. |
| 7547 | // The unextended barrier() goes through createNoArgOperation. |
| 7548 | assert(operands.size() == 4); |
| 7549 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 7550 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 7551 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7552 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, |
| 7553 | (spv::MemorySemanticsMask)semantics); |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7554 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7555 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7556 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7557 | spv::MemorySemanticsVolatileMask)) { |
| 7558 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7559 | } |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7560 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || |
| 7561 | memoryScope == spv::ScopeDevice)) { |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7562 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7563 | } |
| 7564 | return 0; |
| 7565 | } |
| 7566 | break; |
| 7567 | case glslang::EOpMemoryBarrier: |
| 7568 | { |
| 7569 | // This is for the extended memoryBarrier function, with three operands. |
| 7570 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 7571 | assert(operands.size() == 3); |
| 7572 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 7573 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 7574 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 7575 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7576 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7577 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7578 | spv::MemorySemanticsVolatileMask)) { |
| 7579 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7580 | } |
| 7581 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 7582 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7583 | } |
| 7584 | return 0; |
| 7585 | } |
| 7586 | break; |
| 7587 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7588 | #ifndef GLSLANG_WEB |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7589 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7590 | if (typeProxy == glslang::EbtFloat16) |
| 7591 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7592 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 7593 | break; |
| 7594 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7595 | if (typeProxy == glslang::EbtFloat16) |
| 7596 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7597 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 7598 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7599 | case glslang::EOpAddCarry: |
| 7600 | opCode = spv::OpIAddCarry; |
| 7601 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7602 | consumedOperands = 2; |
| 7603 | break; |
| 7604 | case glslang::EOpSubBorrow: |
| 7605 | opCode = spv::OpISubBorrow; |
| 7606 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7607 | consumedOperands = 2; |
| 7608 | break; |
| 7609 | case glslang::EOpUMulExtended: |
| 7610 | opCode = spv::OpUMulExtended; |
| 7611 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7612 | consumedOperands = 2; |
| 7613 | break; |
| 7614 | case glslang::EOpIMulExtended: |
| 7615 | opCode = spv::OpSMulExtended; |
| 7616 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7617 | consumedOperands = 2; |
| 7618 | break; |
| 7619 | case glslang::EOpBitfieldExtract: |
| 7620 | if (isUnsigned) |
| 7621 | opCode = spv::OpBitFieldUExtract; |
| 7622 | else |
| 7623 | opCode = spv::OpBitFieldSExtract; |
| 7624 | break; |
| 7625 | case glslang::EOpBitfieldInsert: |
| 7626 | opCode = spv::OpBitFieldInsert; |
| 7627 | break; |
| 7628 | |
| 7629 | case glslang::EOpFma: |
| 7630 | libCall = spv::GLSLstd450Fma; |
| 7631 | break; |
| 7632 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7633 | { |
| 7634 | libCall = spv::GLSLstd450FrexpStruct; |
| 7635 | assert(builder.isPointerType(typeId1)); |
| 7636 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7637 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 7638 | if (width == 16) |
| 7639 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 7640 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7641 | if (builder.getNumComponents(operands[0]) == 1) |
| 7642 | frexpIntType = builder.makeIntegerType(width, true); |
| 7643 | else |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7644 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), |
| 7645 | builder.getNumComponents(operands[0])); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7646 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 7647 | consumedOperands = 1; |
| 7648 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7649 | break; |
| 7650 | case glslang::EOpLdexp: |
| 7651 | libCall = spv::GLSLstd450Ldexp; |
| 7652 | break; |
| 7653 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7654 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7655 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7656 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7657 | case glslang::EOpSubgroupBroadcast: |
| 7658 | case glslang::EOpSubgroupBallotBitExtract: |
| 7659 | case glslang::EOpSubgroupShuffle: |
| 7660 | case glslang::EOpSubgroupShuffleXor: |
| 7661 | case glslang::EOpSubgroupShuffleUp: |
| 7662 | case glslang::EOpSubgroupShuffleDown: |
| 7663 | case glslang::EOpSubgroupClusteredAdd: |
| 7664 | case glslang::EOpSubgroupClusteredMul: |
| 7665 | case glslang::EOpSubgroupClusteredMin: |
| 7666 | case glslang::EOpSubgroupClusteredMax: |
| 7667 | case glslang::EOpSubgroupClusteredAnd: |
| 7668 | case glslang::EOpSubgroupClusteredOr: |
| 7669 | case glslang::EOpSubgroupClusteredXor: |
| 7670 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7671 | case glslang::EOpSubgroupPartitionedAdd: |
| 7672 | case glslang::EOpSubgroupPartitionedMul: |
| 7673 | case glslang::EOpSubgroupPartitionedMin: |
| 7674 | case glslang::EOpSubgroupPartitionedMax: |
| 7675 | case glslang::EOpSubgroupPartitionedAnd: |
| 7676 | case glslang::EOpSubgroupPartitionedOr: |
| 7677 | case glslang::EOpSubgroupPartitionedXor: |
| 7678 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7679 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7680 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7681 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7682 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7683 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7684 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7685 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7686 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7687 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7688 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7689 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7690 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7691 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7692 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 7693 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7694 | case glslang::EOpSwizzleInvocations: |
| 7695 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7696 | libCall = spv::SwizzleInvocationsAMD; |
| 7697 | break; |
| 7698 | case glslang::EOpSwizzleInvocationsMasked: |
| 7699 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7700 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 7701 | break; |
| 7702 | case glslang::EOpWriteInvocation: |
| 7703 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7704 | libCall = spv::WriteInvocationAMD; |
| 7705 | break; |
| 7706 | |
| 7707 | case glslang::EOpMin3: |
| 7708 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7709 | if (isFloat) |
| 7710 | libCall = spv::FMin3AMD; |
| 7711 | else { |
| 7712 | if (isUnsigned) |
| 7713 | libCall = spv::UMin3AMD; |
| 7714 | else |
| 7715 | libCall = spv::SMin3AMD; |
| 7716 | } |
| 7717 | break; |
| 7718 | case glslang::EOpMax3: |
| 7719 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7720 | if (isFloat) |
| 7721 | libCall = spv::FMax3AMD; |
| 7722 | else { |
| 7723 | if (isUnsigned) |
| 7724 | libCall = spv::UMax3AMD; |
| 7725 | else |
| 7726 | libCall = spv::SMax3AMD; |
| 7727 | } |
| 7728 | break; |
| 7729 | case glslang::EOpMid3: |
| 7730 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7731 | if (isFloat) |
| 7732 | libCall = spv::FMid3AMD; |
| 7733 | else { |
| 7734 | if (isUnsigned) |
| 7735 | libCall = spv::UMid3AMD; |
| 7736 | else |
| 7737 | libCall = spv::SMid3AMD; |
| 7738 | } |
| 7739 | break; |
| 7740 | |
| 7741 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7742 | if (typeProxy == glslang::EbtFloat16) |
| 7743 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7744 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 7745 | libCall = spv::InterpolateAtVertexAMD; |
| 7746 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7747 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7748 | case glslang::EOpReportIntersection: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7749 | typeId = builder.makeBoolType(); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7750 | opCode = spv::OpReportIntersectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7751 | break; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7752 | case glslang::EOpTrace: |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7753 | builder.createNoResultOp(spv::OpTraceRayKHR, operands); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7754 | return 0; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7755 | case glslang::EOpExecuteCallable: |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7756 | builder.createNoResultOp(spv::OpExecuteCallableKHR, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7757 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7758 | |
| 7759 | case glslang::EOpRayQueryInitialize: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7760 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR, operands); |
| 7761 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7762 | case glslang::EOpRayQueryTerminate: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7763 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operands); |
| 7764 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7765 | case glslang::EOpRayQueryGenerateIntersection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7766 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR, operands); |
| 7767 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7768 | case glslang::EOpRayQueryConfirmIntersection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7769 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operands); |
| 7770 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7771 | case glslang::EOpRayQueryProceed: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7772 | typeId = builder.makeBoolType(); |
| 7773 | opCode = spv::OpRayQueryProceedKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7774 | break; |
| 7775 | case glslang::EOpRayQueryGetIntersectionType: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 7776 | typeId = builder.makeUintType(32); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7777 | opCode = spv::OpRayQueryGetIntersectionTypeKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7778 | break; |
| 7779 | case glslang::EOpRayQueryGetRayTMin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7780 | typeId = builder.makeFloatType(32); |
| 7781 | opCode = spv::OpRayQueryGetRayTMinKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7782 | break; |
| 7783 | case glslang::EOpRayQueryGetRayFlags: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7784 | typeId = builder.makeIntType(32); |
| 7785 | opCode = spv::OpRayQueryGetRayFlagsKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7786 | break; |
| 7787 | case glslang::EOpRayQueryGetIntersectionT: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 7788 | typeId = builder.makeFloatType(32); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7789 | opCode = spv::OpRayQueryGetIntersectionTKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7790 | break; |
| 7791 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7792 | typeId = builder.makeIntType(32); |
| 7793 | opCode = spv::OpRayQueryGetIntersectionInstanceCustomIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7794 | break; |
| 7795 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7796 | typeId = builder.makeIntType(32); |
| 7797 | opCode = spv::OpRayQueryGetIntersectionInstanceIdKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7798 | break; |
| 7799 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7800 | typeId = builder.makeIntType(32); |
| 7801 | opCode = spv::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7802 | break; |
| 7803 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7804 | typeId = builder.makeIntType(32); |
| 7805 | opCode = spv::OpRayQueryGetIntersectionGeometryIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7806 | break; |
| 7807 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7808 | typeId = builder.makeIntType(32); |
| 7809 | opCode = spv::OpRayQueryGetIntersectionPrimitiveIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7810 | break; |
| 7811 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7812 | typeId = builder.makeVectorType(builder.makeFloatType(32), 2); |
| 7813 | opCode = spv::OpRayQueryGetIntersectionBarycentricsKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7814 | break; |
| 7815 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7816 | typeId = builder.makeBoolType(); |
| 7817 | opCode = spv::OpRayQueryGetIntersectionFrontFaceKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7818 | break; |
| 7819 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7820 | typeId = builder.makeBoolType(); |
| 7821 | opCode = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7822 | break; |
| 7823 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7824 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7825 | opCode = spv::OpRayQueryGetIntersectionObjectRayDirectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7826 | break; |
| 7827 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7828 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7829 | opCode = spv::OpRayQueryGetIntersectionObjectRayOriginKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7830 | break; |
| 7831 | case glslang::EOpRayQueryGetWorldRayDirection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7832 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7833 | opCode = spv::OpRayQueryGetWorldRayDirectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7834 | break; |
| 7835 | case glslang::EOpRayQueryGetWorldRayOrigin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7836 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7837 | opCode = spv::OpRayQueryGetWorldRayOriginKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7838 | break; |
| 7839 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7840 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7841 | opCode = spv::OpRayQueryGetIntersectionObjectToWorldKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7842 | break; |
| 7843 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7844 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7845 | opCode = spv::OpRayQueryGetIntersectionWorldToObjectKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7846 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7847 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 7848 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 7849 | return 0; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 7850 | case glslang::EOpCooperativeMatrixMulAdd: |
| 7851 | opCode = spv::OpCooperativeMatrixMulAddNV; |
| 7852 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7853 | #endif // GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7854 | default: |
| 7855 | return 0; |
| 7856 | } |
| 7857 | |
| 7858 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7859 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 7860 | // Use an extended instruction from the standard library. |
| 7861 | // Construct the call arguments, without modifying the original operands vector. |
| 7862 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 7863 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7864 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 7865 | } else if (opCode == spv::OpDot && !isFloat) { |
| 7866 | // int dot(int, int) |
| 7867 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 7868 | const int componentCount = builder.getNumComponents(operands[0]); |
| 7869 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 7870 | builder.setPrecision(mulOp, precision); |
| 7871 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 7872 | for (int i = 1; i < componentCount; ++i) { |
| 7873 | builder.setPrecision(id, precision); |
John Kessenich | 5de15a2 | 2019-12-26 10:56:54 -0700 | [diff] [blame] | 7874 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(mulOp, typeId, i)); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 7875 | } |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 7876 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7877 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7878 | case 0: |
| 7879 | // should all be handled by visitAggregate and createNoArgOperation |
| 7880 | assert(0); |
| 7881 | return 0; |
| 7882 | case 1: |
| 7883 | // should all be handled by createUnaryOperation |
| 7884 | assert(0); |
| 7885 | return 0; |
| 7886 | case 2: |
| 7887 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 7888 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7889 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7890 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 7891 | assert(consumedOperands == operands.size()); |
| 7892 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7893 | break; |
| 7894 | } |
| 7895 | } |
| 7896 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7897 | #ifndef GLSLANG_WEB |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7898 | // Decode the return types that were structures |
| 7899 | switch (op) { |
| 7900 | case glslang::EOpAddCarry: |
| 7901 | case glslang::EOpSubBorrow: |
| 7902 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7903 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7904 | break; |
| 7905 | case glslang::EOpUMulExtended: |
| 7906 | case glslang::EOpIMulExtended: |
| 7907 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 7908 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7909 | break; |
| 7910 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7911 | { |
| 7912 | assert(operands.size() == 2); |
| 7913 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 7914 | // "exp" is floating-point type (from HLSL intrinsic) |
| 7915 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 7916 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 7917 | builder.createStore(member1, operands[1]); |
| 7918 | } else |
| 7919 | // "exp" is integer type (from GLSL built-in function) |
| 7920 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 7921 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 7922 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7923 | break; |
| 7924 | default: |
| 7925 | break; |
| 7926 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 7927 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7928 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 7929 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7930 | } |
| 7931 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7932 | // Intrinsics with no arguments (or no return value, and no precision). |
| 7933 | 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] | 7934 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7935 | // 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] | 7936 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? |
| 7937 | spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7938 | |
| 7939 | switch (op) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7940 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7941 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7942 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7943 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7944 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7945 | spv::MemorySemanticsAcquireReleaseMask); |
| 7946 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7947 | } else { |
| 7948 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 7949 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7950 | } else { |
| 7951 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7952 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 7953 | spv::MemorySemanticsAcquireReleaseMask); |
| 7954 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7955 | return 0; |
| 7956 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7957 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 7958 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7959 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7960 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7961 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 7962 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7963 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7964 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7965 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7966 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7967 | return 0; |
| 7968 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7969 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 7970 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7971 | return 0; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7972 | #ifndef GLSLANG_WEB |
| 7973 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 7974 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 7975 | spv::MemorySemanticsAcquireReleaseMask); |
| 7976 | return 0; |
| 7977 | case glslang::EOpMemoryBarrierImage: |
| 7978 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 7979 | spv::MemorySemanticsAcquireReleaseMask); |
| 7980 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7981 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7982 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 7983 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7984 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7985 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7986 | case glslang::EOpDeviceMemoryBarrier: |
| 7987 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7988 | spv::MemorySemanticsImageMemoryMask | |
| 7989 | spv::MemorySemanticsAcquireReleaseMask); |
| 7990 | return 0; |
| 7991 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 7992 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 7993 | spv::MemorySemanticsImageMemoryMask | |
| 7994 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7995 | return 0; |
| 7996 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 7997 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 7998 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 7999 | return 0; |
| 8000 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8001 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 8002 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 8003 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8004 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8005 | case glslang::EOpSubgroupBarrier: |
| 8006 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 8007 | spv::MemorySemanticsAcquireReleaseMask); |
| 8008 | return spv::NoResult; |
| 8009 | case glslang::EOpSubgroupMemoryBarrier: |
| 8010 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 8011 | spv::MemorySemanticsAcquireReleaseMask); |
| 8012 | return spv::NoResult; |
| 8013 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 8014 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 8015 | spv::MemorySemanticsAcquireReleaseMask); |
| 8016 | return spv::NoResult; |
| 8017 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 8018 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 8019 | spv::MemorySemanticsAcquireReleaseMask); |
| 8020 | return spv::NoResult; |
| 8021 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 8022 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8023 | spv::MemorySemanticsAcquireReleaseMask); |
| 8024 | return spv::NoResult; |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 8025 | |
| 8026 | case glslang::EOpEmitVertex: |
| 8027 | builder.createNoResultOp(spv::OpEmitVertex); |
| 8028 | return 0; |
| 8029 | case glslang::EOpEndPrimitive: |
| 8030 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 8031 | return 0; |
| 8032 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8033 | case glslang::EOpSubgroupElect: { |
| 8034 | std::vector<spv::Id> operands; |
| 8035 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 8036 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8037 | case glslang::EOpTime: |
| 8038 | { |
| 8039 | std::vector<spv::Id> args; // Dummy arguments |
| 8040 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 8041 | return builder.setPrecision(id, precision); |
| 8042 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 8043 | case glslang::EOpIgnoreIntersection: |
| 8044 | builder.createNoResultOp(spv::OpIgnoreIntersectionKHR); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 8045 | return 0; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 8046 | case glslang::EOpTerminateRay: |
| 8047 | builder.createNoResultOp(spv::OpTerminateRayKHR); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 8048 | return 0; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8049 | case glslang::EOpRayQueryInitialize: |
| 8050 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR); |
| 8051 | return 0; |
| 8052 | case glslang::EOpRayQueryTerminate: |
| 8053 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR); |
| 8054 | return 0; |
| 8055 | case glslang::EOpRayQueryGenerateIntersection: |
| 8056 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR); |
| 8057 | return 0; |
| 8058 | case glslang::EOpRayQueryConfirmIntersection: |
| 8059 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR); |
| 8060 | return 0; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 8061 | case glslang::EOpBeginInvocationInterlock: |
| 8062 | builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
| 8063 | return 0; |
| 8064 | case glslang::EOpEndInvocationInterlock: |
| 8065 | builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
| 8066 | return 0; |
| 8067 | |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 8068 | case glslang::EOpIsHelperInvocation: |
| 8069 | { |
| 8070 | std::vector<spv::Id> args; // Dummy arguments |
Rex Xu | bb7307b | 2019-07-15 14:57:20 +0800 | [diff] [blame] | 8071 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 8072 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 8073 | return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args); |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 8074 | } |
| 8075 | |
amhagan | 91fb009 | 2019-07-10 21:14:38 -0400 | [diff] [blame] | 8076 | case glslang::EOpReadClockSubgroupKHR: { |
| 8077 | std::vector<spv::Id> args; |
| 8078 | args.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 8079 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 8080 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 8081 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 8082 | } |
| 8083 | |
| 8084 | case glslang::EOpReadClockDeviceKHR: { |
| 8085 | std::vector<spv::Id> args; |
| 8086 | args.push_back(builder.makeUintConstant(spv::ScopeDevice)); |
| 8087 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 8088 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 8089 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 8090 | } |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 8091 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8092 | default: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8093 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8094 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8095 | |
| 8096 | logger->missingFunctionality("unknown operation with no arguments"); |
| 8097 | |
| 8098 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8099 | } |
| 8100 | |
| 8101 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 8102 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 8103 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8104 | spv::Id id; |
| 8105 | if (symbolValues.end() != iter) { |
| 8106 | id = iter->second; |
| 8107 | return id; |
| 8108 | } |
| 8109 | |
| 8110 | // it was not found, create it |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8111 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 8112 | auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8113 | id = createSpvVariable(symbol, forcedType.first); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8114 | symbolValues[symbol->getId()] = id; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8115 | if (forcedType.second != spv::NoType) |
| 8116 | forceType[id] = forcedType.second; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8117 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 8118 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8119 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 8120 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 8121 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8122 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8123 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8124 | if (symbol->getQualifier().hasComponent()) |
| 8125 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 8126 | if (symbol->getQualifier().hasIndex()) |
| 8127 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8128 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8129 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8130 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 8131 | // atomic counters use this: |
| 8132 | if (symbol->getQualifier().hasOffset()) |
| 8133 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8134 | } |
| 8135 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 8136 | if (symbol->getQualifier().hasLocation()) |
| 8137 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8138 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 8139 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8140 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8141 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8142 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8143 | if (symbol->getQualifier().hasSet()) |
| 8144 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8145 | else if (IsDescriptorResource(symbol->getType())) { |
| 8146 | // default to 0 |
| 8147 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 8148 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8149 | if (symbol->getQualifier().hasBinding()) |
| 8150 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
Jeff Bolz | 0a93cfb | 2018-12-11 20:53:59 -0600 | [diff] [blame] | 8151 | else if (IsDescriptorResource(symbol->getType())) { |
| 8152 | // default to 0 |
| 8153 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 8154 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8155 | if (symbol->getQualifier().hasAttachment()) |
| 8156 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8157 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8158 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 8159 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8160 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 8161 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 8162 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 8163 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 8164 | } |
| 8165 | if (symbol->getQualifier().hasXfbOffset()) |
| 8166 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8167 | } |
| 8168 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8169 | // add built-in variable decoration |
| 8170 | if (builtIn != spv::BuiltInMax) { |
| 8171 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
| 8172 | } |
| 8173 | |
| 8174 | #ifndef GLSLANG_WEB |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8175 | if (symbol->getType().isImage()) { |
| 8176 | std::vector<spv::Decoration> memory; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8177 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, |
| 8178 | glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8179 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8180 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8181 | } |
| 8182 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 8183 | // nonuniform |
| 8184 | builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 8185 | |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8186 | if (builtIn == spv::BuiltInSampleMask) { |
| 8187 | spv::Decoration decoration; |
| 8188 | // GL_NV_sample_mask_override_coverage extension |
| 8189 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8190 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8191 | else |
| 8192 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8193 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8194 | if (decoration != spv::DecorationMax) { |
Jason Macnak | dbd4c3c | 2019-07-12 14:33:02 -0700 | [diff] [blame] | 8195 | builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8196 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 8197 | } |
| 8198 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8199 | else if (builtIn == spv::BuiltInLayer) { |
| 8200 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 8201 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8202 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8203 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 8204 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 8205 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 8206 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8207 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 8208 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8209 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 8210 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 8211 | } |
| 8212 | } |
| 8213 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 8214 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8215 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8216 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 8217 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 8218 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 8219 | if (symbol->getQualifier().pervertexNV) { |
| 8220 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 8221 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 8222 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 8223 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8224 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8225 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 8226 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 8227 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 8228 | symbol->getType().getQualifier().semanticName); |
| 8229 | } |
| 8230 | |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 8231 | if (symbol->isReference()) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8232 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? |
| 8233 | spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8234 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8235 | #endif |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8236 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8237 | return id; |
| 8238 | } |
| 8239 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8240 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8241 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 8242 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 8243 | { |
| 8244 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8245 | if (qualifier.perPrimitiveNV) { |
| 8246 | // Need to add capability/extension for fragment shader. |
| 8247 | // Mesh shader already adds this by default. |
| 8248 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 8249 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 8250 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 8251 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8252 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8253 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8254 | if (qualifier.perViewNV) |
| 8255 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 8256 | if (qualifier.perTaskNV) |
| 8257 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 8258 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8259 | if (qualifier.perPrimitiveNV) { |
| 8260 | // Need to add capability/extension for fragment shader. |
| 8261 | // Mesh shader already adds this by default. |
| 8262 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 8263 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 8264 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 8265 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8266 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8267 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8268 | if (qualifier.perViewNV) |
| 8269 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 8270 | if (qualifier.perTaskNV) |
| 8271 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 8272 | } |
| 8273 | } |
| 8274 | #endif |
| 8275 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8276 | // 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] | 8277 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8278 | // |
| 8279 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 8280 | // |
| 8281 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 8282 | // regular constants, which themselves are trees that createSpvConstant() |
| 8283 | // recursively walks. So, this function walks the "top" of the tree: |
| 8284 | // - emit specialization constant-building instructions for specConstant |
| 8285 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8286 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8287 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 8288 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8289 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8290 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8291 | if (! node.getQualifier().specConstant) { |
| 8292 | // hand off to the non-spec-constant path |
| 8293 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 8294 | int nextConst = 0; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8295 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? |
| 8296 | node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
| 8297 | nextConst, false); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8298 | } |
| 8299 | |
| 8300 | // We now know we have a specialization constant to build |
| 8301 | |
Ricardo Garcia | 232ba0d | 2020-06-03 15:52:55 +0200 | [diff] [blame] | 8302 | // Extra capabilities may be needed. |
| 8303 | if (node.getType().contains8BitInt()) |
| 8304 | builder.addCapability(spv::CapabilityInt8); |
| 8305 | if (node.getType().contains16BitFloat()) |
| 8306 | builder.addCapability(spv::CapabilityFloat16); |
| 8307 | if (node.getType().contains16BitInt()) |
| 8308 | builder.addCapability(spv::CapabilityInt16); |
| 8309 | if (node.getType().contains64BitInt()) |
| 8310 | builder.addCapability(spv::CapabilityInt64); |
| 8311 | if (node.getType().containsDouble()) |
| 8312 | builder.addCapability(spv::CapabilityFloat64); |
| 8313 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 8314 | // 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] | 8315 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 8316 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 8317 | std::vector<spv::Id> dimConstId; |
| 8318 | for (int dim = 0; dim < 3; ++dim) { |
| 8319 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 8320 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8321 | if (specConst) { |
| 8322 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 8323 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 8324 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8325 | } |
| 8326 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 8327 | } |
| 8328 | |
| 8329 | // An AST node labelled as specialization constant should be a symbol node. |
| 8330 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 8331 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8332 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8333 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 8334 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 8335 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 8336 | // will set the builder into spec constant op instruction generating mode. |
| 8337 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8338 | result = accessChainLoad(sub_tree->getType()); |
| 8339 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8340 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8341 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 8342 | } else { |
| 8343 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 8344 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8345 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8346 | builder.addName(result, sn->getName().c_str()); |
| 8347 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8348 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8349 | |
| 8350 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 8351 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8352 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8353 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8354 | } |
| 8355 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8356 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 8357 | // build the aggregate SPIR-V constant. |
| 8358 | // |
| 8359 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 8360 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 8361 | // |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8362 | spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, |
| 8363 | const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8364 | { |
| 8365 | // vector of constants for SPIR-V |
| 8366 | std::vector<spv::Id> spvConsts; |
| 8367 | |
| 8368 | // Type is used for struct and array constants |
| 8369 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 8370 | |
| 8371 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 8372 | glslang::TType elementType(glslangType, 0); |
| 8373 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8374 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8375 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 8376 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8377 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8378 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 8379 | } else if (glslangType.isCoopMat()) { |
| 8380 | glslang::TType componentType(glslangType.getBasicType()); |
| 8381 | spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8382 | } else if (glslangType.isStruct()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8383 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 8384 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8385 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 8386 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8387 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 8388 | bool zero = nextConst >= consts.size(); |
| 8389 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8390 | case glslang::EbtInt: |
| 8391 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 8392 | break; |
| 8393 | case glslang::EbtUint: |
| 8394 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 8395 | break; |
| 8396 | case glslang::EbtFloat: |
| 8397 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 8398 | break; |
| 8399 | case glslang::EbtBool: |
| 8400 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 8401 | break; |
| 8402 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8403 | case glslang::EbtInt8: |
| 8404 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 8405 | break; |
| 8406 | case glslang::EbtUint8: |
| 8407 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 8408 | break; |
| 8409 | case glslang::EbtInt16: |
| 8410 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 8411 | break; |
| 8412 | case glslang::EbtUint16: |
| 8413 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 8414 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 8415 | case glslang::EbtInt64: |
| 8416 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 8417 | break; |
| 8418 | case glslang::EbtUint64: |
| 8419 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 8420 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8421 | case glslang::EbtDouble: |
| 8422 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 8423 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 8424 | case glslang::EbtFloat16: |
| 8425 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 8426 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8427 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8428 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8429 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8430 | break; |
| 8431 | } |
| 8432 | ++nextConst; |
| 8433 | } |
| 8434 | } else { |
| 8435 | // we have a non-aggregate (scalar) constant |
| 8436 | bool zero = nextConst >= consts.size(); |
| 8437 | spv::Id scalar = 0; |
| 8438 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8439 | case glslang::EbtInt: |
| 8440 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
| 8441 | break; |
| 8442 | case glslang::EbtUint: |
| 8443 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
| 8444 | break; |
| 8445 | case glslang::EbtFloat: |
| 8446 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8447 | break; |
| 8448 | case glslang::EbtBool: |
| 8449 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
| 8450 | break; |
| 8451 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8452 | case glslang::EbtInt8: |
| 8453 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 8454 | break; |
| 8455 | case glslang::EbtUint8: |
| 8456 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 8457 | break; |
| 8458 | case glslang::EbtInt16: |
| 8459 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 8460 | break; |
| 8461 | case glslang::EbtUint16: |
| 8462 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 8463 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 8464 | case glslang::EbtInt64: |
| 8465 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 8466 | break; |
| 8467 | case glslang::EbtUint64: |
| 8468 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8469 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8470 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8471 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8472 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 8473 | case glslang::EbtFloat16: |
| 8474 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8475 | break; |
Jeff Bolz | 3fd1232 | 2019-03-05 23:27:09 -0600 | [diff] [blame] | 8476 | case glslang::EbtReference: |
| 8477 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8478 | scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar); |
| 8479 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8480 | #endif |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 8481 | case glslang::EbtString: |
| 8482 | scalar = builder.getStringId(consts[nextConst].getSConst()->c_str()); |
| 8483 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8484 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8485 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8486 | break; |
| 8487 | } |
| 8488 | ++nextConst; |
| 8489 | return scalar; |
| 8490 | } |
| 8491 | |
| 8492 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 8493 | } |
| 8494 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8495 | // Return true if the node is a constant or symbol whose reading has no |
| 8496 | // non-trivial observable cost or effect. |
| 8497 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 8498 | { |
| 8499 | // don't know what this is |
| 8500 | if (node == nullptr) |
| 8501 | return false; |
| 8502 | |
| 8503 | // a constant is safe |
| 8504 | if (node->getAsConstantUnion() != nullptr) |
| 8505 | return true; |
| 8506 | |
| 8507 | // not a symbol means non-trivial |
| 8508 | if (node->getAsSymbolNode() == nullptr) |
| 8509 | return false; |
| 8510 | |
| 8511 | // a symbol, depends on what's being read |
| 8512 | switch (node->getType().getQualifier().storage) { |
| 8513 | case glslang::EvqTemporary: |
| 8514 | case glslang::EvqGlobal: |
| 8515 | case glslang::EvqIn: |
| 8516 | case glslang::EvqInOut: |
| 8517 | case glslang::EvqConst: |
| 8518 | case glslang::EvqConstReadOnly: |
| 8519 | case glslang::EvqUniform: |
| 8520 | return true; |
| 8521 | default: |
| 8522 | return false; |
| 8523 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 8524 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8525 | |
| 8526 | // 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] | 8527 | // 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] | 8528 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8529 | // Return true if trivial. |
| 8530 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 8531 | { |
| 8532 | if (node == nullptr) |
| 8533 | return false; |
| 8534 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 8535 | // count non scalars as trivial, as well as anything coming from HLSL |
| 8536 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 8537 | return true; |
| 8538 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8539 | // symbols and constants are trivial |
| 8540 | if (isTrivialLeaf(node)) |
| 8541 | return true; |
| 8542 | |
| 8543 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 8544 | |
| 8545 | // not a simple operation |
| 8546 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 8547 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 8548 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 8549 | return false; |
| 8550 | |
| 8551 | // not on leaf nodes |
| 8552 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 8553 | return false; |
| 8554 | |
| 8555 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 8556 | return false; |
| 8557 | } |
| 8558 | |
| 8559 | switch (node->getAsOperator()->getOp()) { |
| 8560 | case glslang::EOpLogicalNot: |
| 8561 | case glslang::EOpConvIntToBool: |
| 8562 | case glslang::EOpConvUintToBool: |
| 8563 | case glslang::EOpConvFloatToBool: |
| 8564 | case glslang::EOpConvDoubleToBool: |
| 8565 | case glslang::EOpEqual: |
| 8566 | case glslang::EOpNotEqual: |
| 8567 | case glslang::EOpLessThan: |
| 8568 | case glslang::EOpGreaterThan: |
| 8569 | case glslang::EOpLessThanEqual: |
| 8570 | case glslang::EOpGreaterThanEqual: |
| 8571 | case glslang::EOpIndexDirect: |
| 8572 | case glslang::EOpIndexDirectStruct: |
| 8573 | case glslang::EOpLogicalXor: |
| 8574 | case glslang::EOpAny: |
| 8575 | case glslang::EOpAll: |
| 8576 | return true; |
| 8577 | default: |
| 8578 | return false; |
| 8579 | } |
| 8580 | } |
| 8581 | |
| 8582 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 8583 | // the left side is true (for &&) or false (for ||). |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8584 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, |
| 8585 | glslang::TIntermTyped& right) |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8586 | { |
| 8587 | spv::Id boolTypeId = builder.makeBoolType(); |
| 8588 | |
| 8589 | // emit left operand |
| 8590 | builder.clearAccessChain(); |
| 8591 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8592 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8593 | |
| 8594 | // Operands to accumulate OpPhi operands |
| 8595 | std::vector<spv::Id> phiOperands; |
| 8596 | // accumulate left operand's phi information |
| 8597 | phiOperands.push_back(leftId); |
| 8598 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8599 | |
| 8600 | // Make the two kinds of operation symmetric with a "!" |
| 8601 | // || => emit "if (! left) result = right" |
| 8602 | // && => emit "if ( left) result = right" |
| 8603 | // |
| 8604 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 8605 | // to 'builder' to have an "else" without an "then" |
| 8606 | if (op == glslang::EOpLogicalOr) |
| 8607 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 8608 | |
| 8609 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 8610 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8611 | |
| 8612 | // emit right operand as the "then" part of the "if" |
| 8613 | builder.clearAccessChain(); |
| 8614 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8615 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8616 | |
| 8617 | // accumulate left operand's phi information |
| 8618 | phiOperands.push_back(rightId); |
| 8619 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8620 | |
| 8621 | // finish the "if" |
| 8622 | ifBuilder.makeEndIf(); |
| 8623 | |
| 8624 | // phi together the two results |
| 8625 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 8626 | } |
| 8627 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8628 | #ifndef GLSLANG_WEB |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8629 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 8630 | // Import this set if it has not been imported yet. |
| 8631 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 8632 | { |
| 8633 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 8634 | return extBuiltinMap[name]; |
| 8635 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 8636 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8637 | spv::Id extBuiltins = builder.import(name); |
| 8638 | extBuiltinMap[name] = extBuiltins; |
| 8639 | return extBuiltins; |
| 8640 | } |
| 8641 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 8642 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8643 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8644 | }; // end anonymous namespace |
| 8645 | |
| 8646 | namespace glslang { |
| 8647 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8648 | void GetSpirvVersion(std::string& version) |
| 8649 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 8650 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 8651 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8652 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8653 | version = buf; |
| 8654 | } |
| 8655 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8656 | // For low-order part of the generator's magic number. Bump up |
| 8657 | // when there is a change in the style (e.g., if SSA form changes, |
| 8658 | // or a different instruction sequence to do something gets used). |
| 8659 | int GetSpirvGeneratorVersion() |
| 8660 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 8661 | // return 1; // start |
| 8662 | // 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] | 8663 | // 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] | 8664 | // 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] | 8665 | // 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] | 8666 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 8667 | // 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] | 8668 | // 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] | 8669 | // return 8; // switch to new dead block eliminator; use OpUnreachable |
Graeme Leese | 060882f | 2020-06-22 11:03:46 +0100 | [diff] [blame] | 8670 | // return 9; // don't include opaque function parameters in OpEntryPoint global's operand list |
| 8671 | return 10; // Generate OpFUnordNotEqual for != comparisons |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8672 | } |
| 8673 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8674 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8675 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8676 | { |
| 8677 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8678 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8679 | if (out.fail()) |
| 8680 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8681 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 8682 | unsigned int word = spirv[i]; |
| 8683 | out.write((const char*)&word, 4); |
| 8684 | } |
| 8685 | out.close(); |
| 8686 | } |
| 8687 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8688 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 8689 | 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] | 8690 | { |
Shahbaz Youssefi | 8c49d15 | 2020-07-03 15:42:53 -0400 | [diff] [blame] | 8691 | #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE) |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8692 | std::ofstream out; |
| 8693 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8694 | if (out.fail()) |
| 8695 | printf("ERROR: Failed to open file: %s\n", baseName); |
Ben Clayton | fbe9a23 | 2020-06-17 11:17:19 +0100 | [diff] [blame] | 8696 | out << "\t// " << |
| 8697 | GetSpirvGeneratorVersion() << |
| 8698 | GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH << |
| 8699 | GLSLANG_VERSION_FLAVOR << std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8700 | if (varName != nullptr) { |
| 8701 | out << "\t #pragma once" << std::endl; |
| 8702 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 8703 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8704 | const int WORDS_PER_LINE = 8; |
| 8705 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 8706 | out << "\t"; |
| 8707 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 8708 | const unsigned int word = spirv[i + j]; |
| 8709 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 8710 | if (i + j + 1 < (int)spirv.size()) { |
| 8711 | out << ","; |
| 8712 | } |
| 8713 | } |
| 8714 | out << std::endl; |
| 8715 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8716 | if (varName != nullptr) { |
| 8717 | out << "};"; |
| 8718 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8719 | out.close(); |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8720 | #endif |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8721 | } |
| 8722 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8723 | // |
| 8724 | // Set up the glslang traversal |
| 8725 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8726 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8727 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8728 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8729 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8730 | } |
| 8731 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8732 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8733 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8734 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8735 | TIntermNode* root = intermediate.getTreeRoot(); |
| 8736 | |
| 8737 | if (root == 0) |
| 8738 | return; |
| 8739 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8740 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8741 | if (options == nullptr) |
| 8742 | options = &defaultOptions; |
| 8743 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8744 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8745 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 8746 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8747 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 8748 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8749 | it.dumpSpv(spirv); |
| 8750 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 8751 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8752 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 8753 | // eg. forward and remove memory writes of opaque types. |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8754 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
Shahbaz Youssefi | d52dce5 | 2020-06-17 12:47:44 -0400 | [diff] [blame] | 8755 | if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) { |
| 8756 | SpirvToolsTransform(intermediate, spirv, logger, options); |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8757 | prelegalization = false; |
| 8758 | } |
Shahbaz Youssefi | d52dce5 | 2020-06-17 12:47:44 -0400 | [diff] [blame] | 8759 | else if (options->stripDebugInfo) { |
| 8760 | // Strip debug info even if optimization is disabled. |
| 8761 | SpirvToolsStripDebugInfo(intermediate, spirv, logger); |
| 8762 | } |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8763 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8764 | if (options->validate) |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8765 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8766 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8767 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8768 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8769 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8770 | #endif |
| 8771 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8772 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8773 | } |
| 8774 | |
| 8775 | }; // end namespace glslang |