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); |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 152 | spv::Decoration TranslateNonUniformDecoration(const spv::Builder::AccessChain::CoherentFlags& coherentFlags); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 153 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 154 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 155 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 156 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 157 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 158 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 159 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 160 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 161 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 162 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 163 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 164 | spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 165 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 166 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 167 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 168 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 169 | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 170 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 171 | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 172 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 173 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 174 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 175 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 176 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 177 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 178 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 179 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 180 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 181 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 182 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 183 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 184 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 185 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 186 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 187 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 188 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 189 | bool writableParam(glslang::TStorageQualifier) const; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 190 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 191 | void makeFunctions(const glslang::TIntermSequence&); |
| 192 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 193 | void collectRayTracingLinkerObjects(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 194 | void visitFunctions(const glslang::TIntermSequence&); |
| 195 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 196 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 197 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 198 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 199 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 200 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 201 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 202 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 203 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 204 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 205 | 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] | 206 | glslang::TBasicType typeProxy, |
| 207 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 208 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 209 | glslang::TBasicType typeProxy); |
| 210 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 211 | glslang::TBasicType typeProxy); |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 212 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 213 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 214 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
| 215 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 216 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
| 217 | spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
| 218 | glslang::TBasicType typeProxy); |
| 219 | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 220 | spv::Id typeId, std::vector<spv::Id>& operands); |
| 221 | spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
| 222 | glslang::TBasicType typeProxy); |
| 223 | spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
| 224 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 225 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 226 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 227 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 228 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 229 | spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, |
| 230 | int& nextConst, bool specConstant); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 231 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 232 | bool isTrivial(const glslang::TIntermTyped* node); |
| 233 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 234 | spv::Id getExtBuiltins(const char* name); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 235 | 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] | 236 | spv::Id translateForcedType(spv::Id object); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 237 | spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 238 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 239 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 240 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 241 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 242 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 243 | int sequenceDepth; |
| 244 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 245 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 246 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 247 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 248 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 249 | bool inEntryPoint; |
| 250 | bool entryPointTerminated; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 251 | bool linkageOnly; // true when visiting the set of objects in the AST present only for |
| 252 | // establishing interface, whether or not they were statically used |
John Kessenich | 59420fd | 2015-12-21 11:45:34 -0700 | [diff] [blame] | 253 | 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] | 254 | const glslang::TIntermediate* glslangIntermediate; |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 255 | 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] | 256 | spv::Id stdBuiltins; |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 257 | spv::Id nonSemanticDebugPrintf; |
Rex Xu | 77fe62f | 2021-02-26 17:25:45 +0800 | [diff] [blame] | 258 | std::unordered_map<std::string, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 259 | |
Chow | 93b400f | 2020-11-12 15:54:16 +0800 | [diff] [blame] | 260 | std::unordered_map<long long, spv::Id> symbolValues; |
| 261 | std::unordered_set<long long> rValueParameters; // set of formal function parameters passed as rValues, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 262 | // rather than a pointer |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 263 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 264 | 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] | 265 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
Chow | 93b400f | 2020-11-12 15:54:16 +0800 | [diff] [blame] | 266 | std::unordered_map<long long, std::vector<int>> memberRemapper; |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 267 | // for mapping glslang symbol struct to symbol Id |
Chow | 93b400f | 2020-11-12 15:54:16 +0800 | [diff] [blame] | 268 | std::unordered_map<const glslang::TTypeList*, long long> glslangTypeToIdMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 269 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 270 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 271 | // Map pointee types for EbtReference to their forward pointers |
| 272 | std::map<const glslang::TType *, spv::Id> forwardPointers; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 273 | // Type forcing, for when SPIR-V wants a different type than the AST, |
| 274 | // requiring local translation to and from SPIR-V type on every access. |
| 275 | // Maps <builtin-variable-id -> AST-required-type-id> |
| 276 | std::unordered_map<spv::Id, spv::Id> forceType; |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 277 | |
| 278 | // Used later for generating OpTraceKHR/OpExecuteCallableKHR |
| 279 | std::unordered_map<unsigned int, glslang::TIntermSymbol *> locationToSymbol[2]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | // |
| 283 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 284 | // |
| 285 | |
| 286 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 287 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 288 | { |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 289 | #ifdef GLSLANG_WEB |
| 290 | return spv::SourceLanguageESSL; |
Shahbaz Youssefi | 1ef2e25 | 2020-07-03 15:42:53 -0400 | [diff] [blame] | 291 | #elif defined(GLSLANG_ANGLE) |
| 292 | return spv::SourceLanguageGLSL; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 293 | #endif |
| 294 | |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 295 | switch (source) { |
| 296 | case glslang::EShSourceGlsl: |
| 297 | switch (profile) { |
| 298 | case ENoProfile: |
| 299 | case ECoreProfile: |
| 300 | case ECompatibilityProfile: |
| 301 | return spv::SourceLanguageGLSL; |
| 302 | case EEsProfile: |
| 303 | return spv::SourceLanguageESSL; |
| 304 | default: |
| 305 | return spv::SourceLanguageUnknown; |
| 306 | } |
| 307 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 308 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 309 | default: |
| 310 | return spv::SourceLanguageUnknown; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Translate glslang language (stage) to SPIR-V execution model. |
| 315 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 316 | { |
| 317 | switch (stage) { |
| 318 | case EShLangVertex: return spv::ExecutionModelVertex; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 319 | case EShLangFragment: return spv::ExecutionModelFragment; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 320 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
John Kessenich | 51ed01c | 2019-10-10 11:40:11 -0600 | [diff] [blame] | 321 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 322 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 323 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 324 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 325 | case EShLangRayGen: return spv::ExecutionModelRayGenerationKHR; |
| 326 | case EShLangIntersect: return spv::ExecutionModelIntersectionKHR; |
| 327 | case EShLangAnyHit: return spv::ExecutionModelAnyHitKHR; |
| 328 | case EShLangClosestHit: return spv::ExecutionModelClosestHitKHR; |
| 329 | case EShLangMiss: return spv::ExecutionModelMissKHR; |
| 330 | case EShLangCallable: return spv::ExecutionModelCallableKHR; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 331 | case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 332 | case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 333 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 334 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 335 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 336 | return spv::ExecutionModelFragment; |
| 337 | } |
| 338 | } |
| 339 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 340 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 341 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 342 | { |
| 343 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 344 | case glslang::Esd1D: return spv::Dim1D; |
| 345 | case glslang::Esd2D: return spv::Dim2D; |
| 346 | case glslang::Esd3D: return spv::Dim3D; |
| 347 | case glslang::EsdCube: return spv::DimCube; |
| 348 | case glslang::EsdRect: return spv::DimRect; |
| 349 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 350 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 351 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 352 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 353 | return spv::Dim2D; |
| 354 | } |
| 355 | } |
| 356 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 357 | // Translate glslang precision to SPIR-V precision decorations. |
| 358 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 359 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 360 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 361 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 362 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 363 | default: |
| 364 | return spv::NoPrecision; |
| 365 | } |
| 366 | } |
| 367 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 368 | // Translate glslang type to SPIR-V precision decorations. |
| 369 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 370 | { |
| 371 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 372 | } |
| 373 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 374 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 375 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 376 | { |
| 377 | if (type.getBasicType() == glslang::EbtBlock) { |
| 378 | switch (type.getQualifier().storage) { |
| 379 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 380 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 381 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 382 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
Caio Marcelo de Oliveira Filho | 4bfbf62 | 2020-06-02 16:58:51 -0700 | [diff] [blame] | 383 | case glslang::EvqShared: return spv::DecorationBlock; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 384 | #ifndef GLSLANG_WEB |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 385 | case glslang::EvqPayload: return spv::DecorationBlock; |
| 386 | case glslang::EvqPayloadIn: return spv::DecorationBlock; |
| 387 | case glslang::EvqHitAttr: return spv::DecorationBlock; |
| 388 | case glslang::EvqCallableData: return spv::DecorationBlock; |
| 389 | case glslang::EvqCallableDataIn: return spv::DecorationBlock; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 390 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 391 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 392 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 397 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 398 | } |
| 399 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 400 | // Translate glslang type to SPIR-V memory decorations. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 401 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, |
| 402 | bool useVulkanMemoryModel) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 403 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 404 | if (!useVulkanMemoryModel) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 405 | if (qualifier.isCoherent()) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 406 | memory.push_back(spv::DecorationCoherent); |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 407 | if (qualifier.isVolatile()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 408 | memory.push_back(spv::DecorationVolatile); |
| 409 | memory.push_back(spv::DecorationCoherent); |
| 410 | } |
John Kessenich | 14b85d3 | 2018-06-04 15:36:03 -0600 | [diff] [blame] | 411 | } |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 412 | if (qualifier.isRestrict()) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 413 | memory.push_back(spv::DecorationRestrict); |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 414 | if (qualifier.isReadOnly()) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 415 | memory.push_back(spv::DecorationNonWritable); |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 416 | if (qualifier.isWriteOnly()) |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 417 | memory.push_back(spv::DecorationNonReadable); |
| 418 | } |
| 419 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 420 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 421 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 422 | { |
| 423 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 424 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 425 | case glslang::ElmRowMajor: |
| 426 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 427 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 428 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 429 | default: |
| 430 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 431 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 432 | } |
| 433 | } else { |
| 434 | switch (type.getBasicType()) { |
| 435 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 436 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 437 | break; |
| 438 | case glslang::EbtBlock: |
| 439 | switch (type.getQualifier().storage) { |
Caio Marcelo de Oliveira Filho | 4bfbf62 | 2020-06-02 16:58:51 -0700 | [diff] [blame] | 440 | case glslang::EvqShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 441 | case glslang::EvqUniform: |
| 442 | case glslang::EvqBuffer: |
| 443 | switch (type.getQualifier().layoutPacking) { |
| 444 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 445 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 446 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 447 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 448 | } |
| 449 | case glslang::EvqVaryingIn: |
| 450 | case glslang::EvqVaryingOut: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 451 | if (type.getQualifier().isTaskMemory()) { |
| 452 | switch (type.getQualifier().layoutPacking) { |
| 453 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 454 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 455 | default: break; |
| 456 | } |
| 457 | } else { |
| 458 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 459 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 460 | return spv::DecorationMax; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 461 | #ifndef GLSLANG_WEB |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 462 | case glslang::EvqPayload: |
| 463 | case glslang::EvqPayloadIn: |
| 464 | case glslang::EvqHitAttr: |
| 465 | case glslang::EvqCallableData: |
| 466 | case glslang::EvqCallableDataIn: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 467 | return spv::DecorationMax; |
| 468 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 469 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 470 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 471 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 478 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 479 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 480 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 481 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 482 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 483 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 484 | return spv::DecorationMax; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 485 | else if (qualifier.isNonPerspective()) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 486 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 487 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 488 | return spv::DecorationFlat; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 489 | else if (qualifier.isExplicitInterpolation()) { |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 490 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 491 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 492 | } |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 493 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 494 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 498 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 499 | // should be applied. |
| 500 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 501 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 502 | if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 503 | return spv::DecorationCentroid; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 504 | #ifndef GLSLANG_WEB |
| 505 | else if (qualifier.patch) |
| 506 | return spv::DecorationPatch; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 507 | else if (qualifier.sample) { |
| 508 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 509 | return spv::DecorationSample; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 510 | } |
| 511 | #endif |
| 512 | |
| 513 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 514 | } |
| 515 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 516 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 517 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 518 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 519 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 520 | return spv::DecorationInvariant; |
| 521 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 522 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 523 | } |
| 524 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 525 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 526 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 527 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 528 | #ifndef GLSLANG_WEB |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 529 | if (qualifier.isNoContraction()) |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 530 | return spv::DecorationNoContraction; |
| 531 | else |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 532 | #endif |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 533 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 534 | } |
| 535 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 536 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 537 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 538 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 539 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 540 | if (qualifier.isNonUniform()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 541 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 542 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 543 | return spv::DecorationNonUniformEXT; |
| 544 | } else |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 545 | #endif |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 546 | return spv::DecorationMax; |
| 547 | } |
| 548 | |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 549 | // If lvalue flags contains nonUniform, return SPIR-V NonUniform decoration. |
| 550 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration( |
| 551 | const spv::Builder::AccessChain::CoherentFlags& coherentFlags) |
| 552 | { |
| 553 | #ifndef GLSLANG_WEB |
| 554 | if (coherentFlags.isNonUniform()) { |
| 555 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
| 556 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 557 | return spv::DecorationNonUniformEXT; |
| 558 | } else |
| 559 | #endif |
| 560 | return spv::DecorationMax; |
| 561 | } |
| 562 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 563 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess( |
| 564 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 565 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 566 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 567 | |
| 568 | #ifndef GLSLANG_WEB |
| 569 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) |
| 570 | return mask; |
| 571 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 572 | if (coherentFlags.isVolatile() || coherentFlags.anyCoherent()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 573 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 574 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 575 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 576 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 577 | if (coherentFlags.nonprivate) { |
| 578 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 579 | } |
| 580 | if (coherentFlags.volatil) { |
| 581 | mask = mask | spv::MemoryAccessVolatileMask; |
| 582 | } |
| 583 | if (mask != spv::MemoryAccessMaskNone) { |
| 584 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 585 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 586 | #endif |
| 587 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 588 | return mask; |
| 589 | } |
| 590 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 591 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands( |
| 592 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 593 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 594 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 595 | |
| 596 | #ifndef GLSLANG_WEB |
| 597 | if (!glslangIntermediate->usingVulkanMemoryModel()) |
| 598 | return mask; |
| 599 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 600 | if (coherentFlags.volatil || |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 601 | coherentFlags.anyCoherent()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 602 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 603 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 604 | } |
| 605 | if (coherentFlags.nonprivate) { |
| 606 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 607 | } |
| 608 | if (coherentFlags.volatil) { |
| 609 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 610 | } |
| 611 | if (mask != spv::ImageOperandsMaskNone) { |
| 612 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 613 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 614 | #endif |
| 615 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 616 | return mask; |
| 617 | } |
| 618 | |
| 619 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 620 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 621 | spv::Builder::AccessChain::CoherentFlags flags = {}; |
| 622 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 623 | flags.coherent = type.getQualifier().coherent; |
| 624 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 625 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 626 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 627 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 628 | type.getQualifier().storage == glslang::EvqShared; |
| 629 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 630 | flags.shadercallcoherent = type.getQualifier().shadercallcoherent; |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 631 | flags.volatil = type.getQualifier().volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 632 | // *coherent variables are implicitly nonprivate in GLSL |
| 633 | flags.nonprivate = type.getQualifier().nonprivate || |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 634 | flags.anyCoherent() || |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 635 | flags.volatil; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 636 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 637 | #endif |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 638 | flags.nonUniform = type.getQualifier().nonUniform; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 639 | return flags; |
| 640 | } |
| 641 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 642 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope( |
| 643 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 644 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 645 | spv::Scope scope = spv::ScopeMax; |
| 646 | |
| 647 | #ifndef GLSLANG_WEB |
Jeff Bolz | 38cbad1 | 2019-03-05 14:40:07 -0600 | [diff] [blame] | 648 | if (coherentFlags.volatil || coherentFlags.coherent) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 649 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 650 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 651 | } else if (coherentFlags.devicecoherent) { |
| 652 | scope = spv::ScopeDevice; |
| 653 | } else if (coherentFlags.queuefamilycoherent) { |
| 654 | scope = spv::ScopeQueueFamilyKHR; |
| 655 | } else if (coherentFlags.workgroupcoherent) { |
| 656 | scope = spv::ScopeWorkgroup; |
| 657 | } else if (coherentFlags.subgroupcoherent) { |
| 658 | scope = spv::ScopeSubgroup; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 659 | } else if (coherentFlags.shadercallcoherent) { |
| 660 | scope = spv::ScopeShaderCallKHR; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 661 | } |
| 662 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 663 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 664 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 665 | #endif |
| 666 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 667 | return scope; |
| 668 | } |
| 669 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 670 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 671 | // associated capabilities when required. For some built-in variables, a capability |
| 672 | // is generated only when using the variable in an executable instruction, but not when |
| 673 | // just declaring a struct member variable with it. This is true for PointSize, |
| 674 | // ClipDistance, and CullDistance. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 675 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, |
| 676 | bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 677 | { |
| 678 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 679 | case glslang::EbvPointSize: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 680 | #ifndef GLSLANG_WEB |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 681 | // Defer adding the capability until the built-in is actually used. |
| 682 | if (! memberDeclaration) { |
| 683 | switch (glslangIntermediate->getStage()) { |
| 684 | case EShLangGeometry: |
| 685 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 686 | break; |
| 687 | case EShLangTessControl: |
| 688 | case EShLangTessEvaluation: |
| 689 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 690 | break; |
| 691 | default: |
| 692 | break; |
| 693 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 694 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 695 | #endif |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 696 | return spv::BuiltInPointSize; |
| 697 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 698 | case glslang::EbvPosition: return spv::BuiltInPosition; |
| 699 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 700 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
| 701 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 702 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
| 703 | |
| 704 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 705 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 706 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
| 707 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 708 | |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 709 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 710 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 711 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 712 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 713 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 714 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
| 715 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 716 | #ifndef GLSLANG_WEB |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 717 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 718 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 719 | // They are now generated when used, rather than here when declared. |
| 720 | // Potentially, the specification should be more clear what the minimum |
| 721 | // use needed is to trigger the capability. |
| 722 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 723 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 724 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 725 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 726 | return spv::BuiltInClipDistance; |
| 727 | |
| 728 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 729 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 730 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 731 | return spv::BuiltInCullDistance; |
| 732 | |
| 733 | case glslang::EbvViewportIndex: |
David Neto | fb53f83 | 2020-11-12 15:00:16 -0500 | [diff] [blame] | 734 | if (glslangIntermediate->getStage() == EShLangGeometry || |
| 735 | glslangIntermediate->getStage() == EShLangFragment) { |
| 736 | builder.addCapability(spv::CapabilityMultiViewport); |
| 737 | } |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 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 | |
Sidney Just | 56350ca | 2020-11-02 11:27:40 -0800 | [diff] [blame] | 742 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
| 743 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
| 744 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
| 745 | } |
| 746 | else |
| 747 | builder.addCapability(spv::CapabilityShaderViewportIndex); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 748 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 749 | return spv::BuiltInViewportIndex; |
| 750 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 751 | case glslang::EbvSampleId: |
| 752 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 753 | return spv::BuiltInSampleId; |
| 754 | |
| 755 | case glslang::EbvSamplePosition: |
| 756 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 757 | return spv::BuiltInSamplePosition; |
| 758 | |
| 759 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 760 | return spv::BuiltInSampleMask; |
| 761 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 762 | case glslang::EbvLayer: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 763 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 764 | return spv::BuiltInLayer; |
| 765 | } |
David Neto | fb53f83 | 2020-11-12 15:00:16 -0500 | [diff] [blame] | 766 | if (glslangIntermediate->getStage() == EShLangGeometry || |
| 767 | glslangIntermediate->getStage() == EShLangFragment) { |
| 768 | builder.addCapability(spv::CapabilityGeometry); |
| 769 | } |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 770 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 771 | glslangIntermediate->getStage() == EShLangTessControl || |
| 772 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 773 | |
Sidney Just | 56350ca | 2020-11-02 11:27:40 -0800 | [diff] [blame] | 774 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
| 775 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
| 776 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
| 777 | } else |
| 778 | builder.addCapability(spv::CapabilityShaderLayer); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 779 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 780 | return spv::BuiltInLayer; |
| 781 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 782 | case glslang::EbvBaseVertex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 783 | 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] | 784 | builder.addCapability(spv::CapabilityDrawParameters); |
| 785 | return spv::BuiltInBaseVertex; |
| 786 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 787 | case glslang::EbvBaseInstance: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 788 | 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] | 789 | builder.addCapability(spv::CapabilityDrawParameters); |
| 790 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 791 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 792 | case glslang::EbvDrawId: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 793 | 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] | 794 | builder.addCapability(spv::CapabilityDrawParameters); |
| 795 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 796 | |
| 797 | case glslang::EbvPrimitiveId: |
| 798 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 799 | builder.addCapability(spv::CapabilityGeometry); |
| 800 | return spv::BuiltInPrimitiveId; |
| 801 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 802 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 803 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 804 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 805 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 806 | |
Chow | a315b56 | 2020-07-02 15:50:36 +0800 | [diff] [blame] | 807 | case glslang::EbvShadingRateKHR: |
| 808 | builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
| 809 | builder.addCapability(spv::CapabilityFragmentShadingRateKHR); |
| 810 | return spv::BuiltInShadingRateKHR; |
| 811 | |
| 812 | case glslang::EbvPrimitiveShadingRateKHR: |
| 813 | builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
| 814 | builder.addCapability(spv::CapabilityFragmentShadingRateKHR); |
| 815 | return spv::BuiltInPrimitiveShadingRateKHR; |
| 816 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 817 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 818 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 819 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 820 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 821 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 822 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 823 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 824 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 825 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 826 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 827 | return spv::BuiltInSubgroupSize; |
| 828 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 829 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 830 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 831 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 832 | return spv::BuiltInSubgroupLocalInvocationId; |
| 833 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 834 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 835 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 836 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 837 | return spv::BuiltInSubgroupEqMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 838 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 839 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 840 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 841 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 842 | return spv::BuiltInSubgroupGeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 843 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 844 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 845 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 846 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 847 | return spv::BuiltInSubgroupGtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 848 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 849 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 850 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 851 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 852 | return spv::BuiltInSubgroupLeMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 853 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 854 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 855 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 856 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 857 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 858 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 859 | case glslang::EbvNumSubgroups: |
| 860 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 861 | return spv::BuiltInNumSubgroups; |
| 862 | |
| 863 | case glslang::EbvSubgroupID: |
| 864 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 865 | return spv::BuiltInSubgroupId; |
| 866 | |
| 867 | case glslang::EbvSubgroupSize2: |
| 868 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 869 | return spv::BuiltInSubgroupSize; |
| 870 | |
| 871 | case glslang::EbvSubgroupInvocation2: |
| 872 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 873 | return spv::BuiltInSubgroupLocalInvocationId; |
| 874 | |
| 875 | case glslang::EbvSubgroupEqMask2: |
| 876 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 877 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 878 | return spv::BuiltInSubgroupEqMask; |
| 879 | |
| 880 | case glslang::EbvSubgroupGeMask2: |
| 881 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 882 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 883 | return spv::BuiltInSubgroupGeMask; |
| 884 | |
| 885 | case glslang::EbvSubgroupGtMask2: |
| 886 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 887 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 888 | return spv::BuiltInSubgroupGtMask; |
| 889 | |
| 890 | case glslang::EbvSubgroupLeMask2: |
| 891 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 892 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 893 | return spv::BuiltInSubgroupLeMask; |
| 894 | |
| 895 | case glslang::EbvSubgroupLtMask2: |
| 896 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 897 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 898 | return spv::BuiltInSubgroupLtMask; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 899 | |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 900 | case glslang::EbvBaryCoordNoPersp: |
| 901 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 902 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 903 | |
| 904 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 905 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 906 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 907 | |
| 908 | case glslang::EbvBaryCoordNoPerspSample: |
| 909 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 910 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 911 | |
| 912 | case glslang::EbvBaryCoordSmooth: |
| 913 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 914 | return spv::BuiltInBaryCoordSmoothAMD; |
| 915 | |
| 916 | case glslang::EbvBaryCoordSmoothCentroid: |
| 917 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 918 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 919 | |
| 920 | case glslang::EbvBaryCoordSmoothSample: |
| 921 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 922 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 923 | |
| 924 | case glslang::EbvBaryCoordPullModel: |
| 925 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 926 | return spv::BuiltInBaryCoordPullModelAMD; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 927 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 928 | case glslang::EbvDeviceIndex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 929 | builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 930 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 931 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 932 | |
| 933 | case glslang::EbvViewIndex: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 934 | builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 935 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 936 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 937 | |
Daniel Koch | 5154db5 | 2018-11-26 10:01:58 -0500 | [diff] [blame] | 938 | case glslang::EbvFragSizeEXT: |
| 939 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 940 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 941 | return spv::BuiltInFragSizeEXT; |
| 942 | |
| 943 | case glslang::EbvFragInvocationCountEXT: |
| 944 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 945 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 946 | return spv::BuiltInFragInvocationCountEXT; |
| 947 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 948 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 949 | if (!memberDeclaration) { |
| 950 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 951 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 952 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 953 | return spv::BuiltInViewportMaskNV; |
| 954 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 955 | if (!memberDeclaration) { |
| 956 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 957 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 958 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 959 | return spv::BuiltInSecondaryPositionNV; |
| 960 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 961 | if (!memberDeclaration) { |
| 962 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 963 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 964 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 965 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 966 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 967 | if (!memberDeclaration) { |
| 968 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 969 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 970 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 971 | return spv::BuiltInPositionPerViewNV; |
| 972 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 973 | if (!memberDeclaration) { |
| 974 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 975 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 976 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 977 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 978 | case glslang::EbvFragFullyCoveredNV: |
| 979 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 980 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 981 | return spv::BuiltInFullyCoveredEXT; |
Chao Chen | 5b2203d | 2018-09-19 11:43:21 -0700 | [diff] [blame] | 982 | case glslang::EbvFragmentSizeNV: |
| 983 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 984 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 985 | return spv::BuiltInFragmentSizeNV; |
| 986 | case glslang::EbvInvocationsPerPixelNV: |
| 987 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 988 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 989 | return spv::BuiltInInvocationsPerPixelNV; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 990 | |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 991 | // ray tracing |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 992 | case glslang::EbvLaunchId: |
| 993 | return spv::BuiltInLaunchIdKHR; |
| 994 | case glslang::EbvLaunchSize: |
| 995 | return spv::BuiltInLaunchSizeKHR; |
| 996 | case glslang::EbvWorldRayOrigin: |
| 997 | return spv::BuiltInWorldRayOriginKHR; |
| 998 | case glslang::EbvWorldRayDirection: |
| 999 | return spv::BuiltInWorldRayDirectionKHR; |
| 1000 | case glslang::EbvObjectRayOrigin: |
| 1001 | return spv::BuiltInObjectRayOriginKHR; |
| 1002 | case glslang::EbvObjectRayDirection: |
| 1003 | return spv::BuiltInObjectRayDirectionKHR; |
| 1004 | case glslang::EbvRayTmin: |
| 1005 | return spv::BuiltInRayTminKHR; |
| 1006 | case glslang::EbvRayTmax: |
| 1007 | return spv::BuiltInRayTmaxKHR; |
| 1008 | case glslang::EbvInstanceCustomIndex: |
| 1009 | return spv::BuiltInInstanceCustomIndexKHR; |
| 1010 | case glslang::EbvHitT: |
Daniel Koch | e11a2c8 | 2020-11-30 11:57:34 -0500 | [diff] [blame] | 1011 | { |
| 1012 | // this is a GLSL alias of RayTmax |
| 1013 | // in SPV_NV_ray_tracing it has a dedicated builtin |
| 1014 | // but in SPV_KHR_ray_tracing it gets mapped to RayTmax |
| 1015 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 1016 | if (extensions.find("GL_NV_ray_tracing") != extensions.end()) { |
| 1017 | return spv::BuiltInHitTNV; |
| 1018 | } else { |
| 1019 | return spv::BuiltInRayTmaxKHR; |
| 1020 | } |
| 1021 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1022 | case glslang::EbvHitKind: |
| 1023 | return spv::BuiltInHitKindKHR; |
| 1024 | case glslang::EbvObjectToWorld: |
| 1025 | case glslang::EbvObjectToWorld3x4: |
| 1026 | return spv::BuiltInObjectToWorldKHR; |
| 1027 | case glslang::EbvWorldToObject: |
| 1028 | case glslang::EbvWorldToObject3x4: |
| 1029 | return spv::BuiltInWorldToObjectKHR; |
| 1030 | case glslang::EbvIncomingRayFlags: |
| 1031 | return spv::BuiltInIncomingRayFlagsKHR; |
| 1032 | case glslang::EbvGeometryIndex: |
| 1033 | return spv::BuiltInRayGeometryIndexKHR; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1034 | |
| 1035 | // barycentrics |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 1036 | case glslang::EbvBaryCoordNV: |
| 1037 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 1038 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 1039 | return spv::BuiltInBaryCoordNV; |
| 1040 | case glslang::EbvBaryCoordNoPerspNV: |
| 1041 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 1042 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 1043 | return spv::BuiltInBaryCoordNoPerspNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1044 | |
| 1045 | // mesh shaders |
| 1046 | case glslang::EbvTaskCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1047 | return spv::BuiltInTaskCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1048 | case glslang::EbvPrimitiveCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1049 | return spv::BuiltInPrimitiveCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1050 | case glslang::EbvPrimitiveIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1051 | return spv::BuiltInPrimitiveIndicesNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1052 | case glslang::EbvClipDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1053 | return spv::BuiltInClipDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1054 | case glslang::EbvCullDistancePerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1055 | return spv::BuiltInCullDistancePerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1056 | case glslang::EbvLayerPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1057 | return spv::BuiltInLayerPerViewNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1058 | case glslang::EbvMeshViewCountNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1059 | return spv::BuiltInMeshViewCountNV; |
Daniel Koch | 593a4e0 | 2019-05-27 16:46:31 -0400 | [diff] [blame] | 1060 | case glslang::EbvMeshViewIndicesNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1061 | return spv::BuiltInMeshViewIndicesNV; |
Daniel Koch | 2cb2f19 | 2019-06-04 08:43:32 -0400 | [diff] [blame] | 1062 | |
| 1063 | // sm builtins |
| 1064 | case glslang::EbvWarpsPerSM: |
| 1065 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1066 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1067 | return spv::BuiltInWarpsPerSMNV; |
| 1068 | case glslang::EbvSMCount: |
| 1069 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1070 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1071 | return spv::BuiltInSMCountNV; |
| 1072 | case glslang::EbvWarpID: |
| 1073 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1074 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1075 | return spv::BuiltInWarpIDNV; |
| 1076 | case glslang::EbvSMID: |
| 1077 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1078 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1079 | return spv::BuiltInSMIDNV; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1080 | #endif |
| 1081 | |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 1082 | default: |
| 1083 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1087 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1088 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1089 | { |
| 1090 | assert(type.getBasicType() == glslang::EbtSampler); |
| 1091 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1092 | #ifdef GLSLANG_WEB |
| 1093 | return spv::ImageFormatUnknown; |
| 1094 | #endif |
| 1095 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1096 | // Check for capabilities |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1097 | switch (type.getQualifier().getFormat()) { |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1098 | case glslang::ElfRg32f: |
| 1099 | case glslang::ElfRg16f: |
| 1100 | case glslang::ElfR11fG11fB10f: |
| 1101 | case glslang::ElfR16f: |
| 1102 | case glslang::ElfRgba16: |
| 1103 | case glslang::ElfRgb10A2: |
| 1104 | case glslang::ElfRg16: |
| 1105 | case glslang::ElfRg8: |
| 1106 | case glslang::ElfR16: |
| 1107 | case glslang::ElfR8: |
| 1108 | case glslang::ElfRgba16Snorm: |
| 1109 | case glslang::ElfRg16Snorm: |
| 1110 | case glslang::ElfRg8Snorm: |
| 1111 | case glslang::ElfR16Snorm: |
| 1112 | case glslang::ElfR8Snorm: |
| 1113 | |
| 1114 | case glslang::ElfRg32i: |
| 1115 | case glslang::ElfRg16i: |
| 1116 | case glslang::ElfRg8i: |
| 1117 | case glslang::ElfR16i: |
| 1118 | case glslang::ElfR8i: |
| 1119 | |
| 1120 | case glslang::ElfRgb10a2ui: |
| 1121 | case glslang::ElfRg32ui: |
| 1122 | case glslang::ElfRg16ui: |
| 1123 | case glslang::ElfRg8ui: |
| 1124 | case glslang::ElfR16ui: |
| 1125 | case glslang::ElfR8ui: |
| 1126 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 1127 | break; |
| 1128 | |
Tobski | 8c1a3a0 | 2020-11-04 16:24:23 +0000 | [diff] [blame] | 1129 | case glslang::ElfR64ui: |
| 1130 | case glslang::ElfR64i: |
| 1131 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 1132 | builder.addCapability(spv::CapabilityInt64ImageEXT); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 1133 | default: |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | // do the translation |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1138 | switch (type.getQualifier().getFormat()) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1139 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1140 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1141 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1142 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1143 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1144 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1145 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1146 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1147 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1148 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1149 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1150 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1151 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1152 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1153 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1154 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1155 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1156 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1157 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1158 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1159 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1160 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1161 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1162 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1163 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1164 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1165 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1166 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1167 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1168 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1169 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1170 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1171 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1172 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1173 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1174 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1175 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1176 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1177 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1178 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
Tobski | 8c1a3a0 | 2020-11-04 16:24:23 +0000 | [diff] [blame] | 1179 | case glslang::ElfR64ui: return spv::ImageFormatR64ui; |
| 1180 | case glslang::ElfR64i: return spv::ImageFormatR64i; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1181 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1185 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl( |
| 1186 | const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1187 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1188 | if (selectionNode.getFlatten()) |
| 1189 | return spv::SelectionControlFlattenMask; |
| 1190 | if (selectionNode.getDontFlatten()) |
| 1191 | return spv::SelectionControlDontFlattenMask; |
| 1192 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1193 | } |
| 1194 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1195 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) |
| 1196 | const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1197 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1198 | if (switchNode.getFlatten()) |
| 1199 | return spv::SelectionControlFlattenMask; |
| 1200 | if (switchNode.getDontFlatten()) |
| 1201 | return spv::SelectionControlDontFlattenMask; |
| 1202 | return spv::SelectionControlMaskNone; |
| 1203 | } |
| 1204 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1205 | // return a non-0 dependency if the dependency argument must be set |
| 1206 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1207 | std::vector<unsigned int>& operands) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1208 | { |
| 1209 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1210 | |
| 1211 | if (loopNode.getDontUnroll()) |
| 1212 | control = control | spv::LoopControlDontUnrollMask; |
| 1213 | if (loopNode.getUnroll()) |
| 1214 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 1215 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1216 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1217 | else if (loopNode.getLoopDependency() > 0) { |
| 1218 | control = control | spv::LoopControlDependencyLengthMask; |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 1219 | operands.push_back((unsigned int)loopNode.getLoopDependency()); |
| 1220 | } |
| 1221 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 1222 | if (loopNode.getMinIterations() > 0) { |
| 1223 | control = control | spv::LoopControlMinIterationsMask; |
| 1224 | operands.push_back(loopNode.getMinIterations()); |
| 1225 | } |
| 1226 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
| 1227 | control = control | spv::LoopControlMaxIterationsMask; |
| 1228 | operands.push_back(loopNode.getMaxIterations()); |
| 1229 | } |
| 1230 | if (loopNode.getIterationMultiple() > 1) { |
| 1231 | control = control | spv::LoopControlIterationMultipleMask; |
| 1232 | operands.push_back(loopNode.getIterationMultiple()); |
| 1233 | } |
| 1234 | if (loopNode.getPeelCount() > 0) { |
| 1235 | control = control | spv::LoopControlPeelCountMask; |
| 1236 | operands.push_back(loopNode.getPeelCount()); |
| 1237 | } |
| 1238 | if (loopNode.getPartialCount() > 0) { |
| 1239 | control = control | spv::LoopControlPartialCountMask; |
| 1240 | operands.push_back(loopNode.getPartialCount()); |
| 1241 | } |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 1242 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 1243 | |
| 1244 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1245 | } |
| 1246 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1247 | // Translate glslang type to SPIR-V storage class. |
| 1248 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1249 | { |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 1250 | if (type.getBasicType() == glslang::EbtRayQuery) |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 1251 | return spv::StorageClassPrivate; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1252 | if (type.getQualifier().isPipeInput()) |
| 1253 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1254 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1255 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1256 | |
| 1257 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1258 | type.getQualifier().storage == glslang::EvqUniform) { |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1259 | if (type.isAtomic()) |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1260 | return spv::StorageClassAtomicCounter; |
| 1261 | if (type.containsOpaque()) |
| 1262 | return spv::StorageClassUniformConstant; |
| 1263 | } |
| 1264 | |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1265 | if (type.getQualifier().isUniformOrBuffer() && |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1266 | type.getQualifier().isShaderRecord()) { |
| 1267 | return spv::StorageClassShaderRecordBufferKHR; |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1268 | } |
Jeff Bolz | 61a0cd1 | 2018-12-14 20:59:53 -0600 | [diff] [blame] | 1269 | |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1270 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1271 | 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] | 1272 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1276 | if (type.getQualifier().isPushConstant()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1277 | return spv::StorageClassPushConstant; |
| 1278 | if (type.getBasicType() == glslang::EbtBlock) |
| 1279 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1280 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1281 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1282 | |
Caio Marcelo de Oliveira Filho | 4bfbf62 | 2020-06-02 16:58:51 -0700 | [diff] [blame] | 1283 | if (type.getQualifier().storage == glslang::EvqShared && type.getBasicType() == glslang::EbtBlock) { |
| 1284 | builder.addExtension(spv::E_SPV_KHR_workgroup_memory_explicit_layout); |
| 1285 | builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayoutKHR); |
| 1286 | return spv::StorageClassWorkgroup; |
| 1287 | } |
| 1288 | |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1289 | switch (type.getQualifier().storage) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 1290 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1291 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1292 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
John Kessenich | deec193 | 2019-08-13 08:00:30 -0600 | [diff] [blame] | 1293 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 1294 | #ifndef GLSLANG_WEB |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1295 | case glslang::EvqPayload: return spv::StorageClassRayPayloadKHR; |
| 1296 | case glslang::EvqPayloadIn: return spv::StorageClassIncomingRayPayloadKHR; |
| 1297 | case glslang::EvqHitAttr: return spv::StorageClassHitAttributeKHR; |
| 1298 | case glslang::EvqCallableData: return spv::StorageClassCallableDataKHR; |
| 1299 | case glslang::EvqCallableDataIn: return spv::StorageClassIncomingCallableDataKHR; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1300 | #endif |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 1301 | default: |
| 1302 | assert(0); |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 1307 | } |
| 1308 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1309 | // Add capabilities pertaining to how an array is indexed. |
| 1310 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1311 | const glslang::TType& indexType) |
| 1312 | { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1313 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1314 | if (indexType.getQualifier().isNonUniform()) { |
| 1315 | // deal with an asserted non-uniform index |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1316 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1317 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1318 | if (baseType.getQualifier().hasAttachment()) |
| 1319 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1320 | else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1321 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1322 | else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1323 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1324 | else if (baseType.isImage()) |
| 1325 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1326 | else if (baseType.isTexture()) |
| 1327 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1328 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1329 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1330 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1331 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1332 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1333 | } |
| 1334 | } else { |
| 1335 | // assume a dynamically uniform index |
| 1336 | if (baseType.getBasicType() == glslang::EbtSampler) { |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1337 | if (baseType.getQualifier().hasAttachment()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1338 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1339 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1340 | } else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1341 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1342 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 1343 | } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1344 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1345 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
Jeff Bolz | c140b96 | 2018-07-12 16:51:18 -0500 | [diff] [blame] | 1346 | } |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1347 | } |
| 1348 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1349 | #endif |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1350 | } |
| 1351 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1352 | // 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] | 1353 | // descriptor set. |
| 1354 | bool IsDescriptorResource(const glslang::TType& type) |
| 1355 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 1356 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1357 | if (type.getBasicType() == glslang::EbtBlock) |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1358 | return type.getQualifier().isUniformOrBuffer() && |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1359 | ! type.getQualifier().isShaderRecord() && |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1360 | ! type.getQualifier().isPushConstant(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1361 | |
| 1362 | // non block... |
| 1363 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1364 | // if they are the global-scope-class, not the function parameter |
| 1365 | // (or local, if they ever exist) class. |
alelenv | 999d4fd | 2020-06-01 23:24:41 -0700 | [diff] [blame] | 1366 | if (type.getBasicType() == glslang::EbtSampler || |
| 1367 | type.getBasicType() == glslang::EbtAccStruct) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1368 | return type.getQualifier().isUniformOrBuffer(); |
| 1369 | |
| 1370 | // None of the above. |
| 1371 | return false; |
| 1372 | } |
| 1373 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1374 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1375 | { |
| 1376 | if (child.layoutMatrix == glslang::ElmNone) |
| 1377 | child.layoutMatrix = parent.layoutMatrix; |
| 1378 | |
| 1379 | if (parent.invariant) |
| 1380 | child.invariant = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1381 | if (parent.flat) |
| 1382 | child.flat = true; |
| 1383 | if (parent.centroid) |
| 1384 | child.centroid = true; |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1385 | #ifndef GLSLANG_WEB |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1386 | if (parent.nopersp) |
| 1387 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1388 | if (parent.explicitInterp) |
| 1389 | child.explicitInterp = true; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1390 | if (parent.perPrimitiveNV) |
| 1391 | child.perPrimitiveNV = true; |
| 1392 | if (parent.perViewNV) |
| 1393 | child.perViewNV = true; |
| 1394 | if (parent.perTaskNV) |
| 1395 | child.perTaskNV = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1396 | if (parent.patch) |
| 1397 | child.patch = true; |
| 1398 | if (parent.sample) |
| 1399 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1400 | if (parent.coherent) |
| 1401 | child.coherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1402 | if (parent.devicecoherent) |
| 1403 | child.devicecoherent = true; |
| 1404 | if (parent.queuefamilycoherent) |
| 1405 | child.queuefamilycoherent = true; |
| 1406 | if (parent.workgroupcoherent) |
| 1407 | child.workgroupcoherent = true; |
| 1408 | if (parent.subgroupcoherent) |
| 1409 | child.subgroupcoherent = true; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1410 | if (parent.shadercallcoherent) |
| 1411 | child.shadercallcoherent = true; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1412 | if (parent.nonprivate) |
| 1413 | child.nonprivate = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1414 | if (parent.volatil) |
| 1415 | child.volatil = true; |
| 1416 | if (parent.restrict) |
| 1417 | child.restrict = true; |
| 1418 | if (parent.readonly) |
| 1419 | child.readonly = true; |
| 1420 | if (parent.writeonly) |
| 1421 | child.writeonly = true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1422 | #endif |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 1423 | if (parent.nonUniform) |
| 1424 | child.nonUniform = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1427 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1428 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1429 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 1430 | // - struct members might inherit from a struct declaration |
| 1431 | // (note that non-block structs don't explicitly inherit, |
| 1432 | // only implicitly, meaning no decoration involved) |
| 1433 | // - affect decorations on the struct members |
| 1434 | // (note smooth does not, and expecting something like volatile |
| 1435 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1436 | // - 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] | 1437 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1440 | // |
| 1441 | // Implement the TGlslangToSpvTraverser class. |
| 1442 | // |
| 1443 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1444 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, |
| 1445 | const glslang::TIntermediate* glslangIntermediate, |
| 1446 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) : |
| 1447 | TIntermTraverser(true, false, true), |
| 1448 | options(options), |
| 1449 | shaderEntry(nullptr), currentFunction(nullptr), |
| 1450 | sequenceDepth(0), logger(buildLogger), |
| 1451 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
| 1452 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
| 1453 | glslangIntermediate(glslangIntermediate), |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 1454 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()), |
| 1455 | nonSemanticDebugPrintf(0) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1456 | { |
| 1457 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1458 | |
| 1459 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1460 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1461 | glslangIntermediate->getVersion()); |
| 1462 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1463 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1464 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1465 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1466 | |
| 1467 | // Set the source shader's text. If for SPV version 1.0, include |
| 1468 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1469 | // Otherwise, emit those as actual instructions. |
| 1470 | std::string text; |
| 1471 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1472 | for (int p = 0; p < (int)processes.size(); ++p) { |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1473 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1474 | text.append("// OpModuleProcessed "); |
| 1475 | text.append(processes[p]); |
| 1476 | text.append("\n"); |
| 1477 | } else |
| 1478 | builder.addModuleProcessed(processes[p]); |
| 1479 | } |
John Kessenich | 8717a5d | 2018-10-26 10:12:32 -0600 | [diff] [blame] | 1480 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 1481 | text.append("#line 1\n"); |
| 1482 | text.append(glslangIntermediate->getSourceText()); |
| 1483 | builder.setSourceText(text); |
Greg Fischer | d445bb2 | 2018-12-06 11:13:15 -0700 | [diff] [blame] | 1484 | // Pass name and text for all included files |
| 1485 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1486 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1487 | builder.addInclude(iItr->first, iItr->second); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 1488 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1489 | stdBuiltins = builder.import("GLSL.std.450"); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1490 | |
| 1491 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1492 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1493 | |
| 1494 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1495 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
John Kessenich | 1ff0c18 | 2019-10-10 12:01:13 -0600 | [diff] [blame] | 1496 | 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] | 1497 | builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1498 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1499 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1500 | memoryModel = spv::MemoryModelVulkanKHR; |
| 1501 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 1502 | 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] | 1503 | } |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1504 | builder.setMemoryModel(addressingModel, memoryModel); |
| 1505 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 1506 | if (glslangIntermediate->usingVariablePointers()) { |
| 1507 | builder.addCapability(spv::CapabilityVariablePointers); |
| 1508 | } |
| 1509 | |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 1510 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1511 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1512 | |
| 1513 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 1514 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1515 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
johnkslang | 0138472 | 2020-08-14 08:40:06 -0600 | [diff] [blame] | 1516 | builder.addSourceExtension(it->c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1517 | |
| 1518 | // Add the top-level modes for this shader. |
| 1519 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1520 | if (glslangIntermediate->getXfbMode()) { |
| 1521 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1522 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1523 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1524 | |
alelenv | 59216d5 | 2020-05-21 04:38:41 -0700 | [diff] [blame] | 1525 | if (glslangIntermediate->getLayoutPrimitiveCulling()) { |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 1526 | builder.addCapability(spv::CapabilityRayTraversalPrimitiveCullingKHR); |
alelenv | 75de196 | 2020-04-08 21:09:20 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1529 | unsigned int mode; |
| 1530 | switch (glslangIntermediate->getStage()) { |
| 1531 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1532 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1533 | break; |
| 1534 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1535 | case EShLangFragment: |
| 1536 | builder.addCapability(spv::CapabilityShader); |
| 1537 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1538 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
| 1539 | |
| 1540 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1541 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
| 1542 | else |
| 1543 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
| 1544 | |
| 1545 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1546 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1547 | |
| 1548 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1549 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1550 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1551 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1552 | } |
| 1553 | |
Greg Fischer | b479ce0 | 2021-03-01 17:45:03 -0700 | [diff] [blame] | 1554 | if (glslangIntermediate->isDepthReplacing()) |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1555 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
| 1556 | |
| 1557 | #ifndef GLSLANG_WEB |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 1558 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1559 | switch(glslangIntermediate->getDepth()) { |
Greg Fischer | b479ce0 | 2021-03-01 17:45:03 -0700 | [diff] [blame] | 1560 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1561 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
| 1562 | case glslang::EldUnchanged: mode = spv::ExecutionModeDepthUnchanged; break; |
| 1563 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1564 | } |
| 1565 | if (mode != spv::ExecutionModeMax) |
| 1566 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1567 | switch (glslangIntermediate->getInterlockOrdering()) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 1568 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; |
| 1569 | break; |
| 1570 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; |
| 1571 | break; |
| 1572 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; |
| 1573 | break; |
| 1574 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; |
| 1575 | break; |
| 1576 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; |
| 1577 | break; |
| 1578 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; |
| 1579 | break; |
| 1580 | default: mode = spv::ExecutionModeMax; |
| 1581 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1582 | } |
| 1583 | if (mode != spv::ExecutionModeMax) { |
| 1584 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1585 | if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
| 1586 | mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
| 1587 | builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
| 1588 | } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
| 1589 | mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
| 1590 | builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT); |
| 1591 | } else { |
| 1592 | builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT); |
| 1593 | } |
| 1594 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 1595 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1596 | #endif |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 1597 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1598 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 1599 | case EShLangCompute: |
| 1600 | builder.addCapability(spv::CapabilityShader); |
| 1601 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1602 | glslangIntermediate->getLocalSize(1), |
| 1603 | glslangIntermediate->getLocalSize(2)); |
| 1604 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1605 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1606 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1607 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1608 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1609 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1610 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1611 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1612 | } |
| 1613 | break; |
John Kessenich | 51ed01c | 2019-10-10 11:40:11 -0600 | [diff] [blame] | 1614 | #ifndef GLSLANG_WEB |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1615 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1616 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1617 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1618 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1619 | glslang::TLayoutGeometry primitive; |
| 1620 | |
| 1621 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1622 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1623 | glslangIntermediate->getVertices()); |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1624 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1625 | } else { |
| 1626 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1627 | } |
| 1628 | |
| 1629 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1630 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1631 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1632 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1633 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1634 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1635 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1636 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1637 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1638 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1639 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1640 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1641 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1642 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1643 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1644 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1645 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1646 | |
| 1647 | switch (glslangIntermediate->getVertexOrder()) { |
| 1648 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1649 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1650 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1651 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1652 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1653 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1654 | |
| 1655 | if (glslangIntermediate->getPointMode()) |
| 1656 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1657 | break; |
| 1658 | |
| 1659 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1660 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1661 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1662 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1663 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1664 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1665 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1666 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1667 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1668 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1669 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1670 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1671 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1672 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1673 | |
| 1674 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1675 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1676 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1677 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1678 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1679 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1680 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1681 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1682 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1683 | break; |
| 1684 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1685 | case EShLangRayGen: |
| 1686 | case EShLangIntersect: |
| 1687 | case EShLangAnyHit: |
| 1688 | case EShLangClosestHit: |
| 1689 | case EShLangMiss: |
| 1690 | case EShLangCallable: |
| 1691 | { |
| 1692 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 1693 | if (extensions.find("GL_NV_ray_tracing") == extensions.end()) { |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 1694 | builder.addCapability(spv::CapabilityRayTracingKHR); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1695 | builder.addExtension("SPV_KHR_ray_tracing"); |
| 1696 | } |
| 1697 | else { |
| 1698 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1699 | builder.addExtension("SPV_NV_ray_tracing"); |
| 1700 | } |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 1701 | break; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1702 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1703 | case EShLangTaskNV: |
| 1704 | case EShLangMeshNV: |
| 1705 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1706 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1707 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1708 | glslangIntermediate->getLocalSize(1), |
| 1709 | glslangIntermediate->getLocalSize(2)); |
| 1710 | if (glslangIntermediate->getStage() == EShLangMeshNV) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1711 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1712 | glslangIntermediate->getVertices()); |
| 1713 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, |
| 1714 | glslangIntermediate->getPrimitives()); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 1715 | |
| 1716 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1717 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1718 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1719 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1720 | default: mode = spv::ExecutionModeMax; break; |
| 1721 | } |
| 1722 | if (mode != spv::ExecutionModeMax) |
| 1723 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1724 | } |
| 1725 | break; |
| 1726 | #endif |
| 1727 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1728 | default: |
| 1729 | break; |
| 1730 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1731 | } |
| 1732 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1733 | // Finish creating SPV, after the traversal is complete. |
| 1734 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1735 | { |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1736 | // Finish the entry point function |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1737 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1738 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1739 | builder.leaveFunction(); |
| 1740 | } |
| 1741 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1742 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1743 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1744 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1745 | |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 1746 | // Add capabilities, extensions, remove unneeded decorations, etc., |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1747 | // based on the resulting SPIR-V. |
David Neto | 8c3d5b4 | 2019-10-21 14:50:31 -0400 | [diff] [blame] | 1748 | // Note: WebGPU code generation must have the opportunity to aggressively |
| 1749 | // prune unreachable merge blocks and continue targets. |
John Kessenich | f04c51b | 2018-08-03 15:56:12 -0600 | [diff] [blame] | 1750 | builder.postProcess(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1753 | // Write the SPV into 'out'. |
| 1754 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1755 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1756 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | // |
| 1760 | // Implement the traversal functions. |
| 1761 | // |
| 1762 | // Return true from interior nodes to have the external traversal |
| 1763 | // continue on to children. Return false if children were |
| 1764 | // already processed. |
| 1765 | // |
| 1766 | |
| 1767 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1768 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1769 | // - uniform/input reads |
| 1770 | // - output writes |
| 1771 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1772 | // - something simple that degenerates into the last bullet |
| 1773 | // |
| 1774 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1775 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1776 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1777 | if (symbol->getType().isStruct()) |
| 1778 | glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId(); |
| 1779 | |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1780 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1781 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1782 | |
Rex Xu | f6e0fe8 | 2020-10-23 22:54:35 +0800 | [diff] [blame] | 1783 | #ifdef ENABLE_HLSL |
| 1784 | // Skip symbol handling if it is string-typed |
| 1785 | if (symbol->getBasicType() == glslang::EbtString) |
| 1786 | return; |
| 1787 | #endif |
| 1788 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1789 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1790 | // Formal function parameters were mapped during makeFunctions(). |
| 1791 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1792 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1793 | if (builder.isPointer(id)) { |
John Kessenich | c30d335 | 2020-06-10 07:15:24 -0600 | [diff] [blame] | 1794 | if (!symbol->getType().getQualifier().isParamInput() && |
| 1795 | !symbol->getType().getQualifier().isParamOutput()) { |
| 1796 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1797 | // Consider adding to the OpEntryPoint interface list. |
| 1798 | // Only looking at structures if they have at least one member. |
| 1799 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
| 1800 | spv::StorageClass sc = builder.getStorageClass(id); |
| 1801 | // Before SPIR-V 1.4, we only want to include Input and Output. |
| 1802 | // Starting with SPIR-V 1.4, we want all globals. |
John Kessenich | 4e13c90 | 2020-07-13 00:35:58 -0600 | [diff] [blame] | 1803 | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(id)) || |
John Kessenich | c30d335 | 2020-06-10 07:15:24 -0600 | [diff] [blame] | 1804 | (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
| 1805 | iOSet.insert(id); |
| 1806 | } |
John Kessenich | 7c7731e | 2019-01-04 16:47:06 +0700 | [diff] [blame] | 1807 | } |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1808 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1809 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 1810 | // If the SPIR-V type is required to be different than the AST type |
| 1811 | // (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices), |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1812 | // translate now from the SPIR-V type to the AST type, for the consuming |
| 1813 | // operation. |
| 1814 | // Note this turns it from an l-value to an r-value. |
| 1815 | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
| 1816 | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
| 1817 | id = translateForcedType(id); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1821 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1822 | // Prepare to generate code for the access |
| 1823 | |
| 1824 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1825 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1826 | // followed by setting the base. |
| 1827 | builder.clearAccessChain(); |
| 1828 | |
| 1829 | // 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] | 1830 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1831 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1832 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1833 | // 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] | 1834 | // These are also pure R-values. |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1835 | // C) R-Values from type translation, see above call to translateForcedType() |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1836 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 1837 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
| 1838 | !builder.isPointerType(builder.getTypeId(id))) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1839 | builder.setAccessChainRValue(id); |
| 1840 | else |
| 1841 | builder.setAccessChainLValue(id); |
| 1842 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1843 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 1844 | #ifdef ENABLE_HLSL |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1845 | // Process linkage-only nodes for any special additional interface work. |
| 1846 | if (linkageOnly) { |
| 1847 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 1848 | // Map implicit counter buffers to their originating buffers, which should have been |
| 1849 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 1850 | // of declaration. |
| 1851 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1852 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1853 | // Save possible originating buffers for counter buffers, keyed by |
| 1854 | // making the potential counter-buffer name. |
| 1855 | std::string keyName = symbol->getName().c_str(); |
| 1856 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1857 | counterOriginator[keyName] = symbol; |
| 1858 | } else { |
| 1859 | // Handle a counter buffer, by finding the saved originating buffer. |
| 1860 | std::string keyName = symbol->getName().c_str(); |
| 1861 | auto it = counterOriginator.find(keyName); |
| 1862 | if (it != counterOriginator.end()) { |
| 1863 | id = getSymbolId(it->second); |
| 1864 | if (id != spv::NoResult) { |
| 1865 | spv::Id counterId = getSymbolId(symbol); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1866 | if (counterId != spv::NoResult) { |
| 1867 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1868 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
John Kessenich | f52b638 | 2018-04-05 19:35:38 -0600 | [diff] [blame] | 1869 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 1876 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1880 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 1881 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1882 | if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) { |
| 1883 | glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId(); |
| 1884 | } |
| 1885 | if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) { |
| 1886 | glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId(); |
| 1887 | } |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1888 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1889 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1890 | if (node->getType().getQualifier().isSpecConstant()) |
| 1891 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1892 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1893 | // First, handle special cases |
| 1894 | switch (node->getOp()) { |
| 1895 | case glslang::EOpAssign: |
| 1896 | case glslang::EOpAddAssign: |
| 1897 | case glslang::EOpSubAssign: |
| 1898 | case glslang::EOpMulAssign: |
| 1899 | case glslang::EOpVectorTimesMatrixAssign: |
| 1900 | case glslang::EOpVectorTimesScalarAssign: |
| 1901 | case glslang::EOpMatrixTimesScalarAssign: |
| 1902 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1903 | case glslang::EOpDivAssign: |
| 1904 | case glslang::EOpModAssign: |
| 1905 | case glslang::EOpAndAssign: |
| 1906 | case glslang::EOpInclusiveOrAssign: |
| 1907 | case glslang::EOpExclusiveOrAssign: |
| 1908 | case glslang::EOpLeftShiftAssign: |
| 1909 | case glslang::EOpRightShiftAssign: |
| 1910 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1911 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1912 | // says to evaluate the left before the right. So, always, left |
| 1913 | // node then right node. |
| 1914 | { |
| 1915 | // get the left l-value, save it away |
| 1916 | builder.clearAccessChain(); |
| 1917 | node->getLeft()->traverse(this); |
| 1918 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1919 | |
| 1920 | // evaluate the right |
| 1921 | builder.clearAccessChain(); |
| 1922 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1923 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1924 | |
| 1925 | if (node->getOp() != glslang::EOpAssign) { |
| 1926 | // the left is also an r-value |
| 1927 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1928 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1929 | |
| 1930 | // do the operation |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 1931 | spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(node->getLeft()->getType()); |
| 1932 | coherentFlags |= TranslateCoherent(node->getRight()->getType()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1933 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 1934 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 1935 | TranslateNonUniformDecoration(coherentFlags) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 1936 | rValue = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1937 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1938 | node->getType().getBasicType()); |
| 1939 | |
| 1940 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1941 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | // store the result |
| 1945 | builder.setAccessChain(lValue); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 1946 | multiTypeStore(node->getLeft()->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1947 | |
| 1948 | // assignments are expressions having an rValue after they are evaluated... |
| 1949 | builder.clearAccessChain(); |
| 1950 | builder.setAccessChainRValue(rValue); |
| 1951 | } |
| 1952 | return false; |
| 1953 | case glslang::EOpIndexDirect: |
| 1954 | case glslang::EOpIndexDirectStruct: |
| 1955 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 1956 | // Structure, array, matrix, or vector indirection with statically known index. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1957 | // Get the left part of the access chain. |
| 1958 | node->getLeft()->traverse(this); |
| 1959 | |
| 1960 | // Add the next element in the chain |
| 1961 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1962 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1963 | if (! node->getLeft()->getType().isArray() && |
| 1964 | node->getLeft()->getType().isVector() && |
| 1965 | node->getOp() == glslang::EOpIndexDirect) { |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 1966 | // Swizzle is uniform so propagate uniform into access chain |
| 1967 | spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(node->getLeft()->getType()); |
| 1968 | coherentFlags.nonUniform = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1969 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1970 | // so short circuit the access-chain stuff with a swizzle. |
| 1971 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1972 | swizzle.push_back(glslangIndex); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1973 | int dummySize; |
| 1974 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 1975 | coherentFlags, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 1976 | glslangIntermediate->getBaseAlignmentScalar( |
| 1977 | node->getLeft()->getType(), dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1978 | } else { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1979 | |
| 1980 | // Load through a block reference is performed with a dot operator that |
| 1981 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 1982 | // do a load and reset the access chain. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 1983 | if (node->getLeft()->isReference() && |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 1984 | !node->getLeft()->getType().isArray() && |
| 1985 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1986 | { |
| 1987 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 1988 | builder.clearAccessChain(); |
| 1989 | builder.setAccessChainLValue(left); |
| 1990 | } |
| 1991 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1992 | int spvIndex = glslangIndex; |
| 1993 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1994 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1995 | { |
| 1996 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1997 | // index remapping due to hidden members in anonymous blocks. |
Chow | 93b400f | 2020-11-12 15:54:16 +0800 | [diff] [blame] | 1998 | long long glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()]; |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 1999 | if (memberRemapper.find(glslangId) != memberRemapper.end()) { |
| 2000 | std::vector<int>& remapper = memberRemapper[glslangId]; |
| 2001 | assert(remapper.size() > 0); |
| 2002 | spvIndex = remapper[glslangIndex]; |
| 2003 | } |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2004 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2005 | |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 2006 | // Struct reference propagates uniform lvalue |
| 2007 | spv::Builder::AccessChain::CoherentFlags coherentFlags = |
| 2008 | TranslateCoherent(node->getLeft()->getType()); |
| 2009 | coherentFlags.nonUniform = 0; |
| 2010 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2011 | // normal case for indexing array or structure or block |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2012 | builder.accessChainPush(builder.makeIntConstant(spvIndex), |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 2013 | coherentFlags, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2014 | node->getLeft()->getType().getBufferReferenceAlignment()); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2015 | |
| 2016 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 2017 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2018 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2019 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | return false; |
| 2023 | case glslang::EOpIndexIndirect: |
| 2024 | { |
John Kessenich | 61a5ce1 | 2019-02-07 08:04:12 -0700 | [diff] [blame] | 2025 | // Array, matrix, or vector indirection with variable index. |
| 2026 | // Will use native SPIR-V access-chain for and array indirection; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2027 | // matrices are arrays of vectors, so will also work for a matrix. |
| 2028 | // Will use the access chain's 'component' for variable index into a vector. |
| 2029 | |
| 2030 | // This adapter is building access chains left to right. |
| 2031 | // Set up the access chain to the left. |
| 2032 | node->getLeft()->traverse(this); |
| 2033 | |
| 2034 | // save it so that computing the right side doesn't trash it |
| 2035 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 2036 | |
| 2037 | // compute the next index in the chain |
| 2038 | builder.clearAccessChain(); |
| 2039 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2040 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2041 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2042 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 2043 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2044 | // restore the saved access chain |
| 2045 | builder.setAccessChain(partial); |
| 2046 | |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 2047 | // Only if index is nonUniform should we propagate nonUniform into access chain |
| 2048 | spv::Builder::AccessChain::CoherentFlags index_flags = TranslateCoherent(node->getRight()->getType()); |
| 2049 | spv::Builder::AccessChain::CoherentFlags coherent_flags = TranslateCoherent(node->getLeft()->getType()); |
| 2050 | coherent_flags.nonUniform = index_flags.nonUniform; |
| 2051 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2052 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 2053 | int dummySize; |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 2054 | builder.accessChainPushComponent( |
| 2055 | index, convertGlslangToSpvType(node->getLeft()->getType()), coherent_flags, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2056 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 2057 | dummySize)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2058 | } else |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 2059 | builder.accessChainPush(index, coherent_flags, |
| 2060 | node->getLeft()->getType().getBufferReferenceAlignment()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2061 | } |
| 2062 | return false; |
| 2063 | case glslang::EOpVectorSwizzle: |
| 2064 | { |
| 2065 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2066 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2067 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2068 | int dummySize; |
| 2069 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 2070 | TranslateCoherent(node->getLeft()->getType()), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2071 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 2072 | dummySize)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2073 | } |
| 2074 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 2075 | case glslang::EOpMatrixSwizzle: |
| 2076 | logger->missingFunctionality("matrix swizzle"); |
| 2077 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 2078 | case glslang::EOpLogicalOr: |
| 2079 | case glslang::EOpLogicalAnd: |
| 2080 | { |
| 2081 | |
| 2082 | // These may require short circuiting, but can sometimes be done as straight |
| 2083 | // binary operations. The right operand must be short circuited if it has |
| 2084 | // side effects, and should probably be if it is complex. |
| 2085 | if (isTrivial(node->getRight()->getAsTyped())) |
| 2086 | break; // handle below as a normal binary operation |
| 2087 | // otherwise, we need to do dynamic short circuiting on the right operand |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2088 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), |
| 2089 | *node->getRight()->getAsTyped()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 2090 | builder.clearAccessChain(); |
| 2091 | builder.setAccessChainRValue(result); |
| 2092 | } |
| 2093 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2094 | default: |
| 2095 | break; |
| 2096 | } |
| 2097 | |
| 2098 | // Assume generic binary op... |
| 2099 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2100 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2101 | builder.clearAccessChain(); |
| 2102 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2103 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2104 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2105 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2106 | builder.clearAccessChain(); |
| 2107 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2108 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2109 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2110 | // get result |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2111 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2112 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2113 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2114 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2115 | convertGlslangToSpvType(node->getType()), left, right, |
| 2116 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2117 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2118 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2119 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2120 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2121 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2122 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2123 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2124 | return false; |
| 2125 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2126 | } |
| 2127 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2128 | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
| 2129 | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
| 2130 | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2131 | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn, |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2132 | const glslang::TType& glslangType) |
| 2133 | { |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2134 | switch(glslangBuiltIn) |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2135 | { |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2136 | case glslang::EbvSubGroupEqMask: |
| 2137 | case glslang::EbvSubGroupGeMask: |
| 2138 | case glslang::EbvSubGroupGtMask: |
| 2139 | case glslang::EbvSubGroupLeMask: |
| 2140 | case glslang::EbvSubGroupLtMask: { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2141 | // these require changing a 64-bit scaler -> a vector of 32-bit components |
| 2142 | if (glslangType.isVector()) |
| 2143 | break; |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 2144 | spv::Id ivec4_type = builder.makeVectorType(builder.makeUintType(32), 4); |
| 2145 | spv::Id uint64_type = builder.makeUintType(64); |
| 2146 | std::pair<spv::Id, spv::Id> ret(ivec4_type, uint64_type); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2147 | return ret; |
| 2148 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2149 | // There are no SPIR-V builtins defined for these and map onto original non-transposed |
| 2150 | // builtins. During visitBinary we insert a transpose |
| 2151 | case glslang::EbvWorldToObject3x4: |
| 2152 | case glslang::EbvObjectToWorld3x4: { |
John Kessenich | f80ef74 | 2020-07-14 01:44:35 -0600 | [diff] [blame] | 2153 | spv::Id mat43 = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 2154 | spv::Id mat34 = builder.makeMatrixType(builder.makeFloatType(32), 3, 4); |
| 2155 | std::pair<spv::Id, spv::Id> ret(mat43, mat34); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2156 | return ret; |
| 2157 | } |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2158 | default: |
| 2159 | break; |
| 2160 | } |
| 2161 | |
| 2162 | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
| 2163 | return ret; |
| 2164 | } |
| 2165 | |
| 2166 | // For an object previously identified (see getForcedType() and forceType) |
| 2167 | // as needing type translations, do the translation needed for a load, turning |
| 2168 | // an L-value into in R-value. |
| 2169 | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
| 2170 | { |
| 2171 | const auto forceIt = forceType.find(object); |
| 2172 | if (forceIt == forceType.end()) |
| 2173 | return object; |
| 2174 | |
| 2175 | spv::Id desiredTypeId = forceIt->second; |
| 2176 | spv::Id objectTypeId = builder.getTypeId(object); |
| 2177 | assert(builder.isPointerType(objectTypeId)); |
| 2178 | objectTypeId = builder.getContainedTypeId(objectTypeId); |
| 2179 | if (builder.isVectorType(objectTypeId) && |
| 2180 | builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
| 2181 | if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
| 2182 | // handle 32-bit v.xy* -> 64-bit |
| 2183 | builder.clearAccessChain(); |
| 2184 | builder.setAccessChainLValue(object); |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 2185 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, spv::DecorationMax, objectTypeId); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2186 | std::vector<spv::Id> components; |
| 2187 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
| 2188 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
| 2189 | |
| 2190 | spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
| 2191 | return builder.createUnaryOp(spv::OpBitcast, desiredTypeId, |
| 2192 | builder.createCompositeConstruct(vecType, components)); |
| 2193 | } else { |
| 2194 | logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar"); |
| 2195 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2196 | } else if (builder.isMatrixType(objectTypeId)) { |
| 2197 | // There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject |
| 2198 | // and we insert a transpose after loading the original non-transposed builtins |
| 2199 | builder.clearAccessChain(); |
| 2200 | builder.setAccessChainLValue(object); |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 2201 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, spv::DecorationMax, objectTypeId); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 2202 | return builder.createUnaryOp(spv::OpTranspose, desiredTypeId, object); |
| 2203 | |
| 2204 | } else { |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 2205 | logger->missingFunctionality("forcing non 32-bit vector type"); |
| 2206 | } |
| 2207 | |
| 2208 | return object; |
| 2209 | } |
| 2210 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2211 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 2212 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2213 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2214 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 2215 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2216 | if (node->getType().getQualifier().isSpecConstant()) |
| 2217 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2218 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2219 | spv::Id result = spv::NoResult; |
| 2220 | |
| 2221 | // try texturing first |
| 2222 | result = createImageTextureFunctionCall(node); |
| 2223 | if (result != spv::NoResult) { |
| 2224 | builder.clearAccessChain(); |
| 2225 | builder.setAccessChainRValue(result); |
| 2226 | |
| 2227 | return false; // done with this node |
| 2228 | } |
| 2229 | |
| 2230 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2231 | |
| 2232 | if (node->getOp() == glslang::EOpArrayLength) { |
| 2233 | // Quite special; won't want to evaluate the operand. |
| 2234 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2235 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 2236 | // except for the last block membeor of an SSBO. |
| 2237 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 2238 | // size extracted. |
| 2239 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2240 | // Normal .length() would have been constant folded by the front-end. |
| 2241 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 2242 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2243 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2244 | spv::Id length; |
| 2245 | if (node->getOperand()->getType().isCoopMat()) { |
| 2246 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2247 | |
| 2248 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
| 2249 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2250 | |
| 2251 | length = builder.createCooperativeMatrixLength(typeId); |
| 2252 | } else { |
| 2253 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2254 | block->traverse(this); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2255 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion() |
| 2256 | ->getConstArray()[0].getUConst(); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2257 | length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
| 2258 | } |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2259 | |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2260 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 2261 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 2262 | // AST expectation of a signed result. |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2263 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
| 2264 | if (builder.isInSpecConstCodeGenMode()) { |
| 2265 | length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0)); |
| 2266 | } else { |
| 2267 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 2268 | } |
| 2269 | } |
John Kessenich | 8c86967 | 2018-11-28 07:01:37 -0700 | [diff] [blame] | 2270 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2271 | builder.clearAccessChain(); |
| 2272 | builder.setAccessChainRValue(length); |
| 2273 | |
| 2274 | return false; |
| 2275 | } |
| 2276 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2277 | // Start by evaluating the operand |
| 2278 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2279 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2280 | // operate first on the swizzle base, then apply the swizzle. |
| 2281 | spv::Id invertedType = spv::NoType; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2282 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2283 | invertedType : convertGlslangToSpvType(node->getType()); }; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2284 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 2285 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 2286 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2287 | builder.clearAccessChain(); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2288 | TIntermNode *operandNode; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2289 | if (invertedType != spv::NoType) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2290 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2291 | else |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2292 | operandNode = node->getOperand(); |
| 2293 | |
| 2294 | operandNode->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 2295 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2296 | spv::Id operand = spv::NoResult; |
| 2297 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2298 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2299 | |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2300 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2301 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 2302 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2303 | node->getOp() == glslang::EOpAtomicCounter || |
Neslisah Torosdagli | 7d37a68 | 2020-03-26 10:52:33 -0400 | [diff] [blame] | 2304 | node->getOp() == glslang::EOpInterpolateAtCentroid || |
| 2305 | node->getOp() == glslang::EOpRayQueryProceed || |
| 2306 | node->getOp() == glslang::EOpRayQueryGetRayTMin || |
| 2307 | node->getOp() == glslang::EOpRayQueryGetRayFlags || |
| 2308 | node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin || |
| 2309 | node->getOp() == glslang::EOpRayQueryGetWorldRayDirection || |
| 2310 | node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque || |
| 2311 | node->getOp() == glslang::EOpRayQueryTerminate || |
| 2312 | node->getOp() == glslang::EOpRayQueryConfirmIntersection) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2313 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2314 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2315 | lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
| 2316 | } else |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2317 | #endif |
| 2318 | { |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2319 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | fb4f233 | 2019-08-09 03:49:15 -0600 | [diff] [blame] | 2320 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2321 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2322 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2323 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2324 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2325 | |
| 2326 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2327 | if (! result) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2328 | result = createConversion(node->getOp(), decorations, resultType(), operand, |
| 2329 | node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2330 | |
| 2331 | // if not, then possibly an operation |
| 2332 | if (! result) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2333 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, |
| 2334 | node->getOperand()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2335 | |
| 2336 | if (result) { |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2337 | if (invertedType) { |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2338 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 2339 | decorations.addNonUniform(builder, result); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2340 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2341 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2342 | builder.clearAccessChain(); |
| 2343 | builder.setAccessChainRValue(result); |
| 2344 | |
| 2345 | return false; // done with this node |
| 2346 | } |
| 2347 | |
| 2348 | // it must be a special case, check... |
| 2349 | switch (node->getOp()) { |
| 2350 | case glslang::EOpPostIncrement: |
| 2351 | case glslang::EOpPostDecrement: |
| 2352 | case glslang::EOpPreIncrement: |
| 2353 | case glslang::EOpPreDecrement: |
| 2354 | { |
| 2355 | // 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] | 2356 | spv::Id one = 0; |
| 2357 | if (node->getBasicType() == glslang::EbtFloat) |
| 2358 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2359 | #ifndef GLSLANG_WEB |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 2360 | else if (node->getBasicType() == glslang::EbtDouble) |
| 2361 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2362 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 2363 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2364 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 2365 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2366 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 2367 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2368 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 2369 | one = builder.makeInt64Constant(1); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 2370 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2371 | else |
| 2372 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2373 | glslang::TOperator op; |
| 2374 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2375 | node->getOp() == glslang::EOpPostIncrement) |
| 2376 | op = glslang::EOpAdd; |
| 2377 | else |
| 2378 | op = glslang::EOpSub; |
| 2379 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2380 | spv::Id result = createBinaryOperation(op, decorations, |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2381 | convertGlslangToSpvType(node->getType()), operand, one, |
| 2382 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2383 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2384 | |
| 2385 | // The result of operation is always stored, but conditionally the |
| 2386 | // consumed result. The consumed result is always an r-value. |
Bas Nieuwenhuizen | de949a2 | 2020-08-24 23:27:26 +0200 | [diff] [blame] | 2387 | builder.accessChainStore(result, |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 2388 | TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2389 | builder.clearAccessChain(); |
| 2390 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2391 | node->getOp() == glslang::EOpPreDecrement) |
| 2392 | builder.setAccessChainRValue(result); |
| 2393 | else |
| 2394 | builder.setAccessChainRValue(operand); |
| 2395 | } |
| 2396 | |
| 2397 | return false; |
| 2398 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2399 | #ifndef GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2400 | case glslang::EOpEmitStreamVertex: |
| 2401 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 2402 | return false; |
| 2403 | case glslang::EOpEndStreamPrimitive: |
| 2404 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 2405 | return false; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2406 | case glslang::EOpRayQueryTerminate: |
| 2407 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operand); |
| 2408 | return false; |
| 2409 | case glslang::EOpRayQueryConfirmIntersection: |
| 2410 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operand); |
| 2411 | return false; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 2412 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2413 | |
| 2414 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2415 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2416 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2417 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2418 | } |
| 2419 | |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2420 | // Construct a composite object, recursively copying members if their types don't match |
| 2421 | spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
| 2422 | { |
| 2423 | for (int c = 0; c < (int)constituents.size(); ++c) { |
| 2424 | spv::Id& constituent = constituents[c]; |
| 2425 | spv::Id lType = builder.getContainedTypeId(resultTypeId, c); |
| 2426 | spv::Id rType = builder.getTypeId(constituent); |
| 2427 | if (lType != rType) { |
| 2428 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 2429 | constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent); |
| 2430 | } else if (builder.isStructType(rType)) { |
| 2431 | std::vector<spv::Id> rTypeConstituents; |
| 2432 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2433 | for (int i = 0; i < numrTypeConstituents; ++i) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2434 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, |
| 2435 | builder.getContainedTypeId(rType, i), i)); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2436 | } |
| 2437 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2438 | } else { |
| 2439 | assert(builder.isArrayType(rType)); |
| 2440 | std::vector<spv::Id> rTypeConstituents; |
| 2441 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2442 | |
| 2443 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2444 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2445 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i)); |
| 2446 | } |
| 2447 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | return builder.createCompositeConstruct(resultTypeId, constituents); |
| 2452 | } |
| 2453 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2454 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2455 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 2456 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2457 | if (node->getType().getQualifier().isSpecConstant()) |
| 2458 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2459 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2460 | spv::Id result = spv::NoResult; |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 2461 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2462 | std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for |
| 2463 | // SPIR-V, for an out parameter |
| 2464 | std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2465 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2466 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2467 | invertedType : |
| 2468 | convertGlslangToSpvType(node->getType()); }; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2469 | |
| 2470 | // try texturing |
| 2471 | result = createImageTextureFunctionCall(node); |
| 2472 | if (result != spv::NoResult) { |
| 2473 | builder.clearAccessChain(); |
| 2474 | builder.setAccessChainRValue(result); |
| 2475 | |
| 2476 | return false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2477 | } |
| 2478 | #ifndef GLSLANG_WEB |
| 2479 | else if (node->getOp() == glslang::EOpImageStore || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2480 | node->getOp() == glslang::EOpImageStoreLod || |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2481 | node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2482 | // "imageStore" is a special case, which has no result |
| 2483 | return false; |
| 2484 | } |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2485 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2486 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2487 | glslang::TOperator binOp = glslang::EOpNull; |
| 2488 | bool reduceComparison = true; |
| 2489 | bool isMatrix = false; |
| 2490 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2491 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2492 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2493 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2494 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2495 | assert(node->getOp()); |
| 2496 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 2497 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2498 | |
| 2499 | switch (node->getOp()) { |
| 2500 | case glslang::EOpSequence: |
| 2501 | { |
| 2502 | if (preVisit) |
| 2503 | ++sequenceDepth; |
| 2504 | else |
| 2505 | --sequenceDepth; |
| 2506 | |
| 2507 | if (sequenceDepth == 1) { |
| 2508 | // If this is the parent node of all the functions, we want to see them |
| 2509 | // early, so all call points have actual SPIR-V functions to reference. |
| 2510 | // In all cases, still let the traverser visit the children for us. |
| 2511 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 2512 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2513 | // 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] | 2514 | // anything else gets there, so visit out of order, doing them all now. |
| 2515 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2516 | |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 2517 | //Pre process linker objects for ray tracing stages |
| 2518 | if (glslangIntermediate->isRayTracingStage()) |
| 2519 | collectRayTracingLinkerObjects(); |
| 2520 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2521 | // 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] | 2522 | // so do them manually. |
| 2523 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2524 | |
| 2525 | return false; |
| 2526 | } |
| 2527 | |
| 2528 | return true; |
| 2529 | } |
| 2530 | case glslang::EOpLinkerObjects: |
| 2531 | { |
| 2532 | if (visit == glslang::EvPreVisit) |
| 2533 | linkageOnly = true; |
| 2534 | else |
| 2535 | linkageOnly = false; |
| 2536 | |
| 2537 | return true; |
| 2538 | } |
| 2539 | case glslang::EOpComma: |
| 2540 | { |
| 2541 | // processing from left to right naturally leaves the right-most |
| 2542 | // lying around in the access chain |
| 2543 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2544 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2545 | glslangOperands[i]->traverse(this); |
| 2546 | |
| 2547 | return false; |
| 2548 | } |
| 2549 | case glslang::EOpFunction: |
| 2550 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2551 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2552 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2553 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2554 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2555 | } else { |
| 2556 | handleFunctionEntry(node); |
| 2557 | } |
| 2558 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2559 | if (inEntryPoint) |
| 2560 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2561 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 2562 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | return true; |
| 2566 | case glslang::EOpParameters: |
| 2567 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2568 | // the body, so we still visited the function node's children, making this |
| 2569 | // child redundant. |
| 2570 | return false; |
| 2571 | case glslang::EOpFunctionCall: |
| 2572 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2573 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2574 | if (node->isUserDefined()) |
| 2575 | result = handleUserFunctionCall(node); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2576 | if (result) { |
| 2577 | builder.clearAccessChain(); |
| 2578 | builder.setAccessChainRValue(result); |
| 2579 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2580 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2581 | |
| 2582 | return false; |
| 2583 | } |
| 2584 | case glslang::EOpConstructMat2x2: |
| 2585 | case glslang::EOpConstructMat2x3: |
| 2586 | case glslang::EOpConstructMat2x4: |
| 2587 | case glslang::EOpConstructMat3x2: |
| 2588 | case glslang::EOpConstructMat3x3: |
| 2589 | case glslang::EOpConstructMat3x4: |
| 2590 | case glslang::EOpConstructMat4x2: |
| 2591 | case glslang::EOpConstructMat4x3: |
| 2592 | case glslang::EOpConstructMat4x4: |
| 2593 | case glslang::EOpConstructDMat2x2: |
| 2594 | case glslang::EOpConstructDMat2x3: |
| 2595 | case glslang::EOpConstructDMat2x4: |
| 2596 | case glslang::EOpConstructDMat3x2: |
| 2597 | case glslang::EOpConstructDMat3x3: |
| 2598 | case glslang::EOpConstructDMat3x4: |
| 2599 | case glslang::EOpConstructDMat4x2: |
| 2600 | case glslang::EOpConstructDMat4x3: |
| 2601 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 2602 | case glslang::EOpConstructIMat2x2: |
| 2603 | case glslang::EOpConstructIMat2x3: |
| 2604 | case glslang::EOpConstructIMat2x4: |
| 2605 | case glslang::EOpConstructIMat3x2: |
| 2606 | case glslang::EOpConstructIMat3x3: |
| 2607 | case glslang::EOpConstructIMat3x4: |
| 2608 | case glslang::EOpConstructIMat4x2: |
| 2609 | case glslang::EOpConstructIMat4x3: |
| 2610 | case glslang::EOpConstructIMat4x4: |
| 2611 | case glslang::EOpConstructUMat2x2: |
| 2612 | case glslang::EOpConstructUMat2x3: |
| 2613 | case glslang::EOpConstructUMat2x4: |
| 2614 | case glslang::EOpConstructUMat3x2: |
| 2615 | case glslang::EOpConstructUMat3x3: |
| 2616 | case glslang::EOpConstructUMat3x4: |
| 2617 | case glslang::EOpConstructUMat4x2: |
| 2618 | case glslang::EOpConstructUMat4x3: |
| 2619 | case glslang::EOpConstructUMat4x4: |
| 2620 | case glslang::EOpConstructBMat2x2: |
| 2621 | case glslang::EOpConstructBMat2x3: |
| 2622 | case glslang::EOpConstructBMat2x4: |
| 2623 | case glslang::EOpConstructBMat3x2: |
| 2624 | case glslang::EOpConstructBMat3x3: |
| 2625 | case glslang::EOpConstructBMat3x4: |
| 2626 | case glslang::EOpConstructBMat4x2: |
| 2627 | case glslang::EOpConstructBMat4x3: |
| 2628 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2629 | case glslang::EOpConstructF16Mat2x2: |
| 2630 | case glslang::EOpConstructF16Mat2x3: |
| 2631 | case glslang::EOpConstructF16Mat2x4: |
| 2632 | case glslang::EOpConstructF16Mat3x2: |
| 2633 | case glslang::EOpConstructF16Mat3x3: |
| 2634 | case glslang::EOpConstructF16Mat3x4: |
| 2635 | case glslang::EOpConstructF16Mat4x2: |
| 2636 | case glslang::EOpConstructF16Mat4x3: |
| 2637 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2638 | isMatrix = true; |
| 2639 | // fall through |
| 2640 | case glslang::EOpConstructFloat: |
| 2641 | case glslang::EOpConstructVec2: |
| 2642 | case glslang::EOpConstructVec3: |
| 2643 | case glslang::EOpConstructVec4: |
| 2644 | case glslang::EOpConstructDouble: |
| 2645 | case glslang::EOpConstructDVec2: |
| 2646 | case glslang::EOpConstructDVec3: |
| 2647 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2648 | case glslang::EOpConstructFloat16: |
| 2649 | case glslang::EOpConstructF16Vec2: |
| 2650 | case glslang::EOpConstructF16Vec3: |
| 2651 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2652 | case glslang::EOpConstructBool: |
| 2653 | case glslang::EOpConstructBVec2: |
| 2654 | case glslang::EOpConstructBVec3: |
| 2655 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2656 | case glslang::EOpConstructInt8: |
| 2657 | case glslang::EOpConstructI8Vec2: |
| 2658 | case glslang::EOpConstructI8Vec3: |
| 2659 | case glslang::EOpConstructI8Vec4: |
| 2660 | case glslang::EOpConstructUint8: |
| 2661 | case glslang::EOpConstructU8Vec2: |
| 2662 | case glslang::EOpConstructU8Vec3: |
| 2663 | case glslang::EOpConstructU8Vec4: |
| 2664 | case glslang::EOpConstructInt16: |
| 2665 | case glslang::EOpConstructI16Vec2: |
| 2666 | case glslang::EOpConstructI16Vec3: |
| 2667 | case glslang::EOpConstructI16Vec4: |
| 2668 | case glslang::EOpConstructUint16: |
| 2669 | case glslang::EOpConstructU16Vec2: |
| 2670 | case glslang::EOpConstructU16Vec3: |
| 2671 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2672 | case glslang::EOpConstructInt: |
| 2673 | case glslang::EOpConstructIVec2: |
| 2674 | case glslang::EOpConstructIVec3: |
| 2675 | case glslang::EOpConstructIVec4: |
| 2676 | case glslang::EOpConstructUint: |
| 2677 | case glslang::EOpConstructUVec2: |
| 2678 | case glslang::EOpConstructUVec3: |
| 2679 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2680 | case glslang::EOpConstructInt64: |
| 2681 | case glslang::EOpConstructI64Vec2: |
| 2682 | case glslang::EOpConstructI64Vec3: |
| 2683 | case glslang::EOpConstructI64Vec4: |
| 2684 | case glslang::EOpConstructUint64: |
| 2685 | case glslang::EOpConstructU64Vec2: |
| 2686 | case glslang::EOpConstructU64Vec3: |
| 2687 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2688 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2689 | case glslang::EOpConstructTextureSampler: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 2690 | case glslang::EOpConstructReference: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2691 | case glslang::EOpConstructCooperativeMatrix: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2692 | { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2693 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2694 | std::vector<spv::Id> arguments; |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 2695 | translateArguments(*node, arguments, lvalueCoherentFlags); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2696 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2697 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2698 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2699 | else if (node->getOp() == glslang::EOpConstructStruct || |
| 2700 | node->getOp() == glslang::EOpConstructCooperativeMatrix || |
| 2701 | node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2702 | std::vector<spv::Id> constituents; |
| 2703 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 2704 | constituents.push_back(arguments[c]); |
Jeff Bolz | 5313449 | 2019-06-25 13:31:10 -0500 | [diff] [blame] | 2705 | constructed = createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2706 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2707 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2708 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2709 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2710 | |
Bas Nieuwenhuizen | c9ffeec | 2020-09-03 03:09:39 +0200 | [diff] [blame] | 2711 | if (node->getType().getQualifier().isNonUniform()) { |
| 2712 | builder.addDecoration(constructed, spv::DecorationNonUniformEXT); |
| 2713 | } |
| 2714 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2715 | builder.clearAccessChain(); |
| 2716 | builder.setAccessChainRValue(constructed); |
| 2717 | |
| 2718 | return false; |
| 2719 | } |
| 2720 | |
| 2721 | // These six are component-wise compares with component-wise results. |
| 2722 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 2723 | case glslang::EOpLessThan: |
| 2724 | case glslang::EOpGreaterThan: |
| 2725 | case glslang::EOpLessThanEqual: |
| 2726 | case glslang::EOpGreaterThanEqual: |
| 2727 | case glslang::EOpVectorEqual: |
| 2728 | case glslang::EOpVectorNotEqual: |
| 2729 | { |
| 2730 | // Map the operation to a binary |
| 2731 | binOp = node->getOp(); |
| 2732 | reduceComparison = false; |
| 2733 | switch (node->getOp()) { |
| 2734 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2735 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2736 | default: binOp = node->getOp(); break; |
| 2737 | } |
| 2738 | |
| 2739 | break; |
| 2740 | } |
| 2741 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2742 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2743 | binOp = glslang::EOpMul; |
| 2744 | break; |
| 2745 | case glslang::EOpOuterProduct: |
| 2746 | // two vectors multiplied to make a matrix |
| 2747 | binOp = glslang::EOpOuterProduct; |
| 2748 | break; |
| 2749 | case glslang::EOpDot: |
| 2750 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2751 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2752 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 2753 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2754 | binOp = glslang::EOpMul; |
| 2755 | break; |
| 2756 | } |
| 2757 | case glslang::EOpMod: |
| 2758 | // when an aggregate, this is the floating-point mod built-in function, |
| 2759 | // which can be emitted by the one in createBinaryOperation() |
| 2760 | binOp = glslang::EOpMod; |
| 2761 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2762 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2763 | case glslang::EOpEmitVertex: |
| 2764 | case glslang::EOpEndPrimitive: |
| 2765 | case glslang::EOpBarrier: |
| 2766 | case glslang::EOpMemoryBarrier: |
| 2767 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 2768 | case glslang::EOpMemoryBarrierBuffer: |
| 2769 | case glslang::EOpMemoryBarrierImage: |
| 2770 | case glslang::EOpMemoryBarrierShared: |
| 2771 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2772 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2773 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 2774 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 2775 | case glslang::EOpWorkgroupMemoryBarrier: |
| 2776 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2777 | case glslang::EOpSubgroupBarrier: |
| 2778 | case glslang::EOpSubgroupMemoryBarrier: |
| 2779 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2780 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 2781 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2782 | noReturnValue = true; |
| 2783 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2784 | break; |
| 2785 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2786 | case glslang::EOpAtomicAdd: |
| 2787 | case glslang::EOpAtomicMin: |
| 2788 | case glslang::EOpAtomicMax: |
| 2789 | case glslang::EOpAtomicAnd: |
| 2790 | case glslang::EOpAtomicOr: |
| 2791 | case glslang::EOpAtomicXor: |
| 2792 | case glslang::EOpAtomicExchange: |
| 2793 | case glslang::EOpAtomicCompSwap: |
| 2794 | atomic = true; |
| 2795 | break; |
| 2796 | |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2797 | #ifndef GLSLANG_WEB |
| 2798 | case glslang::EOpAtomicStore: |
| 2799 | noReturnValue = true; |
| 2800 | // fallthrough |
| 2801 | case glslang::EOpAtomicLoad: |
| 2802 | atomic = true; |
| 2803 | break; |
| 2804 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2805 | case glslang::EOpAtomicCounterAdd: |
| 2806 | case glslang::EOpAtomicCounterSubtract: |
| 2807 | case glslang::EOpAtomicCounterMin: |
| 2808 | case glslang::EOpAtomicCounterMax: |
| 2809 | case glslang::EOpAtomicCounterAnd: |
| 2810 | case glslang::EOpAtomicCounterOr: |
| 2811 | case glslang::EOpAtomicCounterXor: |
| 2812 | case glslang::EOpAtomicCounterExchange: |
| 2813 | case glslang::EOpAtomicCounterCompSwap: |
| 2814 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2815 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2816 | atomic = true; |
| 2817 | break; |
| 2818 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 2819 | case glslang::EOpAbsDifference: |
| 2820 | case glslang::EOpAddSaturate: |
| 2821 | case glslang::EOpSubSaturate: |
| 2822 | case glslang::EOpAverage: |
| 2823 | case glslang::EOpAverageRounded: |
| 2824 | case glslang::EOpMul32x16: |
| 2825 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 2826 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 2827 | binOp = node->getOp(); |
| 2828 | break; |
| 2829 | |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 2830 | case glslang::EOpIgnoreIntersectionNV: |
| 2831 | case glslang::EOpTerminateRayNV: |
| 2832 | case glslang::EOpTraceNV: |
| 2833 | case glslang::EOpTraceKHR: |
| 2834 | case glslang::EOpExecuteCallableNV: |
| 2835 | case glslang::EOpExecuteCallableKHR: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2836 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2837 | noReturnValue = true; |
| 2838 | break; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2839 | case glslang::EOpRayQueryInitialize: |
| 2840 | case glslang::EOpRayQueryTerminate: |
| 2841 | case glslang::EOpRayQueryGenerateIntersection: |
| 2842 | case glslang::EOpRayQueryConfirmIntersection: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2843 | builder.addExtension("SPV_KHR_ray_query"); |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 2844 | builder.addCapability(spv::CapabilityRayQueryKHR); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2845 | noReturnValue = true; |
| 2846 | break; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2847 | case glslang::EOpRayQueryProceed: |
| 2848 | case glslang::EOpRayQueryGetIntersectionType: |
| 2849 | case glslang::EOpRayQueryGetRayTMin: |
| 2850 | case glslang::EOpRayQueryGetRayFlags: |
| 2851 | case glslang::EOpRayQueryGetIntersectionT: |
| 2852 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 2853 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 2854 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 2855 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 2856 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 2857 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 2858 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 2859 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 2860 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 2861 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 2862 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 2863 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 2864 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 2865 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 2866 | builder.addExtension("SPV_KHR_ray_query"); |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 2867 | builder.addCapability(spv::CapabilityRayQueryKHR); |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2868 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2869 | case glslang::EOpCooperativeMatrixLoad: |
| 2870 | case glslang::EOpCooperativeMatrixStore: |
| 2871 | noReturnValue = true; |
| 2872 | break; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 2873 | case glslang::EOpBeginInvocationInterlock: |
| 2874 | case glslang::EOpEndInvocationInterlock: |
| 2875 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 2876 | noReturnValue = true; |
| 2877 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2878 | #endif |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 2879 | |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 2880 | case glslang::EOpDebugPrintf: |
| 2881 | noReturnValue = true; |
| 2882 | break; |
| 2883 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2884 | default: |
| 2885 | break; |
| 2886 | } |
| 2887 | |
| 2888 | // |
| 2889 | // See if it maps to a regular operation. |
| 2890 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2891 | if (binOp != glslang::EOpNull) { |
| 2892 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2893 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2894 | assert(left && right); |
| 2895 | |
| 2896 | builder.clearAccessChain(); |
| 2897 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2898 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2899 | |
| 2900 | builder.clearAccessChain(); |
| 2901 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2902 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2903 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 2904 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2905 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 2906 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2907 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 2908 | result = createBinaryOperation(binOp, decorations, |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2909 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2910 | left->getType().getBasicType(), reduceComparison); |
| 2911 | |
| 2912 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2913 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2914 | builder.clearAccessChain(); |
| 2915 | builder.setAccessChainRValue(result); |
| 2916 | |
| 2917 | return false; |
| 2918 | } |
| 2919 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2920 | // |
| 2921 | // Create the list of operands. |
| 2922 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2923 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2924 | std::vector<spv::Id> operands; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 2925 | std::vector<spv::IdImmediate> memoryAccessOperands; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2926 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2927 | // special case l-value operands; there are just a few |
| 2928 | bool lvalue = false; |
| 2929 | switch (node->getOp()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2930 | case glslang::EOpModf: |
| 2931 | if (arg == 1) |
| 2932 | lvalue = true; |
| 2933 | break; |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2934 | |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2935 | case glslang::EOpRayQueryInitialize: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 2936 | case glslang::EOpRayQueryTerminate: |
| 2937 | case glslang::EOpRayQueryConfirmIntersection: |
| 2938 | case glslang::EOpRayQueryProceed: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 2939 | case glslang::EOpRayQueryGenerateIntersection: |
| 2940 | case glslang::EOpRayQueryGetIntersectionType: |
| 2941 | case glslang::EOpRayQueryGetIntersectionT: |
| 2942 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 2943 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 2944 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 2945 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 2946 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 2947 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 2948 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 2949 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 2950 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 2951 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 2952 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 2953 | if (arg == 0) |
| 2954 | lvalue = true; |
| 2955 | break; |
| 2956 | |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 2957 | case glslang::EOpAtomicAdd: |
| 2958 | case glslang::EOpAtomicMin: |
| 2959 | case glslang::EOpAtomicMax: |
| 2960 | case glslang::EOpAtomicAnd: |
| 2961 | case glslang::EOpAtomicOr: |
| 2962 | case glslang::EOpAtomicXor: |
| 2963 | case glslang::EOpAtomicExchange: |
| 2964 | case glslang::EOpAtomicCompSwap: |
| 2965 | if (arg == 0) |
| 2966 | lvalue = true; |
| 2967 | break; |
| 2968 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 2969 | #ifndef GLSLANG_WEB |
| 2970 | case glslang::EOpFrexp: |
| 2971 | if (arg == 1) |
| 2972 | lvalue = true; |
| 2973 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2974 | case glslang::EOpInterpolateAtSample: |
| 2975 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 2976 | case glslang::EOpInterpolateAtVertex: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2977 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2978 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2979 | |
| 2980 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2981 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 2982 | // That is, we transform |
| 2983 | // |
| 2984 | // interpolate(v.zy) -> interpolate(v).zy |
| 2985 | // |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2986 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2987 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 2988 | invertedType = convertGlslangToSpvType( |
| 2989 | glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2990 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 2991 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 2992 | case glslang::EOpAtomicLoad: |
| 2993 | case glslang::EOpAtomicStore: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 2994 | case glslang::EOpAtomicCounterAdd: |
| 2995 | case glslang::EOpAtomicCounterSubtract: |
| 2996 | case glslang::EOpAtomicCounterMin: |
| 2997 | case glslang::EOpAtomicCounterMax: |
| 2998 | case glslang::EOpAtomicCounterAnd: |
| 2999 | case glslang::EOpAtomicCounterOr: |
| 3000 | case glslang::EOpAtomicCounterXor: |
| 3001 | case glslang::EOpAtomicCounterExchange: |
| 3002 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 3003 | if (arg == 0) |
| 3004 | lvalue = true; |
| 3005 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3006 | case glslang::EOpAddCarry: |
| 3007 | case glslang::EOpSubBorrow: |
| 3008 | if (arg == 2) |
| 3009 | lvalue = true; |
| 3010 | break; |
| 3011 | case glslang::EOpUMulExtended: |
| 3012 | case glslang::EOpIMulExtended: |
| 3013 | if (arg >= 2) |
| 3014 | lvalue = true; |
| 3015 | break; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3016 | case glslang::EOpCooperativeMatrixLoad: |
| 3017 | if (arg == 0 || arg == 1) |
| 3018 | lvalue = true; |
| 3019 | break; |
| 3020 | case glslang::EOpCooperativeMatrixStore: |
| 3021 | if (arg == 1) |
| 3022 | lvalue = true; |
| 3023 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3024 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3025 | default: |
| 3026 | break; |
| 3027 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3028 | builder.clearAccessChain(); |
| 3029 | if (invertedType != spv::NoType && arg == 0) |
| 3030 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 3031 | else |
| 3032 | glslangOperands[arg]->traverse(this); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3033 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3034 | #ifndef GLSLANG_WEB |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3035 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
| 3036 | node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 3037 | |
| 3038 | if (arg == 1) { |
| 3039 | // fold "element" parameter into the access chain |
| 3040 | spv::Builder::AccessChain save = builder.getAccessChain(); |
| 3041 | builder.clearAccessChain(); |
| 3042 | glslangOperands[2]->traverse(this); |
| 3043 | |
| 3044 | spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
| 3045 | |
| 3046 | builder.setAccessChain(save); |
| 3047 | |
| 3048 | // Point to the first element of the array. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3049 | builder.accessChainPush(elementId, |
| 3050 | TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
| 3051 | glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3052 | |
| 3053 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3054 | unsigned int alignment = builder.getAccessChain().alignment; |
| 3055 | |
| 3056 | int memoryAccess = TranslateMemoryAccess(coherentFlags); |
| 3057 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
| 3058 | memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
| 3059 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
| 3060 | memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3061 | if (builder.getStorageClass(builder.getAccessChain().base) == |
| 3062 | spv::StorageClassPhysicalStorageBufferEXT) { |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3063 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
| 3064 | } |
| 3065 | |
| 3066 | memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
| 3067 | |
| 3068 | if (memoryAccess & spv::MemoryAccessAlignedMask) { |
| 3069 | memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
| 3070 | } |
| 3071 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3072 | if (memoryAccess & |
| 3073 | (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
| 3074 | memoryAccessOperands.push_back(spv::IdImmediate(true, |
| 3075 | builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3076 | } |
| 3077 | } else if (arg == 2) { |
| 3078 | continue; |
| 3079 | } |
| 3080 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3081 | #endif |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3082 | |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3083 | // for l-values, pass the address, for r-values, pass the value |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 3084 | if (lvalue) { |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3085 | if (invertedType == spv::NoType && !builder.isSpvLvalue()) { |
| 3086 | // SPIR-V cannot represent an l-value containing a swizzle that doesn't |
| 3087 | // reduce to a simple access chain. So, we need a temporary vector to |
| 3088 | // receive the result, and must later swizzle that into the original |
| 3089 | // l-value. |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3090 | complexLvalues.push_back(builder.getAccessChain()); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3091 | temporaryLvalues.push_back(builder.createVariable( |
| 3092 | spv::NoPrecision, spv::StorageClassFunction, |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3093 | builder.accessChainGetInferredType(), "swizzleTemp")); |
| 3094 | operands.push_back(temporaryLvalues.back()); |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3095 | } else { |
| 3096 | operands.push_back(builder.accessChainGetLValue()); |
| 3097 | } |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 3098 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 3099 | lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
| 3100 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3101 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 3102 | glslang::TOperator glslangOp = node->getOp(); |
| 3103 | if (arg == 1 && |
| 3104 | (glslangOp == glslang::EOpRayQueryGetIntersectionType || |
| 3105 | glslangOp == glslang::EOpRayQueryGetIntersectionT || |
| 3106 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex || |
| 3107 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId || |
| 3108 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset || |
| 3109 | glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex || |
| 3110 | glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex || |
| 3111 | glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics || |
| 3112 | glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace || |
| 3113 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection || |
| 3114 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin || |
| 3115 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld || |
| 3116 | glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject |
| 3117 | )) { |
| 3118 | bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst(); |
| 3119 | operands.push_back(builder.makeIntConstant(cond ? 1 : 0)); |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 3120 | } else if ((arg == 10 && glslangOp == glslang::EOpTraceKHR) || |
| 3121 | (arg == 1 && glslangOp == glslang::EOpExecuteCallableKHR)) { |
| 3122 | const int opdNum = glslangOp == glslang::EOpTraceKHR ? 10 : 1; |
| 3123 | const int set = glslangOp == glslang::EOpTraceKHR ? 0 : 1; |
| 3124 | const int location = glslangOperands[opdNum]->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 3125 | auto itNode = locationToSymbol[set].find(location); |
| 3126 | visitSymbol(itNode->second); |
| 3127 | spv::Id symId = getSymbolId(itNode->second); |
| 3128 | operands.push_back(symId); |
| 3129 | } else { |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 3130 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 3131 | } |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3132 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3133 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3134 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3135 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3136 | #ifndef GLSLANG_WEB |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3137 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
| 3138 | std::vector<spv::IdImmediate> idImmOps; |
| 3139 | |
| 3140 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3141 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3142 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3143 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3144 | // get the pointee type |
| 3145 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
| 3146 | assert(builder.isCooperativeMatrixType(typeId)); |
| 3147 | // do the op |
| 3148 | spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
| 3149 | // store the result to the pointer (out param 'm') |
| 3150 | builder.createStore(result, operands[0]); |
| 3151 | result = 0; |
| 3152 | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 3153 | std::vector<spv::IdImmediate> idImmOps; |
| 3154 | |
| 3155 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3156 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
| 3157 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3158 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3159 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3160 | |
| 3161 | builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps); |
| 3162 | result = 0; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3163 | } else |
| 3164 | #endif |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 3165 | if (atomic) { |
| 3166 | // Handle all atomics |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3167 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), |
| 3168 | lvalueCoherentFlags); |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 3169 | } else if (node->getOp() == glslang::EOpDebugPrintf) { |
| 3170 | if (!nonSemanticDebugPrintf) { |
| 3171 | nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf"); |
| 3172 | } |
| 3173 | result = builder.createBuiltinCall(builder.makeVoidType(), nonSemanticDebugPrintf, spv::NonSemanticDebugPrintfDebugPrintf, operands); |
| 3174 | builder.addExtension(spv::E_SPV_KHR_non_semantic_info); |
John Kessenich | e5eee8f | 2019-10-18 01:03:11 -0600 | [diff] [blame] | 3175 | } else { |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3176 | // Pass through to generic operations. |
| 3177 | switch (glslangOperands.size()) { |
| 3178 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3179 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3180 | break; |
| 3181 | case 1: |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3182 | { |
| 3183 | OpDecorations decorations = { precision, |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3184 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 3185 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3186 | result = createUnaryOperation( |
| 3187 | node->getOp(), decorations, |
| 3188 | resultType(), operands.front(), |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 3189 | glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3190 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3191 | break; |
| 3192 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3193 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3194 | break; |
| 3195 | } |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3196 | |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3197 | if (invertedType != spv::NoResult) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3198 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
Cody Northrop | 4d2298b | 2020-04-13 21:59:49 -0600 | [diff] [blame] | 3199 | |
| 3200 | for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) { |
| 3201 | builder.setAccessChain(complexLvalues[i]); |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 3202 | builder.accessChainStore(builder.createLoad(temporaryLvalues[i], spv::NoPrecision), |
| 3203 | TranslateNonUniformDecoration(complexLvalues[i].coherentFlags)); |
John Kessenich | bbbd9a2 | 2020-03-03 07:21:37 -0700 | [diff] [blame] | 3204 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | if (noReturnValue) |
| 3208 | return false; |
| 3209 | |
| 3210 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 3211 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 3212 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3213 | } else { |
| 3214 | builder.clearAccessChain(); |
| 3215 | builder.setAccessChainRValue(result); |
| 3216 | return false; |
| 3217 | } |
| 3218 | } |
| 3219 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3220 | // This path handles both if-then-else and ?: |
| 3221 | // The if-then-else has a node type of void, while |
| 3222 | // ?: has either a void or a non-void node type |
| 3223 | // |
| 3224 | // Leaving the result, when not void: |
| 3225 | // GLSL only has r-values as the result of a :?, but |
| 3226 | // if we have an l-value, that can be more efficient if it will |
| 3227 | // become the base of a complex r-value expression, because the |
| 3228 | // 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] | 3229 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 3230 | { |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3231 | // see if OpSelect can handle it |
| 3232 | const auto isOpSelectable = [&]() { |
| 3233 | if (node->getBasicType() == glslang::EbtVoid) |
| 3234 | return false; |
| 3235 | // OpSelect can do all other types starting with SPV 1.4 |
| 3236 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
| 3237 | // pre-1.4, only scalars and vectors can be handled |
| 3238 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
| 3239 | return false; |
| 3240 | } |
| 3241 | return true; |
| 3242 | }; |
| 3243 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3244 | // See if it simple and safe, or required, to execute both sides. |
| 3245 | // Crucially, side effects must be either semantically required or avoided, |
| 3246 | // and there are performance trade-offs. |
| 3247 | // Return true if required or a good idea (and safe) to execute both sides, |
| 3248 | // false otherwise. |
| 3249 | const auto bothSidesPolicy = [&]() -> bool { |
| 3250 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3251 | if (node->getTrueBlock() == nullptr || |
| 3252 | node->getFalseBlock() == nullptr) |
| 3253 | return false; |
| 3254 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3255 | // required? (unless we write additional code to look for side effects |
| 3256 | // and make performance trade-offs if none are present) |
| 3257 | if (!node->getShortCircuit()) |
| 3258 | return true; |
| 3259 | |
| 3260 | // if not required to execute both, decide based on performance/practicality... |
| 3261 | |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3262 | if (!isOpSelectable()) |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3263 | return false; |
| 3264 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3265 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 3266 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 3267 | |
| 3268 | // return true if a single operand to ? : is okay for OpSelect |
| 3269 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 3270 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3271 | }; |
| 3272 | |
| 3273 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 3274 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 3275 | }; |
| 3276 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3277 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 3278 | // emit the condition before doing anything with selection |
| 3279 | node->getCondition()->traverse(this); |
| 3280 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 3281 | |
| 3282 | // Find a way of executing both sides and selecting the right result. |
| 3283 | const auto executeBothSides = [&]() -> void { |
| 3284 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3285 | node->getTrueBlock()->traverse(this); |
| 3286 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3287 | node->getFalseBlock()->traverse(this); |
| 3288 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3289 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3290 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3291 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3292 | // done if void |
| 3293 | if (node->getBasicType() == glslang::EbtVoid) |
| 3294 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 3295 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3296 | // emit code to select between trueValue and falseValue |
| 3297 | |
| 3298 | // see if OpSelect can handle it |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3299 | if (isOpSelectable()) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3300 | // Emit OpSelect for this selection. |
| 3301 | |
| 3302 | // smear condition to vector, if necessary (AST is always scalar) |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3303 | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
| 3304 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3305 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 3306 | builder.makeVectorType(builder.makeBoolType(), |
| 3307 | builder.getNumComponents(trueValue))); |
John Kessenich | 0c1e71a | 2019-01-10 18:23:06 +0700 | [diff] [blame] | 3308 | } |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3309 | |
| 3310 | // OpSelect |
| 3311 | result = builder.createTriOp(spv::OpSelect, |
| 3312 | convertGlslangToSpvType(node->getType()), condition, |
| 3313 | trueValue, falseValue); |
| 3314 | |
| 3315 | builder.clearAccessChain(); |
| 3316 | builder.setAccessChainRValue(result); |
| 3317 | } else { |
| 3318 | // We need control flow to select the result. |
| 3319 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3320 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), |
| 3321 | spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3322 | |
| 3323 | // Selection control: |
| 3324 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3325 | |
| 3326 | // make an "if" based on the value created by the condition |
| 3327 | spv::Builder::If ifBuilder(condition, control, builder); |
| 3328 | |
| 3329 | // emit the "then" statement |
| 3330 | builder.createStore(trueValue, result); |
| 3331 | ifBuilder.makeBeginElse(); |
| 3332 | // emit the "else" statement |
| 3333 | builder.createStore(falseValue, result); |
| 3334 | |
| 3335 | // finish off the control flow |
| 3336 | ifBuilder.makeEndIf(); |
| 3337 | |
| 3338 | builder.clearAccessChain(); |
| 3339 | builder.setAccessChainLValue(result); |
| 3340 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3341 | }; |
| 3342 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3343 | // Execute the one side needed, as per the condition |
| 3344 | const auto executeOneSide = [&]() { |
| 3345 | // Always emit control flow. |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3346 | if (node->getBasicType() != glslang::EbtVoid) { |
| 3347 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), spv::StorageClassFunction, |
| 3348 | convertGlslangToSpvType(node->getType())); |
| 3349 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 3350 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3351 | // Selection control: |
| 3352 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3353 | |
| 3354 | // make an "if" based on the value created by the condition |
| 3355 | spv::Builder::If ifBuilder(condition, control, builder); |
| 3356 | |
| 3357 | // emit the "then" statement |
| 3358 | if (node->getTrueBlock() != nullptr) { |
| 3359 | node->getTrueBlock()->traverse(this); |
| 3360 | if (result != spv::NoResult) |
| 3361 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 3362 | } |
| 3363 | |
| 3364 | if (node->getFalseBlock() != nullptr) { |
| 3365 | ifBuilder.makeBeginElse(); |
| 3366 | // emit the "else" statement |
| 3367 | node->getFalseBlock()->traverse(this); |
| 3368 | if (result != spv::NoResult) |
| 3369 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 3370 | } |
| 3371 | |
| 3372 | // finish off the control flow |
| 3373 | ifBuilder.makeEndIf(); |
| 3374 | |
| 3375 | if (result != spv::NoResult) { |
| 3376 | builder.clearAccessChain(); |
| 3377 | builder.setAccessChainLValue(result); |
| 3378 | } |
| 3379 | }; |
| 3380 | |
| 3381 | // Try for OpSelect (or a requirement to execute both sides) |
| 3382 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 3383 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 3384 | if (node->getType().getQualifier().isSpecConstant()) |
| 3385 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 3386 | executeBothSides(); |
| 3387 | } else |
| 3388 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3389 | |
| 3390 | return false; |
| 3391 | } |
| 3392 | |
| 3393 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 3394 | { |
| 3395 | // emit and get the condition before doing anything with switch |
| 3396 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3397 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3398 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3399 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 3400 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 3401 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3402 | // browse the children to sort out code segments |
| 3403 | int defaultSegment = -1; |
| 3404 | std::vector<TIntermNode*> codeSegments; |
| 3405 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 3406 | std::vector<int> caseValues; |
| 3407 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 3408 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 3409 | TIntermNode* child = *c; |
| 3410 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3411 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3412 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 3413 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3414 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion() |
| 3415 | ->getConstArray()[0].getIConst()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3416 | } else |
| 3417 | codeSegments.push_back(child); |
| 3418 | } |
| 3419 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3420 | // 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] | 3421 | // statements between the last case and the end of the switch statement |
| 3422 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 3423 | (int)codeSegments.size() == defaultSegment) |
| 3424 | codeSegments.push_back(nullptr); |
| 3425 | |
| 3426 | // make the switch statement |
| 3427 | 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] | 3428 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, |
| 3429 | segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3430 | |
| 3431 | // emit all the code in the segments |
| 3432 | breakForLoop.push(false); |
| 3433 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 3434 | builder.nextSwitchSegment(segmentBlocks, s); |
| 3435 | if (codeSegments[s]) |
| 3436 | codeSegments[s]->traverse(this); |
| 3437 | else |
| 3438 | builder.addSwitchBreak(); |
| 3439 | } |
| 3440 | breakForLoop.pop(); |
| 3441 | |
| 3442 | builder.endSwitch(segmentBlocks); |
| 3443 | |
| 3444 | return false; |
| 3445 | } |
| 3446 | |
| 3447 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 3448 | { |
| 3449 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 3450 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3451 | |
| 3452 | builder.clearAccessChain(); |
| 3453 | builder.setAccessChainRValue(constant); |
| 3454 | } |
| 3455 | |
| 3456 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 3457 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3458 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3459 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3460 | |
| 3461 | // Loop control: |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3462 | std::vector<unsigned int> operands; |
| 3463 | const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 3464 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3465 | // Spec requires back edges to target header blocks, and every header block |
| 3466 | // must dominate its merge block. Make a header block first to ensure these |
| 3467 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 3468 | // by a block-ending branch. But we don't want to put any other body/test |
| 3469 | // instructions in it, since the body/test may have arbitrary instructions, |
| 3470 | // including merges of its own. |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3471 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3472 | builder.setBuildPoint(&blocks.head); |
John Kessenich | 1f4d046 | 2019-01-12 17:31:41 +0700 | [diff] [blame] | 3473 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3474 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 3475 | spv::Block& test = builder.makeNewBlock(); |
| 3476 | builder.createBranch(&test); |
| 3477 | |
| 3478 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3479 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3480 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3481 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 3482 | |
| 3483 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3484 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3485 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3486 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3487 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3488 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3489 | |
| 3490 | builder.setBuildPoint(&blocks.continue_target); |
| 3491 | if (node->getTerminal()) |
| 3492 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3493 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 3494 | } else { |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3495 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3496 | builder.createBranch(&blocks.body); |
| 3497 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3498 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3499 | builder.setBuildPoint(&blocks.body); |
| 3500 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 3501 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3502 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3503 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3504 | |
| 3505 | builder.setBuildPoint(&blocks.continue_target); |
| 3506 | if (node->getTerminal()) |
| 3507 | node->getTerminal()->traverse(this); |
| 3508 | if (node->getTest()) { |
| 3509 | node->getTest()->traverse(this); |
| 3510 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3511 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3512 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3513 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 3514 | // TODO: unless there was a break/return/discard instruction |
| 3515 | // somewhere in the body, this is an infinite loop, so we should |
| 3516 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 3517 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3518 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3519 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 3520 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 3521 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3522 | return false; |
| 3523 | } |
| 3524 | |
| 3525 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 3526 | { |
| 3527 | if (node->getExpression()) |
| 3528 | node->getExpression()->traverse(this); |
| 3529 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 3530 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3531 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3532 | switch (node->getFlowOp()) { |
| 3533 | case glslang::EOpKill: |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 3534 | builder.makeStatementTerminator(spv::OpKill, "post-discard"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3535 | break; |
Jesse Hall | 74e8f05 | 2020-11-09 08:30:01 -0800 | [diff] [blame] | 3536 | case glslang::EOpTerminateInvocation: |
| 3537 | builder.addExtension(spv::E_SPV_KHR_terminate_invocation); |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 3538 | builder.makeStatementTerminator(spv::OpTerminateInvocation, "post-terminate-invocation"); |
Jesse Hall | 74e8f05 | 2020-11-09 08:30:01 -0800 | [diff] [blame] | 3539 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3540 | case glslang::EOpBreak: |
| 3541 | if (breakForLoop.top()) |
| 3542 | builder.createLoopExit(); |
| 3543 | else |
| 3544 | builder.addSwitchBreak(); |
| 3545 | break; |
| 3546 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3547 | builder.createLoopContinue(); |
| 3548 | break; |
| 3549 | case glslang::EOpReturn: |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3550 | if (node->getExpression() != nullptr) { |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3551 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 3552 | spv::Id returnId = accessChainLoad(glslangReturnType); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3553 | if (builder.getTypeId(returnId) != currentFunction->getReturnType() || |
| 3554 | TranslatePrecisionDecoration(glslangReturnType) != currentFunction->getReturnPrecision()) { |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3555 | builder.clearAccessChain(); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3556 | spv::Id copyId = builder.createVariable(currentFunction->getReturnPrecision(), |
| 3557 | spv::StorageClassFunction, currentFunction->getReturnType()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3558 | builder.setAccessChainLValue(copyId); |
| 3559 | multiTypeStore(glslangReturnType, returnId); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3560 | returnId = builder.createLoad(copyId, currentFunction->getReturnPrecision()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3561 | } |
| 3562 | builder.makeReturn(false, returnId); |
| 3563 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 3564 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3565 | |
| 3566 | builder.clearAccessChain(); |
| 3567 | break; |
| 3568 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3569 | #ifndef GLSLANG_WEB |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3570 | case glslang::EOpDemote: |
| 3571 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 3572 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 3573 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 3574 | break; |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 3575 | case glslang::EOpTerminateRayKHR: |
| 3576 | builder.makeStatementTerminator(spv::OpTerminateRayKHR, "post-terminateRayKHR"); |
| 3577 | break; |
| 3578 | case glslang::EOpIgnoreIntersectionKHR: |
| 3579 | builder.makeStatementTerminator(spv::OpIgnoreIntersectionKHR, "post-ignoreIntersectionKHR"); |
| 3580 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3581 | #endif |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 3582 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3583 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3584 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3585 | break; |
| 3586 | } |
| 3587 | |
| 3588 | return false; |
| 3589 | } |
| 3590 | |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3591 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3592 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3593 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3594 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3595 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 3596 | if (node->getQualifier().isConstant()) { |
Dan Sinclair | 12fcaa2 | 2018-11-13 09:17:44 -0500 | [diff] [blame] | 3597 | spv::Id result = createSpvConstant(*node); |
| 3598 | if (result != spv::NoResult) |
| 3599 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3603 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 3604 | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
| 3605 | : forcedType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3606 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3607 | const bool contains16BitType = node->getType().contains16BitFloat() || |
| 3608 | node->getType().contains16BitInt(); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3609 | if (contains16BitType) { |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3610 | switch (storageClass) { |
| 3611 | case spv::StorageClassInput: |
| 3612 | case spv::StorageClassOutput: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3613 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3614 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3615 | break; |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3616 | case spv::StorageClassUniform: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3617 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3618 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 3619 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3620 | else |
| 3621 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 3622 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3623 | #ifndef GLSLANG_WEB |
| 3624 | case spv::StorageClassPushConstant: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3625 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3626 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 3627 | break; |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3628 | case spv::StorageClassStorageBuffer: |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3629 | case spv::StorageClassPhysicalStorageBufferEXT: |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3630 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3631 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 3632 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3633 | #endif |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3634 | default: |
Caio Marcelo de Oliveira Filho | 4bfbf62 | 2020-06-02 16:58:51 -0700 | [diff] [blame] | 3635 | if (storageClass == spv::StorageClassWorkgroup && |
| 3636 | node->getType().getBasicType() == glslang::EbtBlock) { |
| 3637 | builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayout16BitAccessKHR); |
| 3638 | break; |
| 3639 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3640 | if (node->getType().contains16BitFloat()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3641 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3642 | if (node->getType().contains16BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3643 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | 1831087 | 2018-05-14 22:08:53 -0600 | [diff] [blame] | 3644 | break; |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3645 | } |
| 3646 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 3647 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3648 | if (node->getType().contains8BitInt()) { |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3649 | if (storageClass == spv::StorageClassPushConstant) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3650 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3651 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 3652 | } else if (storageClass == spv::StorageClassUniform) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3653 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3654 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3655 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3656 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
Neil Henning | b6b01f0 | 2018-10-23 15:02:29 +0100 | [diff] [blame] | 3657 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
Caio Marcelo de Oliveira Filho | 4bfbf62 | 2020-06-02 16:58:51 -0700 | [diff] [blame] | 3658 | } else if (storageClass == spv::StorageClassWorkgroup && |
| 3659 | node->getType().getBasicType() == glslang::EbtBlock) { |
| 3660 | builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayout8BitAccessKHR); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 3661 | } else { |
| 3662 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | 312dcfb | 2018-07-03 13:19:51 -0600 | [diff] [blame] | 3663 | } |
| 3664 | } |
| 3665 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3666 | const char* name = node->getName().c_str(); |
| 3667 | if (glslang::IsAnonymous(name)) |
| 3668 | name = ""; |
| 3669 | |
Alejandro Piñeiro | ff6dcca | 2020-06-04 09:39:31 +0200 | [diff] [blame] | 3670 | spv::Id initializer = spv::NoResult; |
| 3671 | |
John Kessenich | c739e03 | 2020-06-11 08:30:03 -0600 | [diff] [blame] | 3672 | if (node->getType().getQualifier().storage == glslang::EvqUniform && !node->getConstArray().empty()) { |
| 3673 | int nextConst = 0; |
| 3674 | initializer = createSpvConstantFromConstUnionArray(node->getType(), |
| 3675 | node->getConstArray(), |
| 3676 | nextConst, |
| 3677 | false /* specConst */); |
| 3678 | } else if (node->getType().getQualifier().isNullInit()) { |
| 3679 | initializer = builder.makeNullConstant(spvType); |
Alejandro Piñeiro | ff6dcca | 2020-06-04 09:39:31 +0200 | [diff] [blame] | 3680 | } |
| 3681 | |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 3682 | return builder.createVariable(spv::NoPrecision, storageClass, spvType, name, initializer); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | // Return type Id of the sampled type. |
| 3686 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 3687 | { |
| 3688 | switch (sampler.type) { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3689 | case glslang::EbtInt: return builder.makeIntType(32); |
| 3690 | case glslang::EbtUint: return builder.makeUintType(32); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3691 | case glslang::EbtFloat: return builder.makeFloatType(32); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3692 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3693 | case glslang::EbtFloat16: |
| 3694 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 3695 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3696 | return builder.makeFloatType(16); |
Tobski | 8c1a3a0 | 2020-11-04 16:24:23 +0000 | [diff] [blame] | 3697 | case glslang::EbtInt64: return builder.makeIntType(64); |
| 3698 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 3699 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3700 | case glslang::EbtUint64: return builder.makeUintType(64); |
| 3701 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 3702 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3703 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3704 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3705 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3706 | return builder.makeFloatType(32); |
| 3707 | } |
| 3708 | } |
| 3709 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3710 | // If node is a swizzle operation, return the type that should be used if |
| 3711 | // the swizzle base is first consumed by another operation, before the swizzle |
| 3712 | // is applied. |
| 3713 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 3714 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3715 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3716 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 3717 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 3718 | else |
| 3719 | return spv::NoType; |
| 3720 | } |
| 3721 | |
| 3722 | // When inverting a swizzle with a parent op, this function |
| 3723 | // will apply the swizzle operation to a completed parent operation. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 3724 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, |
| 3725 | spv::Id parentResult) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3726 | { |
| 3727 | std::vector<unsigned> swizzle; |
| 3728 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 3729 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 3730 | } |
| 3731 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3732 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 3733 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 3734 | { |
| 3735 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 3736 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 3737 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3738 | } |
| 3739 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3740 | // Convert from a glslang type to an SPV type, by calling into a |
| 3741 | // recursive version of this function. This establishes the inherited |
| 3742 | // layout state rooted from the top-level type. |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3743 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3744 | { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3745 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | // 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] | 3749 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3750 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3751 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 3752 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 3753 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3754 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3755 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3756 | |
| 3757 | switch (type.getBasicType()) { |
| 3758 | case glslang::EbtVoid: |
| 3759 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3760 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3761 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3762 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 3763 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 3764 | // a 32-bit int where non-0 means true. |
| 3765 | if (explicitLayout != glslang::ElpNone) |
| 3766 | spvType = builder.makeUintType(32); |
| 3767 | else |
| 3768 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3769 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3770 | case glslang::EbtInt: |
| 3771 | spvType = builder.makeIntType(32); |
| 3772 | break; |
| 3773 | case glslang::EbtUint: |
| 3774 | spvType = builder.makeUintType(32); |
| 3775 | break; |
| 3776 | case glslang::EbtFloat: |
| 3777 | spvType = builder.makeFloatType(32); |
| 3778 | break; |
| 3779 | #ifndef GLSLANG_WEB |
| 3780 | case glslang::EbtDouble: |
| 3781 | spvType = builder.makeFloatType(64); |
| 3782 | break; |
| 3783 | case glslang::EbtFloat16: |
| 3784 | spvType = builder.makeFloatType(16); |
| 3785 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3786 | case glslang::EbtInt8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3787 | spvType = builder.makeIntType(8); |
| 3788 | break; |
| 3789 | case glslang::EbtUint8: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3790 | spvType = builder.makeUintType(8); |
| 3791 | break; |
John Kessenich | 31aa3d6 | 2018-08-15 13:54:09 -0600 | [diff] [blame] | 3792 | case glslang::EbtInt16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3793 | spvType = builder.makeIntType(16); |
| 3794 | break; |
| 3795 | case glslang::EbtUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3796 | spvType = builder.makeUintType(16); |
| 3797 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3798 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3799 | spvType = builder.makeIntType(64); |
| 3800 | break; |
| 3801 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3802 | spvType = builder.makeUintType(64); |
| 3803 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3804 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 3805 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3806 | spvType = builder.makeUintType(32); |
| 3807 | break; |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 3808 | case glslang::EbtAccStruct: |
Daniel Koch | 4d41da3 | 2020-11-24 23:06:16 -0500 | [diff] [blame] | 3809 | switch (glslangIntermediate->getStage()) { |
| 3810 | case EShLangRayGen: |
| 3811 | case EShLangIntersect: |
| 3812 | case EShLangAnyHit: |
| 3813 | case EShLangClosestHit: |
| 3814 | case EShLangMiss: |
| 3815 | case EShLangCallable: |
| 3816 | // these all should have the RayTracingNV/KHR capability already |
| 3817 | break; |
| 3818 | default: |
| 3819 | { |
| 3820 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3821 | if (extensions.find("GL_EXT_ray_query") != extensions.end()) { |
| 3822 | builder.addExtension(spv::E_SPV_KHR_ray_query); |
| 3823 | builder.addCapability(spv::CapabilityRayQueryKHR); |
| 3824 | } |
| 3825 | } |
| 3826 | break; |
| 3827 | } |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 3828 | spvType = builder.makeAccelerationStructureType(); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3829 | break; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 3830 | case glslang::EbtRayQuery: |
Daniel Koch | 4d41da3 | 2020-11-24 23:06:16 -0500 | [diff] [blame] | 3831 | { |
| 3832 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3833 | if (extensions.find("GL_EXT_ray_query") != extensions.end()) { |
| 3834 | builder.addExtension(spv::E_SPV_KHR_ray_query); |
| 3835 | builder.addCapability(spv::CapabilityRayQueryKHR); |
| 3836 | } |
| 3837 | spvType = builder.makeRayQueryType(); |
| 3838 | } |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 3839 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3840 | case glslang::EbtReference: |
| 3841 | { |
| 3842 | // Make the forward pointer, then recurse to convert the structure type, then |
| 3843 | // patch up the forward pointer with a real pointer type. |
| 3844 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 3845 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 3846 | forwardPointers[type.getReferentType()] = forwardId; |
| 3847 | } |
| 3848 | spvType = forwardPointers[type.getReferentType()]; |
| 3849 | if (!forwardReferenceOnly) { |
| 3850 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 3851 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 3852 | forwardPointers[type.getReferentType()], |
| 3853 | referentType); |
| 3854 | } |
| 3855 | } |
| 3856 | break; |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 3857 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3858 | case glslang::EbtSampler: |
| 3859 | { |
| 3860 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3861 | if (sampler.isPureSampler()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3862 | spvType = builder.makeSamplerType(); |
| 3863 | } else { |
| 3864 | // an image is present, make its type |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 3865 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), |
| 3866 | sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), |
| 3867 | sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type)); |
| 3868 | if (sampler.isCombined()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3869 | // already has both image and sampler, make the combined type |
| 3870 | spvType = builder.makeSampledImageType(spvType); |
| 3871 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3872 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3873 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3874 | break; |
| 3875 | case glslang::EbtStruct: |
| 3876 | case glslang::EbtBlock: |
| 3877 | { |
| 3878 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3879 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3880 | |
| 3881 | // Try to share structs for different layouts, but not yet for other |
| 3882 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 3883 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3884 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3885 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3886 | break; |
| 3887 | |
| 3888 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3889 | if (type.getBasicType() == glslang::EbtBlock) |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 3890 | memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(glslangMembers->size()); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 3891 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3892 | } |
| 3893 | break; |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 3894 | case glslang::EbtString: |
| 3895 | // no type used for OpString |
| 3896 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3897 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3898 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3899 | break; |
| 3900 | } |
| 3901 | |
| 3902 | if (type.isMatrix()) |
| 3903 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3904 | else { |
| 3905 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3906 | if (type.getVectorSize() > 1) |
| 3907 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3908 | } |
| 3909 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3910 | if (type.isCoopMat()) { |
| 3911 | builder.addCapability(spv::CapabilityCooperativeMatrixNV); |
| 3912 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
| 3913 | if (type.getBasicType() == glslang::EbtFloat16) |
| 3914 | builder.addCapability(spv::CapabilityFloat16); |
Jeff Bolz | 387657e | 2019-08-22 20:28:00 -0500 | [diff] [blame] | 3915 | if (type.getBasicType() == glslang::EbtUint8 || |
| 3916 | type.getBasicType() == glslang::EbtInt8) { |
| 3917 | builder.addCapability(spv::CapabilityInt8); |
| 3918 | } |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 3919 | |
| 3920 | spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1); |
| 3921 | spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2); |
| 3922 | spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3); |
| 3923 | |
| 3924 | spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols); |
| 3925 | } |
| 3926 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3927 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3928 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3929 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3930 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3931 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3932 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 3933 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3934 | // Use a dummy glslang type for querying internal strides of |
| 3935 | // arrays of arrays, but using just a one-dimensional array. |
| 3936 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
John Kessenich | 859b034 | 2018-03-26 00:38:53 -0600 | [diff] [blame] | 3937 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3938 | simpleArrayType.getArraySizes()->dereference(); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3939 | |
| 3940 | // Will compute the higher-order strides here, rather than making a whole |
| 3941 | // pile of types and doing repetitive recursion on their contents. |
| 3942 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3943 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3944 | |
| 3945 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3946 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3947 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3948 | if (stride > 0) |
| 3949 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3950 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3951 | } |
| 3952 | } else { |
| 3953 | // single-dimensional array, and don't yet have stride |
| 3954 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 3955 | // 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] | 3956 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3957 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 3958 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 3959 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 3960 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 3961 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3962 | if (type.isSizedArray()) |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3963 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3964 | else { |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3965 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3966 | if (!lastBufferBlockMember) { |
John Kessenich | 8317e6c | 2019-08-18 23:58:08 -0600 | [diff] [blame] | 3967 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3968 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3969 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 3970 | #endif |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 3971 | spvType = builder.makeRuntimeArray(spvType); |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 3972 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 3973 | if (stride > 0) |
| 3974 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | return spvType; |
| 3978 | } |
| 3979 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3980 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 3981 | // |
| 3982 | // Identify interface members that don't have their required extension turned on. |
| 3983 | // |
| 3984 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3985 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 3986 | #ifndef GLSLANG_WEB |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3987 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3988 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 3989 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3990 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3991 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 3992 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3993 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3994 | return true; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 3995 | |
| 3996 | if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3997 | if (member.getFieldName() == "gl_ViewportMask" && |
| 3998 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3999 | return true; |
| 4000 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 4001 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 4002 | return true; |
| 4003 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 4004 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 4005 | return true; |
| 4006 | } |
| 4007 | #endif |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 4008 | |
| 4009 | return false; |
| 4010 | }; |
| 4011 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4012 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 4013 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 4014 | // Mutually recursive with convertGlslangToSpvType(). |
| 4015 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 4016 | const glslang::TTypeList* glslangMembers, |
| 4017 | glslang::TLayoutPacking explicitLayout, |
| 4018 | const glslang::TQualifier& qualifier) |
| 4019 | { |
| 4020 | // Create a vector of struct types for SPIR-V to consume |
| 4021 | std::vector<spv::Id> spvMembers; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4022 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, |
| 4023 | // except sometimes for blocks |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4024 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4025 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 4026 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 4027 | if (glslangMember.hiddenMember()) { |
| 4028 | ++memberDelta; |
| 4029 | if (type.getBasicType() == glslang::EbtBlock) |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 4030 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4031 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 4032 | if (type.getBasicType() == glslang::EbtBlock) { |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 4033 | if (filterMember(glslangMember)) { |
| 4034 | memberDelta++; |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 4035 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 4036 | continue; |
Ashwin Lele | c1e61d6 | 2019-07-22 12:36:38 -0700 | [diff] [blame] | 4037 | } |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 4038 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 4039 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4040 | // modify just this child's view of the qualifier |
| 4041 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 4042 | InheritQualifiers(memberQualifier, qualifier); |
| 4043 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 4044 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4045 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 4046 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4047 | |
| 4048 | // recurse |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4049 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 4050 | i == (int)glslangMembers->size() - 1; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4051 | |
| 4052 | // Make forward pointers for any pointer members, and create a list of members to |
| 4053 | // convert to spirv types after creating the struct. |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 4054 | if (glslangMember.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4055 | if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) { |
| 4056 | deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier)); |
| 4057 | } |
| 4058 | spvMembers.push_back( |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4059 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 4060 | true)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4061 | } else { |
| 4062 | spvMembers.push_back( |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4063 | convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 4064 | false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4065 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | // Make the SPIR-V type |
| 4070 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 4071 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4072 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 4073 | |
| 4074 | // Decorate it |
| 4075 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 4076 | |
John Kessenich | d72f488 | 2019-01-16 14:55:37 +0700 | [diff] [blame] | 4077 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4078 | auto it = deferredForwardPointers[i]; |
| 4079 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 4080 | } |
| 4081 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4082 | return spvType; |
| 4083 | } |
| 4084 | |
| 4085 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 4086 | const glslang::TTypeList* glslangMembers, |
| 4087 | glslang::TLayoutPacking explicitLayout, |
| 4088 | const glslang::TQualifier& qualifier, |
| 4089 | spv::Id spvType) |
| 4090 | { |
| 4091 | // Name and decorate the non-hidden members |
| 4092 | int offset = -1; |
| 4093 | int locationOffset = 0; // for use within the members of this struct |
Chow | 478b232 | 2020-11-04 04:34:19 +0800 | [diff] [blame] | 4094 | bool memberLocationInvalid = type.isArrayOfArrays() || |
| 4095 | (type.isArray() && (type.getQualifier().isArrayedIo(glslangIntermediate->getStage()) == false)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4096 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 4097 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 4098 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 4099 | if (type.getBasicType() == glslang::EbtBlock) { |
Roy | 05a5b53 | 2020-01-03 16:21:34 +0800 | [diff] [blame] | 4100 | member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 4101 | if (filterMember(glslangMember)) |
| 4102 | continue; |
| 4103 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4104 | |
| 4105 | // modify just this child's view of the qualifier |
| 4106 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 4107 | InheritQualifiers(memberQualifier, qualifier); |
| 4108 | |
| 4109 | // using -1 above to indicate a hidden member |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4110 | if (member < 0) |
| 4111 | continue; |
| 4112 | |
| 4113 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 4114 | builder.addMemberDecoration(spvType, member, |
| 4115 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 4116 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 4117 | // Add interpolation and auxiliary storage decorations only to |
| 4118 | // top-level members of Input and Output storage classes |
| 4119 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 4120 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 4121 | if (type.getBasicType() == glslang::EbtBlock || |
| 4122 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 4123 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 4124 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4125 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4126 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 4127 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4128 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4129 | } |
| 4130 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4131 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4132 | #ifndef GLSLANG_WEB |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4133 | if (type.getBasicType() == glslang::EbtBlock && |
| 4134 | qualifier.storage == glslang::EvqBuffer) { |
| 4135 | // Add memory decorations only to top-level members of shader storage block |
| 4136 | std::vector<spv::Decoration> memory; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4137 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4138 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 4139 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 4140 | } |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 4141 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4142 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4143 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4144 | // Location assignment was already completed correctly by the front end, |
| 4145 | // just track whether a member needs to be decorated. |
| 4146 | // Ignore member locations if the container is an array, as that's |
| 4147 | // ill-specified and decisions have been made to not allow this. |
Chow | 478b232 | 2020-11-04 04:34:19 +0800 | [diff] [blame] | 4148 | if (!memberLocationInvalid && memberQualifier.hasLocation()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4149 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4150 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4151 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 4152 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 4153 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 4154 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4155 | // component, XFB, others |
| 4156 | if (glslangMember.getQualifier().hasComponent()) |
| 4157 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 4158 | glslangMember.getQualifier().layoutComponent); |
| 4159 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 4160 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 4161 | glslangMember.getQualifier().layoutXfbOffset); |
| 4162 | else if (explicitLayout != glslang::ElpNone) { |
| 4163 | // figure out what to do with offset, which is accumulating |
| 4164 | int nextOffset; |
| 4165 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 4166 | if (offset >= 0) |
| 4167 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 4168 | offset = nextOffset; |
| 4169 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4170 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4171 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 4172 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 4173 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4174 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4175 | // built-in variable decorations |
| 4176 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 4177 | if (builtIn != spv::BuiltInMax) |
| 4178 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4179 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4180 | #ifndef GLSLANG_WEB |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4181 | // nonuniform |
| 4182 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 4183 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 4184 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 4185 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 4186 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 4187 | memberQualifier.semanticName); |
| 4188 | } |
| 4189 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4190 | if (builtIn == spv::BuiltInLayer) { |
| 4191 | // SPV_NV_viewport_array2 extension |
| 4192 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 4193 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 4194 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 4195 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4196 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4197 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 4198 | builder.addMemberDecoration(spvType, member, |
| 4199 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 4200 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 4201 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 4202 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 4203 | } |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4204 | } |
| 4205 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 4206 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 4207 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 4208 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 4209 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4210 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | // Decorate the structure |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 4214 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 4215 | builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 4216 | } |
| 4217 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4218 | // Turn the expression forming the array size into an id. |
| 4219 | // This is not quite trivial, because of specialization constants. |
| 4220 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 4221 | // a specialization constant expression is. |
| 4222 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 4223 | { |
| 4224 | // First, see if this is sized with a node, meaning a specialization constant: |
| 4225 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 4226 | if (specNode != nullptr) { |
| 4227 | builder.clearAccessChain(); |
| 4228 | specNode->traverse(this); |
| 4229 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 4230 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4231 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4232 | // Otherwise, need a compile-time (front end) size, get it: |
| 4233 | int size = arraySizes.getDimSize(dim); |
| 4234 | assert(size > 0); |
| 4235 | return builder.makeUintConstant(size); |
| 4236 | } |
| 4237 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4238 | // Wrap the builder's accessChainLoad to: |
| 4239 | // - localize handling of RelaxedPrecision |
| 4240 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 4241 | // (avoids unnecessary work and possible type punning for structures) |
| 4242 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4243 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 4244 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4245 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4246 | |
| 4247 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4248 | coherentFlags |= TranslateCoherent(type); |
| 4249 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4250 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 4251 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4252 | |
John Kessenich | 5611c6d | 2018-04-05 11:25:02 -0600 | [diff] [blame] | 4253 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 4254 | TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4255 | TranslateNonUniformDecoration(type.getQualifier()), |
| 4256 | nominalTypeId, |
| 4257 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
| 4258 | TranslateMemoryScope(coherentFlags), |
| 4259 | alignment); |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4260 | |
| 4261 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4262 | if (type.getBasicType() == glslang::EbtBool) { |
| 4263 | if (builder.isScalarType(nominalTypeId)) { |
| 4264 | // Conversion for bool |
| 4265 | spv::Id boolType = builder.makeBoolType(); |
| 4266 | if (nominalTypeId != boolType) |
| 4267 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 4268 | } else if (builder.isVectorType(nominalTypeId)) { |
| 4269 | // Conversion for bvec |
| 4270 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 4271 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 4272 | if (nominalTypeId != bvecType) |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4273 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, |
| 4274 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4275 | } |
| 4276 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 4277 | |
| 4278 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4279 | } |
| 4280 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4281 | // Wrap the builder's accessChainStore to: |
| 4282 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4283 | // |
| 4284 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4285 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 4286 | { |
| 4287 | // Need to convert to abstract types when necessary |
| 4288 | if (type.getBasicType() == glslang::EbtBool) { |
| 4289 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 4290 | |
| 4291 | if (builder.isScalarType(nominalTypeId)) { |
| 4292 | // Conversion for bool |
| 4293 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4294 | if (nominalTypeId != boolType) { |
| 4295 | // keep these outside arguments, for determinant order-of-evaluation |
| 4296 | spv::Id one = builder.makeUintConstant(1); |
| 4297 | spv::Id zero = builder.makeUintConstant(0); |
| 4298 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 4299 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 4300 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4301 | } else if (builder.isVectorType(nominalTypeId)) { |
| 4302 | // Conversion for bvec |
| 4303 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 4304 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4305 | if (nominalTypeId != bvecType) { |
| 4306 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 4307 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 4308 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 4309 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 4310 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 4311 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 4312 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4313 | } |
| 4314 | } |
| 4315 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4316 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4317 | coherentFlags |= TranslateCoherent(type); |
| 4318 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4319 | unsigned int alignment = builder.getAccessChain().alignment; |
Jeff Bolz | 7895e47 | 2019-03-06 13:34:10 -0600 | [diff] [blame] | 4320 | alignment |= type.getBufferReferenceAlignment(); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4321 | |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 4322 | builder.accessChainStore(rvalue, TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags), |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4323 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & |
| 4324 | ~spv::MemoryAccessMakePointerVisibleKHRMask), |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4325 | TranslateMemoryScope(coherentFlags), alignment); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 4326 | } |
| 4327 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4328 | // For storing when types match at the glslang level, but not might match at the |
| 4329 | // SPIR-V level. |
| 4330 | // |
| 4331 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4332 | // 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] | 4333 | // as in a member-decorated way. |
| 4334 | // |
| 4335 | // NOTE: This function can handle any store request; if it's not special it |
| 4336 | // simplifies to a simple OpStore. |
| 4337 | // |
| 4338 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 4339 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 4340 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4341 | // we only do the complex path here if it's an aggregate |
| 4342 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4343 | accessChainStore(type, rValue); |
| 4344 | return; |
| 4345 | } |
| 4346 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4347 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4348 | spv::Id rType = builder.getTypeId(rValue); |
| 4349 | spv::Id lValue = builder.accessChainGetLValue(); |
| 4350 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 4351 | if (lType == rType) { |
| 4352 | accessChainStore(type, rValue); |
| 4353 | return; |
| 4354 | } |
| 4355 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4356 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4357 | // where the two types were the same type in GLSL. This requires member |
| 4358 | // by member copy, recursively. |
| 4359 | |
John Kessenich | fbb6bdf | 2019-01-15 21:48:27 +0700 | [diff] [blame] | 4360 | // SPIR-V 1.4 added an instruction to do help do this. |
| 4361 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 4362 | // However, bool in uniform space is changed to int, so |
| 4363 | // OpCopyLogical does not work for that. |
| 4364 | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
| 4365 | bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0); |
| 4366 | bool lBool = builder.containsType(lType, spv::OpTypeBool, 0); |
| 4367 | if (lBool == rBool) { |
| 4368 | spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue); |
| 4369 | accessChainStore(type, logicalCopy); |
| 4370 | return; |
| 4371 | } |
| 4372 | } |
| 4373 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4374 | // If an array, copy element by element. |
| 4375 | if (type.isArray()) { |
| 4376 | glslang::TType glslangElementType(type, 0); |
| 4377 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 4378 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 4379 | // get the source member |
| 4380 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4381 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4382 | // set up the target storage |
| 4383 | builder.clearAccessChain(); |
| 4384 | builder.setAccessChainLValue(lValue); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4385 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), |
| 4386 | type.getBufferReferenceAlignment()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4387 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4388 | // store the member |
| 4389 | multiTypeStore(glslangElementType, elementRValue); |
| 4390 | } |
| 4391 | } else { |
| 4392 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4393 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4394 | // loop over structure members |
| 4395 | const glslang::TTypeList& members = *type.getStruct(); |
| 4396 | for (int m = 0; m < (int)members.size(); ++m) { |
| 4397 | const glslang::TType& glslangMemberType = *members[m].type; |
| 4398 | |
| 4399 | // get the source member |
| 4400 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 4401 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 4402 | |
| 4403 | // set up the target storage |
| 4404 | builder.clearAccessChain(); |
| 4405 | builder.setAccessChainLValue(lValue); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4406 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), |
| 4407 | type.getBufferReferenceAlignment()); |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 4408 | |
| 4409 | // store the member |
| 4410 | multiTypeStore(glslangMemberType, memberRValue); |
| 4411 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4412 | } |
| 4413 | } |
| 4414 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4415 | // Decide whether or not this type should be |
| 4416 | // decorated with offsets and strides, and if so |
| 4417 | // whether std140 or std430 rules should be applied. |
| 4418 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 4419 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4420 | // has to be a block |
| 4421 | if (type.getBasicType() != glslang::EbtBlock) |
| 4422 | return glslang::ElpNone; |
| 4423 | |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4424 | // 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] | 4425 | if (type.getQualifier().storage != glslang::EvqUniform && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4426 | type.getQualifier().storage != glslang::EvqBuffer && |
Caio Marcelo de Oliveira Filho | 4bfbf62 | 2020-06-02 16:58:51 -0700 | [diff] [blame] | 4427 | type.getQualifier().storage != glslang::EvqShared && |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4428 | !type.getQualifier().isTaskMemory()) |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4429 | return glslang::ElpNone; |
| 4430 | |
| 4431 | // return the layout to use |
| 4432 | switch (type.getQualifier().layoutPacking) { |
| 4433 | case glslang::ElpStd140: |
| 4434 | case glslang::ElpStd430: |
Jeff Bolz | 7da39ed | 2018-11-14 09:30:53 -0600 | [diff] [blame] | 4435 | case glslang::ElpScalar: |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4436 | return type.getQualifier().layoutPacking; |
| 4437 | default: |
| 4438 | return glslang::ElpNone; |
| 4439 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 4440 | } |
| 4441 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4442 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4443 | int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, |
| 4444 | glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4445 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4446 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4447 | int stride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4448 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, |
| 4449 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 4450 | |
| 4451 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4452 | } |
| 4453 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4454 | // 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] | 4455 | // when used as a member of an interface block |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4456 | int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, |
| 4457 | glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4458 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4459 | glslang::TType elementType; |
| 4460 | elementType.shallowCopy(matrixType); |
| 4461 | elementType.clearArraySizes(); |
| 4462 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4463 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4464 | int stride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4465 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, |
| 4466 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4467 | |
| 4468 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 4469 | } |
| 4470 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4471 | // Given a member type of a struct, realign the current offset for it, and compute |
| 4472 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 4473 | // on the next call. |
| 4474 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 4475 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 4476 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4477 | void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, |
| 4478 | int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4479 | { |
| 4480 | // this will get a positive value when deemed necessary |
| 4481 | nextOffset = -1; |
| 4482 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4483 | // override anything in currentOffset with user-set offset |
| 4484 | if (memberType.getQualifier().hasOffset()) |
| 4485 | currentOffset = memberType.getQualifier().layoutOffset; |
| 4486 | |
| 4487 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 4488 | // in which case the following code does not matter. But, that's not quite right |
| 4489 | // once cross-compilation unit GLSL validation is done, as the original user |
| 4490 | // settings are needed in layoutOffset, and then the following will come into play. |
| 4491 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4492 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4493 | if (! memberType.getQualifier().hasOffset()) |
| 4494 | currentOffset = -1; |
| 4495 | |
| 4496 | return; |
| 4497 | } |
| 4498 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 4499 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4500 | if (currentOffset < 0) |
| 4501 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4502 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4503 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 4504 | // but possibly not yet correctly aligned. |
| 4505 | |
| 4506 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 4507 | int dummyStride; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4508 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, |
| 4509 | matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4510 | |
| 4511 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4512 | // TODO: make this consistent in early phases of code: |
| 4513 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 4514 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 4515 | // 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] | 4516 | if (glslangIntermediate->usingHlslOffsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 4517 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4518 | int dummySize; |
| 4519 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 4520 | if (componentAlignment <= 4) |
| 4521 | memberAlignment = componentAlignment; |
| 4522 | } |
| 4523 | |
| 4524 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4525 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4526 | |
| 4527 | // Bump up to vec4 if there is a bad straddle |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4528 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, |
| 4529 | currentOffset)) |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 4530 | glslang::RoundToPow2(currentOffset, 16); |
| 4531 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4532 | nextOffset = currentOffset + memberSize; |
| 4533 | } |
| 4534 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4535 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4536 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4537 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 4538 | switch (glslangBuiltIn) |
| 4539 | { |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4540 | case glslang::EbvPointSize: |
| 4541 | #ifndef GLSLANG_WEB |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4542 | case glslang::EbvClipDistance: |
| 4543 | case glslang::EbvCullDistance: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4544 | case glslang::EbvViewportMaskNV: |
| 4545 | case glslang::EbvSecondaryPositionNV: |
| 4546 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 4547 | case glslang::EbvPositionPerViewNV: |
| 4548 | case glslang::EbvViewportMaskPerViewNV: |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 4549 | case glslang::EbvTaskCountNV: |
| 4550 | case glslang::EbvPrimitiveCountNV: |
| 4551 | case glslang::EbvPrimitiveIndicesNV: |
| 4552 | case glslang::EbvClipDistancePerViewNV: |
| 4553 | case glslang::EbvCullDistancePerViewNV: |
| 4554 | case glslang::EbvLayerPerViewNV: |
| 4555 | case glslang::EbvMeshViewCountNV: |
| 4556 | case glslang::EbvMeshViewIndicesNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 4557 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 4558 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 4559 | // Alternately, we could just call this for any glslang built-in, since the |
| 4560 | // capability already guards against duplicates. |
| 4561 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 4562 | break; |
| 4563 | default: |
| 4564 | // Capabilities were already generated when the struct was declared. |
| 4565 | break; |
| 4566 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 4567 | } |
| 4568 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4569 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4570 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 4571 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4572 | } |
| 4573 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4574 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4575 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 4576 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 4577 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4578 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4579 | assert(qualifier == glslang::EvqIn || |
| 4580 | qualifier == glslang::EvqOut || |
| 4581 | qualifier == glslang::EvqInOut || |
rdb | d8edfd8 | 2020-06-02 08:30:07 +0200 | [diff] [blame] | 4582 | qualifier == glslang::EvqUniform || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4583 | qualifier == glslang::EvqConstReadOnly); |
rdb | d8edfd8 | 2020-06-02 08:30:07 +0200 | [diff] [blame] | 4584 | return qualifier != glslang::EvqConstReadOnly && |
| 4585 | qualifier != glslang::EvqUniform; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | // Is parameter pass-by-original? |
| 4589 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 4590 | bool implicitThisParam) |
| 4591 | { |
| 4592 | if (implicitThisParam) // implicit this |
| 4593 | return true; |
| 4594 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 4595 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4596 | return paramType.containsOpaque() || // sampler, etc. |
| 4597 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 4598 | } |
| 4599 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4600 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 4601 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4602 | { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4603 | const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, |
| 4604 | bool useVulkanMemoryModel) { |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4605 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 4606 | if (paramPrecision != spv::NoPrecision) |
| 4607 | decorations.push_back(paramPrecision); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4608 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 4609 | if (type.isReference()) { |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4610 | // Original and non-writable params pass the pointer directly and |
| 4611 | // use restrict/aliased, others are stored to a pointer in Function |
| 4612 | // memory and use RestrictPointer/AliasedPointer. |
| 4613 | if (originalParam(type.getQualifier().storage, type, false) || |
| 4614 | !writableParam(type.getQualifier().storage)) { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 4615 | decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrict : |
| 4616 | spv::DecorationAliased); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4617 | } else { |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 4618 | decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrictPointerEXT : |
| 4619 | spv::DecorationAliasedPointerEXT); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 4620 | } |
| 4621 | } |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4622 | }; |
| 4623 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4624 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4625 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 4626 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4627 | continue; |
| 4628 | |
| 4629 | // 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] | 4630 | // 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] | 4631 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4632 | // 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] | 4633 | // function. What it is an address of varies: |
| 4634 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4635 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 4636 | // 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] | 4637 | // |
| 4638 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 4639 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4640 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 4641 | // 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] | 4642 | |
| 4643 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4644 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4645 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 4646 | |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4647 | #ifdef ENABLE_HLSL |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4648 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 4649 | glslangIntermediate->implicitThisName; |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 4650 | #else |
| 4651 | bool implicitThis = false; |
| 4652 | #endif |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4653 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4654 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4655 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4656 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4657 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4658 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 4659 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 4660 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4661 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 4662 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 4663 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4664 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4665 | paramTypes.push_back(typeId); |
| 4666 | } |
| 4667 | |
| 4668 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4669 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 4670 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 4671 | glslFunction->getName().c_str(), paramTypes, |
| 4672 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 4673 | if (implicitThis) |
| 4674 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4675 | |
| 4676 | // Track function to emit/call later |
| 4677 | functionMap[glslFunction->getName().c_str()] = function; |
| 4678 | |
| 4679 | // Set the parameter id's |
| 4680 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4681 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 4682 | // give a name too |
| 4683 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4684 | |
| 4685 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4686 | if (paramType.contains8BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4687 | builder.addCapability(spv::CapabilityInt8); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4688 | if (paramType.contains16BitInt()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4689 | builder.addCapability(spv::CapabilityInt16); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 4690 | if (paramType.contains16BitFloat()) |
Jeff Bolz | 2b2316d | 2019-02-17 22:49:28 -0600 | [diff] [blame] | 4691 | builder.addCapability(spv::CapabilityFloat16); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4692 | } |
| 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | // Process all the initializers, while skipping the functions and link objects |
| 4697 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 4698 | { |
| 4699 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 4700 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 4701 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4702 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != |
| 4703 | glslang::EOpLinkerObjects) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4704 | |
| 4705 | // 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] | 4706 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4707 | initializer->traverse(this); |
| 4708 | } |
| 4709 | } |
| 4710 | } |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 4711 | // Walk over all linker objects to create a map for payload and callable data linker objects |
| 4712 | // and their location to be used during codegen for OpTraceKHR and OpExecuteCallableKHR |
| 4713 | // This is done here since it is possible that these linker objects are not be referenced in the AST |
| 4714 | void TGlslangToSpvTraverser::collectRayTracingLinkerObjects() |
| 4715 | { |
| 4716 | glslang::TIntermAggregate* linkerObjects = glslangIntermediate->findLinkerObjects(); |
| 4717 | for (auto& objSeq : linkerObjects->getSequence()) { |
| 4718 | auto objNode = objSeq->getAsSymbolNode(); |
| 4719 | if (objNode != nullptr) { |
| 4720 | if (objNode->getQualifier().hasLocation()) { |
| 4721 | unsigned int location = objNode->getQualifier().layoutLocation; |
| 4722 | auto st = objNode->getQualifier().storage; |
| 4723 | int set; |
| 4724 | switch (st) |
| 4725 | { |
| 4726 | case glslang::EvqPayload: |
| 4727 | case glslang::EvqPayloadIn: |
| 4728 | set = 0; |
| 4729 | break; |
| 4730 | case glslang::EvqCallableData: |
| 4731 | case glslang::EvqCallableDataIn: |
| 4732 | set = 1; |
| 4733 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4734 | |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 4735 | default: |
| 4736 | set = -1; |
| 4737 | } |
| 4738 | if (set != -1) |
| 4739 | locationToSymbol[set].insert(std::make_pair(location, objNode)); |
| 4740 | } |
| 4741 | } |
| 4742 | } |
| 4743 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4744 | // Process all the functions, while skipping initializers. |
| 4745 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4746 | { |
| 4747 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4748 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 4749 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4750 | node->traverse(this); |
| 4751 | } |
| 4752 | } |
| 4753 | |
| 4754 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 4755 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4756 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4757 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 4758 | currentFunction = functionMap[node->getName().c_str()]; |
| 4759 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4760 | builder.setBuildPoint(functionBlock); |
| 4761 | } |
| 4762 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4763 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 4764 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4765 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4766 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4767 | |
| 4768 | glslang::TSampler sampler = {}; |
| 4769 | bool cubeCompare = false; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4770 | #ifndef GLSLANG_WEB |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4771 | bool f16ShadowCompare = false; |
| 4772 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4773 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4774 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 4775 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4776 | #ifndef GLSLANG_WEB |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 4777 | f16ShadowCompare = sampler.shadow && |
| 4778 | glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4779 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4780 | } |
| 4781 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4782 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 4783 | builder.clearAccessChain(); |
| 4784 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4785 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4786 | #ifndef GLSLANG_WEB |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4787 | // Special case l-value operands |
| 4788 | bool lvalue = false; |
| 4789 | switch (node.getOp()) { |
| 4790 | case glslang::EOpImageAtomicAdd: |
| 4791 | case glslang::EOpImageAtomicMin: |
| 4792 | case glslang::EOpImageAtomicMax: |
| 4793 | case glslang::EOpImageAtomicAnd: |
| 4794 | case glslang::EOpImageAtomicOr: |
| 4795 | case glslang::EOpImageAtomicXor: |
| 4796 | case glslang::EOpImageAtomicExchange: |
| 4797 | case glslang::EOpImageAtomicCompSwap: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4798 | case glslang::EOpImageAtomicLoad: |
| 4799 | case glslang::EOpImageAtomicStore: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4800 | if (i == 0) |
| 4801 | lvalue = true; |
| 4802 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 4803 | case glslang::EOpSparseImageLoad: |
| 4804 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 4805 | lvalue = true; |
| 4806 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4807 | case glslang::EOpSparseTexture: |
| 4808 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 4809 | lvalue = true; |
| 4810 | break; |
| 4811 | case glslang::EOpSparseTextureClamp: |
| 4812 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 4813 | lvalue = true; |
| 4814 | break; |
| 4815 | case glslang::EOpSparseTextureLod: |
| 4816 | case glslang::EOpSparseTextureOffset: |
| 4817 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 4818 | lvalue = true; |
| 4819 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4820 | case glslang::EOpSparseTextureFetch: |
| 4821 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 4822 | lvalue = true; |
| 4823 | break; |
| 4824 | case glslang::EOpSparseTextureFetchOffset: |
| 4825 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 4826 | lvalue = true; |
| 4827 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4828 | case glslang::EOpSparseTextureLodOffset: |
| 4829 | case glslang::EOpSparseTextureGrad: |
| 4830 | case glslang::EOpSparseTextureOffsetClamp: |
| 4831 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 4832 | lvalue = true; |
| 4833 | break; |
| 4834 | case glslang::EOpSparseTextureGradOffset: |
| 4835 | case glslang::EOpSparseTextureGradClamp: |
| 4836 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 4837 | lvalue = true; |
| 4838 | break; |
| 4839 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 4840 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 4841 | lvalue = true; |
| 4842 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4843 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4844 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 4845 | lvalue = true; |
| 4846 | break; |
| 4847 | case glslang::EOpSparseTextureGatherOffset: |
| 4848 | case glslang::EOpSparseTextureGatherOffsets: |
| 4849 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 4850 | lvalue = true; |
| 4851 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 4852 | case glslang::EOpSparseTextureGatherLod: |
| 4853 | if (i == 3) |
| 4854 | lvalue = true; |
| 4855 | break; |
| 4856 | case glslang::EOpSparseTextureGatherLodOffset: |
| 4857 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 4858 | if (i == 4) |
| 4859 | lvalue = true; |
| 4860 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 4861 | case glslang::EOpSparseImageLoadLod: |
| 4862 | if (i == 3) |
| 4863 | lvalue = true; |
| 4864 | break; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 4865 | case glslang::EOpImageSampleFootprintNV: |
| 4866 | if (i == 4) |
| 4867 | lvalue = true; |
| 4868 | break; |
| 4869 | case glslang::EOpImageSampleFootprintClampNV: |
| 4870 | case glslang::EOpImageSampleFootprintLodNV: |
| 4871 | if (i == 5) |
| 4872 | lvalue = true; |
| 4873 | break; |
| 4874 | case glslang::EOpImageSampleFootprintGradNV: |
| 4875 | if (i == 6) |
| 4876 | lvalue = true; |
| 4877 | break; |
| 4878 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 4879 | if (i == 7) |
| 4880 | lvalue = true; |
| 4881 | break; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4882 | default: |
| 4883 | break; |
| 4884 | } |
| 4885 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4886 | if (lvalue) { |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 4887 | spv::Id lvalue_id = builder.accessChainGetLValue(); |
| 4888 | arguments.push_back(lvalue_id); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4889 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 4890 | builder.addDecoration(lvalue_id, TranslateNonUniformDecoration(lvalueCoherentFlags)); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4891 | lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
| 4892 | } else |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4893 | #endif |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4894 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4895 | } |
| 4896 | } |
| 4897 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4898 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4899 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4900 | builder.clearAccessChain(); |
| 4901 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4902 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4903 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4904 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4905 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 4906 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4907 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4908 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4909 | |
greg-lunarg | 5d43c4a | 2018-12-07 17:36:33 -0700 | [diff] [blame] | 4910 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 4911 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4912 | // Process a GLSL texturing op (will be SPV image) |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4913 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4914 | const glslang::TType &imageType = node->getAsAggregate() |
| 4915 | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 4916 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 4917 | const glslang::TSampler sampler = imageType.getSampler(); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4918 | #ifdef GLSLANG_WEB |
| 4919 | const bool f16ShadowCompare = false; |
| 4920 | #else |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4921 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4922 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 4923 | : false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 4924 | #endif |
| 4925 | |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 4926 | const auto signExtensionMask = [&]() { |
| 4927 | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
| 4928 | if (sampler.type == glslang::EbtUint) |
| 4929 | return spv::ImageOperandsZeroExtendMask; |
| 4930 | else if (sampler.type == glslang::EbtInt) |
| 4931 | return spv::ImageOperandsSignExtendMask; |
| 4932 | } |
| 4933 | return spv::ImageOperandsMaskNone; |
| 4934 | }; |
| 4935 | |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4936 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 4937 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4938 | std::vector<spv::Id> arguments; |
| 4939 | if (node->getAsAggregate()) |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 4940 | translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4941 | else |
| 4942 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | 12c155f | 2020-06-30 07:52:05 -0600 | [diff] [blame] | 4943 | spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4944 | |
| 4945 | spv::Builder::TextureParameters params = { }; |
| 4946 | params.sampler = arguments[0]; |
| 4947 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4948 | glslang::TCrackedTextureOp cracked; |
| 4949 | node->crackTexture(sampler, cracked); |
| 4950 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 4951 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4952 | |
Bas Nieuwenhuizen | 5806431 | 2020-09-03 03:04:13 +0200 | [diff] [blame] | 4953 | if (builder.isSampledImage(params.sampler) && |
| 4954 | ((cracked.query && node->getOp() != glslang::EOpTextureQueryLod) || cracked.fragMask || cracked.fetch)) { |
| 4955 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4956 | if (imageType.getQualifier().isNonUniform()) { |
| 4957 | builder.addDecoration(params.sampler, spv::DecorationNonUniformEXT); |
| 4958 | } |
| 4959 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4960 | // Check for queries |
| 4961 | if (cracked.query) { |
| 4962 | switch (node->getOp()) { |
| 4963 | case glslang::EOpImageQuerySize: |
| 4964 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4965 | if (arguments.size() > 1) { |
| 4966 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4967 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4968 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4969 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4970 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4971 | case glslang::EOpImageQuerySamples: |
| 4972 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4973 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4974 | case glslang::EOpTextureQueryLod: |
| 4975 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4976 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4977 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 4978 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 4979 | case glslang::EOpSparseTexelsResident: |
| 4980 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 4981 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4982 | default: |
| 4983 | assert(0); |
| 4984 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4985 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4986 | } |
| 4987 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 4988 | int components = node->getType().getVectorSize(); |
| 4989 | |
| 4990 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 4991 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 4992 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 4993 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 4994 | // here around e.g. which ones return scalars or other types. |
| 4995 | components = 4; |
| 4996 | } |
| 4997 | |
| 4998 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 4999 | |
| 5000 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 5001 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5002 | // Check for image functions other than queries |
| 5003 | if (node->isImage()) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5004 | std::vector<spv::IdImmediate> operands; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 5005 | auto opIt = arguments.begin(); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5006 | spv::IdImmediate image = { true, *(opIt++) }; |
| 5007 | operands.push_back(image); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5008 | |
| 5009 | // Handle subpass operations |
| 5010 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 5011 | // built-in function. |
| 5012 | if (cracked.subpass) { |
| 5013 | // add on the (0,0) coordinate |
| 5014 | spv::Id zero = builder.makeIntConstant(0); |
| 5015 | std::vector<spv::Id> comps; |
| 5016 | comps.push_back(zero); |
| 5017 | comps.push_back(zero); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5018 | spv::IdImmediate coord = { true, |
| 5019 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 5020 | operands.push_back(coord); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5021 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
| 5022 | imageOperands.word = imageOperands.word | signExtensionMask(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5023 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5024 | imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
| 5025 | } |
| 5026 | if (imageOperands.word != spv::ImageOperandsMaskNone) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5027 | operands.push_back(imageOperands); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5028 | if (sampler.isMultiSample()) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5029 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 5030 | operands.push_back(imageOperand); |
| 5031 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5032 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 5033 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 5034 | builder.setPrecision(result, precision); |
| 5035 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5036 | } |
| 5037 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5038 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 5039 | operands.push_back(coord); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 5040 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5041 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5042 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5043 | mask = mask | spv::ImageOperandsSampleMask; |
| 5044 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5045 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 5046 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 5047 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5048 | mask = mask | spv::ImageOperandsLodMask; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5049 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5050 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 5051 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5052 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 5053 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5054 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 5055 | operands.push_back(imageOperands); |
| 5056 | } |
| 5057 | if (mask & spv::ImageOperandsSampleMask) { |
| 5058 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5059 | operands.push_back(imageOperand); |
| 5060 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5061 | if (mask & spv::ImageOperandsLodMask) { |
| 5062 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5063 | operands.push_back(imageOperand); |
| 5064 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5065 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5066 | spv::IdImmediate imageOperand = { true, |
| 5067 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5068 | operands.push_back(imageOperand); |
| 5069 | } |
| 5070 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5071 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 5072 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 5073 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5074 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 5075 | builder.setPrecision(result[0], precision); |
| 5076 | |
| 5077 | // If needed, add a conversion constructor to the proper size. |
| 5078 | if (components != node->getType().getVectorSize()) |
| 5079 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 5080 | |
| 5081 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 5082 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 5083 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5084 | // Push the texel value before the operands |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5085 | if (sampler.isMultiSample() || cracked.lod) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5086 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 5087 | operands.push_back(texel); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5088 | } else { |
| 5089 | spv::IdImmediate texel = { true, *opIt }; |
| 5090 | operands.push_back(texel); |
| 5091 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5092 | |
| 5093 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5094 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5095 | mask = mask | spv::ImageOperandsSampleMask; |
| 5096 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5097 | if (cracked.lod) { |
| 5098 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 5099 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 5100 | mask = mask | spv::ImageOperandsLodMask; |
| 5101 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5102 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 5103 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5104 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 5105 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5106 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 5107 | operands.push_back(imageOperands); |
| 5108 | } |
| 5109 | if (mask & spv::ImageOperandsSampleMask) { |
| 5110 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5111 | operands.push_back(imageOperand); |
| 5112 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5113 | if (mask & spv::ImageOperandsLodMask) { |
| 5114 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5115 | operands.push_back(imageOperand); |
| 5116 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5117 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5118 | spv::IdImmediate imageOperand = { true, |
| 5119 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5120 | operands.push_back(imageOperand); |
| 5121 | } |
| 5122 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 5123 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5124 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 5125 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 5126 | return spv::NoResult; |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5127 | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
| 5128 | node->getOp() == glslang::EOpSparseImageLoadLod) { |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 5129 | builder.addCapability(spv::CapabilitySparseResidency); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5130 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 5131 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 5132 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5133 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5134 | if (sampler.isMultiSample()) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5135 | mask = mask | spv::ImageOperandsSampleMask; |
| 5136 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5137 | if (cracked.lod) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 5138 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 5139 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 5140 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5141 | mask = mask | spv::ImageOperandsLodMask; |
| 5142 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5143 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 5144 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5145 | mask = mask | signExtensionMask(); |
John Kessenich | 6e384fe | 2019-05-10 06:47:00 -0600 | [diff] [blame] | 5146 | if (mask != spv::ImageOperandsMaskNone) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5147 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5148 | operands.push_back(imageOperands); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5149 | } |
| 5150 | if (mask & spv::ImageOperandsSampleMask) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5151 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5152 | operands.push_back(imageOperand); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5153 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5154 | if (mask & spv::ImageOperandsLodMask) { |
| 5155 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5156 | operands.push_back(imageOperand); |
| 5157 | } |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5158 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5159 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope( |
| 5160 | TranslateCoherent(imageType))) }; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5161 | operands.push_back(imageOperand); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | // Create the return type that was a special structure |
| 5165 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 5166 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 5167 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 5168 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 5169 | |
| 5170 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 5171 | |
| 5172 | // Decode the return type |
| 5173 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 5174 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 5175 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 5176 | // Process image atomic operations |
| 5177 | |
| 5178 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 5179 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5180 | // For non-MS, the sample value should be 0 |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5181 | spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 5182 | operands.push_back(sample); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5183 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5184 | spv::Id resultTypeId; |
| 5185 | // imageAtomicStore has a void return type so base the pointer type on |
| 5186 | // the type of the value operand. |
| 5187 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
Rex Xu | fb18b6d | 2020-02-22 22:04:31 +0800 | [diff] [blame] | 5188 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(*opIt)); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5189 | } else { |
| 5190 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 5191 | } |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 5192 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Jeff Bolz | 39ffdaf | 2020-03-09 10:48:12 -0500 | [diff] [blame] | 5193 | if (imageType.getQualifier().nonUniform) { |
| 5194 | builder.addDecoration(pointer, spv::DecorationNonUniformEXT); |
| 5195 | } |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5196 | |
| 5197 | std::vector<spv::Id> operands; |
| 5198 | operands.push_back(pointer); |
| 5199 | for (; opIt != arguments.end(); ++opIt) |
| 5200 | operands.push_back(*opIt); |
| 5201 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5202 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), |
| 5203 | lvalueCoherentFlags); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5204 | } |
| 5205 | } |
| 5206 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5207 | #ifndef GLSLANG_WEB |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 5208 | // Check for fragment mask functions other than queries |
| 5209 | if (cracked.fragMask) { |
| 5210 | assert(sampler.ms); |
| 5211 | |
| 5212 | auto opIt = arguments.begin(); |
| 5213 | std::vector<spv::Id> operands; |
| 5214 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 5215 | operands.push_back(params.sampler); |
| 5216 | ++opIt; |
| 5217 | |
| 5218 | if (sampler.isSubpass()) { |
| 5219 | // add on the (0,0) coordinate |
| 5220 | spv::Id zero = builder.makeIntConstant(0); |
| 5221 | std::vector<spv::Id> comps; |
| 5222 | comps.push_back(zero); |
| 5223 | comps.push_back(zero); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5224 | operands.push_back(builder.makeCompositeConstant( |
| 5225 | builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | for (; opIt != arguments.end(); ++opIt) |
| 5229 | operands.push_back(*opIt); |
| 5230 | |
| 5231 | spv::Op fragMaskOp = spv::OpNop; |
| 5232 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 5233 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 5234 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 5235 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 5236 | |
| 5237 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 5238 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 5239 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 5240 | } |
| 5241 | #endif |
| 5242 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5243 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5244 | bool sparse = node->isSparseTexture(); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5245 | bool imageFootprint = node->isImageFootprint(); |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5246 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 5247 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5248 | // check for bias argument |
| 5249 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5250 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5251 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5252 | if (cracked.gather) |
| 5253 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 5254 | |
| 5255 | if (f16ShadowCompare) |
| 5256 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5257 | if (cracked.offset) |
| 5258 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5259 | else if (cracked.offsets) |
| 5260 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5261 | if (cracked.grad) |
| 5262 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5263 | if (cracked.lodClamp) |
| 5264 | ++nonBiasArgCount; |
| 5265 | if (sparse) |
| 5266 | ++nonBiasArgCount; |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5267 | if (imageFootprint) |
| 5268 | //Following three extra arguments |
| 5269 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 5270 | nonBiasArgCount += 3; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5271 | if ((int)arguments.size() > nonBiasArgCount) |
| 5272 | bias = true; |
| 5273 | } |
| 5274 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5275 | #ifndef GLSLANG_WEB |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5276 | if (cracked.gather) { |
| 5277 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 5278 | if (bias || cracked.lod || |
| 5279 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 5280 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 5281 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5282 | } |
| 5283 | } |
| 5284 | #endif |
| 5285 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5286 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5287 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5288 | params.coords = arguments[1]; |
| 5289 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 5290 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5291 | |
| 5292 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 5293 | if (cubeCompare || f16ShadowCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5294 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5295 | ++extraArgs; |
| 5296 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5297 | params.Dref = arguments[2]; |
| 5298 | ++extraArgs; |
| 5299 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5300 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5301 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5302 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5303 | 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] | 5304 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5305 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 5306 | indexes.push_back(dRefComp); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5307 | params.Dref = builder.createCompositeExtract(params.coords, |
| 5308 | builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5309 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5310 | |
| 5311 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5312 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 5313 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5314 | ++extraArgs; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5315 | } else if (glslangIntermediate->getStage() != EShLangFragment && |
| 5316 | !(glslangIntermediate->getStage() == EShLangCompute && |
| 5317 | glslangIntermediate->hasLayoutDerivativeModeNone())) { |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 5318 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 5319 | noImplicitLod = true; |
| 5320 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5321 | |
| 5322 | // multisample |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5323 | if (sampler.isMultiSample()) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 5324 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5325 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5326 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5327 | |
| 5328 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5329 | if (cracked.grad) { |
| 5330 | params.gradX = arguments[2 + extraArgs]; |
| 5331 | params.gradY = arguments[3 + extraArgs]; |
| 5332 | extraArgs += 2; |
| 5333 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5334 | |
| 5335 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5336 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5337 | params.offset = arguments[2 + extraArgs]; |
| 5338 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5339 | } else if (cracked.offsets) { |
| 5340 | params.offsets = arguments[2 + extraArgs]; |
| 5341 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5342 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5343 | |
John Kessenich | 3e4b6ff | 2019-08-08 01:15:24 -0600 | [diff] [blame] | 5344 | #ifndef GLSLANG_WEB |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5345 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5346 | if (cracked.lodClamp) { |
| 5347 | params.lodClamp = arguments[2 + extraArgs]; |
| 5348 | ++extraArgs; |
| 5349 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5350 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 5351 | if (sparse) { |
| 5352 | params.texelOut = arguments[2 + extraArgs]; |
| 5353 | ++extraArgs; |
| 5354 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5355 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5356 | if (cracked.gather && ! sampler.shadow) { |
| 5357 | // default component is 0, if missing, otherwise an argument |
| 5358 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5359 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5360 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5361 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 5362 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5363 | } |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5364 | spv::Id resultStruct = spv::NoResult; |
| 5365 | if (imageFootprint) { |
| 5366 | //Following three extra arguments |
| 5367 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 5368 | params.granularity = arguments[2 + extraArgs]; |
| 5369 | params.coarse = arguments[3 + extraArgs]; |
| 5370 | resultStruct = arguments[4 + extraArgs]; |
| 5371 | extraArgs += 3; |
| 5372 | } |
| 5373 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 5374 | // bias |
| 5375 | if (bias) { |
| 5376 | params.bias = arguments[2 + extraArgs]; |
| 5377 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5378 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 5379 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 5380 | #ifndef GLSLANG_WEB |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5381 | if (imageFootprint) { |
| 5382 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 5383 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 5384 | |
| 5385 | |
| 5386 | //resultStructType(OpenGL type) contains 5 elements: |
| 5387 | //struct gl_TextureFootprint2DNV { |
| 5388 | // uvec2 anchor; |
| 5389 | // uvec2 offset; |
| 5390 | // uvec2 mask; |
| 5391 | // uint lod; |
| 5392 | // uint granularity; |
| 5393 | //}; |
| 5394 | //or |
| 5395 | //struct gl_TextureFootprint3DNV { |
| 5396 | // uvec3 anchor; |
| 5397 | // uvec3 offset; |
| 5398 | // uvec2 mask; |
| 5399 | // uint lod; |
| 5400 | // uint granularity; |
| 5401 | //}; |
| 5402 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 5403 | assert(builder.isStructType(resultStructType)); |
| 5404 | |
| 5405 | //resType (SPIR-V type) contains 6 elements: |
| 5406 | //Member 0 must be a Boolean type scalar(LOD), |
| 5407 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 5408 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 5409 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 5410 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 5411 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 5412 | std::vector<spv::Id> members; |
| 5413 | members.push_back(resultType()); |
| 5414 | for (int i = 0; i < 5; i++) { |
| 5415 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 5416 | } |
| 5417 | spv::Id resType = builder.makeStructType(members, "ResType"); |
| 5418 | |
| 5419 | //call ImageFootprintNV |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5420 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
| 5421 | cracked.gather, noImplicitLod, params, signExtensionMask()); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5422 | |
| 5423 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 5424 | for (int i = 0; i < 5; i++) { |
| 5425 | builder.clearAccessChain(); |
| 5426 | builder.setAccessChainLValue(resultStruct); |
| 5427 | |
| 5428 | //Accessing to a struct we created, no coherent flag is set |
| 5429 | spv::Builder::AccessChain::CoherentFlags flags; |
| 5430 | flags.clear(); |
| 5431 | |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 5432 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5433 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), |
Bas Nieuwenhuizen | de949a2 | 2020-08-24 23:27:26 +0200 | [diff] [blame] | 5434 | i+1), TranslateNonUniformDecoration(imageType.getQualifier())); |
Chao Chen | 3a13796 | 2018-09-19 11:41:27 -0700 | [diff] [blame] | 5435 | } |
| 5436 | return builder.createCompositeExtract(res, resultType(), 0); |
| 5437 | } |
| 5438 | #endif |
| 5439 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5440 | // projective component (might not to move) |
| 5441 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 5442 | // are divided by the last component of P." |
| 5443 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 5444 | // unused components will appear after all used components." |
| 5445 | if (cracked.proj) { |
| 5446 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 5447 | int projTargetComp; |
| 5448 | switch (sampler.dim) { |
| 5449 | case glslang::Esd1D: projTargetComp = 1; break; |
| 5450 | case glslang::Esd2D: projTargetComp = 2; break; |
| 5451 | case glslang::EsdRect: projTargetComp = 2; break; |
| 5452 | default: projTargetComp = projSourceComp; break; |
| 5453 | } |
| 5454 | // copy the projective coordinate if we have to |
| 5455 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 5456 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5457 | builder.getScalarTypeId(builder.getTypeId(params.coords)), projSourceComp); |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5458 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5459 | builder.getTypeId(params.coords), projTargetComp); |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 5460 | } |
| 5461 | } |
| 5462 | |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 5463 | #ifndef GLSLANG_WEB |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5464 | // nonprivate |
| 5465 | if (imageType.getQualifier().nonprivate) { |
| 5466 | params.nonprivate = true; |
| 5467 | } |
| 5468 | |
| 5469 | // volatile |
| 5470 | if (imageType.getQualifier().volatil) { |
| 5471 | params.volatil = true; |
| 5472 | } |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 5473 | #endif |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 5474 | |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 5475 | std::vector<spv::Id> result( 1, |
John Kessenich | f43c739 | 2019-03-31 10:51:57 -0600 | [diff] [blame] | 5476 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
| 5477 | noImplicitLod, params, signExtensionMask()) |
St0fF | a1184dd | 2018-04-09 21:08:14 +0200 | [diff] [blame] | 5478 | ); |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 5479 | |
| 5480 | if (components != node->getType().getVectorSize()) |
| 5481 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 5482 | |
| 5483 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 5487 | { |
| 5488 | // Grab the function's pointer from the previously created function |
| 5489 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 5490 | if (! function) |
| 5491 | return 0; |
| 5492 | |
| 5493 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 5494 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 5495 | |
| 5496 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 5497 | // |
| 5498 | // These imply we need a four step process: |
| 5499 | // 1. Evaluate the arguments |
| 5500 | // 2. Allocate and make copies of in, out, and inout arguments |
| 5501 | // 3. Make the call |
| 5502 | // 4. Copy back the results |
| 5503 | |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5504 | // 1. Evaluate the arguments and their types |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5505 | std::vector<spv::Builder::AccessChain> lValues; |
| 5506 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5507 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5508 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5509 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5510 | // build l-value |
| 5511 | builder.clearAccessChain(); |
| 5512 | glslangArgs[a]->traverse(this); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5513 | // 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] | 5514 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 5515 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5516 | // save l-value |
| 5517 | lValues.push_back(builder.getAccessChain()); |
| 5518 | } else { |
| 5519 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5520 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5521 | } |
| 5522 | } |
| 5523 | |
| 5524 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 5525 | // copy the original into that space. |
| 5526 | // |
| 5527 | // Also, build up the list of actual arguments to pass in for the call |
| 5528 | int lValueCount = 0; |
| 5529 | int rValueCount = 0; |
| 5530 | std::vector<spv::Id> spvArgs; |
| 5531 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 5532 | spv::Id arg; |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5533 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 5534 | builder.setAccessChain(lValues[lValueCount]); |
| 5535 | arg = builder.accessChainGetLValue(); |
| 5536 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5537 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5538 | // need space to hold the copy |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5539 | arg = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5540 | builder.getContainedTypeId(function->getParamType(a)), "param"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5541 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 5542 | // need to copy the input into output space |
| 5543 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5544 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 5545 | builder.clearAccessChain(); |
| 5546 | builder.setAccessChainLValue(arg); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5547 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5548 | } |
| 5549 | ++lValueCount; |
| 5550 | } else { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5551 | // process r-value, which involves a copy for a type mismatch |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 5552 | if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a]) || |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5553 | TranslatePrecisionDecoration(*argTypes[a]) != function->getParamPrecision(a)) |
John Kessenich | 4df1033 | 2020-06-26 08:37:06 -0600 | [diff] [blame] | 5554 | { |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5555 | 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] | 5556 | builder.clearAccessChain(); |
| 5557 | builder.setAccessChainLValue(argCopy); |
| 5558 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5559 | arg = builder.createLoad(argCopy, function->getParamPrecision(a)); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5560 | } else |
| 5561 | arg = rValues[rValueCount]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5562 | ++rValueCount; |
| 5563 | } |
| 5564 | spvArgs.push_back(arg); |
| 5565 | } |
| 5566 | |
| 5567 | // 3. Make the call. |
| 5568 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5569 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 5570 | builder.addDecoration(result, TranslateNonUniformDecoration(node->getType().getQualifier())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5571 | |
| 5572 | // 4. Copy back out an "out" arguments. |
| 5573 | lValueCount = 0; |
| 5574 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5575 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 5576 | ++lValueCount; |
| 5577 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5578 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
John Kessenich | 435dd80 | 2020-06-30 01:27:08 -0600 | [diff] [blame] | 5579 | spv::Id copy = builder.createLoad(spvArgs[a], spv::NoPrecision); |
greg-lunarg | 639f546 | 2020-11-12 11:10:07 -0700 | [diff] [blame] | 5580 | builder.addDecoration(copy, TranslateNonUniformDecoration(argTypes[a]->getQualifier())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5581 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | d3ed90b | 2018-05-04 11:43:03 -0600 | [diff] [blame] | 5582 | multiTypeStore(*argTypes[a], copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5583 | } |
| 5584 | ++lValueCount; |
| 5585 | } |
| 5586 | } |
| 5587 | |
| 5588 | return result; |
| 5589 | } |
| 5590 | |
| 5591 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5592 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5593 | spv::Id typeId, spv::Id left, spv::Id right, |
| 5594 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 5595 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5596 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5597 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5598 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5599 | |
| 5600 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5601 | 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] | 5602 | bool comparison = false; |
| 5603 | |
| 5604 | switch (op) { |
| 5605 | case glslang::EOpAdd: |
| 5606 | case glslang::EOpAddAssign: |
| 5607 | if (isFloat) |
| 5608 | binOp = spv::OpFAdd; |
| 5609 | else |
| 5610 | binOp = spv::OpIAdd; |
| 5611 | break; |
| 5612 | case glslang::EOpSub: |
| 5613 | case glslang::EOpSubAssign: |
| 5614 | if (isFloat) |
| 5615 | binOp = spv::OpFSub; |
| 5616 | else |
| 5617 | binOp = spv::OpISub; |
| 5618 | break; |
| 5619 | case glslang::EOpMul: |
| 5620 | case glslang::EOpMulAssign: |
| 5621 | if (isFloat) |
| 5622 | binOp = spv::OpFMul; |
| 5623 | else |
| 5624 | binOp = spv::OpIMul; |
| 5625 | break; |
| 5626 | case glslang::EOpVectorTimesScalar: |
| 5627 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5628 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5629 | if (builder.isVector(right)) |
| 5630 | std::swap(left, right); |
| 5631 | assert(builder.isScalar(right)); |
| 5632 | needMatchingVectors = false; |
| 5633 | binOp = spv::OpVectorTimesScalar; |
t.jung | 697fdf0 | 2018-11-14 13:04:39 +0100 | [diff] [blame] | 5634 | } else if (isFloat) |
| 5635 | binOp = spv::OpFMul; |
| 5636 | else |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5637 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5638 | break; |
| 5639 | case glslang::EOpVectorTimesMatrix: |
| 5640 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5641 | binOp = spv::OpVectorTimesMatrix; |
| 5642 | break; |
| 5643 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5644 | binOp = spv::OpMatrixTimesVector; |
| 5645 | break; |
| 5646 | case glslang::EOpMatrixTimesScalar: |
| 5647 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5648 | binOp = spv::OpMatrixTimesScalar; |
| 5649 | break; |
| 5650 | case glslang::EOpMatrixTimesMatrix: |
| 5651 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5652 | binOp = spv::OpMatrixTimesMatrix; |
| 5653 | break; |
| 5654 | case glslang::EOpOuterProduct: |
| 5655 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5656 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5657 | break; |
| 5658 | |
| 5659 | case glslang::EOpDiv: |
| 5660 | case glslang::EOpDivAssign: |
| 5661 | if (isFloat) |
| 5662 | binOp = spv::OpFDiv; |
| 5663 | else if (isUnsigned) |
| 5664 | binOp = spv::OpUDiv; |
| 5665 | else |
| 5666 | binOp = spv::OpSDiv; |
| 5667 | break; |
| 5668 | case glslang::EOpMod: |
| 5669 | case glslang::EOpModAssign: |
| 5670 | if (isFloat) |
| 5671 | binOp = spv::OpFMod; |
| 5672 | else if (isUnsigned) |
| 5673 | binOp = spv::OpUMod; |
| 5674 | else |
| 5675 | binOp = spv::OpSMod; |
| 5676 | break; |
| 5677 | case glslang::EOpRightShift: |
| 5678 | case glslang::EOpRightShiftAssign: |
| 5679 | if (isUnsigned) |
| 5680 | binOp = spv::OpShiftRightLogical; |
| 5681 | else |
| 5682 | binOp = spv::OpShiftRightArithmetic; |
| 5683 | break; |
| 5684 | case glslang::EOpLeftShift: |
| 5685 | case glslang::EOpLeftShiftAssign: |
| 5686 | binOp = spv::OpShiftLeftLogical; |
| 5687 | break; |
| 5688 | case glslang::EOpAnd: |
| 5689 | case glslang::EOpAndAssign: |
| 5690 | binOp = spv::OpBitwiseAnd; |
| 5691 | break; |
| 5692 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5693 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5694 | binOp = spv::OpLogicalAnd; |
| 5695 | break; |
| 5696 | case glslang::EOpInclusiveOr: |
| 5697 | case glslang::EOpInclusiveOrAssign: |
| 5698 | binOp = spv::OpBitwiseOr; |
| 5699 | break; |
| 5700 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5701 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5702 | binOp = spv::OpLogicalOr; |
| 5703 | break; |
| 5704 | case glslang::EOpExclusiveOr: |
| 5705 | case glslang::EOpExclusiveOrAssign: |
| 5706 | binOp = spv::OpBitwiseXor; |
| 5707 | break; |
| 5708 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 5709 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5710 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5711 | break; |
| 5712 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 5713 | case glslang::EOpAbsDifference: |
| 5714 | binOp = isUnsigned ? spv::OpAbsUSubINTEL : spv::OpAbsISubINTEL; |
| 5715 | break; |
| 5716 | |
| 5717 | case glslang::EOpAddSaturate: |
| 5718 | binOp = isUnsigned ? spv::OpUAddSatINTEL : spv::OpIAddSatINTEL; |
| 5719 | break; |
| 5720 | |
| 5721 | case glslang::EOpSubSaturate: |
| 5722 | binOp = isUnsigned ? spv::OpUSubSatINTEL : spv::OpISubSatINTEL; |
| 5723 | break; |
| 5724 | |
| 5725 | case glslang::EOpAverage: |
| 5726 | binOp = isUnsigned ? spv::OpUAverageINTEL : spv::OpIAverageINTEL; |
| 5727 | break; |
| 5728 | |
| 5729 | case glslang::EOpAverageRounded: |
| 5730 | binOp = isUnsigned ? spv::OpUAverageRoundedINTEL : spv::OpIAverageRoundedINTEL; |
| 5731 | break; |
| 5732 | |
| 5733 | case glslang::EOpMul32x16: |
| 5734 | binOp = isUnsigned ? spv::OpUMul32x16INTEL : spv::OpIMul32x16INTEL; |
| 5735 | break; |
| 5736 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5737 | case glslang::EOpLessThan: |
| 5738 | case glslang::EOpGreaterThan: |
| 5739 | case glslang::EOpLessThanEqual: |
| 5740 | case glslang::EOpGreaterThanEqual: |
| 5741 | case glslang::EOpEqual: |
| 5742 | case glslang::EOpNotEqual: |
| 5743 | case glslang::EOpVectorEqual: |
| 5744 | case glslang::EOpVectorNotEqual: |
| 5745 | comparison = true; |
| 5746 | break; |
| 5747 | default: |
| 5748 | break; |
| 5749 | } |
| 5750 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5751 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5752 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5753 | assert(comparison == false); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5754 | if (builder.isMatrix(left) || builder.isMatrix(right) || |
| 5755 | builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5756 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5757 | |
| 5758 | // 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] | 5759 | if (needMatchingVectors) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5760 | builder.promoteScalar(decorations.precision, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5761 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5762 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5763 | decorations.addNoContraction(builder, result); |
| 5764 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5765 | return builder.setPrecision(result, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | if (! comparison) |
| 5769 | return 0; |
| 5770 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5771 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5772 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 5773 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5774 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 5775 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5776 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5777 | return result; |
| 5778 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5779 | |
| 5780 | switch (op) { |
| 5781 | case glslang::EOpLessThan: |
| 5782 | if (isFloat) |
| 5783 | binOp = spv::OpFOrdLessThan; |
| 5784 | else if (isUnsigned) |
| 5785 | binOp = spv::OpULessThan; |
| 5786 | else |
| 5787 | binOp = spv::OpSLessThan; |
| 5788 | break; |
| 5789 | case glslang::EOpGreaterThan: |
| 5790 | if (isFloat) |
| 5791 | binOp = spv::OpFOrdGreaterThan; |
| 5792 | else if (isUnsigned) |
| 5793 | binOp = spv::OpUGreaterThan; |
| 5794 | else |
| 5795 | binOp = spv::OpSGreaterThan; |
| 5796 | break; |
| 5797 | case glslang::EOpLessThanEqual: |
| 5798 | if (isFloat) |
| 5799 | binOp = spv::OpFOrdLessThanEqual; |
| 5800 | else if (isUnsigned) |
| 5801 | binOp = spv::OpULessThanEqual; |
| 5802 | else |
| 5803 | binOp = spv::OpSLessThanEqual; |
| 5804 | break; |
| 5805 | case glslang::EOpGreaterThanEqual: |
| 5806 | if (isFloat) |
| 5807 | binOp = spv::OpFOrdGreaterThanEqual; |
| 5808 | else if (isUnsigned) |
| 5809 | binOp = spv::OpUGreaterThanEqual; |
| 5810 | else |
| 5811 | binOp = spv::OpSGreaterThanEqual; |
| 5812 | break; |
| 5813 | case glslang::EOpEqual: |
| 5814 | case glslang::EOpVectorEqual: |
| 5815 | if (isFloat) |
| 5816 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5817 | else if (isBool) |
| 5818 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5819 | else |
| 5820 | binOp = spv::OpIEqual; |
| 5821 | break; |
| 5822 | case glslang::EOpNotEqual: |
| 5823 | case glslang::EOpVectorNotEqual: |
| 5824 | if (isFloat) |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 5825 | binOp = spv::OpFUnordNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 5826 | else if (isBool) |
| 5827 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5828 | else |
| 5829 | binOp = spv::OpINotEqual; |
| 5830 | break; |
| 5831 | default: |
| 5832 | break; |
| 5833 | } |
| 5834 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5835 | if (binOp != spv::OpNop) { |
| 5836 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5837 | decorations.addNoContraction(builder, result); |
| 5838 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5839 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5840 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5841 | |
| 5842 | return 0; |
| 5843 | } |
| 5844 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5845 | // |
| 5846 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 5847 | // These can be any of: |
| 5848 | // |
| 5849 | // matrix * scalar |
| 5850 | // scalar * matrix |
| 5851 | // matrix * matrix linear algebraic |
| 5852 | // matrix * vector |
| 5853 | // vector * matrix |
| 5854 | // matrix * matrix componentwise |
| 5855 | // matrix op matrix op in {+, -, /} |
| 5856 | // matrix op scalar op in {+, -, /} |
| 5857 | // scalar op matrix op in {+, -, /} |
| 5858 | // |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5859 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5860 | spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5861 | { |
| 5862 | bool firstClass = true; |
| 5863 | |
| 5864 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 5865 | switch (op) { |
| 5866 | case spv::OpFDiv: |
| 5867 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 5868 | // turn matrix / scalar into a multiply... |
Neil Roberts | eddb131 | 2018-03-13 10:57:59 +0100 | [diff] [blame] | 5869 | spv::Id resultType = builder.getTypeId(right); |
| 5870 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5871 | op = spv::OpMatrixTimesScalar; |
| 5872 | } else |
| 5873 | firstClass = false; |
| 5874 | break; |
| 5875 | case spv::OpMatrixTimesScalar: |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5876 | if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5877 | std::swap(left, right); |
| 5878 | assert(builder.isScalar(right)); |
| 5879 | break; |
| 5880 | case spv::OpVectorTimesMatrix: |
| 5881 | assert(builder.isVector(left)); |
| 5882 | assert(builder.isMatrix(right)); |
| 5883 | break; |
| 5884 | case spv::OpMatrixTimesVector: |
| 5885 | assert(builder.isMatrix(left)); |
| 5886 | assert(builder.isVector(right)); |
| 5887 | break; |
| 5888 | case spv::OpMatrixTimesMatrix: |
| 5889 | assert(builder.isMatrix(left)); |
| 5890 | assert(builder.isMatrix(right)); |
| 5891 | break; |
| 5892 | default: |
| 5893 | firstClass = false; |
| 5894 | break; |
| 5895 | } |
| 5896 | |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 5897 | if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 5898 | firstClass = true; |
| 5899 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5900 | if (firstClass) { |
| 5901 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5902 | decorations.addNoContraction(builder, result); |
| 5903 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5904 | return builder.setPrecision(result, decorations.precision); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5905 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5906 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5907 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5908 | // The result type of all of them is the same type as the (a) matrix operand. |
| 5909 | // The algorithm is to: |
| 5910 | // - break the matrix(es) into vectors |
| 5911 | // - smear any scalar to a vector |
| 5912 | // - do vector operations |
| 5913 | // - make a matrix out the vector results |
| 5914 | switch (op) { |
| 5915 | case spv::OpFAdd: |
| 5916 | case spv::OpFSub: |
| 5917 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 5918 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5919 | case spv::OpFMul: |
| 5920 | { |
| 5921 | // one time set up... |
| 5922 | bool leftMat = builder.isMatrix(left); |
| 5923 | bool rightMat = builder.isMatrix(right); |
| 5924 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 5925 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 5926 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 5927 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 5928 | std::vector<spv::Id> results; |
| 5929 | spv::Id smearVec = spv::NoResult; |
| 5930 | if (builder.isScalar(left)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5931 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5932 | else if (builder.isScalar(right)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5933 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5934 | |
| 5935 | // do each vector op |
| 5936 | for (unsigned int c = 0; c < numCols; ++c) { |
| 5937 | std::vector<unsigned int> indexes; |
| 5938 | indexes.push_back(c); |
| 5939 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 5940 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5941 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5942 | decorations.addNoContraction(builder, result); |
| 5943 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5944 | results.push_back(builder.setPrecision(result, decorations.precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5945 | } |
| 5946 | |
| 5947 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5948 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 5949 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5950 | return result; |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 5951 | } |
| 5952 | default: |
| 5953 | assert(0); |
| 5954 | return spv::NoResult; |
| 5955 | } |
| 5956 | } |
| 5957 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5958 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 5959 | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5960 | { |
| 5961 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5962 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5963 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5964 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5965 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5966 | |
| 5967 | switch (op) { |
| 5968 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5969 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5970 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5971 | if (builder.isMatrixType(typeId)) |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 5972 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 5973 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5974 | unaryOp = spv::OpSNegate; |
| 5975 | break; |
| 5976 | |
| 5977 | case glslang::EOpLogicalNot: |
| 5978 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5979 | unaryOp = spv::OpLogicalNot; |
| 5980 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5981 | case glslang::EOpBitwiseNot: |
| 5982 | unaryOp = spv::OpNot; |
| 5983 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5984 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5985 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5986 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5987 | break; |
| 5988 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5989 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5990 | break; |
| 5991 | case glslang::EOpTranspose: |
| 5992 | unaryOp = spv::OpTranspose; |
| 5993 | break; |
| 5994 | |
| 5995 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5996 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5997 | break; |
| 5998 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5999 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6000 | break; |
| 6001 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6002 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6003 | break; |
| 6004 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6005 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6006 | break; |
| 6007 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6008 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6009 | break; |
| 6010 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6011 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6012 | break; |
| 6013 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6014 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6015 | break; |
| 6016 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6017 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6018 | break; |
| 6019 | |
| 6020 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6021 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6022 | break; |
| 6023 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6024 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6025 | break; |
| 6026 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6027 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6028 | break; |
| 6029 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6030 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6031 | break; |
| 6032 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6033 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6034 | break; |
| 6035 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6036 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6037 | break; |
| 6038 | |
| 6039 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6040 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6041 | break; |
| 6042 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6043 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6044 | break; |
| 6045 | |
| 6046 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6047 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6048 | break; |
| 6049 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6050 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6051 | break; |
| 6052 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6053 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6054 | break; |
| 6055 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6056 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6057 | break; |
| 6058 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6059 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6060 | break; |
| 6061 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6062 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6063 | break; |
| 6064 | |
| 6065 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6066 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6067 | break; |
| 6068 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6069 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6070 | break; |
| 6071 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6072 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6073 | break; |
| 6074 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6075 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6076 | break; |
| 6077 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6078 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6079 | break; |
| 6080 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6081 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6082 | break; |
| 6083 | |
| 6084 | case glslang::EOpIsNan: |
| 6085 | unaryOp = spv::OpIsNan; |
| 6086 | break; |
| 6087 | case glslang::EOpIsInf: |
| 6088 | unaryOp = spv::OpIsInf; |
| 6089 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 6090 | case glslang::EOpIsFinite: |
| 6091 | unaryOp = spv::OpIsFinite; |
| 6092 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6093 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 6094 | case glslang::EOpFloatBitsToInt: |
| 6095 | case glslang::EOpFloatBitsToUint: |
| 6096 | case glslang::EOpIntBitsToFloat: |
| 6097 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6098 | case glslang::EOpDoubleBitsToInt64: |
| 6099 | case glslang::EOpDoubleBitsToUint64: |
| 6100 | case glslang::EOpInt64BitsToDouble: |
| 6101 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6102 | case glslang::EOpFloat16BitsToInt16: |
| 6103 | case glslang::EOpFloat16BitsToUint16: |
| 6104 | case glslang::EOpInt16BitsToFloat16: |
| 6105 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 6106 | unaryOp = spv::OpBitcast; |
| 6107 | break; |
| 6108 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6109 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6110 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6111 | break; |
| 6112 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6113 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6114 | break; |
| 6115 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6116 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6117 | break; |
| 6118 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6119 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6120 | break; |
| 6121 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6122 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6123 | break; |
| 6124 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6125 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6126 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6127 | #ifndef GLSLANG_WEB |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6128 | case glslang::EOpPackSnorm4x8: |
| 6129 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 6130 | break; |
| 6131 | case glslang::EOpUnpackSnorm4x8: |
| 6132 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 6133 | break; |
| 6134 | case glslang::EOpPackUnorm4x8: |
| 6135 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 6136 | break; |
| 6137 | case glslang::EOpUnpackUnorm4x8: |
| 6138 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 6139 | break; |
| 6140 | case glslang::EOpPackDouble2x32: |
| 6141 | libCall = spv::GLSLstd450PackDouble2x32; |
| 6142 | break; |
| 6143 | case glslang::EOpUnpackDouble2x32: |
| 6144 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 6145 | break; |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6146 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6147 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6148 | case glslang::EOpPackInt2x32: |
| 6149 | case glslang::EOpUnpackInt2x32: |
| 6150 | case glslang::EOpPackUint2x32: |
| 6151 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6152 | case glslang::EOpPack16: |
| 6153 | case glslang::EOpPack32: |
| 6154 | case glslang::EOpPack64: |
| 6155 | case glslang::EOpUnpack32: |
| 6156 | case glslang::EOpUnpack16: |
| 6157 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6158 | case glslang::EOpPackInt2x16: |
| 6159 | case glslang::EOpUnpackInt2x16: |
| 6160 | case glslang::EOpPackUint2x16: |
| 6161 | case glslang::EOpUnpackUint2x16: |
| 6162 | case glslang::EOpPackInt4x16: |
| 6163 | case glslang::EOpUnpackInt4x16: |
| 6164 | case glslang::EOpPackUint4x16: |
| 6165 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6166 | case glslang::EOpPackFloat2x16: |
| 6167 | case glslang::EOpUnpackFloat2x16: |
| 6168 | unaryOp = spv::OpBitcast; |
| 6169 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6170 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6171 | case glslang::EOpDPdx: |
| 6172 | unaryOp = spv::OpDPdx; |
| 6173 | break; |
| 6174 | case glslang::EOpDPdy: |
| 6175 | unaryOp = spv::OpDPdy; |
| 6176 | break; |
| 6177 | case glslang::EOpFwidth: |
| 6178 | unaryOp = spv::OpFwidth; |
| 6179 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6180 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6181 | case glslang::EOpAny: |
| 6182 | unaryOp = spv::OpAny; |
| 6183 | break; |
| 6184 | case glslang::EOpAll: |
| 6185 | unaryOp = spv::OpAll; |
| 6186 | break; |
| 6187 | |
| 6188 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6189 | if (isFloat) |
| 6190 | libCall = spv::GLSLstd450FAbs; |
| 6191 | else |
| 6192 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6193 | break; |
| 6194 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6195 | if (isFloat) |
| 6196 | libCall = spv::GLSLstd450FSign; |
| 6197 | else |
| 6198 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6199 | break; |
| 6200 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6201 | #ifndef GLSLANG_WEB |
| 6202 | case glslang::EOpDPdxFine: |
| 6203 | unaryOp = spv::OpDPdxFine; |
| 6204 | break; |
| 6205 | case glslang::EOpDPdyFine: |
| 6206 | unaryOp = spv::OpDPdyFine; |
| 6207 | break; |
| 6208 | case glslang::EOpFwidthFine: |
| 6209 | unaryOp = spv::OpFwidthFine; |
| 6210 | break; |
| 6211 | case glslang::EOpDPdxCoarse: |
| 6212 | unaryOp = spv::OpDPdxCoarse; |
| 6213 | break; |
| 6214 | case glslang::EOpDPdyCoarse: |
| 6215 | unaryOp = spv::OpDPdyCoarse; |
| 6216 | break; |
| 6217 | case glslang::EOpFwidthCoarse: |
| 6218 | unaryOp = spv::OpFwidthCoarse; |
| 6219 | break; |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 6220 | case glslang::EOpRayQueryProceed: |
| 6221 | unaryOp = spv::OpRayQueryProceedKHR; |
| 6222 | break; |
| 6223 | case glslang::EOpRayQueryGetRayTMin: |
| 6224 | unaryOp = spv::OpRayQueryGetRayTMinKHR; |
| 6225 | break; |
| 6226 | case glslang::EOpRayQueryGetRayFlags: |
| 6227 | unaryOp = spv::OpRayQueryGetRayFlagsKHR; |
| 6228 | break; |
| 6229 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 6230 | unaryOp = spv::OpRayQueryGetWorldRayOriginKHR; |
| 6231 | break; |
| 6232 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 6233 | unaryOp = spv::OpRayQueryGetWorldRayDirectionKHR; |
| 6234 | break; |
| 6235 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 6236 | unaryOp = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
| 6237 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6238 | case glslang::EOpInterpolateAtCentroid: |
| 6239 | if (typeProxy == glslang::EbtFloat16) |
| 6240 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6241 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 6242 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6243 | case glslang::EOpAtomicCounterIncrement: |
| 6244 | case glslang::EOpAtomicCounterDecrement: |
| 6245 | case glslang::EOpAtomicCounter: |
| 6246 | { |
| 6247 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 6248 | std::vector<spv::Id> operands; |
| 6249 | operands.push_back(operand); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 6250 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6251 | } |
| 6252 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6253 | case glslang::EOpBitFieldReverse: |
| 6254 | unaryOp = spv::OpBitReverse; |
| 6255 | break; |
| 6256 | case glslang::EOpBitCount: |
| 6257 | unaryOp = spv::OpBitCount; |
| 6258 | break; |
| 6259 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6260 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6261 | break; |
| 6262 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6263 | if (isUnsigned) |
| 6264 | libCall = spv::GLSLstd450FindUMsb; |
| 6265 | else |
| 6266 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 6267 | break; |
| 6268 | |
Ian Romanick | b3bd402 | 2019-01-21 08:57:25 -0800 | [diff] [blame] | 6269 | case glslang::EOpCountLeadingZeros: |
| 6270 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 6271 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 6272 | unaryOp = spv::OpUCountLeadingZerosINTEL; |
| 6273 | break; |
| 6274 | |
| 6275 | case glslang::EOpCountTrailingZeros: |
| 6276 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 6277 | builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 6278 | unaryOp = spv::OpUCountTrailingZerosINTEL; |
| 6279 | break; |
| 6280 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 6281 | case glslang::EOpBallot: |
| 6282 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6283 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6284 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6285 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6286 | case glslang::EOpMinInvocations: |
| 6287 | case glslang::EOpMaxInvocations: |
| 6288 | case glslang::EOpAddInvocations: |
| 6289 | case glslang::EOpMinInvocationsNonUniform: |
| 6290 | case glslang::EOpMaxInvocationsNonUniform: |
| 6291 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 6292 | case glslang::EOpMinInvocationsInclusiveScan: |
| 6293 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 6294 | case glslang::EOpAddInvocationsInclusiveScan: |
| 6295 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6296 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6297 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6298 | case glslang::EOpMinInvocationsExclusiveScan: |
| 6299 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 6300 | case glslang::EOpAddInvocationsExclusiveScan: |
| 6301 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6302 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6303 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6304 | { |
| 6305 | std::vector<spv::Id> operands; |
| 6306 | operands.push_back(operand); |
| 6307 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 6308 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6309 | case glslang::EOpSubgroupAll: |
| 6310 | case glslang::EOpSubgroupAny: |
| 6311 | case glslang::EOpSubgroupAllEqual: |
| 6312 | case glslang::EOpSubgroupBroadcastFirst: |
| 6313 | case glslang::EOpSubgroupBallot: |
| 6314 | case glslang::EOpSubgroupInverseBallot: |
| 6315 | case glslang::EOpSubgroupBallotBitCount: |
| 6316 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6317 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6318 | case glslang::EOpSubgroupBallotFindLSB: |
| 6319 | case glslang::EOpSubgroupBallotFindMSB: |
| 6320 | case glslang::EOpSubgroupAdd: |
| 6321 | case glslang::EOpSubgroupMul: |
| 6322 | case glslang::EOpSubgroupMin: |
| 6323 | case glslang::EOpSubgroupMax: |
| 6324 | case glslang::EOpSubgroupAnd: |
| 6325 | case glslang::EOpSubgroupOr: |
| 6326 | case glslang::EOpSubgroupXor: |
| 6327 | case glslang::EOpSubgroupInclusiveAdd: |
| 6328 | case glslang::EOpSubgroupInclusiveMul: |
| 6329 | case glslang::EOpSubgroupInclusiveMin: |
| 6330 | case glslang::EOpSubgroupInclusiveMax: |
| 6331 | case glslang::EOpSubgroupInclusiveAnd: |
| 6332 | case glslang::EOpSubgroupInclusiveOr: |
| 6333 | case glslang::EOpSubgroupInclusiveXor: |
| 6334 | case glslang::EOpSubgroupExclusiveAdd: |
| 6335 | case glslang::EOpSubgroupExclusiveMul: |
| 6336 | case glslang::EOpSubgroupExclusiveMin: |
| 6337 | case glslang::EOpSubgroupExclusiveMax: |
| 6338 | case glslang::EOpSubgroupExclusiveAnd: |
| 6339 | case glslang::EOpSubgroupExclusiveOr: |
| 6340 | case glslang::EOpSubgroupExclusiveXor: |
| 6341 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6342 | case glslang::EOpSubgroupQuadSwapVertical: |
| 6343 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 6344 | std::vector<spv::Id> operands; |
| 6345 | operands.push_back(operand); |
| 6346 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 6347 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6348 | case glslang::EOpMbcnt: |
| 6349 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6350 | libCall = spv::MbcntAMD; |
| 6351 | break; |
| 6352 | |
| 6353 | case glslang::EOpCubeFaceIndex: |
| 6354 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 6355 | libCall = spv::CubeFaceIndexAMD; |
| 6356 | break; |
| 6357 | |
| 6358 | case glslang::EOpCubeFaceCoord: |
| 6359 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 6360 | libCall = spv::CubeFaceCoordAMD; |
| 6361 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6362 | case glslang::EOpSubgroupPartition: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 6363 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 6364 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6365 | case glslang::EOpConstructReference: |
| 6366 | unaryOp = spv::OpBitcast; |
| 6367 | break; |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 6368 | |
| 6369 | case glslang::EOpConvUint64ToAccStruct: |
| 6370 | case glslang::EOpConvUvec2ToAccStruct: |
| 6371 | unaryOp = spv::OpConvertUToAccelerationStructureKHR; |
| 6372 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 6373 | #endif |
Jeff Bolz | 88220d5 | 2019-05-08 10:24:46 -0500 | [diff] [blame] | 6374 | |
| 6375 | case glslang::EOpCopyObject: |
| 6376 | unaryOp = spv::OpCopyObject; |
| 6377 | break; |
| 6378 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6379 | default: |
| 6380 | return 0; |
| 6381 | } |
| 6382 | |
| 6383 | spv::Id id; |
| 6384 | if (libCall >= 0) { |
| 6385 | std::vector<spv::Id> args; |
| 6386 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6387 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6388 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 6389 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 6390 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6391 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6392 | decorations.addNoContraction(builder, id); |
| 6393 | decorations.addNonUniform(builder, id); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6394 | return builder.setPrecision(id, decorations.precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6395 | } |
| 6396 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6397 | // Create a unary operation on a matrix |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6398 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 6399 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6400 | { |
| 6401 | // Handle unary operations vector by vector. |
| 6402 | // The result type is the same type as the original type. |
| 6403 | // The algorithm is to: |
| 6404 | // - break the matrix into vectors |
| 6405 | // - apply the operation to each vector |
| 6406 | // - make a matrix out the vector results |
| 6407 | |
| 6408 | // get the types sorted out |
| 6409 | int numCols = builder.getNumColumns(operand); |
| 6410 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 6411 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 6412 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6413 | std::vector<spv::Id> results; |
| 6414 | |
| 6415 | // do each vector op |
| 6416 | for (int c = 0; c < numCols; ++c) { |
| 6417 | std::vector<unsigned int> indexes; |
| 6418 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 6419 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 6420 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6421 | decorations.addNoContraction(builder, destVec); |
| 6422 | decorations.addNonUniform(builder, destVec); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6423 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6424 | } |
| 6425 | |
| 6426 | // put the pieces together |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6427 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6428 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6429 | return result; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 6430 | } |
| 6431 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6432 | // For converting integers where both the bitwidth and the signedness could |
| 6433 | // change, but only do the width change here. The caller is still responsible |
| 6434 | // for the signedness conversion. |
| 6435 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6436 | { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6437 | // Get the result type width, based on the type to convert to. |
| 6438 | int width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6439 | switch(op) { |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6440 | case glslang::EOpConvInt16ToUint8: |
| 6441 | case glslang::EOpConvIntToUint8: |
| 6442 | case glslang::EOpConvInt64ToUint8: |
| 6443 | case glslang::EOpConvUint16ToInt8: |
| 6444 | case glslang::EOpConvUintToInt8: |
| 6445 | case glslang::EOpConvUint64ToInt8: |
| 6446 | width = 8; |
| 6447 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6448 | case glslang::EOpConvInt8ToUint16: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6449 | case glslang::EOpConvIntToUint16: |
| 6450 | case glslang::EOpConvInt64ToUint16: |
| 6451 | case glslang::EOpConvUint8ToInt16: |
| 6452 | case glslang::EOpConvUintToInt16: |
| 6453 | case glslang::EOpConvUint64ToInt16: |
| 6454 | width = 16; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6455 | break; |
| 6456 | case glslang::EOpConvInt8ToUint: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6457 | case glslang::EOpConvInt16ToUint: |
| 6458 | case glslang::EOpConvInt64ToUint: |
| 6459 | case glslang::EOpConvUint8ToInt: |
| 6460 | case glslang::EOpConvUint16ToInt: |
| 6461 | case glslang::EOpConvUint64ToInt: |
| 6462 | width = 32; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6463 | break; |
| 6464 | case glslang::EOpConvInt8ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6465 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6466 | case glslang::EOpConvIntToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6467 | case glslang::EOpConvUint8ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6468 | case glslang::EOpConvUint16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6469 | case glslang::EOpConvUintToInt64: |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6470 | width = 64; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6471 | break; |
| 6472 | |
| 6473 | default: |
| 6474 | assert(false && "Default missing"); |
| 6475 | break; |
| 6476 | } |
| 6477 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6478 | // Get the conversion operation and result type, |
| 6479 | // based on the target width, but the source type. |
| 6480 | spv::Id type = spv::NoType; |
| 6481 | spv::Op convOp = spv::OpNop; |
| 6482 | switch(op) { |
| 6483 | case glslang::EOpConvInt8ToUint16: |
| 6484 | case glslang::EOpConvInt8ToUint: |
| 6485 | case glslang::EOpConvInt8ToUint64: |
| 6486 | case glslang::EOpConvInt16ToUint8: |
| 6487 | case glslang::EOpConvInt16ToUint: |
| 6488 | case glslang::EOpConvInt16ToUint64: |
| 6489 | case glslang::EOpConvIntToUint8: |
| 6490 | case glslang::EOpConvIntToUint16: |
| 6491 | case glslang::EOpConvIntToUint64: |
| 6492 | case glslang::EOpConvInt64ToUint8: |
| 6493 | case glslang::EOpConvInt64ToUint16: |
| 6494 | case glslang::EOpConvInt64ToUint: |
| 6495 | convOp = spv::OpSConvert; |
| 6496 | type = builder.makeIntType(width); |
| 6497 | break; |
| 6498 | default: |
| 6499 | convOp = spv::OpUConvert; |
| 6500 | type = builder.makeUintType(width); |
| 6501 | break; |
| 6502 | } |
| 6503 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6504 | if (vectorSize > 0) |
| 6505 | type = builder.makeVectorType(type, vectorSize); |
| 6506 | |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6507 | return builder.createUnaryOp(convOp, type, operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6508 | } |
| 6509 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6510 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 6511 | spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6512 | { |
| 6513 | spv::Op convOp = spv::OpNop; |
| 6514 | spv::Id zero = 0; |
| 6515 | spv::Id one = 0; |
| 6516 | |
| 6517 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 6518 | |
| 6519 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6520 | case glslang::EOpConvIntToBool: |
| 6521 | case glslang::EOpConvUintToBool: |
| 6522 | zero = builder.makeUintConstant(0); |
| 6523 | zero = makeSmearedConstant(zero, vectorSize); |
| 6524 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6525 | case glslang::EOpConvFloatToBool: |
| 6526 | zero = builder.makeFloatConstant(0.0F); |
| 6527 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6528 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6529 | case glslang::EOpConvBoolToFloat: |
| 6530 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6531 | zero = builder.makeFloatConstant(0.0F); |
| 6532 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6533 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6534 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6535 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6536 | case glslang::EOpConvBoolToInt64: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6537 | #ifndef GLSLANG_WEB |
| 6538 | if (op == glslang::EOpConvBoolToInt64) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6539 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6540 | one = builder.makeInt64Constant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6541 | } else |
| 6542 | #endif |
| 6543 | { |
| 6544 | zero = builder.makeIntConstant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6545 | one = builder.makeIntConstant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6546 | } |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6547 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6548 | convOp = spv::OpSelect; |
| 6549 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6550 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6551 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6552 | case glslang::EOpConvBoolToUint64: |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6553 | #ifndef GLSLANG_WEB |
| 6554 | if (op == glslang::EOpConvBoolToUint64) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6555 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6556 | one = builder.makeUint64Constant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6557 | } else |
| 6558 | #endif |
| 6559 | { |
| 6560 | zero = builder.makeUintConstant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6561 | one = builder.makeUintConstant(1); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6562 | } |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6563 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6564 | convOp = spv::OpSelect; |
| 6565 | break; |
| 6566 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6567 | case glslang::EOpConvInt8ToFloat16: |
| 6568 | case glslang::EOpConvInt8ToFloat: |
| 6569 | case glslang::EOpConvInt8ToDouble: |
| 6570 | case glslang::EOpConvInt16ToFloat16: |
| 6571 | case glslang::EOpConvInt16ToFloat: |
| 6572 | case glslang::EOpConvInt16ToDouble: |
| 6573 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6574 | case glslang::EOpConvIntToFloat: |
| 6575 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6576 | case glslang::EOpConvInt64ToFloat: |
| 6577 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6578 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6579 | convOp = spv::OpConvertSToF; |
| 6580 | break; |
| 6581 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6582 | case glslang::EOpConvUint8ToFloat16: |
| 6583 | case glslang::EOpConvUint8ToFloat: |
| 6584 | case glslang::EOpConvUint8ToDouble: |
| 6585 | case glslang::EOpConvUint16ToFloat16: |
| 6586 | case glslang::EOpConvUint16ToFloat: |
| 6587 | case glslang::EOpConvUint16ToDouble: |
| 6588 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6589 | case glslang::EOpConvUintToFloat: |
| 6590 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6591 | case glslang::EOpConvUint64ToFloat: |
| 6592 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6593 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6594 | convOp = spv::OpConvertUToF; |
| 6595 | break; |
| 6596 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6597 | case glslang::EOpConvFloat16ToInt8: |
| 6598 | case glslang::EOpConvFloatToInt8: |
| 6599 | case glslang::EOpConvDoubleToInt8: |
| 6600 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6601 | case glslang::EOpConvFloatToInt16: |
| 6602 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6603 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6604 | case glslang::EOpConvFloatToInt: |
| 6605 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6606 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6607 | case glslang::EOpConvFloatToInt64: |
| 6608 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6609 | convOp = spv::OpConvertFToS; |
| 6610 | break; |
| 6611 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6612 | case glslang::EOpConvUint8ToInt8: |
| 6613 | case glslang::EOpConvInt8ToUint8: |
| 6614 | case glslang::EOpConvUint16ToInt16: |
| 6615 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6616 | case glslang::EOpConvUintToInt: |
| 6617 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6618 | case glslang::EOpConvUint64ToInt64: |
| 6619 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6620 | if (builder.isInSpecConstCodeGenMode()) { |
| 6621 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6622 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6623 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 6624 | zero = builder.makeUint8Constant(0); |
| 6625 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6626 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6627 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 6628 | zero = builder.makeUint64Constant(0); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6629 | } else |
| 6630 | #endif |
| 6631 | { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6632 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6633 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 6634 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 6635 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6636 | // generating for OpSpecConstantOp instruction. |
| 6637 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6638 | } |
| 6639 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6640 | convOp = spv::OpBitcast; |
| 6641 | break; |
| 6642 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6643 | case glslang::EOpConvFloat16ToUint8: |
| 6644 | case glslang::EOpConvFloatToUint8: |
| 6645 | case glslang::EOpConvDoubleToUint8: |
| 6646 | case glslang::EOpConvFloat16ToUint16: |
| 6647 | case glslang::EOpConvFloatToUint16: |
| 6648 | case glslang::EOpConvDoubleToUint16: |
| 6649 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6650 | case glslang::EOpConvFloatToUint: |
| 6651 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6652 | case glslang::EOpConvFloatToUint64: |
| 6653 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6654 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6655 | convOp = spv::OpConvertFToU; |
| 6656 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6657 | |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6658 | #ifndef GLSLANG_WEB |
| 6659 | case glslang::EOpConvInt8ToBool: |
| 6660 | case glslang::EOpConvUint8ToBool: |
| 6661 | zero = builder.makeUint8Constant(0); |
| 6662 | zero = makeSmearedConstant(zero, vectorSize); |
| 6663 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6664 | case glslang::EOpConvInt16ToBool: |
| 6665 | case glslang::EOpConvUint16ToBool: |
| 6666 | zero = builder.makeUint16Constant(0); |
| 6667 | zero = makeSmearedConstant(zero, vectorSize); |
| 6668 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6669 | case glslang::EOpConvInt64ToBool: |
| 6670 | case glslang::EOpConvUint64ToBool: |
| 6671 | zero = builder.makeUint64Constant(0); |
| 6672 | zero = makeSmearedConstant(zero, vectorSize); |
| 6673 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6674 | case glslang::EOpConvDoubleToBool: |
| 6675 | zero = builder.makeDoubleConstant(0.0); |
| 6676 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6677 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6678 | case glslang::EOpConvFloat16ToBool: |
| 6679 | zero = builder.makeFloat16Constant(0.0F); |
| 6680 | zero = makeSmearedConstant(zero, vectorSize); |
Graeme Leese | 65ce566 | 2020-06-05 13:32:51 +0100 | [diff] [blame] | 6681 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6682 | case glslang::EOpConvBoolToDouble: |
| 6683 | convOp = spv::OpSelect; |
| 6684 | zero = builder.makeDoubleConstant(0.0); |
| 6685 | one = builder.makeDoubleConstant(1.0); |
| 6686 | break; |
| 6687 | case glslang::EOpConvBoolToFloat16: |
| 6688 | convOp = spv::OpSelect; |
| 6689 | zero = builder.makeFloat16Constant(0.0F); |
| 6690 | one = builder.makeFloat16Constant(1.0F); |
| 6691 | break; |
| 6692 | case glslang::EOpConvBoolToInt8: |
| 6693 | zero = builder.makeInt8Constant(0); |
| 6694 | one = builder.makeInt8Constant(1); |
| 6695 | convOp = spv::OpSelect; |
| 6696 | break; |
| 6697 | case glslang::EOpConvBoolToUint8: |
| 6698 | zero = builder.makeUint8Constant(0); |
| 6699 | one = builder.makeUint8Constant(1); |
| 6700 | convOp = spv::OpSelect; |
| 6701 | break; |
| 6702 | case glslang::EOpConvBoolToInt16: |
| 6703 | zero = builder.makeInt16Constant(0); |
| 6704 | one = builder.makeInt16Constant(1); |
| 6705 | convOp = spv::OpSelect; |
| 6706 | break; |
| 6707 | case glslang::EOpConvBoolToUint16: |
| 6708 | zero = builder.makeUint16Constant(0); |
| 6709 | one = builder.makeUint16Constant(1); |
| 6710 | convOp = spv::OpSelect; |
| 6711 | break; |
| 6712 | case glslang::EOpConvDoubleToFloat: |
| 6713 | case glslang::EOpConvFloatToDouble: |
| 6714 | case glslang::EOpConvDoubleToFloat16: |
| 6715 | case glslang::EOpConvFloat16ToDouble: |
| 6716 | case glslang::EOpConvFloatToFloat16: |
| 6717 | case glslang::EOpConvFloat16ToFloat: |
| 6718 | convOp = spv::OpFConvert; |
| 6719 | if (builder.isMatrixType(destType)) |
| 6720 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
| 6721 | break; |
| 6722 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6723 | case glslang::EOpConvInt8ToInt16: |
| 6724 | case glslang::EOpConvInt8ToInt: |
| 6725 | case glslang::EOpConvInt8ToInt64: |
| 6726 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6727 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6728 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6729 | case glslang::EOpConvIntToInt8: |
| 6730 | case glslang::EOpConvIntToInt16: |
| 6731 | case glslang::EOpConvIntToInt64: |
| 6732 | case glslang::EOpConvInt64ToInt8: |
| 6733 | case glslang::EOpConvInt64ToInt16: |
| 6734 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6735 | convOp = spv::OpSConvert; |
| 6736 | break; |
| 6737 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6738 | case glslang::EOpConvUint8ToUint16: |
| 6739 | case glslang::EOpConvUint8ToUint: |
| 6740 | case glslang::EOpConvUint8ToUint64: |
| 6741 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6742 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6743 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6744 | case glslang::EOpConvUintToUint8: |
| 6745 | case glslang::EOpConvUintToUint16: |
| 6746 | case glslang::EOpConvUintToUint64: |
| 6747 | case glslang::EOpConvUint64ToUint8: |
| 6748 | case glslang::EOpConvUint64ToUint16: |
| 6749 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6750 | convOp = spv::OpUConvert; |
| 6751 | break; |
| 6752 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6753 | case glslang::EOpConvInt8ToUint16: |
| 6754 | case glslang::EOpConvInt8ToUint: |
| 6755 | case glslang::EOpConvInt8ToUint64: |
| 6756 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6757 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6758 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6759 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6760 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6761 | case glslang::EOpConvIntToUint64: |
| 6762 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6763 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6764 | case glslang::EOpConvInt64ToUint: |
| 6765 | case glslang::EOpConvUint8ToInt16: |
| 6766 | case glslang::EOpConvUint8ToInt: |
| 6767 | case glslang::EOpConvUint8ToInt64: |
| 6768 | case glslang::EOpConvUint16ToInt8: |
| 6769 | case glslang::EOpConvUint16ToInt: |
| 6770 | case glslang::EOpConvUint16ToInt64: |
| 6771 | case glslang::EOpConvUintToInt8: |
| 6772 | case glslang::EOpConvUintToInt16: |
| 6773 | case glslang::EOpConvUintToInt64: |
| 6774 | case glslang::EOpConvUint64ToInt8: |
| 6775 | case glslang::EOpConvUint64ToInt16: |
| 6776 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6777 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | ad7645f | 2018-06-04 19:11:25 -0600 | [diff] [blame] | 6778 | operand = createIntWidthConversion(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6779 | |
| 6780 | if (builder.isInSpecConstCodeGenMode()) { |
| 6781 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6782 | switch(op) { |
| 6783 | case glslang::EOpConvInt16ToUint8: |
| 6784 | case glslang::EOpConvIntToUint8: |
| 6785 | case glslang::EOpConvInt64ToUint8: |
| 6786 | case glslang::EOpConvUint16ToInt8: |
| 6787 | case glslang::EOpConvUintToInt8: |
| 6788 | case glslang::EOpConvUint64ToInt8: |
| 6789 | zero = builder.makeUint8Constant(0); |
| 6790 | break; |
| 6791 | case glslang::EOpConvInt8ToUint16: |
| 6792 | case glslang::EOpConvIntToUint16: |
| 6793 | case glslang::EOpConvInt64ToUint16: |
| 6794 | case glslang::EOpConvUint8ToInt16: |
| 6795 | case glslang::EOpConvUintToInt16: |
| 6796 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6797 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6798 | break; |
| 6799 | case glslang::EOpConvInt8ToUint: |
| 6800 | case glslang::EOpConvInt16ToUint: |
| 6801 | case glslang::EOpConvInt64ToUint: |
| 6802 | case glslang::EOpConvUint8ToInt: |
| 6803 | case glslang::EOpConvUint16ToInt: |
| 6804 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6805 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6806 | break; |
| 6807 | case glslang::EOpConvInt8ToUint64: |
| 6808 | case glslang::EOpConvInt16ToUint64: |
| 6809 | case glslang::EOpConvIntToUint64: |
| 6810 | case glslang::EOpConvUint8ToInt64: |
| 6811 | case glslang::EOpConvUint16ToInt64: |
| 6812 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 6813 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6814 | break; |
| 6815 | default: |
| 6816 | assert(false && "Default missing"); |
| 6817 | break; |
| 6818 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6819 | zero = makeSmearedConstant(zero, vectorSize); |
| 6820 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6821 | // generating for OpSpecConstantOp instruction. |
| 6822 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6823 | } |
| 6824 | // For normal run-time conversion instruction, use OpBitcast. |
| 6825 | convOp = spv::OpBitcast; |
| 6826 | break; |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 6827 | case glslang::EOpConvUint64ToPtr: |
| 6828 | convOp = spv::OpConvertUToPtr; |
| 6829 | break; |
| 6830 | case glslang::EOpConvPtrToUint64: |
| 6831 | convOp = spv::OpConvertPtrToU; |
| 6832 | break; |
John Kessenich | 90e402f | 2019-09-17 23:19:38 -0600 | [diff] [blame] | 6833 | case glslang::EOpConvPtrToUvec2: |
| 6834 | case glslang::EOpConvUvec2ToPtr: |
John Kessenich | ee8e9c1 | 2019-10-10 20:54:21 -0600 | [diff] [blame] | 6835 | if (builder.isVector(operand)) |
| 6836 | builder.promoteIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, |
| 6837 | spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5); |
John Kessenich | 90e402f | 2019-09-17 23:19:38 -0600 | [diff] [blame] | 6838 | convOp = spv::OpBitcast; |
| 6839 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 6840 | #endif |
| 6841 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6842 | default: |
| 6843 | break; |
| 6844 | } |
| 6845 | |
| 6846 | spv::Id result = 0; |
| 6847 | if (convOp == spv::OpNop) |
| 6848 | return result; |
| 6849 | |
| 6850 | if (convOp == spv::OpSelect) { |
| 6851 | zero = makeSmearedConstant(zero, vectorSize); |
| 6852 | one = makeSmearedConstant(one, vectorSize); |
| 6853 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 6854 | } else |
| 6855 | result = builder.createUnaryOp(convOp, destType, operand); |
| 6856 | |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6857 | result = builder.setPrecision(result, decorations.precision); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6858 | decorations.addNonUniform(builder, result); |
John Kessenich | ead8622 | 2018-03-28 18:01:20 -0600 | [diff] [blame] | 6859 | return result; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6860 | } |
| 6861 | |
| 6862 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 6863 | { |
| 6864 | if (vectorSize == 0) |
| 6865 | return constant; |
| 6866 | |
| 6867 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 6868 | std::vector<spv::Id> components; |
| 6869 | for (int c = 0; c < vectorSize; ++c) |
| 6870 | components.push_back(constant); |
| 6871 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 6872 | } |
| 6873 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6874 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6875 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, |
| 6876 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 6877 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6878 | { |
| 6879 | spv::Op opCode = spv::OpNop; |
| 6880 | |
| 6881 | switch (op) { |
| 6882 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6883 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6884 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6885 | opCode = spv::OpAtomicIAdd; |
Vikram Kushwaha | 79b9392 | 2020-07-19 15:45:01 -0700 | [diff] [blame] | 6886 | if (typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
| 6887 | opCode = spv::OpAtomicFAddEXT; |
| 6888 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_add); |
| 6889 | if (typeProxy == glslang::EbtFloat) |
| 6890 | builder.addCapability(spv::CapabilityAtomicFloat32AddEXT); |
| 6891 | else |
| 6892 | builder.addCapability(spv::CapabilityAtomicFloat64AddEXT); |
| 6893 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6894 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6895 | case glslang::EOpAtomicCounterSubtract: |
| 6896 | opCode = spv::OpAtomicISub; |
| 6897 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6898 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6899 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6900 | case glslang::EOpAtomicCounterMin: |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6901 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? |
| 6902 | spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6903 | break; |
| 6904 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6905 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6906 | case glslang::EOpAtomicCounterMax: |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6907 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? |
| 6908 | spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6909 | break; |
| 6910 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6911 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6912 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6913 | opCode = spv::OpAtomicAnd; |
| 6914 | break; |
| 6915 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6916 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6917 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6918 | opCode = spv::OpAtomicOr; |
| 6919 | break; |
| 6920 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6921 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6922 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6923 | opCode = spv::OpAtomicXor; |
| 6924 | break; |
| 6925 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6926 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6927 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6928 | opCode = spv::OpAtomicExchange; |
| 6929 | break; |
| 6930 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 6931 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 6932 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6933 | opCode = spv::OpAtomicCompareExchange; |
| 6934 | break; |
| 6935 | case glslang::EOpAtomicCounterIncrement: |
| 6936 | opCode = spv::OpAtomicIIncrement; |
| 6937 | break; |
| 6938 | case glslang::EOpAtomicCounterDecrement: |
| 6939 | opCode = spv::OpAtomicIDecrement; |
| 6940 | break; |
| 6941 | case glslang::EOpAtomicCounter: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6942 | case glslang::EOpImageAtomicLoad: |
| 6943 | case glslang::EOpAtomicLoad: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6944 | opCode = spv::OpAtomicLoad; |
| 6945 | break; |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6946 | case glslang::EOpAtomicStore: |
| 6947 | case glslang::EOpImageAtomicStore: |
| 6948 | opCode = spv::OpAtomicStore; |
| 6949 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6950 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6951 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6952 | break; |
| 6953 | } |
| 6954 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 6955 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 6956 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 6957 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 6958 | // Sort out the operands |
| 6959 | // - mapping from glslang -> SPV |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6960 | // - there are extra SPV operands that are optional in glslang |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 6961 | // - compare-exchange swaps the value and comparator |
| 6962 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 6963 | // - EOpAtomicCounterDecrement needs a post decrement |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6964 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 6965 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 6966 | spv::Id scopeId; |
| 6967 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6968 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 6969 | } else { |
| 6970 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 6971 | } |
| 6972 | // semantics default to relaxed |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6973 | spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() && |
| 6974 | glslangIntermediate->usingVulkanMemoryModel() ? |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 6975 | spv::MemorySemanticsVolatileMask : |
| 6976 | spv::MemorySemanticsMaskNone); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6977 | spv::Id semanticsId2 = semanticsId; |
| 6978 | |
| 6979 | pointerId = operands[0]; |
| 6980 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 6981 | // no additional operands |
| 6982 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 6983 | compareId = operands[1]; |
| 6984 | valueId = operands[2]; |
| 6985 | if (operands.size() > 3) { |
| 6986 | scopeId = operands[3]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6987 | semanticsId = builder.makeUintConstant( |
| 6988 | builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 6989 | semanticsId2 = builder.makeUintConstant( |
| 6990 | builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6991 | } |
| 6992 | } else if (opCode == spv::OpAtomicLoad) { |
| 6993 | if (operands.size() > 1) { |
| 6994 | scopeId = operands[1]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 6995 | semanticsId = builder.makeUintConstant( |
| 6996 | builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 6997 | } |
| 6998 | } else { |
| 6999 | // atomic store or RMW |
| 7000 | valueId = operands[1]; |
| 7001 | if (operands.size() > 2) { |
| 7002 | scopeId = operands[2]; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7003 | semanticsId = builder.makeUintConstant |
| 7004 | (builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7005 | } |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 7006 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 7007 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7008 | // Check for capabilities |
| 7009 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
Jeff Bolz | 38a52fc | 2019-06-14 09:56:28 -0500 | [diff] [blame] | 7010 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7011 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7012 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7013 | spv::MemorySemanticsVolatileMask)) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7014 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7015 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 7016 | |
Jeff Bolz | bfd84a3 | 2021-01-27 13:14:34 -0600 | [diff] [blame] | 7017 | if (builder.getConstantScalar(scopeId) == spv::ScopeQueueFamily) { |
| 7018 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7019 | } |
| 7020 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7021 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 7022 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7023 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 7024 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7025 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 7026 | spvAtomicOperands.push_back(pointerId); |
| 7027 | spvAtomicOperands.push_back(scopeId); |
| 7028 | spvAtomicOperands.push_back(semanticsId); |
| 7029 | if (opCode == spv::OpAtomicCompareExchange) { |
| 7030 | spvAtomicOperands.push_back(semanticsId2); |
| 7031 | spvAtomicOperands.push_back(valueId); |
| 7032 | spvAtomicOperands.push_back(compareId); |
| 7033 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 7034 | spvAtomicOperands.push_back(valueId); |
| 7035 | } |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 7036 | |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 7037 | if (opCode == spv::OpAtomicStore) { |
| 7038 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 7039 | return 0; |
| 7040 | } else { |
| 7041 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 7042 | |
| 7043 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 7044 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 7045 | if (op == glslang::EOpAtomicCounterDecrement) |
| 7046 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 7047 | |
| 7048 | return resultId; |
| 7049 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 7050 | } |
| 7051 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7052 | // Create group invocation operations. |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7053 | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, |
| 7054 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7055 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7056 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7057 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7058 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7059 | spv::Op opCode = spv::OpNop; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7060 | std::vector<spv::IdImmediate> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7061 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 7062 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7063 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 7064 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7065 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 7066 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 7067 | } else if (op == glslang::EOpAnyInvocation || |
| 7068 | op == glslang::EOpAllInvocations || |
| 7069 | op == glslang::EOpAllInvocationsEqual) { |
| 7070 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 7071 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7072 | } else { |
| 7073 | builder.addCapability(spv::CapabilityGroups); |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 7074 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 7075 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7076 | op == glslang::EOpAddInvocationsNonUniform || |
| 7077 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 7078 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 7079 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 7080 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 7081 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 7082 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 7083 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7084 | |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7085 | switch (op) { |
| 7086 | case glslang::EOpMinInvocations: |
| 7087 | case glslang::EOpMaxInvocations: |
| 7088 | case glslang::EOpAddInvocations: |
| 7089 | case glslang::EOpMinInvocationsNonUniform: |
| 7090 | case glslang::EOpMaxInvocationsNonUniform: |
| 7091 | case glslang::EOpAddInvocationsNonUniform: |
| 7092 | groupOperation = spv::GroupOperationReduce; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7093 | break; |
| 7094 | case glslang::EOpMinInvocationsInclusiveScan: |
| 7095 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 7096 | case glslang::EOpAddInvocationsInclusiveScan: |
| 7097 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 7098 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 7099 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 7100 | groupOperation = spv::GroupOperationInclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7101 | break; |
| 7102 | case glslang::EOpMinInvocationsExclusiveScan: |
| 7103 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 7104 | case glslang::EOpAddInvocationsExclusiveScan: |
| 7105 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 7106 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 7107 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 7108 | groupOperation = spv::GroupOperationExclusiveScan; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7109 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 7110 | default: |
| 7111 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7112 | } |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7113 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7114 | spvGroupOperands.push_back(scope); |
| 7115 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7116 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7117 | spvGroupOperands.push_back(groupOp); |
| 7118 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7119 | } |
| 7120 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7121 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 7122 | spv::IdImmediate op = { true, *opIt }; |
| 7123 | spvGroupOperands.push_back(op); |
| 7124 | } |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7125 | |
| 7126 | switch (op) { |
| 7127 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 7128 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7129 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7130 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 7131 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7132 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7133 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 7134 | opCode = spv::OpSubgroupAllEqualKHR; |
| 7135 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7136 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7137 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7138 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7139 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7140 | break; |
| 7141 | case glslang::EOpReadFirstInvocation: |
| 7142 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 7143 | break; |
| 7144 | case glslang::EOpBallot: |
| 7145 | { |
| 7146 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 7147 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 7148 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 7149 | // |
| 7150 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 7151 | // |
| 7152 | spv::Id uintType = builder.makeUintType(32); |
| 7153 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 7154 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 7155 | |
| 7156 | std::vector<spv::Id> components; |
| 7157 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 7158 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 7159 | |
| 7160 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 7161 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 7162 | builder.createCompositeConstruct(uvec2Type, components)); |
| 7163 | } |
| 7164 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7165 | case glslang::EOpMinInvocations: |
| 7166 | case glslang::EOpMaxInvocations: |
| 7167 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7168 | case glslang::EOpMinInvocationsInclusiveScan: |
| 7169 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 7170 | case glslang::EOpAddInvocationsInclusiveScan: |
| 7171 | case glslang::EOpMinInvocationsExclusiveScan: |
| 7172 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 7173 | case glslang::EOpAddInvocationsExclusiveScan: |
| 7174 | if (op == glslang::EOpMinInvocations || |
| 7175 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 7176 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7177 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7178 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7179 | else { |
| 7180 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7181 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7182 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7183 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7184 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7185 | } else if (op == glslang::EOpMaxInvocations || |
| 7186 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 7187 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7188 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7189 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7190 | else { |
| 7191 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7192 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7193 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7194 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7195 | } |
| 7196 | } else { |
| 7197 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7198 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7199 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7200 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7201 | } |
| 7202 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7203 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7204 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7205 | |
| 7206 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7207 | case glslang::EOpMinInvocationsNonUniform: |
| 7208 | case glslang::EOpMaxInvocationsNonUniform: |
| 7209 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7210 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 7211 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 7212 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 7213 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 7214 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 7215 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 7216 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 7217 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 7218 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7219 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7220 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7221 | else { |
| 7222 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7223 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7224 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7225 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7226 | } |
| 7227 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7228 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 7229 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 7230 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7231 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7232 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7233 | else { |
| 7234 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7235 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7236 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7237 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7238 | } |
| 7239 | } |
| 7240 | else { |
| 7241 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7242 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7243 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7244 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7245 | } |
| 7246 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7247 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 7248 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7249 | |
| 7250 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7251 | default: |
| 7252 | logger->missingFunctionality("invocation operation"); |
| 7253 | return spv::NoResult; |
| 7254 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7255 | |
| 7256 | assert(opCode != spv::OpNop); |
| 7257 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 7258 | } |
| 7259 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7260 | // Create group invocation operations on a vector |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7261 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 7262 | spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7263 | { |
| 7264 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 7265 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7266 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7267 | op == spv::OpSubgroupReadInvocationKHR || |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7268 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || |
| 7269 | op == spv::OpGroupSMinNonUniformAMD || |
| 7270 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || |
| 7271 | op == spv::OpGroupSMaxNonUniformAMD || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7272 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
| 7273 | |
| 7274 | // Handle group invocation operations scalar by scalar. |
| 7275 | // The result type is the same type as the original type. |
| 7276 | // The algorithm is to: |
| 7277 | // - break the vector into scalars |
| 7278 | // - apply the operation to each scalar |
| 7279 | // - make a vector out the scalar results |
| 7280 | |
| 7281 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7282 | int numComponents = builder.getNumComponents(operands[0]); |
| 7283 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7284 | std::vector<spv::Id> results; |
| 7285 | |
| 7286 | // do each scalar op |
| 7287 | for (int comp = 0; comp < numComponents; ++comp) { |
| 7288 | std::vector<unsigned int> indexes; |
| 7289 | indexes.push_back(comp); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7290 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 7291 | std::vector<spv::IdImmediate> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7292 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 7293 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7294 | spv::IdImmediate operand = { true, operands[1] }; |
| 7295 | spvGroupOperands.push_back(operand); |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 7296 | } else if (op == spv::OpGroupBroadcast) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7297 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7298 | spvGroupOperands.push_back(scope); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7299 | spvGroupOperands.push_back(scalar); |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7300 | spv::IdImmediate operand = { true, operands[1] }; |
| 7301 | spvGroupOperands.push_back(operand); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7302 | } else { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7303 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7304 | spvGroupOperands.push_back(scope); |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7305 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7306 | spvGroupOperands.push_back(groupOp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7307 | spvGroupOperands.push_back(scalar); |
| 7308 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7309 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 7310 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | // put the pieces together |
| 7314 | return builder.createCompositeConstruct(typeId, results); |
| 7315 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 7316 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7317 | // Create subgroup invocation operations. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7318 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 7319 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7320 | { |
| 7321 | // Add the required capabilities. |
| 7322 | switch (op) { |
| 7323 | case glslang::EOpSubgroupElect: |
| 7324 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7325 | break; |
| 7326 | case glslang::EOpSubgroupAll: |
| 7327 | case glslang::EOpSubgroupAny: |
| 7328 | case glslang::EOpSubgroupAllEqual: |
| 7329 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7330 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 7331 | break; |
| 7332 | case glslang::EOpSubgroupBroadcast: |
| 7333 | case glslang::EOpSubgroupBroadcastFirst: |
| 7334 | case glslang::EOpSubgroupBallot: |
| 7335 | case glslang::EOpSubgroupInverseBallot: |
| 7336 | case glslang::EOpSubgroupBallotBitExtract: |
| 7337 | case glslang::EOpSubgroupBallotBitCount: |
| 7338 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7339 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7340 | case glslang::EOpSubgroupBallotFindLSB: |
| 7341 | case glslang::EOpSubgroupBallotFindMSB: |
| 7342 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7343 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 7344 | break; |
| 7345 | case glslang::EOpSubgroupShuffle: |
| 7346 | case glslang::EOpSubgroupShuffleXor: |
| 7347 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7348 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 7349 | break; |
| 7350 | case glslang::EOpSubgroupShuffleUp: |
| 7351 | case glslang::EOpSubgroupShuffleDown: |
| 7352 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7353 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 7354 | break; |
| 7355 | case glslang::EOpSubgroupAdd: |
| 7356 | case glslang::EOpSubgroupMul: |
| 7357 | case glslang::EOpSubgroupMin: |
| 7358 | case glslang::EOpSubgroupMax: |
| 7359 | case glslang::EOpSubgroupAnd: |
| 7360 | case glslang::EOpSubgroupOr: |
| 7361 | case glslang::EOpSubgroupXor: |
| 7362 | case glslang::EOpSubgroupInclusiveAdd: |
| 7363 | case glslang::EOpSubgroupInclusiveMul: |
| 7364 | case glslang::EOpSubgroupInclusiveMin: |
| 7365 | case glslang::EOpSubgroupInclusiveMax: |
| 7366 | case glslang::EOpSubgroupInclusiveAnd: |
| 7367 | case glslang::EOpSubgroupInclusiveOr: |
| 7368 | case glslang::EOpSubgroupInclusiveXor: |
| 7369 | case glslang::EOpSubgroupExclusiveAdd: |
| 7370 | case glslang::EOpSubgroupExclusiveMul: |
| 7371 | case glslang::EOpSubgroupExclusiveMin: |
| 7372 | case glslang::EOpSubgroupExclusiveMax: |
| 7373 | case glslang::EOpSubgroupExclusiveAnd: |
| 7374 | case glslang::EOpSubgroupExclusiveOr: |
| 7375 | case glslang::EOpSubgroupExclusiveXor: |
| 7376 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7377 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 7378 | break; |
| 7379 | case glslang::EOpSubgroupClusteredAdd: |
| 7380 | case glslang::EOpSubgroupClusteredMul: |
| 7381 | case glslang::EOpSubgroupClusteredMin: |
| 7382 | case glslang::EOpSubgroupClusteredMax: |
| 7383 | case glslang::EOpSubgroupClusteredAnd: |
| 7384 | case glslang::EOpSubgroupClusteredOr: |
| 7385 | case glslang::EOpSubgroupClusteredXor: |
| 7386 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7387 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 7388 | break; |
| 7389 | case glslang::EOpSubgroupQuadBroadcast: |
| 7390 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7391 | case glslang::EOpSubgroupQuadSwapVertical: |
| 7392 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 7393 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7394 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 7395 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7396 | case glslang::EOpSubgroupPartitionedAdd: |
| 7397 | case glslang::EOpSubgroupPartitionedMul: |
| 7398 | case glslang::EOpSubgroupPartitionedMin: |
| 7399 | case glslang::EOpSubgroupPartitionedMax: |
| 7400 | case glslang::EOpSubgroupPartitionedAnd: |
| 7401 | case glslang::EOpSubgroupPartitionedOr: |
| 7402 | case glslang::EOpSubgroupPartitionedXor: |
| 7403 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7404 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7405 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7406 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7407 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7408 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7409 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7410 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7411 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7412 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7413 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7414 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7415 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7416 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 7417 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 7418 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 7419 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7420 | default: assert(0 && "Unhandled subgroup operation!"); |
| 7421 | } |
| 7422 | |
Jeff Bolz | c5b669e | 2019-09-08 08:49:18 -0500 | [diff] [blame] | 7423 | |
| 7424 | const bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7425 | const bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7426 | const bool isBool = typeProxy == glslang::EbtBool; |
| 7427 | |
| 7428 | spv::Op opCode = spv::OpNop; |
| 7429 | |
| 7430 | // Figure out which opcode to use. |
| 7431 | switch (op) { |
| 7432 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 7433 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 7434 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 7435 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 7436 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 7437 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 7438 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 7439 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 7440 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 7441 | case glslang::EOpSubgroupBallotBitCount: |
| 7442 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7443 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 7444 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 7445 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 7446 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 7447 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 7448 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 7449 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 7450 | case glslang::EOpSubgroupAdd: |
| 7451 | case glslang::EOpSubgroupInclusiveAdd: |
| 7452 | case glslang::EOpSubgroupExclusiveAdd: |
| 7453 | case glslang::EOpSubgroupClusteredAdd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7454 | case glslang::EOpSubgroupPartitionedAdd: |
| 7455 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7456 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7457 | if (isFloat) { |
| 7458 | opCode = spv::OpGroupNonUniformFAdd; |
| 7459 | } else { |
| 7460 | opCode = spv::OpGroupNonUniformIAdd; |
| 7461 | } |
| 7462 | break; |
| 7463 | case glslang::EOpSubgroupMul: |
| 7464 | case glslang::EOpSubgroupInclusiveMul: |
| 7465 | case glslang::EOpSubgroupExclusiveMul: |
| 7466 | case glslang::EOpSubgroupClusteredMul: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7467 | case glslang::EOpSubgroupPartitionedMul: |
| 7468 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7469 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7470 | if (isFloat) { |
| 7471 | opCode = spv::OpGroupNonUniformFMul; |
| 7472 | } else { |
| 7473 | opCode = spv::OpGroupNonUniformIMul; |
| 7474 | } |
| 7475 | break; |
| 7476 | case glslang::EOpSubgroupMin: |
| 7477 | case glslang::EOpSubgroupInclusiveMin: |
| 7478 | case glslang::EOpSubgroupExclusiveMin: |
| 7479 | case glslang::EOpSubgroupClusteredMin: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7480 | case glslang::EOpSubgroupPartitionedMin: |
| 7481 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7482 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7483 | if (isFloat) { |
| 7484 | opCode = spv::OpGroupNonUniformFMin; |
| 7485 | } else if (isUnsigned) { |
| 7486 | opCode = spv::OpGroupNonUniformUMin; |
| 7487 | } else { |
| 7488 | opCode = spv::OpGroupNonUniformSMin; |
| 7489 | } |
| 7490 | break; |
| 7491 | case glslang::EOpSubgroupMax: |
| 7492 | case glslang::EOpSubgroupInclusiveMax: |
| 7493 | case glslang::EOpSubgroupExclusiveMax: |
| 7494 | case glslang::EOpSubgroupClusteredMax: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7495 | case glslang::EOpSubgroupPartitionedMax: |
| 7496 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7497 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7498 | if (isFloat) { |
| 7499 | opCode = spv::OpGroupNonUniformFMax; |
| 7500 | } else if (isUnsigned) { |
| 7501 | opCode = spv::OpGroupNonUniformUMax; |
| 7502 | } else { |
| 7503 | opCode = spv::OpGroupNonUniformSMax; |
| 7504 | } |
| 7505 | break; |
| 7506 | case glslang::EOpSubgroupAnd: |
| 7507 | case glslang::EOpSubgroupInclusiveAnd: |
| 7508 | case glslang::EOpSubgroupExclusiveAnd: |
| 7509 | case glslang::EOpSubgroupClusteredAnd: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7510 | case glslang::EOpSubgroupPartitionedAnd: |
| 7511 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7512 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7513 | if (isBool) { |
| 7514 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 7515 | } else { |
| 7516 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 7517 | } |
| 7518 | break; |
| 7519 | case glslang::EOpSubgroupOr: |
| 7520 | case glslang::EOpSubgroupInclusiveOr: |
| 7521 | case glslang::EOpSubgroupExclusiveOr: |
| 7522 | case glslang::EOpSubgroupClusteredOr: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7523 | case glslang::EOpSubgroupPartitionedOr: |
| 7524 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7525 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7526 | if (isBool) { |
| 7527 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 7528 | } else { |
| 7529 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 7530 | } |
| 7531 | break; |
| 7532 | case glslang::EOpSubgroupXor: |
| 7533 | case glslang::EOpSubgroupInclusiveXor: |
| 7534 | case glslang::EOpSubgroupExclusiveXor: |
| 7535 | case glslang::EOpSubgroupClusteredXor: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7536 | case glslang::EOpSubgroupPartitionedXor: |
| 7537 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7538 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7539 | if (isBool) { |
| 7540 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 7541 | } else { |
| 7542 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 7543 | } |
| 7544 | break; |
| 7545 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 7546 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7547 | case glslang::EOpSubgroupQuadSwapVertical: |
| 7548 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 7549 | default: assert(0 && "Unhandled subgroup operation!"); |
| 7550 | } |
| 7551 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7552 | // get the right Group Operation |
| 7553 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7554 | switch (op) { |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7555 | default: |
| 7556 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7557 | case glslang::EOpSubgroupBallotBitCount: |
| 7558 | case glslang::EOpSubgroupAdd: |
| 7559 | case glslang::EOpSubgroupMul: |
| 7560 | case glslang::EOpSubgroupMin: |
| 7561 | case glslang::EOpSubgroupMax: |
| 7562 | case glslang::EOpSubgroupAnd: |
| 7563 | case glslang::EOpSubgroupOr: |
| 7564 | case glslang::EOpSubgroupXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7565 | groupOperation = spv::GroupOperationReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7566 | break; |
| 7567 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7568 | case glslang::EOpSubgroupInclusiveAdd: |
| 7569 | case glslang::EOpSubgroupInclusiveMul: |
| 7570 | case glslang::EOpSubgroupInclusiveMin: |
| 7571 | case glslang::EOpSubgroupInclusiveMax: |
| 7572 | case glslang::EOpSubgroupInclusiveAnd: |
| 7573 | case glslang::EOpSubgroupInclusiveOr: |
| 7574 | case glslang::EOpSubgroupInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7575 | groupOperation = spv::GroupOperationInclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7576 | break; |
| 7577 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7578 | case glslang::EOpSubgroupExclusiveAdd: |
| 7579 | case glslang::EOpSubgroupExclusiveMul: |
| 7580 | case glslang::EOpSubgroupExclusiveMin: |
| 7581 | case glslang::EOpSubgroupExclusiveMax: |
| 7582 | case glslang::EOpSubgroupExclusiveAnd: |
| 7583 | case glslang::EOpSubgroupExclusiveOr: |
| 7584 | case glslang::EOpSubgroupExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7585 | groupOperation = spv::GroupOperationExclusiveScan; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7586 | break; |
| 7587 | case glslang::EOpSubgroupClusteredAdd: |
| 7588 | case glslang::EOpSubgroupClusteredMul: |
| 7589 | case glslang::EOpSubgroupClusteredMin: |
| 7590 | case glslang::EOpSubgroupClusteredMax: |
| 7591 | case glslang::EOpSubgroupClusteredAnd: |
| 7592 | case glslang::EOpSubgroupClusteredOr: |
| 7593 | case glslang::EOpSubgroupClusteredXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7594 | groupOperation = spv::GroupOperationClusteredReduce; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7595 | break; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7596 | case glslang::EOpSubgroupPartitionedAdd: |
| 7597 | case glslang::EOpSubgroupPartitionedMul: |
| 7598 | case glslang::EOpSubgroupPartitionedMin: |
| 7599 | case glslang::EOpSubgroupPartitionedMax: |
| 7600 | case glslang::EOpSubgroupPartitionedAnd: |
| 7601 | case glslang::EOpSubgroupPartitionedOr: |
| 7602 | case glslang::EOpSubgroupPartitionedXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7603 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7604 | break; |
| 7605 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7606 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7607 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7608 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7609 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7610 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7611 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7612 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7613 | break; |
| 7614 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7615 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7616 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7617 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7618 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7619 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7620 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7621 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7622 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7623 | } |
| 7624 | |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7625 | // build the instruction |
| 7626 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 7627 | |
| 7628 | // Every operation begins with the Execution Scope operand. |
| 7629 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7630 | spvGroupOperands.push_back(executionScope); |
| 7631 | |
| 7632 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 7633 | if (groupOperation != spv::GroupOperationMax) { |
John Kessenich | d122a72 | 2018-09-18 03:43:30 -0600 | [diff] [blame] | 7634 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7635 | spvGroupOperands.push_back(groupOperand); |
| 7636 | } |
| 7637 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7638 | // Push back the operands next. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7639 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 7640 | spv::IdImmediate operand = { true, *opIt }; |
| 7641 | spvGroupOperands.push_back(operand); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7642 | } |
| 7643 | |
| 7644 | // Some opcodes have additional operands. |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7645 | spv::Id directionId = spv::NoResult; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7646 | switch (op) { |
| 7647 | default: break; |
John Kessenich | 149afc3 | 2018-08-14 13:31:43 -0600 | [diff] [blame] | 7648 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 7649 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 7650 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 7651 | } |
| 7652 | if (directionId != spv::NoResult) { |
| 7653 | spv::IdImmediate direction = { true, directionId }; |
| 7654 | spvGroupOperands.push_back(direction); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7655 | } |
| 7656 | |
| 7657 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 7658 | } |
| 7659 | |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7660 | spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, |
| 7661 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7662 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7663 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7664 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7665 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7666 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7667 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7668 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 7669 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7670 | spv::Id typeId0 = 0; |
| 7671 | if (consumedOperands > 0) |
| 7672 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7673 | spv::Id typeId1 = 0; |
| 7674 | if (consumedOperands > 1) |
| 7675 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7676 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7677 | |
| 7678 | switch (op) { |
| 7679 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7680 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7681 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7682 | else if (isUnsigned) |
| 7683 | libCall = spv::GLSLstd450UMin; |
| 7684 | else |
| 7685 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7686 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7687 | break; |
| 7688 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7689 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7690 | break; |
| 7691 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7692 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7693 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7694 | else if (isUnsigned) |
| 7695 | libCall = spv::GLSLstd450UMax; |
| 7696 | else |
| 7697 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7698 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7699 | break; |
| 7700 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7701 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7702 | break; |
| 7703 | case glslang::EOpDot: |
| 7704 | opCode = spv::OpDot; |
| 7705 | break; |
| 7706 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7707 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7708 | break; |
| 7709 | |
| 7710 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7711 | if (isFloat) |
John Kessenich | 605afc7 | 2019-06-17 23:33:09 -0600 | [diff] [blame] | 7712 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7713 | else if (isUnsigned) |
| 7714 | libCall = spv::GLSLstd450UClamp; |
| 7715 | else |
| 7716 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7717 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 7718 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7719 | break; |
| 7720 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7721 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 7722 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7723 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7724 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7725 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 7726 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 7727 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7728 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7729 | break; |
| 7730 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7731 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7732 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7733 | break; |
| 7734 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7735 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 7736 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 7737 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7738 | break; |
| 7739 | |
| 7740 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7741 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7742 | break; |
| 7743 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7744 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7745 | break; |
| 7746 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7747 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7748 | break; |
| 7749 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7750 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7751 | break; |
| 7752 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 7753 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7754 | break; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7755 | case glslang::EOpBarrier: |
| 7756 | { |
| 7757 | // This is for the extended controlBarrier function, with four operands. |
| 7758 | // The unextended barrier() goes through createNoArgOperation. |
| 7759 | assert(operands.size() == 4); |
| 7760 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 7761 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 7762 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7763 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, |
| 7764 | (spv::MemorySemanticsMask)semantics); |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7765 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7766 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7767 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7768 | spv::MemorySemanticsVolatileMask)) { |
| 7769 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7770 | } |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7771 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || |
| 7772 | memoryScope == spv::ScopeDevice)) { |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 7773 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7774 | } |
| 7775 | return 0; |
| 7776 | } |
| 7777 | break; |
| 7778 | case glslang::EOpMemoryBarrier: |
| 7779 | { |
| 7780 | // This is for the extended memoryBarrier function, with three operands. |
| 7781 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 7782 | assert(operands.size() == 3); |
| 7783 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 7784 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 7785 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 7786 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7787 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7788 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7789 | spv::MemorySemanticsVolatileMask)) { |
| 7790 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7791 | } |
| 7792 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 7793 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7794 | } |
| 7795 | return 0; |
| 7796 | } |
| 7797 | break; |
| 7798 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 7799 | #ifndef GLSLANG_WEB |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7800 | case glslang::EOpInterpolateAtSample: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7801 | if (typeProxy == glslang::EbtFloat16) |
| 7802 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7803 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 7804 | break; |
| 7805 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7806 | if (typeProxy == glslang::EbtFloat16) |
| 7807 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 7808 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 7809 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7810 | case glslang::EOpAddCarry: |
| 7811 | opCode = spv::OpIAddCarry; |
| 7812 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7813 | consumedOperands = 2; |
| 7814 | break; |
| 7815 | case glslang::EOpSubBorrow: |
| 7816 | opCode = spv::OpISubBorrow; |
| 7817 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7818 | consumedOperands = 2; |
| 7819 | break; |
| 7820 | case glslang::EOpUMulExtended: |
| 7821 | opCode = spv::OpUMulExtended; |
| 7822 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7823 | consumedOperands = 2; |
| 7824 | break; |
| 7825 | case glslang::EOpIMulExtended: |
| 7826 | opCode = spv::OpSMulExtended; |
| 7827 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7828 | consumedOperands = 2; |
| 7829 | break; |
| 7830 | case glslang::EOpBitfieldExtract: |
| 7831 | if (isUnsigned) |
| 7832 | opCode = spv::OpBitFieldUExtract; |
| 7833 | else |
| 7834 | opCode = spv::OpBitFieldSExtract; |
| 7835 | break; |
| 7836 | case glslang::EOpBitfieldInsert: |
| 7837 | opCode = spv::OpBitFieldInsert; |
| 7838 | break; |
| 7839 | |
| 7840 | case glslang::EOpFma: |
| 7841 | libCall = spv::GLSLstd450Fma; |
| 7842 | break; |
| 7843 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7844 | { |
| 7845 | libCall = spv::GLSLstd450FrexpStruct; |
| 7846 | assert(builder.isPointerType(typeId1)); |
| 7847 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7848 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 7c88aff | 2018-04-11 16:56:50 +0800 | [diff] [blame] | 7849 | if (width == 16) |
| 7850 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 7851 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7852 | if (builder.getNumComponents(operands[0]) == 1) |
| 7853 | frexpIntType = builder.makeIntegerType(width, true); |
| 7854 | else |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 7855 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), |
| 7856 | builder.getNumComponents(operands[0])); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 7857 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 7858 | consumedOperands = 1; |
| 7859 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 7860 | break; |
| 7861 | case glslang::EOpLdexp: |
| 7862 | libCall = spv::GLSLstd450Ldexp; |
| 7863 | break; |
| 7864 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7865 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 7866 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 7867 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7868 | case glslang::EOpSubgroupBroadcast: |
| 7869 | case glslang::EOpSubgroupBallotBitExtract: |
| 7870 | case glslang::EOpSubgroupShuffle: |
| 7871 | case glslang::EOpSubgroupShuffleXor: |
| 7872 | case glslang::EOpSubgroupShuffleUp: |
| 7873 | case glslang::EOpSubgroupShuffleDown: |
| 7874 | case glslang::EOpSubgroupClusteredAdd: |
| 7875 | case glslang::EOpSubgroupClusteredMul: |
| 7876 | case glslang::EOpSubgroupClusteredMin: |
| 7877 | case glslang::EOpSubgroupClusteredMax: |
| 7878 | case glslang::EOpSubgroupClusteredAnd: |
| 7879 | case glslang::EOpSubgroupClusteredOr: |
| 7880 | case glslang::EOpSubgroupClusteredXor: |
| 7881 | case glslang::EOpSubgroupQuadBroadcast: |
Jeff Bolz | 2abe9a4 | 2018-03-29 22:52:17 -0500 | [diff] [blame] | 7882 | case glslang::EOpSubgroupPartitionedAdd: |
| 7883 | case glslang::EOpSubgroupPartitionedMul: |
| 7884 | case glslang::EOpSubgroupPartitionedMin: |
| 7885 | case glslang::EOpSubgroupPartitionedMax: |
| 7886 | case glslang::EOpSubgroupPartitionedAnd: |
| 7887 | case glslang::EOpSubgroupPartitionedOr: |
| 7888 | case glslang::EOpSubgroupPartitionedXor: |
| 7889 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7890 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7891 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7892 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7893 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7894 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7895 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7896 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7897 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7898 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7899 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7900 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7901 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7902 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 7903 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 7904 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7905 | case glslang::EOpSwizzleInvocations: |
| 7906 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7907 | libCall = spv::SwizzleInvocationsAMD; |
| 7908 | break; |
| 7909 | case glslang::EOpSwizzleInvocationsMasked: |
| 7910 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7911 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 7912 | break; |
| 7913 | case glslang::EOpWriteInvocation: |
| 7914 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7915 | libCall = spv::WriteInvocationAMD; |
| 7916 | break; |
| 7917 | |
| 7918 | case glslang::EOpMin3: |
| 7919 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7920 | if (isFloat) |
| 7921 | libCall = spv::FMin3AMD; |
| 7922 | else { |
| 7923 | if (isUnsigned) |
| 7924 | libCall = spv::UMin3AMD; |
| 7925 | else |
| 7926 | libCall = spv::SMin3AMD; |
| 7927 | } |
| 7928 | break; |
| 7929 | case glslang::EOpMax3: |
| 7930 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7931 | if (isFloat) |
| 7932 | libCall = spv::FMax3AMD; |
| 7933 | else { |
| 7934 | if (isUnsigned) |
| 7935 | libCall = spv::UMax3AMD; |
| 7936 | else |
| 7937 | libCall = spv::SMax3AMD; |
| 7938 | } |
| 7939 | break; |
| 7940 | case glslang::EOpMid3: |
| 7941 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7942 | if (isFloat) |
| 7943 | libCall = spv::FMid3AMD; |
| 7944 | else { |
| 7945 | if (isUnsigned) |
| 7946 | libCall = spv::UMid3AMD; |
| 7947 | else |
| 7948 | libCall = spv::SMid3AMD; |
| 7949 | } |
| 7950 | break; |
| 7951 | |
| 7952 | case glslang::EOpInterpolateAtVertex: |
Rex Xu | b4a2a6c | 2018-05-17 13:51:28 +0800 | [diff] [blame] | 7953 | if (typeProxy == glslang::EbtFloat16) |
| 7954 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 7955 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 7956 | libCall = spv::InterpolateAtVertexAMD; |
| 7957 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 7958 | |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7959 | case glslang::EOpReportIntersection: |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7960 | typeId = builder.makeBoolType(); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7961 | opCode = spv::OpReportIntersectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7962 | break; |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 7963 | case glslang::EOpTraceNV: |
| 7964 | builder.createNoResultOp(spv::OpTraceNV, operands); |
| 7965 | return 0; |
| 7966 | case glslang::EOpTraceKHR: |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7967 | builder.createNoResultOp(spv::OpTraceRayKHR, operands); |
Ashwin Lele | ff1783d | 2018-10-22 16:41:44 -0700 | [diff] [blame] | 7968 | return 0; |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 7969 | case glslang::EOpExecuteCallableNV: |
| 7970 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
| 7971 | return 0; |
| 7972 | case glslang::EOpExecuteCallableKHR: |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 7973 | builder.createNoResultOp(spv::OpExecuteCallableKHR, operands); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 7974 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7975 | |
| 7976 | case glslang::EOpRayQueryInitialize: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7977 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR, operands); |
| 7978 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7979 | case glslang::EOpRayQueryTerminate: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7980 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operands); |
| 7981 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7982 | case glslang::EOpRayQueryGenerateIntersection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7983 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR, operands); |
| 7984 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7985 | case glslang::EOpRayQueryConfirmIntersection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7986 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operands); |
| 7987 | return 0; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7988 | case glslang::EOpRayQueryProceed: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7989 | typeId = builder.makeBoolType(); |
| 7990 | opCode = spv::OpRayQueryProceedKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7991 | break; |
| 7992 | case glslang::EOpRayQueryGetIntersectionType: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 7993 | typeId = builder.makeUintType(32); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7994 | opCode = spv::OpRayQueryGetIntersectionTypeKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7995 | break; |
| 7996 | case glslang::EOpRayQueryGetRayTMin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 7997 | typeId = builder.makeFloatType(32); |
| 7998 | opCode = spv::OpRayQueryGetRayTMinKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 7999 | break; |
| 8000 | case glslang::EOpRayQueryGetRayFlags: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8001 | typeId = builder.makeIntType(32); |
| 8002 | opCode = spv::OpRayQueryGetRayFlagsKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8003 | break; |
| 8004 | case glslang::EOpRayQueryGetIntersectionT: |
Neslisah Torosdagli | 50a7228 | 2020-03-20 18:23:27 -0400 | [diff] [blame] | 8005 | typeId = builder.makeFloatType(32); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8006 | opCode = spv::OpRayQueryGetIntersectionTKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8007 | break; |
| 8008 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8009 | typeId = builder.makeIntType(32); |
| 8010 | opCode = spv::OpRayQueryGetIntersectionInstanceCustomIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8011 | break; |
| 8012 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8013 | typeId = builder.makeIntType(32); |
| 8014 | opCode = spv::OpRayQueryGetIntersectionInstanceIdKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8015 | break; |
| 8016 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
Daniel Koch | c0bcfaf | 2020-12-12 12:34:24 -0500 | [diff] [blame] | 8017 | typeId = builder.makeUintType(32); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8018 | opCode = spv::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8019 | break; |
| 8020 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8021 | typeId = builder.makeIntType(32); |
| 8022 | opCode = spv::OpRayQueryGetIntersectionGeometryIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8023 | break; |
| 8024 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8025 | typeId = builder.makeIntType(32); |
| 8026 | opCode = spv::OpRayQueryGetIntersectionPrimitiveIndexKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8027 | break; |
| 8028 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8029 | typeId = builder.makeVectorType(builder.makeFloatType(32), 2); |
| 8030 | opCode = spv::OpRayQueryGetIntersectionBarycentricsKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8031 | break; |
| 8032 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8033 | typeId = builder.makeBoolType(); |
| 8034 | opCode = spv::OpRayQueryGetIntersectionFrontFaceKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8035 | break; |
| 8036 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8037 | typeId = builder.makeBoolType(); |
| 8038 | opCode = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8039 | break; |
| 8040 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8041 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 8042 | opCode = spv::OpRayQueryGetIntersectionObjectRayDirectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8043 | break; |
| 8044 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8045 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 8046 | opCode = spv::OpRayQueryGetIntersectionObjectRayOriginKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8047 | break; |
| 8048 | case glslang::EOpRayQueryGetWorldRayDirection: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8049 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 8050 | opCode = spv::OpRayQueryGetWorldRayDirectionKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8051 | break; |
| 8052 | case glslang::EOpRayQueryGetWorldRayOrigin: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8053 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 8054 | opCode = spv::OpRayQueryGetWorldRayOriginKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8055 | break; |
| 8056 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8057 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8058 | opCode = spv::OpRayQueryGetIntersectionObjectToWorldKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8059 | break; |
| 8060 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8061 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8062 | opCode = spv::OpRayQueryGetIntersectionWorldToObjectKHR; |
Neslisah Torosdagli | cea9384 | 2020-03-25 08:13:32 -0400 | [diff] [blame] | 8063 | break; |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8064 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 8065 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 8066 | return 0; |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 8067 | case glslang::EOpCooperativeMatrixMulAdd: |
| 8068 | opCode = spv::OpCooperativeMatrixMulAddNV; |
| 8069 | break; |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8070 | #endif // GLSLANG_WEB |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8071 | default: |
| 8072 | return 0; |
| 8073 | } |
| 8074 | |
| 8075 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 8076 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 8077 | // Use an extended instruction from the standard library. |
| 8078 | // Construct the call arguments, without modifying the original operands vector. |
| 8079 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 8080 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8081 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 8082 | } else if (opCode == spv::OpDot && !isFloat) { |
| 8083 | // int dot(int, int) |
| 8084 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 8085 | const int componentCount = builder.getNumComponents(operands[0]); |
| 8086 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 8087 | builder.setPrecision(mulOp, precision); |
| 8088 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 8089 | for (int i = 1; i < componentCount; ++i) { |
| 8090 | builder.setPrecision(id, precision); |
John Kessenich | 5de15a2 | 2019-12-26 10:56:54 -0700 | [diff] [blame] | 8091 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(mulOp, typeId, i)); |
t.jung | b16bea8 | 2018-11-15 10:21:36 +0100 | [diff] [blame] | 8092 | } |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 8093 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8094 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8095 | case 0: |
| 8096 | // should all be handled by visitAggregate and createNoArgOperation |
| 8097 | assert(0); |
| 8098 | return 0; |
| 8099 | case 1: |
| 8100 | // should all be handled by createUnaryOperation |
| 8101 | assert(0); |
| 8102 | return 0; |
| 8103 | case 2: |
| 8104 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 8105 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8106 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8107 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 8108 | assert(consumedOperands == operands.size()); |
| 8109 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8110 | break; |
| 8111 | } |
| 8112 | } |
| 8113 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8114 | #ifndef GLSLANG_WEB |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8115 | // Decode the return types that were structures |
| 8116 | switch (op) { |
| 8117 | case glslang::EOpAddCarry: |
| 8118 | case glslang::EOpSubBorrow: |
| 8119 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 8120 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 8121 | break; |
| 8122 | case glslang::EOpUMulExtended: |
| 8123 | case glslang::EOpIMulExtended: |
| 8124 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 8125 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 8126 | break; |
| 8127 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 8128 | { |
| 8129 | assert(operands.size() == 2); |
| 8130 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 8131 | // "exp" is floating-point type (from HLSL intrinsic) |
| 8132 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 8133 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 8134 | builder.createStore(member1, operands[1]); |
| 8135 | } else |
| 8136 | // "exp" is integer type (from GLSL built-in function) |
| 8137 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 8138 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 8139 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8140 | break; |
| 8141 | default: |
| 8142 | break; |
| 8143 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8144 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8145 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 8146 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8147 | } |
| 8148 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8149 | // Intrinsics with no arguments (or no return value, and no precision). |
| 8150 | 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] | 8151 | { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 8152 | // 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] | 8153 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? |
| 8154 | spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8155 | |
| 8156 | switch (op) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8157 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 8158 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 8159 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 8160 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 8161 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 8162 | spv::MemorySemanticsAcquireReleaseMask); |
| 8163 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 8164 | } else { |
| 8165 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 8166 | } |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 8167 | } else { |
| 8168 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 8169 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 8170 | spv::MemorySemanticsAcquireReleaseMask); |
| 8171 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8172 | return 0; |
| 8173 | case glslang::EOpMemoryBarrier: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 8174 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 8175 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8176 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8177 | case glslang::EOpMemoryBarrierBuffer: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 8178 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 8179 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8180 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8181 | case glslang::EOpMemoryBarrierShared: |
Jeff Bolz | 36831c9 | 2018-09-05 10:11:41 -0500 | [diff] [blame] | 8182 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8183 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8184 | return 0; |
| 8185 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 8186 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 8187 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8188 | return 0; |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 8189 | #ifndef GLSLANG_WEB |
| 8190 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 8191 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 8192 | spv::MemorySemanticsAcquireReleaseMask); |
| 8193 | return 0; |
| 8194 | case glslang::EOpMemoryBarrierImage: |
| 8195 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 8196 | spv::MemorySemanticsAcquireReleaseMask); |
| 8197 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8198 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8199 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 8200 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8201 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8202 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8203 | case glslang::EOpDeviceMemoryBarrier: |
| 8204 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 8205 | spv::MemorySemanticsImageMemoryMask | |
| 8206 | spv::MemorySemanticsAcquireReleaseMask); |
| 8207 | return 0; |
| 8208 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 8209 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 8210 | spv::MemorySemanticsImageMemoryMask | |
| 8211 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8212 | return 0; |
| 8213 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8214 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8215 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8216 | return 0; |
| 8217 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 8218 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 8219 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 8220 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 8221 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8222 | case glslang::EOpSubgroupBarrier: |
| 8223 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 8224 | spv::MemorySemanticsAcquireReleaseMask); |
| 8225 | return spv::NoResult; |
| 8226 | case glslang::EOpSubgroupMemoryBarrier: |
| 8227 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 8228 | spv::MemorySemanticsAcquireReleaseMask); |
| 8229 | return spv::NoResult; |
| 8230 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 8231 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 8232 | spv::MemorySemanticsAcquireReleaseMask); |
| 8233 | return spv::NoResult; |
| 8234 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 8235 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 8236 | spv::MemorySemanticsAcquireReleaseMask); |
| 8237 | return spv::NoResult; |
| 8238 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 8239 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8240 | spv::MemorySemanticsAcquireReleaseMask); |
| 8241 | return spv::NoResult; |
John Kessenich | f8d1d74 | 2019-10-21 06:55:11 -0600 | [diff] [blame] | 8242 | |
| 8243 | case glslang::EOpEmitVertex: |
| 8244 | builder.createNoResultOp(spv::OpEmitVertex); |
| 8245 | return 0; |
| 8246 | case glslang::EOpEndPrimitive: |
| 8247 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 8248 | return 0; |
| 8249 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8250 | case glslang::EOpSubgroupElect: { |
| 8251 | std::vector<spv::Id> operands; |
| 8252 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 8253 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8254 | case glslang::EOpTime: |
| 8255 | { |
| 8256 | std::vector<spv::Id> args; // Dummy arguments |
| 8257 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 8258 | return builder.setPrecision(id, precision); |
| 8259 | } |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 8260 | case glslang::EOpIgnoreIntersectionNV: |
| 8261 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 8262 | return 0; |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 8263 | case glslang::EOpTerminateRayNV: |
| 8264 | builder.createNoResultOp(spv::OpTerminateRayNV); |
Chao Chen | b50c02e | 2018-09-19 11:42:24 -0700 | [diff] [blame] | 8265 | return 0; |
Torosdagli | 06c2eee | 2020-03-19 11:09:57 -0400 | [diff] [blame] | 8266 | case glslang::EOpRayQueryInitialize: |
| 8267 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR); |
| 8268 | return 0; |
| 8269 | case glslang::EOpRayQueryTerminate: |
| 8270 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR); |
| 8271 | return 0; |
| 8272 | case glslang::EOpRayQueryGenerateIntersection: |
| 8273 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR); |
| 8274 | return 0; |
| 8275 | case glslang::EOpRayQueryConfirmIntersection: |
| 8276 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR); |
| 8277 | return 0; |
Jeff Bolz | c6f0ce8 | 2019-06-03 11:33:50 -0500 | [diff] [blame] | 8278 | case glslang::EOpBeginInvocationInterlock: |
| 8279 | builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
| 8280 | return 0; |
| 8281 | case glslang::EOpEndInvocationInterlock: |
| 8282 | builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
| 8283 | return 0; |
| 8284 | |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 8285 | case glslang::EOpIsHelperInvocation: |
| 8286 | { |
| 8287 | std::vector<spv::Id> args; // Dummy arguments |
Rex Xu | bb7307b | 2019-07-15 14:57:20 +0800 | [diff] [blame] | 8288 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 8289 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 8290 | return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args); |
Jeff Bolz | ba6170b | 2019-07-01 09:23:23 -0500 | [diff] [blame] | 8291 | } |
| 8292 | |
amhagan | 91fb009 | 2019-07-10 21:14:38 -0400 | [diff] [blame] | 8293 | case glslang::EOpReadClockSubgroupKHR: { |
| 8294 | std::vector<spv::Id> args; |
| 8295 | args.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 8296 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 8297 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 8298 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 8299 | } |
| 8300 | |
| 8301 | case glslang::EOpReadClockDeviceKHR: { |
| 8302 | std::vector<spv::Id> args; |
| 8303 | args.push_back(builder.makeUintConstant(spv::ScopeDevice)); |
| 8304 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 8305 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 8306 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 8307 | } |
John Kessenich | 3dd1ce5 | 2019-10-17 07:08:40 -0600 | [diff] [blame] | 8308 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8309 | default: |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8310 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8311 | } |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8312 | |
| 8313 | logger->missingFunctionality("unknown operation with no arguments"); |
| 8314 | |
| 8315 | return 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8316 | } |
| 8317 | |
| 8318 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 8319 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 8320 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8321 | spv::Id id; |
| 8322 | if (symbolValues.end() != iter) { |
| 8323 | id = iter->second; |
| 8324 | return id; |
| 8325 | } |
| 8326 | |
| 8327 | // it was not found, create it |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8328 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
Daniel Koch | db32b24 | 2020-03-17 20:42:47 -0400 | [diff] [blame] | 8329 | auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType()); |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8330 | id = createSpvVariable(symbol, forcedType.first); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8331 | symbolValues[symbol->getId()] = id; |
John Kessenich | 9c14f77 | 2019-06-17 08:38:35 -0600 | [diff] [blame] | 8332 | if (forcedType.second != spv::NoType) |
| 8333 | forceType[id] = forcedType.second; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8334 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 8335 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8336 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 8337 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 8338 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8339 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8340 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8341 | if (symbol->getQualifier().hasComponent()) |
| 8342 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 8343 | if (symbol->getQualifier().hasIndex()) |
| 8344 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8345 | #endif |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8346 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8347 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 8348 | // atomic counters use this: |
| 8349 | if (symbol->getQualifier().hasOffset()) |
| 8350 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8351 | } |
| 8352 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 8353 | if (symbol->getQualifier().hasLocation()) |
| 8354 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8355 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 8356 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8357 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8358 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8359 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8360 | if (symbol->getQualifier().hasSet()) |
| 8361 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8362 | else if (IsDescriptorResource(symbol->getType())) { |
| 8363 | // default to 0 |
| 8364 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 8365 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8366 | if (symbol->getQualifier().hasBinding()) |
| 8367 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
Jeff Bolz | 0a93cfb | 2018-12-11 20:53:59 -0600 | [diff] [blame] | 8368 | else if (IsDescriptorResource(symbol->getType())) { |
| 8369 | // default to 0 |
| 8370 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 8371 | } |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8372 | if (symbol->getQualifier().hasAttachment()) |
| 8373 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8374 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 8375 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 8376 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8377 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 8378 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 8379 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 8380 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 8381 | } |
| 8382 | if (symbol->getQualifier().hasXfbOffset()) |
| 8383 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8384 | } |
| 8385 | |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8386 | // add built-in variable decoration |
| 8387 | if (builtIn != spv::BuiltInMax) { |
| 8388 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
| 8389 | } |
| 8390 | |
| 8391 | #ifndef GLSLANG_WEB |
Daniel Koch | ffccefd | 2020-11-23 15:41:27 -0500 | [diff] [blame] | 8392 | // Subgroup builtins which have input storage class are volatile for ray tracing stages. |
| 8393 | if (symbol->getType().isImage() || symbol->getQualifier().isPipeInput()) { |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8394 | std::vector<spv::Decoration> memory; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8395 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, |
| 8396 | glslangIntermediate->usingVulkanMemoryModel()); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8397 | for (unsigned int i = 0; i < memory.size(); ++i) |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8398 | builder.addDecoration(id, memory[i]); |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 8399 | } |
| 8400 | |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8401 | if (builtIn == spv::BuiltInSampleMask) { |
| 8402 | spv::Decoration decoration; |
| 8403 | // GL_NV_sample_mask_override_coverage extension |
| 8404 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8405 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8406 | else |
| 8407 | decoration = (spv::Decoration)spv::DecorationMax; |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8408 | builder.addDecoration(id, decoration); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8409 | if (decoration != spv::DecorationMax) { |
Jason Macnak | dbd4c3c | 2019-07-12 14:33:02 -0700 | [diff] [blame] | 8410 | builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV); |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8411 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 8412 | } |
| 8413 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8414 | else if (builtIn == spv::BuiltInLayer) { |
| 8415 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 8416 | if (symbol->getQualifier().layoutViewportRelative) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8417 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8418 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 8419 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 8420 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 8421 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8422 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 8423 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8424 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 8425 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 8426 | } |
| 8427 | } |
| 8428 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 8429 | if (symbol->getQualifier().layoutPassthrough) { |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8430 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 8431 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 8432 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 8433 | } |
Chao Chen | 9eada4b | 2018-09-19 11:39:56 -0700 | [diff] [blame] | 8434 | if (symbol->getQualifier().pervertexNV) { |
| 8435 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 8436 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 8437 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 8438 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 8439 | |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8440 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 8441 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 8442 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 8443 | symbol->getType().getQualifier().semanticName); |
| 8444 | } |
| 8445 | |
John Kessenich | 7015bd6 | 2019-08-01 03:28:08 -0600 | [diff] [blame] | 8446 | if (symbol->isReference()) { |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8447 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? |
| 8448 | spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8449 | } |
John Kessenich | b9197c8 | 2019-08-11 07:41:45 -0600 | [diff] [blame] | 8450 | #endif |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8451 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8452 | return id; |
| 8453 | } |
| 8454 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8455 | #ifndef GLSLANG_WEB |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8456 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 8457 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 8458 | { |
| 8459 | if (member >= 0) { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8460 | if (qualifier.perPrimitiveNV) { |
| 8461 | // Need to add capability/extension for fragment shader. |
| 8462 | // Mesh shader already adds this by default. |
| 8463 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 8464 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 8465 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 8466 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8467 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8468 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8469 | if (qualifier.perViewNV) |
| 8470 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 8471 | if (qualifier.perTaskNV) |
| 8472 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 8473 | } else { |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8474 | if (qualifier.perPrimitiveNV) { |
| 8475 | // Need to add capability/extension for fragment shader. |
| 8476 | // Mesh shader already adds this by default. |
| 8477 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 8478 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 8479 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 8480 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8481 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
Sahil Parmar | 38772c0 | 2018-10-25 23:50:59 -0700 | [diff] [blame] | 8482 | } |
Chao Chen | 3c36699 | 2018-09-19 11:41:59 -0700 | [diff] [blame] | 8483 | if (qualifier.perViewNV) |
| 8484 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 8485 | if (qualifier.perTaskNV) |
| 8486 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 8487 | } |
| 8488 | } |
| 8489 | #endif |
| 8490 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8491 | // 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] | 8492 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8493 | // |
| 8494 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 8495 | // |
| 8496 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 8497 | // regular constants, which themselves are trees that createSpvConstant() |
| 8498 | // recursively walks. So, this function walks the "top" of the tree: |
| 8499 | // - emit specialization constant-building instructions for specConstant |
| 8500 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8501 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8502 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 8503 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8504 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8505 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8506 | if (! node.getQualifier().specConstant) { |
| 8507 | // hand off to the non-spec-constant path |
| 8508 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 8509 | int nextConst = 0; |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8510 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? |
| 8511 | node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
| 8512 | nextConst, false); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8513 | } |
| 8514 | |
| 8515 | // We now know we have a specialization constant to build |
| 8516 | |
Ricardo Garcia | 232ba0d | 2020-06-03 15:52:55 +0200 | [diff] [blame] | 8517 | // Extra capabilities may be needed. |
| 8518 | if (node.getType().contains8BitInt()) |
| 8519 | builder.addCapability(spv::CapabilityInt8); |
| 8520 | if (node.getType().contains16BitFloat()) |
| 8521 | builder.addCapability(spv::CapabilityFloat16); |
| 8522 | if (node.getType().contains16BitInt()) |
| 8523 | builder.addCapability(spv::CapabilityInt16); |
| 8524 | if (node.getType().contains64BitInt()) |
| 8525 | builder.addCapability(spv::CapabilityInt64); |
| 8526 | if (node.getType().containsDouble()) |
| 8527 | builder.addCapability(spv::CapabilityFloat64); |
| 8528 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 8529 | // 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] | 8530 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 8531 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 8532 | std::vector<spv::Id> dimConstId; |
| 8533 | for (int dim = 0; dim < 3; ++dim) { |
| 8534 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 8535 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
John Kessenich | 5d610ee | 2018-03-07 18:05:55 -0700 | [diff] [blame] | 8536 | if (specConst) { |
| 8537 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 8538 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 8539 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8540 | } |
| 8541 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 8542 | } |
| 8543 | |
| 8544 | // An AST node labelled as specialization constant should be a symbol node. |
| 8545 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 8546 | if (auto* sn = node.getAsSymbolNode()) { |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8547 | spv::Id result; |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8548 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 8549 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 8550 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 8551 | // will set the builder into spec constant op instruction generating mode. |
| 8552 | sub_tree->traverse(this); |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8553 | result = accessChainLoad(sub_tree->getType()); |
| 8554 | } else if (auto* const_union_array = &sn->getConstArray()) { |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8555 | int nextConst = 0; |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8556 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 8557 | } else { |
| 8558 | logger->missingFunctionality("Invalid initializer for spec onstant."); |
Dan Sinclair | 70661b9 | 2018-11-12 13:56:52 -0500 | [diff] [blame] | 8559 | return spv::NoResult; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8560 | } |
Grigory Dzhavadyan | 4c9876b | 2018-10-29 22:56:44 -0700 | [diff] [blame] | 8561 | builder.addName(result, sn->getName().c_str()); |
| 8562 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 8563 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8564 | |
| 8565 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 8566 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8567 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 8568 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8569 | } |
| 8570 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8571 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 8572 | // build the aggregate SPIR-V constant. |
| 8573 | // |
| 8574 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 8575 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 8576 | // |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8577 | spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, |
| 8578 | const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8579 | { |
| 8580 | // vector of constants for SPIR-V |
| 8581 | std::vector<spv::Id> spvConsts; |
| 8582 | |
| 8583 | // Type is used for struct and array constants |
| 8584 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 8585 | |
| 8586 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 8587 | glslang::TType elementType(glslangType, 0); |
| 8588 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8589 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8590 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 8591 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8592 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8593 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
Jeff Bolz | 4605e2e | 2019-02-19 13:10:32 -0600 | [diff] [blame] | 8594 | } else if (glslangType.isCoopMat()) { |
| 8595 | glslang::TType componentType(glslangType.getBasicType()); |
| 8596 | spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
Jeff Bolz | 9f2aec4 | 2019-01-06 17:58:04 -0600 | [diff] [blame] | 8597 | } else if (glslangType.isStruct()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8598 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 8599 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 8600 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 8601 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8602 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 8603 | bool zero = nextConst >= consts.size(); |
| 8604 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8605 | case glslang::EbtInt: |
| 8606 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 8607 | break; |
| 8608 | case glslang::EbtUint: |
| 8609 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 8610 | break; |
| 8611 | case glslang::EbtFloat: |
| 8612 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 8613 | break; |
| 8614 | case glslang::EbtBool: |
| 8615 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 8616 | break; |
| 8617 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8618 | case glslang::EbtInt8: |
| 8619 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 8620 | break; |
| 8621 | case glslang::EbtUint8: |
| 8622 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 8623 | break; |
| 8624 | case glslang::EbtInt16: |
| 8625 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 8626 | break; |
| 8627 | case glslang::EbtUint16: |
| 8628 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 8629 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 8630 | case glslang::EbtInt64: |
| 8631 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 8632 | break; |
| 8633 | case glslang::EbtUint64: |
| 8634 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 8635 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8636 | case glslang::EbtDouble: |
| 8637 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 8638 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 8639 | case glslang::EbtFloat16: |
| 8640 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 8641 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8642 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8643 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8644 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8645 | break; |
| 8646 | } |
| 8647 | ++nextConst; |
| 8648 | } |
| 8649 | } else { |
| 8650 | // we have a non-aggregate (scalar) constant |
| 8651 | bool zero = nextConst >= consts.size(); |
| 8652 | spv::Id scalar = 0; |
| 8653 | switch (glslangType.getBasicType()) { |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8654 | case glslang::EbtInt: |
| 8655 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
| 8656 | break; |
| 8657 | case glslang::EbtUint: |
| 8658 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
| 8659 | break; |
| 8660 | case glslang::EbtFloat: |
| 8661 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8662 | break; |
| 8663 | case glslang::EbtBool: |
| 8664 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
| 8665 | break; |
| 8666 | #ifndef GLSLANG_WEB |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 8667 | case glslang::EbtInt8: |
| 8668 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 8669 | break; |
| 8670 | case glslang::EbtUint8: |
| 8671 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 8672 | break; |
| 8673 | case glslang::EbtInt16: |
| 8674 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 8675 | break; |
| 8676 | case glslang::EbtUint16: |
| 8677 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 8678 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 8679 | case glslang::EbtInt64: |
| 8680 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 8681 | break; |
| 8682 | case glslang::EbtUint64: |
| 8683 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8684 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8685 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8686 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8687 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 8688 | case glslang::EbtFloat16: |
| 8689 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8690 | break; |
Jeff Bolz | 3fd1232 | 2019-03-05 23:27:09 -0600 | [diff] [blame] | 8691 | case glslang::EbtReference: |
| 8692 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8693 | scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar); |
| 8694 | break; |
John Kessenich | 39697cd | 2019-08-08 10:35:51 -0600 | [diff] [blame] | 8695 | #endif |
Jeff Bolz | 04d7373 | 2019-05-31 13:06:01 -0500 | [diff] [blame] | 8696 | case glslang::EbtString: |
| 8697 | scalar = builder.getStringId(consts[nextConst].getSConst()->c_str()); |
| 8698 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8699 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8700 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8701 | break; |
| 8702 | } |
| 8703 | ++nextConst; |
| 8704 | return scalar; |
| 8705 | } |
| 8706 | |
| 8707 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 8708 | } |
| 8709 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8710 | // Return true if the node is a constant or symbol whose reading has no |
| 8711 | // non-trivial observable cost or effect. |
| 8712 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 8713 | { |
| 8714 | // don't know what this is |
| 8715 | if (node == nullptr) |
| 8716 | return false; |
| 8717 | |
| 8718 | // a constant is safe |
| 8719 | if (node->getAsConstantUnion() != nullptr) |
| 8720 | return true; |
| 8721 | |
| 8722 | // not a symbol means non-trivial |
| 8723 | if (node->getAsSymbolNode() == nullptr) |
| 8724 | return false; |
| 8725 | |
| 8726 | // a symbol, depends on what's being read |
| 8727 | switch (node->getType().getQualifier().storage) { |
| 8728 | case glslang::EvqTemporary: |
| 8729 | case glslang::EvqGlobal: |
| 8730 | case glslang::EvqIn: |
| 8731 | case glslang::EvqInOut: |
| 8732 | case glslang::EvqConst: |
| 8733 | case glslang::EvqConstReadOnly: |
| 8734 | case glslang::EvqUniform: |
| 8735 | return true; |
| 8736 | default: |
| 8737 | return false; |
| 8738 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 8739 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8740 | |
| 8741 | // 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] | 8742 | // 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] | 8743 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8744 | // Return true if trivial. |
| 8745 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 8746 | { |
| 8747 | if (node == nullptr) |
| 8748 | return false; |
| 8749 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 8750 | // count non scalars as trivial, as well as anything coming from HLSL |
| 8751 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 8752 | return true; |
| 8753 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8754 | // symbols and constants are trivial |
| 8755 | if (isTrivialLeaf(node)) |
| 8756 | return true; |
| 8757 | |
| 8758 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 8759 | |
| 8760 | // not a simple operation |
| 8761 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 8762 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 8763 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 8764 | return false; |
| 8765 | |
| 8766 | // not on leaf nodes |
| 8767 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 8768 | return false; |
| 8769 | |
| 8770 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 8771 | return false; |
| 8772 | } |
| 8773 | |
| 8774 | switch (node->getAsOperator()->getOp()) { |
| 8775 | case glslang::EOpLogicalNot: |
| 8776 | case glslang::EOpConvIntToBool: |
| 8777 | case glslang::EOpConvUintToBool: |
| 8778 | case glslang::EOpConvFloatToBool: |
| 8779 | case glslang::EOpConvDoubleToBool: |
| 8780 | case glslang::EOpEqual: |
| 8781 | case glslang::EOpNotEqual: |
| 8782 | case glslang::EOpLessThan: |
| 8783 | case glslang::EOpGreaterThan: |
| 8784 | case glslang::EOpLessThanEqual: |
| 8785 | case glslang::EOpGreaterThanEqual: |
| 8786 | case glslang::EOpIndexDirect: |
| 8787 | case glslang::EOpIndexDirectStruct: |
| 8788 | case glslang::EOpLogicalXor: |
| 8789 | case glslang::EOpAny: |
| 8790 | case glslang::EOpAll: |
| 8791 | return true; |
| 8792 | default: |
| 8793 | return false; |
| 8794 | } |
| 8795 | } |
| 8796 | |
| 8797 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 8798 | // the left side is true (for &&) or false (for ||). |
John Kessenich | 8985fc9 | 2020-03-03 10:25:07 -0700 | [diff] [blame] | 8799 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, |
| 8800 | glslang::TIntermTyped& right) |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8801 | { |
| 8802 | spv::Id boolTypeId = builder.makeBoolType(); |
| 8803 | |
| 8804 | // emit left operand |
| 8805 | builder.clearAccessChain(); |
| 8806 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8807 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8808 | |
| 8809 | // Operands to accumulate OpPhi operands |
| 8810 | std::vector<spv::Id> phiOperands; |
| 8811 | // accumulate left operand's phi information |
| 8812 | phiOperands.push_back(leftId); |
| 8813 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8814 | |
| 8815 | // Make the two kinds of operation symmetric with a "!" |
| 8816 | // || => emit "if (! left) result = right" |
| 8817 | // && => emit "if ( left) result = right" |
| 8818 | // |
| 8819 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 8820 | // to 'builder' to have an "else" without an "then" |
| 8821 | if (op == glslang::EOpLogicalOr) |
| 8822 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 8823 | |
| 8824 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 8825 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8826 | |
| 8827 | // emit right operand as the "then" part of the "if" |
| 8828 | builder.clearAccessChain(); |
| 8829 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 8830 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 8831 | |
| 8832 | // accumulate left operand's phi information |
| 8833 | phiOperands.push_back(rightId); |
| 8834 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8835 | |
| 8836 | // finish the "if" |
| 8837 | ifBuilder.makeEndIf(); |
| 8838 | |
| 8839 | // phi together the two results |
| 8840 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 8841 | } |
| 8842 | |
John Kessenich | a28f7a7 | 2019-08-06 07:00:58 -0600 | [diff] [blame] | 8843 | #ifndef GLSLANG_WEB |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8844 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 8845 | // Import this set if it has not been imported yet. |
| 8846 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 8847 | { |
| 8848 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 8849 | return extBuiltinMap[name]; |
| 8850 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 8851 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8852 | spv::Id extBuiltins = builder.import(name); |
| 8853 | extBuiltinMap[name] = extBuiltins; |
| 8854 | return extBuiltins; |
| 8855 | } |
| 8856 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 8857 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 8858 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8859 | }; // end anonymous namespace |
| 8860 | |
| 8861 | namespace glslang { |
| 8862 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8863 | void GetSpirvVersion(std::string& version) |
| 8864 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 8865 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 8866 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 8867 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8868 | version = buf; |
| 8869 | } |
| 8870 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8871 | // For low-order part of the generator's magic number. Bump up |
| 8872 | // when there is a change in the style (e.g., if SSA form changes, |
| 8873 | // or a different instruction sequence to do something gets used). |
| 8874 | int GetSpirvGeneratorVersion() |
| 8875 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 8876 | // return 1; // start |
| 8877 | // 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] | 8878 | // 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] | 8879 | // 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] | 8880 | // 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] | 8881 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 8882 | // 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] | 8883 | // 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] | 8884 | // return 8; // switch to new dead block eliminator; use OpUnreachable |
Graeme Leese | 060882f | 2020-06-22 11:03:46 +0100 | [diff] [blame] | 8885 | // return 9; // don't include opaque function parameters in OpEntryPoint global's operand list |
| 8886 | return 10; // Generate OpFUnordNotEqual for != comparisons |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 8887 | } |
| 8888 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8889 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8890 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8891 | { |
| 8892 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 8893 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8894 | if (out.fail()) |
| 8895 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8896 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 8897 | unsigned int word = spirv[i]; |
| 8898 | out.write((const char*)&word, 4); |
| 8899 | } |
| 8900 | out.close(); |
| 8901 | } |
| 8902 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8903 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 8904 | 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] | 8905 | { |
Shahbaz Youssefi | 1ef2e25 | 2020-07-03 15:42:53 -0400 | [diff] [blame] | 8906 | #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE) |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8907 | std::ofstream out; |
| 8908 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 8909 | if (out.fail()) |
| 8910 | printf("ERROR: Failed to open file: %s\n", baseName); |
Ben Clayton | fbe9a23 | 2020-06-17 11:17:19 +0100 | [diff] [blame] | 8911 | out << "\t// " << |
| 8912 | GetSpirvGeneratorVersion() << |
| 8913 | GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH << |
| 8914 | GLSLANG_VERSION_FLAVOR << std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8915 | if (varName != nullptr) { |
| 8916 | out << "\t #pragma once" << std::endl; |
| 8917 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 8918 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8919 | const int WORDS_PER_LINE = 8; |
| 8920 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 8921 | out << "\t"; |
| 8922 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 8923 | const unsigned int word = spirv[i + j]; |
| 8924 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 8925 | if (i + j + 1 < (int)spirv.size()) { |
| 8926 | out << ","; |
| 8927 | } |
| 8928 | } |
| 8929 | out << std::endl; |
| 8930 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8931 | if (varName != nullptr) { |
| 8932 | out << "};"; |
johnkslang | f881f08 | 2020-08-04 02:13:50 -0600 | [diff] [blame] | 8933 | out << std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 8934 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8935 | out.close(); |
John Kessenich | 155d351 | 2019-08-08 23:29:20 -0600 | [diff] [blame] | 8936 | #endif |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 8937 | } |
| 8938 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8939 | // |
| 8940 | // Set up the glslang traversal |
| 8941 | // |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8942 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8943 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 8944 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8945 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8946 | } |
| 8947 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8948 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8949 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 8950 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8951 | TIntermNode* root = intermediate.getTreeRoot(); |
| 8952 | |
| 8953 | if (root == 0) |
| 8954 | return; |
| 8955 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8956 | SpvOptions defaultOptions; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 8957 | if (options == nullptr) |
| 8958 | options = &defaultOptions; |
| 8959 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8960 | GetThreadPoolAllocator().push(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8961 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 8962 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8963 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 8964 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8965 | it.dumpSpv(spirv); |
| 8966 | |
GregF | fb03a55 | 2018-03-29 11:49:14 -0600 | [diff] [blame] | 8967 | #if ENABLE_OPT |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8968 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 8969 | // eg. forward and remove memory writes of opaque types. |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8970 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
Shahbaz Youssefi | d52dce5 | 2020-06-17 12:47:44 -0400 | [diff] [blame] | 8971 | if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) { |
| 8972 | SpirvToolsTransform(intermediate, spirv, logger, options); |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8973 | prelegalization = false; |
| 8974 | } |
Shahbaz Youssefi | d52dce5 | 2020-06-17 12:47:44 -0400 | [diff] [blame] | 8975 | else if (options->stripDebugInfo) { |
| 8976 | // Strip debug info even if optimization is disabled. |
| 8977 | SpirvToolsStripDebugInfo(intermediate, spirv, logger); |
| 8978 | } |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8979 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8980 | if (options->validate) |
Jeff Bolz | fd556e3 | 2019-06-07 14:42:08 -0500 | [diff] [blame] | 8981 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8982 | |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8983 | if (options->disassemble) |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8984 | SpirvToolsDisassemble(std::cout, spirv); |
John Kessenich | 717c80a | 2018-08-23 15:17:10 -0600 | [diff] [blame] | 8985 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 8986 | #endif |
| 8987 | |
John Kessenich | 4e11b61 | 2018-08-30 16:56:59 -0600 | [diff] [blame] | 8988 | GetThreadPoolAllocator().pop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 8989 | } |
| 8990 | |
| 8991 | }; // end namespace glslang |