John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2 | // Copyright (C) 2014-2016 LunarG, Inc. |
| 3 | // Copyright (C) 2015-2016 Google, Inc. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4 | // Copyright (C) 2017 ARM Limited. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 6 | // All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 7 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions |
| 10 | // are met: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 11 | // |
| 12 | // Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // |
| 15 | // Redistributions in binary form must reproduce the above |
| 16 | // copyright notice, this list of conditions and the following |
| 17 | // disclaimer in the documentation and/or other materials provided |
| 18 | // with the distribution. |
| 19 | // |
| 20 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 21 | // contributors may be used to endorse or promote products derived |
| 22 | // from this software without specific prior written permission. |
| 23 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 24 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 34 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 | // POSSIBILITY OF SUCH DAMAGE. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 36 | |
| 37 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 38 | // Visit the nodes in the glslang intermediate tree representation to |
| 39 | // translate them to SPIR-V. |
| 40 | // |
| 41 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 42 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 43 | #include "GlslangToSpv.h" |
| 44 | #include "SpvBuilder.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 45 | namespace spv { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 46 | #include "GLSL.std.450.h" |
| 47 | #include "GLSL.ext.KHR.h" |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 48 | #include "GLSL.ext.EXT.h" |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 49 | #ifdef AMD_EXTENSIONS |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 50 | #include "GLSL.ext.AMD.h" |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 51 | #endif |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 52 | #ifdef NV_EXTENSIONS |
| 53 | #include "GLSL.ext.NV.h" |
| 54 | #endif |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 55 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 56 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 57 | #ifdef ENABLE_OPT |
| 58 | #include "spirv-tools/optimizer.hpp" |
| 59 | #include "message.h" |
| 60 | #include "SPVRemapper.h" |
| 61 | #endif |
| 62 | |
| 63 | #ifdef ENABLE_OPT |
| 64 | using namespace spvtools; |
| 65 | #endif |
| 66 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 67 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 68 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 69 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 70 | #include "../glslang/Include/Common.h" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 71 | #include "../glslang/Include/revision.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 72 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 73 | #include <fstream> |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 74 | #include <iomanip> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 75 | #include <list> |
| 76 | #include <map> |
| 77 | #include <stack> |
| 78 | #include <string> |
| 79 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 80 | |
| 81 | namespace { |
| 82 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 83 | namespace { |
| 84 | class SpecConstantOpModeGuard { |
| 85 | public: |
| 86 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 87 | : builder_(builder) { |
| 88 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 89 | } |
| 90 | ~SpecConstantOpModeGuard() { |
| 91 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 92 | : builder_->setToNormalCodeGenMode(); |
| 93 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 94 | void turnOnSpecConstantOpMode() { |
| 95 | builder_->setToSpecConstCodeGenMode(); |
| 96 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 97 | |
| 98 | private: |
| 99 | spv::Builder* builder_; |
| 100 | bool previous_flag_; |
| 101 | }; |
| 102 | } |
| 103 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 104 | // |
| 105 | // The main holder of information for translating glslang to SPIR-V. |
| 106 | // |
| 107 | // Derives from the AST walking base class. |
| 108 | // |
| 109 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 110 | public: |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 111 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 112 | glslang::SpvOptions& options); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 113 | virtual ~TGlslangToSpvTraverser() { } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 114 | |
| 115 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 116 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 117 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 118 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 119 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 120 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 121 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 122 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 123 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 124 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 125 | void finishSpv(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 126 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 127 | |
| 128 | protected: |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 129 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 130 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 131 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 132 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 133 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 134 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 135 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, unsigned int& dependencyLength) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 136 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 137 | spv::Id createSpvVariable(const glslang::TIntermSymbol*); |
| 138 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 139 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 140 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 141 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 142 | spv::Id convertGlslangToSpvType(const glslang::TType& type); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 143 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 144 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 145 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 146 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 147 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 148 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 149 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 150 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 151 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 152 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 153 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 154 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 155 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 156 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 157 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 158 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 159 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 160 | bool writableParam(glslang::TStorageQualifier); |
| 161 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 162 | void makeFunctions(const glslang::TIntermSequence&); |
| 163 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 164 | void visitFunctions(const glslang::TIntermSequence&); |
| 165 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 166 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 167 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 168 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 169 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 170 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 171 | spv::Id createBinaryOperation(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right, glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 172 | spv::Id createBinaryMatrixOperation(spv::Op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right); |
| 173 | spv::Id createUnaryOperation(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand,glslang::TBasicType typeProxy); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 174 | spv::Id createUnaryMatrixOperation(spv::Op op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand,glslang::TBasicType typeProxy); |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 175 | spv::Id createConversion(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id destTypeId, spv::Id operand, glslang::TBasicType typeProxy); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 176 | spv::Id createConversionOperation(glslang::TOperator op, spv::Id operand, int vectorSize); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 177 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 178 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 179 | spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 180 | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 181 | spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 182 | spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 183 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 184 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
| 185 | void addDecoration(spv::Id id, spv::Decoration dec); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 186 | void addDecoration(spv::Id id, spv::Decoration dec, unsigned value); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 187 | void addMemberDecoration(spv::Id id, int member, spv::Decoration dec); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 188 | void addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 189 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 190 | spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, int& nextConst, bool specConstant); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 191 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 192 | bool isTrivial(const glslang::TIntermTyped* node); |
| 193 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 194 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 195 | spv::Id getExtBuiltins(const char* name); |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 196 | #endif |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 197 | void addPre13Extension(const char* ext) |
| 198 | { |
| 199 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 200 | builder.addExtension(ext); |
| 201 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 202 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 203 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 204 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 205 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 206 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 207 | int sequenceDepth; |
| 208 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 209 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 210 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 211 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 212 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 213 | bool inEntryPoint; |
| 214 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 215 | bool linkageOnly; // true when visiting the set of objects in the AST present only for establishing interface, whether or not they were statically used |
John Kessenich | 59420fd | 2015-12-21 11:45:34 -0700 | [diff] [blame] | 216 | 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] | 217 | const glslang::TIntermediate* glslangIntermediate; |
| 218 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 219 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 220 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 221 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 222 | std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, rather than a pointer |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 223 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 224 | std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount]; |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 225 | std::unordered_map<const glslang::TTypeList*, std::vector<int> > memberRemapper; // for mapping glslang block indices to spv indices (e.g., due to hidden members) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 226 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | // |
| 230 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 231 | // |
| 232 | |
| 233 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 234 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 235 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 236 | switch (source) { |
| 237 | case glslang::EShSourceGlsl: |
| 238 | switch (profile) { |
| 239 | case ENoProfile: |
| 240 | case ECoreProfile: |
| 241 | case ECompatibilityProfile: |
| 242 | return spv::SourceLanguageGLSL; |
| 243 | case EEsProfile: |
| 244 | return spv::SourceLanguageESSL; |
| 245 | default: |
| 246 | return spv::SourceLanguageUnknown; |
| 247 | } |
| 248 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 249 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 250 | default: |
| 251 | return spv::SourceLanguageUnknown; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Translate glslang language (stage) to SPIR-V execution model. |
| 256 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 257 | { |
| 258 | switch (stage) { |
| 259 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 260 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 261 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 262 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 263 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 264 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
| 265 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 266 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 267 | return spv::ExecutionModelFragment; |
| 268 | } |
| 269 | } |
| 270 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 271 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 272 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 273 | { |
| 274 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 275 | case glslang::Esd1D: return spv::Dim1D; |
| 276 | case glslang::Esd2D: return spv::Dim2D; |
| 277 | case glslang::Esd3D: return spv::Dim3D; |
| 278 | case glslang::EsdCube: return spv::DimCube; |
| 279 | case glslang::EsdRect: return spv::DimRect; |
| 280 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 281 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 282 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 283 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 284 | return spv::Dim2D; |
| 285 | } |
| 286 | } |
| 287 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 288 | // Translate glslang precision to SPIR-V precision decorations. |
| 289 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 290 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 291 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 292 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 293 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 294 | default: |
| 295 | return spv::NoPrecision; |
| 296 | } |
| 297 | } |
| 298 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 299 | // Translate glslang type to SPIR-V precision decorations. |
| 300 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 301 | { |
| 302 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 303 | } |
| 304 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 305 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 306 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 307 | { |
| 308 | if (type.getBasicType() == glslang::EbtBlock) { |
| 309 | switch (type.getQualifier().storage) { |
| 310 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 311 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 312 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 313 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
| 314 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 315 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 320 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 321 | } |
| 322 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 323 | // Translate glslang type to SPIR-V memory decorations. |
| 324 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory) |
| 325 | { |
| 326 | if (qualifier.coherent) |
| 327 | memory.push_back(spv::DecorationCoherent); |
| 328 | if (qualifier.volatil) |
| 329 | memory.push_back(spv::DecorationVolatile); |
| 330 | if (qualifier.restrict) |
| 331 | memory.push_back(spv::DecorationRestrict); |
| 332 | if (qualifier.readonly) |
| 333 | memory.push_back(spv::DecorationNonWritable); |
| 334 | if (qualifier.writeonly) |
| 335 | memory.push_back(spv::DecorationNonReadable); |
| 336 | } |
| 337 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 338 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 339 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 340 | { |
| 341 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 342 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 343 | case glslang::ElmRowMajor: |
| 344 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 345 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 346 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 347 | default: |
| 348 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 349 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 350 | } |
| 351 | } else { |
| 352 | switch (type.getBasicType()) { |
| 353 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 354 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 355 | break; |
| 356 | case glslang::EbtBlock: |
| 357 | switch (type.getQualifier().storage) { |
| 358 | case glslang::EvqUniform: |
| 359 | case glslang::EvqBuffer: |
| 360 | switch (type.getQualifier().layoutPacking) { |
| 361 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 362 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 363 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 364 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 365 | } |
| 366 | case glslang::EvqVaryingIn: |
| 367 | case glslang::EvqVaryingOut: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 368 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 369 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 370 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 371 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 372 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 379 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 380 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 381 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 382 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 383 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 384 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 385 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 386 | else if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 387 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 388 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 389 | return spv::DecorationFlat; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 390 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 391 | else if (qualifier.explicitInterp) { |
| 392 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 393 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 394 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 395 | #endif |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 396 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 397 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 401 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 402 | // should be applied. |
| 403 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 404 | { |
| 405 | if (qualifier.patch) |
| 406 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 407 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 408 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 409 | else if (qualifier.sample) { |
| 410 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 411 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 412 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 413 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 414 | } |
| 415 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 416 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 417 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 418 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 419 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 420 | return spv::DecorationInvariant; |
| 421 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 422 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 423 | } |
| 424 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 425 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 426 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 427 | { |
| 428 | if (qualifier.noContraction) |
| 429 | return spv::DecorationNoContraction; |
| 430 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 431 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 432 | } |
| 433 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 434 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 435 | // associated capabilities when required. For some built-in variables, a capability |
| 436 | // is generated only when using the variable in an executable instruction, but not when |
| 437 | // just declaring a struct member variable with it. This is true for PointSize, |
| 438 | // ClipDistance, and CullDistance. |
| 439 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 440 | { |
| 441 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 442 | case glslang::EbvPointSize: |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 443 | // Defer adding the capability until the built-in is actually used. |
| 444 | if (! memberDeclaration) { |
| 445 | switch (glslangIntermediate->getStage()) { |
| 446 | case EShLangGeometry: |
| 447 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 448 | break; |
| 449 | case EShLangTessControl: |
| 450 | case EShLangTessEvaluation: |
| 451 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 452 | break; |
| 453 | default: |
| 454 | break; |
| 455 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 456 | } |
| 457 | return spv::BuiltInPointSize; |
| 458 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 459 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 460 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 461 | // They are now generated when used, rather than here when declared. |
| 462 | // Potentially, the specification should be more clear what the minimum |
| 463 | // use needed is to trigger the capability. |
| 464 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 465 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 466 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 467 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 468 | return spv::BuiltInClipDistance; |
| 469 | |
| 470 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 471 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 472 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 473 | return spv::BuiltInCullDistance; |
| 474 | |
| 475 | case glslang::EbvViewportIndex: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 476 | builder.addCapability(spv::CapabilityMultiViewport); |
| 477 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 478 | glslangIntermediate->getStage() == EShLangTessControl || |
| 479 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 480 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 481 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 482 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 483 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 484 | return spv::BuiltInViewportIndex; |
| 485 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 486 | case glslang::EbvSampleId: |
| 487 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 488 | return spv::BuiltInSampleId; |
| 489 | |
| 490 | case glslang::EbvSamplePosition: |
| 491 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 492 | return spv::BuiltInSamplePosition; |
| 493 | |
| 494 | case glslang::EbvSampleMask: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 495 | return spv::BuiltInSampleMask; |
| 496 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 497 | case glslang::EbvLayer: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 498 | builder.addCapability(spv::CapabilityGeometry); |
| 499 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 500 | glslangIntermediate->getStage() == EShLangTessControl || |
| 501 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 502 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 503 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 504 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 505 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 506 | return spv::BuiltInLayer; |
| 507 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 508 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 509 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 510 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 511 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 512 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 513 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 514 | case glslang::EbvBaseVertex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 515 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 516 | builder.addCapability(spv::CapabilityDrawParameters); |
| 517 | return spv::BuiltInBaseVertex; |
| 518 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 519 | case glslang::EbvBaseInstance: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 520 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 521 | builder.addCapability(spv::CapabilityDrawParameters); |
| 522 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 523 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 524 | case glslang::EbvDrawId: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 525 | addPre13Extension(spv::E_SPV_KHR_shader_draw_parameters); |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 526 | builder.addCapability(spv::CapabilityDrawParameters); |
| 527 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 528 | |
| 529 | case glslang::EbvPrimitiveId: |
| 530 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 531 | builder.addCapability(spv::CapabilityGeometry); |
| 532 | return spv::BuiltInPrimitiveId; |
| 533 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 534 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 535 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 536 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 537 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 538 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 539 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 540 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 541 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 542 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 543 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 544 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 545 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 546 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 547 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 548 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 549 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 550 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 551 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 552 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 553 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 554 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 555 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 556 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 557 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 558 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 559 | return spv::BuiltInSubgroupSize; |
| 560 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 561 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 562 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 563 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 564 | return spv::BuiltInSubgroupLocalInvocationId; |
| 565 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 566 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 567 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 568 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 569 | return spv::BuiltInSubgroupEqMaskKHR; |
| 570 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 571 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 572 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 573 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 574 | return spv::BuiltInSubgroupGeMaskKHR; |
| 575 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 576 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 577 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 578 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 579 | return spv::BuiltInSubgroupGtMaskKHR; |
| 580 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 581 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 582 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 583 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 584 | return spv::BuiltInSubgroupLeMaskKHR; |
| 585 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 586 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 587 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 588 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 589 | return spv::BuiltInSubgroupLtMaskKHR; |
| 590 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 591 | case glslang::EbvNumSubgroups: |
| 592 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 593 | return spv::BuiltInNumSubgroups; |
| 594 | |
| 595 | case glslang::EbvSubgroupID: |
| 596 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 597 | return spv::BuiltInSubgroupId; |
| 598 | |
| 599 | case glslang::EbvSubgroupSize2: |
| 600 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 601 | return spv::BuiltInSubgroupSize; |
| 602 | |
| 603 | case glslang::EbvSubgroupInvocation2: |
| 604 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 605 | return spv::BuiltInSubgroupLocalInvocationId; |
| 606 | |
| 607 | case glslang::EbvSubgroupEqMask2: |
| 608 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 609 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 610 | return spv::BuiltInSubgroupEqMask; |
| 611 | |
| 612 | case glslang::EbvSubgroupGeMask2: |
| 613 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 614 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 615 | return spv::BuiltInSubgroupGeMask; |
| 616 | |
| 617 | case glslang::EbvSubgroupGtMask2: |
| 618 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 619 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 620 | return spv::BuiltInSubgroupGtMask; |
| 621 | |
| 622 | case glslang::EbvSubgroupLeMask2: |
| 623 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 624 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 625 | return spv::BuiltInSubgroupLeMask; |
| 626 | |
| 627 | case glslang::EbvSubgroupLtMask2: |
| 628 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 629 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 630 | return spv::BuiltInSubgroupLtMask; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 631 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 632 | case glslang::EbvBaryCoordNoPersp: |
| 633 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 634 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 635 | |
| 636 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 637 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 638 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 639 | |
| 640 | case glslang::EbvBaryCoordNoPerspSample: |
| 641 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 642 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 643 | |
| 644 | case glslang::EbvBaryCoordSmooth: |
| 645 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 646 | return spv::BuiltInBaryCoordSmoothAMD; |
| 647 | |
| 648 | case glslang::EbvBaryCoordSmoothCentroid: |
| 649 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 650 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 651 | |
| 652 | case glslang::EbvBaryCoordSmoothSample: |
| 653 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 654 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 655 | |
| 656 | case glslang::EbvBaryCoordPullModel: |
| 657 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 658 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 659 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 660 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 661 | case glslang::EbvDeviceIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 662 | addPre13Extension(spv::E_SPV_KHR_device_group); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 663 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 664 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 665 | |
| 666 | case glslang::EbvViewIndex: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 667 | addPre13Extension(spv::E_SPV_KHR_multiview); |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 668 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 669 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 670 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 671 | #ifdef NV_EXTENSIONS |
| 672 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 673 | if (!memberDeclaration) { |
| 674 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 675 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 676 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 677 | return spv::BuiltInViewportMaskNV; |
| 678 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 679 | if (!memberDeclaration) { |
| 680 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 681 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 682 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 683 | return spv::BuiltInSecondaryPositionNV; |
| 684 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 685 | if (!memberDeclaration) { |
| 686 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 687 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 688 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 689 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 690 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 691 | if (!memberDeclaration) { |
| 692 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 693 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 694 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 695 | return spv::BuiltInPositionPerViewNV; |
| 696 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 697 | if (!memberDeclaration) { |
| 698 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 699 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 700 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 701 | return spv::BuiltInViewportMaskPerViewNV; |
Piers Daniell | 1c5443c | 2017-12-13 13:07:22 -0700 | [diff] [blame] | 702 | case glslang::EbvFragFullyCoveredNV: |
| 703 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 704 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 705 | return spv::BuiltInFullyCoveredEXT; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 706 | #endif |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 707 | default: |
| 708 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 712 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 713 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 714 | { |
| 715 | assert(type.getBasicType() == glslang::EbtSampler); |
| 716 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 717 | // Check for capabilities |
| 718 | switch (type.getQualifier().layoutFormat) { |
| 719 | case glslang::ElfRg32f: |
| 720 | case glslang::ElfRg16f: |
| 721 | case glslang::ElfR11fG11fB10f: |
| 722 | case glslang::ElfR16f: |
| 723 | case glslang::ElfRgba16: |
| 724 | case glslang::ElfRgb10A2: |
| 725 | case glslang::ElfRg16: |
| 726 | case glslang::ElfRg8: |
| 727 | case glslang::ElfR16: |
| 728 | case glslang::ElfR8: |
| 729 | case glslang::ElfRgba16Snorm: |
| 730 | case glslang::ElfRg16Snorm: |
| 731 | case glslang::ElfRg8Snorm: |
| 732 | case glslang::ElfR16Snorm: |
| 733 | case glslang::ElfR8Snorm: |
| 734 | |
| 735 | case glslang::ElfRg32i: |
| 736 | case glslang::ElfRg16i: |
| 737 | case glslang::ElfRg8i: |
| 738 | case glslang::ElfR16i: |
| 739 | case glslang::ElfR8i: |
| 740 | |
| 741 | case glslang::ElfRgb10a2ui: |
| 742 | case glslang::ElfRg32ui: |
| 743 | case glslang::ElfRg16ui: |
| 744 | case glslang::ElfRg8ui: |
| 745 | case glslang::ElfR16ui: |
| 746 | case glslang::ElfR8ui: |
| 747 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 748 | break; |
| 749 | |
| 750 | default: |
| 751 | break; |
| 752 | } |
| 753 | |
| 754 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 755 | switch (type.getQualifier().layoutFormat) { |
| 756 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 757 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 758 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 759 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 760 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 761 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 762 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 763 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 764 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 765 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 766 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 767 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 768 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 769 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 770 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 771 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 772 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 773 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 774 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 775 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 776 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 777 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 778 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 779 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 780 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 781 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 782 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 783 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 784 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 785 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 786 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 787 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 788 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 789 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 790 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 791 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 792 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 793 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 794 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 795 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 796 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 800 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(const glslang::TIntermSelection& selectionNode) const |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 801 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 802 | if (selectionNode.getFlatten()) |
| 803 | return spv::SelectionControlFlattenMask; |
| 804 | if (selectionNode.getDontFlatten()) |
| 805 | return spv::SelectionControlDontFlattenMask; |
| 806 | return spv::SelectionControlMaskNone; |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 807 | } |
| 808 | |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 809 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) const |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 810 | { |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 811 | if (switchNode.getFlatten()) |
| 812 | return spv::SelectionControlFlattenMask; |
| 813 | if (switchNode.getDontFlatten()) |
| 814 | return spv::SelectionControlDontFlattenMask; |
| 815 | return spv::SelectionControlMaskNone; |
| 816 | } |
| 817 | |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 818 | // return a non-0 dependency if the dependency argument must be set |
| 819 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
| 820 | unsigned int& dependencyLength) const |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 821 | { |
| 822 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 823 | |
| 824 | if (loopNode.getDontUnroll()) |
| 825 | control = control | spv::LoopControlDontUnrollMask; |
| 826 | if (loopNode.getUnroll()) |
| 827 | control = control | spv::LoopControlUnrollMask; |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 828 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 829 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 830 | else if (loopNode.getLoopDependency() > 0) { |
| 831 | control = control | spv::LoopControlDependencyLengthMask; |
| 832 | dependencyLength = loopNode.getLoopDependency(); |
| 833 | } |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 834 | |
| 835 | return control; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 836 | } |
| 837 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 838 | // Translate glslang type to SPIR-V storage class. |
| 839 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 840 | { |
| 841 | if (type.getQualifier().isPipeInput()) |
| 842 | return spv::StorageClassInput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 843 | if (type.getQualifier().isPipeOutput()) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 844 | return spv::StorageClassOutput; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 845 | |
| 846 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
| 847 | type.getQualifier().storage == glslang::EvqUniform) { |
| 848 | if (type.getBasicType() == glslang::EbtAtomicUint) |
| 849 | return spv::StorageClassAtomicCounter; |
| 850 | if (type.containsOpaque()) |
| 851 | return spv::StorageClassUniformConstant; |
| 852 | } |
| 853 | |
| 854 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 855 | addPre13Extension(spv::E_SPV_KHR_storage_buffer_storage_class); |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 856 | return spv::StorageClassStorageBuffer; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 860 | if (type.getQualifier().layoutPushConstant) |
| 861 | return spv::StorageClassPushConstant; |
| 862 | if (type.getBasicType() == glslang::EbtBlock) |
| 863 | return spv::StorageClassUniform; |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 864 | return spv::StorageClassUniformConstant; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 865 | } |
John Kessenich | bed4e4f | 2017-09-08 02:38:07 -0600 | [diff] [blame] | 866 | |
| 867 | switch (type.getQualifier().storage) { |
| 868 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
| 869 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 870 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 871 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
| 872 | default: |
| 873 | assert(0); |
| 874 | break; |
| 875 | } |
| 876 | |
| 877 | return spv::StorageClassFunction; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 878 | } |
| 879 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 880 | // 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] | 881 | // descriptor set. |
| 882 | bool IsDescriptorResource(const glslang::TType& type) |
| 883 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 884 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 885 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 886 | return type.getQualifier().isUniformOrBuffer() && ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 887 | |
| 888 | // non block... |
| 889 | // basically samplerXXX/subpass/sampler/texture are all included |
| 890 | // if they are the global-scope-class, not the function parameter |
| 891 | // (or local, if they ever exist) class. |
| 892 | if (type.getBasicType() == glslang::EbtSampler) |
| 893 | return type.getQualifier().isUniformOrBuffer(); |
| 894 | |
| 895 | // None of the above. |
| 896 | return false; |
| 897 | } |
| 898 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 899 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 900 | { |
| 901 | if (child.layoutMatrix == glslang::ElmNone) |
| 902 | child.layoutMatrix = parent.layoutMatrix; |
| 903 | |
| 904 | if (parent.invariant) |
| 905 | child.invariant = true; |
| 906 | if (parent.nopersp) |
| 907 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 908 | #ifdef AMD_EXTENSIONS |
| 909 | if (parent.explicitInterp) |
| 910 | child.explicitInterp = true; |
| 911 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 912 | if (parent.flat) |
| 913 | child.flat = true; |
| 914 | if (parent.centroid) |
| 915 | child.centroid = true; |
| 916 | if (parent.patch) |
| 917 | child.patch = true; |
| 918 | if (parent.sample) |
| 919 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 920 | if (parent.coherent) |
| 921 | child.coherent = true; |
| 922 | if (parent.volatil) |
| 923 | child.volatil = true; |
| 924 | if (parent.restrict) |
| 925 | child.restrict = true; |
| 926 | if (parent.readonly) |
| 927 | child.readonly = true; |
| 928 | if (parent.writeonly) |
| 929 | child.writeonly = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 930 | } |
| 931 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 932 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 933 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 934 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 935 | // - struct members might inherit from a struct declaration |
| 936 | // (note that non-block structs don't explicitly inherit, |
| 937 | // only implicitly, meaning no decoration involved) |
| 938 | // - affect decorations on the struct members |
| 939 | // (note smooth does not, and expecting something like volatile |
| 940 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 941 | // - 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] | 942 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 943 | } |
| 944 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 945 | // |
| 946 | // Implement the TGlslangToSpvTraverser class. |
| 947 | // |
| 948 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 949 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate* glslangIntermediate, |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 950 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 951 | : TIntermTraverser(true, false, true), |
| 952 | options(options), |
| 953 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 954 | sequenceDepth(0), logger(buildLogger), |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 955 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 956 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 957 | glslangIntermediate(glslangIntermediate) |
| 958 | { |
| 959 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 960 | |
| 961 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 962 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 963 | glslangIntermediate->getVersion()); |
| 964 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 965 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 966 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 967 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 968 | |
| 969 | // Set the source shader's text. If for SPV version 1.0, include |
| 970 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 971 | // Otherwise, emit those as actual instructions. |
| 972 | std::string text; |
| 973 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 974 | for (int p = 0; p < (int)processes.size(); ++p) { |
| 975 | if (glslangIntermediate->getSpv().spv < 0x00010100) { |
| 976 | text.append("// OpModuleProcessed "); |
| 977 | text.append(processes[p]); |
| 978 | text.append("\n"); |
| 979 | } else |
| 980 | builder.addModuleProcessed(processes[p]); |
| 981 | } |
| 982 | if (glslangIntermediate->getSpv().spv < 0x00010100 && (int)processes.size() > 0) |
| 983 | text.append("#line 1\n"); |
| 984 | text.append(glslangIntermediate->getSourceText()); |
| 985 | builder.setSourceText(text); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 986 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 987 | stdBuiltins = builder.import("GLSL.std.450"); |
| 988 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 989 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 990 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 991 | |
| 992 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 993 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 994 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 995 | builder.addSourceExtension(it->c_str()); |
| 996 | |
| 997 | // Add the top-level modes for this shader. |
| 998 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 999 | if (glslangIntermediate->getXfbMode()) { |
| 1000 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1001 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1002 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1003 | |
| 1004 | unsigned int mode; |
| 1005 | switch (glslangIntermediate->getStage()) { |
| 1006 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1007 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1008 | break; |
| 1009 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1010 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1011 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1012 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1013 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 1014 | glslang::TLayoutGeometry primitive; |
| 1015 | |
| 1016 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1017 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1018 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1019 | } else { |
| 1020 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1021 | } |
| 1022 | |
| 1023 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1024 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1025 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1026 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1027 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1028 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1029 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1030 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1031 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1032 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1033 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1034 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1035 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1036 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1037 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1038 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1039 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1040 | |
| 1041 | switch (glslangIntermediate->getVertexOrder()) { |
| 1042 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1043 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1044 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1045 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1046 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1047 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1048 | |
| 1049 | if (glslangIntermediate->getPointMode()) |
| 1050 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1051 | break; |
| 1052 | |
| 1053 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1054 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1055 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1056 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1057 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1058 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1059 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1060 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1061 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1062 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1063 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1064 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1065 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1066 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1067 | |
| 1068 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1069 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1070 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1071 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1072 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1073 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1074 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1075 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1076 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1077 | break; |
| 1078 | |
| 1079 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1080 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1081 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1082 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1083 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1084 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1085 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1086 | else |
| 1087 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1088 | |
| 1089 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1090 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1091 | |
chaoc | c120452 | 2017-06-30 17:14:30 -0700 | [diff] [blame] | 1092 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1093 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1094 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1095 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1096 | } |
| 1097 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1098 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1099 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1100 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1101 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1102 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1103 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1104 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1105 | |
| 1106 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1107 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1108 | break; |
| 1109 | |
| 1110 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1111 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 1112 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1113 | glslangIntermediate->getLocalSize(1), |
| 1114 | glslangIntermediate->getLocalSize(2)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1115 | break; |
| 1116 | |
| 1117 | default: |
| 1118 | break; |
| 1119 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1120 | } |
| 1121 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1122 | // Finish creating SPV, after the traversal is complete. |
| 1123 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1124 | { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1125 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1126 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1127 | builder.leaveFunction(); |
| 1128 | } |
| 1129 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1130 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1131 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1132 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1133 | |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 1134 | builder.eliminateDeadDecorations(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1137 | // Write the SPV into 'out'. |
| 1138 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1139 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1140 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | // |
| 1144 | // Implement the traversal functions. |
| 1145 | // |
| 1146 | // Return true from interior nodes to have the external traversal |
| 1147 | // continue on to children. Return false if children were |
| 1148 | // already processed. |
| 1149 | // |
| 1150 | |
| 1151 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1152 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1153 | // - uniform/input reads |
| 1154 | // - output writes |
| 1155 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1156 | // - something simple that degenerates into the last bullet |
| 1157 | // |
| 1158 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1159 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1160 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1161 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1162 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1163 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1164 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1165 | // Formal function parameters were mapped during makeFunctions(). |
| 1166 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1167 | |
| 1168 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1169 | if (builder.isPointer(id)) { |
| 1170 | spv::StorageClass sc = builder.getStorageClass(id); |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1171 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) { |
| 1172 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) |
| 1173 | iOSet.insert(id); |
| 1174 | } |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1178 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1179 | // Prepare to generate code for the access |
| 1180 | |
| 1181 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1182 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1183 | // followed by setting the base. |
| 1184 | builder.clearAccessChain(); |
| 1185 | |
| 1186 | // 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] | 1187 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1188 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1189 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1190 | // 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] | 1191 | // These are also pure R-values. |
| 1192 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1193 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1194 | builder.setAccessChainRValue(id); |
| 1195 | else |
| 1196 | builder.setAccessChainLValue(id); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1201 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1202 | builder.setLine(node->getLoc().line); |
| 1203 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1204 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1205 | if (node->getType().getQualifier().isSpecConstant()) |
| 1206 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1207 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1208 | // First, handle special cases |
| 1209 | switch (node->getOp()) { |
| 1210 | case glslang::EOpAssign: |
| 1211 | case glslang::EOpAddAssign: |
| 1212 | case glslang::EOpSubAssign: |
| 1213 | case glslang::EOpMulAssign: |
| 1214 | case glslang::EOpVectorTimesMatrixAssign: |
| 1215 | case glslang::EOpVectorTimesScalarAssign: |
| 1216 | case glslang::EOpMatrixTimesScalarAssign: |
| 1217 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1218 | case glslang::EOpDivAssign: |
| 1219 | case glslang::EOpModAssign: |
| 1220 | case glslang::EOpAndAssign: |
| 1221 | case glslang::EOpInclusiveOrAssign: |
| 1222 | case glslang::EOpExclusiveOrAssign: |
| 1223 | case glslang::EOpLeftShiftAssign: |
| 1224 | case glslang::EOpRightShiftAssign: |
| 1225 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1226 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1227 | // says to evaluate the left before the right. So, always, left |
| 1228 | // node then right node. |
| 1229 | { |
| 1230 | // get the left l-value, save it away |
| 1231 | builder.clearAccessChain(); |
| 1232 | node->getLeft()->traverse(this); |
| 1233 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1234 | |
| 1235 | // evaluate the right |
| 1236 | builder.clearAccessChain(); |
| 1237 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1238 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1239 | |
| 1240 | if (node->getOp() != glslang::EOpAssign) { |
| 1241 | // the left is also an r-value |
| 1242 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1243 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1244 | |
| 1245 | // do the operation |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1246 | rValue = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1247 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1248 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1249 | node->getType().getBasicType()); |
| 1250 | |
| 1251 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1252 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | // store the result |
| 1256 | builder.setAccessChain(lValue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1257 | multiTypeStore(node->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1258 | |
| 1259 | // assignments are expressions having an rValue after they are evaluated... |
| 1260 | builder.clearAccessChain(); |
| 1261 | builder.setAccessChainRValue(rValue); |
| 1262 | } |
| 1263 | return false; |
| 1264 | case glslang::EOpIndexDirect: |
| 1265 | case glslang::EOpIndexDirectStruct: |
| 1266 | { |
| 1267 | // Get the left part of the access chain. |
| 1268 | node->getLeft()->traverse(this); |
| 1269 | |
| 1270 | // Add the next element in the chain |
| 1271 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1272 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1273 | if (! node->getLeft()->getType().isArray() && |
| 1274 | node->getLeft()->getType().isVector() && |
| 1275 | node->getOp() == glslang::EOpIndexDirect) { |
| 1276 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1277 | // so short circuit the access-chain stuff with a swizzle. |
| 1278 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1279 | swizzle.push_back(glslangIndex); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1280 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1281 | } else { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1282 | int spvIndex = glslangIndex; |
| 1283 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1284 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1285 | { |
| 1286 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1287 | // index remapping due to hidden members in anonymous blocks. |
| 1288 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1289 | assert(remapper.size() > 0); |
| 1290 | spvIndex = remapper[glslangIndex]; |
| 1291 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1292 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1293 | // normal case for indexing array or structure or block |
| 1294 | builder.accessChainPush(builder.makeIntConstant(spvIndex)); |
| 1295 | |
| 1296 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1297 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1298 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1299 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | return false; |
| 1303 | case glslang::EOpIndexIndirect: |
| 1304 | { |
| 1305 | // Structure or array or vector indirection. |
| 1306 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 1307 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1308 | // Will use the access chain's 'component' for variable index into a vector. |
| 1309 | |
| 1310 | // This adapter is building access chains left to right. |
| 1311 | // Set up the access chain to the left. |
| 1312 | node->getLeft()->traverse(this); |
| 1313 | |
| 1314 | // save it so that computing the right side doesn't trash it |
| 1315 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1316 | |
| 1317 | // compute the next index in the chain |
| 1318 | builder.clearAccessChain(); |
| 1319 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1320 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1321 | |
| 1322 | // restore the saved access chain |
| 1323 | builder.setAccessChain(partial); |
| 1324 | |
| 1325 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1326 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1327 | else |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1328 | builder.accessChainPush(index); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1329 | } |
| 1330 | return false; |
| 1331 | case glslang::EOpVectorSwizzle: |
| 1332 | { |
| 1333 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1334 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1335 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1336 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1337 | } |
| 1338 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1339 | case glslang::EOpMatrixSwizzle: |
| 1340 | logger->missingFunctionality("matrix swizzle"); |
| 1341 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1342 | case glslang::EOpLogicalOr: |
| 1343 | case glslang::EOpLogicalAnd: |
| 1344 | { |
| 1345 | |
| 1346 | // These may require short circuiting, but can sometimes be done as straight |
| 1347 | // binary operations. The right operand must be short circuited if it has |
| 1348 | // side effects, and should probably be if it is complex. |
| 1349 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1350 | break; // handle below as a normal binary operation |
| 1351 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1352 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1353 | builder.clearAccessChain(); |
| 1354 | builder.setAccessChainRValue(result); |
| 1355 | } |
| 1356 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1357 | default: |
| 1358 | break; |
| 1359 | } |
| 1360 | |
| 1361 | // Assume generic binary op... |
| 1362 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1363 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1364 | builder.clearAccessChain(); |
| 1365 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1366 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1367 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1368 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1369 | builder.clearAccessChain(); |
| 1370 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1371 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1372 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1373 | // get result |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1374 | spv::Id result = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1375 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1376 | convertGlslangToSpvType(node->getType()), left, right, |
| 1377 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1378 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1379 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1380 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1381 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1382 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1383 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1384 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1385 | return false; |
| 1386 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1390 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1391 | builder.setLine(node->getLoc().line); |
| 1392 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1393 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1394 | if (node->getType().getQualifier().isSpecConstant()) |
| 1395 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1396 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1397 | spv::Id result = spv::NoResult; |
| 1398 | |
| 1399 | // try texturing first |
| 1400 | result = createImageTextureFunctionCall(node); |
| 1401 | if (result != spv::NoResult) { |
| 1402 | builder.clearAccessChain(); |
| 1403 | builder.setAccessChainRValue(result); |
| 1404 | |
| 1405 | return false; // done with this node |
| 1406 | } |
| 1407 | |
| 1408 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1409 | |
| 1410 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1411 | // Quite special; won't want to evaluate the operand. |
| 1412 | |
| 1413 | // Normal .length() would have been constant folded by the front-end. |
| 1414 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1415 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1416 | assert(node->getOperand()->getType().isRuntimeSizedArray()); |
| 1417 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1418 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1419 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1420 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1421 | |
| 1422 | builder.clearAccessChain(); |
| 1423 | builder.setAccessChainRValue(length); |
| 1424 | |
| 1425 | return false; |
| 1426 | } |
| 1427 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1428 | // Start by evaluating the operand |
| 1429 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1430 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1431 | // operate first on the swizzle base, then apply the swizzle. |
| 1432 | spv::Id invertedType = spv::NoType; |
| 1433 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 1434 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 1435 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 1436 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1437 | builder.clearAccessChain(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1438 | if (invertedType != spv::NoType) |
| 1439 | node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this); |
| 1440 | else |
| 1441 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1442 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1443 | spv::Id operand = spv::NoResult; |
| 1444 | |
| 1445 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1446 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1447 | node->getOp() == glslang::EOpAtomicCounter || |
| 1448 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1449 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1450 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1451 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1452 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1453 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1454 | spv::Decoration noContraction = TranslateNoContractionDecoration(node->getType().getQualifier()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1455 | |
| 1456 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1457 | if (! result) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1458 | result = createConversion(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1459 | |
| 1460 | // if not, then possibly an operation |
| 1461 | if (! result) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1462 | result = createUnaryOperation(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1463 | |
| 1464 | if (result) { |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1465 | if (invertedType) |
| 1466 | result = createInvertedSwizzle(precision, *node->getOperand(), result); |
| 1467 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1468 | builder.clearAccessChain(); |
| 1469 | builder.setAccessChainRValue(result); |
| 1470 | |
| 1471 | return false; // done with this node |
| 1472 | } |
| 1473 | |
| 1474 | // it must be a special case, check... |
| 1475 | switch (node->getOp()) { |
| 1476 | case glslang::EOpPostIncrement: |
| 1477 | case glslang::EOpPostDecrement: |
| 1478 | case glslang::EOpPreIncrement: |
| 1479 | case glslang::EOpPreDecrement: |
| 1480 | { |
| 1481 | // 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] | 1482 | spv::Id one = 0; |
| 1483 | if (node->getBasicType() == glslang::EbtFloat) |
| 1484 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 1485 | else if (node->getBasicType() == glslang::EbtDouble) |
| 1486 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1487 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 1488 | one = builder.makeFloat16Constant(1.0F); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1489 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 1490 | one = builder.makeInt8Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1491 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 1492 | one = builder.makeInt16Constant(1); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1493 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1494 | one = builder.makeInt64Constant(1); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1495 | else |
| 1496 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1497 | glslang::TOperator op; |
| 1498 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1499 | node->getOp() == glslang::EOpPostIncrement) |
| 1500 | op = glslang::EOpAdd; |
| 1501 | else |
| 1502 | op = glslang::EOpSub; |
| 1503 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1504 | spv::Id result = createBinaryOperation(op, precision, |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1505 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1506 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1507 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1508 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1509 | |
| 1510 | // The result of operation is always stored, but conditionally the |
| 1511 | // consumed result. The consumed result is always an r-value. |
| 1512 | builder.accessChainStore(result); |
| 1513 | builder.clearAccessChain(); |
| 1514 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1515 | node->getOp() == glslang::EOpPreDecrement) |
| 1516 | builder.setAccessChainRValue(result); |
| 1517 | else |
| 1518 | builder.setAccessChainRValue(operand); |
| 1519 | } |
| 1520 | |
| 1521 | return false; |
| 1522 | |
| 1523 | case glslang::EOpEmitStreamVertex: |
| 1524 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1525 | return false; |
| 1526 | case glslang::EOpEndStreamPrimitive: |
| 1527 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1528 | return false; |
| 1529 | |
| 1530 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1531 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1532 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1533 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 1537 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 1538 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1539 | if (node->getType().getQualifier().isSpecConstant()) |
| 1540 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1541 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1542 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1543 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 1544 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1545 | |
| 1546 | // try texturing |
| 1547 | result = createImageTextureFunctionCall(node); |
| 1548 | if (result != spv::NoResult) { |
| 1549 | builder.clearAccessChain(); |
| 1550 | builder.setAccessChainRValue(result); |
| 1551 | |
| 1552 | return false; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1553 | #ifdef AMD_EXTENSIONS |
| 1554 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 1555 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 1556 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1557 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1558 | // "imageStore" is a special case, which has no result |
| 1559 | return false; |
| 1560 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1561 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1562 | glslang::TOperator binOp = glslang::EOpNull; |
| 1563 | bool reduceComparison = true; |
| 1564 | bool isMatrix = false; |
| 1565 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1566 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1567 | |
| 1568 | assert(node->getOp()); |
| 1569 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1570 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1571 | |
| 1572 | switch (node->getOp()) { |
| 1573 | case glslang::EOpSequence: |
| 1574 | { |
| 1575 | if (preVisit) |
| 1576 | ++sequenceDepth; |
| 1577 | else |
| 1578 | --sequenceDepth; |
| 1579 | |
| 1580 | if (sequenceDepth == 1) { |
| 1581 | // If this is the parent node of all the functions, we want to see them |
| 1582 | // early, so all call points have actual SPIR-V functions to reference. |
| 1583 | // In all cases, still let the traverser visit the children for us. |
| 1584 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 1585 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1586 | // 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] | 1587 | // anything else gets there, so visit out of order, doing them all now. |
| 1588 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 1589 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 1590 | // 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] | 1591 | // so do them manually. |
| 1592 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 1593 | |
| 1594 | return false; |
| 1595 | } |
| 1596 | |
| 1597 | return true; |
| 1598 | } |
| 1599 | case glslang::EOpLinkerObjects: |
| 1600 | { |
| 1601 | if (visit == glslang::EvPreVisit) |
| 1602 | linkageOnly = true; |
| 1603 | else |
| 1604 | linkageOnly = false; |
| 1605 | |
| 1606 | return true; |
| 1607 | } |
| 1608 | case glslang::EOpComma: |
| 1609 | { |
| 1610 | // processing from left to right naturally leaves the right-most |
| 1611 | // lying around in the access chain |
| 1612 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1613 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 1614 | glslangOperands[i]->traverse(this); |
| 1615 | |
| 1616 | return false; |
| 1617 | } |
| 1618 | case glslang::EOpFunction: |
| 1619 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1620 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1621 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1622 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1623 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1624 | } else { |
| 1625 | handleFunctionEntry(node); |
| 1626 | } |
| 1627 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1628 | if (inEntryPoint) |
| 1629 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 1630 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1631 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | return true; |
| 1635 | case glslang::EOpParameters: |
| 1636 | // Parameters will have been consumed by EOpFunction processing, but not |
| 1637 | // the body, so we still visited the function node's children, making this |
| 1638 | // child redundant. |
| 1639 | return false; |
| 1640 | case glslang::EOpFunctionCall: |
| 1641 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1642 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1643 | if (node->isUserDefined()) |
| 1644 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 1645 | // assert(result); // this can happen for bad shaders because the call graph completeness checking is not yet done |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1646 | if (result) { |
| 1647 | builder.clearAccessChain(); |
| 1648 | builder.setAccessChainRValue(result); |
| 1649 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1650 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1651 | |
| 1652 | return false; |
| 1653 | } |
| 1654 | case glslang::EOpConstructMat2x2: |
| 1655 | case glslang::EOpConstructMat2x3: |
| 1656 | case glslang::EOpConstructMat2x4: |
| 1657 | case glslang::EOpConstructMat3x2: |
| 1658 | case glslang::EOpConstructMat3x3: |
| 1659 | case glslang::EOpConstructMat3x4: |
| 1660 | case glslang::EOpConstructMat4x2: |
| 1661 | case glslang::EOpConstructMat4x3: |
| 1662 | case glslang::EOpConstructMat4x4: |
| 1663 | case glslang::EOpConstructDMat2x2: |
| 1664 | case glslang::EOpConstructDMat2x3: |
| 1665 | case glslang::EOpConstructDMat2x4: |
| 1666 | case glslang::EOpConstructDMat3x2: |
| 1667 | case glslang::EOpConstructDMat3x3: |
| 1668 | case glslang::EOpConstructDMat3x4: |
| 1669 | case glslang::EOpConstructDMat4x2: |
| 1670 | case glslang::EOpConstructDMat4x3: |
| 1671 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 1672 | case glslang::EOpConstructIMat2x2: |
| 1673 | case glslang::EOpConstructIMat2x3: |
| 1674 | case glslang::EOpConstructIMat2x4: |
| 1675 | case glslang::EOpConstructIMat3x2: |
| 1676 | case glslang::EOpConstructIMat3x3: |
| 1677 | case glslang::EOpConstructIMat3x4: |
| 1678 | case glslang::EOpConstructIMat4x2: |
| 1679 | case glslang::EOpConstructIMat4x3: |
| 1680 | case glslang::EOpConstructIMat4x4: |
| 1681 | case glslang::EOpConstructUMat2x2: |
| 1682 | case glslang::EOpConstructUMat2x3: |
| 1683 | case glslang::EOpConstructUMat2x4: |
| 1684 | case glslang::EOpConstructUMat3x2: |
| 1685 | case glslang::EOpConstructUMat3x3: |
| 1686 | case glslang::EOpConstructUMat3x4: |
| 1687 | case glslang::EOpConstructUMat4x2: |
| 1688 | case glslang::EOpConstructUMat4x3: |
| 1689 | case glslang::EOpConstructUMat4x4: |
| 1690 | case glslang::EOpConstructBMat2x2: |
| 1691 | case glslang::EOpConstructBMat2x3: |
| 1692 | case glslang::EOpConstructBMat2x4: |
| 1693 | case glslang::EOpConstructBMat3x2: |
| 1694 | case glslang::EOpConstructBMat3x3: |
| 1695 | case glslang::EOpConstructBMat3x4: |
| 1696 | case glslang::EOpConstructBMat4x2: |
| 1697 | case glslang::EOpConstructBMat4x3: |
| 1698 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1699 | case glslang::EOpConstructF16Mat2x2: |
| 1700 | case glslang::EOpConstructF16Mat2x3: |
| 1701 | case glslang::EOpConstructF16Mat2x4: |
| 1702 | case glslang::EOpConstructF16Mat3x2: |
| 1703 | case glslang::EOpConstructF16Mat3x3: |
| 1704 | case glslang::EOpConstructF16Mat3x4: |
| 1705 | case glslang::EOpConstructF16Mat4x2: |
| 1706 | case glslang::EOpConstructF16Mat4x3: |
| 1707 | case glslang::EOpConstructF16Mat4x4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1708 | isMatrix = true; |
| 1709 | // fall through |
| 1710 | case glslang::EOpConstructFloat: |
| 1711 | case glslang::EOpConstructVec2: |
| 1712 | case glslang::EOpConstructVec3: |
| 1713 | case glslang::EOpConstructVec4: |
| 1714 | case glslang::EOpConstructDouble: |
| 1715 | case glslang::EOpConstructDVec2: |
| 1716 | case glslang::EOpConstructDVec3: |
| 1717 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1718 | case glslang::EOpConstructFloat16: |
| 1719 | case glslang::EOpConstructF16Vec2: |
| 1720 | case glslang::EOpConstructF16Vec3: |
| 1721 | case glslang::EOpConstructF16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1722 | case glslang::EOpConstructBool: |
| 1723 | case glslang::EOpConstructBVec2: |
| 1724 | case glslang::EOpConstructBVec3: |
| 1725 | case glslang::EOpConstructBVec4: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1726 | case glslang::EOpConstructInt8: |
| 1727 | case glslang::EOpConstructI8Vec2: |
| 1728 | case glslang::EOpConstructI8Vec3: |
| 1729 | case glslang::EOpConstructI8Vec4: |
| 1730 | case glslang::EOpConstructUint8: |
| 1731 | case glslang::EOpConstructU8Vec2: |
| 1732 | case glslang::EOpConstructU8Vec3: |
| 1733 | case glslang::EOpConstructU8Vec4: |
| 1734 | case glslang::EOpConstructInt16: |
| 1735 | case glslang::EOpConstructI16Vec2: |
| 1736 | case glslang::EOpConstructI16Vec3: |
| 1737 | case glslang::EOpConstructI16Vec4: |
| 1738 | case glslang::EOpConstructUint16: |
| 1739 | case glslang::EOpConstructU16Vec2: |
| 1740 | case glslang::EOpConstructU16Vec3: |
| 1741 | case glslang::EOpConstructU16Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1742 | case glslang::EOpConstructInt: |
| 1743 | case glslang::EOpConstructIVec2: |
| 1744 | case glslang::EOpConstructIVec3: |
| 1745 | case glslang::EOpConstructIVec4: |
| 1746 | case glslang::EOpConstructUint: |
| 1747 | case glslang::EOpConstructUVec2: |
| 1748 | case glslang::EOpConstructUVec3: |
| 1749 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1750 | case glslang::EOpConstructInt64: |
| 1751 | case glslang::EOpConstructI64Vec2: |
| 1752 | case glslang::EOpConstructI64Vec3: |
| 1753 | case glslang::EOpConstructI64Vec4: |
| 1754 | case glslang::EOpConstructUint64: |
| 1755 | case glslang::EOpConstructU64Vec2: |
| 1756 | case glslang::EOpConstructU64Vec3: |
| 1757 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1758 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1759 | case glslang::EOpConstructTextureSampler: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1760 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1761 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1762 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1763 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1764 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1765 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1766 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1767 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1768 | std::vector<spv::Id> constituents; |
| 1769 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 1770 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1771 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1772 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1773 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1774 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1775 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1776 | |
| 1777 | builder.clearAccessChain(); |
| 1778 | builder.setAccessChainRValue(constructed); |
| 1779 | |
| 1780 | return false; |
| 1781 | } |
| 1782 | |
| 1783 | // These six are component-wise compares with component-wise results. |
| 1784 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 1785 | case glslang::EOpLessThan: |
| 1786 | case glslang::EOpGreaterThan: |
| 1787 | case glslang::EOpLessThanEqual: |
| 1788 | case glslang::EOpGreaterThanEqual: |
| 1789 | case glslang::EOpVectorEqual: |
| 1790 | case glslang::EOpVectorNotEqual: |
| 1791 | { |
| 1792 | // Map the operation to a binary |
| 1793 | binOp = node->getOp(); |
| 1794 | reduceComparison = false; |
| 1795 | switch (node->getOp()) { |
| 1796 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 1797 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 1798 | default: binOp = node->getOp(); break; |
| 1799 | } |
| 1800 | |
| 1801 | break; |
| 1802 | } |
| 1803 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1804 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1805 | binOp = glslang::EOpMul; |
| 1806 | break; |
| 1807 | case glslang::EOpOuterProduct: |
| 1808 | // two vectors multiplied to make a matrix |
| 1809 | binOp = glslang::EOpOuterProduct; |
| 1810 | break; |
| 1811 | case glslang::EOpDot: |
| 1812 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1813 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1814 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 1815 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1816 | binOp = glslang::EOpMul; |
| 1817 | break; |
| 1818 | } |
| 1819 | case glslang::EOpMod: |
| 1820 | // when an aggregate, this is the floating-point mod built-in function, |
| 1821 | // which can be emitted by the one in createBinaryOperation() |
| 1822 | binOp = glslang::EOpMod; |
| 1823 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1824 | case glslang::EOpEmitVertex: |
| 1825 | case glslang::EOpEndPrimitive: |
| 1826 | case glslang::EOpBarrier: |
| 1827 | case glslang::EOpMemoryBarrier: |
| 1828 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 1829 | case glslang::EOpMemoryBarrierBuffer: |
| 1830 | case glslang::EOpMemoryBarrierImage: |
| 1831 | case glslang::EOpMemoryBarrierShared: |
| 1832 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 1833 | case glslang::EOpDeviceMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 1834 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 1835 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 1836 | case glslang::EOpWorkgroupMemoryBarrier: |
| 1837 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 1838 | case glslang::EOpSubgroupBarrier: |
| 1839 | case glslang::EOpSubgroupMemoryBarrier: |
| 1840 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 1841 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 1842 | case glslang::EOpSubgroupMemoryBarrierShared: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1843 | noReturnValue = true; |
| 1844 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 1845 | break; |
| 1846 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1847 | case glslang::EOpAtomicAdd: |
| 1848 | case glslang::EOpAtomicMin: |
| 1849 | case glslang::EOpAtomicMax: |
| 1850 | case glslang::EOpAtomicAnd: |
| 1851 | case glslang::EOpAtomicOr: |
| 1852 | case glslang::EOpAtomicXor: |
| 1853 | case glslang::EOpAtomicExchange: |
| 1854 | case glslang::EOpAtomicCompSwap: |
| 1855 | atomic = true; |
| 1856 | break; |
| 1857 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 1858 | case glslang::EOpAtomicCounterAdd: |
| 1859 | case glslang::EOpAtomicCounterSubtract: |
| 1860 | case glslang::EOpAtomicCounterMin: |
| 1861 | case glslang::EOpAtomicCounterMax: |
| 1862 | case glslang::EOpAtomicCounterAnd: |
| 1863 | case glslang::EOpAtomicCounterOr: |
| 1864 | case glslang::EOpAtomicCounterXor: |
| 1865 | case glslang::EOpAtomicCounterExchange: |
| 1866 | case glslang::EOpAtomicCounterCompSwap: |
| 1867 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 1868 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 1869 | atomic = true; |
| 1870 | break; |
| 1871 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1872 | default: |
| 1873 | break; |
| 1874 | } |
| 1875 | |
| 1876 | // |
| 1877 | // See if it maps to a regular operation. |
| 1878 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1879 | if (binOp != glslang::EOpNull) { |
| 1880 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 1881 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 1882 | assert(left && right); |
| 1883 | |
| 1884 | builder.clearAccessChain(); |
| 1885 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1886 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1887 | |
| 1888 | builder.clearAccessChain(); |
| 1889 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1890 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1891 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1892 | builder.setLine(node->getLoc().line); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1893 | result = createBinaryOperation(binOp, precision, TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1894 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1895 | left->getType().getBasicType(), reduceComparison); |
| 1896 | |
| 1897 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1898 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1899 | builder.clearAccessChain(); |
| 1900 | builder.setAccessChainRValue(result); |
| 1901 | |
| 1902 | return false; |
| 1903 | } |
| 1904 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1905 | // |
| 1906 | // Create the list of operands. |
| 1907 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1908 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1909 | std::vector<spv::Id> operands; |
| 1910 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1911 | // special case l-value operands; there are just a few |
| 1912 | bool lvalue = false; |
| 1913 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1914 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1915 | case glslang::EOpModf: |
| 1916 | if (arg == 1) |
| 1917 | lvalue = true; |
| 1918 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1919 | case glslang::EOpInterpolateAtSample: |
| 1920 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1921 | #ifdef AMD_EXTENSIONS |
| 1922 | case glslang::EOpInterpolateAtVertex: |
| 1923 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1924 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1925 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1926 | |
| 1927 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1928 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 1929 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1930 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 1931 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 1932 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1933 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 1934 | case glslang::EOpAtomicAdd: |
| 1935 | case glslang::EOpAtomicMin: |
| 1936 | case glslang::EOpAtomicMax: |
| 1937 | case glslang::EOpAtomicAnd: |
| 1938 | case glslang::EOpAtomicOr: |
| 1939 | case glslang::EOpAtomicXor: |
| 1940 | case glslang::EOpAtomicExchange: |
| 1941 | case glslang::EOpAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 1942 | case glslang::EOpAtomicCounterAdd: |
| 1943 | case glslang::EOpAtomicCounterSubtract: |
| 1944 | case glslang::EOpAtomicCounterMin: |
| 1945 | case glslang::EOpAtomicCounterMax: |
| 1946 | case glslang::EOpAtomicCounterAnd: |
| 1947 | case glslang::EOpAtomicCounterOr: |
| 1948 | case glslang::EOpAtomicCounterXor: |
| 1949 | case glslang::EOpAtomicCounterExchange: |
| 1950 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 1951 | if (arg == 0) |
| 1952 | lvalue = true; |
| 1953 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1954 | case glslang::EOpAddCarry: |
| 1955 | case glslang::EOpSubBorrow: |
| 1956 | if (arg == 2) |
| 1957 | lvalue = true; |
| 1958 | break; |
| 1959 | case glslang::EOpUMulExtended: |
| 1960 | case glslang::EOpIMulExtended: |
| 1961 | if (arg >= 2) |
| 1962 | lvalue = true; |
| 1963 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1964 | default: |
| 1965 | break; |
| 1966 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1967 | builder.clearAccessChain(); |
| 1968 | if (invertedType != spv::NoType && arg == 0) |
| 1969 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 1970 | else |
| 1971 | glslangOperands[arg]->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1972 | if (lvalue) |
| 1973 | operands.push_back(builder.accessChainGetLValue()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1974 | else { |
| 1975 | builder.setLine(node->getLoc().line); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1976 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1977 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1978 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1979 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1980 | builder.setLine(node->getLoc().line); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1981 | if (atomic) { |
| 1982 | // Handle all atomics |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1983 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1984 | } else { |
| 1985 | // Pass through to generic operations. |
| 1986 | switch (glslangOperands.size()) { |
| 1987 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1988 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1989 | break; |
| 1990 | case 1: |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1991 | result = createUnaryOperation( |
| 1992 | node->getOp(), precision, |
| 1993 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1994 | resultType(), operands.front(), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1995 | glslangOperands[0]->getAsTyped()->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1996 | break; |
| 1997 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1998 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1999 | break; |
| 2000 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2001 | if (invertedType) |
| 2002 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | if (noReturnValue) |
| 2006 | return false; |
| 2007 | |
| 2008 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 2009 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 2010 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2011 | } else { |
| 2012 | builder.clearAccessChain(); |
| 2013 | builder.setAccessChainRValue(result); |
| 2014 | return false; |
| 2015 | } |
| 2016 | } |
| 2017 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2018 | // This path handles both if-then-else and ?: |
| 2019 | // The if-then-else has a node type of void, while |
| 2020 | // ?: has either a void or a non-void node type |
| 2021 | // |
| 2022 | // Leaving the result, when not void: |
| 2023 | // GLSL only has r-values as the result of a :?, but |
| 2024 | // if we have an l-value, that can be more efficient if it will |
| 2025 | // become the base of a complex r-value expression, because the |
| 2026 | // 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] | 2027 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 2028 | { |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2029 | // See if it simple and safe, or required, to execute both sides. |
| 2030 | // Crucially, side effects must be either semantically required or avoided, |
| 2031 | // and there are performance trade-offs. |
| 2032 | // Return true if required or a good idea (and safe) to execute both sides, |
| 2033 | // false otherwise. |
| 2034 | const auto bothSidesPolicy = [&]() -> bool { |
| 2035 | // do we have both sides? |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2036 | if (node->getTrueBlock() == nullptr || |
| 2037 | node->getFalseBlock() == nullptr) |
| 2038 | return false; |
| 2039 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2040 | // required? (unless we write additional code to look for side effects |
| 2041 | // and make performance trade-offs if none are present) |
| 2042 | if (!node->getShortCircuit()) |
| 2043 | return true; |
| 2044 | |
| 2045 | // if not required to execute both, decide based on performance/practicality... |
| 2046 | |
| 2047 | // see if OpSelect can handle it |
| 2048 | if ((!node->getType().isScalar() && !node->getType().isVector()) || |
| 2049 | node->getBasicType() == glslang::EbtVoid) |
| 2050 | return false; |
| 2051 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2052 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 2053 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 2054 | |
| 2055 | // return true if a single operand to ? : is okay for OpSelect |
| 2056 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2057 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2058 | }; |
| 2059 | |
| 2060 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 2061 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 2062 | }; |
| 2063 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2064 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 2065 | // emit the condition before doing anything with selection |
| 2066 | node->getCondition()->traverse(this); |
| 2067 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 2068 | |
| 2069 | // Find a way of executing both sides and selecting the right result. |
| 2070 | const auto executeBothSides = [&]() -> void { |
| 2071 | // execute both sides |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2072 | node->getTrueBlock()->traverse(this); |
| 2073 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2074 | node->getFalseBlock()->traverse(this); |
| 2075 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 2076 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2077 | builder.setLine(node->getLoc().line); |
| 2078 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2079 | // done if void |
| 2080 | if (node->getBasicType() == glslang::EbtVoid) |
| 2081 | return; |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 2082 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2083 | // emit code to select between trueValue and falseValue |
| 2084 | |
| 2085 | // see if OpSelect can handle it |
| 2086 | if (node->getType().isScalar() || node->getType().isVector()) { |
| 2087 | // Emit OpSelect for this selection. |
| 2088 | |
| 2089 | // smear condition to vector, if necessary (AST is always scalar) |
| 2090 | if (builder.isVector(trueValue)) |
| 2091 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 2092 | builder.makeVectorType(builder.makeBoolType(), |
| 2093 | builder.getNumComponents(trueValue))); |
| 2094 | |
| 2095 | // OpSelect |
| 2096 | result = builder.createTriOp(spv::OpSelect, |
| 2097 | convertGlslangToSpvType(node->getType()), condition, |
| 2098 | trueValue, falseValue); |
| 2099 | |
| 2100 | builder.clearAccessChain(); |
| 2101 | builder.setAccessChainRValue(result); |
| 2102 | } else { |
| 2103 | // We need control flow to select the result. |
| 2104 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 2105 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2106 | |
| 2107 | // Selection control: |
| 2108 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2109 | |
| 2110 | // make an "if" based on the value created by the condition |
| 2111 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2112 | |
| 2113 | // emit the "then" statement |
| 2114 | builder.createStore(trueValue, result); |
| 2115 | ifBuilder.makeBeginElse(); |
| 2116 | // emit the "else" statement |
| 2117 | builder.createStore(falseValue, result); |
| 2118 | |
| 2119 | // finish off the control flow |
| 2120 | ifBuilder.makeEndIf(); |
| 2121 | |
| 2122 | builder.clearAccessChain(); |
| 2123 | builder.setAccessChainLValue(result); |
| 2124 | } |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2125 | }; |
| 2126 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2127 | // Execute the one side needed, as per the condition |
| 2128 | const auto executeOneSide = [&]() { |
| 2129 | // Always emit control flow. |
| 2130 | if (node->getBasicType() != glslang::EbtVoid) |
| 2131 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2132 | |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2133 | // Selection control: |
| 2134 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 2135 | |
| 2136 | // make an "if" based on the value created by the condition |
| 2137 | spv::Builder::If ifBuilder(condition, control, builder); |
| 2138 | |
| 2139 | // emit the "then" statement |
| 2140 | if (node->getTrueBlock() != nullptr) { |
| 2141 | node->getTrueBlock()->traverse(this); |
| 2142 | if (result != spv::NoResult) |
| 2143 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 2144 | } |
| 2145 | |
| 2146 | if (node->getFalseBlock() != nullptr) { |
| 2147 | ifBuilder.makeBeginElse(); |
| 2148 | // emit the "else" statement |
| 2149 | node->getFalseBlock()->traverse(this); |
| 2150 | if (result != spv::NoResult) |
| 2151 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 2152 | } |
| 2153 | |
| 2154 | // finish off the control flow |
| 2155 | ifBuilder.makeEndIf(); |
| 2156 | |
| 2157 | if (result != spv::NoResult) { |
| 2158 | builder.clearAccessChain(); |
| 2159 | builder.setAccessChainLValue(result); |
| 2160 | } |
| 2161 | }; |
| 2162 | |
| 2163 | // Try for OpSelect (or a requirement to execute both sides) |
| 2164 | if (bothSidesPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 2165 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2166 | if (node->getType().getQualifier().isSpecConstant()) |
| 2167 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
John Kessenich | 4bee531 | 2018-02-20 21:29:05 -0700 | [diff] [blame] | 2168 | executeBothSides(); |
| 2169 | } else |
| 2170 | executeOneSide(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2171 | |
| 2172 | return false; |
| 2173 | } |
| 2174 | |
| 2175 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2176 | { |
| 2177 | // emit and get the condition before doing anything with switch |
| 2178 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2179 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2180 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2181 | // Selection control: |
John Kessenich | e18fd20 | 2018-01-30 11:01:39 -0700 | [diff] [blame] | 2182 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2183 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2184 | // browse the children to sort out code segments |
| 2185 | int defaultSegment = -1; |
| 2186 | std::vector<TIntermNode*> codeSegments; |
| 2187 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 2188 | std::vector<int> caseValues; |
| 2189 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 2190 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 2191 | TIntermNode* child = *c; |
| 2192 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2193 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2194 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2195 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2196 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2197 | } else |
| 2198 | codeSegments.push_back(child); |
| 2199 | } |
| 2200 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2201 | // 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] | 2202 | // statements between the last case and the end of the switch statement |
| 2203 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 2204 | (int)codeSegments.size() == defaultSegment) |
| 2205 | codeSegments.push_back(nullptr); |
| 2206 | |
| 2207 | // make the switch statement |
| 2208 | std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2209 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2210 | |
| 2211 | // emit all the code in the segments |
| 2212 | breakForLoop.push(false); |
| 2213 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 2214 | builder.nextSwitchSegment(segmentBlocks, s); |
| 2215 | if (codeSegments[s]) |
| 2216 | codeSegments[s]->traverse(this); |
| 2217 | else |
| 2218 | builder.addSwitchBreak(); |
| 2219 | } |
| 2220 | breakForLoop.pop(); |
| 2221 | |
| 2222 | builder.endSwitch(segmentBlocks); |
| 2223 | |
| 2224 | return false; |
| 2225 | } |
| 2226 | |
| 2227 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 2228 | { |
| 2229 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2230 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2231 | |
| 2232 | builder.clearAccessChain(); |
| 2233 | builder.setAccessChainRValue(constant); |
| 2234 | } |
| 2235 | |
| 2236 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 2237 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2238 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2239 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2240 | |
| 2241 | // Loop control: |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2242 | unsigned int dependencyLength = glslang::TIntermLoop::dependencyInfinite; |
| 2243 | const spv::LoopControlMask control = TranslateLoopControl(*node, dependencyLength); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2244 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2245 | // Spec requires back edges to target header blocks, and every header block |
| 2246 | // must dominate its merge block. Make a header block first to ensure these |
| 2247 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 2248 | // by a block-ending branch. But we don't want to put any other body/test |
| 2249 | // instructions in it, since the body/test may have arbitrary instructions, |
| 2250 | // including merges of its own. |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2251 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2252 | builder.setBuildPoint(&blocks.head); |
John Kessenich | a2858d9 | 2018-01-31 08:11:18 -0700 | [diff] [blame] | 2253 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, dependencyLength); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2254 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2255 | spv::Block& test = builder.makeNewBlock(); |
| 2256 | builder.createBranch(&test); |
| 2257 | |
| 2258 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2259 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2260 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2261 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 2262 | |
| 2263 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2264 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2265 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2266 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2267 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2268 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2269 | |
| 2270 | builder.setBuildPoint(&blocks.continue_target); |
| 2271 | if (node->getTerminal()) |
| 2272 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2273 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 2274 | } else { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2275 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2276 | builder.createBranch(&blocks.body); |
| 2277 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2278 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2279 | builder.setBuildPoint(&blocks.body); |
| 2280 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2281 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2282 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2283 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2284 | |
| 2285 | builder.setBuildPoint(&blocks.continue_target); |
| 2286 | if (node->getTerminal()) |
| 2287 | node->getTerminal()->traverse(this); |
| 2288 | if (node->getTest()) { |
| 2289 | node->getTest()->traverse(this); |
| 2290 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2291 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2292 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2293 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 2294 | // TODO: unless there was a break/return/discard instruction |
| 2295 | // somewhere in the body, this is an infinite loop, so we should |
| 2296 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2297 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2298 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2299 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2300 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2301 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2302 | return false; |
| 2303 | } |
| 2304 | |
| 2305 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 2306 | { |
| 2307 | if (node->getExpression()) |
| 2308 | node->getExpression()->traverse(this); |
| 2309 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2310 | builder.setLine(node->getLoc().line); |
| 2311 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2312 | switch (node->getFlowOp()) { |
| 2313 | case glslang::EOpKill: |
| 2314 | builder.makeDiscard(); |
| 2315 | break; |
| 2316 | case glslang::EOpBreak: |
| 2317 | if (breakForLoop.top()) |
| 2318 | builder.createLoopExit(); |
| 2319 | else |
| 2320 | builder.addSwitchBreak(); |
| 2321 | break; |
| 2322 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2323 | builder.createLoopContinue(); |
| 2324 | break; |
| 2325 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2326 | if (node->getExpression()) { |
| 2327 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 2328 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 2329 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 2330 | builder.clearAccessChain(); |
| 2331 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 2332 | builder.setAccessChainLValue(copyId); |
| 2333 | multiTypeStore(glslangReturnType, returnId); |
| 2334 | returnId = builder.createLoad(copyId); |
| 2335 | } |
| 2336 | builder.makeReturn(false, returnId); |
| 2337 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2338 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2339 | |
| 2340 | builder.clearAccessChain(); |
| 2341 | break; |
| 2342 | |
| 2343 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2344 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2345 | break; |
| 2346 | } |
| 2347 | |
| 2348 | return false; |
| 2349 | } |
| 2350 | |
| 2351 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 2352 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2353 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2354 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2355 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 2356 | if (node->getQualifier().isConstant()) { |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2357 | return createSpvConstant(*node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2361 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2362 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 2363 | |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2364 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 2365 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 2366 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2367 | if (contains16BitType) { |
| 2368 | if (storageClass == spv::StorageClassInput || storageClass == spv::StorageClassOutput) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2369 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2370 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
| 2371 | } else if (storageClass == spv::StorageClassPushConstant) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2372 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2373 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 2374 | } else if (storageClass == spv::StorageClassUniform) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2375 | addPre13Extension(spv::E_SPV_KHR_16bit_storage); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2376 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 2377 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2378 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 2379 | } |
| 2380 | } |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2381 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2382 | const char* name = node->getName().c_str(); |
| 2383 | if (glslang::IsAnonymous(name)) |
| 2384 | name = ""; |
| 2385 | |
| 2386 | return builder.createVariable(storageClass, spvType, name); |
| 2387 | } |
| 2388 | |
| 2389 | // Return type Id of the sampled type. |
| 2390 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 2391 | { |
| 2392 | switch (sampler.type) { |
| 2393 | case glslang::EbtFloat: return builder.makeFloatType(32); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 2394 | #ifdef AMD_EXTENSIONS |
| 2395 | case glslang::EbtFloat16: |
| 2396 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 2397 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 2398 | return builder.makeFloatType(16); |
| 2399 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2400 | case glslang::EbtInt: return builder.makeIntType(32); |
| 2401 | case glslang::EbtUint: return builder.makeUintType(32); |
| 2402 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2403 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2404 | return builder.makeFloatType(32); |
| 2405 | } |
| 2406 | } |
| 2407 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2408 | // If node is a swizzle operation, return the type that should be used if |
| 2409 | // the swizzle base is first consumed by another operation, before the swizzle |
| 2410 | // is applied. |
| 2411 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 2412 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2413 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2414 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2415 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 2416 | else |
| 2417 | return spv::NoType; |
| 2418 | } |
| 2419 | |
| 2420 | // When inverting a swizzle with a parent op, this function |
| 2421 | // will apply the swizzle operation to a completed parent operation. |
| 2422 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 2423 | { |
| 2424 | std::vector<unsigned> swizzle; |
| 2425 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 2426 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 2427 | } |
| 2428 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2429 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 2430 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 2431 | { |
| 2432 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 2433 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 2434 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2435 | } |
| 2436 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2437 | // Convert from a glslang type to an SPV type, by calling into a |
| 2438 | // recursive version of this function. This establishes the inherited |
| 2439 | // layout state rooted from the top-level type. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2440 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type) |
| 2441 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2442 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier()); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | // 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] | 2446 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2447 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2448 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier) |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2449 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2450 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2451 | |
| 2452 | switch (type.getBasicType()) { |
| 2453 | case glslang::EbtVoid: |
| 2454 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2455 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2456 | break; |
| 2457 | case glslang::EbtFloat: |
| 2458 | spvType = builder.makeFloatType(32); |
| 2459 | break; |
| 2460 | case glslang::EbtDouble: |
| 2461 | spvType = builder.makeFloatType(64); |
| 2462 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2463 | case glslang::EbtFloat16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2464 | builder.addCapability(spv::CapabilityFloat16); |
| 2465 | #if AMD_EXTENSIONS |
| 2466 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 2467 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 2468 | #endif |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2469 | spvType = builder.makeFloatType(16); |
| 2470 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2471 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2472 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 2473 | // a 32-bit int where non-0 means true. |
| 2474 | if (explicitLayout != glslang::ElpNone) |
| 2475 | spvType = builder.makeUintType(32); |
| 2476 | else |
| 2477 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2478 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 2479 | case glslang::EbtInt8: |
| 2480 | builder.addCapability(spv::CapabilityInt8); |
| 2481 | spvType = builder.makeIntType(8); |
| 2482 | break; |
| 2483 | case glslang::EbtUint8: |
| 2484 | builder.addCapability(spv::CapabilityInt8); |
| 2485 | spvType = builder.makeUintType(8); |
| 2486 | break; |
| 2487 | case glslang::EbtInt16: |
| 2488 | builder.addCapability(spv::CapabilityInt16); |
| 2489 | #ifdef AMD_EXTENSIONS |
| 2490 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 2491 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 2492 | #endif |
| 2493 | spvType = builder.makeIntType(16); |
| 2494 | break; |
| 2495 | case glslang::EbtUint16: |
| 2496 | builder.addCapability(spv::CapabilityInt16); |
| 2497 | #ifdef AMD_EXTENSIONS |
| 2498 | if (builder.getSpvVersion() < glslang::EShTargetSpv_1_3) |
| 2499 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 2500 | #endif |
| 2501 | spvType = builder.makeUintType(16); |
| 2502 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2503 | case glslang::EbtInt: |
| 2504 | spvType = builder.makeIntType(32); |
| 2505 | break; |
| 2506 | case glslang::EbtUint: |
| 2507 | spvType = builder.makeUintType(32); |
| 2508 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2509 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2510 | spvType = builder.makeIntType(64); |
| 2511 | break; |
| 2512 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2513 | spvType = builder.makeUintType(64); |
| 2514 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2515 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 2516 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2517 | spvType = builder.makeUintType(32); |
| 2518 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2519 | case glslang::EbtSampler: |
| 2520 | { |
| 2521 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2522 | if (sampler.sampler) { |
| 2523 | // pure sampler |
| 2524 | spvType = builder.makeSamplerType(); |
| 2525 | } else { |
| 2526 | // an image is present, make its type |
| 2527 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 2528 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 2529 | if (sampler.combined) { |
| 2530 | // already has both image and sampler, make the combined type |
| 2531 | spvType = builder.makeSampledImageType(spvType); |
| 2532 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2533 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2534 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2535 | break; |
| 2536 | case glslang::EbtStruct: |
| 2537 | case glslang::EbtBlock: |
| 2538 | { |
| 2539 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2540 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2541 | |
| 2542 | // Try to share structs for different layouts, but not yet for other |
| 2543 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2544 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2545 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2546 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2547 | break; |
| 2548 | |
| 2549 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2550 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2551 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 2552 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2553 | } |
| 2554 | break; |
| 2555 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2556 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2557 | break; |
| 2558 | } |
| 2559 | |
| 2560 | if (type.isMatrix()) |
| 2561 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 2562 | else { |
| 2563 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 2564 | if (type.getVectorSize() > 1) |
| 2565 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 2566 | } |
| 2567 | |
| 2568 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2569 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 2570 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2571 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2572 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2573 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 2574 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2575 | // Use a dummy glslang type for querying internal strides of |
| 2576 | // arrays of arrays, but using just a one-dimensional array. |
| 2577 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
| 2578 | while (simpleArrayType.getArraySizes().getNumDims() > 1) |
| 2579 | simpleArrayType.getArraySizes().dereference(); |
| 2580 | |
| 2581 | // Will compute the higher-order strides here, rather than making a whole |
| 2582 | // pile of types and doing repetitive recursion on their contents. |
| 2583 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 2584 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2585 | |
| 2586 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2587 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2588 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2589 | if (stride > 0) |
| 2590 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2591 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2592 | } |
| 2593 | } else { |
| 2594 | // single-dimensional array, and don't yet have stride |
| 2595 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2596 | // 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] | 2597 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 2598 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2599 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2600 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2601 | // Do the outer dimension, which might not be known for a runtime-sized array |
| 2602 | if (type.isRuntimeSizedArray()) { |
| 2603 | spvType = builder.makeRuntimeArray(spvType); |
| 2604 | } else { |
| 2605 | assert(type.getOuterArraySize() > 0); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2606 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2607 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2608 | if (stride > 0) |
| 2609 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | return spvType; |
| 2613 | } |
| 2614 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2615 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 2616 | // |
| 2617 | // Identify interface members that don't have their required extension turned on. |
| 2618 | // |
| 2619 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 2620 | { |
| 2621 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 2622 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2623 | if (member.getFieldName() == "gl_ViewportMask" && |
| 2624 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 2625 | return true; |
| 2626 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 2627 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2628 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2629 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 2630 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2631 | return true; |
| 2632 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 2633 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2634 | return true; |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2635 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 2636 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2637 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2638 | |
| 2639 | return false; |
| 2640 | }; |
| 2641 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2642 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 2643 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 2644 | // Mutually recursive with convertGlslangToSpvType(). |
| 2645 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 2646 | const glslang::TTypeList* glslangMembers, |
| 2647 | glslang::TLayoutPacking explicitLayout, |
| 2648 | const glslang::TQualifier& qualifier) |
| 2649 | { |
| 2650 | // Create a vector of struct types for SPIR-V to consume |
| 2651 | std::vector<spv::Id> spvMembers; |
| 2652 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2653 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2654 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2655 | if (glslangMember.hiddenMember()) { |
| 2656 | ++memberDelta; |
| 2657 | if (type.getBasicType() == glslang::EbtBlock) |
| 2658 | memberRemapper[glslangMembers][i] = -1; |
| 2659 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2660 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2661 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2662 | if (filterMember(glslangMember)) |
| 2663 | continue; |
| 2664 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2665 | // modify just this child's view of the qualifier |
| 2666 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2667 | InheritQualifiers(memberQualifier, qualifier); |
| 2668 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2669 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2670 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2671 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2672 | |
| 2673 | // recurse |
| 2674 | spvMembers.push_back(convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier)); |
| 2675 | } |
| 2676 | } |
| 2677 | |
| 2678 | // Make the SPIR-V type |
| 2679 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2680 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2681 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 2682 | |
| 2683 | // Decorate it |
| 2684 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 2685 | |
| 2686 | return spvType; |
| 2687 | } |
| 2688 | |
| 2689 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 2690 | const glslang::TTypeList* glslangMembers, |
| 2691 | glslang::TLayoutPacking explicitLayout, |
| 2692 | const glslang::TQualifier& qualifier, |
| 2693 | spv::Id spvType) |
| 2694 | { |
| 2695 | // Name and decorate the non-hidden members |
| 2696 | int offset = -1; |
| 2697 | int locationOffset = 0; // for use within the members of this struct |
| 2698 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2699 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2700 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2701 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2702 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2703 | if (filterMember(glslangMember)) |
| 2704 | continue; |
| 2705 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2706 | |
| 2707 | // modify just this child's view of the qualifier |
| 2708 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2709 | InheritQualifiers(memberQualifier, qualifier); |
| 2710 | |
| 2711 | // using -1 above to indicate a hidden member |
| 2712 | if (member >= 0) { |
| 2713 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 2714 | addMemberDecoration(spvType, member, TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 2715 | addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 2716 | // Add interpolation and auxiliary storage decorations only to top-level members of Input and Output storage classes |
John Kessenich | 65ee230 | 2017-02-06 18:44:52 -0700 | [diff] [blame] | 2717 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 2718 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 2719 | if (type.getBasicType() == glslang::EbtBlock || |
| 2720 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2721 | addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 2722 | addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
| 2723 | } |
| 2724 | } |
| 2725 | addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
| 2726 | |
Rex Xu | 286ca43 | 2017-07-27 14:33:16 +0800 | [diff] [blame] | 2727 | if (type.getBasicType() == glslang::EbtBlock && |
| 2728 | qualifier.storage == glslang::EvqBuffer) { |
| 2729 | // Add memory decorations only to top-level members of shader storage block |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2730 | std::vector<spv::Decoration> memory; |
| 2731 | TranslateMemoryDecoration(memberQualifier, memory); |
| 2732 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 2733 | addMemberDecoration(spvType, member, memory[i]); |
| 2734 | } |
| 2735 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2736 | // Location assignment was already completed correctly by the front end, |
| 2737 | // just track whether a member needs to be decorated. |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2738 | // Ignore member locations if the container is an array, as that's |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2739 | // ill-specified and decisions have been made to not allow this. |
| 2740 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 2741 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2742 | |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2743 | if (qualifier.hasLocation()) // track for upcoming inheritance |
John Kessenich | c521579 | 2018-02-27 13:23:08 -0700 | [diff] [blame] | 2744 | locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 2745 | glslangMember, glslangIntermediate->getStage()); |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2746 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2747 | // component, XFB, others |
| 2748 | if (glslangMember.getQualifier().hasComponent()) |
| 2749 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, glslangMember.getQualifier().layoutComponent); |
| 2750 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 2751 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, glslangMember.getQualifier().layoutXfbOffset); |
| 2752 | else if (explicitLayout != glslang::ElpNone) { |
| 2753 | // figure out what to do with offset, which is accumulating |
| 2754 | int nextOffset; |
| 2755 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 2756 | if (offset >= 0) |
| 2757 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 2758 | offset = nextOffset; |
| 2759 | } |
| 2760 | |
| 2761 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 2762 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
| 2763 | |
| 2764 | // built-in variable decorations |
| 2765 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 2766 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2767 | addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2768 | |
| 2769 | #ifdef NV_EXTENSIONS |
| 2770 | if (builtIn == spv::BuiltInLayer) { |
| 2771 | // SPV_NV_viewport_array2 extension |
| 2772 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 2773 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 2774 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 2775 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 2776 | } |
| 2777 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 2778 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 2779 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 2780 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 2781 | } |
| 2782 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 2783 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 2784 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 2785 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 2786 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 2787 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2788 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | // Decorate the structure |
| 2793 | addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 2794 | addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2795 | if (type.getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
| 2796 | builder.addCapability(spv::CapabilityGeometryStreams); |
| 2797 | builder.addDecoration(spvType, spv::DecorationStream, type.getQualifier().layoutStream); |
| 2798 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2799 | } |
| 2800 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2801 | // Turn the expression forming the array size into an id. |
| 2802 | // This is not quite trivial, because of specialization constants. |
| 2803 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 2804 | // a specialization constant expression is. |
| 2805 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 2806 | { |
| 2807 | // First, see if this is sized with a node, meaning a specialization constant: |
| 2808 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 2809 | if (specNode != nullptr) { |
| 2810 | builder.clearAccessChain(); |
| 2811 | specNode->traverse(this); |
| 2812 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 2813 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2814 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2815 | // Otherwise, need a compile-time (front end) size, get it: |
| 2816 | int size = arraySizes.getDimSize(dim); |
| 2817 | assert(size > 0); |
| 2818 | return builder.makeUintConstant(size); |
| 2819 | } |
| 2820 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2821 | // Wrap the builder's accessChainLoad to: |
| 2822 | // - localize handling of RelaxedPrecision |
| 2823 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 2824 | // (avoids unnecessary work and possible type punning for structures) |
| 2825 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2826 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 2827 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2828 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2829 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), nominalTypeId); |
| 2830 | |
| 2831 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2832 | if (type.getBasicType() == glslang::EbtBool) { |
| 2833 | if (builder.isScalarType(nominalTypeId)) { |
| 2834 | // Conversion for bool |
| 2835 | spv::Id boolType = builder.makeBoolType(); |
| 2836 | if (nominalTypeId != boolType) |
| 2837 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 2838 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2839 | // Conversion for bvec |
| 2840 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2841 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 2842 | if (nominalTypeId != bvecType) |
| 2843 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 2844 | } |
| 2845 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2846 | |
| 2847 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2848 | } |
| 2849 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2850 | // Wrap the builder's accessChainStore to: |
| 2851 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2852 | // |
| 2853 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2854 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 2855 | { |
| 2856 | // Need to convert to abstract types when necessary |
| 2857 | if (type.getBasicType() == glslang::EbtBool) { |
| 2858 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2859 | |
| 2860 | if (builder.isScalarType(nominalTypeId)) { |
| 2861 | // Conversion for bool |
| 2862 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2863 | if (nominalTypeId != boolType) { |
| 2864 | // keep these outside arguments, for determinant order-of-evaluation |
| 2865 | spv::Id one = builder.makeUintConstant(1); |
| 2866 | spv::Id zero = builder.makeUintConstant(0); |
| 2867 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 2868 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2869 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2870 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2871 | // Conversion for bvec |
| 2872 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2873 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2874 | if (nominalTypeId != bvecType) { |
| 2875 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 2876 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 2877 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 2878 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2879 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2880 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 2881 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | builder.accessChainStore(rvalue); |
| 2886 | } |
| 2887 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2888 | // For storing when types match at the glslang level, but not might match at the |
| 2889 | // SPIR-V level. |
| 2890 | // |
| 2891 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2892 | // 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] | 2893 | // as in a member-decorated way. |
| 2894 | // |
| 2895 | // NOTE: This function can handle any store request; if it's not special it |
| 2896 | // simplifies to a simple OpStore. |
| 2897 | // |
| 2898 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 2899 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 2900 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2901 | // we only do the complex path here if it's an aggregate |
| 2902 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2903 | accessChainStore(type, rValue); |
| 2904 | return; |
| 2905 | } |
| 2906 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2907 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2908 | spv::Id rType = builder.getTypeId(rValue); |
| 2909 | spv::Id lValue = builder.accessChainGetLValue(); |
| 2910 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 2911 | if (lType == rType) { |
| 2912 | accessChainStore(type, rValue); |
| 2913 | return; |
| 2914 | } |
| 2915 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2916 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2917 | // where the two types were the same type in GLSL. This requires member |
| 2918 | // by member copy, recursively. |
| 2919 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2920 | // If an array, copy element by element. |
| 2921 | if (type.isArray()) { |
| 2922 | glslang::TType glslangElementType(type, 0); |
| 2923 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2924 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 2925 | // get the source member |
| 2926 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2927 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2928 | // set up the target storage |
| 2929 | builder.clearAccessChain(); |
| 2930 | builder.setAccessChainLValue(lValue); |
| 2931 | builder.accessChainPush(builder.makeIntConstant(index)); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2932 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2933 | // store the member |
| 2934 | multiTypeStore(glslangElementType, elementRValue); |
| 2935 | } |
| 2936 | } else { |
| 2937 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2938 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2939 | // loop over structure members |
| 2940 | const glslang::TTypeList& members = *type.getStruct(); |
| 2941 | for (int m = 0; m < (int)members.size(); ++m) { |
| 2942 | const glslang::TType& glslangMemberType = *members[m].type; |
| 2943 | |
| 2944 | // get the source member |
| 2945 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 2946 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 2947 | |
| 2948 | // set up the target storage |
| 2949 | builder.clearAccessChain(); |
| 2950 | builder.setAccessChainLValue(lValue); |
| 2951 | builder.accessChainPush(builder.makeIntConstant(m)); |
| 2952 | |
| 2953 | // store the member |
| 2954 | multiTypeStore(glslangMemberType, memberRValue); |
| 2955 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2959 | // Decide whether or not this type should be |
| 2960 | // decorated with offsets and strides, and if so |
| 2961 | // whether std140 or std430 rules should be applied. |
| 2962 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2963 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2964 | // has to be a block |
| 2965 | if (type.getBasicType() != glslang::EbtBlock) |
| 2966 | return glslang::ElpNone; |
| 2967 | |
| 2968 | // has to be a uniform or buffer block |
| 2969 | if (type.getQualifier().storage != glslang::EvqUniform && |
| 2970 | type.getQualifier().storage != glslang::EvqBuffer) |
| 2971 | return glslang::ElpNone; |
| 2972 | |
| 2973 | // return the layout to use |
| 2974 | switch (type.getQualifier().layoutPacking) { |
| 2975 | case glslang::ElpStd140: |
| 2976 | case glslang::ElpStd430: |
| 2977 | return type.getQualifier().layoutPacking; |
| 2978 | default: |
| 2979 | return glslang::ElpNone; |
| 2980 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2981 | } |
| 2982 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2983 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2984 | int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2985 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2986 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2987 | int stride; |
| 2988 | glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 2989 | |
| 2990 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2991 | } |
| 2992 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2993 | // 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] | 2994 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2995 | int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2996 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2997 | glslang::TType elementType; |
| 2998 | elementType.shallowCopy(matrixType); |
| 2999 | elementType.clearArraySizes(); |
| 3000 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3001 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3002 | int stride; |
| 3003 | glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
| 3004 | |
| 3005 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3008 | // Given a member type of a struct, realign the current offset for it, and compute |
| 3009 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 3010 | // on the next call. |
| 3011 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 3012 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 3013 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3014 | void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, int& nextOffset, |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 3015 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3016 | { |
| 3017 | // this will get a positive value when deemed necessary |
| 3018 | nextOffset = -1; |
| 3019 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3020 | // override anything in currentOffset with user-set offset |
| 3021 | if (memberType.getQualifier().hasOffset()) |
| 3022 | currentOffset = memberType.getQualifier().layoutOffset; |
| 3023 | |
| 3024 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 3025 | // in which case the following code does not matter. But, that's not quite right |
| 3026 | // once cross-compilation unit GLSL validation is done, as the original user |
| 3027 | // settings are needed in layoutOffset, and then the following will come into play. |
| 3028 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3029 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3030 | if (! memberType.getQualifier().hasOffset()) |
| 3031 | currentOffset = -1; |
| 3032 | |
| 3033 | return; |
| 3034 | } |
| 3035 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 3036 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3037 | if (currentOffset < 0) |
| 3038 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3039 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3040 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 3041 | // but possibly not yet correctly aligned. |
| 3042 | |
| 3043 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 3044 | int dummyStride; |
| 3045 | int memberAlignment = glslangIntermediate->getBaseAlignment(memberType, memberSize, dummyStride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3046 | |
| 3047 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3048 | // TODO: make this consistent in early phases of code: |
| 3049 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 3050 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 3051 | // which is the most likely to rely on reflection, and least likely to rely implicit layouts |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3052 | if (glslangIntermediate->usingHlslOFfsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 3053 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3054 | int dummySize; |
| 3055 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 3056 | if (componentAlignment <= 4) |
| 3057 | memberAlignment = componentAlignment; |
| 3058 | } |
| 3059 | |
| 3060 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3061 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 3062 | |
| 3063 | // Bump up to vec4 if there is a bad straddle |
| 3064 | if (glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
| 3065 | glslang::RoundToPow2(currentOffset, 16); |
| 3066 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3067 | nextOffset = currentOffset + memberSize; |
| 3068 | } |
| 3069 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3070 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3071 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3072 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 3073 | switch (glslangBuiltIn) |
| 3074 | { |
| 3075 | case glslang::EbvClipDistance: |
| 3076 | case glslang::EbvCullDistance: |
| 3077 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3078 | #ifdef NV_EXTENSIONS |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3079 | case glslang::EbvViewportMaskNV: |
| 3080 | case glslang::EbvSecondaryPositionNV: |
| 3081 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 3082 | case glslang::EbvPositionPerViewNV: |
| 3083 | case glslang::EbvViewportMaskPerViewNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 3084 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 3085 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 3086 | // Alternately, we could just call this for any glslang built-in, since the |
| 3087 | // capability already guards against duplicates. |
| 3088 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 3089 | break; |
| 3090 | default: |
| 3091 | // Capabilities were already generated when the struct was declared. |
| 3092 | break; |
| 3093 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3094 | } |
| 3095 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3096 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3097 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 3098 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3099 | } |
| 3100 | |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3101 | // Does parameter need a place to keep writes, separate from the original? |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3102 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 3103 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3104 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) |
| 3105 | { |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3106 | assert(qualifier == glslang::EvqIn || |
| 3107 | qualifier == glslang::EvqOut || |
| 3108 | qualifier == glslang::EvqInOut || |
| 3109 | qualifier == glslang::EvqConstReadOnly); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3110 | return qualifier != glslang::EvqConstReadOnly; |
| 3111 | } |
| 3112 | |
| 3113 | // Is parameter pass-by-original? |
| 3114 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 3115 | bool implicitThisParam) |
| 3116 | { |
| 3117 | if (implicitThisParam) // implicit this |
| 3118 | return true; |
| 3119 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3120 | return paramType.getBasicType() == glslang::EbtBlock; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3121 | return paramType.containsOpaque() || // sampler, etc. |
| 3122 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 3123 | } |
| 3124 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3125 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 3126 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3127 | { |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3128 | const auto getParamDecorations = [](std::vector<spv::Decoration>& decorations, const glslang::TType& type) { |
| 3129 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 3130 | if (paramPrecision != spv::NoPrecision) |
| 3131 | decorations.push_back(paramPrecision); |
John Kessenich | 961cd35 | 2017-07-18 02:58:06 -0600 | [diff] [blame] | 3132 | TranslateMemoryDecoration(type.getQualifier(), decorations); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3133 | }; |
| 3134 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3135 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3136 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 3137 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3138 | continue; |
| 3139 | |
| 3140 | // 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] | 3141 | // 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] | 3142 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3143 | // 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] | 3144 | // function. What it is an address of varies: |
| 3145 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3146 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 3147 | // 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] | 3148 | // |
| 3149 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 3150 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3151 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 3152 | // 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] | 3153 | |
| 3154 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3155 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3156 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 3157 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3158 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 3159 | glslangIntermediate->implicitThisName; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3160 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3161 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3162 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3163 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 3164 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3165 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3166 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3167 | else if (writableParam(paramType.getQualifier().storage)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3168 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 3169 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3170 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3171 | getParamDecorations(paramDecorations[p], paramType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3172 | paramTypes.push_back(typeId); |
| 3173 | } |
| 3174 | |
| 3175 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3176 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 3177 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3178 | glslFunction->getName().c_str(), paramTypes, |
| 3179 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3180 | if (implicitThis) |
| 3181 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3182 | |
| 3183 | // Track function to emit/call later |
| 3184 | functionMap[glslFunction->getName().c_str()] = function; |
| 3185 | |
| 3186 | // Set the parameter id's |
| 3187 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3188 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 3189 | // give a name too |
| 3190 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | // Process all the initializers, while skipping the functions and link objects |
| 3196 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 3197 | { |
| 3198 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 3199 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 3200 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 3201 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 3202 | |
| 3203 | // 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] | 3204 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3205 | initializer->traverse(this); |
| 3206 | } |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | // Process all the functions, while skipping initializers. |
| 3211 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3212 | { |
| 3213 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3214 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 3215 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3216 | node->traverse(this); |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 3221 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3222 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3223 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3224 | currentFunction = functionMap[node->getName().c_str()]; |
| 3225 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3226 | builder.setBuildPoint(functionBlock); |
| 3227 | } |
| 3228 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3229 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3230 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3231 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3232 | |
| 3233 | glslang::TSampler sampler = {}; |
| 3234 | bool cubeCompare = false; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3235 | #ifdef AMD_EXTENSIONS |
| 3236 | bool f16ShadowCompare = false; |
| 3237 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3238 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3239 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 3240 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3241 | #ifdef AMD_EXTENSIONS |
| 3242 | f16ShadowCompare = sampler.shadow && glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 3243 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3244 | } |
| 3245 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3246 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 3247 | builder.clearAccessChain(); |
| 3248 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3249 | |
| 3250 | // Special case l-value operands |
| 3251 | bool lvalue = false; |
| 3252 | switch (node.getOp()) { |
| 3253 | case glslang::EOpImageAtomicAdd: |
| 3254 | case glslang::EOpImageAtomicMin: |
| 3255 | case glslang::EOpImageAtomicMax: |
| 3256 | case glslang::EOpImageAtomicAnd: |
| 3257 | case glslang::EOpImageAtomicOr: |
| 3258 | case glslang::EOpImageAtomicXor: |
| 3259 | case glslang::EOpImageAtomicExchange: |
| 3260 | case glslang::EOpImageAtomicCompSwap: |
| 3261 | if (i == 0) |
| 3262 | lvalue = true; |
| 3263 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3264 | case glslang::EOpSparseImageLoad: |
| 3265 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 3266 | lvalue = true; |
| 3267 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3268 | #ifdef AMD_EXTENSIONS |
| 3269 | case glslang::EOpSparseTexture: |
| 3270 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 3271 | lvalue = true; |
| 3272 | break; |
| 3273 | case glslang::EOpSparseTextureClamp: |
| 3274 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 3275 | lvalue = true; |
| 3276 | break; |
| 3277 | case glslang::EOpSparseTextureLod: |
| 3278 | case glslang::EOpSparseTextureOffset: |
| 3279 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 3280 | lvalue = true; |
| 3281 | break; |
| 3282 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3283 | case glslang::EOpSparseTexture: |
| 3284 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 3285 | lvalue = true; |
| 3286 | break; |
| 3287 | case glslang::EOpSparseTextureClamp: |
| 3288 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 3289 | lvalue = true; |
| 3290 | break; |
| 3291 | case glslang::EOpSparseTextureLod: |
| 3292 | case glslang::EOpSparseTextureOffset: |
| 3293 | if (i == 3) |
| 3294 | lvalue = true; |
| 3295 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3296 | #endif |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3297 | case glslang::EOpSparseTextureFetch: |
| 3298 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 3299 | lvalue = true; |
| 3300 | break; |
| 3301 | case glslang::EOpSparseTextureFetchOffset: |
| 3302 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 3303 | lvalue = true; |
| 3304 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3305 | #ifdef AMD_EXTENSIONS |
| 3306 | case glslang::EOpSparseTextureLodOffset: |
| 3307 | case glslang::EOpSparseTextureGrad: |
| 3308 | case glslang::EOpSparseTextureOffsetClamp: |
| 3309 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 3310 | lvalue = true; |
| 3311 | break; |
| 3312 | case glslang::EOpSparseTextureGradOffset: |
| 3313 | case glslang::EOpSparseTextureGradClamp: |
| 3314 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 3315 | lvalue = true; |
| 3316 | break; |
| 3317 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3318 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 3319 | lvalue = true; |
| 3320 | break; |
| 3321 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3322 | case glslang::EOpSparseTextureLodOffset: |
| 3323 | case glslang::EOpSparseTextureGrad: |
| 3324 | case glslang::EOpSparseTextureOffsetClamp: |
| 3325 | if (i == 4) |
| 3326 | lvalue = true; |
| 3327 | break; |
| 3328 | case glslang::EOpSparseTextureGradOffset: |
| 3329 | case glslang::EOpSparseTextureGradClamp: |
| 3330 | if (i == 5) |
| 3331 | lvalue = true; |
| 3332 | break; |
| 3333 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3334 | if (i == 6) |
| 3335 | lvalue = true; |
| 3336 | break; |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3337 | #endif |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3338 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3339 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 3340 | lvalue = true; |
| 3341 | break; |
| 3342 | case glslang::EOpSparseTextureGatherOffset: |
| 3343 | case glslang::EOpSparseTextureGatherOffsets: |
| 3344 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 3345 | lvalue = true; |
| 3346 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3347 | #ifdef AMD_EXTENSIONS |
| 3348 | case glslang::EOpSparseTextureGatherLod: |
| 3349 | if (i == 3) |
| 3350 | lvalue = true; |
| 3351 | break; |
| 3352 | case glslang::EOpSparseTextureGatherLodOffset: |
| 3353 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 3354 | if (i == 4) |
| 3355 | lvalue = true; |
| 3356 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3357 | case glslang::EOpSparseImageLoadLod: |
| 3358 | if (i == 3) |
| 3359 | lvalue = true; |
| 3360 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3361 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3362 | default: |
| 3363 | break; |
| 3364 | } |
| 3365 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3366 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3367 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3368 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3369 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3370 | } |
| 3371 | } |
| 3372 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3373 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3374 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3375 | builder.clearAccessChain(); |
| 3376 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3377 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3378 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3379 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3380 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 3381 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3382 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3383 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3384 | |
| 3385 | builder.setLine(node->getLoc().line); |
| 3386 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3387 | // Process a GLSL texturing op (will be SPV image) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3388 | const glslang::TSampler sampler = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType().getSampler() |
| 3389 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType().getSampler(); |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3390 | #ifdef AMD_EXTENSIONS |
| 3391 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
| 3392 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 3393 | : false; |
| 3394 | #endif |
| 3395 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3396 | std::vector<spv::Id> arguments; |
| 3397 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3398 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3399 | else |
| 3400 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 3401 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3402 | |
| 3403 | spv::Builder::TextureParameters params = { }; |
| 3404 | params.sampler = arguments[0]; |
| 3405 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3406 | glslang::TCrackedTextureOp cracked; |
| 3407 | node->crackTexture(sampler, cracked); |
| 3408 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 3409 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3410 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3411 | // Check for queries |
| 3412 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3413 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 3414 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 3415 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3416 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3417 | switch (node->getOp()) { |
| 3418 | case glslang::EOpImageQuerySize: |
| 3419 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3420 | if (arguments.size() > 1) { |
| 3421 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3422 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3423 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3424 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3425 | case glslang::EOpImageQuerySamples: |
| 3426 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3427 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3428 | case glslang::EOpTextureQueryLod: |
| 3429 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3430 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3431 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3432 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3433 | case glslang::EOpSparseTexelsResident: |
| 3434 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3435 | default: |
| 3436 | assert(0); |
| 3437 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3438 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3439 | } |
| 3440 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 3441 | int components = node->getType().getVectorSize(); |
| 3442 | |
| 3443 | if (node->getOp() == glslang::EOpTextureFetch) { |
| 3444 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 3445 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 3446 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 3447 | // here around e.g. which ones return scalars or other types. |
| 3448 | components = 4; |
| 3449 | } |
| 3450 | |
| 3451 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 3452 | |
| 3453 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 3454 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3455 | // Check for image functions other than queries |
| 3456 | if (node->isImage()) { |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3457 | std::vector<spv::Id> operands; |
| 3458 | auto opIt = arguments.begin(); |
| 3459 | operands.push_back(*(opIt++)); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3460 | |
| 3461 | // Handle subpass operations |
| 3462 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 3463 | // built-in function. |
| 3464 | if (cracked.subpass) { |
| 3465 | // add on the (0,0) coordinate |
| 3466 | spv::Id zero = builder.makeIntConstant(0); |
| 3467 | std::vector<spv::Id> comps; |
| 3468 | comps.push_back(zero); |
| 3469 | comps.push_back(zero); |
| 3470 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 3471 | if (sampler.ms) { |
| 3472 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3473 | operands.push_back(*(opIt++)); |
| 3474 | } |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 3475 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 3476 | builder.setPrecision(result, precision); |
| 3477 | return result; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3478 | } |
| 3479 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3480 | operands.push_back(*(opIt++)); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3481 | #ifdef AMD_EXTENSIONS |
| 3482 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 3483 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3484 | if (node->getOp() == glslang::EOpImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3485 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3486 | if (sampler.ms) { |
| 3487 | operands.push_back(spv::ImageOperandsSampleMask); |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3488 | operands.push_back(*opIt); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3489 | #ifdef AMD_EXTENSIONS |
| 3490 | } else if (cracked.lod) { |
| 3491 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3492 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3493 | |
| 3494 | operands.push_back(spv::ImageOperandsLodMask); |
| 3495 | operands.push_back(*opIt); |
| 3496 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3497 | } |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3498 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3499 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | fe4e572 | 2017-10-19 02:07:30 -0600 | [diff] [blame] | 3500 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 3501 | std::vector<spv::Id> result = { builder.createOp(spv::OpImageRead, resultType(), operands) }; |
| 3502 | builder.setPrecision(result[0], precision); |
| 3503 | |
| 3504 | // If needed, add a conversion constructor to the proper size. |
| 3505 | if (components != node->getType().getVectorSize()) |
| 3506 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 3507 | |
| 3508 | return result[0]; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3509 | #ifdef AMD_EXTENSIONS |
| 3510 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 3511 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3512 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3513 | #endif |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3514 | if (sampler.ms) { |
| 3515 | operands.push_back(*(opIt + 1)); |
| 3516 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3517 | operands.push_back(*opIt); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3518 | #ifdef AMD_EXTENSIONS |
| 3519 | } else if (cracked.lod) { |
| 3520 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3521 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3522 | |
| 3523 | operands.push_back(*(opIt + 1)); |
| 3524 | operands.push_back(spv::ImageOperandsLodMask); |
| 3525 | operands.push_back(*opIt); |
| 3526 | #endif |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3527 | } else |
| 3528 | operands.push_back(*opIt); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3529 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3530 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3531 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3532 | return spv::NoResult; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3533 | #ifdef AMD_EXTENSIONS |
| 3534 | } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 3535 | #else |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3536 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3537 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3538 | builder.addCapability(spv::CapabilitySparseResidency); |
| 3539 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3540 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 3541 | |
| 3542 | if (sampler.ms) { |
| 3543 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3544 | operands.push_back(*opIt++); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3545 | #ifdef AMD_EXTENSIONS |
| 3546 | } else if (cracked.lod) { |
| 3547 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3548 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3549 | |
| 3550 | operands.push_back(spv::ImageOperandsLodMask); |
| 3551 | operands.push_back(*opIt++); |
| 3552 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | // Create the return type that was a special structure |
| 3556 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3557 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3558 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 3559 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 3560 | |
| 3561 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 3562 | |
| 3563 | // Decode the return type |
| 3564 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 3565 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3566 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3567 | // Process image atomic operations |
| 3568 | |
| 3569 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 3570 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3571 | operands.push_back(sampler.ms ? *(opIt++) : builder.makeUintConstant(0)); // For non-MS, the value should be 0 |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3572 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3573 | spv::Id resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3574 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3575 | |
| 3576 | std::vector<spv::Id> operands; |
| 3577 | operands.push_back(pointer); |
| 3578 | for (; opIt != arguments.end(); ++opIt) |
| 3579 | operands.push_back(*opIt); |
| 3580 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3581 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 3585 | #ifdef AMD_EXTENSIONS |
| 3586 | // Check for fragment mask functions other than queries |
| 3587 | if (cracked.fragMask) { |
| 3588 | assert(sampler.ms); |
| 3589 | |
| 3590 | auto opIt = arguments.begin(); |
| 3591 | std::vector<spv::Id> operands; |
| 3592 | |
| 3593 | // Extract the image if necessary |
| 3594 | if (builder.isSampledImage(params.sampler)) |
| 3595 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 3596 | |
| 3597 | operands.push_back(params.sampler); |
| 3598 | ++opIt; |
| 3599 | |
| 3600 | if (sampler.isSubpass()) { |
| 3601 | // add on the (0,0) coordinate |
| 3602 | spv::Id zero = builder.makeIntConstant(0); |
| 3603 | std::vector<spv::Id> comps; |
| 3604 | comps.push_back(zero); |
| 3605 | comps.push_back(zero); |
| 3606 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 3607 | } |
| 3608 | |
| 3609 | for (; opIt != arguments.end(); ++opIt) |
| 3610 | operands.push_back(*opIt); |
| 3611 | |
| 3612 | spv::Op fragMaskOp = spv::OpNop; |
| 3613 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 3614 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 3615 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 3616 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 3617 | |
| 3618 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 3619 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 3620 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 3621 | } |
| 3622 | #endif |
| 3623 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3624 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3625 | bool sparse = node->isSparseTexture(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 3626 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 3627 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3628 | // check for bias argument |
| 3629 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3630 | #ifdef AMD_EXTENSIONS |
| 3631 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 3632 | #else |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 3633 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3634 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3635 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3636 | #ifdef AMD_EXTENSIONS |
| 3637 | if (cracked.gather) |
| 3638 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3639 | |
| 3640 | if (f16ShadowCompare) |
| 3641 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3642 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3643 | if (cracked.offset) |
| 3644 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3645 | #ifdef AMD_EXTENSIONS |
| 3646 | else if (cracked.offsets) |
| 3647 | ++nonBiasArgCount; |
| 3648 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3649 | if (cracked.grad) |
| 3650 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3651 | if (cracked.lodClamp) |
| 3652 | ++nonBiasArgCount; |
| 3653 | if (sparse) |
| 3654 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3655 | |
| 3656 | if ((int)arguments.size() > nonBiasArgCount) |
| 3657 | bias = true; |
| 3658 | } |
| 3659 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 3660 | // See if the sampler param should really be just the SPV image part |
| 3661 | if (cracked.fetch) { |
| 3662 | // a fetch needs to have the image extracted first |
| 3663 | if (builder.isSampledImage(params.sampler)) |
| 3664 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 3665 | } |
| 3666 | |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3667 | #ifdef AMD_EXTENSIONS |
| 3668 | if (cracked.gather) { |
| 3669 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 3670 | if (bias || cracked.lod || |
| 3671 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 3672 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 3673 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | #endif |
| 3677 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3678 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3679 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3680 | params.coords = arguments[1]; |
| 3681 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3682 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3683 | |
| 3684 | // sort out where Dref is coming from |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3685 | #ifdef AMD_EXTENSIONS |
| 3686 | if (cubeCompare || f16ShadowCompare) { |
| 3687 | #else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3688 | if (cubeCompare) { |
Rex Xu | 1e5d7b0 | 2016-11-29 17:36:31 +0800 | [diff] [blame] | 3689 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3690 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3691 | ++extraArgs; |
| 3692 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3693 | params.Dref = arguments[2]; |
| 3694 | ++extraArgs; |
| 3695 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3696 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3697 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3698 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3699 | 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] | 3700 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3701 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 3702 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3703 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 3704 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3705 | |
| 3706 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3707 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 3708 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3709 | ++extraArgs; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3710 | } else if (glslangIntermediate->getStage() != EShLangFragment) { |
| 3711 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 3712 | noImplicitLod = true; |
| 3713 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3714 | |
| 3715 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3716 | if (sampler.ms) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 3717 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3718 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3719 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3720 | |
| 3721 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3722 | if (cracked.grad) { |
| 3723 | params.gradX = arguments[2 + extraArgs]; |
| 3724 | params.gradY = arguments[3 + extraArgs]; |
| 3725 | extraArgs += 2; |
| 3726 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3727 | |
| 3728 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3729 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3730 | params.offset = arguments[2 + extraArgs]; |
| 3731 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3732 | } else if (cracked.offsets) { |
| 3733 | params.offsets = arguments[2 + extraArgs]; |
| 3734 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3735 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3736 | |
| 3737 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3738 | if (cracked.lodClamp) { |
| 3739 | params.lodClamp = arguments[2 + extraArgs]; |
| 3740 | ++extraArgs; |
| 3741 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3742 | |
| 3743 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3744 | if (sparse) { |
| 3745 | params.texelOut = arguments[2 + extraArgs]; |
| 3746 | ++extraArgs; |
| 3747 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3748 | |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3749 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3750 | if (cracked.gather && ! sampler.shadow) { |
| 3751 | // default component is 0, if missing, otherwise an argument |
| 3752 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3753 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3754 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3755 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3756 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | // bias |
| 3760 | if (bias) { |
| 3761 | params.bias = arguments[2 + extraArgs]; |
| 3762 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3763 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3764 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 3765 | // projective component (might not to move) |
| 3766 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 3767 | // are divided by the last component of P." |
| 3768 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 3769 | // unused components will appear after all used components." |
| 3770 | if (cracked.proj) { |
| 3771 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 3772 | int projTargetComp; |
| 3773 | switch (sampler.dim) { |
| 3774 | case glslang::Esd1D: projTargetComp = 1; break; |
| 3775 | case glslang::Esd2D: projTargetComp = 2; break; |
| 3776 | case glslang::EsdRect: projTargetComp = 2; break; |
| 3777 | default: projTargetComp = projSourceComp; break; |
| 3778 | } |
| 3779 | // copy the projective coordinate if we have to |
| 3780 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3781 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 3782 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 3783 | projSourceComp); |
| 3784 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 3785 | builder.getTypeId(params.coords), projTargetComp); |
| 3786 | } |
| 3787 | } |
| 3788 | |
LoopDawg | 4425f24 | 2018-02-18 11:40:01 -0700 | [diff] [blame] | 3789 | std::vector<spv::Id> result = { |
| 3790 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params) |
| 3791 | }; |
| 3792 | |
| 3793 | if (components != node->getType().getVectorSize()) |
| 3794 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 3795 | |
| 3796 | return result[0]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 3800 | { |
| 3801 | // Grab the function's pointer from the previously created function |
| 3802 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 3803 | if (! function) |
| 3804 | return 0; |
| 3805 | |
| 3806 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 3807 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 3808 | |
| 3809 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 3810 | // |
| 3811 | // These imply we need a four step process: |
| 3812 | // 1. Evaluate the arguments |
| 3813 | // 2. Allocate and make copies of in, out, and inout arguments |
| 3814 | // 3. Make the call |
| 3815 | // 4. Copy back the results |
| 3816 | |
| 3817 | // 1. Evaluate the arguments |
| 3818 | std::vector<spv::Builder::AccessChain> lValues; |
| 3819 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3820 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3821 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3822 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3823 | // build l-value |
| 3824 | builder.clearAccessChain(); |
| 3825 | glslangArgs[a]->traverse(this); |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3826 | argTypes.push_back(¶mType); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3827 | // keep outputs and pass-by-originals as l-values, evaluate others as r-values |
John Kessenich | 6a14f78 | 2017-12-04 02:48:10 -0700 | [diff] [blame] | 3828 | if (originalParam(qualifiers[a], paramType, function->hasImplicitThis() && a == 0) || |
| 3829 | writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3830 | // save l-value |
| 3831 | lValues.push_back(builder.getAccessChain()); |
| 3832 | } else { |
| 3833 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3834 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | |
| 3838 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 3839 | // copy the original into that space. |
| 3840 | // |
| 3841 | // Also, build up the list of actual arguments to pass in for the call |
| 3842 | int lValueCount = 0; |
| 3843 | int rValueCount = 0; |
| 3844 | std::vector<spv::Id> spvArgs; |
| 3845 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3846 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3847 | spv::Id arg; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3848 | if (originalParam(qualifiers[a], paramType, function->hasImplicitThis() && a == 0)) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3849 | builder.setAccessChain(lValues[lValueCount]); |
| 3850 | arg = builder.accessChainGetLValue(); |
| 3851 | ++lValueCount; |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3852 | } else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3853 | // need space to hold the copy |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3854 | arg = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(paramType), "param"); |
| 3855 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 3856 | // need to copy the input into output space |
| 3857 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3858 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3859 | builder.clearAccessChain(); |
| 3860 | builder.setAccessChainLValue(arg); |
| 3861 | multiTypeStore(paramType, copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3862 | } |
| 3863 | ++lValueCount; |
| 3864 | } else { |
| 3865 | arg = rValues[rValueCount]; |
| 3866 | ++rValueCount; |
| 3867 | } |
| 3868 | spvArgs.push_back(arg); |
| 3869 | } |
| 3870 | |
| 3871 | // 3. Make the call. |
| 3872 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3873 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3874 | |
| 3875 | // 4. Copy back out an "out" arguments. |
| 3876 | lValueCount = 0; |
| 3877 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3878 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | d41993d | 2017-09-10 15:21:05 -0600 | [diff] [blame] | 3879 | if (originalParam(qualifiers[a], paramType, function->hasImplicitThis() && a == 0)) |
| 3880 | ++lValueCount; |
| 3881 | else if (writableParam(qualifiers[a])) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3882 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 3883 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 3884 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3885 | multiTypeStore(paramType, copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3886 | } |
| 3887 | ++lValueCount; |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | return result; |
| 3892 | } |
| 3893 | |
| 3894 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3895 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, spv::Decoration precision, |
| 3896 | spv::Decoration noContraction, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3897 | spv::Id typeId, spv::Id left, spv::Id right, |
| 3898 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 3899 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 3900 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 3901 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3902 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3903 | |
| 3904 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3905 | 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] | 3906 | bool comparison = false; |
| 3907 | |
| 3908 | switch (op) { |
| 3909 | case glslang::EOpAdd: |
| 3910 | case glslang::EOpAddAssign: |
| 3911 | if (isFloat) |
| 3912 | binOp = spv::OpFAdd; |
| 3913 | else |
| 3914 | binOp = spv::OpIAdd; |
| 3915 | break; |
| 3916 | case glslang::EOpSub: |
| 3917 | case glslang::EOpSubAssign: |
| 3918 | if (isFloat) |
| 3919 | binOp = spv::OpFSub; |
| 3920 | else |
| 3921 | binOp = spv::OpISub; |
| 3922 | break; |
| 3923 | case glslang::EOpMul: |
| 3924 | case glslang::EOpMulAssign: |
| 3925 | if (isFloat) |
| 3926 | binOp = spv::OpFMul; |
| 3927 | else |
| 3928 | binOp = spv::OpIMul; |
| 3929 | break; |
| 3930 | case glslang::EOpVectorTimesScalar: |
| 3931 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 3932 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3933 | if (builder.isVector(right)) |
| 3934 | std::swap(left, right); |
| 3935 | assert(builder.isScalar(right)); |
| 3936 | needMatchingVectors = false; |
| 3937 | binOp = spv::OpVectorTimesScalar; |
| 3938 | } else |
| 3939 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3940 | break; |
| 3941 | case glslang::EOpVectorTimesMatrix: |
| 3942 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3943 | binOp = spv::OpVectorTimesMatrix; |
| 3944 | break; |
| 3945 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3946 | binOp = spv::OpMatrixTimesVector; |
| 3947 | break; |
| 3948 | case glslang::EOpMatrixTimesScalar: |
| 3949 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3950 | binOp = spv::OpMatrixTimesScalar; |
| 3951 | break; |
| 3952 | case glslang::EOpMatrixTimesMatrix: |
| 3953 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3954 | binOp = spv::OpMatrixTimesMatrix; |
| 3955 | break; |
| 3956 | case glslang::EOpOuterProduct: |
| 3957 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3958 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3959 | break; |
| 3960 | |
| 3961 | case glslang::EOpDiv: |
| 3962 | case glslang::EOpDivAssign: |
| 3963 | if (isFloat) |
| 3964 | binOp = spv::OpFDiv; |
| 3965 | else if (isUnsigned) |
| 3966 | binOp = spv::OpUDiv; |
| 3967 | else |
| 3968 | binOp = spv::OpSDiv; |
| 3969 | break; |
| 3970 | case glslang::EOpMod: |
| 3971 | case glslang::EOpModAssign: |
| 3972 | if (isFloat) |
| 3973 | binOp = spv::OpFMod; |
| 3974 | else if (isUnsigned) |
| 3975 | binOp = spv::OpUMod; |
| 3976 | else |
| 3977 | binOp = spv::OpSMod; |
| 3978 | break; |
| 3979 | case glslang::EOpRightShift: |
| 3980 | case glslang::EOpRightShiftAssign: |
| 3981 | if (isUnsigned) |
| 3982 | binOp = spv::OpShiftRightLogical; |
| 3983 | else |
| 3984 | binOp = spv::OpShiftRightArithmetic; |
| 3985 | break; |
| 3986 | case glslang::EOpLeftShift: |
| 3987 | case glslang::EOpLeftShiftAssign: |
| 3988 | binOp = spv::OpShiftLeftLogical; |
| 3989 | break; |
| 3990 | case glslang::EOpAnd: |
| 3991 | case glslang::EOpAndAssign: |
| 3992 | binOp = spv::OpBitwiseAnd; |
| 3993 | break; |
| 3994 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3995 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3996 | binOp = spv::OpLogicalAnd; |
| 3997 | break; |
| 3998 | case glslang::EOpInclusiveOr: |
| 3999 | case glslang::EOpInclusiveOrAssign: |
| 4000 | binOp = spv::OpBitwiseOr; |
| 4001 | break; |
| 4002 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4003 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4004 | binOp = spv::OpLogicalOr; |
| 4005 | break; |
| 4006 | case glslang::EOpExclusiveOr: |
| 4007 | case glslang::EOpExclusiveOrAssign: |
| 4008 | binOp = spv::OpBitwiseXor; |
| 4009 | break; |
| 4010 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 4011 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4012 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4013 | break; |
| 4014 | |
| 4015 | case glslang::EOpLessThan: |
| 4016 | case glslang::EOpGreaterThan: |
| 4017 | case glslang::EOpLessThanEqual: |
| 4018 | case glslang::EOpGreaterThanEqual: |
| 4019 | case glslang::EOpEqual: |
| 4020 | case glslang::EOpNotEqual: |
| 4021 | case glslang::EOpVectorEqual: |
| 4022 | case glslang::EOpVectorNotEqual: |
| 4023 | comparison = true; |
| 4024 | break; |
| 4025 | default: |
| 4026 | break; |
| 4027 | } |
| 4028 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4029 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4030 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4031 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4032 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4033 | return createBinaryMatrixOperation(binOp, precision, noContraction, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4034 | |
| 4035 | // 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] | 4036 | if (needMatchingVectors) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4037 | builder.promoteScalar(precision, left, right); |
| 4038 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4039 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 4040 | addDecoration(result, noContraction); |
| 4041 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | if (! comparison) |
| 4045 | return 0; |
| 4046 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4047 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4048 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 4049 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
| 4050 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 4051 | return builder.createCompositeCompare(precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4052 | |
| 4053 | switch (op) { |
| 4054 | case glslang::EOpLessThan: |
| 4055 | if (isFloat) |
| 4056 | binOp = spv::OpFOrdLessThan; |
| 4057 | else if (isUnsigned) |
| 4058 | binOp = spv::OpULessThan; |
| 4059 | else |
| 4060 | binOp = spv::OpSLessThan; |
| 4061 | break; |
| 4062 | case glslang::EOpGreaterThan: |
| 4063 | if (isFloat) |
| 4064 | binOp = spv::OpFOrdGreaterThan; |
| 4065 | else if (isUnsigned) |
| 4066 | binOp = spv::OpUGreaterThan; |
| 4067 | else |
| 4068 | binOp = spv::OpSGreaterThan; |
| 4069 | break; |
| 4070 | case glslang::EOpLessThanEqual: |
| 4071 | if (isFloat) |
| 4072 | binOp = spv::OpFOrdLessThanEqual; |
| 4073 | else if (isUnsigned) |
| 4074 | binOp = spv::OpULessThanEqual; |
| 4075 | else |
| 4076 | binOp = spv::OpSLessThanEqual; |
| 4077 | break; |
| 4078 | case glslang::EOpGreaterThanEqual: |
| 4079 | if (isFloat) |
| 4080 | binOp = spv::OpFOrdGreaterThanEqual; |
| 4081 | else if (isUnsigned) |
| 4082 | binOp = spv::OpUGreaterThanEqual; |
| 4083 | else |
| 4084 | binOp = spv::OpSGreaterThanEqual; |
| 4085 | break; |
| 4086 | case glslang::EOpEqual: |
| 4087 | case glslang::EOpVectorEqual: |
| 4088 | if (isFloat) |
| 4089 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4090 | else if (isBool) |
| 4091 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4092 | else |
| 4093 | binOp = spv::OpIEqual; |
| 4094 | break; |
| 4095 | case glslang::EOpNotEqual: |
| 4096 | case glslang::EOpVectorNotEqual: |
| 4097 | if (isFloat) |
| 4098 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 4099 | else if (isBool) |
| 4100 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4101 | else |
| 4102 | binOp = spv::OpINotEqual; |
| 4103 | break; |
| 4104 | default: |
| 4105 | break; |
| 4106 | } |
| 4107 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4108 | if (binOp != spv::OpNop) { |
| 4109 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 4110 | addDecoration(result, noContraction); |
| 4111 | return builder.setPrecision(result, precision); |
| 4112 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4113 | |
| 4114 | return 0; |
| 4115 | } |
| 4116 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4117 | // |
| 4118 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 4119 | // These can be any of: |
| 4120 | // |
| 4121 | // matrix * scalar |
| 4122 | // scalar * matrix |
| 4123 | // matrix * matrix linear algebraic |
| 4124 | // matrix * vector |
| 4125 | // vector * matrix |
| 4126 | // matrix * matrix componentwise |
| 4127 | // matrix op matrix op in {+, -, /} |
| 4128 | // matrix op scalar op in {+, -, /} |
| 4129 | // scalar op matrix op in {+, -, /} |
| 4130 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4131 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right) |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4132 | { |
| 4133 | bool firstClass = true; |
| 4134 | |
| 4135 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 4136 | switch (op) { |
| 4137 | case spv::OpFDiv: |
| 4138 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 4139 | // turn matrix / scalar into a multiply... |
| 4140 | right = builder.createBinOp(spv::OpFDiv, builder.getTypeId(right), builder.makeFloatConstant(1.0F), right); |
| 4141 | op = spv::OpMatrixTimesScalar; |
| 4142 | } else |
| 4143 | firstClass = false; |
| 4144 | break; |
| 4145 | case spv::OpMatrixTimesScalar: |
| 4146 | if (builder.isMatrix(right)) |
| 4147 | std::swap(left, right); |
| 4148 | assert(builder.isScalar(right)); |
| 4149 | break; |
| 4150 | case spv::OpVectorTimesMatrix: |
| 4151 | assert(builder.isVector(left)); |
| 4152 | assert(builder.isMatrix(right)); |
| 4153 | break; |
| 4154 | case spv::OpMatrixTimesVector: |
| 4155 | assert(builder.isMatrix(left)); |
| 4156 | assert(builder.isVector(right)); |
| 4157 | break; |
| 4158 | case spv::OpMatrixTimesMatrix: |
| 4159 | assert(builder.isMatrix(left)); |
| 4160 | assert(builder.isMatrix(right)); |
| 4161 | break; |
| 4162 | default: |
| 4163 | firstClass = false; |
| 4164 | break; |
| 4165 | } |
| 4166 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4167 | if (firstClass) { |
| 4168 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
| 4169 | addDecoration(result, noContraction); |
| 4170 | return builder.setPrecision(result, precision); |
| 4171 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4172 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4173 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4174 | // The result type of all of them is the same type as the (a) matrix operand. |
| 4175 | // The algorithm is to: |
| 4176 | // - break the matrix(es) into vectors |
| 4177 | // - smear any scalar to a vector |
| 4178 | // - do vector operations |
| 4179 | // - make a matrix out the vector results |
| 4180 | switch (op) { |
| 4181 | case spv::OpFAdd: |
| 4182 | case spv::OpFSub: |
| 4183 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4184 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4185 | case spv::OpFMul: |
| 4186 | { |
| 4187 | // one time set up... |
| 4188 | bool leftMat = builder.isMatrix(left); |
| 4189 | bool rightMat = builder.isMatrix(right); |
| 4190 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 4191 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 4192 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 4193 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 4194 | std::vector<spv::Id> results; |
| 4195 | spv::Id smearVec = spv::NoResult; |
| 4196 | if (builder.isScalar(left)) |
| 4197 | smearVec = builder.smearScalar(precision, left, vecType); |
| 4198 | else if (builder.isScalar(right)) |
| 4199 | smearVec = builder.smearScalar(precision, right, vecType); |
| 4200 | |
| 4201 | // do each vector op |
| 4202 | for (unsigned int c = 0; c < numCols; ++c) { |
| 4203 | std::vector<unsigned int> indexes; |
| 4204 | indexes.push_back(c); |
| 4205 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 4206 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4207 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
| 4208 | addDecoration(result, noContraction); |
| 4209 | results.push_back(builder.setPrecision(result, precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4213 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 4214 | } |
| 4215 | default: |
| 4216 | assert(0); |
| 4217 | return spv::NoResult; |
| 4218 | } |
| 4219 | } |
| 4220 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4221 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4222 | { |
| 4223 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4224 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4225 | int libCall = -1; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4226 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 4227 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4228 | |
| 4229 | switch (op) { |
| 4230 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4231 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4232 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4233 | if (builder.isMatrixType(typeId)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4234 | return createUnaryMatrixOperation(unaryOp, precision, noContraction, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4235 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4236 | unaryOp = spv::OpSNegate; |
| 4237 | break; |
| 4238 | |
| 4239 | case glslang::EOpLogicalNot: |
| 4240 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4241 | unaryOp = spv::OpLogicalNot; |
| 4242 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4243 | case glslang::EOpBitwiseNot: |
| 4244 | unaryOp = spv::OpNot; |
| 4245 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4246 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4247 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4248 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4249 | break; |
| 4250 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4251 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4252 | break; |
| 4253 | case glslang::EOpTranspose: |
| 4254 | unaryOp = spv::OpTranspose; |
| 4255 | break; |
| 4256 | |
| 4257 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4258 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4259 | break; |
| 4260 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4261 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4262 | break; |
| 4263 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4264 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4265 | break; |
| 4266 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4267 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4268 | break; |
| 4269 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4270 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4271 | break; |
| 4272 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4273 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4274 | break; |
| 4275 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4276 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4277 | break; |
| 4278 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4279 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4280 | break; |
| 4281 | |
| 4282 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4283 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4284 | break; |
| 4285 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4286 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4287 | break; |
| 4288 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4289 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4290 | break; |
| 4291 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4292 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4293 | break; |
| 4294 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4295 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4296 | break; |
| 4297 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4298 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4299 | break; |
| 4300 | |
| 4301 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4302 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4303 | break; |
| 4304 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4305 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4306 | break; |
| 4307 | |
| 4308 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4309 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4310 | break; |
| 4311 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4312 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4313 | break; |
| 4314 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4315 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4316 | break; |
| 4317 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4318 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4319 | break; |
| 4320 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4321 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4322 | break; |
| 4323 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4324 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4325 | break; |
| 4326 | |
| 4327 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4328 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4329 | break; |
| 4330 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4331 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4332 | break; |
| 4333 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4334 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4335 | break; |
| 4336 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4337 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4338 | break; |
| 4339 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4340 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4341 | break; |
| 4342 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4343 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4344 | break; |
| 4345 | |
| 4346 | case glslang::EOpIsNan: |
| 4347 | unaryOp = spv::OpIsNan; |
| 4348 | break; |
| 4349 | case glslang::EOpIsInf: |
| 4350 | unaryOp = spv::OpIsInf; |
| 4351 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4352 | case glslang::EOpIsFinite: |
| 4353 | unaryOp = spv::OpIsFinite; |
| 4354 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4355 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 4356 | case glslang::EOpFloatBitsToInt: |
| 4357 | case glslang::EOpFloatBitsToUint: |
| 4358 | case glslang::EOpIntBitsToFloat: |
| 4359 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4360 | case glslang::EOpDoubleBitsToInt64: |
| 4361 | case glslang::EOpDoubleBitsToUint64: |
| 4362 | case glslang::EOpInt64BitsToDouble: |
| 4363 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4364 | case glslang::EOpFloat16BitsToInt16: |
| 4365 | case glslang::EOpFloat16BitsToUint16: |
| 4366 | case glslang::EOpInt16BitsToFloat16: |
| 4367 | case glslang::EOpUint16BitsToFloat16: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 4368 | unaryOp = spv::OpBitcast; |
| 4369 | break; |
| 4370 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4371 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4372 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4373 | break; |
| 4374 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4375 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4376 | break; |
| 4377 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4378 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4379 | break; |
| 4380 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4381 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4382 | break; |
| 4383 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4384 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4385 | break; |
| 4386 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4387 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4388 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4389 | case glslang::EOpPackSnorm4x8: |
| 4390 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 4391 | break; |
| 4392 | case glslang::EOpUnpackSnorm4x8: |
| 4393 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 4394 | break; |
| 4395 | case glslang::EOpPackUnorm4x8: |
| 4396 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 4397 | break; |
| 4398 | case glslang::EOpUnpackUnorm4x8: |
| 4399 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 4400 | break; |
| 4401 | case glslang::EOpPackDouble2x32: |
| 4402 | libCall = spv::GLSLstd450PackDouble2x32; |
| 4403 | break; |
| 4404 | case glslang::EOpUnpackDouble2x32: |
| 4405 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 4406 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4407 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4408 | case glslang::EOpPackInt2x32: |
| 4409 | case glslang::EOpUnpackInt2x32: |
| 4410 | case glslang::EOpPackUint2x32: |
| 4411 | case glslang::EOpUnpackUint2x32: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4412 | case glslang::EOpPack16: |
| 4413 | case glslang::EOpPack32: |
| 4414 | case glslang::EOpPack64: |
| 4415 | case glslang::EOpUnpack32: |
| 4416 | case glslang::EOpUnpack16: |
| 4417 | case glslang::EOpUnpack8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4418 | case glslang::EOpPackInt2x16: |
| 4419 | case glslang::EOpUnpackInt2x16: |
| 4420 | case glslang::EOpPackUint2x16: |
| 4421 | case glslang::EOpUnpackUint2x16: |
| 4422 | case glslang::EOpPackInt4x16: |
| 4423 | case glslang::EOpUnpackInt4x16: |
| 4424 | case glslang::EOpPackUint4x16: |
| 4425 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4426 | case glslang::EOpPackFloat2x16: |
| 4427 | case glslang::EOpUnpackFloat2x16: |
| 4428 | unaryOp = spv::OpBitcast; |
| 4429 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4430 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4431 | case glslang::EOpDPdx: |
| 4432 | unaryOp = spv::OpDPdx; |
| 4433 | break; |
| 4434 | case glslang::EOpDPdy: |
| 4435 | unaryOp = spv::OpDPdy; |
| 4436 | break; |
| 4437 | case glslang::EOpFwidth: |
| 4438 | unaryOp = spv::OpFwidth; |
| 4439 | break; |
| 4440 | case glslang::EOpDPdxFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4441 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4442 | unaryOp = spv::OpDPdxFine; |
| 4443 | break; |
| 4444 | case glslang::EOpDPdyFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4445 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4446 | unaryOp = spv::OpDPdyFine; |
| 4447 | break; |
| 4448 | case glslang::EOpFwidthFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4449 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4450 | unaryOp = spv::OpFwidthFine; |
| 4451 | break; |
| 4452 | case glslang::EOpDPdxCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4453 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4454 | unaryOp = spv::OpDPdxCoarse; |
| 4455 | break; |
| 4456 | case glslang::EOpDPdyCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4457 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4458 | unaryOp = spv::OpDPdyCoarse; |
| 4459 | break; |
| 4460 | case glslang::EOpFwidthCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4461 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4462 | unaryOp = spv::OpFwidthCoarse; |
| 4463 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4464 | case glslang::EOpInterpolateAtCentroid: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4465 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4466 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 4467 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4468 | case glslang::EOpAny: |
| 4469 | unaryOp = spv::OpAny; |
| 4470 | break; |
| 4471 | case glslang::EOpAll: |
| 4472 | unaryOp = spv::OpAll; |
| 4473 | break; |
| 4474 | |
| 4475 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4476 | if (isFloat) |
| 4477 | libCall = spv::GLSLstd450FAbs; |
| 4478 | else |
| 4479 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4480 | break; |
| 4481 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4482 | if (isFloat) |
| 4483 | libCall = spv::GLSLstd450FSign; |
| 4484 | else |
| 4485 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4486 | break; |
| 4487 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4488 | case glslang::EOpAtomicCounterIncrement: |
| 4489 | case glslang::EOpAtomicCounterDecrement: |
| 4490 | case glslang::EOpAtomicCounter: |
| 4491 | { |
| 4492 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 4493 | std::vector<spv::Id> operands; |
| 4494 | operands.push_back(operand); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4495 | return createAtomicOperation(op, precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4496 | } |
| 4497 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4498 | case glslang::EOpBitFieldReverse: |
| 4499 | unaryOp = spv::OpBitReverse; |
| 4500 | break; |
| 4501 | case glslang::EOpBitCount: |
| 4502 | unaryOp = spv::OpBitCount; |
| 4503 | break; |
| 4504 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4505 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4506 | break; |
| 4507 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4508 | if (isUnsigned) |
| 4509 | libCall = spv::GLSLstd450FindUMsb; |
| 4510 | else |
| 4511 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4512 | break; |
| 4513 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 4514 | case glslang::EOpBallot: |
| 4515 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4516 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4517 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4518 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4519 | #ifdef AMD_EXTENSIONS |
| 4520 | case glslang::EOpMinInvocations: |
| 4521 | case glslang::EOpMaxInvocations: |
| 4522 | case glslang::EOpAddInvocations: |
| 4523 | case glslang::EOpMinInvocationsNonUniform: |
| 4524 | case glslang::EOpMaxInvocationsNonUniform: |
| 4525 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4526 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4527 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4528 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4529 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4530 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4531 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4532 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4533 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4534 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4535 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4536 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4537 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4538 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4539 | { |
| 4540 | std::vector<spv::Id> operands; |
| 4541 | operands.push_back(operand); |
| 4542 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 4543 | } |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4544 | case glslang::EOpSubgroupAll: |
| 4545 | case glslang::EOpSubgroupAny: |
| 4546 | case glslang::EOpSubgroupAllEqual: |
| 4547 | case glslang::EOpSubgroupBroadcastFirst: |
| 4548 | case glslang::EOpSubgroupBallot: |
| 4549 | case glslang::EOpSubgroupInverseBallot: |
| 4550 | case glslang::EOpSubgroupBallotBitCount: |
| 4551 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 4552 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 4553 | case glslang::EOpSubgroupBallotFindLSB: |
| 4554 | case glslang::EOpSubgroupBallotFindMSB: |
| 4555 | case glslang::EOpSubgroupAdd: |
| 4556 | case glslang::EOpSubgroupMul: |
| 4557 | case glslang::EOpSubgroupMin: |
| 4558 | case glslang::EOpSubgroupMax: |
| 4559 | case glslang::EOpSubgroupAnd: |
| 4560 | case glslang::EOpSubgroupOr: |
| 4561 | case glslang::EOpSubgroupXor: |
| 4562 | case glslang::EOpSubgroupInclusiveAdd: |
| 4563 | case glslang::EOpSubgroupInclusiveMul: |
| 4564 | case glslang::EOpSubgroupInclusiveMin: |
| 4565 | case glslang::EOpSubgroupInclusiveMax: |
| 4566 | case glslang::EOpSubgroupInclusiveAnd: |
| 4567 | case glslang::EOpSubgroupInclusiveOr: |
| 4568 | case glslang::EOpSubgroupInclusiveXor: |
| 4569 | case glslang::EOpSubgroupExclusiveAdd: |
| 4570 | case glslang::EOpSubgroupExclusiveMul: |
| 4571 | case glslang::EOpSubgroupExclusiveMin: |
| 4572 | case glslang::EOpSubgroupExclusiveMax: |
| 4573 | case glslang::EOpSubgroupExclusiveAnd: |
| 4574 | case glslang::EOpSubgroupExclusiveOr: |
| 4575 | case glslang::EOpSubgroupExclusiveXor: |
| 4576 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 4577 | case glslang::EOpSubgroupQuadSwapVertical: |
| 4578 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 4579 | std::vector<spv::Id> operands; |
| 4580 | operands.push_back(operand); |
| 4581 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 4582 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4583 | #ifdef AMD_EXTENSIONS |
| 4584 | case glslang::EOpMbcnt: |
| 4585 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 4586 | libCall = spv::MbcntAMD; |
| 4587 | break; |
| 4588 | |
| 4589 | case glslang::EOpCubeFaceIndex: |
| 4590 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 4591 | libCall = spv::CubeFaceIndexAMD; |
| 4592 | break; |
| 4593 | |
| 4594 | case glslang::EOpCubeFaceCoord: |
| 4595 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 4596 | libCall = spv::CubeFaceCoordAMD; |
| 4597 | break; |
| 4598 | #endif |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4599 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4600 | default: |
| 4601 | return 0; |
| 4602 | } |
| 4603 | |
| 4604 | spv::Id id; |
| 4605 | if (libCall >= 0) { |
| 4606 | std::vector<spv::Id> args; |
| 4607 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4608 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4609 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4610 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4611 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4612 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4613 | addDecoration(id, noContraction); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4614 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4615 | } |
| 4616 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4617 | // Create a unary operation on a matrix |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4618 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand, glslang::TBasicType /* typeProxy */) |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4619 | { |
| 4620 | // Handle unary operations vector by vector. |
| 4621 | // The result type is the same type as the original type. |
| 4622 | // The algorithm is to: |
| 4623 | // - break the matrix into vectors |
| 4624 | // - apply the operation to each vector |
| 4625 | // - make a matrix out the vector results |
| 4626 | |
| 4627 | // get the types sorted out |
| 4628 | int numCols = builder.getNumColumns(operand); |
| 4629 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 4630 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 4631 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4632 | std::vector<spv::Id> results; |
| 4633 | |
| 4634 | // do each vector op |
| 4635 | for (int c = 0; c < numCols; ++c) { |
| 4636 | std::vector<unsigned int> indexes; |
| 4637 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 4638 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 4639 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
| 4640 | addDecoration(destVec, noContraction); |
| 4641 | results.push_back(builder.setPrecision(destVec, precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4645 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4646 | } |
| 4647 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4648 | spv::Id TGlslangToSpvTraverser::createConversionOperation(glslang::TOperator op, spv::Id operand, int vectorSize) |
| 4649 | { |
| 4650 | spv::Op convOp = spv::OpNop; |
| 4651 | spv::Id type = 0; |
| 4652 | |
| 4653 | spv::Id result = 0; |
| 4654 | |
| 4655 | switch(op) { |
| 4656 | case glslang::EOpConvInt8ToUint16: |
| 4657 | convOp = spv::OpSConvert; |
| 4658 | type = builder.makeIntType(16); |
| 4659 | break; |
| 4660 | case glslang::EOpConvInt8ToUint: |
| 4661 | convOp = spv::OpSConvert; |
| 4662 | type = builder.makeIntType(32); |
| 4663 | break; |
| 4664 | case glslang::EOpConvInt8ToUint64: |
| 4665 | convOp = spv::OpSConvert; |
| 4666 | type = builder.makeIntType(64); |
| 4667 | break; |
| 4668 | case glslang::EOpConvInt16ToUint8: |
| 4669 | convOp = spv::OpSConvert; |
| 4670 | type = builder.makeIntType(8); |
| 4671 | break; |
| 4672 | case glslang::EOpConvInt16ToUint: |
| 4673 | convOp = spv::OpSConvert; |
| 4674 | type = builder.makeIntType(32); |
| 4675 | break; |
| 4676 | case glslang::EOpConvInt16ToUint64: |
| 4677 | convOp = spv::OpSConvert; |
| 4678 | type = builder.makeIntType(64); |
| 4679 | break; |
| 4680 | case glslang::EOpConvIntToUint8: |
| 4681 | convOp = spv::OpSConvert; |
| 4682 | type = builder.makeIntType(8); |
| 4683 | break; |
| 4684 | case glslang::EOpConvIntToUint16: |
| 4685 | convOp = spv::OpSConvert; |
| 4686 | type = builder.makeIntType(16); |
| 4687 | break; |
| 4688 | case glslang::EOpConvIntToUint64: |
| 4689 | convOp = spv::OpSConvert; |
| 4690 | type = builder.makeIntType(64); |
| 4691 | break; |
| 4692 | case glslang::EOpConvInt64ToUint8: |
| 4693 | convOp = spv::OpSConvert; |
| 4694 | type = builder.makeIntType(8); |
| 4695 | break; |
| 4696 | case glslang::EOpConvInt64ToUint16: |
| 4697 | convOp = spv::OpSConvert; |
| 4698 | type = builder.makeIntType(16); |
| 4699 | break; |
| 4700 | case glslang::EOpConvInt64ToUint: |
| 4701 | convOp = spv::OpSConvert; |
| 4702 | type = builder.makeIntType(32); |
| 4703 | break; |
| 4704 | case glslang::EOpConvUint8ToInt16: |
| 4705 | convOp = spv::OpUConvert; |
| 4706 | type = builder.makeIntType(16); |
| 4707 | break; |
| 4708 | case glslang::EOpConvUint8ToInt: |
| 4709 | convOp = spv::OpUConvert; |
| 4710 | type = builder.makeIntType(32); |
| 4711 | break; |
| 4712 | case glslang::EOpConvUint8ToInt64: |
| 4713 | convOp = spv::OpUConvert; |
| 4714 | type = builder.makeIntType(64); |
| 4715 | break; |
| 4716 | case glslang::EOpConvUint16ToInt8: |
| 4717 | convOp = spv::OpUConvert; |
| 4718 | type = builder.makeIntType(8); |
| 4719 | break; |
| 4720 | case glslang::EOpConvUint16ToInt: |
| 4721 | convOp = spv::OpUConvert; |
| 4722 | type = builder.makeIntType(32); |
| 4723 | break; |
| 4724 | case glslang::EOpConvUint16ToInt64: |
| 4725 | convOp = spv::OpUConvert; |
| 4726 | type = builder.makeIntType(64); |
| 4727 | break; |
| 4728 | case glslang::EOpConvUintToInt8: |
| 4729 | convOp = spv::OpUConvert; |
| 4730 | type = builder.makeIntType(8); |
| 4731 | break; |
| 4732 | case glslang::EOpConvUintToInt16: |
| 4733 | convOp = spv::OpUConvert; |
| 4734 | type = builder.makeIntType(16); |
| 4735 | break; |
| 4736 | case glslang::EOpConvUintToInt64: |
| 4737 | convOp = spv::OpUConvert; |
| 4738 | type = builder.makeIntType(64); |
| 4739 | break; |
| 4740 | case glslang::EOpConvUint64ToInt8: |
| 4741 | convOp = spv::OpUConvert; |
| 4742 | type = builder.makeIntType(8); |
| 4743 | break; |
| 4744 | case glslang::EOpConvUint64ToInt16: |
| 4745 | convOp = spv::OpUConvert; |
| 4746 | type = builder.makeIntType(16); |
| 4747 | break; |
| 4748 | case glslang::EOpConvUint64ToInt: |
| 4749 | convOp = spv::OpUConvert; |
| 4750 | type = builder.makeIntType(32); |
| 4751 | break; |
| 4752 | |
| 4753 | default: |
| 4754 | assert(false && "Default missing"); |
| 4755 | break; |
| 4756 | } |
| 4757 | |
| 4758 | if (vectorSize > 0) |
| 4759 | type = builder.makeVectorType(type, vectorSize); |
| 4760 | |
| 4761 | result = builder.createUnaryOp(convOp, type, operand); |
| 4762 | return result; |
| 4763 | } |
| 4764 | |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 4765 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id destType, spv::Id operand, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4766 | { |
| 4767 | spv::Op convOp = spv::OpNop; |
| 4768 | spv::Id zero = 0; |
| 4769 | spv::Id one = 0; |
| 4770 | |
| 4771 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 4772 | |
| 4773 | switch (op) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4774 | case glslang::EOpConvInt8ToBool: |
| 4775 | case glslang::EOpConvUint8ToBool: |
| 4776 | zero = builder.makeUint8Constant(0); |
| 4777 | zero = makeSmearedConstant(zero, vectorSize); |
| 4778 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4779 | case glslang::EOpConvInt16ToBool: |
| 4780 | case glslang::EOpConvUint16ToBool: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4781 | zero = builder.makeUint16Constant(0); |
| 4782 | zero = makeSmearedConstant(zero, vectorSize); |
| 4783 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 4784 | case glslang::EOpConvIntToBool: |
| 4785 | case glslang::EOpConvUintToBool: |
| 4786 | zero = builder.makeUintConstant(0); |
| 4787 | zero = makeSmearedConstant(zero, vectorSize); |
| 4788 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 4789 | case glslang::EOpConvInt64ToBool: |
| 4790 | case glslang::EOpConvUint64ToBool: |
| 4791 | zero = builder.makeUint64Constant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4792 | zero = makeSmearedConstant(zero, vectorSize); |
| 4793 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 4794 | |
| 4795 | case glslang::EOpConvFloatToBool: |
| 4796 | zero = builder.makeFloatConstant(0.0F); |
| 4797 | zero = makeSmearedConstant(zero, vectorSize); |
| 4798 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4799 | |
| 4800 | case glslang::EOpConvDoubleToBool: |
| 4801 | zero = builder.makeDoubleConstant(0.0); |
| 4802 | zero = makeSmearedConstant(zero, vectorSize); |
| 4803 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4804 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4805 | case glslang::EOpConvFloat16ToBool: |
| 4806 | zero = builder.makeFloat16Constant(0.0F); |
| 4807 | zero = makeSmearedConstant(zero, vectorSize); |
| 4808 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4809 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4810 | case glslang::EOpConvBoolToFloat: |
| 4811 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4812 | zero = builder.makeFloatConstant(0.0F); |
| 4813 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4814 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4815 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4816 | case glslang::EOpConvBoolToDouble: |
| 4817 | convOp = spv::OpSelect; |
| 4818 | zero = builder.makeDoubleConstant(0.0); |
| 4819 | one = builder.makeDoubleConstant(1.0); |
| 4820 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4821 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4822 | case glslang::EOpConvBoolToFloat16: |
| 4823 | convOp = spv::OpSelect; |
| 4824 | zero = builder.makeFloat16Constant(0.0F); |
| 4825 | one = builder.makeFloat16Constant(1.0F); |
| 4826 | break; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4827 | |
| 4828 | case glslang::EOpConvBoolToInt8: |
| 4829 | zero = builder.makeInt8Constant(0); |
| 4830 | one = builder.makeInt8Constant(1); |
| 4831 | convOp = spv::OpSelect; |
| 4832 | break; |
| 4833 | |
| 4834 | case glslang::EOpConvBoolToUint8: |
| 4835 | zero = builder.makeUint8Constant(0); |
| 4836 | one = builder.makeUint8Constant(1); |
| 4837 | convOp = spv::OpSelect; |
| 4838 | break; |
| 4839 | |
| 4840 | case glslang::EOpConvBoolToInt16: |
| 4841 | zero = builder.makeInt16Constant(0); |
| 4842 | one = builder.makeInt16Constant(1); |
| 4843 | convOp = spv::OpSelect; |
| 4844 | break; |
| 4845 | |
| 4846 | case glslang::EOpConvBoolToUint16: |
| 4847 | zero = builder.makeUint16Constant(0); |
| 4848 | one = builder.makeUint16Constant(1); |
| 4849 | convOp = spv::OpSelect; |
| 4850 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4851 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4852 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4853 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4854 | if (op == glslang::EOpConvBoolToInt64) |
| 4855 | zero = builder.makeInt64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4856 | else |
| 4857 | zero = builder.makeIntConstant(0); |
| 4858 | |
| 4859 | if (op == glslang::EOpConvBoolToInt64) |
| 4860 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4861 | else |
| 4862 | one = builder.makeIntConstant(1); |
| 4863 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4864 | convOp = spv::OpSelect; |
| 4865 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4866 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4867 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4868 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4869 | if (op == glslang::EOpConvBoolToUint64) |
| 4870 | zero = builder.makeUint64Constant(0); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4871 | else |
| 4872 | zero = builder.makeUintConstant(0); |
| 4873 | |
| 4874 | if (op == glslang::EOpConvBoolToUint64) |
| 4875 | one = builder.makeUint64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4876 | else |
| 4877 | one = builder.makeUintConstant(1); |
| 4878 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4879 | convOp = spv::OpSelect; |
| 4880 | break; |
| 4881 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4882 | case glslang::EOpConvInt8ToFloat16: |
| 4883 | case glslang::EOpConvInt8ToFloat: |
| 4884 | case glslang::EOpConvInt8ToDouble: |
| 4885 | case glslang::EOpConvInt16ToFloat16: |
| 4886 | case glslang::EOpConvInt16ToFloat: |
| 4887 | case glslang::EOpConvInt16ToDouble: |
| 4888 | case glslang::EOpConvIntToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4889 | case glslang::EOpConvIntToFloat: |
| 4890 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4891 | case glslang::EOpConvInt64ToFloat: |
| 4892 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4893 | case glslang::EOpConvInt64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4894 | convOp = spv::OpConvertSToF; |
| 4895 | break; |
| 4896 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4897 | case glslang::EOpConvUint8ToFloat16: |
| 4898 | case glslang::EOpConvUint8ToFloat: |
| 4899 | case glslang::EOpConvUint8ToDouble: |
| 4900 | case glslang::EOpConvUint16ToFloat16: |
| 4901 | case glslang::EOpConvUint16ToFloat: |
| 4902 | case glslang::EOpConvUint16ToDouble: |
| 4903 | case glslang::EOpConvUintToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4904 | case glslang::EOpConvUintToFloat: |
| 4905 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4906 | case glslang::EOpConvUint64ToFloat: |
| 4907 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4908 | case glslang::EOpConvUint64ToFloat16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4909 | convOp = spv::OpConvertUToF; |
| 4910 | break; |
| 4911 | |
| 4912 | case glslang::EOpConvDoubleToFloat: |
| 4913 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4914 | case glslang::EOpConvDoubleToFloat16: |
| 4915 | case glslang::EOpConvFloat16ToDouble: |
| 4916 | case glslang::EOpConvFloatToFloat16: |
| 4917 | case glslang::EOpConvFloat16ToFloat: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4918 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 4919 | if (builder.isMatrixType(destType)) |
| 4920 | return createUnaryMatrixOperation(convOp, precision, noContraction, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4921 | break; |
| 4922 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4923 | case glslang::EOpConvFloat16ToInt8: |
| 4924 | case glslang::EOpConvFloatToInt8: |
| 4925 | case glslang::EOpConvDoubleToInt8: |
| 4926 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4927 | case glslang::EOpConvFloatToInt16: |
| 4928 | case glslang::EOpConvDoubleToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4929 | case glslang::EOpConvFloat16ToInt: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4930 | case glslang::EOpConvFloatToInt: |
| 4931 | case glslang::EOpConvDoubleToInt: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4932 | case glslang::EOpConvFloat16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4933 | case glslang::EOpConvFloatToInt64: |
| 4934 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4935 | convOp = spv::OpConvertFToS; |
| 4936 | break; |
| 4937 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4938 | case glslang::EOpConvUint8ToInt8: |
| 4939 | case glslang::EOpConvInt8ToUint8: |
| 4940 | case glslang::EOpConvUint16ToInt16: |
| 4941 | case glslang::EOpConvInt16ToUint16: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4942 | case glslang::EOpConvUintToInt: |
| 4943 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4944 | case glslang::EOpConvUint64ToInt64: |
| 4945 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 4946 | if (builder.isInSpecConstCodeGenMode()) { |
| 4947 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4948 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 4949 | zero = builder.makeUint8Constant(0); |
| 4950 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4951 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4952 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 4953 | zero = builder.makeUint64Constant(0); |
| 4954 | } else { |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4955 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4956 | } |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 4957 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 4958 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 4959 | // generating for OpSpecConstantOp instruction. |
| 4960 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 4961 | } |
| 4962 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4963 | convOp = spv::OpBitcast; |
| 4964 | break; |
| 4965 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4966 | case glslang::EOpConvFloat16ToUint8: |
| 4967 | case glslang::EOpConvFloatToUint8: |
| 4968 | case glslang::EOpConvDoubleToUint8: |
| 4969 | case glslang::EOpConvFloat16ToUint16: |
| 4970 | case glslang::EOpConvFloatToUint16: |
| 4971 | case glslang::EOpConvDoubleToUint16: |
| 4972 | case glslang::EOpConvFloat16ToUint: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4973 | case glslang::EOpConvFloatToUint: |
| 4974 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4975 | case glslang::EOpConvFloatToUint64: |
| 4976 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4977 | case glslang::EOpConvFloat16ToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4978 | convOp = spv::OpConvertFToU; |
| 4979 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4980 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4981 | case glslang::EOpConvInt8ToInt16: |
| 4982 | case glslang::EOpConvInt8ToInt: |
| 4983 | case glslang::EOpConvInt8ToInt64: |
| 4984 | case glslang::EOpConvInt16ToInt8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4985 | case glslang::EOpConvInt16ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4986 | case glslang::EOpConvInt16ToInt64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4987 | case glslang::EOpConvIntToInt8: |
| 4988 | case glslang::EOpConvIntToInt16: |
| 4989 | case glslang::EOpConvIntToInt64: |
| 4990 | case glslang::EOpConvInt64ToInt8: |
| 4991 | case glslang::EOpConvInt64ToInt16: |
| 4992 | case glslang::EOpConvInt64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4993 | convOp = spv::OpSConvert; |
| 4994 | break; |
| 4995 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 4996 | case glslang::EOpConvUint8ToUint16: |
| 4997 | case glslang::EOpConvUint8ToUint: |
| 4998 | case glslang::EOpConvUint8ToUint64: |
| 4999 | case glslang::EOpConvUint16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5000 | case glslang::EOpConvUint16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5001 | case glslang::EOpConvUint16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5002 | case glslang::EOpConvUintToUint8: |
| 5003 | case glslang::EOpConvUintToUint16: |
| 5004 | case glslang::EOpConvUintToUint64: |
| 5005 | case glslang::EOpConvUint64ToUint8: |
| 5006 | case glslang::EOpConvUint64ToUint16: |
| 5007 | case glslang::EOpConvUint64ToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5008 | convOp = spv::OpUConvert; |
| 5009 | break; |
| 5010 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5011 | case glslang::EOpConvInt8ToUint16: |
| 5012 | case glslang::EOpConvInt8ToUint: |
| 5013 | case glslang::EOpConvInt8ToUint64: |
| 5014 | case glslang::EOpConvInt16ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5015 | case glslang::EOpConvInt16ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5016 | case glslang::EOpConvInt16ToUint64: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5017 | case glslang::EOpConvIntToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5018 | case glslang::EOpConvIntToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5019 | case glslang::EOpConvIntToUint64: |
| 5020 | case glslang::EOpConvInt64ToUint8: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5021 | case glslang::EOpConvInt64ToUint16: |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5022 | case glslang::EOpConvInt64ToUint: |
| 5023 | case glslang::EOpConvUint8ToInt16: |
| 5024 | case glslang::EOpConvUint8ToInt: |
| 5025 | case glslang::EOpConvUint8ToInt64: |
| 5026 | case glslang::EOpConvUint16ToInt8: |
| 5027 | case glslang::EOpConvUint16ToInt: |
| 5028 | case glslang::EOpConvUint16ToInt64: |
| 5029 | case glslang::EOpConvUintToInt8: |
| 5030 | case glslang::EOpConvUintToInt16: |
| 5031 | case glslang::EOpConvUintToInt64: |
| 5032 | case glslang::EOpConvUint64ToInt8: |
| 5033 | case glslang::EOpConvUint64ToInt16: |
| 5034 | case glslang::EOpConvUint64ToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5035 | // OpSConvert/OpUConvert + OpBitCast |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5036 | operand = createConversionOperation(op, operand, vectorSize); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5037 | |
| 5038 | if (builder.isInSpecConstCodeGenMode()) { |
| 5039 | // Build zero scalar or vector for OpIAdd. |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5040 | switch(op) { |
| 5041 | case glslang::EOpConvInt16ToUint8: |
| 5042 | case glslang::EOpConvIntToUint8: |
| 5043 | case glslang::EOpConvInt64ToUint8: |
| 5044 | case glslang::EOpConvUint16ToInt8: |
| 5045 | case glslang::EOpConvUintToInt8: |
| 5046 | case glslang::EOpConvUint64ToInt8: |
| 5047 | zero = builder.makeUint8Constant(0); |
| 5048 | break; |
| 5049 | case glslang::EOpConvInt8ToUint16: |
| 5050 | case glslang::EOpConvIntToUint16: |
| 5051 | case glslang::EOpConvInt64ToUint16: |
| 5052 | case glslang::EOpConvUint8ToInt16: |
| 5053 | case glslang::EOpConvUintToInt16: |
| 5054 | case glslang::EOpConvUint64ToInt16: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5055 | zero = builder.makeUint16Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5056 | break; |
| 5057 | case glslang::EOpConvInt8ToUint: |
| 5058 | case glslang::EOpConvInt16ToUint: |
| 5059 | case glslang::EOpConvInt64ToUint: |
| 5060 | case glslang::EOpConvUint8ToInt: |
| 5061 | case glslang::EOpConvUint16ToInt: |
| 5062 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5063 | zero = builder.makeUintConstant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5064 | break; |
| 5065 | case glslang::EOpConvInt8ToUint64: |
| 5066 | case glslang::EOpConvInt16ToUint64: |
| 5067 | case glslang::EOpConvIntToUint64: |
| 5068 | case glslang::EOpConvUint8ToInt64: |
| 5069 | case glslang::EOpConvUint16ToInt64: |
| 5070 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5071 | zero = builder.makeUint64Constant(0); |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5072 | break; |
| 5073 | default: |
| 5074 | assert(false && "Default missing"); |
| 5075 | break; |
| 5076 | } |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5077 | zero = makeSmearedConstant(zero, vectorSize); |
| 5078 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 5079 | // generating for OpSpecConstantOp instruction. |
| 5080 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 5081 | } |
| 5082 | // For normal run-time conversion instruction, use OpBitcast. |
| 5083 | convOp = spv::OpBitcast; |
| 5084 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5085 | default: |
| 5086 | break; |
| 5087 | } |
| 5088 | |
| 5089 | spv::Id result = 0; |
| 5090 | if (convOp == spv::OpNop) |
| 5091 | return result; |
| 5092 | |
| 5093 | if (convOp == spv::OpSelect) { |
| 5094 | zero = makeSmearedConstant(zero, vectorSize); |
| 5095 | one = makeSmearedConstant(one, vectorSize); |
| 5096 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 5097 | } else |
| 5098 | result = builder.createUnaryOp(convOp, destType, operand); |
| 5099 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5100 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 5104 | { |
| 5105 | if (vectorSize == 0) |
| 5106 | return constant; |
| 5107 | |
| 5108 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 5109 | std::vector<spv::Id> components; |
| 5110 | for (int c = 0; c < vectorSize; ++c) |
| 5111 | components.push_back(constant); |
| 5112 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 5113 | } |
| 5114 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5115 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5116 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5117 | { |
| 5118 | spv::Op opCode = spv::OpNop; |
| 5119 | |
| 5120 | switch (op) { |
| 5121 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5122 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5123 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5124 | opCode = spv::OpAtomicIAdd; |
| 5125 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5126 | case glslang::EOpAtomicCounterSubtract: |
| 5127 | opCode = spv::OpAtomicISub; |
| 5128 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5129 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5130 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5131 | case glslang::EOpAtomicCounterMin: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5132 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5133 | break; |
| 5134 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5135 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5136 | case glslang::EOpAtomicCounterMax: |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5137 | opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5138 | break; |
| 5139 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5140 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5141 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5142 | opCode = spv::OpAtomicAnd; |
| 5143 | break; |
| 5144 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5145 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5146 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5147 | opCode = spv::OpAtomicOr; |
| 5148 | break; |
| 5149 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5150 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5151 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5152 | opCode = spv::OpAtomicXor; |
| 5153 | break; |
| 5154 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5155 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5156 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5157 | opCode = spv::OpAtomicExchange; |
| 5158 | break; |
| 5159 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 5160 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 5161 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5162 | opCode = spv::OpAtomicCompareExchange; |
| 5163 | break; |
| 5164 | case glslang::EOpAtomicCounterIncrement: |
| 5165 | opCode = spv::OpAtomicIIncrement; |
| 5166 | break; |
| 5167 | case glslang::EOpAtomicCounterDecrement: |
| 5168 | opCode = spv::OpAtomicIDecrement; |
| 5169 | break; |
| 5170 | case glslang::EOpAtomicCounter: |
| 5171 | opCode = spv::OpAtomicLoad; |
| 5172 | break; |
| 5173 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5174 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5175 | break; |
| 5176 | } |
| 5177 | |
Rex Xu | e8fe8b0 | 2017-09-26 15:42:56 +0800 | [diff] [blame] | 5178 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 5179 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 5180 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5181 | // Sort out the operands |
| 5182 | // - mapping from glslang -> SPV |
| 5183 | // - there are extra SPV operands with no glslang source |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 5184 | // - compare-exchange swaps the value and comparator |
| 5185 | // - compare-exchange has an extra memory semantics |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5186 | // - EOpAtomicCounterDecrement needs a post decrement |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5187 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 5188 | auto opIt = operands.begin(); // walk the glslang operands |
| 5189 | spvAtomicOperands.push_back(*(opIt++)); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5190 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::ScopeDevice)); // TBD: what is the correct scope? |
| 5191 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); // TBD: what are the correct memory semantics? |
| 5192 | if (opCode == spv::OpAtomicCompareExchange) { |
Rex Xu | bba5c80 | 2015-09-16 13:20:37 +0800 | [diff] [blame] | 5193 | // There are 2 memory semantics for compare-exchange. And the operand order of "comparator" and "new value" in GLSL |
| 5194 | // differs from that in SPIR-V. Hence, special processing is required. |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5195 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 5196 | spvAtomicOperands.push_back(*(opIt + 1)); |
| 5197 | spvAtomicOperands.push_back(*opIt); |
| 5198 | opIt += 2; |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 5199 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5200 | |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 5201 | // Add the rest of the operands, skipping any that were dealt with above. |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5202 | for (; opIt != operands.end(); ++opIt) |
| 5203 | spvAtomicOperands.push_back(*opIt); |
| 5204 | |
John Kessenich | 48d6e79 | 2017-10-06 21:21:48 -0600 | [diff] [blame] | 5205 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 5206 | |
| 5207 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 5208 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 5209 | if (op == glslang::EOpAtomicCounterDecrement) |
| 5210 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 5211 | |
| 5212 | return resultId; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 5213 | } |
| 5214 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5215 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5216 | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5217 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5218 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5219 | bool isFloat = isTypeFloat(typeProxy); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5220 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5221 | spv::Op opCode = spv::OpNop; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5222 | std::vector<spv::Id> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5223 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 5224 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5225 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 5226 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5227 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 5228 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5229 | } else if (op == glslang::EOpAnyInvocation || |
| 5230 | op == glslang::EOpAllInvocations || |
| 5231 | op == glslang::EOpAllInvocationsEqual) { |
| 5232 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 5233 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5234 | } else { |
| 5235 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 5236 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 5237 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 5238 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5239 | op == glslang::EOpAddInvocationsNonUniform || |
| 5240 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 5241 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 5242 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 5243 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 5244 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 5245 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 5246 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 5247 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5248 | |
| 5249 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5250 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5251 | switch (op) { |
| 5252 | case glslang::EOpMinInvocations: |
| 5253 | case glslang::EOpMaxInvocations: |
| 5254 | case glslang::EOpAddInvocations: |
| 5255 | case glslang::EOpMinInvocationsNonUniform: |
| 5256 | case glslang::EOpMaxInvocationsNonUniform: |
| 5257 | case glslang::EOpAddInvocationsNonUniform: |
| 5258 | groupOperation = spv::GroupOperationReduce; |
| 5259 | spvGroupOperands.push_back(groupOperation); |
| 5260 | break; |
| 5261 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5262 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5263 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5264 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5265 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5266 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5267 | groupOperation = spv::GroupOperationInclusiveScan; |
| 5268 | spvGroupOperands.push_back(groupOperation); |
| 5269 | break; |
| 5270 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5271 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5272 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5273 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5274 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5275 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 5276 | groupOperation = spv::GroupOperationExclusiveScan; |
| 5277 | spvGroupOperands.push_back(groupOperation); |
| 5278 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 5279 | default: |
| 5280 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5281 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5282 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) |
| 5286 | spvGroupOperands.push_back(*opIt); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5287 | |
| 5288 | switch (op) { |
| 5289 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5290 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5291 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5292 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5293 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5294 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5295 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 5296 | opCode = spv::OpSubgroupAllEqualKHR; |
| 5297 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5298 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5299 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5300 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5301 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5302 | break; |
| 5303 | case glslang::EOpReadFirstInvocation: |
| 5304 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 5305 | break; |
| 5306 | case glslang::EOpBallot: |
| 5307 | { |
| 5308 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 5309 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 5310 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 5311 | // |
| 5312 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 5313 | // |
| 5314 | spv::Id uintType = builder.makeUintType(32); |
| 5315 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 5316 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 5317 | |
| 5318 | std::vector<spv::Id> components; |
| 5319 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 5320 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 5321 | |
| 5322 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 5323 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 5324 | builder.createCompositeConstruct(uvec2Type, components)); |
| 5325 | } |
| 5326 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5327 | #ifdef AMD_EXTENSIONS |
| 5328 | case glslang::EOpMinInvocations: |
| 5329 | case glslang::EOpMaxInvocations: |
| 5330 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5331 | case glslang::EOpMinInvocationsInclusiveScan: |
| 5332 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 5333 | case glslang::EOpAddInvocationsInclusiveScan: |
| 5334 | case glslang::EOpMinInvocationsExclusiveScan: |
| 5335 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 5336 | case glslang::EOpAddInvocationsExclusiveScan: |
| 5337 | if (op == glslang::EOpMinInvocations || |
| 5338 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 5339 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5340 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5341 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5342 | else { |
| 5343 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5344 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5345 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5346 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5347 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5348 | } else if (op == glslang::EOpMaxInvocations || |
| 5349 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 5350 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5351 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5352 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5353 | else { |
| 5354 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5355 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5356 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5357 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5358 | } |
| 5359 | } else { |
| 5360 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5361 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5362 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5363 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5364 | } |
| 5365 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5366 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5367 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5368 | |
| 5369 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5370 | case glslang::EOpMinInvocationsNonUniform: |
| 5371 | case glslang::EOpMaxInvocationsNonUniform: |
| 5372 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5373 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 5374 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 5375 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 5376 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 5377 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 5378 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 5379 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 5380 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 5381 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5382 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5383 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5384 | else { |
| 5385 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5386 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5387 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5388 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5389 | } |
| 5390 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5391 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 5392 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 5393 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5394 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5395 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5396 | else { |
| 5397 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5398 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5399 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5400 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5401 | } |
| 5402 | } |
| 5403 | else { |
| 5404 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5405 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5406 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5407 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5408 | } |
| 5409 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5410 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5411 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5412 | |
| 5413 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5414 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5415 | default: |
| 5416 | logger->missingFunctionality("invocation operation"); |
| 5417 | return spv::NoResult; |
| 5418 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5419 | |
| 5420 | assert(opCode != spv::OpNop); |
| 5421 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5422 | } |
| 5423 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5424 | // Create group invocation operations on a vector |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5425 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands) |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5426 | { |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5427 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5428 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 5429 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5430 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5431 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5432 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 5433 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 5434 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5435 | #else |
| 5436 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 5437 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5438 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 5439 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5440 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5441 | |
| 5442 | // Handle group invocation operations scalar by scalar. |
| 5443 | // The result type is the same type as the original type. |
| 5444 | // The algorithm is to: |
| 5445 | // - break the vector into scalars |
| 5446 | // - apply the operation to each scalar |
| 5447 | // - make a vector out the scalar results |
| 5448 | |
| 5449 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5450 | int numComponents = builder.getNumComponents(operands[0]); |
| 5451 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5452 | std::vector<spv::Id> results; |
| 5453 | |
| 5454 | // do each scalar op |
| 5455 | for (int comp = 0; comp < numComponents; ++comp) { |
| 5456 | std::vector<unsigned int> indexes; |
| 5457 | indexes.push_back(comp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5458 | spv::Id scalar = builder.createCompositeExtract(operands[0], scalarType, indexes); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5459 | std::vector<spv::Id> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5460 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 5461 | spvGroupOperands.push_back(scalar); |
| 5462 | spvGroupOperands.push_back(operands[1]); |
| 5463 | } else if (op == spv::OpGroupBroadcast) { |
| 5464 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5465 | spvGroupOperands.push_back(scalar); |
| 5466 | spvGroupOperands.push_back(operands[1]); |
| 5467 | } else { |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5468 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5469 | spvGroupOperands.push_back(groupOperation); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5470 | spvGroupOperands.push_back(scalar); |
| 5471 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5472 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5473 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | // put the pieces together |
| 5477 | return builder.createCompositeConstruct(typeId, results); |
| 5478 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5479 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5480 | // Create subgroup invocation operations. |
| 5481 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
| 5482 | { |
| 5483 | // Add the required capabilities. |
| 5484 | switch (op) { |
| 5485 | case glslang::EOpSubgroupElect: |
| 5486 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5487 | break; |
| 5488 | case glslang::EOpSubgroupAll: |
| 5489 | case glslang::EOpSubgroupAny: |
| 5490 | case glslang::EOpSubgroupAllEqual: |
| 5491 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5492 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 5493 | break; |
| 5494 | case glslang::EOpSubgroupBroadcast: |
| 5495 | case glslang::EOpSubgroupBroadcastFirst: |
| 5496 | case glslang::EOpSubgroupBallot: |
| 5497 | case glslang::EOpSubgroupInverseBallot: |
| 5498 | case glslang::EOpSubgroupBallotBitExtract: |
| 5499 | case glslang::EOpSubgroupBallotBitCount: |
| 5500 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5501 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5502 | case glslang::EOpSubgroupBallotFindLSB: |
| 5503 | case glslang::EOpSubgroupBallotFindMSB: |
| 5504 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5505 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 5506 | break; |
| 5507 | case glslang::EOpSubgroupShuffle: |
| 5508 | case glslang::EOpSubgroupShuffleXor: |
| 5509 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5510 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 5511 | break; |
| 5512 | case glslang::EOpSubgroupShuffleUp: |
| 5513 | case glslang::EOpSubgroupShuffleDown: |
| 5514 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5515 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 5516 | break; |
| 5517 | case glslang::EOpSubgroupAdd: |
| 5518 | case glslang::EOpSubgroupMul: |
| 5519 | case glslang::EOpSubgroupMin: |
| 5520 | case glslang::EOpSubgroupMax: |
| 5521 | case glslang::EOpSubgroupAnd: |
| 5522 | case glslang::EOpSubgroupOr: |
| 5523 | case glslang::EOpSubgroupXor: |
| 5524 | case glslang::EOpSubgroupInclusiveAdd: |
| 5525 | case glslang::EOpSubgroupInclusiveMul: |
| 5526 | case glslang::EOpSubgroupInclusiveMin: |
| 5527 | case glslang::EOpSubgroupInclusiveMax: |
| 5528 | case glslang::EOpSubgroupInclusiveAnd: |
| 5529 | case glslang::EOpSubgroupInclusiveOr: |
| 5530 | case glslang::EOpSubgroupInclusiveXor: |
| 5531 | case glslang::EOpSubgroupExclusiveAdd: |
| 5532 | case glslang::EOpSubgroupExclusiveMul: |
| 5533 | case glslang::EOpSubgroupExclusiveMin: |
| 5534 | case glslang::EOpSubgroupExclusiveMax: |
| 5535 | case glslang::EOpSubgroupExclusiveAnd: |
| 5536 | case glslang::EOpSubgroupExclusiveOr: |
| 5537 | case glslang::EOpSubgroupExclusiveXor: |
| 5538 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5539 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 5540 | break; |
| 5541 | case glslang::EOpSubgroupClusteredAdd: |
| 5542 | case glslang::EOpSubgroupClusteredMul: |
| 5543 | case glslang::EOpSubgroupClusteredMin: |
| 5544 | case glslang::EOpSubgroupClusteredMax: |
| 5545 | case glslang::EOpSubgroupClusteredAnd: |
| 5546 | case glslang::EOpSubgroupClusteredOr: |
| 5547 | case glslang::EOpSubgroupClusteredXor: |
| 5548 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5549 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 5550 | break; |
| 5551 | case glslang::EOpSubgroupQuadBroadcast: |
| 5552 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5553 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5554 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 5555 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 5556 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 5557 | break; |
| 5558 | default: assert(0 && "Unhandled subgroup operation!"); |
| 5559 | } |
| 5560 | |
| 5561 | const bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
| 5562 | const bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
| 5563 | const bool isBool = typeProxy == glslang::EbtBool; |
| 5564 | |
| 5565 | spv::Op opCode = spv::OpNop; |
| 5566 | |
| 5567 | // Figure out which opcode to use. |
| 5568 | switch (op) { |
| 5569 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 5570 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 5571 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 5572 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 5573 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 5574 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 5575 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 5576 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 5577 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 5578 | case glslang::EOpSubgroupBallotBitCount: |
| 5579 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5580 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 5581 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 5582 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 5583 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 5584 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 5585 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 5586 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 5587 | case glslang::EOpSubgroupAdd: |
| 5588 | case glslang::EOpSubgroupInclusiveAdd: |
| 5589 | case glslang::EOpSubgroupExclusiveAdd: |
| 5590 | case glslang::EOpSubgroupClusteredAdd: |
| 5591 | if (isFloat) { |
| 5592 | opCode = spv::OpGroupNonUniformFAdd; |
| 5593 | } else { |
| 5594 | opCode = spv::OpGroupNonUniformIAdd; |
| 5595 | } |
| 5596 | break; |
| 5597 | case glslang::EOpSubgroupMul: |
| 5598 | case glslang::EOpSubgroupInclusiveMul: |
| 5599 | case glslang::EOpSubgroupExclusiveMul: |
| 5600 | case glslang::EOpSubgroupClusteredMul: |
| 5601 | if (isFloat) { |
| 5602 | opCode = spv::OpGroupNonUniformFMul; |
| 5603 | } else { |
| 5604 | opCode = spv::OpGroupNonUniformIMul; |
| 5605 | } |
| 5606 | break; |
| 5607 | case glslang::EOpSubgroupMin: |
| 5608 | case glslang::EOpSubgroupInclusiveMin: |
| 5609 | case glslang::EOpSubgroupExclusiveMin: |
| 5610 | case glslang::EOpSubgroupClusteredMin: |
| 5611 | if (isFloat) { |
| 5612 | opCode = spv::OpGroupNonUniformFMin; |
| 5613 | } else if (isUnsigned) { |
| 5614 | opCode = spv::OpGroupNonUniformUMin; |
| 5615 | } else { |
| 5616 | opCode = spv::OpGroupNonUniformSMin; |
| 5617 | } |
| 5618 | break; |
| 5619 | case glslang::EOpSubgroupMax: |
| 5620 | case glslang::EOpSubgroupInclusiveMax: |
| 5621 | case glslang::EOpSubgroupExclusiveMax: |
| 5622 | case glslang::EOpSubgroupClusteredMax: |
| 5623 | if (isFloat) { |
| 5624 | opCode = spv::OpGroupNonUniformFMax; |
| 5625 | } else if (isUnsigned) { |
| 5626 | opCode = spv::OpGroupNonUniformUMax; |
| 5627 | } else { |
| 5628 | opCode = spv::OpGroupNonUniformSMax; |
| 5629 | } |
| 5630 | break; |
| 5631 | case glslang::EOpSubgroupAnd: |
| 5632 | case glslang::EOpSubgroupInclusiveAnd: |
| 5633 | case glslang::EOpSubgroupExclusiveAnd: |
| 5634 | case glslang::EOpSubgroupClusteredAnd: |
| 5635 | if (isBool) { |
| 5636 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 5637 | } else { |
| 5638 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 5639 | } |
| 5640 | break; |
| 5641 | case glslang::EOpSubgroupOr: |
| 5642 | case glslang::EOpSubgroupInclusiveOr: |
| 5643 | case glslang::EOpSubgroupExclusiveOr: |
| 5644 | case glslang::EOpSubgroupClusteredOr: |
| 5645 | if (isBool) { |
| 5646 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 5647 | } else { |
| 5648 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 5649 | } |
| 5650 | break; |
| 5651 | case glslang::EOpSubgroupXor: |
| 5652 | case glslang::EOpSubgroupInclusiveXor: |
| 5653 | case glslang::EOpSubgroupExclusiveXor: |
| 5654 | case glslang::EOpSubgroupClusteredXor: |
| 5655 | if (isBool) { |
| 5656 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 5657 | } else { |
| 5658 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 5659 | } |
| 5660 | break; |
| 5661 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 5662 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 5663 | case glslang::EOpSubgroupQuadSwapVertical: |
| 5664 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 5665 | default: assert(0 && "Unhandled subgroup operation!"); |
| 5666 | } |
| 5667 | |
| 5668 | std::vector<spv::Id> spvGroupOperands; |
| 5669 | |
| 5670 | // Every operation begins with the Execution Scope operand. |
| 5671 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 5672 | |
| 5673 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 5674 | switch (op) { |
| 5675 | default: break; |
| 5676 | case glslang::EOpSubgroupBallotBitCount: |
| 5677 | case glslang::EOpSubgroupAdd: |
| 5678 | case glslang::EOpSubgroupMul: |
| 5679 | case glslang::EOpSubgroupMin: |
| 5680 | case glslang::EOpSubgroupMax: |
| 5681 | case glslang::EOpSubgroupAnd: |
| 5682 | case glslang::EOpSubgroupOr: |
| 5683 | case glslang::EOpSubgroupXor: |
| 5684 | spvGroupOperands.push_back(spv::GroupOperationReduce); |
| 5685 | break; |
| 5686 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 5687 | case glslang::EOpSubgroupInclusiveAdd: |
| 5688 | case glslang::EOpSubgroupInclusiveMul: |
| 5689 | case glslang::EOpSubgroupInclusiveMin: |
| 5690 | case glslang::EOpSubgroupInclusiveMax: |
| 5691 | case glslang::EOpSubgroupInclusiveAnd: |
| 5692 | case glslang::EOpSubgroupInclusiveOr: |
| 5693 | case glslang::EOpSubgroupInclusiveXor: |
| 5694 | spvGroupOperands.push_back(spv::GroupOperationInclusiveScan); |
| 5695 | break; |
| 5696 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 5697 | case glslang::EOpSubgroupExclusiveAdd: |
| 5698 | case glslang::EOpSubgroupExclusiveMul: |
| 5699 | case glslang::EOpSubgroupExclusiveMin: |
| 5700 | case glslang::EOpSubgroupExclusiveMax: |
| 5701 | case glslang::EOpSubgroupExclusiveAnd: |
| 5702 | case glslang::EOpSubgroupExclusiveOr: |
| 5703 | case glslang::EOpSubgroupExclusiveXor: |
| 5704 | spvGroupOperands.push_back(spv::GroupOperationExclusiveScan); |
| 5705 | break; |
| 5706 | case glslang::EOpSubgroupClusteredAdd: |
| 5707 | case glslang::EOpSubgroupClusteredMul: |
| 5708 | case glslang::EOpSubgroupClusteredMin: |
| 5709 | case glslang::EOpSubgroupClusteredMax: |
| 5710 | case glslang::EOpSubgroupClusteredAnd: |
| 5711 | case glslang::EOpSubgroupClusteredOr: |
| 5712 | case glslang::EOpSubgroupClusteredXor: |
| 5713 | spvGroupOperands.push_back(spv::GroupOperationClusteredReduce); |
| 5714 | break; |
| 5715 | } |
| 5716 | |
| 5717 | // Push back the operands next. |
| 5718 | for (auto opIt : operands) { |
| 5719 | spvGroupOperands.push_back(opIt); |
| 5720 | } |
| 5721 | |
| 5722 | // Some opcodes have additional operands. |
| 5723 | switch (op) { |
| 5724 | default: break; |
| 5725 | case glslang::EOpSubgroupQuadSwapHorizontal: spvGroupOperands.push_back(builder.makeUintConstant(0)); break; |
| 5726 | case glslang::EOpSubgroupQuadSwapVertical: spvGroupOperands.push_back(builder.makeUintConstant(1)); break; |
| 5727 | case glslang::EOpSubgroupQuadSwapDiagonal: spvGroupOperands.push_back(builder.makeUintConstant(2)); break; |
| 5728 | } |
| 5729 | |
| 5730 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 5731 | } |
| 5732 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5733 | spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5734 | { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5735 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5736 | bool isFloat = isTypeFloat(typeProxy); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5737 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5738 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5739 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5740 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 5741 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5742 | spv::Id typeId0 = 0; |
| 5743 | if (consumedOperands > 0) |
| 5744 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5745 | spv::Id typeId1 = 0; |
| 5746 | if (consumedOperands > 1) |
| 5747 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5748 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5749 | |
| 5750 | switch (op) { |
| 5751 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5752 | if (isFloat) |
| 5753 | libCall = spv::GLSLstd450FMin; |
| 5754 | else if (isUnsigned) |
| 5755 | libCall = spv::GLSLstd450UMin; |
| 5756 | else |
| 5757 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5758 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5759 | break; |
| 5760 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5761 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5762 | break; |
| 5763 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5764 | if (isFloat) |
| 5765 | libCall = spv::GLSLstd450FMax; |
| 5766 | else if (isUnsigned) |
| 5767 | libCall = spv::GLSLstd450UMax; |
| 5768 | else |
| 5769 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5770 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5771 | break; |
| 5772 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5773 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5774 | break; |
| 5775 | case glslang::EOpDot: |
| 5776 | opCode = spv::OpDot; |
| 5777 | break; |
| 5778 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5779 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5780 | break; |
| 5781 | |
| 5782 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5783 | if (isFloat) |
| 5784 | libCall = spv::GLSLstd450FClamp; |
| 5785 | else if (isUnsigned) |
| 5786 | libCall = spv::GLSLstd450UClamp; |
| 5787 | else |
| 5788 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5789 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 5790 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5791 | break; |
| 5792 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5793 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 5794 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5795 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5796 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5797 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5798 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5799 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5800 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5801 | break; |
| 5802 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5803 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5804 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5805 | break; |
| 5806 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5807 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5808 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 5809 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5810 | break; |
| 5811 | |
| 5812 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5813 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5814 | break; |
| 5815 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5816 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5817 | break; |
| 5818 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5819 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5820 | break; |
| 5821 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5822 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5823 | break; |
| 5824 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5825 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5826 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5827 | case glslang::EOpInterpolateAtSample: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5828 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5829 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 5830 | break; |
| 5831 | case glslang::EOpInterpolateAtOffset: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5832 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5833 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 5834 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5835 | case glslang::EOpAddCarry: |
| 5836 | opCode = spv::OpIAddCarry; |
| 5837 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5838 | consumedOperands = 2; |
| 5839 | break; |
| 5840 | case glslang::EOpSubBorrow: |
| 5841 | opCode = spv::OpISubBorrow; |
| 5842 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5843 | consumedOperands = 2; |
| 5844 | break; |
| 5845 | case glslang::EOpUMulExtended: |
| 5846 | opCode = spv::OpUMulExtended; |
| 5847 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5848 | consumedOperands = 2; |
| 5849 | break; |
| 5850 | case glslang::EOpIMulExtended: |
| 5851 | opCode = spv::OpSMulExtended; |
| 5852 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5853 | consumedOperands = 2; |
| 5854 | break; |
| 5855 | case glslang::EOpBitfieldExtract: |
| 5856 | if (isUnsigned) |
| 5857 | opCode = spv::OpBitFieldUExtract; |
| 5858 | else |
| 5859 | opCode = spv::OpBitFieldSExtract; |
| 5860 | break; |
| 5861 | case glslang::EOpBitfieldInsert: |
| 5862 | opCode = spv::OpBitFieldInsert; |
| 5863 | break; |
| 5864 | |
| 5865 | case glslang::EOpFma: |
| 5866 | libCall = spv::GLSLstd450Fma; |
| 5867 | break; |
| 5868 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5869 | { |
| 5870 | libCall = spv::GLSLstd450FrexpStruct; |
| 5871 | assert(builder.isPointerType(typeId1)); |
| 5872 | typeId1 = builder.getContainedTypeId(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5873 | int width = builder.getScalarTypeWidth(typeId1); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5874 | if (builder.getNumComponents(operands[0]) == 1) |
| 5875 | frexpIntType = builder.makeIntegerType(width, true); |
| 5876 | else |
| 5877 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 5878 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 5879 | consumedOperands = 1; |
| 5880 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5881 | break; |
| 5882 | case glslang::EOpLdexp: |
| 5883 | libCall = spv::GLSLstd450Ldexp; |
| 5884 | break; |
| 5885 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5886 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5887 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5888 | |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 5889 | case glslang::EOpSubgroupBroadcast: |
| 5890 | case glslang::EOpSubgroupBallotBitExtract: |
| 5891 | case glslang::EOpSubgroupShuffle: |
| 5892 | case glslang::EOpSubgroupShuffleXor: |
| 5893 | case glslang::EOpSubgroupShuffleUp: |
| 5894 | case glslang::EOpSubgroupShuffleDown: |
| 5895 | case glslang::EOpSubgroupClusteredAdd: |
| 5896 | case glslang::EOpSubgroupClusteredMul: |
| 5897 | case glslang::EOpSubgroupClusteredMin: |
| 5898 | case glslang::EOpSubgroupClusteredMax: |
| 5899 | case glslang::EOpSubgroupClusteredAnd: |
| 5900 | case glslang::EOpSubgroupClusteredOr: |
| 5901 | case glslang::EOpSubgroupClusteredXor: |
| 5902 | case glslang::EOpSubgroupQuadBroadcast: |
| 5903 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 5904 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5905 | #ifdef AMD_EXTENSIONS |
| 5906 | case glslang::EOpSwizzleInvocations: |
| 5907 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5908 | libCall = spv::SwizzleInvocationsAMD; |
| 5909 | break; |
| 5910 | case glslang::EOpSwizzleInvocationsMasked: |
| 5911 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5912 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 5913 | break; |
| 5914 | case glslang::EOpWriteInvocation: |
| 5915 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5916 | libCall = spv::WriteInvocationAMD; |
| 5917 | break; |
| 5918 | |
| 5919 | case glslang::EOpMin3: |
| 5920 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5921 | if (isFloat) |
| 5922 | libCall = spv::FMin3AMD; |
| 5923 | else { |
| 5924 | if (isUnsigned) |
| 5925 | libCall = spv::UMin3AMD; |
| 5926 | else |
| 5927 | libCall = spv::SMin3AMD; |
| 5928 | } |
| 5929 | break; |
| 5930 | case glslang::EOpMax3: |
| 5931 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5932 | if (isFloat) |
| 5933 | libCall = spv::FMax3AMD; |
| 5934 | else { |
| 5935 | if (isUnsigned) |
| 5936 | libCall = spv::UMax3AMD; |
| 5937 | else |
| 5938 | libCall = spv::SMax3AMD; |
| 5939 | } |
| 5940 | break; |
| 5941 | case glslang::EOpMid3: |
| 5942 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5943 | if (isFloat) |
| 5944 | libCall = spv::FMid3AMD; |
| 5945 | else { |
| 5946 | if (isUnsigned) |
| 5947 | libCall = spv::UMid3AMD; |
| 5948 | else |
| 5949 | libCall = spv::SMid3AMD; |
| 5950 | } |
| 5951 | break; |
| 5952 | |
| 5953 | case glslang::EOpInterpolateAtVertex: |
| 5954 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 5955 | libCall = spv::InterpolateAtVertexAMD; |
| 5956 | break; |
| 5957 | #endif |
| 5958 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5959 | default: |
| 5960 | return 0; |
| 5961 | } |
| 5962 | |
| 5963 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 5964 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 5965 | // Use an extended instruction from the standard library. |
| 5966 | // Construct the call arguments, without modifying the original operands vector. |
| 5967 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 5968 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5969 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 5970 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5971 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5972 | case 0: |
| 5973 | // should all be handled by visitAggregate and createNoArgOperation |
| 5974 | assert(0); |
| 5975 | return 0; |
| 5976 | case 1: |
| 5977 | // should all be handled by createUnaryOperation |
| 5978 | assert(0); |
| 5979 | return 0; |
| 5980 | case 2: |
| 5981 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 5982 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5983 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5984 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 5985 | assert(consumedOperands == operands.size()); |
| 5986 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5987 | break; |
| 5988 | } |
| 5989 | } |
| 5990 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5991 | // Decode the return types that were structures |
| 5992 | switch (op) { |
| 5993 | case glslang::EOpAddCarry: |
| 5994 | case glslang::EOpSubBorrow: |
| 5995 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 5996 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 5997 | break; |
| 5998 | case glslang::EOpUMulExtended: |
| 5999 | case glslang::EOpIMulExtended: |
| 6000 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 6001 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 6002 | break; |
| 6003 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 6004 | { |
| 6005 | assert(operands.size() == 2); |
| 6006 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 6007 | // "exp" is floating-point type (from HLSL intrinsic) |
| 6008 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 6009 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 6010 | builder.createStore(member1, operands[1]); |
| 6011 | } else |
| 6012 | // "exp" is integer type (from GLSL built-in function) |
| 6013 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 6014 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 6015 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6016 | break; |
| 6017 | default: |
| 6018 | break; |
| 6019 | } |
| 6020 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 6021 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6022 | } |
| 6023 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6024 | // Intrinsics with no arguments (or no return value, and no precision). |
| 6025 | 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] | 6026 | { |
| 6027 | // TODO: get the barrier operands correct |
| 6028 | |
| 6029 | switch (op) { |
| 6030 | case glslang::EOpEmitVertex: |
| 6031 | builder.createNoResultOp(spv::OpEmitVertex); |
| 6032 | return 0; |
| 6033 | case glslang::EOpEndPrimitive: |
| 6034 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 6035 | return 0; |
| 6036 | case glslang::EOpBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6037 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 6038 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 6039 | // TODO: prefer the following, when available: |
| 6040 | // builder.createControlBarrier(spv::ScopePatch, spv::ScopePatch, |
| 6041 | // spv::MemorySemanticsPatchMask | |
| 6042 | // spv::MemorySemanticsAcquireReleaseMask); |
| 6043 | } else { |
| 6044 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 6045 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 6046 | spv::MemorySemanticsAcquireReleaseMask); |
| 6047 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6048 | return 0; |
| 6049 | case glslang::EOpMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6050 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAllMemory | |
| 6051 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6052 | return 0; |
| 6053 | case glslang::EOpMemoryBarrierAtomicCounter: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6054 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 6055 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6056 | return 0; |
| 6057 | case glslang::EOpMemoryBarrierBuffer: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6058 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 6059 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6060 | return 0; |
| 6061 | case glslang::EOpMemoryBarrierImage: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6062 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsImageMemoryMask | |
| 6063 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6064 | return 0; |
| 6065 | case glslang::EOpMemoryBarrierShared: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6066 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsWorkgroupMemoryMask | |
| 6067 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6068 | return 0; |
| 6069 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6070 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 6071 | spv::MemorySemanticsAcquireReleaseMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6072 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6073 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6074 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
John Kessenich | 8297936 | 2017-12-11 04:02:24 -0700 | [diff] [blame] | 6075 | spv::MemorySemanticsAllMemory | |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6076 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6077 | return 0; |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6078 | case glslang::EOpDeviceMemoryBarrier: |
| 6079 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 6080 | spv::MemorySemanticsImageMemoryMask | |
| 6081 | spv::MemorySemanticsAcquireReleaseMask); |
| 6082 | return 0; |
| 6083 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 6084 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 6085 | spv::MemorySemanticsImageMemoryMask | |
| 6086 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6087 | return 0; |
| 6088 | case glslang::EOpWorkgroupMemoryBarrier: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6089 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 6090 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6091 | return 0; |
| 6092 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 838d7af | 2017-12-12 22:50:53 -0700 | [diff] [blame] | 6093 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 6094 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 6095 | spv::MemorySemanticsAcquireReleaseMask); |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 6096 | return 0; |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6097 | case glslang::EOpSubgroupBarrier: |
| 6098 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 6099 | spv::MemorySemanticsAcquireReleaseMask); |
| 6100 | return spv::NoResult; |
| 6101 | case glslang::EOpSubgroupMemoryBarrier: |
| 6102 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 6103 | spv::MemorySemanticsAcquireReleaseMask); |
| 6104 | return spv::NoResult; |
| 6105 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 6106 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 6107 | spv::MemorySemanticsAcquireReleaseMask); |
| 6108 | return spv::NoResult; |
| 6109 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 6110 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 6111 | spv::MemorySemanticsAcquireReleaseMask); |
| 6112 | return spv::NoResult; |
| 6113 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 6114 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 6115 | spv::MemorySemanticsAcquireReleaseMask); |
| 6116 | return spv::NoResult; |
| 6117 | case glslang::EOpSubgroupElect: { |
| 6118 | std::vector<spv::Id> operands; |
| 6119 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 6120 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6121 | #ifdef AMD_EXTENSIONS |
| 6122 | case glslang::EOpTime: |
| 6123 | { |
| 6124 | std::vector<spv::Id> args; // Dummy arguments |
| 6125 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 6126 | return builder.setPrecision(id, precision); |
| 6127 | } |
| 6128 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6129 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 6130 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6131 | return 0; |
| 6132 | } |
| 6133 | } |
| 6134 | |
| 6135 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 6136 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 6137 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6138 | spv::Id id; |
| 6139 | if (symbolValues.end() != iter) { |
| 6140 | id = iter->second; |
| 6141 | return id; |
| 6142 | } |
| 6143 | |
| 6144 | // it was not found, create it |
| 6145 | id = createSpvVariable(symbol); |
| 6146 | symbolValues[symbol->getId()] = id; |
| 6147 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 6148 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6149 | addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 6150 | addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 6151 | addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6152 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
| 6153 | addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6154 | if (symbol->getQualifier().hasIndex()) |
| 6155 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 6156 | if (symbol->getQualifier().hasComponent()) |
| 6157 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 6158 | // atomic counters use this: |
| 6159 | if (symbol->getQualifier().hasOffset()) |
| 6160 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6161 | } |
| 6162 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 6163 | if (symbol->getQualifier().hasLocation()) |
| 6164 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 6165 | addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 6166 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6167 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6168 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6169 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6170 | if (symbol->getQualifier().hasSet()) |
| 6171 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6172 | else if (IsDescriptorResource(symbol->getType())) { |
| 6173 | // default to 0 |
| 6174 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 6175 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6176 | if (symbol->getQualifier().hasBinding()) |
| 6177 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6178 | if (symbol->getQualifier().hasAttachment()) |
| 6179 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6180 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6181 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6182 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 6183 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 6184 | if (symbol->getQualifier().hasXfbBuffer()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6185 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
John Kessenich | edaf556 | 2017-12-15 06:21:46 -0700 | [diff] [blame] | 6186 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 6187 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 6188 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 6189 | } |
| 6190 | if (symbol->getQualifier().hasXfbOffset()) |
| 6191 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6192 | } |
| 6193 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 6194 | if (symbol->getType().isImage()) { |
| 6195 | std::vector<spv::Decoration> memory; |
| 6196 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory); |
| 6197 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 6198 | addDecoration(id, memory[i]); |
| 6199 | } |
| 6200 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6201 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 6202 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 6203 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6204 | addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6205 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 6206 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 6207 | if (builtIn == spv::BuiltInSampleMask) { |
| 6208 | spv::Decoration decoration; |
| 6209 | // GL_NV_sample_mask_override_coverage extension |
| 6210 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6211 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 6212 | else |
| 6213 | decoration = (spv::Decoration)spv::DecorationMax; |
| 6214 | addDecoration(id, decoration); |
| 6215 | if (decoration != spv::DecorationMax) { |
| 6216 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 6217 | } |
| 6218 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6219 | else if (builtIn == spv::BuiltInLayer) { |
| 6220 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 6221 | if (symbol->getQualifier().layoutViewportRelative) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6222 | addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 6223 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 6224 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 6225 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 6226 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6227 | addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
| 6228 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 6229 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 6230 | } |
| 6231 | } |
| 6232 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 6233 | if (symbol->getQualifier().layoutPassthrough) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 6234 | addDecoration(id, spv::DecorationPassthroughNV); |
| 6235 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 6236 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 6237 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 6238 | #endif |
| 6239 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6240 | return id; |
| 6241 | } |
| 6242 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6243 | // If 'dec' is valid, add no-operand decoration to an object |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6244 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec) |
| 6245 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 6246 | if (dec != spv::DecorationMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6247 | builder.addDecoration(id, dec); |
| 6248 | } |
| 6249 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6250 | // If 'dec' is valid, add a one-operand decoration to an object |
| 6251 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec, unsigned value) |
| 6252 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 6253 | if (dec != spv::DecorationMax) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6254 | builder.addDecoration(id, dec, value); |
| 6255 | } |
| 6256 | |
| 6257 | // If 'dec' is valid, add a no-operand decoration to a struct member |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6258 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec) |
| 6259 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 6260 | if (dec != spv::DecorationMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6261 | builder.addMemberDecoration(id, (unsigned)member, dec); |
| 6262 | } |
| 6263 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6264 | // If 'dec' is valid, add a one-operand decoration to a struct member |
| 6265 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value) |
| 6266 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 6267 | if (dec != spv::DecorationMax) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 6268 | builder.addMemberDecoration(id, (unsigned)member, dec, value); |
| 6269 | } |
| 6270 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6271 | // 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] | 6272 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6273 | // |
| 6274 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 6275 | // |
| 6276 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 6277 | // regular constants, which themselves are trees that createSpvConstant() |
| 6278 | // recursively walks. So, this function walks the "top" of the tree: |
| 6279 | // - emit specialization constant-building instructions for specConstant |
| 6280 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6281 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6282 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 6283 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6284 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6285 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6286 | if (! node.getQualifier().specConstant) { |
| 6287 | // hand off to the non-spec-constant path |
| 6288 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 6289 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6290 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6291 | nextConst, false); |
| 6292 | } |
| 6293 | |
| 6294 | // We now know we have a specialization constant to build |
| 6295 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 6296 | // 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] | 6297 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 6298 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 6299 | std::vector<spv::Id> dimConstId; |
| 6300 | for (int dim = 0; dim < 3; ++dim) { |
| 6301 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 6302 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 6303 | if (specConst) |
| 6304 | addDecoration(dimConstId.back(), spv::DecorationSpecId, glslangIntermediate->getLocalSizeSpecId(dim)); |
| 6305 | } |
| 6306 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 6307 | } |
| 6308 | |
| 6309 | // An AST node labelled as specialization constant should be a symbol node. |
| 6310 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 6311 | if (auto* sn = node.getAsSymbolNode()) { |
| 6312 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 6313 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 6314 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 6315 | // will set the builder into spec constant op instruction generating mode. |
| 6316 | sub_tree->traverse(this); |
| 6317 | return accessChainLoad(sub_tree->getType()); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6318 | } else if (auto* const_union_array = &sn->getConstArray()){ |
| 6319 | int nextConst = 0; |
Endre Oma | ad58d45 | 2017-01-31 21:08:19 +0100 | [diff] [blame] | 6320 | spv::Id id = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
| 6321 | builder.addName(id, sn->getName().c_str()); |
| 6322 | return id; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 6323 | } |
| 6324 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6325 | |
| 6326 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 6327 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 6328 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 6329 | exit(1); |
| 6330 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6331 | } |
| 6332 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6333 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 6334 | // build the aggregate SPIR-V constant. |
| 6335 | // |
| 6336 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 6337 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 6338 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6339 | spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6340 | { |
| 6341 | // vector of constants for SPIR-V |
| 6342 | std::vector<spv::Id> spvConsts; |
| 6343 | |
| 6344 | // Type is used for struct and array constants |
| 6345 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 6346 | |
| 6347 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 6348 | glslang::TType elementType(glslangType, 0); |
| 6349 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6350 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6351 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 6352 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6353 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6354 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6355 | } else if (glslangType.getStruct()) { |
| 6356 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 6357 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 6358 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 6359 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6360 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 6361 | bool zero = nextConst >= consts.size(); |
| 6362 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6363 | case glslang::EbtInt8: |
| 6364 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 6365 | break; |
| 6366 | case glslang::EbtUint8: |
| 6367 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 6368 | break; |
| 6369 | case glslang::EbtInt16: |
| 6370 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 6371 | break; |
| 6372 | case glslang::EbtUint16: |
| 6373 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 6374 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6375 | case glslang::EbtInt: |
| 6376 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 6377 | break; |
| 6378 | case glslang::EbtUint: |
| 6379 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 6380 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6381 | case glslang::EbtInt64: |
| 6382 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 6383 | break; |
| 6384 | case glslang::EbtUint64: |
| 6385 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 6386 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6387 | case glslang::EbtFloat: |
| 6388 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 6389 | break; |
| 6390 | case glslang::EbtDouble: |
| 6391 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 6392 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6393 | case glslang::EbtFloat16: |
| 6394 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 6395 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6396 | case glslang::EbtBool: |
| 6397 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 6398 | break; |
| 6399 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6400 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6401 | break; |
| 6402 | } |
| 6403 | ++nextConst; |
| 6404 | } |
| 6405 | } else { |
| 6406 | // we have a non-aggregate (scalar) constant |
| 6407 | bool zero = nextConst >= consts.size(); |
| 6408 | spv::Id scalar = 0; |
| 6409 | switch (glslangType.getBasicType()) { |
John Kessenich | 66011cb | 2018-03-06 16:12:04 -0700 | [diff] [blame] | 6410 | case glslang::EbtInt8: |
| 6411 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 6412 | break; |
| 6413 | case glslang::EbtUint8: |
| 6414 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 6415 | break; |
| 6416 | case glslang::EbtInt16: |
| 6417 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 6418 | break; |
| 6419 | case glslang::EbtUint16: |
| 6420 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 6421 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6422 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6423 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6424 | break; |
| 6425 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6426 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6427 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 6428 | case glslang::EbtInt64: |
| 6429 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 6430 | break; |
| 6431 | case glslang::EbtUint64: |
| 6432 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 6433 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6434 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6435 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6436 | break; |
| 6437 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6438 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6439 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 6440 | case glslang::EbtFloat16: |
| 6441 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 6442 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6443 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6444 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6445 | break; |
| 6446 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6447 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6448 | break; |
| 6449 | } |
| 6450 | ++nextConst; |
| 6451 | return scalar; |
| 6452 | } |
| 6453 | |
| 6454 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 6455 | } |
| 6456 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 6457 | // Return true if the node is a constant or symbol whose reading has no |
| 6458 | // non-trivial observable cost or effect. |
| 6459 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 6460 | { |
| 6461 | // don't know what this is |
| 6462 | if (node == nullptr) |
| 6463 | return false; |
| 6464 | |
| 6465 | // a constant is safe |
| 6466 | if (node->getAsConstantUnion() != nullptr) |
| 6467 | return true; |
| 6468 | |
| 6469 | // not a symbol means non-trivial |
| 6470 | if (node->getAsSymbolNode() == nullptr) |
| 6471 | return false; |
| 6472 | |
| 6473 | // a symbol, depends on what's being read |
| 6474 | switch (node->getType().getQualifier().storage) { |
| 6475 | case glslang::EvqTemporary: |
| 6476 | case glslang::EvqGlobal: |
| 6477 | case glslang::EvqIn: |
| 6478 | case glslang::EvqInOut: |
| 6479 | case glslang::EvqConst: |
| 6480 | case glslang::EvqConstReadOnly: |
| 6481 | case glslang::EvqUniform: |
| 6482 | return true; |
| 6483 | default: |
| 6484 | return false; |
| 6485 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 6486 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 6487 | |
| 6488 | // 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] | 6489 | // 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] | 6490 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 6491 | // Return true if trivial. |
| 6492 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 6493 | { |
| 6494 | if (node == nullptr) |
| 6495 | return false; |
| 6496 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 6497 | // count non scalars as trivial, as well as anything coming from HLSL |
| 6498 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 6499 | return true; |
| 6500 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 6501 | // symbols and constants are trivial |
| 6502 | if (isTrivialLeaf(node)) |
| 6503 | return true; |
| 6504 | |
| 6505 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 6506 | |
| 6507 | // not a simple operation |
| 6508 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 6509 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 6510 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 6511 | return false; |
| 6512 | |
| 6513 | // not on leaf nodes |
| 6514 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 6515 | return false; |
| 6516 | |
| 6517 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 6518 | return false; |
| 6519 | } |
| 6520 | |
| 6521 | switch (node->getAsOperator()->getOp()) { |
| 6522 | case glslang::EOpLogicalNot: |
| 6523 | case glslang::EOpConvIntToBool: |
| 6524 | case glslang::EOpConvUintToBool: |
| 6525 | case glslang::EOpConvFloatToBool: |
| 6526 | case glslang::EOpConvDoubleToBool: |
| 6527 | case glslang::EOpEqual: |
| 6528 | case glslang::EOpNotEqual: |
| 6529 | case glslang::EOpLessThan: |
| 6530 | case glslang::EOpGreaterThan: |
| 6531 | case glslang::EOpLessThanEqual: |
| 6532 | case glslang::EOpGreaterThanEqual: |
| 6533 | case glslang::EOpIndexDirect: |
| 6534 | case glslang::EOpIndexDirectStruct: |
| 6535 | case glslang::EOpLogicalXor: |
| 6536 | case glslang::EOpAny: |
| 6537 | case glslang::EOpAll: |
| 6538 | return true; |
| 6539 | default: |
| 6540 | return false; |
| 6541 | } |
| 6542 | } |
| 6543 | |
| 6544 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 6545 | // the left side is true (for &&) or false (for ||). |
| 6546 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 6547 | { |
| 6548 | spv::Id boolTypeId = builder.makeBoolType(); |
| 6549 | |
| 6550 | // emit left operand |
| 6551 | builder.clearAccessChain(); |
| 6552 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 6553 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 6554 | |
| 6555 | // Operands to accumulate OpPhi operands |
| 6556 | std::vector<spv::Id> phiOperands; |
| 6557 | // accumulate left operand's phi information |
| 6558 | phiOperands.push_back(leftId); |
| 6559 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 6560 | |
| 6561 | // Make the two kinds of operation symmetric with a "!" |
| 6562 | // || => emit "if (! left) result = right" |
| 6563 | // && => emit "if ( left) result = right" |
| 6564 | // |
| 6565 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 6566 | // to 'builder' to have an "else" without an "then" |
| 6567 | if (op == glslang::EOpLogicalOr) |
| 6568 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 6569 | |
| 6570 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 6571 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 6572 | |
| 6573 | // emit right operand as the "then" part of the "if" |
| 6574 | builder.clearAccessChain(); |
| 6575 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 6576 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 6577 | |
| 6578 | // accumulate left operand's phi information |
| 6579 | phiOperands.push_back(rightId); |
| 6580 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 6581 | |
| 6582 | // finish the "if" |
| 6583 | ifBuilder.makeEndIf(); |
| 6584 | |
| 6585 | // phi together the two results |
| 6586 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 6587 | } |
| 6588 | |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 6589 | #ifdef AMD_EXTENSIONS |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6590 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 6591 | // Import this set if it has not been imported yet. |
| 6592 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 6593 | { |
| 6594 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 6595 | return extBuiltinMap[name]; |
| 6596 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 6597 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6598 | spv::Id extBuiltins = builder.import(name); |
| 6599 | extBuiltinMap[name] = extBuiltins; |
| 6600 | return extBuiltins; |
| 6601 | } |
| 6602 | } |
Frank Henigman | 541f7bb | 2018-01-16 00:18:26 -0500 | [diff] [blame] | 6603 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 6604 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6605 | }; // end anonymous namespace |
| 6606 | |
| 6607 | namespace glslang { |
| 6608 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 6609 | void GetSpirvVersion(std::string& version) |
| 6610 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 6611 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 6612 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 6613 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 6614 | version = buf; |
| 6615 | } |
| 6616 | |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 6617 | // For low-order part of the generator's magic number. Bump up |
| 6618 | // when there is a change in the style (e.g., if SSA form changes, |
| 6619 | // or a different instruction sequence to do something gets used). |
| 6620 | int GetSpirvGeneratorVersion() |
| 6621 | { |
John Kessenich | 3f0d4bc | 2017-12-16 23:46:37 -0700 | [diff] [blame] | 6622 | // return 1; // start |
| 6623 | // 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] | 6624 | // 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] | 6625 | // 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] | 6626 | // return 5; // make OpArrayLength result type be an int with signedness of 0 |
| 6627 | return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 6628 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
John Kessenich | a372a3e | 2017-11-02 22:32:14 -0600 | [diff] [blame] | 6629 | } |
| 6630 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6631 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 6632 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6633 | { |
| 6634 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 6635 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 6636 | if (out.fail()) |
| 6637 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6638 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 6639 | unsigned int word = spirv[i]; |
| 6640 | out.write((const char*)&word, 4); |
| 6641 | } |
| 6642 | out.close(); |
| 6643 | } |
| 6644 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 6645 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 6646 | 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] | 6647 | { |
| 6648 | std::ofstream out; |
| 6649 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 6650 | if (out.fail()) |
| 6651 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | c6c80a6 | 2018-03-05 22:23:17 -0700 | [diff] [blame] | 6652 | out << "\t// " << |
| 6653 | glslang::GetSpirvGeneratorVersion() << "." << GLSLANG_MINOR_VERSION << "." << GLSLANG_PATCH_LEVEL << |
| 6654 | std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 6655 | if (varName != nullptr) { |
| 6656 | out << "\t #pragma once" << std::endl; |
| 6657 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 6658 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 6659 | const int WORDS_PER_LINE = 8; |
| 6660 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 6661 | out << "\t"; |
| 6662 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 6663 | const unsigned int word = spirv[i + j]; |
| 6664 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 6665 | if (i + j + 1 < (int)spirv.size()) { |
| 6666 | out << ","; |
| 6667 | } |
| 6668 | } |
| 6669 | out << std::endl; |
| 6670 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 6671 | if (varName != nullptr) { |
| 6672 | out << "};"; |
| 6673 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 6674 | out.close(); |
| 6675 | } |
| 6676 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 6677 | #ifdef ENABLE_OPT |
| 6678 | void errHandler(const std::string& str) { |
| 6679 | std::cerr << str << std::endl; |
| 6680 | } |
| 6681 | #endif |
| 6682 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6683 | // |
| 6684 | // Set up the glslang traversal |
| 6685 | // |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 6686 | void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6687 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 6688 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 6689 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 6690 | } |
| 6691 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 6692 | void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
| 6693 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 6694 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6695 | TIntermNode* root = intermediate.getTreeRoot(); |
| 6696 | |
| 6697 | if (root == 0) |
| 6698 | return; |
| 6699 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 6700 | glslang::SpvOptions defaultOptions; |
| 6701 | if (options == nullptr) |
| 6702 | options = &defaultOptions; |
| 6703 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6704 | glslang::GetThreadPoolAllocator().push(); |
| 6705 | |
John Kessenich | 2b5ea9f | 2018-01-31 18:35:56 -0700 | [diff] [blame] | 6706 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6707 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 6708 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6709 | it.dumpSpv(spirv); |
| 6710 | |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 6711 | #ifdef ENABLE_OPT |
| 6712 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 6713 | // eg. forward and remove memory writes of opaque types. |
| 6714 | if ((intermediate.getSource() == EShSourceHlsl || |
| 6715 | options->optimizeSize) && |
| 6716 | !options->disableOptimizer) { |
| 6717 | spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2; |
| 6718 | |
| 6719 | spvtools::Optimizer optimizer(target_env); |
| 6720 | optimizer.SetMessageConsumer([](spv_message_level_t level, |
| 6721 | const char* source, |
| 6722 | const spv_position_t& position, |
| 6723 | const char* message) { |
| 6724 | std::cerr << StringifyMessage(level, source, position, message) |
| 6725 | << std::endl; |
| 6726 | }); |
| 6727 | |
| 6728 | optimizer.RegisterPass(CreateInlineExhaustivePass()); |
GregF | e063928 | 2017-12-21 10:55:57 -0700 | [diff] [blame] | 6729 | optimizer.RegisterPass(CreateEliminateDeadFunctionsPass()); |
| 6730 | optimizer.RegisterPass(CreateScalarReplacementPass()); |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 6731 | optimizer.RegisterPass(CreateLocalAccessChainConvertPass()); |
| 6732 | optimizer.RegisterPass(CreateLocalSingleBlockLoadStoreElimPass()); |
| 6733 | optimizer.RegisterPass(CreateLocalSingleStoreElimPass()); |
| 6734 | optimizer.RegisterPass(CreateInsertExtractElimPass()); |
GregF | 8a4848f | 2018-02-07 16:04:42 -0700 | [diff] [blame] | 6735 | optimizer.RegisterPass(CreateDeadInsertElimPass()); |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 6736 | optimizer.RegisterPass(CreateAggressiveDCEPass()); |
| 6737 | optimizer.RegisterPass(CreateDeadBranchElimPass()); |
GregF | cc80d80 | 2017-10-23 16:48:42 -0600 | [diff] [blame] | 6738 | optimizer.RegisterPass(CreateCFGCleanupPass()); |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 6739 | optimizer.RegisterPass(CreateBlockMergePass()); |
| 6740 | optimizer.RegisterPass(CreateLocalMultiStoreElimPass()); |
| 6741 | optimizer.RegisterPass(CreateInsertExtractElimPass()); |
GregF | 8a4848f | 2018-02-07 16:04:42 -0700 | [diff] [blame] | 6742 | optimizer.RegisterPass(CreateDeadInsertElimPass()); |
| 6743 | if (options->optimizeSize) { |
| 6744 | optimizer.RegisterPass(CreateRedundancyEliminationPass()); |
| 6745 | // TODO(greg-lunarg): Add this when AMD driver issues are resolved |
| 6746 | // optimizer.RegisterPass(CreateCommonUniformElimPass()); |
| 6747 | } |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 6748 | optimizer.RegisterPass(CreateAggressiveDCEPass()); |
GregF | cd1f169 | 2017-09-21 18:40:22 -0600 | [diff] [blame] | 6749 | |
| 6750 | if (!optimizer.Run(spirv.data(), spirv.size(), &spirv)) |
| 6751 | return; |
| 6752 | |
| 6753 | // Remove dead module-level objects: functions, types, vars |
| 6754 | // TODO(greg-lunarg): Switch to spirv-opt versions when available |
| 6755 | spv::spirvbin_t Remapper(0); |
| 6756 | Remapper.registerErrorHandler(errHandler); |
| 6757 | Remapper.remap(spirv, spv::spirvbin_t::DCE_ALL); |
| 6758 | } |
| 6759 | #endif |
| 6760 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6761 | glslang::GetThreadPoolAllocator().pop(); |
| 6762 | } |
| 6763 | |
| 6764 | }; // end namespace glslang |