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 | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 5 | // All rights reserved. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 6 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 7 | // Redistribution and use in source and binary forms, with or without |
| 8 | // modification, are permitted provided that the following conditions |
| 9 | // are met: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 10 | // |
| 11 | // Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // |
| 14 | // Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following |
| 16 | // disclaimer in the documentation and/or other materials provided |
| 17 | // with the distribution. |
| 18 | // |
| 19 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 20 | // contributors may be used to endorse or promote products derived |
| 21 | // from this software without specific prior written permission. |
| 22 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 29 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 33 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | // POSSIBILITY OF SUCH DAMAGE. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 35 | |
| 36 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 37 | // Visit the nodes in the glslang intermediate tree representation to |
| 38 | // translate them to SPIR-V. |
| 39 | // |
| 40 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 41 | #include "spirv.hpp" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 42 | #include "GlslangToSpv.h" |
| 43 | #include "SpvBuilder.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 44 | namespace spv { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 45 | #include "GLSL.std.450.h" |
| 46 | #include "GLSL.ext.KHR.h" |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 47 | #ifdef AMD_EXTENSIONS |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 48 | #include "GLSL.ext.AMD.h" |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 49 | #endif |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 50 | #ifdef NV_EXTENSIONS |
| 51 | #include "GLSL.ext.NV.h" |
| 52 | #endif |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 53 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 54 | |
| 55 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 56 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 57 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 58 | #include "../glslang/Include/Common.h" |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 59 | #include "../glslang/Include/revision.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 60 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 61 | #include <fstream> |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 62 | #include <iomanip> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 63 | #include <list> |
| 64 | #include <map> |
| 65 | #include <stack> |
| 66 | #include <string> |
| 67 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 68 | |
| 69 | namespace { |
| 70 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 71 | // For low-order part of the generator's magic number. Bump up |
| 72 | // when there is a change in the style (e.g., if SSA form changes, |
| 73 | // or a different instruction sequence to do something gets used). |
| 74 | const int GeneratorVersion = 1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 75 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 76 | namespace { |
| 77 | class SpecConstantOpModeGuard { |
| 78 | public: |
| 79 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 80 | : builder_(builder) { |
| 81 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 82 | } |
| 83 | ~SpecConstantOpModeGuard() { |
| 84 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 85 | : builder_->setToNormalCodeGenMode(); |
| 86 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 87 | void turnOnSpecConstantOpMode() { |
| 88 | builder_->setToSpecConstCodeGenMode(); |
| 89 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | spv::Builder* builder_; |
| 93 | bool previous_flag_; |
| 94 | }; |
| 95 | } |
| 96 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 97 | // |
| 98 | // The main holder of information for translating glslang to SPIR-V. |
| 99 | // |
| 100 | // Derives from the AST walking base class. |
| 101 | // |
| 102 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 103 | public: |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 104 | TGlslangToSpvTraverser(const glslang::TIntermediate*, spv::SpvBuildLogger* logger, glslang::SpvOptions& options); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 105 | virtual ~TGlslangToSpvTraverser() { } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 106 | |
| 107 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 108 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 109 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 110 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 111 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 112 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 113 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 114 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 115 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 116 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 117 | void finishSpv(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 118 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 119 | |
| 120 | protected: |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 121 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 122 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 123 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 124 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 125 | spv::SelectionControlMask TranslateSelectionControl(glslang::TSelectionControl) const; |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 126 | spv::LoopControlMask TranslateLoopControl(glslang::TLoopControl) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 127 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 128 | spv::Id createSpvVariable(const glslang::TIntermSymbol*); |
| 129 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 130 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 131 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 132 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 133 | spv::Id convertGlslangToSpvType(const glslang::TType& type); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 134 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 135 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 136 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 137 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 138 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 139 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 140 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 141 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 142 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 143 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 144 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 145 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 146 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 147 | 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] | 148 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 149 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 150 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 151 | void makeFunctions(const glslang::TIntermSequence&); |
| 152 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 153 | void visitFunctions(const glslang::TIntermSequence&); |
| 154 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 155 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 156 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 157 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 158 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 159 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 160 | 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); |
| 161 | spv::Id createBinaryMatrixOperation(spv::Op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right); |
| 162 | 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] | 163 | 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] | 164 | spv::Id createConversion(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id destTypeId, spv::Id operand, glslang::TBasicType typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 165 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 166 | 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] | 167 | 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] | 168 | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, spv::Id typeId, std::vector<spv::Id>& operands); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 169 | 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] | 170 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 171 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
| 172 | void addDecoration(spv::Id id, spv::Decoration dec); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 173 | void addDecoration(spv::Id id, spv::Decoration dec, unsigned value); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 174 | void addMemberDecoration(spv::Id id, int member, spv::Decoration dec); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 175 | void addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 176 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 177 | 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] | 178 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 179 | bool isTrivial(const glslang::TIntermTyped* node); |
| 180 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 181 | spv::Id getExtBuiltins(const char* name); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 182 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 183 | glslang::SpvOptions& options; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 184 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 185 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 186 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 187 | int sequenceDepth; |
| 188 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 189 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 190 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 191 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 192 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 193 | bool inEntryPoint; |
| 194 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 195 | 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] | 196 | 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] | 197 | const glslang::TIntermediate* glslangIntermediate; |
| 198 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 199 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 200 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 201 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 202 | 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] | 203 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 204 | 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] | 205 | 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] | 206 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | // |
| 210 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 211 | // |
| 212 | |
| 213 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 214 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 215 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 216 | switch (source) { |
| 217 | case glslang::EShSourceGlsl: |
| 218 | switch (profile) { |
| 219 | case ENoProfile: |
| 220 | case ECoreProfile: |
| 221 | case ECompatibilityProfile: |
| 222 | return spv::SourceLanguageGLSL; |
| 223 | case EEsProfile: |
| 224 | return spv::SourceLanguageESSL; |
| 225 | default: |
| 226 | return spv::SourceLanguageUnknown; |
| 227 | } |
| 228 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 229 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 230 | default: |
| 231 | return spv::SourceLanguageUnknown; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Translate glslang language (stage) to SPIR-V execution model. |
| 236 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 237 | { |
| 238 | switch (stage) { |
| 239 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 240 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 241 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 242 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 243 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 244 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
| 245 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 246 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 247 | return spv::ExecutionModelFragment; |
| 248 | } |
| 249 | } |
| 250 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 251 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 252 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 253 | { |
| 254 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 255 | case glslang::Esd1D: return spv::Dim1D; |
| 256 | case glslang::Esd2D: return spv::Dim2D; |
| 257 | case glslang::Esd3D: return spv::Dim3D; |
| 258 | case glslang::EsdCube: return spv::DimCube; |
| 259 | case glslang::EsdRect: return spv::DimRect; |
| 260 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 261 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 262 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 263 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 264 | return spv::Dim2D; |
| 265 | } |
| 266 | } |
| 267 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 268 | // Translate glslang precision to SPIR-V precision decorations. |
| 269 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 270 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 271 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 272 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 273 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 274 | default: |
| 275 | return spv::NoPrecision; |
| 276 | } |
| 277 | } |
| 278 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 279 | // Translate glslang type to SPIR-V precision decorations. |
| 280 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 281 | { |
| 282 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 283 | } |
| 284 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 285 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 286 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 287 | { |
| 288 | if (type.getBasicType() == glslang::EbtBlock) { |
| 289 | switch (type.getQualifier().storage) { |
| 290 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 291 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 292 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 293 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
| 294 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 295 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 300 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 301 | } |
| 302 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 303 | // Translate glslang type to SPIR-V memory decorations. |
| 304 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory) |
| 305 | { |
| 306 | if (qualifier.coherent) |
| 307 | memory.push_back(spv::DecorationCoherent); |
| 308 | if (qualifier.volatil) |
| 309 | memory.push_back(spv::DecorationVolatile); |
| 310 | if (qualifier.restrict) |
| 311 | memory.push_back(spv::DecorationRestrict); |
| 312 | if (qualifier.readonly) |
| 313 | memory.push_back(spv::DecorationNonWritable); |
| 314 | if (qualifier.writeonly) |
| 315 | memory.push_back(spv::DecorationNonReadable); |
| 316 | } |
| 317 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 318 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 319 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 320 | { |
| 321 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 322 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 323 | case glslang::ElmRowMajor: |
| 324 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 325 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 326 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 327 | default: |
| 328 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 329 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 330 | } |
| 331 | } else { |
| 332 | switch (type.getBasicType()) { |
| 333 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 334 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 335 | break; |
| 336 | case glslang::EbtBlock: |
| 337 | switch (type.getQualifier().storage) { |
| 338 | case glslang::EvqUniform: |
| 339 | case glslang::EvqBuffer: |
| 340 | switch (type.getQualifier().layoutPacking) { |
| 341 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 342 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 343 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 344 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 345 | } |
| 346 | case glslang::EvqVaryingIn: |
| 347 | case glslang::EvqVaryingOut: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 348 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
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 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 351 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 352 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 359 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 360 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 361 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 362 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 363 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 364 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 365 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 366 | else if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 367 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 368 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 369 | return spv::DecorationFlat; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 370 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 371 | else if (qualifier.explicitInterp) { |
| 372 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 373 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 374 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 375 | #endif |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 376 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 377 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 381 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 382 | // should be applied. |
| 383 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 384 | { |
| 385 | if (qualifier.patch) |
| 386 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 387 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 388 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 389 | else if (qualifier.sample) { |
| 390 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 391 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 392 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 393 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 394 | } |
| 395 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 396 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 397 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 398 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 399 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 400 | return spv::DecorationInvariant; |
| 401 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 402 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 403 | } |
| 404 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 405 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 406 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 407 | { |
| 408 | if (qualifier.noContraction) |
| 409 | return spv::DecorationNoContraction; |
| 410 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 411 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 412 | } |
| 413 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 414 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 415 | // associated capabilities when required. For some built-in variables, a capability |
| 416 | // is generated only when using the variable in an executable instruction, but not when |
| 417 | // just declaring a struct member variable with it. This is true for PointSize, |
| 418 | // ClipDistance, and CullDistance. |
| 419 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 420 | { |
| 421 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 422 | case glslang::EbvPointSize: |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 423 | // Defer adding the capability until the built-in is actually used. |
| 424 | if (! memberDeclaration) { |
| 425 | switch (glslangIntermediate->getStage()) { |
| 426 | case EShLangGeometry: |
| 427 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 428 | break; |
| 429 | case EShLangTessControl: |
| 430 | case EShLangTessEvaluation: |
| 431 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 432 | break; |
| 433 | default: |
| 434 | break; |
| 435 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 436 | } |
| 437 | return spv::BuiltInPointSize; |
| 438 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 439 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 440 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 441 | // They are now generated when used, rather than here when declared. |
| 442 | // Potentially, the specification should be more clear what the minimum |
| 443 | // use needed is to trigger the capability. |
| 444 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 445 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 446 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 447 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 448 | return spv::BuiltInClipDistance; |
| 449 | |
| 450 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 451 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 452 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 453 | return spv::BuiltInCullDistance; |
| 454 | |
| 455 | case glslang::EbvViewportIndex: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 456 | if (!memberDeclaration) { |
| 457 | builder.addCapability(spv::CapabilityMultiViewport); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 458 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 459 | glslangIntermediate->getStage() == EShLangTessControl || |
| 460 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
| 461 | |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 462 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 463 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 464 | } |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 465 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 466 | return spv::BuiltInViewportIndex; |
| 467 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 468 | case glslang::EbvSampleId: |
| 469 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 470 | return spv::BuiltInSampleId; |
| 471 | |
| 472 | case glslang::EbvSamplePosition: |
| 473 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 474 | return spv::BuiltInSamplePosition; |
| 475 | |
| 476 | case glslang::EbvSampleMask: |
| 477 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 478 | return spv::BuiltInSampleMask; |
| 479 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 480 | case glslang::EbvLayer: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 481 | if (!memberDeclaration) { |
| 482 | builder.addCapability(spv::CapabilityGeometry); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 483 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 484 | glslangIntermediate->getStage() == EShLangTessControl || |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 485 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
| 486 | |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 487 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 488 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 489 | } |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 490 | } |
| 491 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 492 | return spv::BuiltInLayer; |
| 493 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 494 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 495 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 496 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 497 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 498 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 499 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 500 | case glslang::EbvBaseVertex: |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 501 | builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters); |
| 502 | builder.addCapability(spv::CapabilityDrawParameters); |
| 503 | return spv::BuiltInBaseVertex; |
| 504 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 505 | case glslang::EbvBaseInstance: |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 506 | builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters); |
| 507 | builder.addCapability(spv::CapabilityDrawParameters); |
| 508 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 509 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 510 | case glslang::EbvDrawId: |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 511 | builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters); |
| 512 | builder.addCapability(spv::CapabilityDrawParameters); |
| 513 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 514 | |
| 515 | case glslang::EbvPrimitiveId: |
| 516 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 517 | builder.addCapability(spv::CapabilityGeometry); |
| 518 | return spv::BuiltInPrimitiveId; |
| 519 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 520 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 521 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 522 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 523 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 524 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 525 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 526 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 527 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 528 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 529 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 530 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 531 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 532 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 533 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 534 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 535 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 536 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 537 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 538 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 539 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 540 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 541 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 542 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 543 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 544 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 545 | return spv::BuiltInSubgroupSize; |
| 546 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 547 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 548 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 549 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 550 | return spv::BuiltInSubgroupLocalInvocationId; |
| 551 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 552 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 553 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 554 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 555 | return spv::BuiltInSubgroupEqMaskKHR; |
| 556 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 557 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 558 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 559 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 560 | return spv::BuiltInSubgroupGeMaskKHR; |
| 561 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 562 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 563 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 564 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 565 | return spv::BuiltInSubgroupGtMaskKHR; |
| 566 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 567 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 568 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 569 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 570 | return spv::BuiltInSubgroupLeMaskKHR; |
| 571 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 572 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 573 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 574 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 575 | return spv::BuiltInSubgroupLtMaskKHR; |
| 576 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 577 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 578 | case glslang::EbvBaryCoordNoPersp: |
| 579 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 580 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 581 | |
| 582 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 583 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 584 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 585 | |
| 586 | case glslang::EbvBaryCoordNoPerspSample: |
| 587 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 588 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 589 | |
| 590 | case glslang::EbvBaryCoordSmooth: |
| 591 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 592 | return spv::BuiltInBaryCoordSmoothAMD; |
| 593 | |
| 594 | case glslang::EbvBaryCoordSmoothCentroid: |
| 595 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 596 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 597 | |
| 598 | case glslang::EbvBaryCoordSmoothSample: |
| 599 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 600 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 601 | |
| 602 | case glslang::EbvBaryCoordPullModel: |
| 603 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 604 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 605 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 606 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 607 | case glslang::EbvDeviceIndex: |
| 608 | builder.addExtension(spv::E_SPV_KHR_device_group); |
| 609 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 610 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 611 | |
| 612 | case glslang::EbvViewIndex: |
| 613 | builder.addExtension(spv::E_SPV_KHR_multiview); |
| 614 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 615 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 616 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 617 | #ifdef NV_EXTENSIONS |
| 618 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 619 | if (!memberDeclaration) { |
| 620 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 621 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 622 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 623 | return spv::BuiltInViewportMaskNV; |
| 624 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 625 | if (!memberDeclaration) { |
| 626 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 627 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 628 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 629 | return spv::BuiltInSecondaryPositionNV; |
| 630 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 631 | if (!memberDeclaration) { |
| 632 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 633 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 634 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 635 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 636 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 637 | if (!memberDeclaration) { |
| 638 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 639 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 640 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 641 | return spv::BuiltInPositionPerViewNV; |
| 642 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 643 | if (!memberDeclaration) { |
| 644 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 645 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 646 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 647 | return spv::BuiltInViewportMaskPerViewNV; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 648 | #endif |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 649 | default: |
| 650 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 654 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 655 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 656 | { |
| 657 | assert(type.getBasicType() == glslang::EbtSampler); |
| 658 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 659 | // Check for capabilities |
| 660 | switch (type.getQualifier().layoutFormat) { |
| 661 | case glslang::ElfRg32f: |
| 662 | case glslang::ElfRg16f: |
| 663 | case glslang::ElfR11fG11fB10f: |
| 664 | case glslang::ElfR16f: |
| 665 | case glslang::ElfRgba16: |
| 666 | case glslang::ElfRgb10A2: |
| 667 | case glslang::ElfRg16: |
| 668 | case glslang::ElfRg8: |
| 669 | case glslang::ElfR16: |
| 670 | case glslang::ElfR8: |
| 671 | case glslang::ElfRgba16Snorm: |
| 672 | case glslang::ElfRg16Snorm: |
| 673 | case glslang::ElfRg8Snorm: |
| 674 | case glslang::ElfR16Snorm: |
| 675 | case glslang::ElfR8Snorm: |
| 676 | |
| 677 | case glslang::ElfRg32i: |
| 678 | case glslang::ElfRg16i: |
| 679 | case glslang::ElfRg8i: |
| 680 | case glslang::ElfR16i: |
| 681 | case glslang::ElfR8i: |
| 682 | |
| 683 | case glslang::ElfRgb10a2ui: |
| 684 | case glslang::ElfRg32ui: |
| 685 | case glslang::ElfRg16ui: |
| 686 | case glslang::ElfRg8ui: |
| 687 | case glslang::ElfR16ui: |
| 688 | case glslang::ElfR8ui: |
| 689 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 690 | break; |
| 691 | |
| 692 | default: |
| 693 | break; |
| 694 | } |
| 695 | |
| 696 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 697 | switch (type.getQualifier().layoutFormat) { |
| 698 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 699 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 700 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 701 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 702 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 703 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 704 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 705 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 706 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 707 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 708 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 709 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 710 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 711 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 712 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 713 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 714 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 715 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 716 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 717 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 718 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 719 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 720 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 721 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 722 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 723 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 724 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 725 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 726 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 727 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 728 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 729 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 730 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 731 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 732 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 733 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 734 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 735 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 736 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 737 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 738 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 742 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(glslang::TSelectionControl selectionControl) const |
| 743 | { |
| 744 | switch (selectionControl) { |
| 745 | case glslang::ESelectionControlNone: return spv::SelectionControlMaskNone; |
| 746 | case glslang::ESelectionControlFlatten: return spv::SelectionControlFlattenMask; |
| 747 | case glslang::ESelectionControlDontFlatten: return spv::SelectionControlDontFlattenMask; |
| 748 | default: return spv::SelectionControlMaskNone; |
| 749 | } |
| 750 | } |
| 751 | |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 752 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(glslang::TLoopControl loopControl) const |
| 753 | { |
| 754 | switch (loopControl) { |
| 755 | case glslang::ELoopControlNone: return spv::LoopControlMaskNone; |
| 756 | case glslang::ELoopControlUnroll: return spv::LoopControlUnrollMask; |
| 757 | case glslang::ELoopControlDontUnroll: return spv::LoopControlDontUnrollMask; |
| 758 | // TODO: DependencyInfinite |
| 759 | // TODO: DependencyLength |
| 760 | default: return spv::LoopControlMaskNone; |
| 761 | } |
| 762 | } |
| 763 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 764 | // Translate glslang type to SPIR-V storage class. |
| 765 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 766 | { |
| 767 | if (type.getQualifier().isPipeInput()) |
| 768 | return spv::StorageClassInput; |
| 769 | else if (type.getQualifier().isPipeOutput()) |
| 770 | return spv::StorageClassOutput; |
| 771 | else if (type.getBasicType() == glslang::EbtAtomicUint) |
| 772 | return spv::StorageClassAtomicCounter; |
| 773 | else if (type.containsOpaque()) |
| 774 | return spv::StorageClassUniformConstant; |
| 775 | else if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
| 776 | builder.addExtension(spv::E_SPV_KHR_storage_buffer_storage_class); |
| 777 | return spv::StorageClassStorageBuffer; |
| 778 | } else if (type.getQualifier().isUniformOrBuffer()) { |
| 779 | if (type.getQualifier().layoutPushConstant) |
| 780 | return spv::StorageClassPushConstant; |
| 781 | if (type.getBasicType() == glslang::EbtBlock) |
| 782 | return spv::StorageClassUniform; |
| 783 | else |
| 784 | return spv::StorageClassUniformConstant; |
| 785 | } else { |
| 786 | switch (type.getQualifier().storage) { |
| 787 | case glslang::EvqShared: return spv::StorageClassWorkgroup; break; |
| 788 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 789 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 790 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
| 791 | default: |
| 792 | assert(0); |
| 793 | return spv::StorageClassFunction; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 798 | // 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] | 799 | // descriptor set. |
| 800 | bool IsDescriptorResource(const glslang::TType& type) |
| 801 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 802 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 803 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 804 | return type.getQualifier().isUniformOrBuffer() && ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 805 | |
| 806 | // non block... |
| 807 | // basically samplerXXX/subpass/sampler/texture are all included |
| 808 | // if they are the global-scope-class, not the function parameter |
| 809 | // (or local, if they ever exist) class. |
| 810 | if (type.getBasicType() == glslang::EbtSampler) |
| 811 | return type.getQualifier().isUniformOrBuffer(); |
| 812 | |
| 813 | // None of the above. |
| 814 | return false; |
| 815 | } |
| 816 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 817 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 818 | { |
| 819 | if (child.layoutMatrix == glslang::ElmNone) |
| 820 | child.layoutMatrix = parent.layoutMatrix; |
| 821 | |
| 822 | if (parent.invariant) |
| 823 | child.invariant = true; |
| 824 | if (parent.nopersp) |
| 825 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 826 | #ifdef AMD_EXTENSIONS |
| 827 | if (parent.explicitInterp) |
| 828 | child.explicitInterp = true; |
| 829 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 830 | if (parent.flat) |
| 831 | child.flat = true; |
| 832 | if (parent.centroid) |
| 833 | child.centroid = true; |
| 834 | if (parent.patch) |
| 835 | child.patch = true; |
| 836 | if (parent.sample) |
| 837 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 838 | if (parent.coherent) |
| 839 | child.coherent = true; |
| 840 | if (parent.volatil) |
| 841 | child.volatil = true; |
| 842 | if (parent.restrict) |
| 843 | child.restrict = true; |
| 844 | if (parent.readonly) |
| 845 | child.readonly = true; |
| 846 | if (parent.writeonly) |
| 847 | child.writeonly = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 848 | } |
| 849 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 850 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 851 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 852 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 853 | // - struct members might inherit from a struct declaration |
| 854 | // (note that non-block structs don't explicitly inherit, |
| 855 | // only implicitly, meaning no decoration involved) |
| 856 | // - affect decorations on the struct members |
| 857 | // (note smooth does not, and expecting something like volatile |
| 858 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 859 | // - 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] | 860 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 861 | } |
| 862 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 863 | // |
| 864 | // Implement the TGlslangToSpvTraverser class. |
| 865 | // |
| 866 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 867 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(const glslang::TIntermediate* glslangIntermediate, |
| 868 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 869 | : TIntermTraverser(true, false, true), |
| 870 | options(options), |
| 871 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 872 | sequenceDepth(0), logger(buildLogger), |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 873 | builder((glslang::GetKhronosToolId() << 16) | GeneratorVersion, logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 874 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 875 | glslangIntermediate(glslangIntermediate) |
| 876 | { |
| 877 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 878 | |
| 879 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 880 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 881 | glslangIntermediate->getVersion()); |
| 882 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 883 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 884 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 885 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 886 | |
| 887 | // Set the source shader's text. If for SPV version 1.0, include |
| 888 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 889 | // Otherwise, emit those as actual instructions. |
| 890 | std::string text; |
| 891 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 892 | for (int p = 0; p < (int)processes.size(); ++p) { |
| 893 | if (glslangIntermediate->getSpv().spv < 0x00010100) { |
| 894 | text.append("// OpModuleProcessed "); |
| 895 | text.append(processes[p]); |
| 896 | text.append("\n"); |
| 897 | } else |
| 898 | builder.addModuleProcessed(processes[p]); |
| 899 | } |
| 900 | if (glslangIntermediate->getSpv().spv < 0x00010100 && (int)processes.size() > 0) |
| 901 | text.append("#line 1\n"); |
| 902 | text.append(glslangIntermediate->getSourceText()); |
| 903 | builder.setSourceText(text); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 904 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 905 | stdBuiltins = builder.import("GLSL.std.450"); |
| 906 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 907 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 908 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 909 | |
| 910 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 911 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 912 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 913 | builder.addSourceExtension(it->c_str()); |
| 914 | |
| 915 | // Add the top-level modes for this shader. |
| 916 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 917 | if (glslangIntermediate->getXfbMode()) { |
| 918 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 919 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 920 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 921 | |
| 922 | unsigned int mode; |
| 923 | switch (glslangIntermediate->getStage()) { |
| 924 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 925 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 926 | break; |
| 927 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 928 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 929 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 930 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 931 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 932 | glslang::TLayoutGeometry primitive; |
| 933 | |
| 934 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 935 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 936 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 937 | } else { |
| 938 | primitive = glslangIntermediate->getInputPrimitive(); |
| 939 | } |
| 940 | |
| 941 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 942 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 943 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 944 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 945 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 946 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 947 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 948 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 949 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 950 | switch (glslangIntermediate->getVertexSpacing()) { |
| 951 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 952 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 953 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 954 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 955 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 956 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 957 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 958 | |
| 959 | switch (glslangIntermediate->getVertexOrder()) { |
| 960 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 961 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 962 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 963 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 964 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 965 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 966 | |
| 967 | if (glslangIntermediate->getPointMode()) |
| 968 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 969 | break; |
| 970 | |
| 971 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 972 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 973 | switch (glslangIntermediate->getInputPrimitive()) { |
| 974 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 975 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 976 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 977 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 978 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 979 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 980 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 981 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 982 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 983 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 984 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 985 | |
| 986 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 987 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 988 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 989 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 990 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 991 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 992 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 993 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 994 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 995 | break; |
| 996 | |
| 997 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 998 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 999 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1000 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1001 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1002 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1003 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1004 | else |
| 1005 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1006 | |
| 1007 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1008 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1009 | |
chaoc | c120452 | 2017-06-30 17:14:30 -0700 | [diff] [blame] | 1010 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1011 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1012 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1013 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1014 | } |
| 1015 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1016 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1017 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1018 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1019 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1020 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1021 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1022 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1023 | |
| 1024 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1025 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1026 | break; |
| 1027 | |
| 1028 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1029 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 1030 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1031 | glslangIntermediate->getLocalSize(1), |
| 1032 | glslangIntermediate->getLocalSize(2)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1033 | break; |
| 1034 | |
| 1035 | default: |
| 1036 | break; |
| 1037 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1038 | } |
| 1039 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1040 | // Finish creating SPV, after the traversal is complete. |
| 1041 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1042 | { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1043 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1044 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1045 | builder.leaveFunction(); |
| 1046 | } |
| 1047 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1048 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1049 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1050 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1051 | |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 1052 | builder.eliminateDeadDecorations(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1055 | // Write the SPV into 'out'. |
| 1056 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1057 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1058 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | // |
| 1062 | // Implement the traversal functions. |
| 1063 | // |
| 1064 | // Return true from interior nodes to have the external traversal |
| 1065 | // continue on to children. Return false if children were |
| 1066 | // already processed. |
| 1067 | // |
| 1068 | |
| 1069 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1070 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1071 | // - uniform/input reads |
| 1072 | // - output writes |
| 1073 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1074 | // - something simple that degenerates into the last bullet |
| 1075 | // |
| 1076 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1077 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1078 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1079 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1080 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1081 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1082 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1083 | // Formal function parameters were mapped during makeFunctions(). |
| 1084 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1085 | |
| 1086 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1087 | if (builder.isPointer(id)) { |
| 1088 | spv::StorageClass sc = builder.getStorageClass(id); |
| 1089 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) |
| 1090 | iOSet.insert(id); |
| 1091 | } |
| 1092 | |
| 1093 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1094 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1095 | // Prepare to generate code for the access |
| 1096 | |
| 1097 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1098 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1099 | // followed by setting the base. |
| 1100 | builder.clearAccessChain(); |
| 1101 | |
| 1102 | // 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] | 1103 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1104 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1105 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1106 | // 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] | 1107 | // These are also pure R-values. |
| 1108 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1109 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1110 | builder.setAccessChainRValue(id); |
| 1111 | else |
| 1112 | builder.setAccessChainLValue(id); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1117 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1118 | builder.setLine(node->getLoc().line); |
| 1119 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1120 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1121 | if (node->getType().getQualifier().isSpecConstant()) |
| 1122 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1123 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1124 | // First, handle special cases |
| 1125 | switch (node->getOp()) { |
| 1126 | case glslang::EOpAssign: |
| 1127 | case glslang::EOpAddAssign: |
| 1128 | case glslang::EOpSubAssign: |
| 1129 | case glslang::EOpMulAssign: |
| 1130 | case glslang::EOpVectorTimesMatrixAssign: |
| 1131 | case glslang::EOpVectorTimesScalarAssign: |
| 1132 | case glslang::EOpMatrixTimesScalarAssign: |
| 1133 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1134 | case glslang::EOpDivAssign: |
| 1135 | case glslang::EOpModAssign: |
| 1136 | case glslang::EOpAndAssign: |
| 1137 | case glslang::EOpInclusiveOrAssign: |
| 1138 | case glslang::EOpExclusiveOrAssign: |
| 1139 | case glslang::EOpLeftShiftAssign: |
| 1140 | case glslang::EOpRightShiftAssign: |
| 1141 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1142 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1143 | // says to evaluate the left before the right. So, always, left |
| 1144 | // node then right node. |
| 1145 | { |
| 1146 | // get the left l-value, save it away |
| 1147 | builder.clearAccessChain(); |
| 1148 | node->getLeft()->traverse(this); |
| 1149 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1150 | |
| 1151 | // evaluate the right |
| 1152 | builder.clearAccessChain(); |
| 1153 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1154 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1155 | |
| 1156 | if (node->getOp() != glslang::EOpAssign) { |
| 1157 | // the left is also an r-value |
| 1158 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1159 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1160 | |
| 1161 | // do the operation |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1162 | rValue = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1163 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1164 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1165 | node->getType().getBasicType()); |
| 1166 | |
| 1167 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1168 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | // store the result |
| 1172 | builder.setAccessChain(lValue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1173 | multiTypeStore(node->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1174 | |
| 1175 | // assignments are expressions having an rValue after they are evaluated... |
| 1176 | builder.clearAccessChain(); |
| 1177 | builder.setAccessChainRValue(rValue); |
| 1178 | } |
| 1179 | return false; |
| 1180 | case glslang::EOpIndexDirect: |
| 1181 | case glslang::EOpIndexDirectStruct: |
| 1182 | { |
| 1183 | // Get the left part of the access chain. |
| 1184 | node->getLeft()->traverse(this); |
| 1185 | |
| 1186 | // Add the next element in the chain |
| 1187 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1188 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1189 | if (! node->getLeft()->getType().isArray() && |
| 1190 | node->getLeft()->getType().isVector() && |
| 1191 | node->getOp() == glslang::EOpIndexDirect) { |
| 1192 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1193 | // so short circuit the access-chain stuff with a swizzle. |
| 1194 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1195 | swizzle.push_back(glslangIndex); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1196 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1197 | } else { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1198 | int spvIndex = glslangIndex; |
| 1199 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1200 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1201 | { |
| 1202 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1203 | // index remapping due to hidden members in anonymous blocks. |
| 1204 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1205 | assert(remapper.size() > 0); |
| 1206 | spvIndex = remapper[glslangIndex]; |
| 1207 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1208 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1209 | // normal case for indexing array or structure or block |
| 1210 | builder.accessChainPush(builder.makeIntConstant(spvIndex)); |
| 1211 | |
| 1212 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1213 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1214 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1215 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | return false; |
| 1219 | case glslang::EOpIndexIndirect: |
| 1220 | { |
| 1221 | // Structure or array or vector indirection. |
| 1222 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 1223 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1224 | // Will use the access chain's 'component' for variable index into a vector. |
| 1225 | |
| 1226 | // This adapter is building access chains left to right. |
| 1227 | // Set up the access chain to the left. |
| 1228 | node->getLeft()->traverse(this); |
| 1229 | |
| 1230 | // save it so that computing the right side doesn't trash it |
| 1231 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1232 | |
| 1233 | // compute the next index in the chain |
| 1234 | builder.clearAccessChain(); |
| 1235 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1236 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1237 | |
| 1238 | // restore the saved access chain |
| 1239 | builder.setAccessChain(partial); |
| 1240 | |
| 1241 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1242 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1243 | else |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1244 | builder.accessChainPush(index); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1245 | } |
| 1246 | return false; |
| 1247 | case glslang::EOpVectorSwizzle: |
| 1248 | { |
| 1249 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1250 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1251 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1252 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1253 | } |
| 1254 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1255 | case glslang::EOpMatrixSwizzle: |
| 1256 | logger->missingFunctionality("matrix swizzle"); |
| 1257 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1258 | case glslang::EOpLogicalOr: |
| 1259 | case glslang::EOpLogicalAnd: |
| 1260 | { |
| 1261 | |
| 1262 | // These may require short circuiting, but can sometimes be done as straight |
| 1263 | // binary operations. The right operand must be short circuited if it has |
| 1264 | // side effects, and should probably be if it is complex. |
| 1265 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1266 | break; // handle below as a normal binary operation |
| 1267 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1268 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1269 | builder.clearAccessChain(); |
| 1270 | builder.setAccessChainRValue(result); |
| 1271 | } |
| 1272 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1273 | default: |
| 1274 | break; |
| 1275 | } |
| 1276 | |
| 1277 | // Assume generic binary op... |
| 1278 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1279 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1280 | builder.clearAccessChain(); |
| 1281 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1282 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1283 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1284 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1285 | builder.clearAccessChain(); |
| 1286 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1287 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1288 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1289 | // get result |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1290 | spv::Id result = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1291 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1292 | convertGlslangToSpvType(node->getType()), left, right, |
| 1293 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1294 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1295 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1296 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1297 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1298 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1299 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1300 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1301 | return false; |
| 1302 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1306 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1307 | builder.setLine(node->getLoc().line); |
| 1308 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1309 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1310 | if (node->getType().getQualifier().isSpecConstant()) |
| 1311 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1312 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1313 | spv::Id result = spv::NoResult; |
| 1314 | |
| 1315 | // try texturing first |
| 1316 | result = createImageTextureFunctionCall(node); |
| 1317 | if (result != spv::NoResult) { |
| 1318 | builder.clearAccessChain(); |
| 1319 | builder.setAccessChainRValue(result); |
| 1320 | |
| 1321 | return false; // done with this node |
| 1322 | } |
| 1323 | |
| 1324 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1325 | |
| 1326 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1327 | // Quite special; won't want to evaluate the operand. |
| 1328 | |
| 1329 | // Normal .length() would have been constant folded by the front-end. |
| 1330 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1331 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1332 | assert(node->getOperand()->getType().isRuntimeSizedArray()); |
| 1333 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1334 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1335 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1336 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1337 | |
| 1338 | builder.clearAccessChain(); |
| 1339 | builder.setAccessChainRValue(length); |
| 1340 | |
| 1341 | return false; |
| 1342 | } |
| 1343 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1344 | // Start by evaluating the operand |
| 1345 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1346 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1347 | // operate first on the swizzle base, then apply the swizzle. |
| 1348 | spv::Id invertedType = spv::NoType; |
| 1349 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 1350 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 1351 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 1352 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1353 | builder.clearAccessChain(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1354 | if (invertedType != spv::NoType) |
| 1355 | node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this); |
| 1356 | else |
| 1357 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1358 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1359 | spv::Id operand = spv::NoResult; |
| 1360 | |
| 1361 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1362 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1363 | node->getOp() == glslang::EOpAtomicCounter || |
| 1364 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1365 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1366 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1367 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1368 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1369 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1370 | spv::Decoration noContraction = TranslateNoContractionDecoration(node->getType().getQualifier()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1371 | |
| 1372 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1373 | if (! result) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1374 | result = createConversion(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1375 | |
| 1376 | // if not, then possibly an operation |
| 1377 | if (! result) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1378 | result = createUnaryOperation(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1379 | |
| 1380 | if (result) { |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1381 | if (invertedType) |
| 1382 | result = createInvertedSwizzle(precision, *node->getOperand(), result); |
| 1383 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1384 | builder.clearAccessChain(); |
| 1385 | builder.setAccessChainRValue(result); |
| 1386 | |
| 1387 | return false; // done with this node |
| 1388 | } |
| 1389 | |
| 1390 | // it must be a special case, check... |
| 1391 | switch (node->getOp()) { |
| 1392 | case glslang::EOpPostIncrement: |
| 1393 | case glslang::EOpPostDecrement: |
| 1394 | case glslang::EOpPreIncrement: |
| 1395 | case glslang::EOpPreDecrement: |
| 1396 | { |
| 1397 | // 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] | 1398 | spv::Id one = 0; |
| 1399 | if (node->getBasicType() == glslang::EbtFloat) |
| 1400 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 1401 | else if (node->getBasicType() == glslang::EbtDouble) |
| 1402 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1403 | #ifdef AMD_EXTENSIONS |
| 1404 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 1405 | one = builder.makeFloat16Constant(1.0F); |
| 1406 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1407 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1408 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1409 | #ifdef AMD_EXTENSIONS |
| 1410 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 1411 | one = builder.makeInt16Constant(1); |
| 1412 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1413 | else |
| 1414 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1415 | glslang::TOperator op; |
| 1416 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1417 | node->getOp() == glslang::EOpPostIncrement) |
| 1418 | op = glslang::EOpAdd; |
| 1419 | else |
| 1420 | op = glslang::EOpSub; |
| 1421 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1422 | spv::Id result = createBinaryOperation(op, precision, |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1423 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1424 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1425 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1426 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1427 | |
| 1428 | // The result of operation is always stored, but conditionally the |
| 1429 | // consumed result. The consumed result is always an r-value. |
| 1430 | builder.accessChainStore(result); |
| 1431 | builder.clearAccessChain(); |
| 1432 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1433 | node->getOp() == glslang::EOpPreDecrement) |
| 1434 | builder.setAccessChainRValue(result); |
| 1435 | else |
| 1436 | builder.setAccessChainRValue(operand); |
| 1437 | } |
| 1438 | |
| 1439 | return false; |
| 1440 | |
| 1441 | case glslang::EOpEmitStreamVertex: |
| 1442 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1443 | return false; |
| 1444 | case glslang::EOpEndStreamPrimitive: |
| 1445 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1446 | return false; |
| 1447 | |
| 1448 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1449 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1450 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1451 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 1455 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 1456 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1457 | if (node->getType().getQualifier().isSpecConstant()) |
| 1458 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1459 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1460 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1461 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 1462 | 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] | 1463 | |
| 1464 | // try texturing |
| 1465 | result = createImageTextureFunctionCall(node); |
| 1466 | if (result != spv::NoResult) { |
| 1467 | builder.clearAccessChain(); |
| 1468 | builder.setAccessChainRValue(result); |
| 1469 | |
| 1470 | return false; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1471 | #ifdef AMD_EXTENSIONS |
| 1472 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 1473 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 1474 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1475 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1476 | // "imageStore" is a special case, which has no result |
| 1477 | return false; |
| 1478 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1479 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1480 | glslang::TOperator binOp = glslang::EOpNull; |
| 1481 | bool reduceComparison = true; |
| 1482 | bool isMatrix = false; |
| 1483 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1484 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1485 | |
| 1486 | assert(node->getOp()); |
| 1487 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1488 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1489 | |
| 1490 | switch (node->getOp()) { |
| 1491 | case glslang::EOpSequence: |
| 1492 | { |
| 1493 | if (preVisit) |
| 1494 | ++sequenceDepth; |
| 1495 | else |
| 1496 | --sequenceDepth; |
| 1497 | |
| 1498 | if (sequenceDepth == 1) { |
| 1499 | // If this is the parent node of all the functions, we want to see them |
| 1500 | // early, so all call points have actual SPIR-V functions to reference. |
| 1501 | // In all cases, still let the traverser visit the children for us. |
| 1502 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 1503 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1504 | // 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] | 1505 | // anything else gets there, so visit out of order, doing them all now. |
| 1506 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 1507 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 1508 | // 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] | 1509 | // so do them manually. |
| 1510 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 1511 | |
| 1512 | return false; |
| 1513 | } |
| 1514 | |
| 1515 | return true; |
| 1516 | } |
| 1517 | case glslang::EOpLinkerObjects: |
| 1518 | { |
| 1519 | if (visit == glslang::EvPreVisit) |
| 1520 | linkageOnly = true; |
| 1521 | else |
| 1522 | linkageOnly = false; |
| 1523 | |
| 1524 | return true; |
| 1525 | } |
| 1526 | case glslang::EOpComma: |
| 1527 | { |
| 1528 | // processing from left to right naturally leaves the right-most |
| 1529 | // lying around in the access chain |
| 1530 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1531 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 1532 | glslangOperands[i]->traverse(this); |
| 1533 | |
| 1534 | return false; |
| 1535 | } |
| 1536 | case glslang::EOpFunction: |
| 1537 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1538 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1539 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1540 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1541 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1542 | } else { |
| 1543 | handleFunctionEntry(node); |
| 1544 | } |
| 1545 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1546 | if (inEntryPoint) |
| 1547 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 1548 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1549 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | return true; |
| 1553 | case glslang::EOpParameters: |
| 1554 | // Parameters will have been consumed by EOpFunction processing, but not |
| 1555 | // the body, so we still visited the function node's children, making this |
| 1556 | // child redundant. |
| 1557 | return false; |
| 1558 | case glslang::EOpFunctionCall: |
| 1559 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1560 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1561 | if (node->isUserDefined()) |
| 1562 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 1563 | // 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] | 1564 | if (result) { |
| 1565 | builder.clearAccessChain(); |
| 1566 | builder.setAccessChainRValue(result); |
| 1567 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1568 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1569 | |
| 1570 | return false; |
| 1571 | } |
| 1572 | case glslang::EOpConstructMat2x2: |
| 1573 | case glslang::EOpConstructMat2x3: |
| 1574 | case glslang::EOpConstructMat2x4: |
| 1575 | case glslang::EOpConstructMat3x2: |
| 1576 | case glslang::EOpConstructMat3x3: |
| 1577 | case glslang::EOpConstructMat3x4: |
| 1578 | case glslang::EOpConstructMat4x2: |
| 1579 | case glslang::EOpConstructMat4x3: |
| 1580 | case glslang::EOpConstructMat4x4: |
| 1581 | case glslang::EOpConstructDMat2x2: |
| 1582 | case glslang::EOpConstructDMat2x3: |
| 1583 | case glslang::EOpConstructDMat2x4: |
| 1584 | case glslang::EOpConstructDMat3x2: |
| 1585 | case glslang::EOpConstructDMat3x3: |
| 1586 | case glslang::EOpConstructDMat3x4: |
| 1587 | case glslang::EOpConstructDMat4x2: |
| 1588 | case glslang::EOpConstructDMat4x3: |
| 1589 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 1590 | case glslang::EOpConstructIMat2x2: |
| 1591 | case glslang::EOpConstructIMat2x3: |
| 1592 | case glslang::EOpConstructIMat2x4: |
| 1593 | case glslang::EOpConstructIMat3x2: |
| 1594 | case glslang::EOpConstructIMat3x3: |
| 1595 | case glslang::EOpConstructIMat3x4: |
| 1596 | case glslang::EOpConstructIMat4x2: |
| 1597 | case glslang::EOpConstructIMat4x3: |
| 1598 | case glslang::EOpConstructIMat4x4: |
| 1599 | case glslang::EOpConstructUMat2x2: |
| 1600 | case glslang::EOpConstructUMat2x3: |
| 1601 | case glslang::EOpConstructUMat2x4: |
| 1602 | case glslang::EOpConstructUMat3x2: |
| 1603 | case glslang::EOpConstructUMat3x3: |
| 1604 | case glslang::EOpConstructUMat3x4: |
| 1605 | case glslang::EOpConstructUMat4x2: |
| 1606 | case glslang::EOpConstructUMat4x3: |
| 1607 | case glslang::EOpConstructUMat4x4: |
| 1608 | case glslang::EOpConstructBMat2x2: |
| 1609 | case glslang::EOpConstructBMat2x3: |
| 1610 | case glslang::EOpConstructBMat2x4: |
| 1611 | case glslang::EOpConstructBMat3x2: |
| 1612 | case glslang::EOpConstructBMat3x3: |
| 1613 | case glslang::EOpConstructBMat3x4: |
| 1614 | case glslang::EOpConstructBMat4x2: |
| 1615 | case glslang::EOpConstructBMat4x3: |
| 1616 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1617 | #ifdef AMD_EXTENSIONS |
| 1618 | case glslang::EOpConstructF16Mat2x2: |
| 1619 | case glslang::EOpConstructF16Mat2x3: |
| 1620 | case glslang::EOpConstructF16Mat2x4: |
| 1621 | case glslang::EOpConstructF16Mat3x2: |
| 1622 | case glslang::EOpConstructF16Mat3x3: |
| 1623 | case glslang::EOpConstructF16Mat3x4: |
| 1624 | case glslang::EOpConstructF16Mat4x2: |
| 1625 | case glslang::EOpConstructF16Mat4x3: |
| 1626 | case glslang::EOpConstructF16Mat4x4: |
| 1627 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1628 | isMatrix = true; |
| 1629 | // fall through |
| 1630 | case glslang::EOpConstructFloat: |
| 1631 | case glslang::EOpConstructVec2: |
| 1632 | case glslang::EOpConstructVec3: |
| 1633 | case glslang::EOpConstructVec4: |
| 1634 | case glslang::EOpConstructDouble: |
| 1635 | case glslang::EOpConstructDVec2: |
| 1636 | case glslang::EOpConstructDVec3: |
| 1637 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1638 | #ifdef AMD_EXTENSIONS |
| 1639 | case glslang::EOpConstructFloat16: |
| 1640 | case glslang::EOpConstructF16Vec2: |
| 1641 | case glslang::EOpConstructF16Vec3: |
| 1642 | case glslang::EOpConstructF16Vec4: |
| 1643 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1644 | case glslang::EOpConstructBool: |
| 1645 | case glslang::EOpConstructBVec2: |
| 1646 | case glslang::EOpConstructBVec3: |
| 1647 | case glslang::EOpConstructBVec4: |
| 1648 | case glslang::EOpConstructInt: |
| 1649 | case glslang::EOpConstructIVec2: |
| 1650 | case glslang::EOpConstructIVec3: |
| 1651 | case glslang::EOpConstructIVec4: |
| 1652 | case glslang::EOpConstructUint: |
| 1653 | case glslang::EOpConstructUVec2: |
| 1654 | case glslang::EOpConstructUVec3: |
| 1655 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1656 | case glslang::EOpConstructInt64: |
| 1657 | case glslang::EOpConstructI64Vec2: |
| 1658 | case glslang::EOpConstructI64Vec3: |
| 1659 | case glslang::EOpConstructI64Vec4: |
| 1660 | case glslang::EOpConstructUint64: |
| 1661 | case glslang::EOpConstructU64Vec2: |
| 1662 | case glslang::EOpConstructU64Vec3: |
| 1663 | case glslang::EOpConstructU64Vec4: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1664 | #ifdef AMD_EXTENSIONS |
| 1665 | case glslang::EOpConstructInt16: |
| 1666 | case glslang::EOpConstructI16Vec2: |
| 1667 | case glslang::EOpConstructI16Vec3: |
| 1668 | case glslang::EOpConstructI16Vec4: |
| 1669 | case glslang::EOpConstructUint16: |
| 1670 | case glslang::EOpConstructU16Vec2: |
| 1671 | case glslang::EOpConstructU16Vec3: |
| 1672 | case glslang::EOpConstructU16Vec4: |
| 1673 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1674 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1675 | case glslang::EOpConstructTextureSampler: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1676 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1677 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1678 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1679 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1680 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1681 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1682 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1683 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1684 | std::vector<spv::Id> constituents; |
| 1685 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 1686 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1687 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1688 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1689 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1690 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1691 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1692 | |
| 1693 | builder.clearAccessChain(); |
| 1694 | builder.setAccessChainRValue(constructed); |
| 1695 | |
| 1696 | return false; |
| 1697 | } |
| 1698 | |
| 1699 | // These six are component-wise compares with component-wise results. |
| 1700 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 1701 | case glslang::EOpLessThan: |
| 1702 | case glslang::EOpGreaterThan: |
| 1703 | case glslang::EOpLessThanEqual: |
| 1704 | case glslang::EOpGreaterThanEqual: |
| 1705 | case glslang::EOpVectorEqual: |
| 1706 | case glslang::EOpVectorNotEqual: |
| 1707 | { |
| 1708 | // Map the operation to a binary |
| 1709 | binOp = node->getOp(); |
| 1710 | reduceComparison = false; |
| 1711 | switch (node->getOp()) { |
| 1712 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 1713 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 1714 | default: binOp = node->getOp(); break; |
| 1715 | } |
| 1716 | |
| 1717 | break; |
| 1718 | } |
| 1719 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1720 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1721 | binOp = glslang::EOpMul; |
| 1722 | break; |
| 1723 | case glslang::EOpOuterProduct: |
| 1724 | // two vectors multiplied to make a matrix |
| 1725 | binOp = glslang::EOpOuterProduct; |
| 1726 | break; |
| 1727 | case glslang::EOpDot: |
| 1728 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1729 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1730 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 1731 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1732 | binOp = glslang::EOpMul; |
| 1733 | break; |
| 1734 | } |
| 1735 | case glslang::EOpMod: |
| 1736 | // when an aggregate, this is the floating-point mod built-in function, |
| 1737 | // which can be emitted by the one in createBinaryOperation() |
| 1738 | binOp = glslang::EOpMod; |
| 1739 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1740 | case glslang::EOpEmitVertex: |
| 1741 | case glslang::EOpEndPrimitive: |
| 1742 | case glslang::EOpBarrier: |
| 1743 | case glslang::EOpMemoryBarrier: |
| 1744 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 1745 | case glslang::EOpMemoryBarrierBuffer: |
| 1746 | case glslang::EOpMemoryBarrierImage: |
| 1747 | case glslang::EOpMemoryBarrierShared: |
| 1748 | case glslang::EOpGroupMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 1749 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 1750 | case glslang::EOpGroupMemoryBarrierWithGroupSync: |
| 1751 | case glslang::EOpWorkgroupMemoryBarrier: |
| 1752 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1753 | noReturnValue = true; |
| 1754 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 1755 | break; |
| 1756 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1757 | case glslang::EOpAtomicAdd: |
| 1758 | case glslang::EOpAtomicMin: |
| 1759 | case glslang::EOpAtomicMax: |
| 1760 | case glslang::EOpAtomicAnd: |
| 1761 | case glslang::EOpAtomicOr: |
| 1762 | case glslang::EOpAtomicXor: |
| 1763 | case glslang::EOpAtomicExchange: |
| 1764 | case glslang::EOpAtomicCompSwap: |
| 1765 | atomic = true; |
| 1766 | break; |
| 1767 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 1768 | case glslang::EOpAtomicCounterAdd: |
| 1769 | case glslang::EOpAtomicCounterSubtract: |
| 1770 | case glslang::EOpAtomicCounterMin: |
| 1771 | case glslang::EOpAtomicCounterMax: |
| 1772 | case glslang::EOpAtomicCounterAnd: |
| 1773 | case glslang::EOpAtomicCounterOr: |
| 1774 | case glslang::EOpAtomicCounterXor: |
| 1775 | case glslang::EOpAtomicCounterExchange: |
| 1776 | case glslang::EOpAtomicCounterCompSwap: |
| 1777 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 1778 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 1779 | atomic = true; |
| 1780 | break; |
| 1781 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1782 | default: |
| 1783 | break; |
| 1784 | } |
| 1785 | |
| 1786 | // |
| 1787 | // See if it maps to a regular operation. |
| 1788 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1789 | if (binOp != glslang::EOpNull) { |
| 1790 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 1791 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 1792 | assert(left && right); |
| 1793 | |
| 1794 | builder.clearAccessChain(); |
| 1795 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1796 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1797 | |
| 1798 | builder.clearAccessChain(); |
| 1799 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1800 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1801 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1802 | builder.setLine(node->getLoc().line); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1803 | result = createBinaryOperation(binOp, precision, TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1804 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1805 | left->getType().getBasicType(), reduceComparison); |
| 1806 | |
| 1807 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1808 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1809 | builder.clearAccessChain(); |
| 1810 | builder.setAccessChainRValue(result); |
| 1811 | |
| 1812 | return false; |
| 1813 | } |
| 1814 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1815 | // |
| 1816 | // Create the list of operands. |
| 1817 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1818 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1819 | std::vector<spv::Id> operands; |
| 1820 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1821 | // special case l-value operands; there are just a few |
| 1822 | bool lvalue = false; |
| 1823 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1824 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1825 | case glslang::EOpModf: |
| 1826 | if (arg == 1) |
| 1827 | lvalue = true; |
| 1828 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1829 | case glslang::EOpInterpolateAtSample: |
| 1830 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1831 | #ifdef AMD_EXTENSIONS |
| 1832 | case glslang::EOpInterpolateAtVertex: |
| 1833 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1834 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1835 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1836 | |
| 1837 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1838 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 1839 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1840 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 1841 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 1842 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1843 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 1844 | case glslang::EOpAtomicAdd: |
| 1845 | case glslang::EOpAtomicMin: |
| 1846 | case glslang::EOpAtomicMax: |
| 1847 | case glslang::EOpAtomicAnd: |
| 1848 | case glslang::EOpAtomicOr: |
| 1849 | case glslang::EOpAtomicXor: |
| 1850 | case glslang::EOpAtomicExchange: |
| 1851 | case glslang::EOpAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 1852 | case glslang::EOpAtomicCounterAdd: |
| 1853 | case glslang::EOpAtomicCounterSubtract: |
| 1854 | case glslang::EOpAtomicCounterMin: |
| 1855 | case glslang::EOpAtomicCounterMax: |
| 1856 | case glslang::EOpAtomicCounterAnd: |
| 1857 | case glslang::EOpAtomicCounterOr: |
| 1858 | case glslang::EOpAtomicCounterXor: |
| 1859 | case glslang::EOpAtomicCounterExchange: |
| 1860 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 1861 | if (arg == 0) |
| 1862 | lvalue = true; |
| 1863 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1864 | case glslang::EOpAddCarry: |
| 1865 | case glslang::EOpSubBorrow: |
| 1866 | if (arg == 2) |
| 1867 | lvalue = true; |
| 1868 | break; |
| 1869 | case glslang::EOpUMulExtended: |
| 1870 | case glslang::EOpIMulExtended: |
| 1871 | if (arg >= 2) |
| 1872 | lvalue = true; |
| 1873 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1874 | default: |
| 1875 | break; |
| 1876 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1877 | builder.clearAccessChain(); |
| 1878 | if (invertedType != spv::NoType && arg == 0) |
| 1879 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 1880 | else |
| 1881 | glslangOperands[arg]->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1882 | if (lvalue) |
| 1883 | operands.push_back(builder.accessChainGetLValue()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1884 | else { |
| 1885 | builder.setLine(node->getLoc().line); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1886 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1887 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1888 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1889 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1890 | builder.setLine(node->getLoc().line); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1891 | if (atomic) { |
| 1892 | // Handle all atomics |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1893 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1894 | } else { |
| 1895 | // Pass through to generic operations. |
| 1896 | switch (glslangOperands.size()) { |
| 1897 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1898 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1899 | break; |
| 1900 | case 1: |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1901 | result = createUnaryOperation( |
| 1902 | node->getOp(), precision, |
| 1903 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1904 | resultType(), operands.front(), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1905 | glslangOperands[0]->getAsTyped()->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1906 | break; |
| 1907 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1908 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1909 | break; |
| 1910 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1911 | if (invertedType) |
| 1912 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | if (noReturnValue) |
| 1916 | return false; |
| 1917 | |
| 1918 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1919 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1920 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1921 | } else { |
| 1922 | builder.clearAccessChain(); |
| 1923 | builder.setAccessChainRValue(result); |
| 1924 | return false; |
| 1925 | } |
| 1926 | } |
| 1927 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1928 | // This path handles both if-then-else and ?: |
| 1929 | // The if-then-else has a node type of void, while |
| 1930 | // ?: has either a void or a non-void node type |
| 1931 | // |
| 1932 | // Leaving the result, when not void: |
| 1933 | // GLSL only has r-values as the result of a :?, but |
| 1934 | // if we have an l-value, that can be more efficient if it will |
| 1935 | // become the base of a complex r-value expression, because the |
| 1936 | // 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] | 1937 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 1938 | { |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1939 | // See if it simple and safe to generate OpSelect instead of using control flow. |
| 1940 | // Crucially, side effects must be avoided, and there are performance trade-offs. |
| 1941 | // Return true if good idea (and safe) for OpSelect, false otherwise. |
| 1942 | const auto selectPolicy = [&]() -> bool { |
John Kessenich | 0479437 | 2017-03-01 13:49:11 -0700 | [diff] [blame] | 1943 | if ((!node->getType().isScalar() && !node->getType().isVector()) || |
| 1944 | node->getBasicType() == glslang::EbtVoid) |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1945 | return false; |
| 1946 | |
| 1947 | if (node->getTrueBlock() == nullptr || |
| 1948 | node->getFalseBlock() == nullptr) |
| 1949 | return false; |
| 1950 | |
| 1951 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 1952 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 1953 | |
| 1954 | // return true if a single operand to ? : is okay for OpSelect |
| 1955 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 1956 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1957 | }; |
| 1958 | |
| 1959 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 1960 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 1961 | }; |
| 1962 | |
| 1963 | // Emit OpSelect for this selection. |
| 1964 | const auto handleAsOpSelect = [&]() { |
| 1965 | node->getCondition()->traverse(this); |
| 1966 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 1967 | node->getTrueBlock()->traverse(this); |
| 1968 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 1969 | node->getFalseBlock()->traverse(this); |
| 1970 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 1971 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1972 | builder.setLine(node->getLoc().line); |
| 1973 | |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 1974 | // smear condition to vector, if necessary (AST is always scalar) |
| 1975 | if (builder.isVector(trueValue)) |
| 1976 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 1977 | builder.makeVectorType(builder.makeBoolType(), |
| 1978 | builder.getNumComponents(trueValue))); |
| 1979 | |
| 1980 | spv::Id select = builder.createTriOp(spv::OpSelect, |
| 1981 | convertGlslangToSpvType(node->getType()), condition, |
| 1982 | trueValue, falseValue); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1983 | builder.clearAccessChain(); |
| 1984 | builder.setAccessChainRValue(select); |
| 1985 | }; |
| 1986 | |
| 1987 | // Try for OpSelect |
| 1988 | |
| 1989 | if (selectPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 1990 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1991 | if (node->getType().getQualifier().isSpecConstant()) |
| 1992 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1993 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1994 | handleAsOpSelect(); |
| 1995 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1996 | } |
| 1997 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1998 | // Instead, emit control flow... |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1999 | // Don't handle results as temporaries, because there will be two names |
| 2000 | // and better to leave SSA to later passes. |
| 2001 | spv::Id result = (node->getBasicType() == glslang::EbtVoid) |
| 2002 | ? spv::NoResult |
| 2003 | : builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2004 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2005 | // emit the condition before doing anything with selection |
| 2006 | node->getCondition()->traverse(this); |
| 2007 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2008 | // Selection control: |
| 2009 | const spv::SelectionControlMask control = TranslateSelectionControl(node->getSelectionControl()); |
| 2010 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2011 | // make an "if" based on the value created by the condition |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2012 | spv::Builder::If ifBuilder(accessChainLoad(node->getCondition()->getType()), control, builder); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2013 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2014 | // emit the "then" statement |
| 2015 | if (node->getTrueBlock() != nullptr) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2016 | node->getTrueBlock()->traverse(this); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2017 | if (result != spv::NoResult) |
| 2018 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2019 | } |
| 2020 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2021 | if (node->getFalseBlock() != nullptr) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2022 | ifBuilder.makeBeginElse(); |
| 2023 | // emit the "else" statement |
| 2024 | node->getFalseBlock()->traverse(this); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2025 | if (result != spv::NoResult) |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2026 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2027 | } |
| 2028 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2029 | // finish off the control flow |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2030 | ifBuilder.makeEndIf(); |
| 2031 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2032 | if (result != spv::NoResult) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2033 | // GLSL only has r-values as the result of a :?, but |
| 2034 | // if we have an l-value, that can be more efficient if it will |
| 2035 | // become the base of a complex r-value expression, because the |
| 2036 | // next layer copies r-values into memory to use the access-chain mechanism |
| 2037 | builder.clearAccessChain(); |
| 2038 | builder.setAccessChainLValue(result); |
| 2039 | } |
| 2040 | |
| 2041 | return false; |
| 2042 | } |
| 2043 | |
| 2044 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2045 | { |
| 2046 | // emit and get the condition before doing anything with switch |
| 2047 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2048 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2049 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2050 | // Selection control: |
| 2051 | const spv::SelectionControlMask control = TranslateSelectionControl(node->getSelectionControl()); |
| 2052 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2053 | // browse the children to sort out code segments |
| 2054 | int defaultSegment = -1; |
| 2055 | std::vector<TIntermNode*> codeSegments; |
| 2056 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 2057 | std::vector<int> caseValues; |
| 2058 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 2059 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 2060 | TIntermNode* child = *c; |
| 2061 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2062 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2063 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2064 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2065 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2066 | } else |
| 2067 | codeSegments.push_back(child); |
| 2068 | } |
| 2069 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2070 | // 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] | 2071 | // statements between the last case and the end of the switch statement |
| 2072 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 2073 | (int)codeSegments.size() == defaultSegment) |
| 2074 | codeSegments.push_back(nullptr); |
| 2075 | |
| 2076 | // make the switch statement |
| 2077 | 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] | 2078 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2079 | |
| 2080 | // emit all the code in the segments |
| 2081 | breakForLoop.push(false); |
| 2082 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 2083 | builder.nextSwitchSegment(segmentBlocks, s); |
| 2084 | if (codeSegments[s]) |
| 2085 | codeSegments[s]->traverse(this); |
| 2086 | else |
| 2087 | builder.addSwitchBreak(); |
| 2088 | } |
| 2089 | breakForLoop.pop(); |
| 2090 | |
| 2091 | builder.endSwitch(segmentBlocks); |
| 2092 | |
| 2093 | return false; |
| 2094 | } |
| 2095 | |
| 2096 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 2097 | { |
| 2098 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2099 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2100 | |
| 2101 | builder.clearAccessChain(); |
| 2102 | builder.setAccessChainRValue(constant); |
| 2103 | } |
| 2104 | |
| 2105 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 2106 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2107 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2108 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2109 | |
| 2110 | // Loop control: |
| 2111 | const spv::LoopControlMask control = TranslateLoopControl(node->getLoopControl()); |
| 2112 | |
| 2113 | // TODO: dependency length |
| 2114 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2115 | // Spec requires back edges to target header blocks, and every header block |
| 2116 | // must dominate its merge block. Make a header block first to ensure these |
| 2117 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 2118 | // by a block-ending branch. But we don't want to put any other body/test |
| 2119 | // instructions in it, since the body/test may have arbitrary instructions, |
| 2120 | // including merges of its own. |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2121 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2122 | builder.setBuildPoint(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2123 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2124 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2125 | spv::Block& test = builder.makeNewBlock(); |
| 2126 | builder.createBranch(&test); |
| 2127 | |
| 2128 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2129 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2130 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2131 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 2132 | |
| 2133 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2134 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2135 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2136 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2137 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2138 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2139 | |
| 2140 | builder.setBuildPoint(&blocks.continue_target); |
| 2141 | if (node->getTerminal()) |
| 2142 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2143 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 2144 | } else { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2145 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2146 | builder.createBranch(&blocks.body); |
| 2147 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2148 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2149 | builder.setBuildPoint(&blocks.body); |
| 2150 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2151 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2152 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2153 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2154 | |
| 2155 | builder.setBuildPoint(&blocks.continue_target); |
| 2156 | if (node->getTerminal()) |
| 2157 | node->getTerminal()->traverse(this); |
| 2158 | if (node->getTest()) { |
| 2159 | node->getTest()->traverse(this); |
| 2160 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2161 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2162 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2163 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 2164 | // TODO: unless there was a break/return/discard instruction |
| 2165 | // somewhere in the body, this is an infinite loop, so we should |
| 2166 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2167 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2168 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2169 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2170 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2171 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2172 | return false; |
| 2173 | } |
| 2174 | |
| 2175 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 2176 | { |
| 2177 | if (node->getExpression()) |
| 2178 | node->getExpression()->traverse(this); |
| 2179 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2180 | builder.setLine(node->getLoc().line); |
| 2181 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2182 | switch (node->getFlowOp()) { |
| 2183 | case glslang::EOpKill: |
| 2184 | builder.makeDiscard(); |
| 2185 | break; |
| 2186 | case glslang::EOpBreak: |
| 2187 | if (breakForLoop.top()) |
| 2188 | builder.createLoopExit(); |
| 2189 | else |
| 2190 | builder.addSwitchBreak(); |
| 2191 | break; |
| 2192 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2193 | builder.createLoopContinue(); |
| 2194 | break; |
| 2195 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2196 | if (node->getExpression()) { |
| 2197 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 2198 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 2199 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 2200 | builder.clearAccessChain(); |
| 2201 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 2202 | builder.setAccessChainLValue(copyId); |
| 2203 | multiTypeStore(glslangReturnType, returnId); |
| 2204 | returnId = builder.createLoad(copyId); |
| 2205 | } |
| 2206 | builder.makeReturn(false, returnId); |
| 2207 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2208 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2209 | |
| 2210 | builder.clearAccessChain(); |
| 2211 | break; |
| 2212 | |
| 2213 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2214 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2215 | break; |
| 2216 | } |
| 2217 | |
| 2218 | return false; |
| 2219 | } |
| 2220 | |
| 2221 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 2222 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2223 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2224 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2225 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 2226 | if (node->getQualifier().isConstant()) { |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2227 | return createSpvConstant(*node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2231 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2232 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 2233 | |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2234 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2235 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 2236 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 2237 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2238 | if (contains16BitType) { |
| 2239 | if (storageClass == spv::StorageClassInput || storageClass == spv::StorageClassOutput) { |
| 2240 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2241 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
| 2242 | } else if (storageClass == spv::StorageClassPushConstant) { |
| 2243 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2244 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 2245 | } else if (storageClass == spv::StorageClassUniform) { |
| 2246 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2247 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 2248 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2249 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 2250 | } |
| 2251 | } |
| 2252 | #endif |
| 2253 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2254 | const char* name = node->getName().c_str(); |
| 2255 | if (glslang::IsAnonymous(name)) |
| 2256 | name = ""; |
| 2257 | |
| 2258 | return builder.createVariable(storageClass, spvType, name); |
| 2259 | } |
| 2260 | |
| 2261 | // Return type Id of the sampled type. |
| 2262 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 2263 | { |
| 2264 | switch (sampler.type) { |
| 2265 | case glslang::EbtFloat: return builder.makeFloatType(32); |
| 2266 | case glslang::EbtInt: return builder.makeIntType(32); |
| 2267 | case glslang::EbtUint: return builder.makeUintType(32); |
| 2268 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2269 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2270 | return builder.makeFloatType(32); |
| 2271 | } |
| 2272 | } |
| 2273 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2274 | // If node is a swizzle operation, return the type that should be used if |
| 2275 | // the swizzle base is first consumed by another operation, before the swizzle |
| 2276 | // is applied. |
| 2277 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 2278 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2279 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2280 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2281 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 2282 | else |
| 2283 | return spv::NoType; |
| 2284 | } |
| 2285 | |
| 2286 | // When inverting a swizzle with a parent op, this function |
| 2287 | // will apply the swizzle operation to a completed parent operation. |
| 2288 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 2289 | { |
| 2290 | std::vector<unsigned> swizzle; |
| 2291 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 2292 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 2293 | } |
| 2294 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2295 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 2296 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 2297 | { |
| 2298 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 2299 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 2300 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2301 | } |
| 2302 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2303 | // Convert from a glslang type to an SPV type, by calling into a |
| 2304 | // recursive version of this function. This establishes the inherited |
| 2305 | // layout state rooted from the top-level type. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2306 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type) |
| 2307 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2308 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier()); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | // 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] | 2312 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2313 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2314 | 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] | 2315 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2316 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2317 | |
| 2318 | switch (type.getBasicType()) { |
| 2319 | case glslang::EbtVoid: |
| 2320 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2321 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2322 | break; |
| 2323 | case glslang::EbtFloat: |
| 2324 | spvType = builder.makeFloatType(32); |
| 2325 | break; |
| 2326 | case glslang::EbtDouble: |
| 2327 | spvType = builder.makeFloatType(64); |
| 2328 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2329 | #ifdef AMD_EXTENSIONS |
| 2330 | case glslang::EbtFloat16: |
| 2331 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2332 | spvType = builder.makeFloatType(16); |
| 2333 | break; |
| 2334 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2335 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2336 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 2337 | // a 32-bit int where non-0 means true. |
| 2338 | if (explicitLayout != glslang::ElpNone) |
| 2339 | spvType = builder.makeUintType(32); |
| 2340 | else |
| 2341 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2342 | break; |
| 2343 | case glslang::EbtInt: |
| 2344 | spvType = builder.makeIntType(32); |
| 2345 | break; |
| 2346 | case glslang::EbtUint: |
| 2347 | spvType = builder.makeUintType(32); |
| 2348 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2349 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2350 | spvType = builder.makeIntType(64); |
| 2351 | break; |
| 2352 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2353 | spvType = builder.makeUintType(64); |
| 2354 | break; |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2355 | #ifdef AMD_EXTENSIONS |
| 2356 | case glslang::EbtInt16: |
| 2357 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 2358 | spvType = builder.makeIntType(16); |
| 2359 | break; |
| 2360 | case glslang::EbtUint16: |
| 2361 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 2362 | spvType = builder.makeUintType(16); |
| 2363 | break; |
| 2364 | #endif |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2365 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 2366 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2367 | spvType = builder.makeUintType(32); |
| 2368 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2369 | case glslang::EbtSampler: |
| 2370 | { |
| 2371 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2372 | if (sampler.sampler) { |
| 2373 | // pure sampler |
| 2374 | spvType = builder.makeSamplerType(); |
| 2375 | } else { |
| 2376 | // an image is present, make its type |
| 2377 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 2378 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 2379 | if (sampler.combined) { |
| 2380 | // already has both image and sampler, make the combined type |
| 2381 | spvType = builder.makeSampledImageType(spvType); |
| 2382 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2383 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2384 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2385 | break; |
| 2386 | case glslang::EbtStruct: |
| 2387 | case glslang::EbtBlock: |
| 2388 | { |
| 2389 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2390 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2391 | |
| 2392 | // Try to share structs for different layouts, but not yet for other |
| 2393 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2394 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2395 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2396 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2397 | break; |
| 2398 | |
| 2399 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2400 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2401 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 2402 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2403 | } |
| 2404 | break; |
| 2405 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2406 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2407 | break; |
| 2408 | } |
| 2409 | |
| 2410 | if (type.isMatrix()) |
| 2411 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 2412 | else { |
| 2413 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 2414 | if (type.getVectorSize() > 1) |
| 2415 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 2416 | } |
| 2417 | |
| 2418 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2419 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 2420 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2421 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2422 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2423 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 2424 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2425 | // Use a dummy glslang type for querying internal strides of |
| 2426 | // arrays of arrays, but using just a one-dimensional array. |
| 2427 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
| 2428 | while (simpleArrayType.getArraySizes().getNumDims() > 1) |
| 2429 | simpleArrayType.getArraySizes().dereference(); |
| 2430 | |
| 2431 | // Will compute the higher-order strides here, rather than making a whole |
| 2432 | // pile of types and doing repetitive recursion on their contents. |
| 2433 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 2434 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2435 | |
| 2436 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2437 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2438 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2439 | if (stride > 0) |
| 2440 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2441 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2442 | } |
| 2443 | } else { |
| 2444 | // single-dimensional array, and don't yet have stride |
| 2445 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2446 | // 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] | 2447 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 2448 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2449 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2450 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2451 | // Do the outer dimension, which might not be known for a runtime-sized array |
| 2452 | if (type.isRuntimeSizedArray()) { |
| 2453 | spvType = builder.makeRuntimeArray(spvType); |
| 2454 | } else { |
| 2455 | assert(type.getOuterArraySize() > 0); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2456 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2457 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2458 | if (stride > 0) |
| 2459 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | return spvType; |
| 2463 | } |
| 2464 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2465 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 2466 | // |
| 2467 | // Identify interface members that don't have their required extension turned on. |
| 2468 | // |
| 2469 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 2470 | { |
| 2471 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 2472 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2473 | if (member.getFieldName() == "gl_ViewportMask" && |
| 2474 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 2475 | return true; |
| 2476 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 2477 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2478 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2479 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 2480 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2481 | return true; |
| 2482 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 2483 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2484 | return true; |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2485 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 2486 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2487 | return true; |
John Kessenich | d6be6da | 2017-08-17 23:49:39 -0600 | [diff] [blame] | 2488 | if ((member.getFieldName() == "gl_ViewportIndex" || member.getFieldName() == "gl_Layer") && |
| 2489 | extensions.find(glslang::E_GL_ARB_shader_viewport_layer_array) == extensions.end() && |
John Kessenich | 786e879 | 2017-08-19 15:54:49 -0600 | [diff] [blame] | 2490 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
John Kessenich | d6be6da | 2017-08-17 23:49:39 -0600 | [diff] [blame] | 2491 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2492 | |
| 2493 | return false; |
| 2494 | }; |
| 2495 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2496 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 2497 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 2498 | // Mutually recursive with convertGlslangToSpvType(). |
| 2499 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 2500 | const glslang::TTypeList* glslangMembers, |
| 2501 | glslang::TLayoutPacking explicitLayout, |
| 2502 | const glslang::TQualifier& qualifier) |
| 2503 | { |
| 2504 | // Create a vector of struct types for SPIR-V to consume |
| 2505 | std::vector<spv::Id> spvMembers; |
| 2506 | 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] | 2507 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2508 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2509 | if (glslangMember.hiddenMember()) { |
| 2510 | ++memberDelta; |
| 2511 | if (type.getBasicType() == glslang::EbtBlock) |
| 2512 | memberRemapper[glslangMembers][i] = -1; |
| 2513 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2514 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2515 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2516 | if (filterMember(glslangMember)) |
| 2517 | continue; |
| 2518 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2519 | // modify just this child's view of the qualifier |
| 2520 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2521 | InheritQualifiers(memberQualifier, qualifier); |
| 2522 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2523 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2524 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2525 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2526 | |
| 2527 | // recurse |
| 2528 | spvMembers.push_back(convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier)); |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | // Make the SPIR-V type |
| 2533 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2534 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2535 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 2536 | |
| 2537 | // Decorate it |
| 2538 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 2539 | |
| 2540 | return spvType; |
| 2541 | } |
| 2542 | |
| 2543 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 2544 | const glslang::TTypeList* glslangMembers, |
| 2545 | glslang::TLayoutPacking explicitLayout, |
| 2546 | const glslang::TQualifier& qualifier, |
| 2547 | spv::Id spvType) |
| 2548 | { |
| 2549 | // Name and decorate the non-hidden members |
| 2550 | int offset = -1; |
| 2551 | int locationOffset = 0; // for use within the members of this struct |
| 2552 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2553 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2554 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2555 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2556 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2557 | if (filterMember(glslangMember)) |
| 2558 | continue; |
| 2559 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2560 | |
| 2561 | // modify just this child's view of the qualifier |
| 2562 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2563 | InheritQualifiers(memberQualifier, qualifier); |
| 2564 | |
| 2565 | // using -1 above to indicate a hidden member |
| 2566 | if (member >= 0) { |
| 2567 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 2568 | addMemberDecoration(spvType, member, TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 2569 | addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 2570 | // 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] | 2571 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 2572 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 2573 | if (type.getBasicType() == glslang::EbtBlock || |
| 2574 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2575 | addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 2576 | addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
| 2577 | } |
| 2578 | } |
| 2579 | addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
| 2580 | |
Rex Xu | 286ca43 | 2017-07-27 14:33:16 +0800 | [diff] [blame] | 2581 | if (type.getBasicType() == glslang::EbtBlock && |
| 2582 | qualifier.storage == glslang::EvqBuffer) { |
| 2583 | // Add memory decorations only to top-level members of shader storage block |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2584 | std::vector<spv::Decoration> memory; |
| 2585 | TranslateMemoryDecoration(memberQualifier, memory); |
| 2586 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 2587 | addMemberDecoration(spvType, member, memory[i]); |
| 2588 | } |
| 2589 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2590 | // Location assignment was already completed correctly by the front end, |
| 2591 | // just track whether a member needs to be decorated. |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2592 | // Ignore member locations if the container is an array, as that's |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2593 | // ill-specified and decisions have been made to not allow this. |
| 2594 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 2595 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2596 | |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2597 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 2598 | locationOffset += glslangIntermediate->computeTypeLocationSize(glslangMember); |
| 2599 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2600 | // component, XFB, others |
| 2601 | if (glslangMember.getQualifier().hasComponent()) |
| 2602 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, glslangMember.getQualifier().layoutComponent); |
| 2603 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 2604 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, glslangMember.getQualifier().layoutXfbOffset); |
| 2605 | else if (explicitLayout != glslang::ElpNone) { |
| 2606 | // figure out what to do with offset, which is accumulating |
| 2607 | int nextOffset; |
| 2608 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 2609 | if (offset >= 0) |
| 2610 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 2611 | offset = nextOffset; |
| 2612 | } |
| 2613 | |
| 2614 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 2615 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
| 2616 | |
| 2617 | // built-in variable decorations |
| 2618 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 2619 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2620 | addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2621 | |
| 2622 | #ifdef NV_EXTENSIONS |
| 2623 | if (builtIn == spv::BuiltInLayer) { |
| 2624 | // SPV_NV_viewport_array2 extension |
| 2625 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 2626 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 2627 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 2628 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 2629 | } |
| 2630 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 2631 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 2632 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 2633 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 2634 | } |
| 2635 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 2636 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 2637 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 2638 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 2639 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 2640 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2641 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | // Decorate the structure |
| 2646 | addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 2647 | addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2648 | if (type.getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
| 2649 | builder.addCapability(spv::CapabilityGeometryStreams); |
| 2650 | builder.addDecoration(spvType, spv::DecorationStream, type.getQualifier().layoutStream); |
| 2651 | } |
| 2652 | if (glslangIntermediate->getXfbMode()) { |
| 2653 | builder.addCapability(spv::CapabilityTransformFeedback); |
| 2654 | if (type.getQualifier().hasXfbStride()) |
| 2655 | builder.addDecoration(spvType, spv::DecorationXfbStride, type.getQualifier().layoutXfbStride); |
| 2656 | if (type.getQualifier().hasXfbBuffer()) |
| 2657 | builder.addDecoration(spvType, spv::DecorationXfbBuffer, type.getQualifier().layoutXfbBuffer); |
| 2658 | } |
| 2659 | } |
| 2660 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2661 | // Turn the expression forming the array size into an id. |
| 2662 | // This is not quite trivial, because of specialization constants. |
| 2663 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 2664 | // a specialization constant expression is. |
| 2665 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 2666 | { |
| 2667 | // First, see if this is sized with a node, meaning a specialization constant: |
| 2668 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 2669 | if (specNode != nullptr) { |
| 2670 | builder.clearAccessChain(); |
| 2671 | specNode->traverse(this); |
| 2672 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 2673 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2674 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2675 | // Otherwise, need a compile-time (front end) size, get it: |
| 2676 | int size = arraySizes.getDimSize(dim); |
| 2677 | assert(size > 0); |
| 2678 | return builder.makeUintConstant(size); |
| 2679 | } |
| 2680 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2681 | // Wrap the builder's accessChainLoad to: |
| 2682 | // - localize handling of RelaxedPrecision |
| 2683 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 2684 | // (avoids unnecessary work and possible type punning for structures) |
| 2685 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2686 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 2687 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2688 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2689 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), nominalTypeId); |
| 2690 | |
| 2691 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2692 | if (type.getBasicType() == glslang::EbtBool) { |
| 2693 | if (builder.isScalarType(nominalTypeId)) { |
| 2694 | // Conversion for bool |
| 2695 | spv::Id boolType = builder.makeBoolType(); |
| 2696 | if (nominalTypeId != boolType) |
| 2697 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 2698 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2699 | // Conversion for bvec |
| 2700 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2701 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 2702 | if (nominalTypeId != bvecType) |
| 2703 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 2704 | } |
| 2705 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2706 | |
| 2707 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2710 | // Wrap the builder's accessChainStore to: |
| 2711 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2712 | // |
| 2713 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2714 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 2715 | { |
| 2716 | // Need to convert to abstract types when necessary |
| 2717 | if (type.getBasicType() == glslang::EbtBool) { |
| 2718 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2719 | |
| 2720 | if (builder.isScalarType(nominalTypeId)) { |
| 2721 | // Conversion for bool |
| 2722 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2723 | if (nominalTypeId != boolType) { |
| 2724 | // keep these outside arguments, for determinant order-of-evaluation |
| 2725 | spv::Id one = builder.makeUintConstant(1); |
| 2726 | spv::Id zero = builder.makeUintConstant(0); |
| 2727 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 2728 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2729 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2730 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2731 | // Conversion for bvec |
| 2732 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2733 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2734 | if (nominalTypeId != bvecType) { |
| 2735 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 2736 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 2737 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 2738 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2739 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2740 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 2741 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | |
| 2745 | builder.accessChainStore(rvalue); |
| 2746 | } |
| 2747 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2748 | // For storing when types match at the glslang level, but not might match at the |
| 2749 | // SPIR-V level. |
| 2750 | // |
| 2751 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2752 | // 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] | 2753 | // as in a member-decorated way. |
| 2754 | // |
| 2755 | // NOTE: This function can handle any store request; if it's not special it |
| 2756 | // simplifies to a simple OpStore. |
| 2757 | // |
| 2758 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 2759 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 2760 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2761 | // we only do the complex path here if it's an aggregate |
| 2762 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2763 | accessChainStore(type, rValue); |
| 2764 | return; |
| 2765 | } |
| 2766 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2767 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2768 | spv::Id rType = builder.getTypeId(rValue); |
| 2769 | spv::Id lValue = builder.accessChainGetLValue(); |
| 2770 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 2771 | if (lType == rType) { |
| 2772 | accessChainStore(type, rValue); |
| 2773 | return; |
| 2774 | } |
| 2775 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2776 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2777 | // where the two types were the same type in GLSL. This requires member |
| 2778 | // by member copy, recursively. |
| 2779 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2780 | // If an array, copy element by element. |
| 2781 | if (type.isArray()) { |
| 2782 | glslang::TType glslangElementType(type, 0); |
| 2783 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2784 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 2785 | // get the source member |
| 2786 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2787 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2788 | // set up the target storage |
| 2789 | builder.clearAccessChain(); |
| 2790 | builder.setAccessChainLValue(lValue); |
| 2791 | builder.accessChainPush(builder.makeIntConstant(index)); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2792 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2793 | // store the member |
| 2794 | multiTypeStore(glslangElementType, elementRValue); |
| 2795 | } |
| 2796 | } else { |
| 2797 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2798 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2799 | // loop over structure members |
| 2800 | const glslang::TTypeList& members = *type.getStruct(); |
| 2801 | for (int m = 0; m < (int)members.size(); ++m) { |
| 2802 | const glslang::TType& glslangMemberType = *members[m].type; |
| 2803 | |
| 2804 | // get the source member |
| 2805 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 2806 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 2807 | |
| 2808 | // set up the target storage |
| 2809 | builder.clearAccessChain(); |
| 2810 | builder.setAccessChainLValue(lValue); |
| 2811 | builder.accessChainPush(builder.makeIntConstant(m)); |
| 2812 | |
| 2813 | // store the member |
| 2814 | multiTypeStore(glslangMemberType, memberRValue); |
| 2815 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2816 | } |
| 2817 | } |
| 2818 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2819 | // Decide whether or not this type should be |
| 2820 | // decorated with offsets and strides, and if so |
| 2821 | // whether std140 or std430 rules should be applied. |
| 2822 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2823 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2824 | // has to be a block |
| 2825 | if (type.getBasicType() != glslang::EbtBlock) |
| 2826 | return glslang::ElpNone; |
| 2827 | |
| 2828 | // has to be a uniform or buffer block |
| 2829 | if (type.getQualifier().storage != glslang::EvqUniform && |
| 2830 | type.getQualifier().storage != glslang::EvqBuffer) |
| 2831 | return glslang::ElpNone; |
| 2832 | |
| 2833 | // return the layout to use |
| 2834 | switch (type.getQualifier().layoutPacking) { |
| 2835 | case glslang::ElpStd140: |
| 2836 | case glslang::ElpStd430: |
| 2837 | return type.getQualifier().layoutPacking; |
| 2838 | default: |
| 2839 | return glslang::ElpNone; |
| 2840 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2841 | } |
| 2842 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2843 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2844 | 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] | 2845 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2846 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2847 | int stride; |
| 2848 | glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 2849 | |
| 2850 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2851 | } |
| 2852 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2853 | // 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] | 2854 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2855 | 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] | 2856 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2857 | glslang::TType elementType; |
| 2858 | elementType.shallowCopy(matrixType); |
| 2859 | elementType.clearArraySizes(); |
| 2860 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2861 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2862 | int stride; |
| 2863 | glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
| 2864 | |
| 2865 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2866 | } |
| 2867 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2868 | // Given a member type of a struct, realign the current offset for it, and compute |
| 2869 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 2870 | // on the next call. |
| 2871 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 2872 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 2873 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 2874 | 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] | 2875 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2876 | { |
| 2877 | // this will get a positive value when deemed necessary |
| 2878 | nextOffset = -1; |
| 2879 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2880 | // override anything in currentOffset with user-set offset |
| 2881 | if (memberType.getQualifier().hasOffset()) |
| 2882 | currentOffset = memberType.getQualifier().layoutOffset; |
| 2883 | |
| 2884 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 2885 | // in which case the following code does not matter. But, that's not quite right |
| 2886 | // once cross-compilation unit GLSL validation is done, as the original user |
| 2887 | // settings are needed in layoutOffset, and then the following will come into play. |
| 2888 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2889 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2890 | if (! memberType.getQualifier().hasOffset()) |
| 2891 | currentOffset = -1; |
| 2892 | |
| 2893 | return; |
| 2894 | } |
| 2895 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2896 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2897 | if (currentOffset < 0) |
| 2898 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2899 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2900 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 2901 | // but possibly not yet correctly aligned. |
| 2902 | |
| 2903 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2904 | int dummyStride; |
| 2905 | 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] | 2906 | |
| 2907 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 2908 | // TODO: make this consistent in early phases of code: |
| 2909 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 2910 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 2911 | // 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] | 2912 | if (glslangIntermediate->usingHlslOFfsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 2913 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 2914 | int dummySize; |
| 2915 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 2916 | if (componentAlignment <= 4) |
| 2917 | memberAlignment = componentAlignment; |
| 2918 | } |
| 2919 | |
| 2920 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2921 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 2922 | |
| 2923 | // Bump up to vec4 if there is a bad straddle |
| 2924 | if (glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
| 2925 | glslang::RoundToPow2(currentOffset, 16); |
| 2926 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2927 | nextOffset = currentOffset + memberSize; |
| 2928 | } |
| 2929 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2930 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2931 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2932 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 2933 | switch (glslangBuiltIn) |
| 2934 | { |
| 2935 | case glslang::EbvClipDistance: |
| 2936 | case glslang::EbvCullDistance: |
| 2937 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2938 | #ifdef NV_EXTENSIONS |
| 2939 | case glslang::EbvLayer: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 2940 | case glslang::EbvViewportIndex: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2941 | case glslang::EbvViewportMaskNV: |
| 2942 | case glslang::EbvSecondaryPositionNV: |
| 2943 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 2944 | case glslang::EbvPositionPerViewNV: |
| 2945 | case glslang::EbvViewportMaskPerViewNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2946 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2947 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 2948 | // Alternately, we could just call this for any glslang built-in, since the |
| 2949 | // capability already guards against duplicates. |
| 2950 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 2951 | break; |
| 2952 | default: |
| 2953 | // Capabilities were already generated when the struct was declared. |
| 2954 | break; |
| 2955 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2956 | } |
| 2957 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2958 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2959 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 2960 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 2964 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 2965 | { |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2966 | const auto getParamDecorations = [](std::vector<spv::Decoration>& decorations, const glslang::TType& type) { |
| 2967 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 2968 | if (paramPrecision != spv::NoPrecision) |
| 2969 | decorations.push_back(paramPrecision); |
John Kessenich | 961cd35 | 2017-07-18 02:58:06 -0600 | [diff] [blame] | 2970 | TranslateMemoryDecoration(type.getQualifier(), decorations); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2971 | }; |
| 2972 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2973 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 2974 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2975 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2976 | continue; |
| 2977 | |
| 2978 | // 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] | 2979 | // 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] | 2980 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2981 | // 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] | 2982 | // function. What it is an address of varies: |
| 2983 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2984 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 2985 | // 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] | 2986 | // |
| 2987 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 2988 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2989 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 2990 | // 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] | 2991 | |
| 2992 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2993 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2994 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 2995 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2996 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 2997 | glslangIntermediate->implicitThisName; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 2998 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2999 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3000 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3001 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 3002 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3003 | // can we pass by reference? |
| 3004 | if (paramType.containsOpaque() || // sampler, etc. |
John Kessenich | 4960baa | 2017-03-19 18:09:59 -0600 | [diff] [blame] | 3005 | (paramType.getBasicType() == glslang::EbtBlock && |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3006 | paramType.getQualifier().storage == glslang::EvqBuffer) || // SSBO |
John Kessenich | aa3c64c | 2017-03-28 09:52:38 -0600 | [diff] [blame] | 3007 | (p == 0 && implicitThis)) // implicit 'this' |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 3008 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
Jason Ekstrand | ed15ef1 | 2016-06-08 13:54:48 -0700 | [diff] [blame] | 3009 | else if (paramType.getQualifier().storage != glslang::EvqConstReadOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3010 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 3011 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3012 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3013 | getParamDecorations(paramDecorations[p], paramType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3014 | paramTypes.push_back(typeId); |
| 3015 | } |
| 3016 | |
| 3017 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3018 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 3019 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3020 | glslFunction->getName().c_str(), paramTypes, |
| 3021 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3022 | if (implicitThis) |
| 3023 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3024 | |
| 3025 | // Track function to emit/call later |
| 3026 | functionMap[glslFunction->getName().c_str()] = function; |
| 3027 | |
| 3028 | // Set the parameter id's |
| 3029 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3030 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 3031 | // give a name too |
| 3032 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 3033 | } |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | // Process all the initializers, while skipping the functions and link objects |
| 3038 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 3039 | { |
| 3040 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 3041 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 3042 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 3043 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 3044 | |
| 3045 | // 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] | 3046 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3047 | initializer->traverse(this); |
| 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | // Process all the functions, while skipping initializers. |
| 3053 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3054 | { |
| 3055 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3056 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 3057 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3058 | node->traverse(this); |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 3063 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3064 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3065 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3066 | currentFunction = functionMap[node->getName().c_str()]; |
| 3067 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3068 | builder.setBuildPoint(functionBlock); |
| 3069 | } |
| 3070 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3071 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3072 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3073 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3074 | |
| 3075 | glslang::TSampler sampler = {}; |
| 3076 | bool cubeCompare = false; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3077 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3078 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 3079 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 3080 | } |
| 3081 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3082 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 3083 | builder.clearAccessChain(); |
| 3084 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3085 | |
| 3086 | // Special case l-value operands |
| 3087 | bool lvalue = false; |
| 3088 | switch (node.getOp()) { |
| 3089 | case glslang::EOpImageAtomicAdd: |
| 3090 | case glslang::EOpImageAtomicMin: |
| 3091 | case glslang::EOpImageAtomicMax: |
| 3092 | case glslang::EOpImageAtomicAnd: |
| 3093 | case glslang::EOpImageAtomicOr: |
| 3094 | case glslang::EOpImageAtomicXor: |
| 3095 | case glslang::EOpImageAtomicExchange: |
| 3096 | case glslang::EOpImageAtomicCompSwap: |
| 3097 | if (i == 0) |
| 3098 | lvalue = true; |
| 3099 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3100 | case glslang::EOpSparseImageLoad: |
| 3101 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 3102 | lvalue = true; |
| 3103 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3104 | case glslang::EOpSparseTexture: |
| 3105 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 3106 | lvalue = true; |
| 3107 | break; |
| 3108 | case glslang::EOpSparseTextureClamp: |
| 3109 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 3110 | lvalue = true; |
| 3111 | break; |
| 3112 | case glslang::EOpSparseTextureLod: |
| 3113 | case glslang::EOpSparseTextureOffset: |
| 3114 | if (i == 3) |
| 3115 | lvalue = true; |
| 3116 | break; |
| 3117 | case glslang::EOpSparseTextureFetch: |
| 3118 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 3119 | lvalue = true; |
| 3120 | break; |
| 3121 | case glslang::EOpSparseTextureFetchOffset: |
| 3122 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 3123 | lvalue = true; |
| 3124 | break; |
| 3125 | case glslang::EOpSparseTextureLodOffset: |
| 3126 | case glslang::EOpSparseTextureGrad: |
| 3127 | case glslang::EOpSparseTextureOffsetClamp: |
| 3128 | if (i == 4) |
| 3129 | lvalue = true; |
| 3130 | break; |
| 3131 | case glslang::EOpSparseTextureGradOffset: |
| 3132 | case glslang::EOpSparseTextureGradClamp: |
| 3133 | if (i == 5) |
| 3134 | lvalue = true; |
| 3135 | break; |
| 3136 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3137 | if (i == 6) |
| 3138 | lvalue = true; |
| 3139 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3140 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3141 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 3142 | lvalue = true; |
| 3143 | break; |
| 3144 | case glslang::EOpSparseTextureGatherOffset: |
| 3145 | case glslang::EOpSparseTextureGatherOffsets: |
| 3146 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 3147 | lvalue = true; |
| 3148 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3149 | #ifdef AMD_EXTENSIONS |
| 3150 | case glslang::EOpSparseTextureGatherLod: |
| 3151 | if (i == 3) |
| 3152 | lvalue = true; |
| 3153 | break; |
| 3154 | case glslang::EOpSparseTextureGatherLodOffset: |
| 3155 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 3156 | if (i == 4) |
| 3157 | lvalue = true; |
| 3158 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3159 | case glslang::EOpSparseImageLoadLod: |
| 3160 | if (i == 3) |
| 3161 | lvalue = true; |
| 3162 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3163 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3164 | default: |
| 3165 | break; |
| 3166 | } |
| 3167 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3168 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3169 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3170 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3171 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3172 | } |
| 3173 | } |
| 3174 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3175 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3176 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3177 | builder.clearAccessChain(); |
| 3178 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3179 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3180 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3181 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3182 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 3183 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3184 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3185 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3186 | |
| 3187 | builder.setLine(node->getLoc().line); |
| 3188 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3189 | auto resultType = [&node,this]{ return convertGlslangToSpvType(node->getType()); }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3190 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3191 | // Process a GLSL texturing op (will be SPV image) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3192 | const glslang::TSampler sampler = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType().getSampler() |
| 3193 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType().getSampler(); |
| 3194 | std::vector<spv::Id> arguments; |
| 3195 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3196 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3197 | else |
| 3198 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 3199 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3200 | |
| 3201 | spv::Builder::TextureParameters params = { }; |
| 3202 | params.sampler = arguments[0]; |
| 3203 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3204 | glslang::TCrackedTextureOp cracked; |
| 3205 | node->crackTexture(sampler, cracked); |
| 3206 | |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3207 | const bool isUnsignedResult = |
| 3208 | node->getType().getBasicType() == glslang::EbtUint64 || |
| 3209 | node->getType().getBasicType() == glslang::EbtUint; |
| 3210 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3211 | // Check for queries |
| 3212 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3213 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 3214 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 3215 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3216 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3217 | switch (node->getOp()) { |
| 3218 | case glslang::EOpImageQuerySize: |
| 3219 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3220 | if (arguments.size() > 1) { |
| 3221 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3222 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3223 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3224 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3225 | case glslang::EOpImageQuerySamples: |
| 3226 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3227 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3228 | case glslang::EOpTextureQueryLod: |
| 3229 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3230 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3231 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3232 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3233 | case glslang::EOpSparseTexelsResident: |
| 3234 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3235 | default: |
| 3236 | assert(0); |
| 3237 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3238 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3239 | } |
| 3240 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3241 | // Check for image functions other than queries |
| 3242 | if (node->isImage()) { |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3243 | std::vector<spv::Id> operands; |
| 3244 | auto opIt = arguments.begin(); |
| 3245 | operands.push_back(*(opIt++)); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3246 | |
| 3247 | // Handle subpass operations |
| 3248 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 3249 | // built-in function. |
| 3250 | if (cracked.subpass) { |
| 3251 | // add on the (0,0) coordinate |
| 3252 | spv::Id zero = builder.makeIntConstant(0); |
| 3253 | std::vector<spv::Id> comps; |
| 3254 | comps.push_back(zero); |
| 3255 | comps.push_back(zero); |
| 3256 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 3257 | if (sampler.ms) { |
| 3258 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3259 | operands.push_back(*(opIt++)); |
| 3260 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3261 | return builder.createOp(spv::OpImageRead, resultType(), operands); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3262 | } |
| 3263 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3264 | operands.push_back(*(opIt++)); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3265 | #ifdef AMD_EXTENSIONS |
| 3266 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 3267 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3268 | if (node->getOp() == glslang::EOpImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3269 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3270 | if (sampler.ms) { |
| 3271 | operands.push_back(spv::ImageOperandsSampleMask); |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3272 | operands.push_back(*opIt); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3273 | #ifdef AMD_EXTENSIONS |
| 3274 | } else if (cracked.lod) { |
| 3275 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3276 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3277 | |
| 3278 | operands.push_back(spv::ImageOperandsLodMask); |
| 3279 | operands.push_back(*opIt); |
| 3280 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3281 | } |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3282 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3283 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3284 | return builder.createOp(spv::OpImageRead, resultType(), operands); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3285 | #ifdef AMD_EXTENSIONS |
| 3286 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 3287 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3288 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3289 | #endif |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3290 | if (sampler.ms) { |
| 3291 | operands.push_back(*(opIt + 1)); |
| 3292 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3293 | operands.push_back(*opIt); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3294 | #ifdef AMD_EXTENSIONS |
| 3295 | } else if (cracked.lod) { |
| 3296 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3297 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3298 | |
| 3299 | operands.push_back(*(opIt + 1)); |
| 3300 | operands.push_back(spv::ImageOperandsLodMask); |
| 3301 | operands.push_back(*opIt); |
| 3302 | #endif |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3303 | } else |
| 3304 | operands.push_back(*opIt); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3305 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3306 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3307 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3308 | return spv::NoResult; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3309 | #ifdef AMD_EXTENSIONS |
| 3310 | } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 3311 | #else |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3312 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3313 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3314 | builder.addCapability(spv::CapabilitySparseResidency); |
| 3315 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3316 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 3317 | |
| 3318 | if (sampler.ms) { |
| 3319 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3320 | operands.push_back(*opIt++); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3321 | #ifdef AMD_EXTENSIONS |
| 3322 | } else if (cracked.lod) { |
| 3323 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3324 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3325 | |
| 3326 | operands.push_back(spv::ImageOperandsLodMask); |
| 3327 | operands.push_back(*opIt++); |
| 3328 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | // Create the return type that was a special structure |
| 3332 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3333 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3334 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 3335 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 3336 | |
| 3337 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 3338 | |
| 3339 | // Decode the return type |
| 3340 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 3341 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3342 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3343 | // Process image atomic operations |
| 3344 | |
| 3345 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 3346 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3347 | 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] | 3348 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3349 | spv::Id resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3350 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3351 | |
| 3352 | std::vector<spv::Id> operands; |
| 3353 | operands.push_back(pointer); |
| 3354 | for (; opIt != arguments.end(); ++opIt) |
| 3355 | operands.push_back(*opIt); |
| 3356 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3357 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3362 | bool sparse = node->isSparseTexture(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 3363 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 3364 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3365 | // check for bias argument |
| 3366 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3367 | #ifdef AMD_EXTENSIONS |
| 3368 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 3369 | #else |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 3370 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3371 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3372 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3373 | #ifdef AMD_EXTENSIONS |
| 3374 | if (cracked.gather) |
| 3375 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
| 3376 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3377 | if (cracked.offset) |
| 3378 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3379 | #ifdef AMD_EXTENSIONS |
| 3380 | else if (cracked.offsets) |
| 3381 | ++nonBiasArgCount; |
| 3382 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3383 | if (cracked.grad) |
| 3384 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3385 | if (cracked.lodClamp) |
| 3386 | ++nonBiasArgCount; |
| 3387 | if (sparse) |
| 3388 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3389 | |
| 3390 | if ((int)arguments.size() > nonBiasArgCount) |
| 3391 | bias = true; |
| 3392 | } |
| 3393 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 3394 | // See if the sampler param should really be just the SPV image part |
| 3395 | if (cracked.fetch) { |
| 3396 | // a fetch needs to have the image extracted first |
| 3397 | if (builder.isSampledImage(params.sampler)) |
| 3398 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 3399 | } |
| 3400 | |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3401 | #ifdef AMD_EXTENSIONS |
| 3402 | if (cracked.gather) { |
| 3403 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 3404 | if (bias || cracked.lod || |
| 3405 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 3406 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 3407 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3408 | } |
| 3409 | } |
| 3410 | #endif |
| 3411 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3412 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3413 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3414 | params.coords = arguments[1]; |
| 3415 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3416 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3417 | |
| 3418 | // sort out where Dref is coming from |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3419 | if (cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3420 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3421 | ++extraArgs; |
| 3422 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3423 | params.Dref = arguments[2]; |
| 3424 | ++extraArgs; |
| 3425 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3426 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3427 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3428 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3429 | 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] | 3430 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3431 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 3432 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3433 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 3434 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3435 | |
| 3436 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3437 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 3438 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3439 | ++extraArgs; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3440 | } else if (glslangIntermediate->getStage() != EShLangFragment) { |
| 3441 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 3442 | noImplicitLod = true; |
| 3443 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3444 | |
| 3445 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3446 | if (sampler.ms) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 3447 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3448 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3449 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3450 | |
| 3451 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3452 | if (cracked.grad) { |
| 3453 | params.gradX = arguments[2 + extraArgs]; |
| 3454 | params.gradY = arguments[3 + extraArgs]; |
| 3455 | extraArgs += 2; |
| 3456 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3457 | |
| 3458 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3459 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3460 | params.offset = arguments[2 + extraArgs]; |
| 3461 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3462 | } else if (cracked.offsets) { |
| 3463 | params.offsets = arguments[2 + extraArgs]; |
| 3464 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3465 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3466 | |
| 3467 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3468 | if (cracked.lodClamp) { |
| 3469 | params.lodClamp = arguments[2 + extraArgs]; |
| 3470 | ++extraArgs; |
| 3471 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3472 | |
| 3473 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3474 | if (sparse) { |
| 3475 | params.texelOut = arguments[2 + extraArgs]; |
| 3476 | ++extraArgs; |
| 3477 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3478 | |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3479 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3480 | if (cracked.gather && ! sampler.shadow) { |
| 3481 | // default component is 0, if missing, otherwise an argument |
| 3482 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3483 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3484 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3485 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3486 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | // bias |
| 3490 | if (bias) { |
| 3491 | params.bias = arguments[2 + extraArgs]; |
| 3492 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3493 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3494 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 3495 | // projective component (might not to move) |
| 3496 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 3497 | // are divided by the last component of P." |
| 3498 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 3499 | // unused components will appear after all used components." |
| 3500 | if (cracked.proj) { |
| 3501 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 3502 | int projTargetComp; |
| 3503 | switch (sampler.dim) { |
| 3504 | case glslang::Esd1D: projTargetComp = 1; break; |
| 3505 | case glslang::Esd2D: projTargetComp = 2; break; |
| 3506 | case glslang::EsdRect: projTargetComp = 2; break; |
| 3507 | default: projTargetComp = projSourceComp; break; |
| 3508 | } |
| 3509 | // copy the projective coordinate if we have to |
| 3510 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3511 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 3512 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 3513 | projSourceComp); |
| 3514 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 3515 | builder.getTypeId(params.coords), projTargetComp); |
| 3516 | } |
| 3517 | } |
| 3518 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3519 | return builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 3523 | { |
| 3524 | // Grab the function's pointer from the previously created function |
| 3525 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 3526 | if (! function) |
| 3527 | return 0; |
| 3528 | |
| 3529 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 3530 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 3531 | |
LoopDawg | 7611792 | 2017-09-06 14:59:06 -0600 | [diff] [blame^] | 3532 | // Encapsulate lvalue logic, used in two places below, for safety. |
| 3533 | const auto isLValue = [](int qualifier, const glslang::TType& paramType) -> bool { |
| 3534 | return qualifier != glslang::EvqConstReadOnly || paramType.containsOpaque(); |
| 3535 | }; |
| 3536 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3537 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 3538 | // |
| 3539 | // These imply we need a four step process: |
| 3540 | // 1. Evaluate the arguments |
| 3541 | // 2. Allocate and make copies of in, out, and inout arguments |
| 3542 | // 3. Make the call |
| 3543 | // 4. Copy back the results |
| 3544 | |
| 3545 | // 1. Evaluate the arguments |
| 3546 | std::vector<spv::Builder::AccessChain> lValues; |
| 3547 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3548 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3549 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3550 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3551 | // build l-value |
| 3552 | builder.clearAccessChain(); |
| 3553 | glslangArgs[a]->traverse(this); |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3554 | argTypes.push_back(¶mType); |
John Kessenich | 1176530 | 2016-07-31 12:39:46 -0600 | [diff] [blame] | 3555 | // keep outputs and opaque objects as l-values, evaluate input-only as r-values |
LoopDawg | 7611792 | 2017-09-06 14:59:06 -0600 | [diff] [blame^] | 3556 | if (isLValue(qualifiers[a], paramType)) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3557 | // save l-value |
| 3558 | lValues.push_back(builder.getAccessChain()); |
| 3559 | } else { |
| 3560 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3561 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 3566 | // copy the original into that space. |
| 3567 | // |
| 3568 | // Also, build up the list of actual arguments to pass in for the call |
| 3569 | int lValueCount = 0; |
| 3570 | int rValueCount = 0; |
| 3571 | std::vector<spv::Id> spvArgs; |
| 3572 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3573 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3574 | spv::Id arg; |
steve-lunarg | dd8287a | 2017-02-23 18:04:12 -0700 | [diff] [blame] | 3575 | if (paramType.containsOpaque() || |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3576 | (paramType.getBasicType() == glslang::EbtBlock && qualifiers[a] == glslang::EvqBuffer) || |
| 3577 | (a == 0 && function->hasImplicitThis())) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3578 | builder.setAccessChain(lValues[lValueCount]); |
| 3579 | arg = builder.accessChainGetLValue(); |
| 3580 | ++lValueCount; |
LoopDawg | 7611792 | 2017-09-06 14:59:06 -0600 | [diff] [blame^] | 3581 | } else if (isLValue(qualifiers[a], paramType)) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3582 | // need space to hold the copy |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3583 | arg = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(paramType), "param"); |
| 3584 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 3585 | // need to copy the input into output space |
| 3586 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3587 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3588 | builder.clearAccessChain(); |
| 3589 | builder.setAccessChainLValue(arg); |
| 3590 | multiTypeStore(paramType, copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3591 | } |
| 3592 | ++lValueCount; |
| 3593 | } else { |
| 3594 | arg = rValues[rValueCount]; |
| 3595 | ++rValueCount; |
| 3596 | } |
| 3597 | spvArgs.push_back(arg); |
| 3598 | } |
| 3599 | |
| 3600 | // 3. Make the call. |
| 3601 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3602 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3603 | |
| 3604 | // 4. Copy back out an "out" arguments. |
| 3605 | lValueCount = 0; |
| 3606 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3607 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
LoopDawg | 7611792 | 2017-09-06 14:59:06 -0600 | [diff] [blame^] | 3608 | if (isLValue(qualifiers[a], paramType)) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3609 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 3610 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 3611 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3612 | multiTypeStore(paramType, copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3613 | } |
| 3614 | ++lValueCount; |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | return result; |
| 3619 | } |
| 3620 | |
| 3621 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3622 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, spv::Decoration precision, |
| 3623 | spv::Decoration noContraction, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3624 | spv::Id typeId, spv::Id left, spv::Id right, |
| 3625 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 3626 | { |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3627 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3628 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64 || typeProxy == glslang::EbtUint16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3629 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 3630 | #else |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3631 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3632 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3633 | #endif |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3634 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3635 | |
| 3636 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3637 | 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] | 3638 | bool comparison = false; |
| 3639 | |
| 3640 | switch (op) { |
| 3641 | case glslang::EOpAdd: |
| 3642 | case glslang::EOpAddAssign: |
| 3643 | if (isFloat) |
| 3644 | binOp = spv::OpFAdd; |
| 3645 | else |
| 3646 | binOp = spv::OpIAdd; |
| 3647 | break; |
| 3648 | case glslang::EOpSub: |
| 3649 | case glslang::EOpSubAssign: |
| 3650 | if (isFloat) |
| 3651 | binOp = spv::OpFSub; |
| 3652 | else |
| 3653 | binOp = spv::OpISub; |
| 3654 | break; |
| 3655 | case glslang::EOpMul: |
| 3656 | case glslang::EOpMulAssign: |
| 3657 | if (isFloat) |
| 3658 | binOp = spv::OpFMul; |
| 3659 | else |
| 3660 | binOp = spv::OpIMul; |
| 3661 | break; |
| 3662 | case glslang::EOpVectorTimesScalar: |
| 3663 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 3664 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3665 | if (builder.isVector(right)) |
| 3666 | std::swap(left, right); |
| 3667 | assert(builder.isScalar(right)); |
| 3668 | needMatchingVectors = false; |
| 3669 | binOp = spv::OpVectorTimesScalar; |
| 3670 | } else |
| 3671 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3672 | break; |
| 3673 | case glslang::EOpVectorTimesMatrix: |
| 3674 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3675 | binOp = spv::OpVectorTimesMatrix; |
| 3676 | break; |
| 3677 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3678 | binOp = spv::OpMatrixTimesVector; |
| 3679 | break; |
| 3680 | case glslang::EOpMatrixTimesScalar: |
| 3681 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3682 | binOp = spv::OpMatrixTimesScalar; |
| 3683 | break; |
| 3684 | case glslang::EOpMatrixTimesMatrix: |
| 3685 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3686 | binOp = spv::OpMatrixTimesMatrix; |
| 3687 | break; |
| 3688 | case glslang::EOpOuterProduct: |
| 3689 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3690 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3691 | break; |
| 3692 | |
| 3693 | case glslang::EOpDiv: |
| 3694 | case glslang::EOpDivAssign: |
| 3695 | if (isFloat) |
| 3696 | binOp = spv::OpFDiv; |
| 3697 | else if (isUnsigned) |
| 3698 | binOp = spv::OpUDiv; |
| 3699 | else |
| 3700 | binOp = spv::OpSDiv; |
| 3701 | break; |
| 3702 | case glslang::EOpMod: |
| 3703 | case glslang::EOpModAssign: |
| 3704 | if (isFloat) |
| 3705 | binOp = spv::OpFMod; |
| 3706 | else if (isUnsigned) |
| 3707 | binOp = spv::OpUMod; |
| 3708 | else |
| 3709 | binOp = spv::OpSMod; |
| 3710 | break; |
| 3711 | case glslang::EOpRightShift: |
| 3712 | case glslang::EOpRightShiftAssign: |
| 3713 | if (isUnsigned) |
| 3714 | binOp = spv::OpShiftRightLogical; |
| 3715 | else |
| 3716 | binOp = spv::OpShiftRightArithmetic; |
| 3717 | break; |
| 3718 | case glslang::EOpLeftShift: |
| 3719 | case glslang::EOpLeftShiftAssign: |
| 3720 | binOp = spv::OpShiftLeftLogical; |
| 3721 | break; |
| 3722 | case glslang::EOpAnd: |
| 3723 | case glslang::EOpAndAssign: |
| 3724 | binOp = spv::OpBitwiseAnd; |
| 3725 | break; |
| 3726 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3727 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3728 | binOp = spv::OpLogicalAnd; |
| 3729 | break; |
| 3730 | case glslang::EOpInclusiveOr: |
| 3731 | case glslang::EOpInclusiveOrAssign: |
| 3732 | binOp = spv::OpBitwiseOr; |
| 3733 | break; |
| 3734 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3735 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3736 | binOp = spv::OpLogicalOr; |
| 3737 | break; |
| 3738 | case glslang::EOpExclusiveOr: |
| 3739 | case glslang::EOpExclusiveOrAssign: |
| 3740 | binOp = spv::OpBitwiseXor; |
| 3741 | break; |
| 3742 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3743 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3744 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3745 | break; |
| 3746 | |
| 3747 | case glslang::EOpLessThan: |
| 3748 | case glslang::EOpGreaterThan: |
| 3749 | case glslang::EOpLessThanEqual: |
| 3750 | case glslang::EOpGreaterThanEqual: |
| 3751 | case glslang::EOpEqual: |
| 3752 | case glslang::EOpNotEqual: |
| 3753 | case glslang::EOpVectorEqual: |
| 3754 | case glslang::EOpVectorNotEqual: |
| 3755 | comparison = true; |
| 3756 | break; |
| 3757 | default: |
| 3758 | break; |
| 3759 | } |
| 3760 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3761 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3762 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3763 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3764 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3765 | return createBinaryMatrixOperation(binOp, precision, noContraction, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3766 | |
| 3767 | // 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] | 3768 | if (needMatchingVectors) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3769 | builder.promoteScalar(precision, left, right); |
| 3770 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3771 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 3772 | addDecoration(result, noContraction); |
| 3773 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | if (! comparison) |
| 3777 | return 0; |
| 3778 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3779 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3780 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 3781 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
| 3782 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 3783 | return builder.createCompositeCompare(precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3784 | |
| 3785 | switch (op) { |
| 3786 | case glslang::EOpLessThan: |
| 3787 | if (isFloat) |
| 3788 | binOp = spv::OpFOrdLessThan; |
| 3789 | else if (isUnsigned) |
| 3790 | binOp = spv::OpULessThan; |
| 3791 | else |
| 3792 | binOp = spv::OpSLessThan; |
| 3793 | break; |
| 3794 | case glslang::EOpGreaterThan: |
| 3795 | if (isFloat) |
| 3796 | binOp = spv::OpFOrdGreaterThan; |
| 3797 | else if (isUnsigned) |
| 3798 | binOp = spv::OpUGreaterThan; |
| 3799 | else |
| 3800 | binOp = spv::OpSGreaterThan; |
| 3801 | break; |
| 3802 | case glslang::EOpLessThanEqual: |
| 3803 | if (isFloat) |
| 3804 | binOp = spv::OpFOrdLessThanEqual; |
| 3805 | else if (isUnsigned) |
| 3806 | binOp = spv::OpULessThanEqual; |
| 3807 | else |
| 3808 | binOp = spv::OpSLessThanEqual; |
| 3809 | break; |
| 3810 | case glslang::EOpGreaterThanEqual: |
| 3811 | if (isFloat) |
| 3812 | binOp = spv::OpFOrdGreaterThanEqual; |
| 3813 | else if (isUnsigned) |
| 3814 | binOp = spv::OpUGreaterThanEqual; |
| 3815 | else |
| 3816 | binOp = spv::OpSGreaterThanEqual; |
| 3817 | break; |
| 3818 | case glslang::EOpEqual: |
| 3819 | case glslang::EOpVectorEqual: |
| 3820 | if (isFloat) |
| 3821 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3822 | else if (isBool) |
| 3823 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3824 | else |
| 3825 | binOp = spv::OpIEqual; |
| 3826 | break; |
| 3827 | case glslang::EOpNotEqual: |
| 3828 | case glslang::EOpVectorNotEqual: |
| 3829 | if (isFloat) |
| 3830 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3831 | else if (isBool) |
| 3832 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3833 | else |
| 3834 | binOp = spv::OpINotEqual; |
| 3835 | break; |
| 3836 | default: |
| 3837 | break; |
| 3838 | } |
| 3839 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3840 | if (binOp != spv::OpNop) { |
| 3841 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 3842 | addDecoration(result, noContraction); |
| 3843 | return builder.setPrecision(result, precision); |
| 3844 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3845 | |
| 3846 | return 0; |
| 3847 | } |
| 3848 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3849 | // |
| 3850 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 3851 | // These can be any of: |
| 3852 | // |
| 3853 | // matrix * scalar |
| 3854 | // scalar * matrix |
| 3855 | // matrix * matrix linear algebraic |
| 3856 | // matrix * vector |
| 3857 | // vector * matrix |
| 3858 | // matrix * matrix componentwise |
| 3859 | // matrix op matrix op in {+, -, /} |
| 3860 | // matrix op scalar op in {+, -, /} |
| 3861 | // scalar op matrix op in {+, -, /} |
| 3862 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3863 | 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] | 3864 | { |
| 3865 | bool firstClass = true; |
| 3866 | |
| 3867 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 3868 | switch (op) { |
| 3869 | case spv::OpFDiv: |
| 3870 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 3871 | // turn matrix / scalar into a multiply... |
| 3872 | right = builder.createBinOp(spv::OpFDiv, builder.getTypeId(right), builder.makeFloatConstant(1.0F), right); |
| 3873 | op = spv::OpMatrixTimesScalar; |
| 3874 | } else |
| 3875 | firstClass = false; |
| 3876 | break; |
| 3877 | case spv::OpMatrixTimesScalar: |
| 3878 | if (builder.isMatrix(right)) |
| 3879 | std::swap(left, right); |
| 3880 | assert(builder.isScalar(right)); |
| 3881 | break; |
| 3882 | case spv::OpVectorTimesMatrix: |
| 3883 | assert(builder.isVector(left)); |
| 3884 | assert(builder.isMatrix(right)); |
| 3885 | break; |
| 3886 | case spv::OpMatrixTimesVector: |
| 3887 | assert(builder.isMatrix(left)); |
| 3888 | assert(builder.isVector(right)); |
| 3889 | break; |
| 3890 | case spv::OpMatrixTimesMatrix: |
| 3891 | assert(builder.isMatrix(left)); |
| 3892 | assert(builder.isMatrix(right)); |
| 3893 | break; |
| 3894 | default: |
| 3895 | firstClass = false; |
| 3896 | break; |
| 3897 | } |
| 3898 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3899 | if (firstClass) { |
| 3900 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
| 3901 | addDecoration(result, noContraction); |
| 3902 | return builder.setPrecision(result, precision); |
| 3903 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3904 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 3905 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3906 | // The result type of all of them is the same type as the (a) matrix operand. |
| 3907 | // The algorithm is to: |
| 3908 | // - break the matrix(es) into vectors |
| 3909 | // - smear any scalar to a vector |
| 3910 | // - do vector operations |
| 3911 | // - make a matrix out the vector results |
| 3912 | switch (op) { |
| 3913 | case spv::OpFAdd: |
| 3914 | case spv::OpFSub: |
| 3915 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 3916 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3917 | case spv::OpFMul: |
| 3918 | { |
| 3919 | // one time set up... |
| 3920 | bool leftMat = builder.isMatrix(left); |
| 3921 | bool rightMat = builder.isMatrix(right); |
| 3922 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 3923 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 3924 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 3925 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 3926 | std::vector<spv::Id> results; |
| 3927 | spv::Id smearVec = spv::NoResult; |
| 3928 | if (builder.isScalar(left)) |
| 3929 | smearVec = builder.smearScalar(precision, left, vecType); |
| 3930 | else if (builder.isScalar(right)) |
| 3931 | smearVec = builder.smearScalar(precision, right, vecType); |
| 3932 | |
| 3933 | // do each vector op |
| 3934 | for (unsigned int c = 0; c < numCols; ++c) { |
| 3935 | std::vector<unsigned int> indexes; |
| 3936 | indexes.push_back(c); |
| 3937 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 3938 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3939 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
| 3940 | addDecoration(result, noContraction); |
| 3941 | results.push_back(builder.setPrecision(result, precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3942 | } |
| 3943 | |
| 3944 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3945 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3946 | } |
| 3947 | default: |
| 3948 | assert(0); |
| 3949 | return spv::NoResult; |
| 3950 | } |
| 3951 | } |
| 3952 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3953 | 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] | 3954 | { |
| 3955 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 3956 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3957 | int libCall = -1; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3958 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3959 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64 || typeProxy == glslang::EbtUint16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3960 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 3961 | #else |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3962 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3963 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3964 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3965 | |
| 3966 | switch (op) { |
| 3967 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3968 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3969 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3970 | if (builder.isMatrixType(typeId)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3971 | return createUnaryMatrixOperation(unaryOp, precision, noContraction, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3972 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3973 | unaryOp = spv::OpSNegate; |
| 3974 | break; |
| 3975 | |
| 3976 | case glslang::EOpLogicalNot: |
| 3977 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3978 | unaryOp = spv::OpLogicalNot; |
| 3979 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3980 | case glslang::EOpBitwiseNot: |
| 3981 | unaryOp = spv::OpNot; |
| 3982 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3983 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3984 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3985 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3986 | break; |
| 3987 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3988 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3989 | break; |
| 3990 | case glslang::EOpTranspose: |
| 3991 | unaryOp = spv::OpTranspose; |
| 3992 | break; |
| 3993 | |
| 3994 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3995 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3996 | break; |
| 3997 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3998 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3999 | break; |
| 4000 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4001 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4002 | break; |
| 4003 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4004 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4005 | break; |
| 4006 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4007 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4008 | break; |
| 4009 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4010 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4011 | break; |
| 4012 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4013 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4014 | break; |
| 4015 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4016 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4017 | break; |
| 4018 | |
| 4019 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4020 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4021 | break; |
| 4022 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4023 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4024 | break; |
| 4025 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4026 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4027 | break; |
| 4028 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4029 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4030 | break; |
| 4031 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4032 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4033 | break; |
| 4034 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4035 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4036 | break; |
| 4037 | |
| 4038 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4039 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4040 | break; |
| 4041 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4042 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4043 | break; |
| 4044 | |
| 4045 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4046 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4047 | break; |
| 4048 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4049 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4050 | break; |
| 4051 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4052 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4053 | break; |
| 4054 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4055 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4056 | break; |
| 4057 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4058 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4059 | break; |
| 4060 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4061 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4062 | break; |
| 4063 | |
| 4064 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4065 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4066 | break; |
| 4067 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4068 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4069 | break; |
| 4070 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4071 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4072 | break; |
| 4073 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4074 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4075 | break; |
| 4076 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4077 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4078 | break; |
| 4079 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4080 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4081 | break; |
| 4082 | |
| 4083 | case glslang::EOpIsNan: |
| 4084 | unaryOp = spv::OpIsNan; |
| 4085 | break; |
| 4086 | case glslang::EOpIsInf: |
| 4087 | unaryOp = spv::OpIsInf; |
| 4088 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4089 | case glslang::EOpIsFinite: |
| 4090 | unaryOp = spv::OpIsFinite; |
| 4091 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4092 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 4093 | case glslang::EOpFloatBitsToInt: |
| 4094 | case glslang::EOpFloatBitsToUint: |
| 4095 | case glslang::EOpIntBitsToFloat: |
| 4096 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4097 | case glslang::EOpDoubleBitsToInt64: |
| 4098 | case glslang::EOpDoubleBitsToUint64: |
| 4099 | case glslang::EOpInt64BitsToDouble: |
| 4100 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4101 | #ifdef AMD_EXTENSIONS |
| 4102 | case glslang::EOpFloat16BitsToInt16: |
| 4103 | case glslang::EOpFloat16BitsToUint16: |
| 4104 | case glslang::EOpInt16BitsToFloat16: |
| 4105 | case glslang::EOpUint16BitsToFloat16: |
| 4106 | #endif |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 4107 | unaryOp = spv::OpBitcast; |
| 4108 | break; |
| 4109 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4110 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4111 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4112 | break; |
| 4113 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4114 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4115 | break; |
| 4116 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4117 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4118 | break; |
| 4119 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4120 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4121 | break; |
| 4122 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4123 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4124 | break; |
| 4125 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4126 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4127 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4128 | case glslang::EOpPackSnorm4x8: |
| 4129 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 4130 | break; |
| 4131 | case glslang::EOpUnpackSnorm4x8: |
| 4132 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 4133 | break; |
| 4134 | case glslang::EOpPackUnorm4x8: |
| 4135 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 4136 | break; |
| 4137 | case glslang::EOpUnpackUnorm4x8: |
| 4138 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 4139 | break; |
| 4140 | case glslang::EOpPackDouble2x32: |
| 4141 | libCall = spv::GLSLstd450PackDouble2x32; |
| 4142 | break; |
| 4143 | case glslang::EOpUnpackDouble2x32: |
| 4144 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 4145 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4146 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4147 | case glslang::EOpPackInt2x32: |
| 4148 | case glslang::EOpUnpackInt2x32: |
| 4149 | case glslang::EOpPackUint2x32: |
| 4150 | case glslang::EOpUnpackUint2x32: |
Rex Xu | c9f3492 | 2016-09-09 17:50:07 +0800 | [diff] [blame] | 4151 | unaryOp = spv::OpBitcast; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4152 | break; |
| 4153 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4154 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4155 | case glslang::EOpPackInt2x16: |
| 4156 | case glslang::EOpUnpackInt2x16: |
| 4157 | case glslang::EOpPackUint2x16: |
| 4158 | case glslang::EOpUnpackUint2x16: |
| 4159 | case glslang::EOpPackInt4x16: |
| 4160 | case glslang::EOpUnpackInt4x16: |
| 4161 | case glslang::EOpPackUint4x16: |
| 4162 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4163 | case glslang::EOpPackFloat2x16: |
| 4164 | case glslang::EOpUnpackFloat2x16: |
| 4165 | unaryOp = spv::OpBitcast; |
| 4166 | break; |
| 4167 | #endif |
| 4168 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4169 | case glslang::EOpDPdx: |
| 4170 | unaryOp = spv::OpDPdx; |
| 4171 | break; |
| 4172 | case glslang::EOpDPdy: |
| 4173 | unaryOp = spv::OpDPdy; |
| 4174 | break; |
| 4175 | case glslang::EOpFwidth: |
| 4176 | unaryOp = spv::OpFwidth; |
| 4177 | break; |
| 4178 | case glslang::EOpDPdxFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4179 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4180 | unaryOp = spv::OpDPdxFine; |
| 4181 | break; |
| 4182 | case glslang::EOpDPdyFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4183 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4184 | unaryOp = spv::OpDPdyFine; |
| 4185 | break; |
| 4186 | case glslang::EOpFwidthFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4187 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4188 | unaryOp = spv::OpFwidthFine; |
| 4189 | break; |
| 4190 | case glslang::EOpDPdxCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4191 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4192 | unaryOp = spv::OpDPdxCoarse; |
| 4193 | break; |
| 4194 | case glslang::EOpDPdyCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4195 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4196 | unaryOp = spv::OpDPdyCoarse; |
| 4197 | break; |
| 4198 | case glslang::EOpFwidthCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4199 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4200 | unaryOp = spv::OpFwidthCoarse; |
| 4201 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4202 | case glslang::EOpInterpolateAtCentroid: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4203 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4204 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 4205 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4206 | case glslang::EOpAny: |
| 4207 | unaryOp = spv::OpAny; |
| 4208 | break; |
| 4209 | case glslang::EOpAll: |
| 4210 | unaryOp = spv::OpAll; |
| 4211 | break; |
| 4212 | |
| 4213 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4214 | if (isFloat) |
| 4215 | libCall = spv::GLSLstd450FAbs; |
| 4216 | else |
| 4217 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4218 | break; |
| 4219 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4220 | if (isFloat) |
| 4221 | libCall = spv::GLSLstd450FSign; |
| 4222 | else |
| 4223 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4224 | break; |
| 4225 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4226 | case glslang::EOpAtomicCounterIncrement: |
| 4227 | case glslang::EOpAtomicCounterDecrement: |
| 4228 | case glslang::EOpAtomicCounter: |
| 4229 | { |
| 4230 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 4231 | std::vector<spv::Id> operands; |
| 4232 | operands.push_back(operand); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4233 | return createAtomicOperation(op, precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4234 | } |
| 4235 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4236 | case glslang::EOpBitFieldReverse: |
| 4237 | unaryOp = spv::OpBitReverse; |
| 4238 | break; |
| 4239 | case glslang::EOpBitCount: |
| 4240 | unaryOp = spv::OpBitCount; |
| 4241 | break; |
| 4242 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4243 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4244 | break; |
| 4245 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4246 | if (isUnsigned) |
| 4247 | libCall = spv::GLSLstd450FindUMsb; |
| 4248 | else |
| 4249 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4250 | break; |
| 4251 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 4252 | case glslang::EOpBallot: |
| 4253 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4254 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4255 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4256 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4257 | #ifdef AMD_EXTENSIONS |
| 4258 | case glslang::EOpMinInvocations: |
| 4259 | case glslang::EOpMaxInvocations: |
| 4260 | case glslang::EOpAddInvocations: |
| 4261 | case glslang::EOpMinInvocationsNonUniform: |
| 4262 | case glslang::EOpMaxInvocationsNonUniform: |
| 4263 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4264 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4265 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4266 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4267 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4268 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4269 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4270 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4271 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4272 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4273 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4274 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4275 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4276 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4277 | { |
| 4278 | std::vector<spv::Id> operands; |
| 4279 | operands.push_back(operand); |
| 4280 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 4281 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4282 | |
| 4283 | #ifdef AMD_EXTENSIONS |
| 4284 | case glslang::EOpMbcnt: |
| 4285 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 4286 | libCall = spv::MbcntAMD; |
| 4287 | break; |
| 4288 | |
| 4289 | case glslang::EOpCubeFaceIndex: |
| 4290 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 4291 | libCall = spv::CubeFaceIndexAMD; |
| 4292 | break; |
| 4293 | |
| 4294 | case glslang::EOpCubeFaceCoord: |
| 4295 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 4296 | libCall = spv::CubeFaceCoordAMD; |
| 4297 | break; |
| 4298 | #endif |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4299 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4300 | default: |
| 4301 | return 0; |
| 4302 | } |
| 4303 | |
| 4304 | spv::Id id; |
| 4305 | if (libCall >= 0) { |
| 4306 | std::vector<spv::Id> args; |
| 4307 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4308 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4309 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4310 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4311 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4312 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4313 | addDecoration(id, noContraction); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4314 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4315 | } |
| 4316 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4317 | // Create a unary operation on a matrix |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4318 | 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] | 4319 | { |
| 4320 | // Handle unary operations vector by vector. |
| 4321 | // The result type is the same type as the original type. |
| 4322 | // The algorithm is to: |
| 4323 | // - break the matrix into vectors |
| 4324 | // - apply the operation to each vector |
| 4325 | // - make a matrix out the vector results |
| 4326 | |
| 4327 | // get the types sorted out |
| 4328 | int numCols = builder.getNumColumns(operand); |
| 4329 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 4330 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 4331 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4332 | std::vector<spv::Id> results; |
| 4333 | |
| 4334 | // do each vector op |
| 4335 | for (int c = 0; c < numCols; ++c) { |
| 4336 | std::vector<unsigned int> indexes; |
| 4337 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 4338 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 4339 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
| 4340 | addDecoration(destVec, noContraction); |
| 4341 | results.push_back(builder.setPrecision(destVec, precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4345 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4346 | } |
| 4347 | |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 4348 | 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] | 4349 | { |
| 4350 | spv::Op convOp = spv::OpNop; |
| 4351 | spv::Id zero = 0; |
| 4352 | spv::Id one = 0; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4353 | spv::Id type = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4354 | |
| 4355 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 4356 | |
| 4357 | switch (op) { |
| 4358 | case glslang::EOpConvIntToBool: |
| 4359 | case glslang::EOpConvUintToBool: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4360 | case glslang::EOpConvInt64ToBool: |
| 4361 | case glslang::EOpConvUint64ToBool: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4362 | #ifdef AMD_EXTENSIONS |
| 4363 | case glslang::EOpConvInt16ToBool: |
| 4364 | case glslang::EOpConvUint16ToBool: |
| 4365 | #endif |
| 4366 | if (op == glslang::EOpConvInt64ToBool || op == glslang::EOpConvUint64ToBool) |
| 4367 | zero = builder.makeUint64Constant(0); |
| 4368 | #ifdef AMD_EXTENSIONS |
| 4369 | else if (op == glslang::EOpConvInt16ToBool || op == glslang::EOpConvUint16ToBool) |
| 4370 | zero = builder.makeUint16Constant(0); |
| 4371 | #endif |
| 4372 | else |
| 4373 | zero = builder.makeUintConstant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4374 | zero = makeSmearedConstant(zero, vectorSize); |
| 4375 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 4376 | |
| 4377 | case glslang::EOpConvFloatToBool: |
| 4378 | zero = builder.makeFloatConstant(0.0F); |
| 4379 | zero = makeSmearedConstant(zero, vectorSize); |
| 4380 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4381 | |
| 4382 | case glslang::EOpConvDoubleToBool: |
| 4383 | zero = builder.makeDoubleConstant(0.0); |
| 4384 | zero = makeSmearedConstant(zero, vectorSize); |
| 4385 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4386 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4387 | #ifdef AMD_EXTENSIONS |
| 4388 | case glslang::EOpConvFloat16ToBool: |
| 4389 | zero = builder.makeFloat16Constant(0.0F); |
| 4390 | zero = makeSmearedConstant(zero, vectorSize); |
| 4391 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4392 | #endif |
| 4393 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4394 | case glslang::EOpConvBoolToFloat: |
| 4395 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4396 | zero = builder.makeFloatConstant(0.0F); |
| 4397 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4398 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4399 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4400 | case glslang::EOpConvBoolToDouble: |
| 4401 | convOp = spv::OpSelect; |
| 4402 | zero = builder.makeDoubleConstant(0.0); |
| 4403 | one = builder.makeDoubleConstant(1.0); |
| 4404 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4405 | |
| 4406 | #ifdef AMD_EXTENSIONS |
| 4407 | case glslang::EOpConvBoolToFloat16: |
| 4408 | convOp = spv::OpSelect; |
| 4409 | zero = builder.makeFloat16Constant(0.0F); |
| 4410 | one = builder.makeFloat16Constant(1.0F); |
| 4411 | break; |
| 4412 | #endif |
| 4413 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4414 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4415 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4416 | #ifdef AMD_EXTENSIONS |
| 4417 | case glslang::EOpConvBoolToInt16: |
| 4418 | #endif |
| 4419 | if (op == glslang::EOpConvBoolToInt64) |
| 4420 | zero = builder.makeInt64Constant(0); |
| 4421 | #ifdef AMD_EXTENSIONS |
| 4422 | else if (op == glslang::EOpConvBoolToInt16) |
| 4423 | zero = builder.makeInt16Constant(0); |
| 4424 | #endif |
| 4425 | else |
| 4426 | zero = builder.makeIntConstant(0); |
| 4427 | |
| 4428 | if (op == glslang::EOpConvBoolToInt64) |
| 4429 | one = builder.makeInt64Constant(1); |
| 4430 | #ifdef AMD_EXTENSIONS |
| 4431 | else if (op == glslang::EOpConvBoolToInt16) |
| 4432 | one = builder.makeInt16Constant(1); |
| 4433 | #endif |
| 4434 | else |
| 4435 | one = builder.makeIntConstant(1); |
| 4436 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4437 | convOp = spv::OpSelect; |
| 4438 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4439 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4440 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4441 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4442 | #ifdef AMD_EXTENSIONS |
| 4443 | case glslang::EOpConvBoolToUint16: |
| 4444 | #endif |
| 4445 | if (op == glslang::EOpConvBoolToUint64) |
| 4446 | zero = builder.makeUint64Constant(0); |
| 4447 | #ifdef AMD_EXTENSIONS |
| 4448 | else if (op == glslang::EOpConvBoolToUint16) |
| 4449 | zero = builder.makeUint16Constant(0); |
| 4450 | #endif |
| 4451 | else |
| 4452 | zero = builder.makeUintConstant(0); |
| 4453 | |
| 4454 | if (op == glslang::EOpConvBoolToUint64) |
| 4455 | one = builder.makeUint64Constant(1); |
| 4456 | #ifdef AMD_EXTENSIONS |
| 4457 | else if (op == glslang::EOpConvBoolToUint16) |
| 4458 | one = builder.makeUint16Constant(1); |
| 4459 | #endif |
| 4460 | else |
| 4461 | one = builder.makeUintConstant(1); |
| 4462 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4463 | convOp = spv::OpSelect; |
| 4464 | break; |
| 4465 | |
| 4466 | case glslang::EOpConvIntToFloat: |
| 4467 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4468 | case glslang::EOpConvInt64ToFloat: |
| 4469 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4470 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4471 | case glslang::EOpConvInt16ToFloat: |
| 4472 | case glslang::EOpConvInt16ToDouble: |
| 4473 | case glslang::EOpConvInt16ToFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4474 | case glslang::EOpConvIntToFloat16: |
| 4475 | case glslang::EOpConvInt64ToFloat16: |
| 4476 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4477 | convOp = spv::OpConvertSToF; |
| 4478 | break; |
| 4479 | |
| 4480 | case glslang::EOpConvUintToFloat: |
| 4481 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4482 | case glslang::EOpConvUint64ToFloat: |
| 4483 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4484 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4485 | case glslang::EOpConvUint16ToFloat: |
| 4486 | case glslang::EOpConvUint16ToDouble: |
| 4487 | case glslang::EOpConvUint16ToFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4488 | case glslang::EOpConvUintToFloat16: |
| 4489 | case glslang::EOpConvUint64ToFloat16: |
| 4490 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4491 | convOp = spv::OpConvertUToF; |
| 4492 | break; |
| 4493 | |
| 4494 | case glslang::EOpConvDoubleToFloat: |
| 4495 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4496 | #ifdef AMD_EXTENSIONS |
| 4497 | case glslang::EOpConvDoubleToFloat16: |
| 4498 | case glslang::EOpConvFloat16ToDouble: |
| 4499 | case glslang::EOpConvFloatToFloat16: |
| 4500 | case glslang::EOpConvFloat16ToFloat: |
| 4501 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4502 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 4503 | if (builder.isMatrixType(destType)) |
| 4504 | return createUnaryMatrixOperation(convOp, precision, noContraction, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4505 | break; |
| 4506 | |
| 4507 | case glslang::EOpConvFloatToInt: |
| 4508 | case glslang::EOpConvDoubleToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4509 | case glslang::EOpConvFloatToInt64: |
| 4510 | case glslang::EOpConvDoubleToInt64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4511 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4512 | case glslang::EOpConvFloatToInt16: |
| 4513 | case glslang::EOpConvDoubleToInt16: |
| 4514 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4515 | case glslang::EOpConvFloat16ToInt: |
| 4516 | case glslang::EOpConvFloat16ToInt64: |
| 4517 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4518 | convOp = spv::OpConvertFToS; |
| 4519 | break; |
| 4520 | |
| 4521 | case glslang::EOpConvUintToInt: |
| 4522 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4523 | case glslang::EOpConvUint64ToInt64: |
| 4524 | case glslang::EOpConvInt64ToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4525 | #ifdef AMD_EXTENSIONS |
| 4526 | case glslang::EOpConvUint16ToInt16: |
| 4527 | case glslang::EOpConvInt16ToUint16: |
| 4528 | #endif |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 4529 | if (builder.isInSpecConstCodeGenMode()) { |
| 4530 | // Build zero scalar or vector for OpIAdd. |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4531 | if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) |
| 4532 | zero = builder.makeUint64Constant(0); |
| 4533 | #ifdef AMD_EXTENSIONS |
| 4534 | else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) |
| 4535 | zero = builder.makeUint16Constant(0); |
| 4536 | #endif |
| 4537 | else |
| 4538 | zero = builder.makeUintConstant(0); |
| 4539 | |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 4540 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 4541 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 4542 | // generating for OpSpecConstantOp instruction. |
| 4543 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 4544 | } |
| 4545 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4546 | convOp = spv::OpBitcast; |
| 4547 | break; |
| 4548 | |
| 4549 | case glslang::EOpConvFloatToUint: |
| 4550 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4551 | case glslang::EOpConvFloatToUint64: |
| 4552 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4553 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4554 | case glslang::EOpConvFloatToUint16: |
| 4555 | case glslang::EOpConvDoubleToUint16: |
| 4556 | case glslang::EOpConvFloat16ToUint16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4557 | case glslang::EOpConvFloat16ToUint: |
| 4558 | case glslang::EOpConvFloat16ToUint64: |
| 4559 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4560 | convOp = spv::OpConvertFToU; |
| 4561 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4562 | |
| 4563 | case glslang::EOpConvIntToInt64: |
| 4564 | case glslang::EOpConvInt64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4565 | #ifdef AMD_EXTENSIONS |
| 4566 | case glslang::EOpConvIntToInt16: |
| 4567 | case glslang::EOpConvInt16ToInt: |
| 4568 | case glslang::EOpConvInt64ToInt16: |
| 4569 | case glslang::EOpConvInt16ToInt64: |
| 4570 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4571 | convOp = spv::OpSConvert; |
| 4572 | break; |
| 4573 | |
| 4574 | case glslang::EOpConvUintToUint64: |
| 4575 | case glslang::EOpConvUint64ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4576 | #ifdef AMD_EXTENSIONS |
| 4577 | case glslang::EOpConvUintToUint16: |
| 4578 | case glslang::EOpConvUint16ToUint: |
| 4579 | case glslang::EOpConvUint64ToUint16: |
| 4580 | case glslang::EOpConvUint16ToUint64: |
| 4581 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4582 | convOp = spv::OpUConvert; |
| 4583 | break; |
| 4584 | |
| 4585 | case glslang::EOpConvIntToUint64: |
| 4586 | case glslang::EOpConvInt64ToUint: |
| 4587 | case glslang::EOpConvUint64ToInt: |
| 4588 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4589 | #ifdef AMD_EXTENSIONS |
| 4590 | case glslang::EOpConvInt16ToUint: |
| 4591 | case glslang::EOpConvUintToInt16: |
| 4592 | case glslang::EOpConvInt16ToUint64: |
| 4593 | case glslang::EOpConvUint64ToInt16: |
| 4594 | case glslang::EOpConvUint16ToInt: |
| 4595 | case glslang::EOpConvIntToUint16: |
| 4596 | case glslang::EOpConvUint16ToInt64: |
| 4597 | case glslang::EOpConvInt64ToUint16: |
| 4598 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4599 | // OpSConvert/OpUConvert + OpBitCast |
| 4600 | switch (op) { |
| 4601 | case glslang::EOpConvIntToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4602 | #ifdef AMD_EXTENSIONS |
| 4603 | case glslang::EOpConvInt16ToUint64: |
| 4604 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4605 | convOp = spv::OpSConvert; |
| 4606 | type = builder.makeIntType(64); |
| 4607 | break; |
| 4608 | case glslang::EOpConvInt64ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4609 | #ifdef AMD_EXTENSIONS |
| 4610 | case glslang::EOpConvInt16ToUint: |
| 4611 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4612 | convOp = spv::OpSConvert; |
| 4613 | type = builder.makeIntType(32); |
| 4614 | break; |
| 4615 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4616 | #ifdef AMD_EXTENSIONS |
| 4617 | case glslang::EOpConvUint16ToInt: |
| 4618 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4619 | convOp = spv::OpUConvert; |
| 4620 | type = builder.makeUintType(32); |
| 4621 | break; |
| 4622 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4623 | #ifdef AMD_EXTENSIONS |
| 4624 | case glslang::EOpConvUint16ToInt64: |
| 4625 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4626 | convOp = spv::OpUConvert; |
| 4627 | type = builder.makeUintType(64); |
| 4628 | break; |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4629 | #ifdef AMD_EXTENSIONS |
| 4630 | case glslang::EOpConvUintToInt16: |
| 4631 | case glslang::EOpConvUint64ToInt16: |
| 4632 | convOp = spv::OpUConvert; |
| 4633 | type = builder.makeUintType(16); |
| 4634 | break; |
| 4635 | case glslang::EOpConvIntToUint16: |
| 4636 | case glslang::EOpConvInt64ToUint16: |
| 4637 | convOp = spv::OpSConvert; |
| 4638 | type = builder.makeIntType(16); |
| 4639 | break; |
| 4640 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4641 | default: |
| 4642 | assert(0); |
| 4643 | break; |
| 4644 | } |
| 4645 | |
| 4646 | if (vectorSize > 0) |
| 4647 | type = builder.makeVectorType(type, vectorSize); |
| 4648 | |
| 4649 | operand = builder.createUnaryOp(convOp, type, operand); |
| 4650 | |
| 4651 | if (builder.isInSpecConstCodeGenMode()) { |
| 4652 | // Build zero scalar or vector for OpIAdd. |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4653 | #ifdef AMD_EXTENSIONS |
| 4654 | if (op == glslang::EOpConvIntToUint64 || op == glslang::EOpConvUintToInt64 || |
| 4655 | op == glslang::EOpConvInt16ToUint64 || op == glslang::EOpConvUint16ToInt64) |
| 4656 | zero = builder.makeUint64Constant(0); |
| 4657 | else if (op == glslang::EOpConvIntToUint16 || op == glslang::EOpConvUintToInt16 || |
| 4658 | op == glslang::EOpConvInt64ToUint16 || op == glslang::EOpConvUint64ToInt16) |
| 4659 | zero = builder.makeUint16Constant(0); |
| 4660 | else |
| 4661 | zero = builder.makeUintConstant(0); |
| 4662 | #else |
| 4663 | if (op == glslang::EOpConvIntToUint64 || op == glslang::EOpConvUintToInt64) |
| 4664 | zero = builder.makeUint64Constant(0); |
| 4665 | else |
| 4666 | zero = builder.makeUintConstant(0); |
| 4667 | #endif |
| 4668 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4669 | zero = makeSmearedConstant(zero, vectorSize); |
| 4670 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 4671 | // generating for OpSpecConstantOp instruction. |
| 4672 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 4673 | } |
| 4674 | // For normal run-time conversion instruction, use OpBitcast. |
| 4675 | convOp = spv::OpBitcast; |
| 4676 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4677 | default: |
| 4678 | break; |
| 4679 | } |
| 4680 | |
| 4681 | spv::Id result = 0; |
| 4682 | if (convOp == spv::OpNop) |
| 4683 | return result; |
| 4684 | |
| 4685 | if (convOp == spv::OpSelect) { |
| 4686 | zero = makeSmearedConstant(zero, vectorSize); |
| 4687 | one = makeSmearedConstant(one, vectorSize); |
| 4688 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 4689 | } else |
| 4690 | result = builder.createUnaryOp(convOp, destType, operand); |
| 4691 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4692 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 4696 | { |
| 4697 | if (vectorSize == 0) |
| 4698 | return constant; |
| 4699 | |
| 4700 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 4701 | std::vector<spv::Id> components; |
| 4702 | for (int c = 0; c < vectorSize; ++c) |
| 4703 | components.push_back(constant); |
| 4704 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 4705 | } |
| 4706 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4707 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4708 | 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] | 4709 | { |
| 4710 | spv::Op opCode = spv::OpNop; |
| 4711 | |
| 4712 | switch (op) { |
| 4713 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4714 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4715 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4716 | opCode = spv::OpAtomicIAdd; |
| 4717 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4718 | case glslang::EOpAtomicCounterSubtract: |
| 4719 | opCode = spv::OpAtomicISub; |
| 4720 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4721 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4722 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4723 | case glslang::EOpAtomicCounterMin: |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4724 | opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4725 | break; |
| 4726 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4727 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4728 | case glslang::EOpAtomicCounterMax: |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4729 | opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4730 | break; |
| 4731 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4732 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4733 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4734 | opCode = spv::OpAtomicAnd; |
| 4735 | break; |
| 4736 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4737 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4738 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4739 | opCode = spv::OpAtomicOr; |
| 4740 | break; |
| 4741 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4742 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4743 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4744 | opCode = spv::OpAtomicXor; |
| 4745 | break; |
| 4746 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4747 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4748 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4749 | opCode = spv::OpAtomicExchange; |
| 4750 | break; |
| 4751 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4752 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4753 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4754 | opCode = spv::OpAtomicCompareExchange; |
| 4755 | break; |
| 4756 | case glslang::EOpAtomicCounterIncrement: |
| 4757 | opCode = spv::OpAtomicIIncrement; |
| 4758 | break; |
| 4759 | case glslang::EOpAtomicCounterDecrement: |
| 4760 | opCode = spv::OpAtomicIDecrement; |
| 4761 | break; |
| 4762 | case glslang::EOpAtomicCounter: |
| 4763 | opCode = spv::OpAtomicLoad; |
| 4764 | break; |
| 4765 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4766 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4767 | break; |
| 4768 | } |
| 4769 | |
| 4770 | // Sort out the operands |
| 4771 | // - mapping from glslang -> SPV |
| 4772 | // - there are extra SPV operands with no glslang source |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4773 | // - compare-exchange swaps the value and comparator |
| 4774 | // - compare-exchange has an extra memory semantics |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4775 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 4776 | auto opIt = operands.begin(); // walk the glslang operands |
| 4777 | spvAtomicOperands.push_back(*(opIt++)); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4778 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::ScopeDevice)); // TBD: what is the correct scope? |
| 4779 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); // TBD: what are the correct memory semantics? |
| 4780 | if (opCode == spv::OpAtomicCompareExchange) { |
Rex Xu | bba5c80 | 2015-09-16 13:20:37 +0800 | [diff] [blame] | 4781 | // There are 2 memory semantics for compare-exchange. And the operand order of "comparator" and "new value" in GLSL |
| 4782 | // differs from that in SPIR-V. Hence, special processing is required. |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4783 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4784 | spvAtomicOperands.push_back(*(opIt + 1)); |
| 4785 | spvAtomicOperands.push_back(*opIt); |
| 4786 | opIt += 2; |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4787 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4788 | |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4789 | // 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] | 4790 | for (; opIt != operands.end(); ++opIt) |
| 4791 | spvAtomicOperands.push_back(*opIt); |
| 4792 | |
| 4793 | return builder.createOp(opCode, typeId, spvAtomicOperands); |
| 4794 | } |
| 4795 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4796 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4797 | 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] | 4798 | { |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4799 | #ifdef AMD_EXTENSIONS |
Jamie Madill | 57cb69a | 2016-11-09 13:49:24 -0500 | [diff] [blame] | 4800 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4801 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4802 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4803 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4804 | spv::Op opCode = spv::OpNop; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4805 | std::vector<spv::Id> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4806 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 4807 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4808 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 4809 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4810 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 4811 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4812 | } else if (op == glslang::EOpAnyInvocation || |
| 4813 | op == glslang::EOpAllInvocations || |
| 4814 | op == glslang::EOpAllInvocationsEqual) { |
| 4815 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 4816 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4817 | } else { |
| 4818 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 4819 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 4820 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 4821 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4822 | op == glslang::EOpAddInvocationsNonUniform || |
| 4823 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 4824 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 4825 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 4826 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 4827 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 4828 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 4829 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 4830 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4831 | |
| 4832 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4833 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4834 | switch (op) { |
| 4835 | case glslang::EOpMinInvocations: |
| 4836 | case glslang::EOpMaxInvocations: |
| 4837 | case glslang::EOpAddInvocations: |
| 4838 | case glslang::EOpMinInvocationsNonUniform: |
| 4839 | case glslang::EOpMaxInvocationsNonUniform: |
| 4840 | case glslang::EOpAddInvocationsNonUniform: |
| 4841 | groupOperation = spv::GroupOperationReduce; |
| 4842 | spvGroupOperands.push_back(groupOperation); |
| 4843 | break; |
| 4844 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4845 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4846 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4847 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4848 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4849 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4850 | groupOperation = spv::GroupOperationInclusiveScan; |
| 4851 | spvGroupOperands.push_back(groupOperation); |
| 4852 | break; |
| 4853 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4854 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4855 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4856 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4857 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4858 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 4859 | groupOperation = spv::GroupOperationExclusiveScan; |
| 4860 | spvGroupOperands.push_back(groupOperation); |
| 4861 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 4862 | default: |
| 4863 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4864 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4865 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) |
| 4869 | spvGroupOperands.push_back(*opIt); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4870 | |
| 4871 | switch (op) { |
| 4872 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4873 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4874 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4875 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4876 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4877 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4878 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4879 | opCode = spv::OpSubgroupAllEqualKHR; |
| 4880 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4881 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4882 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4883 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4884 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4885 | break; |
| 4886 | case glslang::EOpReadFirstInvocation: |
| 4887 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 4888 | break; |
| 4889 | case glslang::EOpBallot: |
| 4890 | { |
| 4891 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 4892 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 4893 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 4894 | // |
| 4895 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 4896 | // |
| 4897 | spv::Id uintType = builder.makeUintType(32); |
| 4898 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 4899 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 4900 | |
| 4901 | std::vector<spv::Id> components; |
| 4902 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 4903 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 4904 | |
| 4905 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 4906 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 4907 | builder.createCompositeConstruct(uvec2Type, components)); |
| 4908 | } |
| 4909 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4910 | #ifdef AMD_EXTENSIONS |
| 4911 | case glslang::EOpMinInvocations: |
| 4912 | case glslang::EOpMaxInvocations: |
| 4913 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4914 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4915 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4916 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4917 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4918 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4919 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4920 | if (op == glslang::EOpMinInvocations || |
| 4921 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 4922 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4923 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4924 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4925 | else { |
| 4926 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4927 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4928 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4929 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4930 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4931 | } else if (op == glslang::EOpMaxInvocations || |
| 4932 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 4933 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4934 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4935 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4936 | else { |
| 4937 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4938 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4939 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4940 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4941 | } |
| 4942 | } else { |
| 4943 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4944 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4945 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4946 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4947 | } |
| 4948 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4949 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4950 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4951 | |
| 4952 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4953 | case glslang::EOpMinInvocationsNonUniform: |
| 4954 | case glslang::EOpMaxInvocationsNonUniform: |
| 4955 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4956 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4957 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4958 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4959 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4960 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4961 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 4962 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 4963 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 4964 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4965 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4966 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4967 | else { |
| 4968 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4969 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4970 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4971 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4972 | } |
| 4973 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4974 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 4975 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 4976 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4977 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4978 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4979 | else { |
| 4980 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4981 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4982 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4983 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4984 | } |
| 4985 | } |
| 4986 | else { |
| 4987 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4988 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4989 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4990 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4991 | } |
| 4992 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4993 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4994 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4995 | |
| 4996 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4997 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4998 | default: |
| 4999 | logger->missingFunctionality("invocation operation"); |
| 5000 | return spv::NoResult; |
| 5001 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5002 | |
| 5003 | assert(opCode != spv::OpNop); |
| 5004 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5005 | } |
| 5006 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5007 | // Create group invocation operations on a vector |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5008 | 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] | 5009 | { |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5010 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5011 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 5012 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5013 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5014 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5015 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 5016 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 5017 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5018 | #else |
| 5019 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 5020 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5021 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 5022 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5023 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5024 | |
| 5025 | // Handle group invocation operations scalar by scalar. |
| 5026 | // The result type is the same type as the original type. |
| 5027 | // The algorithm is to: |
| 5028 | // - break the vector into scalars |
| 5029 | // - apply the operation to each scalar |
| 5030 | // - make a vector out the scalar results |
| 5031 | |
| 5032 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5033 | int numComponents = builder.getNumComponents(operands[0]); |
| 5034 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5035 | std::vector<spv::Id> results; |
| 5036 | |
| 5037 | // do each scalar op |
| 5038 | for (int comp = 0; comp < numComponents; ++comp) { |
| 5039 | std::vector<unsigned int> indexes; |
| 5040 | indexes.push_back(comp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5041 | spv::Id scalar = builder.createCompositeExtract(operands[0], scalarType, indexes); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5042 | std::vector<spv::Id> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5043 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 5044 | spvGroupOperands.push_back(scalar); |
| 5045 | spvGroupOperands.push_back(operands[1]); |
| 5046 | } else if (op == spv::OpGroupBroadcast) { |
| 5047 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5048 | spvGroupOperands.push_back(scalar); |
| 5049 | spvGroupOperands.push_back(operands[1]); |
| 5050 | } else { |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5051 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5052 | spvGroupOperands.push_back(groupOperation); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5053 | spvGroupOperands.push_back(scalar); |
| 5054 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5055 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5056 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | // put the pieces together |
| 5060 | return builder.createCompositeConstruct(typeId, results); |
| 5061 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5062 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5063 | 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] | 5064 | { |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5065 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5066 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64 || typeProxy == glslang::EbtUint16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5067 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 5068 | #else |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5069 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5070 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5071 | #endif |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5072 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5073 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5074 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5075 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 5076 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5077 | spv::Id typeId0 = 0; |
| 5078 | if (consumedOperands > 0) |
| 5079 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5080 | spv::Id typeId1 = 0; |
| 5081 | if (consumedOperands > 1) |
| 5082 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5083 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5084 | |
| 5085 | switch (op) { |
| 5086 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5087 | if (isFloat) |
| 5088 | libCall = spv::GLSLstd450FMin; |
| 5089 | else if (isUnsigned) |
| 5090 | libCall = spv::GLSLstd450UMin; |
| 5091 | else |
| 5092 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5093 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5094 | break; |
| 5095 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5096 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5097 | break; |
| 5098 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5099 | if (isFloat) |
| 5100 | libCall = spv::GLSLstd450FMax; |
| 5101 | else if (isUnsigned) |
| 5102 | libCall = spv::GLSLstd450UMax; |
| 5103 | else |
| 5104 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5105 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5106 | break; |
| 5107 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5108 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5109 | break; |
| 5110 | case glslang::EOpDot: |
| 5111 | opCode = spv::OpDot; |
| 5112 | break; |
| 5113 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5114 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5115 | break; |
| 5116 | |
| 5117 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5118 | if (isFloat) |
| 5119 | libCall = spv::GLSLstd450FClamp; |
| 5120 | else if (isUnsigned) |
| 5121 | libCall = spv::GLSLstd450UClamp; |
| 5122 | else |
| 5123 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5124 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 5125 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5126 | break; |
| 5127 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5128 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 5129 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5130 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5131 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5132 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5133 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5134 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5135 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5136 | break; |
| 5137 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5138 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5139 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5140 | break; |
| 5141 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5142 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5143 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 5144 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5145 | break; |
| 5146 | |
| 5147 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5148 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5149 | break; |
| 5150 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5151 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5152 | break; |
| 5153 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5154 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5155 | break; |
| 5156 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5157 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5158 | break; |
| 5159 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5160 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5161 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5162 | case glslang::EOpInterpolateAtSample: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5163 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5164 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 5165 | break; |
| 5166 | case glslang::EOpInterpolateAtOffset: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5167 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5168 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 5169 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5170 | case glslang::EOpAddCarry: |
| 5171 | opCode = spv::OpIAddCarry; |
| 5172 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5173 | consumedOperands = 2; |
| 5174 | break; |
| 5175 | case glslang::EOpSubBorrow: |
| 5176 | opCode = spv::OpISubBorrow; |
| 5177 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5178 | consumedOperands = 2; |
| 5179 | break; |
| 5180 | case glslang::EOpUMulExtended: |
| 5181 | opCode = spv::OpUMulExtended; |
| 5182 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5183 | consumedOperands = 2; |
| 5184 | break; |
| 5185 | case glslang::EOpIMulExtended: |
| 5186 | opCode = spv::OpSMulExtended; |
| 5187 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5188 | consumedOperands = 2; |
| 5189 | break; |
| 5190 | case glslang::EOpBitfieldExtract: |
| 5191 | if (isUnsigned) |
| 5192 | opCode = spv::OpBitFieldUExtract; |
| 5193 | else |
| 5194 | opCode = spv::OpBitFieldSExtract; |
| 5195 | break; |
| 5196 | case glslang::EOpBitfieldInsert: |
| 5197 | opCode = spv::OpBitFieldInsert; |
| 5198 | break; |
| 5199 | |
| 5200 | case glslang::EOpFma: |
| 5201 | libCall = spv::GLSLstd450Fma; |
| 5202 | break; |
| 5203 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5204 | { |
| 5205 | libCall = spv::GLSLstd450FrexpStruct; |
| 5206 | assert(builder.isPointerType(typeId1)); |
| 5207 | typeId1 = builder.getContainedTypeId(typeId1); |
| 5208 | #ifdef AMD_EXTENSIONS |
| 5209 | int width = builder.getScalarTypeWidth(typeId1); |
| 5210 | #else |
| 5211 | int width = 32; |
| 5212 | #endif |
| 5213 | if (builder.getNumComponents(operands[0]) == 1) |
| 5214 | frexpIntType = builder.makeIntegerType(width, true); |
| 5215 | else |
| 5216 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 5217 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 5218 | consumedOperands = 1; |
| 5219 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5220 | break; |
| 5221 | case glslang::EOpLdexp: |
| 5222 | libCall = spv::GLSLstd450Ldexp; |
| 5223 | break; |
| 5224 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5225 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5226 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5227 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5228 | #ifdef AMD_EXTENSIONS |
| 5229 | case glslang::EOpSwizzleInvocations: |
| 5230 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5231 | libCall = spv::SwizzleInvocationsAMD; |
| 5232 | break; |
| 5233 | case glslang::EOpSwizzleInvocationsMasked: |
| 5234 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5235 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 5236 | break; |
| 5237 | case glslang::EOpWriteInvocation: |
| 5238 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5239 | libCall = spv::WriteInvocationAMD; |
| 5240 | break; |
| 5241 | |
| 5242 | case glslang::EOpMin3: |
| 5243 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5244 | if (isFloat) |
| 5245 | libCall = spv::FMin3AMD; |
| 5246 | else { |
| 5247 | if (isUnsigned) |
| 5248 | libCall = spv::UMin3AMD; |
| 5249 | else |
| 5250 | libCall = spv::SMin3AMD; |
| 5251 | } |
| 5252 | break; |
| 5253 | case glslang::EOpMax3: |
| 5254 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5255 | if (isFloat) |
| 5256 | libCall = spv::FMax3AMD; |
| 5257 | else { |
| 5258 | if (isUnsigned) |
| 5259 | libCall = spv::UMax3AMD; |
| 5260 | else |
| 5261 | libCall = spv::SMax3AMD; |
| 5262 | } |
| 5263 | break; |
| 5264 | case glslang::EOpMid3: |
| 5265 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5266 | if (isFloat) |
| 5267 | libCall = spv::FMid3AMD; |
| 5268 | else { |
| 5269 | if (isUnsigned) |
| 5270 | libCall = spv::UMid3AMD; |
| 5271 | else |
| 5272 | libCall = spv::SMid3AMD; |
| 5273 | } |
| 5274 | break; |
| 5275 | |
| 5276 | case glslang::EOpInterpolateAtVertex: |
| 5277 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 5278 | libCall = spv::InterpolateAtVertexAMD; |
| 5279 | break; |
| 5280 | #endif |
| 5281 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5282 | default: |
| 5283 | return 0; |
| 5284 | } |
| 5285 | |
| 5286 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 5287 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 5288 | // Use an extended instruction from the standard library. |
| 5289 | // Construct the call arguments, without modifying the original operands vector. |
| 5290 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 5291 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5292 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 5293 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5294 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5295 | case 0: |
| 5296 | // should all be handled by visitAggregate and createNoArgOperation |
| 5297 | assert(0); |
| 5298 | return 0; |
| 5299 | case 1: |
| 5300 | // should all be handled by createUnaryOperation |
| 5301 | assert(0); |
| 5302 | return 0; |
| 5303 | case 2: |
| 5304 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 5305 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5306 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5307 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 5308 | assert(consumedOperands == operands.size()); |
| 5309 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5310 | break; |
| 5311 | } |
| 5312 | } |
| 5313 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5314 | // Decode the return types that were structures |
| 5315 | switch (op) { |
| 5316 | case glslang::EOpAddCarry: |
| 5317 | case glslang::EOpSubBorrow: |
| 5318 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 5319 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 5320 | break; |
| 5321 | case glslang::EOpUMulExtended: |
| 5322 | case glslang::EOpIMulExtended: |
| 5323 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 5324 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 5325 | break; |
| 5326 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5327 | { |
| 5328 | assert(operands.size() == 2); |
| 5329 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 5330 | // "exp" is floating-point type (from HLSL intrinsic) |
| 5331 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 5332 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 5333 | builder.createStore(member1, operands[1]); |
| 5334 | } else |
| 5335 | // "exp" is integer type (from GLSL built-in function) |
| 5336 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 5337 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 5338 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5339 | break; |
| 5340 | default: |
| 5341 | break; |
| 5342 | } |
| 5343 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5344 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5345 | } |
| 5346 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5347 | // Intrinsics with no arguments (or no return value, and no precision). |
| 5348 | 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] | 5349 | { |
| 5350 | // TODO: get the barrier operands correct |
| 5351 | |
| 5352 | switch (op) { |
| 5353 | case glslang::EOpEmitVertex: |
| 5354 | builder.createNoResultOp(spv::OpEmitVertex); |
| 5355 | return 0; |
| 5356 | case glslang::EOpEndPrimitive: |
| 5357 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 5358 | return 0; |
| 5359 | case glslang::EOpBarrier: |
chrgau01@arm.com | c3f1cdf | 2016-11-14 10:10:05 +0100 | [diff] [blame] | 5360 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsMaskNone); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5361 | return 0; |
| 5362 | case glslang::EOpMemoryBarrier: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5363 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAllMemory); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5364 | return 0; |
| 5365 | case glslang::EOpMemoryBarrierAtomicCounter: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5366 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAtomicCounterMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5367 | return 0; |
| 5368 | case glslang::EOpMemoryBarrierBuffer: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5369 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5370 | return 0; |
| 5371 | case glslang::EOpMemoryBarrierImage: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5372 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsImageMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5373 | return 0; |
| 5374 | case glslang::EOpMemoryBarrierShared: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5375 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsWorkgroupMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5376 | return 0; |
| 5377 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5378 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5379 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 5380 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 5381 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5382 | builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsAllMemory); |
| 5383 | return 0; |
| 5384 | case glslang::EOpGroupMemoryBarrierWithGroupSync: |
| 5385 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5386 | builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask); |
| 5387 | return 0; |
| 5388 | case glslang::EOpWorkgroupMemoryBarrier: |
| 5389 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask); |
| 5390 | return 0; |
| 5391 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
| 5392 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5393 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask); |
| 5394 | return 0; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5395 | #ifdef AMD_EXTENSIONS |
| 5396 | case glslang::EOpTime: |
| 5397 | { |
| 5398 | std::vector<spv::Id> args; // Dummy arguments |
| 5399 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 5400 | return builder.setPrecision(id, precision); |
| 5401 | } |
| 5402 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5403 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5404 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5405 | return 0; |
| 5406 | } |
| 5407 | } |
| 5408 | |
| 5409 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 5410 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 5411 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5412 | spv::Id id; |
| 5413 | if (symbolValues.end() != iter) { |
| 5414 | id = iter->second; |
| 5415 | return id; |
| 5416 | } |
| 5417 | |
| 5418 | // it was not found, create it |
| 5419 | id = createSpvVariable(symbol); |
| 5420 | symbolValues[symbol->getId()] = id; |
| 5421 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 5422 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5423 | addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 5424 | addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 5425 | addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5426 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
| 5427 | addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5428 | if (symbol->getQualifier().hasIndex()) |
| 5429 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 5430 | if (symbol->getQualifier().hasComponent()) |
| 5431 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 5432 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5433 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5434 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5435 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5436 | if (symbol->getQualifier().hasXfbBuffer()) |
| 5437 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 5438 | if (symbol->getQualifier().hasXfbOffset()) |
| 5439 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
| 5440 | } |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 5441 | // atomic counters use this: |
| 5442 | if (symbol->getQualifier().hasOffset()) |
| 5443 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5444 | } |
| 5445 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 5446 | if (symbol->getQualifier().hasLocation()) |
| 5447 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 5448 | addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 5449 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5450 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5451 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5452 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5453 | if (symbol->getQualifier().hasSet()) |
| 5454 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5455 | else if (IsDescriptorResource(symbol->getType())) { |
| 5456 | // default to 0 |
| 5457 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 5458 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5459 | if (symbol->getQualifier().hasBinding()) |
| 5460 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5461 | if (symbol->getQualifier().hasAttachment()) |
| 5462 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5463 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5464 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5465 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5466 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5467 | if (symbol->getQualifier().hasXfbBuffer()) |
| 5468 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 5469 | } |
| 5470 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 5471 | if (symbol->getType().isImage()) { |
| 5472 | std::vector<spv::Decoration> memory; |
| 5473 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory); |
| 5474 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 5475 | addDecoration(id, memory[i]); |
| 5476 | } |
| 5477 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5478 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 5479 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5480 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5481 | addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5482 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 5483 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5484 | if (builtIn == spv::BuiltInSampleMask) { |
| 5485 | spv::Decoration decoration; |
| 5486 | // GL_NV_sample_mask_override_coverage extension |
| 5487 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5488 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5489 | else |
| 5490 | decoration = (spv::Decoration)spv::DecorationMax; |
| 5491 | addDecoration(id, decoration); |
| 5492 | if (decoration != spv::DecorationMax) { |
| 5493 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 5494 | } |
| 5495 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5496 | else if (builtIn == spv::BuiltInLayer) { |
| 5497 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 5498 | if (symbol->getQualifier().layoutViewportRelative) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5499 | addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 5500 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 5501 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 5502 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 5503 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5504 | addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
| 5505 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 5506 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 5507 | } |
| 5508 | } |
| 5509 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 5510 | if (symbol->getQualifier().layoutPassthrough) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5511 | addDecoration(id, spv::DecorationPassthroughNV); |
| 5512 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 5513 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 5514 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5515 | #endif |
| 5516 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5517 | return id; |
| 5518 | } |
| 5519 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5520 | // If 'dec' is valid, add no-operand decoration to an object |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5521 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec) |
| 5522 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5523 | if (dec != spv::DecorationMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5524 | builder.addDecoration(id, dec); |
| 5525 | } |
| 5526 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5527 | // If 'dec' is valid, add a one-operand decoration to an object |
| 5528 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec, unsigned value) |
| 5529 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5530 | if (dec != spv::DecorationMax) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5531 | builder.addDecoration(id, dec, value); |
| 5532 | } |
| 5533 | |
| 5534 | // 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] | 5535 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec) |
| 5536 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5537 | if (dec != spv::DecorationMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5538 | builder.addMemberDecoration(id, (unsigned)member, dec); |
| 5539 | } |
| 5540 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5541 | // If 'dec' is valid, add a one-operand decoration to a struct member |
| 5542 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value) |
| 5543 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5544 | if (dec != spv::DecorationMax) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5545 | builder.addMemberDecoration(id, (unsigned)member, dec, value); |
| 5546 | } |
| 5547 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5548 | // 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] | 5549 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5550 | // |
| 5551 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 5552 | // |
| 5553 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 5554 | // regular constants, which themselves are trees that createSpvConstant() |
| 5555 | // recursively walks. So, this function walks the "top" of the tree: |
| 5556 | // - emit specialization constant-building instructions for specConstant |
| 5557 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5558 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5559 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 5560 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5561 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5562 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5563 | if (! node.getQualifier().specConstant) { |
| 5564 | // hand off to the non-spec-constant path |
| 5565 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 5566 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5567 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5568 | nextConst, false); |
| 5569 | } |
| 5570 | |
| 5571 | // We now know we have a specialization constant to build |
| 5572 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 5573 | // 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] | 5574 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 5575 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 5576 | std::vector<spv::Id> dimConstId; |
| 5577 | for (int dim = 0; dim < 3; ++dim) { |
| 5578 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 5579 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 5580 | if (specConst) |
| 5581 | addDecoration(dimConstId.back(), spv::DecorationSpecId, glslangIntermediate->getLocalSizeSpecId(dim)); |
| 5582 | } |
| 5583 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 5584 | } |
| 5585 | |
| 5586 | // An AST node labelled as specialization constant should be a symbol node. |
| 5587 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 5588 | if (auto* sn = node.getAsSymbolNode()) { |
| 5589 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 5590 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 5591 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 5592 | // will set the builder into spec constant op instruction generating mode. |
| 5593 | sub_tree->traverse(this); |
| 5594 | return accessChainLoad(sub_tree->getType()); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5595 | } else if (auto* const_union_array = &sn->getConstArray()){ |
| 5596 | int nextConst = 0; |
Endre Oma | ad58d45 | 2017-01-31 21:08:19 +0100 | [diff] [blame] | 5597 | spv::Id id = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
| 5598 | builder.addName(id, sn->getName().c_str()); |
| 5599 | return id; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5600 | } |
| 5601 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5602 | |
| 5603 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 5604 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5605 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5606 | exit(1); |
| 5607 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5608 | } |
| 5609 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5610 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 5611 | // build the aggregate SPIR-V constant. |
| 5612 | // |
| 5613 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 5614 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 5615 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5616 | 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] | 5617 | { |
| 5618 | // vector of constants for SPIR-V |
| 5619 | std::vector<spv::Id> spvConsts; |
| 5620 | |
| 5621 | // Type is used for struct and array constants |
| 5622 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 5623 | |
| 5624 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 5625 | glslang::TType elementType(glslangType, 0); |
| 5626 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5627 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5628 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 5629 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5630 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5631 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5632 | } else if (glslangType.getStruct()) { |
| 5633 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 5634 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5635 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5636 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5637 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 5638 | bool zero = nextConst >= consts.size(); |
| 5639 | switch (glslangType.getBasicType()) { |
| 5640 | case glslang::EbtInt: |
| 5641 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 5642 | break; |
| 5643 | case glslang::EbtUint: |
| 5644 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 5645 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5646 | case glslang::EbtInt64: |
| 5647 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 5648 | break; |
| 5649 | case glslang::EbtUint64: |
| 5650 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 5651 | break; |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5652 | #ifdef AMD_EXTENSIONS |
| 5653 | case glslang::EbtInt16: |
| 5654 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : (short)consts[nextConst].getIConst())); |
| 5655 | break; |
| 5656 | case glslang::EbtUint16: |
| 5657 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : (unsigned short)consts[nextConst].getUConst())); |
| 5658 | break; |
| 5659 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5660 | case glslang::EbtFloat: |
| 5661 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 5662 | break; |
| 5663 | case glslang::EbtDouble: |
| 5664 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 5665 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5666 | #ifdef AMD_EXTENSIONS |
| 5667 | case glslang::EbtFloat16: |
| 5668 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 5669 | break; |
| 5670 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5671 | case glslang::EbtBool: |
| 5672 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 5673 | break; |
| 5674 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5675 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5676 | break; |
| 5677 | } |
| 5678 | ++nextConst; |
| 5679 | } |
| 5680 | } else { |
| 5681 | // we have a non-aggregate (scalar) constant |
| 5682 | bool zero = nextConst >= consts.size(); |
| 5683 | spv::Id scalar = 0; |
| 5684 | switch (glslangType.getBasicType()) { |
| 5685 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5686 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5687 | break; |
| 5688 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5689 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5690 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5691 | case glslang::EbtInt64: |
| 5692 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 5693 | break; |
| 5694 | case glslang::EbtUint64: |
| 5695 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 5696 | break; |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5697 | #ifdef AMD_EXTENSIONS |
| 5698 | case glslang::EbtInt16: |
| 5699 | scalar = builder.makeInt16Constant(zero ? 0 : (short)consts[nextConst].getIConst(), specConstant); |
| 5700 | break; |
| 5701 | case glslang::EbtUint16: |
| 5702 | scalar = builder.makeUint16Constant(zero ? 0 : (unsigned short)consts[nextConst].getUConst(), specConstant); |
| 5703 | break; |
| 5704 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5705 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5706 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5707 | break; |
| 5708 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5709 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5710 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5711 | #ifdef AMD_EXTENSIONS |
| 5712 | case glslang::EbtFloat16: |
| 5713 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 5714 | break; |
| 5715 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5716 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5717 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5718 | break; |
| 5719 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5720 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5721 | break; |
| 5722 | } |
| 5723 | ++nextConst; |
| 5724 | return scalar; |
| 5725 | } |
| 5726 | |
| 5727 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 5728 | } |
| 5729 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5730 | // Return true if the node is a constant or symbol whose reading has no |
| 5731 | // non-trivial observable cost or effect. |
| 5732 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 5733 | { |
| 5734 | // don't know what this is |
| 5735 | if (node == nullptr) |
| 5736 | return false; |
| 5737 | |
| 5738 | // a constant is safe |
| 5739 | if (node->getAsConstantUnion() != nullptr) |
| 5740 | return true; |
| 5741 | |
| 5742 | // not a symbol means non-trivial |
| 5743 | if (node->getAsSymbolNode() == nullptr) |
| 5744 | return false; |
| 5745 | |
| 5746 | // a symbol, depends on what's being read |
| 5747 | switch (node->getType().getQualifier().storage) { |
| 5748 | case glslang::EvqTemporary: |
| 5749 | case glslang::EvqGlobal: |
| 5750 | case glslang::EvqIn: |
| 5751 | case glslang::EvqInOut: |
| 5752 | case glslang::EvqConst: |
| 5753 | case glslang::EvqConstReadOnly: |
| 5754 | case glslang::EvqUniform: |
| 5755 | return true; |
| 5756 | default: |
| 5757 | return false; |
| 5758 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5759 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5760 | |
| 5761 | // 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] | 5762 | // 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] | 5763 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5764 | // Return true if trivial. |
| 5765 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 5766 | { |
| 5767 | if (node == nullptr) |
| 5768 | return false; |
| 5769 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 5770 | // count non scalars as trivial, as well as anything coming from HLSL |
| 5771 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 5772 | return true; |
| 5773 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5774 | // symbols and constants are trivial |
| 5775 | if (isTrivialLeaf(node)) |
| 5776 | return true; |
| 5777 | |
| 5778 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 5779 | |
| 5780 | // not a simple operation |
| 5781 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 5782 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 5783 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 5784 | return false; |
| 5785 | |
| 5786 | // not on leaf nodes |
| 5787 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 5788 | return false; |
| 5789 | |
| 5790 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 5791 | return false; |
| 5792 | } |
| 5793 | |
| 5794 | switch (node->getAsOperator()->getOp()) { |
| 5795 | case glslang::EOpLogicalNot: |
| 5796 | case glslang::EOpConvIntToBool: |
| 5797 | case glslang::EOpConvUintToBool: |
| 5798 | case glslang::EOpConvFloatToBool: |
| 5799 | case glslang::EOpConvDoubleToBool: |
| 5800 | case glslang::EOpEqual: |
| 5801 | case glslang::EOpNotEqual: |
| 5802 | case glslang::EOpLessThan: |
| 5803 | case glslang::EOpGreaterThan: |
| 5804 | case glslang::EOpLessThanEqual: |
| 5805 | case glslang::EOpGreaterThanEqual: |
| 5806 | case glslang::EOpIndexDirect: |
| 5807 | case glslang::EOpIndexDirectStruct: |
| 5808 | case glslang::EOpLogicalXor: |
| 5809 | case glslang::EOpAny: |
| 5810 | case glslang::EOpAll: |
| 5811 | return true; |
| 5812 | default: |
| 5813 | return false; |
| 5814 | } |
| 5815 | } |
| 5816 | |
| 5817 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 5818 | // the left side is true (for &&) or false (for ||). |
| 5819 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 5820 | { |
| 5821 | spv::Id boolTypeId = builder.makeBoolType(); |
| 5822 | |
| 5823 | // emit left operand |
| 5824 | builder.clearAccessChain(); |
| 5825 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 5826 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5827 | |
| 5828 | // Operands to accumulate OpPhi operands |
| 5829 | std::vector<spv::Id> phiOperands; |
| 5830 | // accumulate left operand's phi information |
| 5831 | phiOperands.push_back(leftId); |
| 5832 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 5833 | |
| 5834 | // Make the two kinds of operation symmetric with a "!" |
| 5835 | // || => emit "if (! left) result = right" |
| 5836 | // && => emit "if ( left) result = right" |
| 5837 | // |
| 5838 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 5839 | // to 'builder' to have an "else" without an "then" |
| 5840 | if (op == glslang::EOpLogicalOr) |
| 5841 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 5842 | |
| 5843 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 5844 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5845 | |
| 5846 | // emit right operand as the "then" part of the "if" |
| 5847 | builder.clearAccessChain(); |
| 5848 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 5849 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5850 | |
| 5851 | // accumulate left operand's phi information |
| 5852 | phiOperands.push_back(rightId); |
| 5853 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 5854 | |
| 5855 | // finish the "if" |
| 5856 | ifBuilder.makeEndIf(); |
| 5857 | |
| 5858 | // phi together the two results |
| 5859 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 5860 | } |
| 5861 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5862 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 5863 | // Import this set if it has not been imported yet. |
| 5864 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 5865 | { |
| 5866 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 5867 | return extBuiltinMap[name]; |
| 5868 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5869 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5870 | spv::Id extBuiltins = builder.import(name); |
| 5871 | extBuiltinMap[name] = extBuiltins; |
| 5872 | return extBuiltins; |
| 5873 | } |
| 5874 | } |
| 5875 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5876 | }; // end anonymous namespace |
| 5877 | |
| 5878 | namespace glslang { |
| 5879 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5880 | void GetSpirvVersion(std::string& version) |
| 5881 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 5882 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 5883 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5884 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5885 | version = buf; |
| 5886 | } |
| 5887 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5888 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5889 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5890 | { |
| 5891 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5892 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 5893 | if (out.fail()) |
| 5894 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5895 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 5896 | unsigned int word = spirv[i]; |
| 5897 | out.write((const char*)&word, 4); |
| 5898 | } |
| 5899 | out.close(); |
| 5900 | } |
| 5901 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5902 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 5903 | 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] | 5904 | { |
| 5905 | std::ofstream out; |
| 5906 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 5907 | if (out.fail()) |
| 5908 | printf("ERROR: Failed to open file: %s\n", baseName); |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5909 | out << "\t// " GLSLANG_REVISION " " GLSLANG_DATE << std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 5910 | if (varName != nullptr) { |
| 5911 | out << "\t #pragma once" << std::endl; |
| 5912 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 5913 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5914 | const int WORDS_PER_LINE = 8; |
| 5915 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 5916 | out << "\t"; |
| 5917 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 5918 | const unsigned int word = spirv[i + j]; |
| 5919 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 5920 | if (i + j + 1 < (int)spirv.size()) { |
| 5921 | out << ","; |
| 5922 | } |
| 5923 | } |
| 5924 | out << std::endl; |
| 5925 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 5926 | if (varName != nullptr) { |
| 5927 | out << "};"; |
| 5928 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5929 | out.close(); |
| 5930 | } |
| 5931 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5932 | // |
| 5933 | // Set up the glslang traversal |
| 5934 | // |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5935 | 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] | 5936 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5937 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5938 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 5939 | } |
| 5940 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5941 | void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
| 5942 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 5943 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5944 | TIntermNode* root = intermediate.getTreeRoot(); |
| 5945 | |
| 5946 | if (root == 0) |
| 5947 | return; |
| 5948 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5949 | glslang::SpvOptions defaultOptions; |
| 5950 | if (options == nullptr) |
| 5951 | options = &defaultOptions; |
| 5952 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5953 | glslang::GetThreadPoolAllocator().push(); |
| 5954 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5955 | TGlslangToSpvTraverser it(&intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5956 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 5957 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5958 | it.dumpSpv(spirv); |
| 5959 | |
| 5960 | glslang::GetThreadPoolAllocator().pop(); |
| 5961 | } |
| 5962 | |
| 5963 | }; // end namespace glslang |