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: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 456 | builder.addCapability(spv::CapabilityMultiViewport); |
| 457 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 458 | glslangIntermediate->getStage() == EShLangTessControl || |
| 459 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 460 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 461 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 462 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 463 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 464 | return spv::BuiltInViewportIndex; |
| 465 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 466 | case glslang::EbvSampleId: |
| 467 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 468 | return spv::BuiltInSampleId; |
| 469 | |
| 470 | case glslang::EbvSamplePosition: |
| 471 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 472 | return spv::BuiltInSamplePosition; |
| 473 | |
| 474 | case glslang::EbvSampleMask: |
| 475 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 476 | return spv::BuiltInSampleMask; |
| 477 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 478 | case glslang::EbvLayer: |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 479 | builder.addCapability(spv::CapabilityGeometry); |
| 480 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 481 | glslangIntermediate->getStage() == EShLangTessControl || |
| 482 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 483 | |
John Kessenich | ba6a3c2 | 2017-09-13 13:22:50 -0600 | [diff] [blame] | 484 | builder.addExtension(spv::E_SPV_EXT_shader_viewport_index_layer); |
| 485 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 486 | } |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 487 | return spv::BuiltInLayer; |
| 488 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 489 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 490 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 491 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 492 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 493 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 494 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 495 | case glslang::EbvBaseVertex: |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 496 | builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters); |
| 497 | builder.addCapability(spv::CapabilityDrawParameters); |
| 498 | return spv::BuiltInBaseVertex; |
| 499 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 500 | case glslang::EbvBaseInstance: |
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::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 504 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 505 | case glslang::EbvDrawId: |
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::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 509 | |
| 510 | case glslang::EbvPrimitiveId: |
| 511 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 512 | builder.addCapability(spv::CapabilityGeometry); |
| 513 | return spv::BuiltInPrimitiveId; |
| 514 | |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 515 | case glslang::EbvFragStencilRef: |
Rex Xu | e8fdd79 | 2017-08-23 23:24:42 +0800 | [diff] [blame] | 516 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 517 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 518 | return spv::BuiltInFragStencilRefEXT; |
Rex Xu | 37cdcee | 2017-06-29 17:46:34 +0800 | [diff] [blame] | 519 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 520 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 521 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 522 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 523 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 524 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 525 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 526 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 527 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 528 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 529 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 530 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 531 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 532 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 533 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 534 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 535 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 536 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 537 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 538 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 539 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 540 | return spv::BuiltInSubgroupSize; |
| 541 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 542 | case glslang::EbvSubGroupInvocation: |
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::BuiltInSubgroupLocalInvocationId; |
| 546 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 547 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 548 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 549 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 550 | return spv::BuiltInSubgroupEqMaskKHR; |
| 551 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 552 | case glslang::EbvSubGroupGeMask: |
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::BuiltInSubgroupGeMaskKHR; |
| 556 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 557 | case glslang::EbvSubGroupGtMask: |
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::BuiltInSubgroupGtMaskKHR; |
| 561 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 562 | case glslang::EbvSubGroupLeMask: |
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::BuiltInSubgroupLeMaskKHR; |
| 566 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 567 | case glslang::EbvSubGroupLtMask: |
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::BuiltInSubgroupLtMaskKHR; |
| 571 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 572 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 573 | case glslang::EbvBaryCoordNoPersp: |
| 574 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 575 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 576 | |
| 577 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 578 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 579 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 580 | |
| 581 | case glslang::EbvBaryCoordNoPerspSample: |
| 582 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 583 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 584 | |
| 585 | case glslang::EbvBaryCoordSmooth: |
| 586 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 587 | return spv::BuiltInBaryCoordSmoothAMD; |
| 588 | |
| 589 | case glslang::EbvBaryCoordSmoothCentroid: |
| 590 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 591 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 592 | |
| 593 | case glslang::EbvBaryCoordSmoothSample: |
| 594 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 595 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 596 | |
| 597 | case glslang::EbvBaryCoordPullModel: |
| 598 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 599 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 600 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 601 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 602 | case glslang::EbvDeviceIndex: |
| 603 | builder.addExtension(spv::E_SPV_KHR_device_group); |
| 604 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 605 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 606 | |
| 607 | case glslang::EbvViewIndex: |
| 608 | builder.addExtension(spv::E_SPV_KHR_multiview); |
| 609 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 610 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 611 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 612 | #ifdef NV_EXTENSIONS |
| 613 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 614 | if (!memberDeclaration) { |
| 615 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 616 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 617 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 618 | return spv::BuiltInViewportMaskNV; |
| 619 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 620 | if (!memberDeclaration) { |
| 621 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 622 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 623 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 624 | return spv::BuiltInSecondaryPositionNV; |
| 625 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 626 | if (!memberDeclaration) { |
| 627 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 628 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 629 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 630 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 631 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 632 | if (!memberDeclaration) { |
| 633 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 634 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 635 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 636 | return spv::BuiltInPositionPerViewNV; |
| 637 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 638 | if (!memberDeclaration) { |
| 639 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 640 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 641 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 642 | return spv::BuiltInViewportMaskPerViewNV; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 643 | #endif |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 644 | default: |
| 645 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 649 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 650 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 651 | { |
| 652 | assert(type.getBasicType() == glslang::EbtSampler); |
| 653 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 654 | // Check for capabilities |
| 655 | switch (type.getQualifier().layoutFormat) { |
| 656 | case glslang::ElfRg32f: |
| 657 | case glslang::ElfRg16f: |
| 658 | case glslang::ElfR11fG11fB10f: |
| 659 | case glslang::ElfR16f: |
| 660 | case glslang::ElfRgba16: |
| 661 | case glslang::ElfRgb10A2: |
| 662 | case glslang::ElfRg16: |
| 663 | case glslang::ElfRg8: |
| 664 | case glslang::ElfR16: |
| 665 | case glslang::ElfR8: |
| 666 | case glslang::ElfRgba16Snorm: |
| 667 | case glslang::ElfRg16Snorm: |
| 668 | case glslang::ElfRg8Snorm: |
| 669 | case glslang::ElfR16Snorm: |
| 670 | case glslang::ElfR8Snorm: |
| 671 | |
| 672 | case glslang::ElfRg32i: |
| 673 | case glslang::ElfRg16i: |
| 674 | case glslang::ElfRg8i: |
| 675 | case glslang::ElfR16i: |
| 676 | case glslang::ElfR8i: |
| 677 | |
| 678 | case glslang::ElfRgb10a2ui: |
| 679 | case glslang::ElfRg32ui: |
| 680 | case glslang::ElfRg16ui: |
| 681 | case glslang::ElfRg8ui: |
| 682 | case glslang::ElfR16ui: |
| 683 | case glslang::ElfR8ui: |
| 684 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 685 | break; |
| 686 | |
| 687 | default: |
| 688 | break; |
| 689 | } |
| 690 | |
| 691 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 692 | switch (type.getQualifier().layoutFormat) { |
| 693 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 694 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 695 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 696 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 697 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 698 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 699 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 700 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 701 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 702 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 703 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 704 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 705 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 706 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 707 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 708 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 709 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 710 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 711 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 712 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 713 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 714 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 715 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 716 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 717 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 718 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 719 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 720 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 721 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 722 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 723 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 724 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 725 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 726 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 727 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 728 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 729 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 730 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 731 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 732 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 733 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 737 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(glslang::TSelectionControl selectionControl) const |
| 738 | { |
| 739 | switch (selectionControl) { |
| 740 | case glslang::ESelectionControlNone: return spv::SelectionControlMaskNone; |
| 741 | case glslang::ESelectionControlFlatten: return spv::SelectionControlFlattenMask; |
| 742 | case glslang::ESelectionControlDontFlatten: return spv::SelectionControlDontFlattenMask; |
| 743 | default: return spv::SelectionControlMaskNone; |
| 744 | } |
| 745 | } |
| 746 | |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 747 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(glslang::TLoopControl loopControl) const |
| 748 | { |
| 749 | switch (loopControl) { |
| 750 | case glslang::ELoopControlNone: return spv::LoopControlMaskNone; |
| 751 | case glslang::ELoopControlUnroll: return spv::LoopControlUnrollMask; |
| 752 | case glslang::ELoopControlDontUnroll: return spv::LoopControlDontUnrollMask; |
| 753 | // TODO: DependencyInfinite |
| 754 | // TODO: DependencyLength |
| 755 | default: return spv::LoopControlMaskNone; |
| 756 | } |
| 757 | } |
| 758 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 759 | // Translate glslang type to SPIR-V storage class. |
| 760 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 761 | { |
| 762 | if (type.getQualifier().isPipeInput()) |
| 763 | return spv::StorageClassInput; |
| 764 | else if (type.getQualifier().isPipeOutput()) |
| 765 | return spv::StorageClassOutput; |
| 766 | else if (type.getBasicType() == glslang::EbtAtomicUint) |
| 767 | return spv::StorageClassAtomicCounter; |
| 768 | else if (type.containsOpaque()) |
| 769 | return spv::StorageClassUniformConstant; |
| 770 | else if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
| 771 | builder.addExtension(spv::E_SPV_KHR_storage_buffer_storage_class); |
| 772 | return spv::StorageClassStorageBuffer; |
| 773 | } else if (type.getQualifier().isUniformOrBuffer()) { |
| 774 | if (type.getQualifier().layoutPushConstant) |
| 775 | return spv::StorageClassPushConstant; |
| 776 | if (type.getBasicType() == glslang::EbtBlock) |
| 777 | return spv::StorageClassUniform; |
| 778 | else |
| 779 | return spv::StorageClassUniformConstant; |
| 780 | } else { |
| 781 | switch (type.getQualifier().storage) { |
| 782 | case glslang::EvqShared: return spv::StorageClassWorkgroup; break; |
| 783 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 784 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 785 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
| 786 | default: |
| 787 | assert(0); |
| 788 | return spv::StorageClassFunction; |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 793 | // 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] | 794 | // descriptor set. |
| 795 | bool IsDescriptorResource(const glslang::TType& type) |
| 796 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 797 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 798 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 799 | return type.getQualifier().isUniformOrBuffer() && ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 800 | |
| 801 | // non block... |
| 802 | // basically samplerXXX/subpass/sampler/texture are all included |
| 803 | // if they are the global-scope-class, not the function parameter |
| 804 | // (or local, if they ever exist) class. |
| 805 | if (type.getBasicType() == glslang::EbtSampler) |
| 806 | return type.getQualifier().isUniformOrBuffer(); |
| 807 | |
| 808 | // None of the above. |
| 809 | return false; |
| 810 | } |
| 811 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 812 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 813 | { |
| 814 | if (child.layoutMatrix == glslang::ElmNone) |
| 815 | child.layoutMatrix = parent.layoutMatrix; |
| 816 | |
| 817 | if (parent.invariant) |
| 818 | child.invariant = true; |
| 819 | if (parent.nopersp) |
| 820 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 821 | #ifdef AMD_EXTENSIONS |
| 822 | if (parent.explicitInterp) |
| 823 | child.explicitInterp = true; |
| 824 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 825 | if (parent.flat) |
| 826 | child.flat = true; |
| 827 | if (parent.centroid) |
| 828 | child.centroid = true; |
| 829 | if (parent.patch) |
| 830 | child.patch = true; |
| 831 | if (parent.sample) |
| 832 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 833 | if (parent.coherent) |
| 834 | child.coherent = true; |
| 835 | if (parent.volatil) |
| 836 | child.volatil = true; |
| 837 | if (parent.restrict) |
| 838 | child.restrict = true; |
| 839 | if (parent.readonly) |
| 840 | child.readonly = true; |
| 841 | if (parent.writeonly) |
| 842 | child.writeonly = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 843 | } |
| 844 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 845 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 846 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 847 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 848 | // - struct members might inherit from a struct declaration |
| 849 | // (note that non-block structs don't explicitly inherit, |
| 850 | // only implicitly, meaning no decoration involved) |
| 851 | // - affect decorations on the struct members |
| 852 | // (note smooth does not, and expecting something like volatile |
| 853 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 854 | // - 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] | 855 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 856 | } |
| 857 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 858 | // |
| 859 | // Implement the TGlslangToSpvTraverser class. |
| 860 | // |
| 861 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 862 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(const glslang::TIntermediate* glslangIntermediate, |
| 863 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) |
| 864 | : TIntermTraverser(true, false, true), |
| 865 | options(options), |
| 866 | shaderEntry(nullptr), currentFunction(nullptr), |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 867 | sequenceDepth(0), logger(buildLogger), |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 868 | builder((glslang::GetKhronosToolId() << 16) | GeneratorVersion, logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 869 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 870 | glslangIntermediate(glslangIntermediate) |
| 871 | { |
| 872 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 873 | |
| 874 | builder.clearAccessChain(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 875 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 876 | glslangIntermediate->getVersion()); |
| 877 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 878 | if (options.generateDebugInfo) { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 879 | builder.setEmitOpLines(); |
John Kessenich | 2a27116 | 2017-07-20 20:00:36 -0600 | [diff] [blame] | 880 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 881 | |
| 882 | // Set the source shader's text. If for SPV version 1.0, include |
| 883 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 884 | // Otherwise, emit those as actual instructions. |
| 885 | std::string text; |
| 886 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 887 | for (int p = 0; p < (int)processes.size(); ++p) { |
| 888 | if (glslangIntermediate->getSpv().spv < 0x00010100) { |
| 889 | text.append("// OpModuleProcessed "); |
| 890 | text.append(processes[p]); |
| 891 | text.append("\n"); |
| 892 | } else |
| 893 | builder.addModuleProcessed(processes[p]); |
| 894 | } |
| 895 | if (glslangIntermediate->getSpv().spv < 0x00010100 && (int)processes.size() > 0) |
| 896 | text.append("#line 1\n"); |
| 897 | text.append(glslangIntermediate->getSourceText()); |
| 898 | builder.setSourceText(text); |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 899 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 900 | stdBuiltins = builder.import("GLSL.std.450"); |
| 901 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 902 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 903 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 904 | |
| 905 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 906 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 907 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 908 | builder.addSourceExtension(it->c_str()); |
| 909 | |
| 910 | // Add the top-level modes for this shader. |
| 911 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 912 | if (glslangIntermediate->getXfbMode()) { |
| 913 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 914 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 915 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 916 | |
| 917 | unsigned int mode; |
| 918 | switch (glslangIntermediate->getStage()) { |
| 919 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 920 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 921 | break; |
| 922 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 923 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 924 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 925 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 926 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 927 | glslang::TLayoutGeometry primitive; |
| 928 | |
| 929 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 930 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 931 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 932 | } else { |
| 933 | primitive = glslangIntermediate->getInputPrimitive(); |
| 934 | } |
| 935 | |
| 936 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 937 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 938 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 939 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 940 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 941 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 942 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 943 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 944 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 945 | switch (glslangIntermediate->getVertexSpacing()) { |
| 946 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 947 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 948 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 949 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 950 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 951 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 952 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 953 | |
| 954 | switch (glslangIntermediate->getVertexOrder()) { |
| 955 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 956 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 957 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 958 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 959 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 960 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 961 | |
| 962 | if (glslangIntermediate->getPointMode()) |
| 963 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 964 | break; |
| 965 | |
| 966 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 967 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 968 | switch (glslangIntermediate->getInputPrimitive()) { |
| 969 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 970 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 971 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 972 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 973 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 974 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 975 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 976 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 977 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 978 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 979 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 980 | |
| 981 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 982 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 983 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 984 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 985 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 986 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 987 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 988 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 989 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 990 | break; |
| 991 | |
| 992 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 993 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 994 | if (glslangIntermediate->getPixelCenterInteger()) |
| 995 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 996 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 997 | if (glslangIntermediate->getOriginUpperLeft()) |
| 998 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 999 | else |
| 1000 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1001 | |
| 1002 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1003 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1004 | |
chaoc | c120452 | 2017-06-30 17:14:30 -0700 | [diff] [blame] | 1005 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1006 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1007 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1008 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1009 | } |
| 1010 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1011 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1012 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1013 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1014 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1015 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 1016 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 1017 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1018 | |
| 1019 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1020 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1021 | break; |
| 1022 | |
| 1023 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1024 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 1025 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1026 | glslangIntermediate->getLocalSize(1), |
| 1027 | glslangIntermediate->getLocalSize(2)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1028 | break; |
| 1029 | |
| 1030 | default: |
| 1031 | break; |
| 1032 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1033 | } |
| 1034 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1035 | // Finish creating SPV, after the traversal is complete. |
| 1036 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1037 | { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1038 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1039 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1040 | builder.leaveFunction(); |
| 1041 | } |
| 1042 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1043 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1044 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1045 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1046 | |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 1047 | builder.eliminateDeadDecorations(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1050 | // Write the SPV into 'out'. |
| 1051 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1052 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1053 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | // |
| 1057 | // Implement the traversal functions. |
| 1058 | // |
| 1059 | // Return true from interior nodes to have the external traversal |
| 1060 | // continue on to children. Return false if children were |
| 1061 | // already processed. |
| 1062 | // |
| 1063 | |
| 1064 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1065 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1066 | // - uniform/input reads |
| 1067 | // - output writes |
| 1068 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1069 | // - something simple that degenerates into the last bullet |
| 1070 | // |
| 1071 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1072 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1073 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1074 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1075 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1076 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1077 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1078 | // Formal function parameters were mapped during makeFunctions(). |
| 1079 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1080 | |
| 1081 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1082 | if (builder.isPointer(id)) { |
| 1083 | spv::StorageClass sc = builder.getStorageClass(id); |
John Kessenich | 5f77d86 | 2017-09-19 11:09:59 -0600 | [diff] [blame] | 1084 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) { |
| 1085 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) |
| 1086 | iOSet.insert(id); |
| 1087 | } |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1091 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1092 | // Prepare to generate code for the access |
| 1093 | |
| 1094 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1095 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1096 | // followed by setting the base. |
| 1097 | builder.clearAccessChain(); |
| 1098 | |
| 1099 | // 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] | 1100 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1101 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1102 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1103 | // 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] | 1104 | // These are also pure R-values. |
| 1105 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1106 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1107 | builder.setAccessChainRValue(id); |
| 1108 | else |
| 1109 | builder.setAccessChainLValue(id); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1114 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1115 | builder.setLine(node->getLoc().line); |
| 1116 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1117 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1118 | if (node->getType().getQualifier().isSpecConstant()) |
| 1119 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1120 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1121 | // First, handle special cases |
| 1122 | switch (node->getOp()) { |
| 1123 | case glslang::EOpAssign: |
| 1124 | case glslang::EOpAddAssign: |
| 1125 | case glslang::EOpSubAssign: |
| 1126 | case glslang::EOpMulAssign: |
| 1127 | case glslang::EOpVectorTimesMatrixAssign: |
| 1128 | case glslang::EOpVectorTimesScalarAssign: |
| 1129 | case glslang::EOpMatrixTimesScalarAssign: |
| 1130 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1131 | case glslang::EOpDivAssign: |
| 1132 | case glslang::EOpModAssign: |
| 1133 | case glslang::EOpAndAssign: |
| 1134 | case glslang::EOpInclusiveOrAssign: |
| 1135 | case glslang::EOpExclusiveOrAssign: |
| 1136 | case glslang::EOpLeftShiftAssign: |
| 1137 | case glslang::EOpRightShiftAssign: |
| 1138 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1139 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1140 | // says to evaluate the left before the right. So, always, left |
| 1141 | // node then right node. |
| 1142 | { |
| 1143 | // get the left l-value, save it away |
| 1144 | builder.clearAccessChain(); |
| 1145 | node->getLeft()->traverse(this); |
| 1146 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1147 | |
| 1148 | // evaluate the right |
| 1149 | builder.clearAccessChain(); |
| 1150 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1151 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1152 | |
| 1153 | if (node->getOp() != glslang::EOpAssign) { |
| 1154 | // the left is also an r-value |
| 1155 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1156 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1157 | |
| 1158 | // do the operation |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1159 | rValue = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1160 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1161 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1162 | node->getType().getBasicType()); |
| 1163 | |
| 1164 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1165 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | // store the result |
| 1169 | builder.setAccessChain(lValue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1170 | multiTypeStore(node->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1171 | |
| 1172 | // assignments are expressions having an rValue after they are evaluated... |
| 1173 | builder.clearAccessChain(); |
| 1174 | builder.setAccessChainRValue(rValue); |
| 1175 | } |
| 1176 | return false; |
| 1177 | case glslang::EOpIndexDirect: |
| 1178 | case glslang::EOpIndexDirectStruct: |
| 1179 | { |
| 1180 | // Get the left part of the access chain. |
| 1181 | node->getLeft()->traverse(this); |
| 1182 | |
| 1183 | // Add the next element in the chain |
| 1184 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1185 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1186 | if (! node->getLeft()->getType().isArray() && |
| 1187 | node->getLeft()->getType().isVector() && |
| 1188 | node->getOp() == glslang::EOpIndexDirect) { |
| 1189 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1190 | // so short circuit the access-chain stuff with a swizzle. |
| 1191 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1192 | swizzle.push_back(glslangIndex); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1193 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1194 | } else { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1195 | int spvIndex = glslangIndex; |
| 1196 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1197 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1198 | { |
| 1199 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1200 | // index remapping due to hidden members in anonymous blocks. |
| 1201 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1202 | assert(remapper.size() > 0); |
| 1203 | spvIndex = remapper[glslangIndex]; |
| 1204 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1205 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1206 | // normal case for indexing array or structure or block |
| 1207 | builder.accessChainPush(builder.makeIntConstant(spvIndex)); |
| 1208 | |
| 1209 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1210 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1211 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1212 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | return false; |
| 1216 | case glslang::EOpIndexIndirect: |
| 1217 | { |
| 1218 | // Structure or array or vector indirection. |
| 1219 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 1220 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1221 | // Will use the access chain's 'component' for variable index into a vector. |
| 1222 | |
| 1223 | // This adapter is building access chains left to right. |
| 1224 | // Set up the access chain to the left. |
| 1225 | node->getLeft()->traverse(this); |
| 1226 | |
| 1227 | // save it so that computing the right side doesn't trash it |
| 1228 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1229 | |
| 1230 | // compute the next index in the chain |
| 1231 | builder.clearAccessChain(); |
| 1232 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1233 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1234 | |
| 1235 | // restore the saved access chain |
| 1236 | builder.setAccessChain(partial); |
| 1237 | |
| 1238 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1239 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1240 | else |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1241 | builder.accessChainPush(index); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1242 | } |
| 1243 | return false; |
| 1244 | case glslang::EOpVectorSwizzle: |
| 1245 | { |
| 1246 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1247 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1248 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1249 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1250 | } |
| 1251 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1252 | case glslang::EOpMatrixSwizzle: |
| 1253 | logger->missingFunctionality("matrix swizzle"); |
| 1254 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1255 | case glslang::EOpLogicalOr: |
| 1256 | case glslang::EOpLogicalAnd: |
| 1257 | { |
| 1258 | |
| 1259 | // These may require short circuiting, but can sometimes be done as straight |
| 1260 | // binary operations. The right operand must be short circuited if it has |
| 1261 | // side effects, and should probably be if it is complex. |
| 1262 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1263 | break; // handle below as a normal binary operation |
| 1264 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1265 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1266 | builder.clearAccessChain(); |
| 1267 | builder.setAccessChainRValue(result); |
| 1268 | } |
| 1269 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1270 | default: |
| 1271 | break; |
| 1272 | } |
| 1273 | |
| 1274 | // Assume generic binary op... |
| 1275 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1276 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1277 | builder.clearAccessChain(); |
| 1278 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1279 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1280 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1281 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1282 | builder.clearAccessChain(); |
| 1283 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1284 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1285 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1286 | // get result |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1287 | spv::Id result = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1288 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1289 | convertGlslangToSpvType(node->getType()), left, right, |
| 1290 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1291 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1292 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1293 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1294 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1295 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1296 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1297 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1298 | return false; |
| 1299 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1303 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1304 | builder.setLine(node->getLoc().line); |
| 1305 | |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1306 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1307 | if (node->getType().getQualifier().isSpecConstant()) |
| 1308 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1309 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1310 | spv::Id result = spv::NoResult; |
| 1311 | |
| 1312 | // try texturing first |
| 1313 | result = createImageTextureFunctionCall(node); |
| 1314 | if (result != spv::NoResult) { |
| 1315 | builder.clearAccessChain(); |
| 1316 | builder.setAccessChainRValue(result); |
| 1317 | |
| 1318 | return false; // done with this node |
| 1319 | } |
| 1320 | |
| 1321 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1322 | |
| 1323 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1324 | // Quite special; won't want to evaluate the operand. |
| 1325 | |
| 1326 | // Normal .length() would have been constant folded by the front-end. |
| 1327 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1328 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1329 | assert(node->getOperand()->getType().isRuntimeSizedArray()); |
| 1330 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1331 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1332 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1333 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1334 | |
| 1335 | builder.clearAccessChain(); |
| 1336 | builder.setAccessChainRValue(length); |
| 1337 | |
| 1338 | return false; |
| 1339 | } |
| 1340 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1341 | // Start by evaluating the operand |
| 1342 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1343 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1344 | // operate first on the swizzle base, then apply the swizzle. |
| 1345 | spv::Id invertedType = spv::NoType; |
| 1346 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 1347 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 1348 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 1349 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1350 | builder.clearAccessChain(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1351 | if (invertedType != spv::NoType) |
| 1352 | node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this); |
| 1353 | else |
| 1354 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1355 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1356 | spv::Id operand = spv::NoResult; |
| 1357 | |
| 1358 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1359 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1360 | node->getOp() == glslang::EOpAtomicCounter || |
| 1361 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1362 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1363 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1364 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1365 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1366 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1367 | spv::Decoration noContraction = TranslateNoContractionDecoration(node->getType().getQualifier()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1368 | |
| 1369 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1370 | if (! result) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1371 | result = createConversion(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1372 | |
| 1373 | // if not, then possibly an operation |
| 1374 | if (! result) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1375 | result = createUnaryOperation(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1376 | |
| 1377 | if (result) { |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1378 | if (invertedType) |
| 1379 | result = createInvertedSwizzle(precision, *node->getOperand(), result); |
| 1380 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1381 | builder.clearAccessChain(); |
| 1382 | builder.setAccessChainRValue(result); |
| 1383 | |
| 1384 | return false; // done with this node |
| 1385 | } |
| 1386 | |
| 1387 | // it must be a special case, check... |
| 1388 | switch (node->getOp()) { |
| 1389 | case glslang::EOpPostIncrement: |
| 1390 | case glslang::EOpPostDecrement: |
| 1391 | case glslang::EOpPreIncrement: |
| 1392 | case glslang::EOpPreDecrement: |
| 1393 | { |
| 1394 | // 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] | 1395 | spv::Id one = 0; |
| 1396 | if (node->getBasicType() == glslang::EbtFloat) |
| 1397 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 1398 | else if (node->getBasicType() == glslang::EbtDouble) |
| 1399 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1400 | #ifdef AMD_EXTENSIONS |
| 1401 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 1402 | one = builder.makeFloat16Constant(1.0F); |
| 1403 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1404 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1405 | one = builder.makeInt64Constant(1); |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1406 | #ifdef AMD_EXTENSIONS |
| 1407 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 1408 | one = builder.makeInt16Constant(1); |
| 1409 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1410 | else |
| 1411 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1412 | glslang::TOperator op; |
| 1413 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1414 | node->getOp() == glslang::EOpPostIncrement) |
| 1415 | op = glslang::EOpAdd; |
| 1416 | else |
| 1417 | op = glslang::EOpSub; |
| 1418 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1419 | spv::Id result = createBinaryOperation(op, precision, |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1420 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1421 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1422 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1423 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1424 | |
| 1425 | // The result of operation is always stored, but conditionally the |
| 1426 | // consumed result. The consumed result is always an r-value. |
| 1427 | builder.accessChainStore(result); |
| 1428 | builder.clearAccessChain(); |
| 1429 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1430 | node->getOp() == glslang::EOpPreDecrement) |
| 1431 | builder.setAccessChainRValue(result); |
| 1432 | else |
| 1433 | builder.setAccessChainRValue(operand); |
| 1434 | } |
| 1435 | |
| 1436 | return false; |
| 1437 | |
| 1438 | case glslang::EOpEmitStreamVertex: |
| 1439 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1440 | return false; |
| 1441 | case glslang::EOpEndStreamPrimitive: |
| 1442 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1443 | return false; |
| 1444 | |
| 1445 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1446 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1447 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1448 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 1452 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 1453 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1454 | if (node->getType().getQualifier().isSpecConstant()) |
| 1455 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1456 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1457 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1458 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 1459 | 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] | 1460 | |
| 1461 | // try texturing |
| 1462 | result = createImageTextureFunctionCall(node); |
| 1463 | if (result != spv::NoResult) { |
| 1464 | builder.clearAccessChain(); |
| 1465 | builder.setAccessChainRValue(result); |
| 1466 | |
| 1467 | return false; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1468 | #ifdef AMD_EXTENSIONS |
| 1469 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 1470 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 1471 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 1472 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1473 | // "imageStore" is a special case, which has no result |
| 1474 | return false; |
| 1475 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1476 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1477 | glslang::TOperator binOp = glslang::EOpNull; |
| 1478 | bool reduceComparison = true; |
| 1479 | bool isMatrix = false; |
| 1480 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1481 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1482 | |
| 1483 | assert(node->getOp()); |
| 1484 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1485 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1486 | |
| 1487 | switch (node->getOp()) { |
| 1488 | case glslang::EOpSequence: |
| 1489 | { |
| 1490 | if (preVisit) |
| 1491 | ++sequenceDepth; |
| 1492 | else |
| 1493 | --sequenceDepth; |
| 1494 | |
| 1495 | if (sequenceDepth == 1) { |
| 1496 | // If this is the parent node of all the functions, we want to see them |
| 1497 | // early, so all call points have actual SPIR-V functions to reference. |
| 1498 | // In all cases, still let the traverser visit the children for us. |
| 1499 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 1500 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1501 | // 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] | 1502 | // anything else gets there, so visit out of order, doing them all now. |
| 1503 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 1504 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 1505 | // 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] | 1506 | // so do them manually. |
| 1507 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 1508 | |
| 1509 | return false; |
| 1510 | } |
| 1511 | |
| 1512 | return true; |
| 1513 | } |
| 1514 | case glslang::EOpLinkerObjects: |
| 1515 | { |
| 1516 | if (visit == glslang::EvPreVisit) |
| 1517 | linkageOnly = true; |
| 1518 | else |
| 1519 | linkageOnly = false; |
| 1520 | |
| 1521 | return true; |
| 1522 | } |
| 1523 | case glslang::EOpComma: |
| 1524 | { |
| 1525 | // processing from left to right naturally leaves the right-most |
| 1526 | // lying around in the access chain |
| 1527 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1528 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 1529 | glslangOperands[i]->traverse(this); |
| 1530 | |
| 1531 | return false; |
| 1532 | } |
| 1533 | case glslang::EOpFunction: |
| 1534 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1535 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1536 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1537 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1538 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1539 | } else { |
| 1540 | handleFunctionEntry(node); |
| 1541 | } |
| 1542 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1543 | if (inEntryPoint) |
| 1544 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 1545 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1546 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | return true; |
| 1550 | case glslang::EOpParameters: |
| 1551 | // Parameters will have been consumed by EOpFunction processing, but not |
| 1552 | // the body, so we still visited the function node's children, making this |
| 1553 | // child redundant. |
| 1554 | return false; |
| 1555 | case glslang::EOpFunctionCall: |
| 1556 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1557 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1558 | if (node->isUserDefined()) |
| 1559 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 1560 | // 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] | 1561 | if (result) { |
| 1562 | builder.clearAccessChain(); |
| 1563 | builder.setAccessChainRValue(result); |
| 1564 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1565 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1566 | |
| 1567 | return false; |
| 1568 | } |
| 1569 | case glslang::EOpConstructMat2x2: |
| 1570 | case glslang::EOpConstructMat2x3: |
| 1571 | case glslang::EOpConstructMat2x4: |
| 1572 | case glslang::EOpConstructMat3x2: |
| 1573 | case glslang::EOpConstructMat3x3: |
| 1574 | case glslang::EOpConstructMat3x4: |
| 1575 | case glslang::EOpConstructMat4x2: |
| 1576 | case glslang::EOpConstructMat4x3: |
| 1577 | case glslang::EOpConstructMat4x4: |
| 1578 | case glslang::EOpConstructDMat2x2: |
| 1579 | case glslang::EOpConstructDMat2x3: |
| 1580 | case glslang::EOpConstructDMat2x4: |
| 1581 | case glslang::EOpConstructDMat3x2: |
| 1582 | case glslang::EOpConstructDMat3x3: |
| 1583 | case glslang::EOpConstructDMat3x4: |
| 1584 | case glslang::EOpConstructDMat4x2: |
| 1585 | case glslang::EOpConstructDMat4x3: |
| 1586 | case glslang::EOpConstructDMat4x4: |
LoopDawg | 174ccb8 | 2017-05-20 21:40:27 -0600 | [diff] [blame] | 1587 | case glslang::EOpConstructIMat2x2: |
| 1588 | case glslang::EOpConstructIMat2x3: |
| 1589 | case glslang::EOpConstructIMat2x4: |
| 1590 | case glslang::EOpConstructIMat3x2: |
| 1591 | case glslang::EOpConstructIMat3x3: |
| 1592 | case glslang::EOpConstructIMat3x4: |
| 1593 | case glslang::EOpConstructIMat4x2: |
| 1594 | case glslang::EOpConstructIMat4x3: |
| 1595 | case glslang::EOpConstructIMat4x4: |
| 1596 | case glslang::EOpConstructUMat2x2: |
| 1597 | case glslang::EOpConstructUMat2x3: |
| 1598 | case glslang::EOpConstructUMat2x4: |
| 1599 | case glslang::EOpConstructUMat3x2: |
| 1600 | case glslang::EOpConstructUMat3x3: |
| 1601 | case glslang::EOpConstructUMat3x4: |
| 1602 | case glslang::EOpConstructUMat4x2: |
| 1603 | case glslang::EOpConstructUMat4x3: |
| 1604 | case glslang::EOpConstructUMat4x4: |
| 1605 | case glslang::EOpConstructBMat2x2: |
| 1606 | case glslang::EOpConstructBMat2x3: |
| 1607 | case glslang::EOpConstructBMat2x4: |
| 1608 | case glslang::EOpConstructBMat3x2: |
| 1609 | case glslang::EOpConstructBMat3x3: |
| 1610 | case glslang::EOpConstructBMat3x4: |
| 1611 | case glslang::EOpConstructBMat4x2: |
| 1612 | case glslang::EOpConstructBMat4x3: |
| 1613 | case glslang::EOpConstructBMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1614 | #ifdef AMD_EXTENSIONS |
| 1615 | case glslang::EOpConstructF16Mat2x2: |
| 1616 | case glslang::EOpConstructF16Mat2x3: |
| 1617 | case glslang::EOpConstructF16Mat2x4: |
| 1618 | case glslang::EOpConstructF16Mat3x2: |
| 1619 | case glslang::EOpConstructF16Mat3x3: |
| 1620 | case glslang::EOpConstructF16Mat3x4: |
| 1621 | case glslang::EOpConstructF16Mat4x2: |
| 1622 | case glslang::EOpConstructF16Mat4x3: |
| 1623 | case glslang::EOpConstructF16Mat4x4: |
| 1624 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1625 | isMatrix = true; |
| 1626 | // fall through |
| 1627 | case glslang::EOpConstructFloat: |
| 1628 | case glslang::EOpConstructVec2: |
| 1629 | case glslang::EOpConstructVec3: |
| 1630 | case glslang::EOpConstructVec4: |
| 1631 | case glslang::EOpConstructDouble: |
| 1632 | case glslang::EOpConstructDVec2: |
| 1633 | case glslang::EOpConstructDVec3: |
| 1634 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1635 | #ifdef AMD_EXTENSIONS |
| 1636 | case glslang::EOpConstructFloat16: |
| 1637 | case glslang::EOpConstructF16Vec2: |
| 1638 | case glslang::EOpConstructF16Vec3: |
| 1639 | case glslang::EOpConstructF16Vec4: |
| 1640 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1641 | case glslang::EOpConstructBool: |
| 1642 | case glslang::EOpConstructBVec2: |
| 1643 | case glslang::EOpConstructBVec3: |
| 1644 | case glslang::EOpConstructBVec4: |
| 1645 | case glslang::EOpConstructInt: |
| 1646 | case glslang::EOpConstructIVec2: |
| 1647 | case glslang::EOpConstructIVec3: |
| 1648 | case glslang::EOpConstructIVec4: |
| 1649 | case glslang::EOpConstructUint: |
| 1650 | case glslang::EOpConstructUVec2: |
| 1651 | case glslang::EOpConstructUVec3: |
| 1652 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1653 | case glslang::EOpConstructInt64: |
| 1654 | case glslang::EOpConstructI64Vec2: |
| 1655 | case glslang::EOpConstructI64Vec3: |
| 1656 | case glslang::EOpConstructI64Vec4: |
| 1657 | case glslang::EOpConstructUint64: |
| 1658 | case glslang::EOpConstructU64Vec2: |
| 1659 | case glslang::EOpConstructU64Vec3: |
| 1660 | case glslang::EOpConstructU64Vec4: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 1661 | #ifdef AMD_EXTENSIONS |
| 1662 | case glslang::EOpConstructInt16: |
| 1663 | case glslang::EOpConstructI16Vec2: |
| 1664 | case glslang::EOpConstructI16Vec3: |
| 1665 | case glslang::EOpConstructI16Vec4: |
| 1666 | case glslang::EOpConstructUint16: |
| 1667 | case glslang::EOpConstructU16Vec2: |
| 1668 | case glslang::EOpConstructU16Vec3: |
| 1669 | case glslang::EOpConstructU16Vec4: |
| 1670 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1671 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1672 | case glslang::EOpConstructTextureSampler: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1673 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1674 | builder.setLine(node->getLoc().line); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1675 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1676 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1677 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1678 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1679 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1680 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1681 | std::vector<spv::Id> constituents; |
| 1682 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 1683 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1684 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1685 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1686 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1687 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1688 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1689 | |
| 1690 | builder.clearAccessChain(); |
| 1691 | builder.setAccessChainRValue(constructed); |
| 1692 | |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
| 1696 | // These six are component-wise compares with component-wise results. |
| 1697 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 1698 | case glslang::EOpLessThan: |
| 1699 | case glslang::EOpGreaterThan: |
| 1700 | case glslang::EOpLessThanEqual: |
| 1701 | case glslang::EOpGreaterThanEqual: |
| 1702 | case glslang::EOpVectorEqual: |
| 1703 | case glslang::EOpVectorNotEqual: |
| 1704 | { |
| 1705 | // Map the operation to a binary |
| 1706 | binOp = node->getOp(); |
| 1707 | reduceComparison = false; |
| 1708 | switch (node->getOp()) { |
| 1709 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 1710 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 1711 | default: binOp = node->getOp(); break; |
| 1712 | } |
| 1713 | |
| 1714 | break; |
| 1715 | } |
| 1716 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1717 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1718 | binOp = glslang::EOpMul; |
| 1719 | break; |
| 1720 | case glslang::EOpOuterProduct: |
| 1721 | // two vectors multiplied to make a matrix |
| 1722 | binOp = glslang::EOpOuterProduct; |
| 1723 | break; |
| 1724 | case glslang::EOpDot: |
| 1725 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1726 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1727 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 1728 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1729 | binOp = glslang::EOpMul; |
| 1730 | break; |
| 1731 | } |
| 1732 | case glslang::EOpMod: |
| 1733 | // when an aggregate, this is the floating-point mod built-in function, |
| 1734 | // which can be emitted by the one in createBinaryOperation() |
| 1735 | binOp = glslang::EOpMod; |
| 1736 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1737 | case glslang::EOpEmitVertex: |
| 1738 | case glslang::EOpEndPrimitive: |
| 1739 | case glslang::EOpBarrier: |
| 1740 | case glslang::EOpMemoryBarrier: |
| 1741 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 1742 | case glslang::EOpMemoryBarrierBuffer: |
| 1743 | case glslang::EOpMemoryBarrierImage: |
| 1744 | case glslang::EOpMemoryBarrierShared: |
| 1745 | case glslang::EOpGroupMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 1746 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 1747 | case glslang::EOpGroupMemoryBarrierWithGroupSync: |
| 1748 | case glslang::EOpWorkgroupMemoryBarrier: |
| 1749 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1750 | noReturnValue = true; |
| 1751 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 1752 | break; |
| 1753 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1754 | case glslang::EOpAtomicAdd: |
| 1755 | case glslang::EOpAtomicMin: |
| 1756 | case glslang::EOpAtomicMax: |
| 1757 | case glslang::EOpAtomicAnd: |
| 1758 | case glslang::EOpAtomicOr: |
| 1759 | case glslang::EOpAtomicXor: |
| 1760 | case glslang::EOpAtomicExchange: |
| 1761 | case glslang::EOpAtomicCompSwap: |
| 1762 | atomic = true; |
| 1763 | break; |
| 1764 | |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 1765 | case glslang::EOpAtomicCounterAdd: |
| 1766 | case glslang::EOpAtomicCounterSubtract: |
| 1767 | case glslang::EOpAtomicCounterMin: |
| 1768 | case glslang::EOpAtomicCounterMax: |
| 1769 | case glslang::EOpAtomicCounterAnd: |
| 1770 | case glslang::EOpAtomicCounterOr: |
| 1771 | case glslang::EOpAtomicCounterXor: |
| 1772 | case glslang::EOpAtomicCounterExchange: |
| 1773 | case glslang::EOpAtomicCounterCompSwap: |
| 1774 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 1775 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 1776 | atomic = true; |
| 1777 | break; |
| 1778 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1779 | default: |
| 1780 | break; |
| 1781 | } |
| 1782 | |
| 1783 | // |
| 1784 | // See if it maps to a regular operation. |
| 1785 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1786 | if (binOp != glslang::EOpNull) { |
| 1787 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 1788 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 1789 | assert(left && right); |
| 1790 | |
| 1791 | builder.clearAccessChain(); |
| 1792 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1793 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1794 | |
| 1795 | builder.clearAccessChain(); |
| 1796 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1797 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1798 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1799 | builder.setLine(node->getLoc().line); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1800 | result = createBinaryOperation(binOp, precision, TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1801 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1802 | left->getType().getBasicType(), reduceComparison); |
| 1803 | |
| 1804 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1805 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1806 | builder.clearAccessChain(); |
| 1807 | builder.setAccessChainRValue(result); |
| 1808 | |
| 1809 | return false; |
| 1810 | } |
| 1811 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1812 | // |
| 1813 | // Create the list of operands. |
| 1814 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1815 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1816 | std::vector<spv::Id> operands; |
| 1817 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1818 | // special case l-value operands; there are just a few |
| 1819 | bool lvalue = false; |
| 1820 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1821 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1822 | case glslang::EOpModf: |
| 1823 | if (arg == 1) |
| 1824 | lvalue = true; |
| 1825 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1826 | case glslang::EOpInterpolateAtSample: |
| 1827 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1828 | #ifdef AMD_EXTENSIONS |
| 1829 | case glslang::EOpInterpolateAtVertex: |
| 1830 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1831 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1832 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1833 | |
| 1834 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1835 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 1836 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1837 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 1838 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 1839 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1840 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 1841 | case glslang::EOpAtomicAdd: |
| 1842 | case glslang::EOpAtomicMin: |
| 1843 | case glslang::EOpAtomicMax: |
| 1844 | case glslang::EOpAtomicAnd: |
| 1845 | case glslang::EOpAtomicOr: |
| 1846 | case glslang::EOpAtomicXor: |
| 1847 | case glslang::EOpAtomicExchange: |
| 1848 | case glslang::EOpAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 1849 | case glslang::EOpAtomicCounterAdd: |
| 1850 | case glslang::EOpAtomicCounterSubtract: |
| 1851 | case glslang::EOpAtomicCounterMin: |
| 1852 | case glslang::EOpAtomicCounterMax: |
| 1853 | case glslang::EOpAtomicCounterAnd: |
| 1854 | case glslang::EOpAtomicCounterOr: |
| 1855 | case glslang::EOpAtomicCounterXor: |
| 1856 | case glslang::EOpAtomicCounterExchange: |
| 1857 | case glslang::EOpAtomicCounterCompSwap: |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 1858 | if (arg == 0) |
| 1859 | lvalue = true; |
| 1860 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1861 | case glslang::EOpAddCarry: |
| 1862 | case glslang::EOpSubBorrow: |
| 1863 | if (arg == 2) |
| 1864 | lvalue = true; |
| 1865 | break; |
| 1866 | case glslang::EOpUMulExtended: |
| 1867 | case glslang::EOpIMulExtended: |
| 1868 | if (arg >= 2) |
| 1869 | lvalue = true; |
| 1870 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1871 | default: |
| 1872 | break; |
| 1873 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1874 | builder.clearAccessChain(); |
| 1875 | if (invertedType != spv::NoType && arg == 0) |
| 1876 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 1877 | else |
| 1878 | glslangOperands[arg]->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1879 | if (lvalue) |
| 1880 | operands.push_back(builder.accessChainGetLValue()); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1881 | else { |
| 1882 | builder.setLine(node->getLoc().line); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1883 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1884 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1885 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1886 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1887 | builder.setLine(node->getLoc().line); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1888 | if (atomic) { |
| 1889 | // Handle all atomics |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1890 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1891 | } else { |
| 1892 | // Pass through to generic operations. |
| 1893 | switch (glslangOperands.size()) { |
| 1894 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1895 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1896 | break; |
| 1897 | case 1: |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1898 | result = createUnaryOperation( |
| 1899 | node->getOp(), precision, |
| 1900 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1901 | resultType(), operands.front(), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1902 | glslangOperands[0]->getAsTyped()->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1903 | break; |
| 1904 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1905 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1906 | break; |
| 1907 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1908 | if (invertedType) |
| 1909 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | if (noReturnValue) |
| 1913 | return false; |
| 1914 | |
| 1915 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1916 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1917 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1918 | } else { |
| 1919 | builder.clearAccessChain(); |
| 1920 | builder.setAccessChainRValue(result); |
| 1921 | return false; |
| 1922 | } |
| 1923 | } |
| 1924 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1925 | // This path handles both if-then-else and ?: |
| 1926 | // The if-then-else has a node type of void, while |
| 1927 | // ?: has either a void or a non-void node type |
| 1928 | // |
| 1929 | // Leaving the result, when not void: |
| 1930 | // GLSL only has r-values as the result of a :?, but |
| 1931 | // if we have an l-value, that can be more efficient if it will |
| 1932 | // become the base of a complex r-value expression, because the |
| 1933 | // 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] | 1934 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 1935 | { |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1936 | // See if it simple and safe to generate OpSelect instead of using control flow. |
| 1937 | // Crucially, side effects must be avoided, and there are performance trade-offs. |
| 1938 | // Return true if good idea (and safe) for OpSelect, false otherwise. |
| 1939 | const auto selectPolicy = [&]() -> bool { |
John Kessenich | 0479437 | 2017-03-01 13:49:11 -0700 | [diff] [blame] | 1940 | if ((!node->getType().isScalar() && !node->getType().isVector()) || |
| 1941 | node->getBasicType() == glslang::EbtVoid) |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1942 | return false; |
| 1943 | |
| 1944 | if (node->getTrueBlock() == nullptr || |
| 1945 | node->getFalseBlock() == nullptr) |
| 1946 | return false; |
| 1947 | |
| 1948 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 1949 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 1950 | |
| 1951 | // return true if a single operand to ? : is okay for OpSelect |
| 1952 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 1953 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1954 | }; |
| 1955 | |
| 1956 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 1957 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 1958 | }; |
| 1959 | |
| 1960 | // Emit OpSelect for this selection. |
| 1961 | const auto handleAsOpSelect = [&]() { |
| 1962 | node->getCondition()->traverse(this); |
| 1963 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 1964 | node->getTrueBlock()->traverse(this); |
| 1965 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 1966 | node->getFalseBlock()->traverse(this); |
| 1967 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 1968 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 1969 | builder.setLine(node->getLoc().line); |
| 1970 | |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 1971 | // smear condition to vector, if necessary (AST is always scalar) |
| 1972 | if (builder.isVector(trueValue)) |
| 1973 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 1974 | builder.makeVectorType(builder.makeBoolType(), |
| 1975 | builder.getNumComponents(trueValue))); |
| 1976 | |
| 1977 | spv::Id select = builder.createTriOp(spv::OpSelect, |
| 1978 | convertGlslangToSpvType(node->getType()), condition, |
| 1979 | trueValue, falseValue); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1980 | builder.clearAccessChain(); |
| 1981 | builder.setAccessChainRValue(select); |
| 1982 | }; |
| 1983 | |
| 1984 | // Try for OpSelect |
| 1985 | |
| 1986 | if (selectPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 1987 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1988 | if (node->getType().getQualifier().isSpecConstant()) |
| 1989 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1990 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1991 | handleAsOpSelect(); |
| 1992 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1993 | } |
| 1994 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 1995 | // Instead, emit control flow... |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1996 | // Don't handle results as temporaries, because there will be two names |
| 1997 | // and better to leave SSA to later passes. |
| 1998 | spv::Id result = (node->getBasicType() == glslang::EbtVoid) |
| 1999 | ? spv::NoResult |
| 2000 | : builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 2001 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2002 | // emit the condition before doing anything with selection |
| 2003 | node->getCondition()->traverse(this); |
| 2004 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2005 | // Selection control: |
| 2006 | const spv::SelectionControlMask control = TranslateSelectionControl(node->getSelectionControl()); |
| 2007 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2008 | // make an "if" based on the value created by the condition |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2009 | spv::Builder::If ifBuilder(accessChainLoad(node->getCondition()->getType()), control, builder); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2010 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2011 | // emit the "then" statement |
| 2012 | if (node->getTrueBlock() != nullptr) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2013 | node->getTrueBlock()->traverse(this); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2014 | if (result != spv::NoResult) |
| 2015 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2016 | } |
| 2017 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2018 | if (node->getFalseBlock() != nullptr) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2019 | ifBuilder.makeBeginElse(); |
| 2020 | // emit the "else" statement |
| 2021 | node->getFalseBlock()->traverse(this); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2022 | if (result != spv::NoResult) |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2023 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2024 | } |
| 2025 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2026 | // finish off the control flow |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2027 | ifBuilder.makeEndIf(); |
| 2028 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 2029 | if (result != spv::NoResult) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2030 | // GLSL only has r-values as the result of a :?, but |
| 2031 | // if we have an l-value, that can be more efficient if it will |
| 2032 | // become the base of a complex r-value expression, because the |
| 2033 | // next layer copies r-values into memory to use the access-chain mechanism |
| 2034 | builder.clearAccessChain(); |
| 2035 | builder.setAccessChainLValue(result); |
| 2036 | } |
| 2037 | |
| 2038 | return false; |
| 2039 | } |
| 2040 | |
| 2041 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 2042 | { |
| 2043 | // emit and get the condition before doing anything with switch |
| 2044 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2045 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2046 | |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 2047 | // Selection control: |
| 2048 | const spv::SelectionControlMask control = TranslateSelectionControl(node->getSelectionControl()); |
| 2049 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2050 | // browse the children to sort out code segments |
| 2051 | int defaultSegment = -1; |
| 2052 | std::vector<TIntermNode*> codeSegments; |
| 2053 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 2054 | std::vector<int> caseValues; |
| 2055 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 2056 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 2057 | TIntermNode* child = *c; |
| 2058 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2059 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2060 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 2061 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2062 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2063 | } else |
| 2064 | codeSegments.push_back(child); |
| 2065 | } |
| 2066 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2067 | // 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] | 2068 | // statements between the last case and the end of the switch statement |
| 2069 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 2070 | (int)codeSegments.size() == defaultSegment) |
| 2071 | codeSegments.push_back(nullptr); |
| 2072 | |
| 2073 | // make the switch statement |
| 2074 | 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] | 2075 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2076 | |
| 2077 | // emit all the code in the segments |
| 2078 | breakForLoop.push(false); |
| 2079 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 2080 | builder.nextSwitchSegment(segmentBlocks, s); |
| 2081 | if (codeSegments[s]) |
| 2082 | codeSegments[s]->traverse(this); |
| 2083 | else |
| 2084 | builder.addSwitchBreak(); |
| 2085 | } |
| 2086 | breakForLoop.pop(); |
| 2087 | |
| 2088 | builder.endSwitch(segmentBlocks); |
| 2089 | |
| 2090 | return false; |
| 2091 | } |
| 2092 | |
| 2093 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 2094 | { |
| 2095 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2096 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2097 | |
| 2098 | builder.clearAccessChain(); |
| 2099 | builder.setAccessChainRValue(constant); |
| 2100 | } |
| 2101 | |
| 2102 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 2103 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2104 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2105 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2106 | |
| 2107 | // Loop control: |
| 2108 | const spv::LoopControlMask control = TranslateLoopControl(node->getLoopControl()); |
| 2109 | |
| 2110 | // TODO: dependency length |
| 2111 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2112 | // Spec requires back edges to target header blocks, and every header block |
| 2113 | // must dominate its merge block. Make a header block first to ensure these |
| 2114 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 2115 | // by a block-ending branch. But we don't want to put any other body/test |
| 2116 | // instructions in it, since the body/test may have arbitrary instructions, |
| 2117 | // including merges of its own. |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2118 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2119 | builder.setBuildPoint(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 2120 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2121 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 2122 | spv::Block& test = builder.makeNewBlock(); |
| 2123 | builder.createBranch(&test); |
| 2124 | |
| 2125 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2126 | node->getTest()->traverse(this); |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2127 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2128 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 2129 | |
| 2130 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2131 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2132 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2133 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2134 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2135 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2136 | |
| 2137 | builder.setBuildPoint(&blocks.continue_target); |
| 2138 | if (node->getTerminal()) |
| 2139 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2140 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 2141 | } else { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2142 | builder.setLine(node->getLoc().line); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2143 | builder.createBranch(&blocks.body); |
| 2144 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2145 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2146 | builder.setBuildPoint(&blocks.body); |
| 2147 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2148 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2149 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2150 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2151 | |
| 2152 | builder.setBuildPoint(&blocks.continue_target); |
| 2153 | if (node->getTerminal()) |
| 2154 | node->getTerminal()->traverse(this); |
| 2155 | if (node->getTest()) { |
| 2156 | node->getTest()->traverse(this); |
| 2157 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2158 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2159 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2160 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 2161 | // TODO: unless there was a break/return/discard instruction |
| 2162 | // somewhere in the body, this is an infinite loop, so we should |
| 2163 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2164 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2165 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2166 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2167 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2168 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2169 | return false; |
| 2170 | } |
| 2171 | |
| 2172 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 2173 | { |
| 2174 | if (node->getExpression()) |
| 2175 | node->getExpression()->traverse(this); |
| 2176 | |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 2177 | builder.setLine(node->getLoc().line); |
| 2178 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2179 | switch (node->getFlowOp()) { |
| 2180 | case glslang::EOpKill: |
| 2181 | builder.makeDiscard(); |
| 2182 | break; |
| 2183 | case glslang::EOpBreak: |
| 2184 | if (breakForLoop.top()) |
| 2185 | builder.createLoopExit(); |
| 2186 | else |
| 2187 | builder.addSwitchBreak(); |
| 2188 | break; |
| 2189 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2190 | builder.createLoopContinue(); |
| 2191 | break; |
| 2192 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2193 | if (node->getExpression()) { |
| 2194 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 2195 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 2196 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 2197 | builder.clearAccessChain(); |
| 2198 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 2199 | builder.setAccessChainLValue(copyId); |
| 2200 | multiTypeStore(glslangReturnType, returnId); |
| 2201 | returnId = builder.createLoad(copyId); |
| 2202 | } |
| 2203 | builder.makeReturn(false, returnId); |
| 2204 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2205 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2206 | |
| 2207 | builder.clearAccessChain(); |
| 2208 | break; |
| 2209 | |
| 2210 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2211 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2212 | break; |
| 2213 | } |
| 2214 | |
| 2215 | return false; |
| 2216 | } |
| 2217 | |
| 2218 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 2219 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2220 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2221 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2222 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 2223 | if (node->getQualifier().isConstant()) { |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2224 | return createSpvConstant(*node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2228 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2229 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 2230 | |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2231 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2232 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16) || |
| 2233 | node->getType().containsBasicType(glslang::EbtInt16) || |
| 2234 | node->getType().containsBasicType(glslang::EbtUint16); |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2235 | if (contains16BitType) { |
| 2236 | if (storageClass == spv::StorageClassInput || storageClass == spv::StorageClassOutput) { |
| 2237 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2238 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
| 2239 | } else if (storageClass == spv::StorageClassPushConstant) { |
| 2240 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2241 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 2242 | } else if (storageClass == spv::StorageClassUniform) { |
| 2243 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2244 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 2245 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2246 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 2247 | } |
| 2248 | } |
| 2249 | #endif |
| 2250 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2251 | const char* name = node->getName().c_str(); |
| 2252 | if (glslang::IsAnonymous(name)) |
| 2253 | name = ""; |
| 2254 | |
| 2255 | return builder.createVariable(storageClass, spvType, name); |
| 2256 | } |
| 2257 | |
| 2258 | // Return type Id of the sampled type. |
| 2259 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 2260 | { |
| 2261 | switch (sampler.type) { |
| 2262 | case glslang::EbtFloat: return builder.makeFloatType(32); |
| 2263 | case glslang::EbtInt: return builder.makeIntType(32); |
| 2264 | case glslang::EbtUint: return builder.makeUintType(32); |
| 2265 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2266 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2267 | return builder.makeFloatType(32); |
| 2268 | } |
| 2269 | } |
| 2270 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2271 | // If node is a swizzle operation, return the type that should be used if |
| 2272 | // the swizzle base is first consumed by another operation, before the swizzle |
| 2273 | // is applied. |
| 2274 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 2275 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2276 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2277 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2278 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 2279 | else |
| 2280 | return spv::NoType; |
| 2281 | } |
| 2282 | |
| 2283 | // When inverting a swizzle with a parent op, this function |
| 2284 | // will apply the swizzle operation to a completed parent operation. |
| 2285 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 2286 | { |
| 2287 | std::vector<unsigned> swizzle; |
| 2288 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 2289 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 2290 | } |
| 2291 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2292 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 2293 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 2294 | { |
| 2295 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 2296 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 2297 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2298 | } |
| 2299 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2300 | // Convert from a glslang type to an SPV type, by calling into a |
| 2301 | // recursive version of this function. This establishes the inherited |
| 2302 | // layout state rooted from the top-level type. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2303 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type) |
| 2304 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2305 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier()); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | // 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] | 2309 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2310 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2311 | 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] | 2312 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2313 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2314 | |
| 2315 | switch (type.getBasicType()) { |
| 2316 | case glslang::EbtVoid: |
| 2317 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2318 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2319 | break; |
| 2320 | case glslang::EbtFloat: |
| 2321 | spvType = builder.makeFloatType(32); |
| 2322 | break; |
| 2323 | case glslang::EbtDouble: |
| 2324 | spvType = builder.makeFloatType(64); |
| 2325 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2326 | #ifdef AMD_EXTENSIONS |
| 2327 | case glslang::EbtFloat16: |
| 2328 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2329 | spvType = builder.makeFloatType(16); |
| 2330 | break; |
| 2331 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2332 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2333 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 2334 | // a 32-bit int where non-0 means true. |
| 2335 | if (explicitLayout != glslang::ElpNone) |
| 2336 | spvType = builder.makeUintType(32); |
| 2337 | else |
| 2338 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2339 | break; |
| 2340 | case glslang::EbtInt: |
| 2341 | spvType = builder.makeIntType(32); |
| 2342 | break; |
| 2343 | case glslang::EbtUint: |
| 2344 | spvType = builder.makeUintType(32); |
| 2345 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2346 | case glslang::EbtInt64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2347 | spvType = builder.makeIntType(64); |
| 2348 | break; |
| 2349 | case glslang::EbtUint64: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2350 | spvType = builder.makeUintType(64); |
| 2351 | break; |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 2352 | #ifdef AMD_EXTENSIONS |
| 2353 | case glslang::EbtInt16: |
| 2354 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 2355 | spvType = builder.makeIntType(16); |
| 2356 | break; |
| 2357 | case glslang::EbtUint16: |
| 2358 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 2359 | spvType = builder.makeUintType(16); |
| 2360 | break; |
| 2361 | #endif |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2362 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 2363 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2364 | spvType = builder.makeUintType(32); |
| 2365 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2366 | case glslang::EbtSampler: |
| 2367 | { |
| 2368 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2369 | if (sampler.sampler) { |
| 2370 | // pure sampler |
| 2371 | spvType = builder.makeSamplerType(); |
| 2372 | } else { |
| 2373 | // an image is present, make its type |
| 2374 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 2375 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 2376 | if (sampler.combined) { |
| 2377 | // already has both image and sampler, make the combined type |
| 2378 | spvType = builder.makeSampledImageType(spvType); |
| 2379 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2380 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2381 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2382 | break; |
| 2383 | case glslang::EbtStruct: |
| 2384 | case glslang::EbtBlock: |
| 2385 | { |
| 2386 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2387 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2388 | |
| 2389 | // Try to share structs for different layouts, but not yet for other |
| 2390 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2391 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2392 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2393 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2394 | break; |
| 2395 | |
| 2396 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2397 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2398 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 2399 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2400 | } |
| 2401 | break; |
| 2402 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2403 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2404 | break; |
| 2405 | } |
| 2406 | |
| 2407 | if (type.isMatrix()) |
| 2408 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 2409 | else { |
| 2410 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 2411 | if (type.getVectorSize() > 1) |
| 2412 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 2413 | } |
| 2414 | |
| 2415 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2416 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 2417 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2418 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2419 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2420 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 2421 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2422 | // Use a dummy glslang type for querying internal strides of |
| 2423 | // arrays of arrays, but using just a one-dimensional array. |
| 2424 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
| 2425 | while (simpleArrayType.getArraySizes().getNumDims() > 1) |
| 2426 | simpleArrayType.getArraySizes().dereference(); |
| 2427 | |
| 2428 | // Will compute the higher-order strides here, rather than making a whole |
| 2429 | // pile of types and doing repetitive recursion on their contents. |
| 2430 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 2431 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2432 | |
| 2433 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2434 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2435 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2436 | if (stride > 0) |
| 2437 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2438 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2439 | } |
| 2440 | } else { |
| 2441 | // single-dimensional array, and don't yet have stride |
| 2442 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2443 | // 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] | 2444 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 2445 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2446 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2447 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2448 | // Do the outer dimension, which might not be known for a runtime-sized array |
| 2449 | if (type.isRuntimeSizedArray()) { |
| 2450 | spvType = builder.makeRuntimeArray(spvType); |
| 2451 | } else { |
| 2452 | assert(type.getOuterArraySize() > 0); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2453 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2454 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2455 | if (stride > 0) |
| 2456 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | return spvType; |
| 2460 | } |
| 2461 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2462 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 2463 | // |
| 2464 | // Identify interface members that don't have their required extension turned on. |
| 2465 | // |
| 2466 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 2467 | { |
| 2468 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 2469 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2470 | if (member.getFieldName() == "gl_ViewportMask" && |
| 2471 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 2472 | return true; |
| 2473 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 2474 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2475 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2476 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 2477 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2478 | return true; |
| 2479 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 2480 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2481 | return true; |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2482 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 2483 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2484 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2485 | |
| 2486 | return false; |
| 2487 | }; |
| 2488 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2489 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 2490 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 2491 | // Mutually recursive with convertGlslangToSpvType(). |
| 2492 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 2493 | const glslang::TTypeList* glslangMembers, |
| 2494 | glslang::TLayoutPacking explicitLayout, |
| 2495 | const glslang::TQualifier& qualifier) |
| 2496 | { |
| 2497 | // Create a vector of struct types for SPIR-V to consume |
| 2498 | std::vector<spv::Id> spvMembers; |
| 2499 | 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] | 2500 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2501 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2502 | if (glslangMember.hiddenMember()) { |
| 2503 | ++memberDelta; |
| 2504 | if (type.getBasicType() == glslang::EbtBlock) |
| 2505 | memberRemapper[glslangMembers][i] = -1; |
| 2506 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2507 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2508 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2509 | if (filterMember(glslangMember)) |
| 2510 | continue; |
| 2511 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2512 | // modify just this child's view of the qualifier |
| 2513 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2514 | InheritQualifiers(memberQualifier, qualifier); |
| 2515 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2516 | // manually inherit location |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2517 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2518 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2519 | |
| 2520 | // recurse |
| 2521 | spvMembers.push_back(convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier)); |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | // Make the SPIR-V type |
| 2526 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2527 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2528 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 2529 | |
| 2530 | // Decorate it |
| 2531 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 2532 | |
| 2533 | return spvType; |
| 2534 | } |
| 2535 | |
| 2536 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 2537 | const glslang::TTypeList* glslangMembers, |
| 2538 | glslang::TLayoutPacking explicitLayout, |
| 2539 | const glslang::TQualifier& qualifier, |
| 2540 | spv::Id spvType) |
| 2541 | { |
| 2542 | // Name and decorate the non-hidden members |
| 2543 | int offset = -1; |
| 2544 | int locationOffset = 0; // for use within the members of this struct |
| 2545 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2546 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2547 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2548 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2549 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2550 | if (filterMember(glslangMember)) |
| 2551 | continue; |
| 2552 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2553 | |
| 2554 | // modify just this child's view of the qualifier |
| 2555 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2556 | InheritQualifiers(memberQualifier, qualifier); |
| 2557 | |
| 2558 | // using -1 above to indicate a hidden member |
| 2559 | if (member >= 0) { |
| 2560 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 2561 | addMemberDecoration(spvType, member, TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 2562 | addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 2563 | // 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] | 2564 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 2565 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 2566 | if (type.getBasicType() == glslang::EbtBlock || |
| 2567 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2568 | addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 2569 | addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
| 2570 | } |
| 2571 | } |
| 2572 | addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
| 2573 | |
Rex Xu | 286ca43 | 2017-07-27 14:33:16 +0800 | [diff] [blame] | 2574 | if (type.getBasicType() == glslang::EbtBlock && |
| 2575 | qualifier.storage == glslang::EvqBuffer) { |
| 2576 | // Add memory decorations only to top-level members of shader storage block |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2577 | std::vector<spv::Decoration> memory; |
| 2578 | TranslateMemoryDecoration(memberQualifier, memory); |
| 2579 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 2580 | addMemberDecoration(spvType, member, memory[i]); |
| 2581 | } |
| 2582 | |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2583 | // Location assignment was already completed correctly by the front end, |
| 2584 | // just track whether a member needs to be decorated. |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2585 | // Ignore member locations if the container is an array, as that's |
John Kessenich | 7cdf3fc | 2017-06-04 13:22:39 -0600 | [diff] [blame] | 2586 | // ill-specified and decisions have been made to not allow this. |
| 2587 | if (! type.isArray() && memberQualifier.hasLocation()) |
| 2588 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2589 | |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2590 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 2591 | locationOffset += glslangIntermediate->computeTypeLocationSize(glslangMember); |
| 2592 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2593 | // component, XFB, others |
| 2594 | if (glslangMember.getQualifier().hasComponent()) |
| 2595 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, glslangMember.getQualifier().layoutComponent); |
| 2596 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 2597 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, glslangMember.getQualifier().layoutXfbOffset); |
| 2598 | else if (explicitLayout != glslang::ElpNone) { |
| 2599 | // figure out what to do with offset, which is accumulating |
| 2600 | int nextOffset; |
| 2601 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 2602 | if (offset >= 0) |
| 2603 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 2604 | offset = nextOffset; |
| 2605 | } |
| 2606 | |
| 2607 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 2608 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
| 2609 | |
| 2610 | // built-in variable decorations |
| 2611 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 2612 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2613 | addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2614 | |
| 2615 | #ifdef NV_EXTENSIONS |
| 2616 | if (builtIn == spv::BuiltInLayer) { |
| 2617 | // SPV_NV_viewport_array2 extension |
| 2618 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 2619 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 2620 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 2621 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 2622 | } |
| 2623 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 2624 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 2625 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 2626 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 2627 | } |
| 2628 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 2629 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 2630 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 2631 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 2632 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 2633 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2634 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | // Decorate the structure |
| 2639 | addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 2640 | addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2641 | if (type.getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
| 2642 | builder.addCapability(spv::CapabilityGeometryStreams); |
| 2643 | builder.addDecoration(spvType, spv::DecorationStream, type.getQualifier().layoutStream); |
| 2644 | } |
| 2645 | if (glslangIntermediate->getXfbMode()) { |
| 2646 | builder.addCapability(spv::CapabilityTransformFeedback); |
| 2647 | if (type.getQualifier().hasXfbStride()) |
| 2648 | builder.addDecoration(spvType, spv::DecorationXfbStride, type.getQualifier().layoutXfbStride); |
| 2649 | if (type.getQualifier().hasXfbBuffer()) |
| 2650 | builder.addDecoration(spvType, spv::DecorationXfbBuffer, type.getQualifier().layoutXfbBuffer); |
| 2651 | } |
| 2652 | } |
| 2653 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2654 | // Turn the expression forming the array size into an id. |
| 2655 | // This is not quite trivial, because of specialization constants. |
| 2656 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 2657 | // a specialization constant expression is. |
| 2658 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 2659 | { |
| 2660 | // First, see if this is sized with a node, meaning a specialization constant: |
| 2661 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 2662 | if (specNode != nullptr) { |
| 2663 | builder.clearAccessChain(); |
| 2664 | specNode->traverse(this); |
| 2665 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 2666 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2667 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2668 | // Otherwise, need a compile-time (front end) size, get it: |
| 2669 | int size = arraySizes.getDimSize(dim); |
| 2670 | assert(size > 0); |
| 2671 | return builder.makeUintConstant(size); |
| 2672 | } |
| 2673 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2674 | // Wrap the builder's accessChainLoad to: |
| 2675 | // - localize handling of RelaxedPrecision |
| 2676 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 2677 | // (avoids unnecessary work and possible type punning for structures) |
| 2678 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2679 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 2680 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2681 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2682 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), nominalTypeId); |
| 2683 | |
| 2684 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2685 | if (type.getBasicType() == glslang::EbtBool) { |
| 2686 | if (builder.isScalarType(nominalTypeId)) { |
| 2687 | // Conversion for bool |
| 2688 | spv::Id boolType = builder.makeBoolType(); |
| 2689 | if (nominalTypeId != boolType) |
| 2690 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 2691 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2692 | // Conversion for bvec |
| 2693 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2694 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 2695 | if (nominalTypeId != bvecType) |
| 2696 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 2697 | } |
| 2698 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2699 | |
| 2700 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2703 | // Wrap the builder's accessChainStore to: |
| 2704 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2705 | // |
| 2706 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2707 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 2708 | { |
| 2709 | // Need to convert to abstract types when necessary |
| 2710 | if (type.getBasicType() == glslang::EbtBool) { |
| 2711 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2712 | |
| 2713 | if (builder.isScalarType(nominalTypeId)) { |
| 2714 | // Conversion for bool |
| 2715 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2716 | if (nominalTypeId != boolType) { |
| 2717 | // keep these outside arguments, for determinant order-of-evaluation |
| 2718 | spv::Id one = builder.makeUintConstant(1); |
| 2719 | spv::Id zero = builder.makeUintConstant(0); |
| 2720 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 2721 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2722 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2723 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2724 | // Conversion for bvec |
| 2725 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2726 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2727 | if (nominalTypeId != bvecType) { |
| 2728 | // keep these outside arguments, for determinant order-of-evaluation |
John Kessenich | 7b8c386 | 2017-05-19 23:44:51 -0600 | [diff] [blame] | 2729 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 2730 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 2731 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame] | 2732 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2733 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 2734 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | builder.accessChainStore(rvalue); |
| 2739 | } |
| 2740 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2741 | // For storing when types match at the glslang level, but not might match at the |
| 2742 | // SPIR-V level. |
| 2743 | // |
| 2744 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2745 | // 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] | 2746 | // as in a member-decorated way. |
| 2747 | // |
| 2748 | // NOTE: This function can handle any store request; if it's not special it |
| 2749 | // simplifies to a simple OpStore. |
| 2750 | // |
| 2751 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 2752 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 2753 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2754 | // we only do the complex path here if it's an aggregate |
| 2755 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2756 | accessChainStore(type, rValue); |
| 2757 | return; |
| 2758 | } |
| 2759 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2760 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2761 | spv::Id rType = builder.getTypeId(rValue); |
| 2762 | spv::Id lValue = builder.accessChainGetLValue(); |
| 2763 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 2764 | if (lType == rType) { |
| 2765 | accessChainStore(type, rValue); |
| 2766 | return; |
| 2767 | } |
| 2768 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2769 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2770 | // where the two types were the same type in GLSL. This requires member |
| 2771 | // by member copy, recursively. |
| 2772 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2773 | // If an array, copy element by element. |
| 2774 | if (type.isArray()) { |
| 2775 | glslang::TType glslangElementType(type, 0); |
| 2776 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2777 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 2778 | // get the source member |
| 2779 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2780 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2781 | // set up the target storage |
| 2782 | builder.clearAccessChain(); |
| 2783 | builder.setAccessChainLValue(lValue); |
| 2784 | builder.accessChainPush(builder.makeIntConstant(index)); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2785 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2786 | // store the member |
| 2787 | multiTypeStore(glslangElementType, elementRValue); |
| 2788 | } |
| 2789 | } else { |
| 2790 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2791 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2792 | // loop over structure members |
| 2793 | const glslang::TTypeList& members = *type.getStruct(); |
| 2794 | for (int m = 0; m < (int)members.size(); ++m) { |
| 2795 | const glslang::TType& glslangMemberType = *members[m].type; |
| 2796 | |
| 2797 | // get the source member |
| 2798 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 2799 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 2800 | |
| 2801 | // set up the target storage |
| 2802 | builder.clearAccessChain(); |
| 2803 | builder.setAccessChainLValue(lValue); |
| 2804 | builder.accessChainPush(builder.makeIntConstant(m)); |
| 2805 | |
| 2806 | // store the member |
| 2807 | multiTypeStore(glslangMemberType, memberRValue); |
| 2808 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2809 | } |
| 2810 | } |
| 2811 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2812 | // Decide whether or not this type should be |
| 2813 | // decorated with offsets and strides, and if so |
| 2814 | // whether std140 or std430 rules should be applied. |
| 2815 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2816 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2817 | // has to be a block |
| 2818 | if (type.getBasicType() != glslang::EbtBlock) |
| 2819 | return glslang::ElpNone; |
| 2820 | |
| 2821 | // has to be a uniform or buffer block |
| 2822 | if (type.getQualifier().storage != glslang::EvqUniform && |
| 2823 | type.getQualifier().storage != glslang::EvqBuffer) |
| 2824 | return glslang::ElpNone; |
| 2825 | |
| 2826 | // return the layout to use |
| 2827 | switch (type.getQualifier().layoutPacking) { |
| 2828 | case glslang::ElpStd140: |
| 2829 | case glslang::ElpStd430: |
| 2830 | return type.getQualifier().layoutPacking; |
| 2831 | default: |
| 2832 | return glslang::ElpNone; |
| 2833 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2834 | } |
| 2835 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2836 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2837 | 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] | 2838 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2839 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2840 | int stride; |
| 2841 | glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 2842 | |
| 2843 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2844 | } |
| 2845 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2846 | // 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] | 2847 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2848 | 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] | 2849 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2850 | glslang::TType elementType; |
| 2851 | elementType.shallowCopy(matrixType); |
| 2852 | elementType.clearArraySizes(); |
| 2853 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2854 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2855 | int stride; |
| 2856 | glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
| 2857 | |
| 2858 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2861 | // Given a member type of a struct, realign the current offset for it, and compute |
| 2862 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 2863 | // on the next call. |
| 2864 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 2865 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 2866 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 2867 | 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] | 2868 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2869 | { |
| 2870 | // this will get a positive value when deemed necessary |
| 2871 | nextOffset = -1; |
| 2872 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2873 | // override anything in currentOffset with user-set offset |
| 2874 | if (memberType.getQualifier().hasOffset()) |
| 2875 | currentOffset = memberType.getQualifier().layoutOffset; |
| 2876 | |
| 2877 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 2878 | // in which case the following code does not matter. But, that's not quite right |
| 2879 | // once cross-compilation unit GLSL validation is done, as the original user |
| 2880 | // settings are needed in layoutOffset, and then the following will come into play. |
| 2881 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2882 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2883 | if (! memberType.getQualifier().hasOffset()) |
| 2884 | currentOffset = -1; |
| 2885 | |
| 2886 | return; |
| 2887 | } |
| 2888 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2889 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2890 | if (currentOffset < 0) |
| 2891 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2892 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2893 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 2894 | // but possibly not yet correctly aligned. |
| 2895 | |
| 2896 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2897 | int dummyStride; |
| 2898 | 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] | 2899 | |
| 2900 | // Adjust alignment for HLSL rules |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 2901 | // TODO: make this consistent in early phases of code: |
| 2902 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 2903 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 2904 | // 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] | 2905 | if (glslangIntermediate->usingHlslOFfsets() && |
John Kessenich | 735d7e5 | 2017-07-13 11:39:16 -0600 | [diff] [blame] | 2906 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 2907 | int dummySize; |
| 2908 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 2909 | if (componentAlignment <= 4) |
| 2910 | memberAlignment = componentAlignment; |
| 2911 | } |
| 2912 | |
| 2913 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2914 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 2915 | |
| 2916 | // Bump up to vec4 if there is a bad straddle |
| 2917 | if (glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
| 2918 | glslang::RoundToPow2(currentOffset, 16); |
| 2919 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2920 | nextOffset = currentOffset + memberSize; |
| 2921 | } |
| 2922 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2923 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2924 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2925 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 2926 | switch (glslangBuiltIn) |
| 2927 | { |
| 2928 | case glslang::EbvClipDistance: |
| 2929 | case glslang::EbvCullDistance: |
| 2930 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2931 | #ifdef NV_EXTENSIONS |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2932 | case glslang::EbvViewportMaskNV: |
| 2933 | case glslang::EbvSecondaryPositionNV: |
| 2934 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 2935 | case glslang::EbvPositionPerViewNV: |
| 2936 | case glslang::EbvViewportMaskPerViewNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2937 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2938 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 2939 | // Alternately, we could just call this for any glslang built-in, since the |
| 2940 | // capability already guards against duplicates. |
| 2941 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 2942 | break; |
| 2943 | default: |
| 2944 | // Capabilities were already generated when the struct was declared. |
| 2945 | break; |
| 2946 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2947 | } |
| 2948 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2949 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2950 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 2951 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 2955 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 2956 | { |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2957 | const auto getParamDecorations = [](std::vector<spv::Decoration>& decorations, const glslang::TType& type) { |
| 2958 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 2959 | if (paramPrecision != spv::NoPrecision) |
| 2960 | decorations.push_back(paramPrecision); |
John Kessenich | 961cd35 | 2017-07-18 02:58:06 -0600 | [diff] [blame] | 2961 | TranslateMemoryDecoration(type.getQualifier(), decorations); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2962 | }; |
| 2963 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2964 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 2965 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2966 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2967 | continue; |
| 2968 | |
| 2969 | // 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] | 2970 | // 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] | 2971 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2972 | // 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] | 2973 | // function. What it is an address of varies: |
| 2974 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2975 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 2976 | // 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] | 2977 | // |
| 2978 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 2979 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2980 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 2981 | // 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] | 2982 | |
| 2983 | std::vector<spv::Id> paramTypes; |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2984 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2985 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 2986 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2987 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 2988 | glslangIntermediate->implicitThisName; |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 2989 | |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 2990 | paramDecorations.resize(parameters.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2991 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 2992 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 2993 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 2994 | // can we pass by reference? |
| 2995 | if (paramType.containsOpaque() || // sampler, etc. |
John Kessenich | 4960baa | 2017-03-19 18:09:59 -0600 | [diff] [blame] | 2996 | (paramType.getBasicType() == glslang::EbtBlock && |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 2997 | paramType.getQualifier().storage == glslang::EvqBuffer) || // SSBO |
John Kessenich | aa3c64c | 2017-03-28 09:52:38 -0600 | [diff] [blame] | 2998 | (p == 0 && implicitThis)) // implicit 'this' |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2999 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
Jason Ekstrand | ed15ef1 | 2016-06-08 13:54:48 -0700 | [diff] [blame] | 3000 | else if (paramType.getQualifier().storage != glslang::EvqConstReadOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3001 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 3002 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3003 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3004 | getParamDecorations(paramDecorations[p], paramType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3005 | paramTypes.push_back(typeId); |
| 3006 | } |
| 3007 | |
| 3008 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3009 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 3010 | convertGlslangToSpvType(glslFunction->getType()), |
John Kessenich | fad6297 | 2017-07-18 02:35:46 -0600 | [diff] [blame] | 3011 | glslFunction->getName().c_str(), paramTypes, |
| 3012 | paramDecorations, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3013 | if (implicitThis) |
| 3014 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3015 | |
| 3016 | // Track function to emit/call later |
| 3017 | functionMap[glslFunction->getName().c_str()] = function; |
| 3018 | |
| 3019 | // Set the parameter id's |
| 3020 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 3021 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 3022 | // give a name too |
| 3023 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 3024 | } |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | // Process all the initializers, while skipping the functions and link objects |
| 3029 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 3030 | { |
| 3031 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 3032 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 3033 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 3034 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 3035 | |
| 3036 | // 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] | 3037 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3038 | initializer->traverse(this); |
| 3039 | } |
| 3040 | } |
| 3041 | } |
| 3042 | |
| 3043 | // Process all the functions, while skipping initializers. |
| 3044 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 3045 | { |
| 3046 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 3047 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 3048 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3049 | node->traverse(this); |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 3054 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3055 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3056 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 3057 | currentFunction = functionMap[node->getName().c_str()]; |
| 3058 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3059 | builder.setBuildPoint(functionBlock); |
| 3060 | } |
| 3061 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3062 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3063 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3064 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3065 | |
| 3066 | glslang::TSampler sampler = {}; |
| 3067 | bool cubeCompare = false; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3068 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3069 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 3070 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 3071 | } |
| 3072 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3073 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 3074 | builder.clearAccessChain(); |
| 3075 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3076 | |
| 3077 | // Special case l-value operands |
| 3078 | bool lvalue = false; |
| 3079 | switch (node.getOp()) { |
| 3080 | case glslang::EOpImageAtomicAdd: |
| 3081 | case glslang::EOpImageAtomicMin: |
| 3082 | case glslang::EOpImageAtomicMax: |
| 3083 | case glslang::EOpImageAtomicAnd: |
| 3084 | case glslang::EOpImageAtomicOr: |
| 3085 | case glslang::EOpImageAtomicXor: |
| 3086 | case glslang::EOpImageAtomicExchange: |
| 3087 | case glslang::EOpImageAtomicCompSwap: |
| 3088 | if (i == 0) |
| 3089 | lvalue = true; |
| 3090 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3091 | case glslang::EOpSparseImageLoad: |
| 3092 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 3093 | lvalue = true; |
| 3094 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3095 | case glslang::EOpSparseTexture: |
| 3096 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 3097 | lvalue = true; |
| 3098 | break; |
| 3099 | case glslang::EOpSparseTextureClamp: |
| 3100 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 3101 | lvalue = true; |
| 3102 | break; |
| 3103 | case glslang::EOpSparseTextureLod: |
| 3104 | case glslang::EOpSparseTextureOffset: |
| 3105 | if (i == 3) |
| 3106 | lvalue = true; |
| 3107 | break; |
| 3108 | case glslang::EOpSparseTextureFetch: |
| 3109 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 3110 | lvalue = true; |
| 3111 | break; |
| 3112 | case glslang::EOpSparseTextureFetchOffset: |
| 3113 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 3114 | lvalue = true; |
| 3115 | break; |
| 3116 | case glslang::EOpSparseTextureLodOffset: |
| 3117 | case glslang::EOpSparseTextureGrad: |
| 3118 | case glslang::EOpSparseTextureOffsetClamp: |
| 3119 | if (i == 4) |
| 3120 | lvalue = true; |
| 3121 | break; |
| 3122 | case glslang::EOpSparseTextureGradOffset: |
| 3123 | case glslang::EOpSparseTextureGradClamp: |
| 3124 | if (i == 5) |
| 3125 | lvalue = true; |
| 3126 | break; |
| 3127 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 3128 | if (i == 6) |
| 3129 | lvalue = true; |
| 3130 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3131 | case glslang::EOpSparseTextureGather: |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3132 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 3133 | lvalue = true; |
| 3134 | break; |
| 3135 | case glslang::EOpSparseTextureGatherOffset: |
| 3136 | case glslang::EOpSparseTextureGatherOffsets: |
| 3137 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 3138 | lvalue = true; |
| 3139 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3140 | #ifdef AMD_EXTENSIONS |
| 3141 | case glslang::EOpSparseTextureGatherLod: |
| 3142 | if (i == 3) |
| 3143 | lvalue = true; |
| 3144 | break; |
| 3145 | case glslang::EOpSparseTextureGatherLodOffset: |
| 3146 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 3147 | if (i == 4) |
| 3148 | lvalue = true; |
| 3149 | break; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3150 | case glslang::EOpSparseImageLoadLod: |
| 3151 | if (i == 3) |
| 3152 | lvalue = true; |
| 3153 | break; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3154 | #endif |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3155 | default: |
| 3156 | break; |
| 3157 | } |
| 3158 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3159 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3160 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3161 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3162 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3166 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3167 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3168 | builder.clearAccessChain(); |
| 3169 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3170 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3171 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3172 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3173 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 3174 | { |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3175 | if (! node->isImage() && ! node->isTexture()) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3176 | return spv::NoResult; |
John Kessenich | e485c7a | 2017-05-31 18:50:53 -0600 | [diff] [blame] | 3177 | |
| 3178 | builder.setLine(node->getLoc().line); |
| 3179 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3180 | auto resultType = [&node,this]{ return convertGlslangToSpvType(node->getType()); }; |
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 | // Process a GLSL texturing op (will be SPV image) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3183 | const glslang::TSampler sampler = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType().getSampler() |
| 3184 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType().getSampler(); |
| 3185 | std::vector<spv::Id> arguments; |
| 3186 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3187 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3188 | else |
| 3189 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 3190 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3191 | |
| 3192 | spv::Builder::TextureParameters params = { }; |
| 3193 | params.sampler = arguments[0]; |
| 3194 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3195 | glslang::TCrackedTextureOp cracked; |
| 3196 | node->crackTexture(sampler, cracked); |
| 3197 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 3198 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3199 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3200 | // Check for queries |
| 3201 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3202 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 3203 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 3204 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3205 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3206 | switch (node->getOp()) { |
| 3207 | case glslang::EOpImageQuerySize: |
| 3208 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3209 | if (arguments.size() > 1) { |
| 3210 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3211 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3212 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3213 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3214 | case glslang::EOpImageQuerySamples: |
| 3215 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3216 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3217 | case glslang::EOpTextureQueryLod: |
| 3218 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3219 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3220 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3221 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3222 | case glslang::EOpSparseTexelsResident: |
| 3223 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3224 | default: |
| 3225 | assert(0); |
| 3226 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3227 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3228 | } |
| 3229 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3230 | // Check for image functions other than queries |
| 3231 | if (node->isImage()) { |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3232 | std::vector<spv::Id> operands; |
| 3233 | auto opIt = arguments.begin(); |
| 3234 | operands.push_back(*(opIt++)); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3235 | |
| 3236 | // Handle subpass operations |
| 3237 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 3238 | // built-in function. |
| 3239 | if (cracked.subpass) { |
| 3240 | // add on the (0,0) coordinate |
| 3241 | spv::Id zero = builder.makeIntConstant(0); |
| 3242 | std::vector<spv::Id> comps; |
| 3243 | comps.push_back(zero); |
| 3244 | comps.push_back(zero); |
| 3245 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 3246 | if (sampler.ms) { |
| 3247 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3248 | operands.push_back(*(opIt++)); |
| 3249 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3250 | return builder.createOp(spv::OpImageRead, resultType(), operands); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3253 | operands.push_back(*(opIt++)); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3254 | #ifdef AMD_EXTENSIONS |
| 3255 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 3256 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3257 | if (node->getOp() == glslang::EOpImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3258 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3259 | if (sampler.ms) { |
| 3260 | operands.push_back(spv::ImageOperandsSampleMask); |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3261 | operands.push_back(*opIt); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3262 | #ifdef AMD_EXTENSIONS |
| 3263 | } else if (cracked.lod) { |
| 3264 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3265 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3266 | |
| 3267 | operands.push_back(spv::ImageOperandsLodMask); |
| 3268 | operands.push_back(*opIt); |
| 3269 | #endif |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3270 | } |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3271 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3272 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3273 | return builder.createOp(spv::OpImageRead, resultType(), operands); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3274 | #ifdef AMD_EXTENSIONS |
| 3275 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 3276 | #else |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3277 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3278 | #endif |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3279 | if (sampler.ms) { |
| 3280 | operands.push_back(*(opIt + 1)); |
| 3281 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3282 | operands.push_back(*opIt); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3283 | #ifdef AMD_EXTENSIONS |
| 3284 | } else if (cracked.lod) { |
| 3285 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3286 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3287 | |
| 3288 | operands.push_back(*(opIt + 1)); |
| 3289 | operands.push_back(spv::ImageOperandsLodMask); |
| 3290 | operands.push_back(*opIt); |
| 3291 | #endif |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3292 | } else |
| 3293 | operands.push_back(*opIt); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3294 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3295 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3296 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3297 | return spv::NoResult; |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3298 | #ifdef AMD_EXTENSIONS |
| 3299 | } else if (node->getOp() == glslang::EOpSparseImageLoad || node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 3300 | #else |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3301 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3302 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3303 | builder.addCapability(spv::CapabilitySparseResidency); |
| 3304 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3305 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 3306 | |
| 3307 | if (sampler.ms) { |
| 3308 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3309 | operands.push_back(*opIt++); |
Rex Xu | 129799a | 2017-07-05 17:23:28 +0800 | [diff] [blame] | 3310 | #ifdef AMD_EXTENSIONS |
| 3311 | } else if (cracked.lod) { |
| 3312 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 3313 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 3314 | |
| 3315 | operands.push_back(spv::ImageOperandsLodMask); |
| 3316 | operands.push_back(*opIt++); |
| 3317 | #endif |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | // Create the return type that was a special structure |
| 3321 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3322 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3323 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 3324 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 3325 | |
| 3326 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 3327 | |
| 3328 | // Decode the return type |
| 3329 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 3330 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3331 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3332 | // Process image atomic operations |
| 3333 | |
| 3334 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 3335 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3336 | 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] | 3337 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3338 | spv::Id resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3339 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3340 | |
| 3341 | std::vector<spv::Id> operands; |
| 3342 | operands.push_back(pointer); |
| 3343 | for (; opIt != arguments.end(); ++opIt) |
| 3344 | operands.push_back(*opIt); |
| 3345 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3346 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | |
amhagan | 05506bb | 2017-06-13 16:53:02 -0400 | [diff] [blame] | 3350 | #ifdef AMD_EXTENSIONS |
| 3351 | // Check for fragment mask functions other than queries |
| 3352 | if (cracked.fragMask) { |
| 3353 | assert(sampler.ms); |
| 3354 | |
| 3355 | auto opIt = arguments.begin(); |
| 3356 | std::vector<spv::Id> operands; |
| 3357 | |
| 3358 | // Extract the image if necessary |
| 3359 | if (builder.isSampledImage(params.sampler)) |
| 3360 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 3361 | |
| 3362 | operands.push_back(params.sampler); |
| 3363 | ++opIt; |
| 3364 | |
| 3365 | if (sampler.isSubpass()) { |
| 3366 | // add on the (0,0) coordinate |
| 3367 | spv::Id zero = builder.makeIntConstant(0); |
| 3368 | std::vector<spv::Id> comps; |
| 3369 | comps.push_back(zero); |
| 3370 | comps.push_back(zero); |
| 3371 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 3372 | } |
| 3373 | |
| 3374 | for (; opIt != arguments.end(); ++opIt) |
| 3375 | operands.push_back(*opIt); |
| 3376 | |
| 3377 | spv::Op fragMaskOp = spv::OpNop; |
| 3378 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 3379 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 3380 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 3381 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 3382 | |
| 3383 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 3384 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 3385 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 3386 | } |
| 3387 | #endif |
| 3388 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3389 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3390 | bool sparse = node->isSparseTexture(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 3391 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 3392 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3393 | // check for bias argument |
| 3394 | bool bias = false; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3395 | #ifdef AMD_EXTENSIONS |
| 3396 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 3397 | #else |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 3398 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3399 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3400 | int nonBiasArgCount = 2; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3401 | #ifdef AMD_EXTENSIONS |
| 3402 | if (cracked.gather) |
| 3403 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
| 3404 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3405 | if (cracked.offset) |
| 3406 | ++nonBiasArgCount; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3407 | #ifdef AMD_EXTENSIONS |
| 3408 | else if (cracked.offsets) |
| 3409 | ++nonBiasArgCount; |
| 3410 | #endif |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3411 | if (cracked.grad) |
| 3412 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3413 | if (cracked.lodClamp) |
| 3414 | ++nonBiasArgCount; |
| 3415 | if (sparse) |
| 3416 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3417 | |
| 3418 | if ((int)arguments.size() > nonBiasArgCount) |
| 3419 | bias = true; |
| 3420 | } |
| 3421 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 3422 | // See if the sampler param should really be just the SPV image part |
| 3423 | if (cracked.fetch) { |
| 3424 | // a fetch needs to have the image extracted first |
| 3425 | if (builder.isSampledImage(params.sampler)) |
| 3426 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 3427 | } |
| 3428 | |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3429 | #ifdef AMD_EXTENSIONS |
| 3430 | if (cracked.gather) { |
| 3431 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 3432 | if (bias || cracked.lod || |
| 3433 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 3434 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
Rex Xu | 301a2bc | 2017-06-14 23:09:39 +0800 | [diff] [blame] | 3435 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3436 | } |
| 3437 | } |
| 3438 | #endif |
| 3439 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3440 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3441 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3442 | params.coords = arguments[1]; |
| 3443 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3444 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3445 | |
| 3446 | // sort out where Dref is coming from |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3447 | if (cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3448 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3449 | ++extraArgs; |
| 3450 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3451 | params.Dref = arguments[2]; |
| 3452 | ++extraArgs; |
| 3453 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3454 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3455 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3456 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3457 | 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] | 3458 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3459 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 3460 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3461 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 3462 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3463 | |
| 3464 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3465 | if (cracked.lod) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 3466 | params.lod = arguments[2 + extraArgs]; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3467 | ++extraArgs; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3468 | } else if (glslangIntermediate->getStage() != EShLangFragment) { |
| 3469 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 3470 | noImplicitLod = true; |
| 3471 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3472 | |
| 3473 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3474 | if (sampler.ms) { |
LoopDawg | ef94b1a | 2017-07-24 18:45:37 -0600 | [diff] [blame] | 3475 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3476 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3477 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3478 | |
| 3479 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3480 | if (cracked.grad) { |
| 3481 | params.gradX = arguments[2 + extraArgs]; |
| 3482 | params.gradY = arguments[3 + extraArgs]; |
| 3483 | extraArgs += 2; |
| 3484 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3485 | |
| 3486 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3487 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3488 | params.offset = arguments[2 + extraArgs]; |
| 3489 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3490 | } else if (cracked.offsets) { |
| 3491 | params.offsets = arguments[2 + extraArgs]; |
| 3492 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3493 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3494 | |
| 3495 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3496 | if (cracked.lodClamp) { |
| 3497 | params.lodClamp = arguments[2 + extraArgs]; |
| 3498 | ++extraArgs; |
| 3499 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3500 | |
| 3501 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3502 | if (sparse) { |
| 3503 | params.texelOut = arguments[2 + extraArgs]; |
| 3504 | ++extraArgs; |
| 3505 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3506 | |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3507 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3508 | if (cracked.gather && ! sampler.shadow) { |
| 3509 | // default component is 0, if missing, otherwise an argument |
| 3510 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3511 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3512 | ++extraArgs; |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3513 | } else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3514 | params.component = builder.makeIntConstant(0); |
Rex Xu | 225e0fc | 2016-11-17 17:47:59 +0800 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | // bias |
| 3518 | if (bias) { |
| 3519 | params.bias = arguments[2 + extraArgs]; |
| 3520 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3521 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3522 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 3523 | // projective component (might not to move) |
| 3524 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 3525 | // are divided by the last component of P." |
| 3526 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 3527 | // unused components will appear after all used components." |
| 3528 | if (cracked.proj) { |
| 3529 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 3530 | int projTargetComp; |
| 3531 | switch (sampler.dim) { |
| 3532 | case glslang::Esd1D: projTargetComp = 1; break; |
| 3533 | case glslang::Esd2D: projTargetComp = 2; break; |
| 3534 | case glslang::EsdRect: projTargetComp = 2; break; |
| 3535 | default: projTargetComp = projSourceComp; break; |
| 3536 | } |
| 3537 | // copy the projective coordinate if we have to |
| 3538 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3539 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 3540 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 3541 | projSourceComp); |
| 3542 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 3543 | builder.getTypeId(params.coords), projTargetComp); |
| 3544 | } |
| 3545 | } |
| 3546 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3547 | 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] | 3548 | } |
| 3549 | |
| 3550 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 3551 | { |
| 3552 | // Grab the function's pointer from the previously created function |
| 3553 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 3554 | if (! function) |
| 3555 | return 0; |
| 3556 | |
| 3557 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 3558 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 3559 | |
LoopDawg | a5d8616 | 2017-09-10 09:46:55 -0600 | [diff] [blame] | 3560 | // Encapsulate lvalue logic, used in several places below, for safety. |
LoopDawg | 7611792 | 2017-09-06 14:59:06 -0600 | [diff] [blame] | 3561 | const auto isLValue = [](int qualifier, const glslang::TType& paramType) -> bool { |
| 3562 | return qualifier != glslang::EvqConstReadOnly || paramType.containsOpaque(); |
| 3563 | }; |
| 3564 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3565 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 3566 | // |
| 3567 | // These imply we need a four step process: |
| 3568 | // 1. Evaluate the arguments |
| 3569 | // 2. Allocate and make copies of in, out, and inout arguments |
| 3570 | // 3. Make the call |
| 3571 | // 4. Copy back the results |
| 3572 | |
| 3573 | // 1. Evaluate the arguments |
| 3574 | std::vector<spv::Builder::AccessChain> lValues; |
| 3575 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3576 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3577 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3578 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3579 | // build l-value |
| 3580 | builder.clearAccessChain(); |
| 3581 | glslangArgs[a]->traverse(this); |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3582 | argTypes.push_back(¶mType); |
John Kessenich | 1176530 | 2016-07-31 12:39:46 -0600 | [diff] [blame] | 3583 | // 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] | 3584 | if (isLValue(qualifiers[a], paramType)) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3585 | // save l-value |
| 3586 | lValues.push_back(builder.getAccessChain()); |
| 3587 | } else { |
| 3588 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3589 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 3594 | // copy the original into that space. |
| 3595 | // |
| 3596 | // Also, build up the list of actual arguments to pass in for the call |
| 3597 | int lValueCount = 0; |
| 3598 | int rValueCount = 0; |
| 3599 | std::vector<spv::Id> spvArgs; |
| 3600 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3601 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3602 | spv::Id arg; |
steve-lunarg | dd8287a | 2017-02-23 18:04:12 -0700 | [diff] [blame] | 3603 | if (paramType.containsOpaque() || |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3604 | (paramType.getBasicType() == glslang::EbtBlock && qualifiers[a] == glslang::EvqBuffer) || |
| 3605 | (a == 0 && function->hasImplicitThis())) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3606 | builder.setAccessChain(lValues[lValueCount]); |
| 3607 | arg = builder.accessChainGetLValue(); |
| 3608 | ++lValueCount; |
LoopDawg | 7611792 | 2017-09-06 14:59:06 -0600 | [diff] [blame] | 3609 | } else if (isLValue(qualifiers[a], paramType)) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3610 | // need space to hold the copy |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3611 | arg = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(paramType), "param"); |
| 3612 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 3613 | // need to copy the input into output space |
| 3614 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3615 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3616 | builder.clearAccessChain(); |
| 3617 | builder.setAccessChainLValue(arg); |
| 3618 | multiTypeStore(paramType, copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3619 | } |
| 3620 | ++lValueCount; |
| 3621 | } else { |
| 3622 | arg = rValues[rValueCount]; |
| 3623 | ++rValueCount; |
| 3624 | } |
| 3625 | spvArgs.push_back(arg); |
| 3626 | } |
| 3627 | |
| 3628 | // 3. Make the call. |
| 3629 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3630 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3631 | |
| 3632 | // 4. Copy back out an "out" arguments. |
| 3633 | lValueCount = 0; |
| 3634 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3635 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
LoopDawg | 7611792 | 2017-09-06 14:59:06 -0600 | [diff] [blame] | 3636 | if (isLValue(qualifiers[a], paramType)) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3637 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 3638 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 3639 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3640 | multiTypeStore(paramType, copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3641 | } |
| 3642 | ++lValueCount; |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | return result; |
| 3647 | } |
| 3648 | |
| 3649 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3650 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, spv::Decoration precision, |
| 3651 | spv::Decoration noContraction, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3652 | spv::Id typeId, spv::Id left, spv::Id right, |
| 3653 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 3654 | { |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3655 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3656 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64 || typeProxy == glslang::EbtUint16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3657 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 3658 | #else |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3659 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3660 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3661 | #endif |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3662 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3663 | |
| 3664 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3665 | 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] | 3666 | bool comparison = false; |
| 3667 | |
| 3668 | switch (op) { |
| 3669 | case glslang::EOpAdd: |
| 3670 | case glslang::EOpAddAssign: |
| 3671 | if (isFloat) |
| 3672 | binOp = spv::OpFAdd; |
| 3673 | else |
| 3674 | binOp = spv::OpIAdd; |
| 3675 | break; |
| 3676 | case glslang::EOpSub: |
| 3677 | case glslang::EOpSubAssign: |
| 3678 | if (isFloat) |
| 3679 | binOp = spv::OpFSub; |
| 3680 | else |
| 3681 | binOp = spv::OpISub; |
| 3682 | break; |
| 3683 | case glslang::EOpMul: |
| 3684 | case glslang::EOpMulAssign: |
| 3685 | if (isFloat) |
| 3686 | binOp = spv::OpFMul; |
| 3687 | else |
| 3688 | binOp = spv::OpIMul; |
| 3689 | break; |
| 3690 | case glslang::EOpVectorTimesScalar: |
| 3691 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 3692 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3693 | if (builder.isVector(right)) |
| 3694 | std::swap(left, right); |
| 3695 | assert(builder.isScalar(right)); |
| 3696 | needMatchingVectors = false; |
| 3697 | binOp = spv::OpVectorTimesScalar; |
| 3698 | } else |
| 3699 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3700 | break; |
| 3701 | case glslang::EOpVectorTimesMatrix: |
| 3702 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3703 | binOp = spv::OpVectorTimesMatrix; |
| 3704 | break; |
| 3705 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3706 | binOp = spv::OpMatrixTimesVector; |
| 3707 | break; |
| 3708 | case glslang::EOpMatrixTimesScalar: |
| 3709 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3710 | binOp = spv::OpMatrixTimesScalar; |
| 3711 | break; |
| 3712 | case glslang::EOpMatrixTimesMatrix: |
| 3713 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3714 | binOp = spv::OpMatrixTimesMatrix; |
| 3715 | break; |
| 3716 | case glslang::EOpOuterProduct: |
| 3717 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3718 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3719 | break; |
| 3720 | |
| 3721 | case glslang::EOpDiv: |
| 3722 | case glslang::EOpDivAssign: |
| 3723 | if (isFloat) |
| 3724 | binOp = spv::OpFDiv; |
| 3725 | else if (isUnsigned) |
| 3726 | binOp = spv::OpUDiv; |
| 3727 | else |
| 3728 | binOp = spv::OpSDiv; |
| 3729 | break; |
| 3730 | case glslang::EOpMod: |
| 3731 | case glslang::EOpModAssign: |
| 3732 | if (isFloat) |
| 3733 | binOp = spv::OpFMod; |
| 3734 | else if (isUnsigned) |
| 3735 | binOp = spv::OpUMod; |
| 3736 | else |
| 3737 | binOp = spv::OpSMod; |
| 3738 | break; |
| 3739 | case glslang::EOpRightShift: |
| 3740 | case glslang::EOpRightShiftAssign: |
| 3741 | if (isUnsigned) |
| 3742 | binOp = spv::OpShiftRightLogical; |
| 3743 | else |
| 3744 | binOp = spv::OpShiftRightArithmetic; |
| 3745 | break; |
| 3746 | case glslang::EOpLeftShift: |
| 3747 | case glslang::EOpLeftShiftAssign: |
| 3748 | binOp = spv::OpShiftLeftLogical; |
| 3749 | break; |
| 3750 | case glslang::EOpAnd: |
| 3751 | case glslang::EOpAndAssign: |
| 3752 | binOp = spv::OpBitwiseAnd; |
| 3753 | break; |
| 3754 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3755 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3756 | binOp = spv::OpLogicalAnd; |
| 3757 | break; |
| 3758 | case glslang::EOpInclusiveOr: |
| 3759 | case glslang::EOpInclusiveOrAssign: |
| 3760 | binOp = spv::OpBitwiseOr; |
| 3761 | break; |
| 3762 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3763 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3764 | binOp = spv::OpLogicalOr; |
| 3765 | break; |
| 3766 | case glslang::EOpExclusiveOr: |
| 3767 | case glslang::EOpExclusiveOrAssign: |
| 3768 | binOp = spv::OpBitwiseXor; |
| 3769 | break; |
| 3770 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3771 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3772 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3773 | break; |
| 3774 | |
| 3775 | case glslang::EOpLessThan: |
| 3776 | case glslang::EOpGreaterThan: |
| 3777 | case glslang::EOpLessThanEqual: |
| 3778 | case glslang::EOpGreaterThanEqual: |
| 3779 | case glslang::EOpEqual: |
| 3780 | case glslang::EOpNotEqual: |
| 3781 | case glslang::EOpVectorEqual: |
| 3782 | case glslang::EOpVectorNotEqual: |
| 3783 | comparison = true; |
| 3784 | break; |
| 3785 | default: |
| 3786 | break; |
| 3787 | } |
| 3788 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3789 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3790 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3791 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3792 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3793 | return createBinaryMatrixOperation(binOp, precision, noContraction, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3794 | |
| 3795 | // 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] | 3796 | if (needMatchingVectors) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3797 | builder.promoteScalar(precision, left, right); |
| 3798 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3799 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 3800 | addDecoration(result, noContraction); |
| 3801 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | if (! comparison) |
| 3805 | return 0; |
| 3806 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3807 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3808 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 3809 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
| 3810 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 3811 | return builder.createCompositeCompare(precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3812 | |
| 3813 | switch (op) { |
| 3814 | case glslang::EOpLessThan: |
| 3815 | if (isFloat) |
| 3816 | binOp = spv::OpFOrdLessThan; |
| 3817 | else if (isUnsigned) |
| 3818 | binOp = spv::OpULessThan; |
| 3819 | else |
| 3820 | binOp = spv::OpSLessThan; |
| 3821 | break; |
| 3822 | case glslang::EOpGreaterThan: |
| 3823 | if (isFloat) |
| 3824 | binOp = spv::OpFOrdGreaterThan; |
| 3825 | else if (isUnsigned) |
| 3826 | binOp = spv::OpUGreaterThan; |
| 3827 | else |
| 3828 | binOp = spv::OpSGreaterThan; |
| 3829 | break; |
| 3830 | case glslang::EOpLessThanEqual: |
| 3831 | if (isFloat) |
| 3832 | binOp = spv::OpFOrdLessThanEqual; |
| 3833 | else if (isUnsigned) |
| 3834 | binOp = spv::OpULessThanEqual; |
| 3835 | else |
| 3836 | binOp = spv::OpSLessThanEqual; |
| 3837 | break; |
| 3838 | case glslang::EOpGreaterThanEqual: |
| 3839 | if (isFloat) |
| 3840 | binOp = spv::OpFOrdGreaterThanEqual; |
| 3841 | else if (isUnsigned) |
| 3842 | binOp = spv::OpUGreaterThanEqual; |
| 3843 | else |
| 3844 | binOp = spv::OpSGreaterThanEqual; |
| 3845 | break; |
| 3846 | case glslang::EOpEqual: |
| 3847 | case glslang::EOpVectorEqual: |
| 3848 | if (isFloat) |
| 3849 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3850 | else if (isBool) |
| 3851 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3852 | else |
| 3853 | binOp = spv::OpIEqual; |
| 3854 | break; |
| 3855 | case glslang::EOpNotEqual: |
| 3856 | case glslang::EOpVectorNotEqual: |
| 3857 | if (isFloat) |
| 3858 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3859 | else if (isBool) |
| 3860 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3861 | else |
| 3862 | binOp = spv::OpINotEqual; |
| 3863 | break; |
| 3864 | default: |
| 3865 | break; |
| 3866 | } |
| 3867 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3868 | if (binOp != spv::OpNop) { |
| 3869 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 3870 | addDecoration(result, noContraction); |
| 3871 | return builder.setPrecision(result, precision); |
| 3872 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3873 | |
| 3874 | return 0; |
| 3875 | } |
| 3876 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3877 | // |
| 3878 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 3879 | // These can be any of: |
| 3880 | // |
| 3881 | // matrix * scalar |
| 3882 | // scalar * matrix |
| 3883 | // matrix * matrix linear algebraic |
| 3884 | // matrix * vector |
| 3885 | // vector * matrix |
| 3886 | // matrix * matrix componentwise |
| 3887 | // matrix op matrix op in {+, -, /} |
| 3888 | // matrix op scalar op in {+, -, /} |
| 3889 | // scalar op matrix op in {+, -, /} |
| 3890 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3891 | 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] | 3892 | { |
| 3893 | bool firstClass = true; |
| 3894 | |
| 3895 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 3896 | switch (op) { |
| 3897 | case spv::OpFDiv: |
| 3898 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 3899 | // turn matrix / scalar into a multiply... |
| 3900 | right = builder.createBinOp(spv::OpFDiv, builder.getTypeId(right), builder.makeFloatConstant(1.0F), right); |
| 3901 | op = spv::OpMatrixTimesScalar; |
| 3902 | } else |
| 3903 | firstClass = false; |
| 3904 | break; |
| 3905 | case spv::OpMatrixTimesScalar: |
| 3906 | if (builder.isMatrix(right)) |
| 3907 | std::swap(left, right); |
| 3908 | assert(builder.isScalar(right)); |
| 3909 | break; |
| 3910 | case spv::OpVectorTimesMatrix: |
| 3911 | assert(builder.isVector(left)); |
| 3912 | assert(builder.isMatrix(right)); |
| 3913 | break; |
| 3914 | case spv::OpMatrixTimesVector: |
| 3915 | assert(builder.isMatrix(left)); |
| 3916 | assert(builder.isVector(right)); |
| 3917 | break; |
| 3918 | case spv::OpMatrixTimesMatrix: |
| 3919 | assert(builder.isMatrix(left)); |
| 3920 | assert(builder.isMatrix(right)); |
| 3921 | break; |
| 3922 | default: |
| 3923 | firstClass = false; |
| 3924 | break; |
| 3925 | } |
| 3926 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3927 | if (firstClass) { |
| 3928 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
| 3929 | addDecoration(result, noContraction); |
| 3930 | return builder.setPrecision(result, precision); |
| 3931 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3932 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 3933 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3934 | // The result type of all of them is the same type as the (a) matrix operand. |
| 3935 | // The algorithm is to: |
| 3936 | // - break the matrix(es) into vectors |
| 3937 | // - smear any scalar to a vector |
| 3938 | // - do vector operations |
| 3939 | // - make a matrix out the vector results |
| 3940 | switch (op) { |
| 3941 | case spv::OpFAdd: |
| 3942 | case spv::OpFSub: |
| 3943 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 3944 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3945 | case spv::OpFMul: |
| 3946 | { |
| 3947 | // one time set up... |
| 3948 | bool leftMat = builder.isMatrix(left); |
| 3949 | bool rightMat = builder.isMatrix(right); |
| 3950 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 3951 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 3952 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 3953 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 3954 | std::vector<spv::Id> results; |
| 3955 | spv::Id smearVec = spv::NoResult; |
| 3956 | if (builder.isScalar(left)) |
| 3957 | smearVec = builder.smearScalar(precision, left, vecType); |
| 3958 | else if (builder.isScalar(right)) |
| 3959 | smearVec = builder.smearScalar(precision, right, vecType); |
| 3960 | |
| 3961 | // do each vector op |
| 3962 | for (unsigned int c = 0; c < numCols; ++c) { |
| 3963 | std::vector<unsigned int> indexes; |
| 3964 | indexes.push_back(c); |
| 3965 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 3966 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3967 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
| 3968 | addDecoration(result, noContraction); |
| 3969 | results.push_back(builder.setPrecision(result, precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3973 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3974 | } |
| 3975 | default: |
| 3976 | assert(0); |
| 3977 | return spv::NoResult; |
| 3978 | } |
| 3979 | } |
| 3980 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3981 | 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] | 3982 | { |
| 3983 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 3984 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3985 | int libCall = -1; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3986 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3987 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64 || typeProxy == glslang::EbtUint16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3988 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 3989 | #else |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 3990 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3991 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3992 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3993 | |
| 3994 | switch (op) { |
| 3995 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3996 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3997 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3998 | if (builder.isMatrixType(typeId)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3999 | return createUnaryMatrixOperation(unaryOp, precision, noContraction, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4000 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4001 | unaryOp = spv::OpSNegate; |
| 4002 | break; |
| 4003 | |
| 4004 | case glslang::EOpLogicalNot: |
| 4005 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4006 | unaryOp = spv::OpLogicalNot; |
| 4007 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4008 | case glslang::EOpBitwiseNot: |
| 4009 | unaryOp = spv::OpNot; |
| 4010 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4011 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4012 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4013 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4014 | break; |
| 4015 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4016 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4017 | break; |
| 4018 | case glslang::EOpTranspose: |
| 4019 | unaryOp = spv::OpTranspose; |
| 4020 | break; |
| 4021 | |
| 4022 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4023 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4024 | break; |
| 4025 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4026 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4027 | break; |
| 4028 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4029 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4030 | break; |
| 4031 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4032 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4033 | break; |
| 4034 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4035 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4036 | break; |
| 4037 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4038 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4039 | break; |
| 4040 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4041 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4042 | break; |
| 4043 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4044 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4045 | break; |
| 4046 | |
| 4047 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4048 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4049 | break; |
| 4050 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4051 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4052 | break; |
| 4053 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4054 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4055 | break; |
| 4056 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4057 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4058 | break; |
| 4059 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4060 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4061 | break; |
| 4062 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4063 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4064 | break; |
| 4065 | |
| 4066 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4067 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4068 | break; |
| 4069 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4070 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4071 | break; |
| 4072 | |
| 4073 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4074 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4075 | break; |
| 4076 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4077 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4078 | break; |
| 4079 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4080 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4081 | break; |
| 4082 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4083 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4084 | break; |
| 4085 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4086 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4087 | break; |
| 4088 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4089 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4090 | break; |
| 4091 | |
| 4092 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4093 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4094 | break; |
| 4095 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4096 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4097 | break; |
| 4098 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4099 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4100 | break; |
| 4101 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4102 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4103 | break; |
| 4104 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4105 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4106 | break; |
| 4107 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4108 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4109 | break; |
| 4110 | |
| 4111 | case glslang::EOpIsNan: |
| 4112 | unaryOp = spv::OpIsNan; |
| 4113 | break; |
| 4114 | case glslang::EOpIsInf: |
| 4115 | unaryOp = spv::OpIsInf; |
| 4116 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 4117 | case glslang::EOpIsFinite: |
| 4118 | unaryOp = spv::OpIsFinite; |
| 4119 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4120 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 4121 | case glslang::EOpFloatBitsToInt: |
| 4122 | case glslang::EOpFloatBitsToUint: |
| 4123 | case glslang::EOpIntBitsToFloat: |
| 4124 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4125 | case glslang::EOpDoubleBitsToInt64: |
| 4126 | case glslang::EOpDoubleBitsToUint64: |
| 4127 | case glslang::EOpInt64BitsToDouble: |
| 4128 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4129 | #ifdef AMD_EXTENSIONS |
| 4130 | case glslang::EOpFloat16BitsToInt16: |
| 4131 | case glslang::EOpFloat16BitsToUint16: |
| 4132 | case glslang::EOpInt16BitsToFloat16: |
| 4133 | case glslang::EOpUint16BitsToFloat16: |
| 4134 | #endif |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 4135 | unaryOp = spv::OpBitcast; |
| 4136 | break; |
| 4137 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4138 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4139 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4140 | break; |
| 4141 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4142 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4143 | break; |
| 4144 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4145 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4146 | break; |
| 4147 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4148 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4149 | break; |
| 4150 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4151 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4152 | break; |
| 4153 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4154 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4155 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4156 | case glslang::EOpPackSnorm4x8: |
| 4157 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 4158 | break; |
| 4159 | case glslang::EOpUnpackSnorm4x8: |
| 4160 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 4161 | break; |
| 4162 | case glslang::EOpPackUnorm4x8: |
| 4163 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 4164 | break; |
| 4165 | case glslang::EOpUnpackUnorm4x8: |
| 4166 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 4167 | break; |
| 4168 | case glslang::EOpPackDouble2x32: |
| 4169 | libCall = spv::GLSLstd450PackDouble2x32; |
| 4170 | break; |
| 4171 | case glslang::EOpUnpackDouble2x32: |
| 4172 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 4173 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4174 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4175 | case glslang::EOpPackInt2x32: |
| 4176 | case glslang::EOpUnpackInt2x32: |
| 4177 | case glslang::EOpPackUint2x32: |
| 4178 | case glslang::EOpUnpackUint2x32: |
Rex Xu | c9f3492 | 2016-09-09 17:50:07 +0800 | [diff] [blame] | 4179 | unaryOp = spv::OpBitcast; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4180 | break; |
| 4181 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4182 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4183 | case glslang::EOpPackInt2x16: |
| 4184 | case glslang::EOpUnpackInt2x16: |
| 4185 | case glslang::EOpPackUint2x16: |
| 4186 | case glslang::EOpUnpackUint2x16: |
| 4187 | case glslang::EOpPackInt4x16: |
| 4188 | case glslang::EOpUnpackInt4x16: |
| 4189 | case glslang::EOpPackUint4x16: |
| 4190 | case glslang::EOpUnpackUint4x16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4191 | case glslang::EOpPackFloat2x16: |
| 4192 | case glslang::EOpUnpackFloat2x16: |
| 4193 | unaryOp = spv::OpBitcast; |
| 4194 | break; |
| 4195 | #endif |
| 4196 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4197 | case glslang::EOpDPdx: |
| 4198 | unaryOp = spv::OpDPdx; |
| 4199 | break; |
| 4200 | case glslang::EOpDPdy: |
| 4201 | unaryOp = spv::OpDPdy; |
| 4202 | break; |
| 4203 | case glslang::EOpFwidth: |
| 4204 | unaryOp = spv::OpFwidth; |
| 4205 | break; |
| 4206 | case glslang::EOpDPdxFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4207 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4208 | unaryOp = spv::OpDPdxFine; |
| 4209 | break; |
| 4210 | case glslang::EOpDPdyFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4211 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4212 | unaryOp = spv::OpDPdyFine; |
| 4213 | break; |
| 4214 | case glslang::EOpFwidthFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4215 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4216 | unaryOp = spv::OpFwidthFine; |
| 4217 | break; |
| 4218 | case glslang::EOpDPdxCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4219 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4220 | unaryOp = spv::OpDPdxCoarse; |
| 4221 | break; |
| 4222 | case glslang::EOpDPdyCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4223 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4224 | unaryOp = spv::OpDPdyCoarse; |
| 4225 | break; |
| 4226 | case glslang::EOpFwidthCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4227 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4228 | unaryOp = spv::OpFwidthCoarse; |
| 4229 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4230 | case glslang::EOpInterpolateAtCentroid: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4231 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4232 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 4233 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4234 | case glslang::EOpAny: |
| 4235 | unaryOp = spv::OpAny; |
| 4236 | break; |
| 4237 | case glslang::EOpAll: |
| 4238 | unaryOp = spv::OpAll; |
| 4239 | break; |
| 4240 | |
| 4241 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4242 | if (isFloat) |
| 4243 | libCall = spv::GLSLstd450FAbs; |
| 4244 | else |
| 4245 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4246 | break; |
| 4247 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4248 | if (isFloat) |
| 4249 | libCall = spv::GLSLstd450FSign; |
| 4250 | else |
| 4251 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4252 | break; |
| 4253 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4254 | case glslang::EOpAtomicCounterIncrement: |
| 4255 | case glslang::EOpAtomicCounterDecrement: |
| 4256 | case glslang::EOpAtomicCounter: |
| 4257 | { |
| 4258 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 4259 | std::vector<spv::Id> operands; |
| 4260 | operands.push_back(operand); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4261 | return createAtomicOperation(op, precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4262 | } |
| 4263 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4264 | case glslang::EOpBitFieldReverse: |
| 4265 | unaryOp = spv::OpBitReverse; |
| 4266 | break; |
| 4267 | case glslang::EOpBitCount: |
| 4268 | unaryOp = spv::OpBitCount; |
| 4269 | break; |
| 4270 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4271 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4272 | break; |
| 4273 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4274 | if (isUnsigned) |
| 4275 | libCall = spv::GLSLstd450FindUMsb; |
| 4276 | else |
| 4277 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4278 | break; |
| 4279 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 4280 | case glslang::EOpBallot: |
| 4281 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4282 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4283 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4284 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4285 | #ifdef AMD_EXTENSIONS |
| 4286 | case glslang::EOpMinInvocations: |
| 4287 | case glslang::EOpMaxInvocations: |
| 4288 | case glslang::EOpAddInvocations: |
| 4289 | case glslang::EOpMinInvocationsNonUniform: |
| 4290 | case glslang::EOpMaxInvocationsNonUniform: |
| 4291 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4292 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4293 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4294 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4295 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4296 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4297 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4298 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4299 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4300 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4301 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4302 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4303 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4304 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4305 | { |
| 4306 | std::vector<spv::Id> operands; |
| 4307 | operands.push_back(operand); |
| 4308 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 4309 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4310 | |
| 4311 | #ifdef AMD_EXTENSIONS |
| 4312 | case glslang::EOpMbcnt: |
| 4313 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 4314 | libCall = spv::MbcntAMD; |
| 4315 | break; |
| 4316 | |
| 4317 | case glslang::EOpCubeFaceIndex: |
| 4318 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 4319 | libCall = spv::CubeFaceIndexAMD; |
| 4320 | break; |
| 4321 | |
| 4322 | case glslang::EOpCubeFaceCoord: |
| 4323 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 4324 | libCall = spv::CubeFaceCoordAMD; |
| 4325 | break; |
| 4326 | #endif |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4327 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4328 | default: |
| 4329 | return 0; |
| 4330 | } |
| 4331 | |
| 4332 | spv::Id id; |
| 4333 | if (libCall >= 0) { |
| 4334 | std::vector<spv::Id> args; |
| 4335 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4336 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4337 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4338 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4339 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4340 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4341 | addDecoration(id, noContraction); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4342 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4343 | } |
| 4344 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4345 | // Create a unary operation on a matrix |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4346 | 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] | 4347 | { |
| 4348 | // Handle unary operations vector by vector. |
| 4349 | // The result type is the same type as the original type. |
| 4350 | // The algorithm is to: |
| 4351 | // - break the matrix into vectors |
| 4352 | // - apply the operation to each vector |
| 4353 | // - make a matrix out the vector results |
| 4354 | |
| 4355 | // get the types sorted out |
| 4356 | int numCols = builder.getNumColumns(operand); |
| 4357 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 4358 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 4359 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4360 | std::vector<spv::Id> results; |
| 4361 | |
| 4362 | // do each vector op |
| 4363 | for (int c = 0; c < numCols; ++c) { |
| 4364 | std::vector<unsigned int> indexes; |
| 4365 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 4366 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 4367 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
| 4368 | addDecoration(destVec, noContraction); |
| 4369 | results.push_back(builder.setPrecision(destVec, precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4373 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4374 | } |
| 4375 | |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 4376 | 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] | 4377 | { |
| 4378 | spv::Op convOp = spv::OpNop; |
| 4379 | spv::Id zero = 0; |
| 4380 | spv::Id one = 0; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4381 | spv::Id type = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4382 | |
| 4383 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 4384 | |
| 4385 | switch (op) { |
| 4386 | case glslang::EOpConvIntToBool: |
| 4387 | case glslang::EOpConvUintToBool: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4388 | case glslang::EOpConvInt64ToBool: |
| 4389 | case glslang::EOpConvUint64ToBool: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4390 | #ifdef AMD_EXTENSIONS |
| 4391 | case glslang::EOpConvInt16ToBool: |
| 4392 | case glslang::EOpConvUint16ToBool: |
| 4393 | #endif |
| 4394 | if (op == glslang::EOpConvInt64ToBool || op == glslang::EOpConvUint64ToBool) |
| 4395 | zero = builder.makeUint64Constant(0); |
| 4396 | #ifdef AMD_EXTENSIONS |
| 4397 | else if (op == glslang::EOpConvInt16ToBool || op == glslang::EOpConvUint16ToBool) |
| 4398 | zero = builder.makeUint16Constant(0); |
| 4399 | #endif |
| 4400 | else |
| 4401 | zero = builder.makeUintConstant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4402 | zero = makeSmearedConstant(zero, vectorSize); |
| 4403 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 4404 | |
| 4405 | case glslang::EOpConvFloatToBool: |
| 4406 | zero = builder.makeFloatConstant(0.0F); |
| 4407 | zero = makeSmearedConstant(zero, vectorSize); |
| 4408 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4409 | |
| 4410 | case glslang::EOpConvDoubleToBool: |
| 4411 | zero = builder.makeDoubleConstant(0.0); |
| 4412 | zero = makeSmearedConstant(zero, vectorSize); |
| 4413 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4414 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4415 | #ifdef AMD_EXTENSIONS |
| 4416 | case glslang::EOpConvFloat16ToBool: |
| 4417 | zero = builder.makeFloat16Constant(0.0F); |
| 4418 | zero = makeSmearedConstant(zero, vectorSize); |
| 4419 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4420 | #endif |
| 4421 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4422 | case glslang::EOpConvBoolToFloat: |
| 4423 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4424 | zero = builder.makeFloatConstant(0.0F); |
| 4425 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4426 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4427 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4428 | case glslang::EOpConvBoolToDouble: |
| 4429 | convOp = spv::OpSelect; |
| 4430 | zero = builder.makeDoubleConstant(0.0); |
| 4431 | one = builder.makeDoubleConstant(1.0); |
| 4432 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4433 | |
| 4434 | #ifdef AMD_EXTENSIONS |
| 4435 | case glslang::EOpConvBoolToFloat16: |
| 4436 | convOp = spv::OpSelect; |
| 4437 | zero = builder.makeFloat16Constant(0.0F); |
| 4438 | one = builder.makeFloat16Constant(1.0F); |
| 4439 | break; |
| 4440 | #endif |
| 4441 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4442 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4443 | case glslang::EOpConvBoolToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4444 | #ifdef AMD_EXTENSIONS |
| 4445 | case glslang::EOpConvBoolToInt16: |
| 4446 | #endif |
| 4447 | if (op == glslang::EOpConvBoolToInt64) |
| 4448 | zero = builder.makeInt64Constant(0); |
| 4449 | #ifdef AMD_EXTENSIONS |
| 4450 | else if (op == glslang::EOpConvBoolToInt16) |
| 4451 | zero = builder.makeInt16Constant(0); |
| 4452 | #endif |
| 4453 | else |
| 4454 | zero = builder.makeIntConstant(0); |
| 4455 | |
| 4456 | if (op == glslang::EOpConvBoolToInt64) |
| 4457 | one = builder.makeInt64Constant(1); |
| 4458 | #ifdef AMD_EXTENSIONS |
| 4459 | else if (op == glslang::EOpConvBoolToInt16) |
| 4460 | one = builder.makeInt16Constant(1); |
| 4461 | #endif |
| 4462 | else |
| 4463 | one = builder.makeIntConstant(1); |
| 4464 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4465 | convOp = spv::OpSelect; |
| 4466 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4467 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4468 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4469 | case glslang::EOpConvBoolToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4470 | #ifdef AMD_EXTENSIONS |
| 4471 | case glslang::EOpConvBoolToUint16: |
| 4472 | #endif |
| 4473 | if (op == glslang::EOpConvBoolToUint64) |
| 4474 | zero = builder.makeUint64Constant(0); |
| 4475 | #ifdef AMD_EXTENSIONS |
| 4476 | else if (op == glslang::EOpConvBoolToUint16) |
| 4477 | zero = builder.makeUint16Constant(0); |
| 4478 | #endif |
| 4479 | else |
| 4480 | zero = builder.makeUintConstant(0); |
| 4481 | |
| 4482 | if (op == glslang::EOpConvBoolToUint64) |
| 4483 | one = builder.makeUint64Constant(1); |
| 4484 | #ifdef AMD_EXTENSIONS |
| 4485 | else if (op == glslang::EOpConvBoolToUint16) |
| 4486 | one = builder.makeUint16Constant(1); |
| 4487 | #endif |
| 4488 | else |
| 4489 | one = builder.makeUintConstant(1); |
| 4490 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4491 | convOp = spv::OpSelect; |
| 4492 | break; |
| 4493 | |
| 4494 | case glslang::EOpConvIntToFloat: |
| 4495 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4496 | case glslang::EOpConvInt64ToFloat: |
| 4497 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4498 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4499 | case glslang::EOpConvInt16ToFloat: |
| 4500 | case glslang::EOpConvInt16ToDouble: |
| 4501 | case glslang::EOpConvInt16ToFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4502 | case glslang::EOpConvIntToFloat16: |
| 4503 | case glslang::EOpConvInt64ToFloat16: |
| 4504 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4505 | convOp = spv::OpConvertSToF; |
| 4506 | break; |
| 4507 | |
| 4508 | case glslang::EOpConvUintToFloat: |
| 4509 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4510 | case glslang::EOpConvUint64ToFloat: |
| 4511 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4512 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4513 | case glslang::EOpConvUint16ToFloat: |
| 4514 | case glslang::EOpConvUint16ToDouble: |
| 4515 | case glslang::EOpConvUint16ToFloat16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4516 | case glslang::EOpConvUintToFloat16: |
| 4517 | case glslang::EOpConvUint64ToFloat16: |
| 4518 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4519 | convOp = spv::OpConvertUToF; |
| 4520 | break; |
| 4521 | |
| 4522 | case glslang::EOpConvDoubleToFloat: |
| 4523 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4524 | #ifdef AMD_EXTENSIONS |
| 4525 | case glslang::EOpConvDoubleToFloat16: |
| 4526 | case glslang::EOpConvFloat16ToDouble: |
| 4527 | case glslang::EOpConvFloatToFloat16: |
| 4528 | case glslang::EOpConvFloat16ToFloat: |
| 4529 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4530 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 4531 | if (builder.isMatrixType(destType)) |
| 4532 | return createUnaryMatrixOperation(convOp, precision, noContraction, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4533 | break; |
| 4534 | |
| 4535 | case glslang::EOpConvFloatToInt: |
| 4536 | case glslang::EOpConvDoubleToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4537 | case glslang::EOpConvFloatToInt64: |
| 4538 | case glslang::EOpConvDoubleToInt64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4539 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4540 | case glslang::EOpConvFloatToInt16: |
| 4541 | case glslang::EOpConvDoubleToInt16: |
| 4542 | case glslang::EOpConvFloat16ToInt16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4543 | case glslang::EOpConvFloat16ToInt: |
| 4544 | case glslang::EOpConvFloat16ToInt64: |
| 4545 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4546 | convOp = spv::OpConvertFToS; |
| 4547 | break; |
| 4548 | |
| 4549 | case glslang::EOpConvUintToInt: |
| 4550 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4551 | case glslang::EOpConvUint64ToInt64: |
| 4552 | case glslang::EOpConvInt64ToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4553 | #ifdef AMD_EXTENSIONS |
| 4554 | case glslang::EOpConvUint16ToInt16: |
| 4555 | case glslang::EOpConvInt16ToUint16: |
| 4556 | #endif |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 4557 | if (builder.isInSpecConstCodeGenMode()) { |
| 4558 | // Build zero scalar or vector for OpIAdd. |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4559 | if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) |
| 4560 | zero = builder.makeUint64Constant(0); |
| 4561 | #ifdef AMD_EXTENSIONS |
| 4562 | else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) |
| 4563 | zero = builder.makeUint16Constant(0); |
| 4564 | #endif |
| 4565 | else |
| 4566 | zero = builder.makeUintConstant(0); |
| 4567 | |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 4568 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 4569 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 4570 | // generating for OpSpecConstantOp instruction. |
| 4571 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 4572 | } |
| 4573 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4574 | convOp = spv::OpBitcast; |
| 4575 | break; |
| 4576 | |
| 4577 | case glslang::EOpConvFloatToUint: |
| 4578 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4579 | case glslang::EOpConvFloatToUint64: |
| 4580 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4581 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4582 | case glslang::EOpConvFloatToUint16: |
| 4583 | case glslang::EOpConvDoubleToUint16: |
| 4584 | case glslang::EOpConvFloat16ToUint16: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4585 | case glslang::EOpConvFloat16ToUint: |
| 4586 | case glslang::EOpConvFloat16ToUint64: |
| 4587 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4588 | convOp = spv::OpConvertFToU; |
| 4589 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4590 | |
| 4591 | case glslang::EOpConvIntToInt64: |
| 4592 | case glslang::EOpConvInt64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4593 | #ifdef AMD_EXTENSIONS |
| 4594 | case glslang::EOpConvIntToInt16: |
| 4595 | case glslang::EOpConvInt16ToInt: |
| 4596 | case glslang::EOpConvInt64ToInt16: |
| 4597 | case glslang::EOpConvInt16ToInt64: |
| 4598 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4599 | convOp = spv::OpSConvert; |
| 4600 | break; |
| 4601 | |
| 4602 | case glslang::EOpConvUintToUint64: |
| 4603 | case glslang::EOpConvUint64ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4604 | #ifdef AMD_EXTENSIONS |
| 4605 | case glslang::EOpConvUintToUint16: |
| 4606 | case glslang::EOpConvUint16ToUint: |
| 4607 | case glslang::EOpConvUint64ToUint16: |
| 4608 | case glslang::EOpConvUint16ToUint64: |
| 4609 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4610 | convOp = spv::OpUConvert; |
| 4611 | break; |
| 4612 | |
| 4613 | case glslang::EOpConvIntToUint64: |
| 4614 | case glslang::EOpConvInt64ToUint: |
| 4615 | case glslang::EOpConvUint64ToInt: |
| 4616 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4617 | #ifdef AMD_EXTENSIONS |
| 4618 | case glslang::EOpConvInt16ToUint: |
| 4619 | case glslang::EOpConvUintToInt16: |
| 4620 | case glslang::EOpConvInt16ToUint64: |
| 4621 | case glslang::EOpConvUint64ToInt16: |
| 4622 | case glslang::EOpConvUint16ToInt: |
| 4623 | case glslang::EOpConvIntToUint16: |
| 4624 | case glslang::EOpConvUint16ToInt64: |
| 4625 | case glslang::EOpConvInt64ToUint16: |
| 4626 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4627 | // OpSConvert/OpUConvert + OpBitCast |
| 4628 | switch (op) { |
| 4629 | case glslang::EOpConvIntToUint64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4630 | #ifdef AMD_EXTENSIONS |
| 4631 | case glslang::EOpConvInt16ToUint64: |
| 4632 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4633 | convOp = spv::OpSConvert; |
| 4634 | type = builder.makeIntType(64); |
| 4635 | break; |
| 4636 | case glslang::EOpConvInt64ToUint: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4637 | #ifdef AMD_EXTENSIONS |
| 4638 | case glslang::EOpConvInt16ToUint: |
| 4639 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4640 | convOp = spv::OpSConvert; |
| 4641 | type = builder.makeIntType(32); |
| 4642 | break; |
| 4643 | case glslang::EOpConvUint64ToInt: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4644 | #ifdef AMD_EXTENSIONS |
| 4645 | case glslang::EOpConvUint16ToInt: |
| 4646 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4647 | convOp = spv::OpUConvert; |
| 4648 | type = builder.makeUintType(32); |
| 4649 | break; |
| 4650 | case glslang::EOpConvUintToInt64: |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4651 | #ifdef AMD_EXTENSIONS |
| 4652 | case glslang::EOpConvUint16ToInt64: |
| 4653 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4654 | convOp = spv::OpUConvert; |
| 4655 | type = builder.makeUintType(64); |
| 4656 | break; |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4657 | #ifdef AMD_EXTENSIONS |
| 4658 | case glslang::EOpConvUintToInt16: |
| 4659 | case glslang::EOpConvUint64ToInt16: |
| 4660 | convOp = spv::OpUConvert; |
| 4661 | type = builder.makeUintType(16); |
| 4662 | break; |
| 4663 | case glslang::EOpConvIntToUint16: |
| 4664 | case glslang::EOpConvInt64ToUint16: |
| 4665 | convOp = spv::OpSConvert; |
| 4666 | type = builder.makeIntType(16); |
| 4667 | break; |
| 4668 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4669 | default: |
| 4670 | assert(0); |
| 4671 | break; |
| 4672 | } |
| 4673 | |
| 4674 | if (vectorSize > 0) |
| 4675 | type = builder.makeVectorType(type, vectorSize); |
| 4676 | |
| 4677 | operand = builder.createUnaryOp(convOp, type, operand); |
| 4678 | |
| 4679 | if (builder.isInSpecConstCodeGenMode()) { |
| 4680 | // Build zero scalar or vector for OpIAdd. |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 4681 | #ifdef AMD_EXTENSIONS |
| 4682 | if (op == glslang::EOpConvIntToUint64 || op == glslang::EOpConvUintToInt64 || |
| 4683 | op == glslang::EOpConvInt16ToUint64 || op == glslang::EOpConvUint16ToInt64) |
| 4684 | zero = builder.makeUint64Constant(0); |
| 4685 | else if (op == glslang::EOpConvIntToUint16 || op == glslang::EOpConvUintToInt16 || |
| 4686 | op == glslang::EOpConvInt64ToUint16 || op == glslang::EOpConvUint64ToInt16) |
| 4687 | zero = builder.makeUint16Constant(0); |
| 4688 | else |
| 4689 | zero = builder.makeUintConstant(0); |
| 4690 | #else |
| 4691 | if (op == glslang::EOpConvIntToUint64 || op == glslang::EOpConvUintToInt64) |
| 4692 | zero = builder.makeUint64Constant(0); |
| 4693 | else |
| 4694 | zero = builder.makeUintConstant(0); |
| 4695 | #endif |
| 4696 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4697 | zero = makeSmearedConstant(zero, vectorSize); |
| 4698 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 4699 | // generating for OpSpecConstantOp instruction. |
| 4700 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 4701 | } |
| 4702 | // For normal run-time conversion instruction, use OpBitcast. |
| 4703 | convOp = spv::OpBitcast; |
| 4704 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4705 | default: |
| 4706 | break; |
| 4707 | } |
| 4708 | |
| 4709 | spv::Id result = 0; |
| 4710 | if (convOp == spv::OpNop) |
| 4711 | return result; |
| 4712 | |
| 4713 | if (convOp == spv::OpSelect) { |
| 4714 | zero = makeSmearedConstant(zero, vectorSize); |
| 4715 | one = makeSmearedConstant(one, vectorSize); |
| 4716 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 4717 | } else |
| 4718 | result = builder.createUnaryOp(convOp, destType, operand); |
| 4719 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4720 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 4724 | { |
| 4725 | if (vectorSize == 0) |
| 4726 | return constant; |
| 4727 | |
| 4728 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 4729 | std::vector<spv::Id> components; |
| 4730 | for (int c = 0; c < vectorSize; ++c) |
| 4731 | components.push_back(constant); |
| 4732 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 4733 | } |
| 4734 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4735 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4736 | 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] | 4737 | { |
| 4738 | spv::Op opCode = spv::OpNop; |
| 4739 | |
| 4740 | switch (op) { |
| 4741 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4742 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4743 | case glslang::EOpAtomicCounterAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4744 | opCode = spv::OpAtomicIAdd; |
| 4745 | break; |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4746 | case glslang::EOpAtomicCounterSubtract: |
| 4747 | opCode = spv::OpAtomicISub; |
| 4748 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4749 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4750 | case glslang::EOpImageAtomicMin: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4751 | case glslang::EOpAtomicCounterMin: |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4752 | opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4753 | break; |
| 4754 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4755 | case glslang::EOpImageAtomicMax: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4756 | case glslang::EOpAtomicCounterMax: |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4757 | opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4758 | break; |
| 4759 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4760 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4761 | case glslang::EOpAtomicCounterAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4762 | opCode = spv::OpAtomicAnd; |
| 4763 | break; |
| 4764 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4765 | case glslang::EOpImageAtomicOr: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4766 | case glslang::EOpAtomicCounterOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4767 | opCode = spv::OpAtomicOr; |
| 4768 | break; |
| 4769 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4770 | case glslang::EOpImageAtomicXor: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4771 | case glslang::EOpAtomicCounterXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4772 | opCode = spv::OpAtomicXor; |
| 4773 | break; |
| 4774 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4775 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4776 | case glslang::EOpAtomicCounterExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4777 | opCode = spv::OpAtomicExchange; |
| 4778 | break; |
| 4779 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4780 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 0d0c6d3 | 2017-07-23 16:08:26 -0600 | [diff] [blame] | 4781 | case glslang::EOpAtomicCounterCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4782 | opCode = spv::OpAtomicCompareExchange; |
| 4783 | break; |
| 4784 | case glslang::EOpAtomicCounterIncrement: |
| 4785 | opCode = spv::OpAtomicIIncrement; |
| 4786 | break; |
| 4787 | case glslang::EOpAtomicCounterDecrement: |
| 4788 | opCode = spv::OpAtomicIDecrement; |
| 4789 | break; |
| 4790 | case glslang::EOpAtomicCounter: |
| 4791 | opCode = spv::OpAtomicLoad; |
| 4792 | break; |
| 4793 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4794 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4795 | break; |
| 4796 | } |
| 4797 | |
| 4798 | // Sort out the operands |
| 4799 | // - mapping from glslang -> SPV |
| 4800 | // - there are extra SPV operands with no glslang source |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4801 | // - compare-exchange swaps the value and comparator |
| 4802 | // - compare-exchange has an extra memory semantics |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4803 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 4804 | auto opIt = operands.begin(); // walk the glslang operands |
| 4805 | spvAtomicOperands.push_back(*(opIt++)); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4806 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::ScopeDevice)); // TBD: what is the correct scope? |
| 4807 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); // TBD: what are the correct memory semantics? |
| 4808 | if (opCode == spv::OpAtomicCompareExchange) { |
Rex Xu | bba5c80 | 2015-09-16 13:20:37 +0800 | [diff] [blame] | 4809 | // There are 2 memory semantics for compare-exchange. And the operand order of "comparator" and "new value" in GLSL |
| 4810 | // differs from that in SPIR-V. Hence, special processing is required. |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4811 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4812 | spvAtomicOperands.push_back(*(opIt + 1)); |
| 4813 | spvAtomicOperands.push_back(*opIt); |
| 4814 | opIt += 2; |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4815 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4816 | |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4817 | // 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] | 4818 | for (; opIt != operands.end(); ++opIt) |
| 4819 | spvAtomicOperands.push_back(*opIt); |
| 4820 | |
| 4821 | return builder.createOp(opCode, typeId, spvAtomicOperands); |
| 4822 | } |
| 4823 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4824 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4825 | 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] | 4826 | { |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4827 | #ifdef AMD_EXTENSIONS |
Jamie Madill | 57cb69a | 2016-11-09 13:49:24 -0500 | [diff] [blame] | 4828 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4829 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4830 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4831 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4832 | spv::Op opCode = spv::OpNop; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4833 | std::vector<spv::Id> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4834 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 4835 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4836 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 4837 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4838 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 4839 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4840 | } else if (op == glslang::EOpAnyInvocation || |
| 4841 | op == glslang::EOpAllInvocations || |
| 4842 | op == glslang::EOpAllInvocationsEqual) { |
| 4843 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 4844 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4845 | } else { |
| 4846 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 4847 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 4848 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 4849 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4850 | op == glslang::EOpAddInvocationsNonUniform || |
| 4851 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 4852 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 4853 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 4854 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 4855 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 4856 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 4857 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 4858 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4859 | |
| 4860 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4861 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4862 | switch (op) { |
| 4863 | case glslang::EOpMinInvocations: |
| 4864 | case glslang::EOpMaxInvocations: |
| 4865 | case glslang::EOpAddInvocations: |
| 4866 | case glslang::EOpMinInvocationsNonUniform: |
| 4867 | case glslang::EOpMaxInvocationsNonUniform: |
| 4868 | case glslang::EOpAddInvocationsNonUniform: |
| 4869 | groupOperation = spv::GroupOperationReduce; |
| 4870 | spvGroupOperands.push_back(groupOperation); |
| 4871 | break; |
| 4872 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4873 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4874 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4875 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4876 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4877 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4878 | groupOperation = spv::GroupOperationInclusiveScan; |
| 4879 | spvGroupOperands.push_back(groupOperation); |
| 4880 | break; |
| 4881 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4882 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4883 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4884 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4885 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4886 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 4887 | groupOperation = spv::GroupOperationExclusiveScan; |
| 4888 | spvGroupOperands.push_back(groupOperation); |
| 4889 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 4890 | default: |
| 4891 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4892 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4893 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) |
| 4897 | spvGroupOperands.push_back(*opIt); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4898 | |
| 4899 | switch (op) { |
| 4900 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4901 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4902 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4903 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4904 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4905 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4906 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4907 | opCode = spv::OpSubgroupAllEqualKHR; |
| 4908 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4909 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4910 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4911 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4912 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4913 | break; |
| 4914 | case glslang::EOpReadFirstInvocation: |
| 4915 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 4916 | break; |
| 4917 | case glslang::EOpBallot: |
| 4918 | { |
| 4919 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 4920 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 4921 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 4922 | // |
| 4923 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 4924 | // |
| 4925 | spv::Id uintType = builder.makeUintType(32); |
| 4926 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 4927 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 4928 | |
| 4929 | std::vector<spv::Id> components; |
| 4930 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 4931 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 4932 | |
| 4933 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 4934 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 4935 | builder.createCompositeConstruct(uvec2Type, components)); |
| 4936 | } |
| 4937 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4938 | #ifdef AMD_EXTENSIONS |
| 4939 | case glslang::EOpMinInvocations: |
| 4940 | case glslang::EOpMaxInvocations: |
| 4941 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4942 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4943 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4944 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4945 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4946 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4947 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4948 | if (op == glslang::EOpMinInvocations || |
| 4949 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 4950 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4951 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4952 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4953 | else { |
| 4954 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4955 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4956 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4957 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4958 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4959 | } else if (op == glslang::EOpMaxInvocations || |
| 4960 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 4961 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4962 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4963 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4964 | else { |
| 4965 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4966 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4967 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4968 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4969 | } |
| 4970 | } else { |
| 4971 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4972 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4973 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4974 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4975 | } |
| 4976 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4977 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4978 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4979 | |
| 4980 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4981 | case glslang::EOpMinInvocationsNonUniform: |
| 4982 | case glslang::EOpMaxInvocationsNonUniform: |
| 4983 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4984 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4985 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4986 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4987 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4988 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4989 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 4990 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 4991 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 4992 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4993 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4994 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4995 | else { |
| 4996 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4997 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4998 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4999 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5000 | } |
| 5001 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5002 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 5003 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 5004 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5005 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5006 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5007 | else { |
| 5008 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5009 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5010 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5011 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5012 | } |
| 5013 | } |
| 5014 | else { |
| 5015 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5016 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5017 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5018 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5019 | } |
| 5020 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5021 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5022 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5023 | |
| 5024 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5025 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5026 | default: |
| 5027 | logger->missingFunctionality("invocation operation"); |
| 5028 | return spv::NoResult; |
| 5029 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5030 | |
| 5031 | assert(opCode != spv::OpNop); |
| 5032 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 5033 | } |
| 5034 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5035 | // Create group invocation operations on a vector |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5036 | 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] | 5037 | { |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5038 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5039 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 5040 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5041 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5042 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5043 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 5044 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 5045 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5046 | #else |
| 5047 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 5048 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5049 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 5050 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5051 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5052 | |
| 5053 | // Handle group invocation operations scalar by scalar. |
| 5054 | // The result type is the same type as the original type. |
| 5055 | // The algorithm is to: |
| 5056 | // - break the vector into scalars |
| 5057 | // - apply the operation to each scalar |
| 5058 | // - make a vector out the scalar results |
| 5059 | |
| 5060 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5061 | int numComponents = builder.getNumComponents(operands[0]); |
| 5062 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5063 | std::vector<spv::Id> results; |
| 5064 | |
| 5065 | // do each scalar op |
| 5066 | for (int comp = 0; comp < numComponents; ++comp) { |
| 5067 | std::vector<unsigned int> indexes; |
| 5068 | indexes.push_back(comp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5069 | spv::Id scalar = builder.createCompositeExtract(operands[0], scalarType, indexes); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5070 | std::vector<spv::Id> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5071 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 5072 | spvGroupOperands.push_back(scalar); |
| 5073 | spvGroupOperands.push_back(operands[1]); |
| 5074 | } else if (op == spv::OpGroupBroadcast) { |
| 5075 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5076 | spvGroupOperands.push_back(scalar); |
| 5077 | spvGroupOperands.push_back(operands[1]); |
| 5078 | } else { |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 5079 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 5080 | spvGroupOperands.push_back(groupOperation); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5081 | spvGroupOperands.push_back(scalar); |
| 5082 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5083 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 5084 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | // put the pieces together |
| 5088 | return builder.createCompositeConstruct(typeId, results); |
| 5089 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 5090 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5091 | 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] | 5092 | { |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5093 | #ifdef AMD_EXTENSIONS |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5094 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64 || typeProxy == glslang::EbtUint16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5095 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 5096 | #else |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5097 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5098 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5099 | #endif |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5100 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5101 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5102 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5103 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 5104 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5105 | spv::Id typeId0 = 0; |
| 5106 | if (consumedOperands > 0) |
| 5107 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5108 | spv::Id typeId1 = 0; |
| 5109 | if (consumedOperands > 1) |
| 5110 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5111 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5112 | |
| 5113 | switch (op) { |
| 5114 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5115 | if (isFloat) |
| 5116 | libCall = spv::GLSLstd450FMin; |
| 5117 | else if (isUnsigned) |
| 5118 | libCall = spv::GLSLstd450UMin; |
| 5119 | else |
| 5120 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5121 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5122 | break; |
| 5123 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5124 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5125 | break; |
| 5126 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5127 | if (isFloat) |
| 5128 | libCall = spv::GLSLstd450FMax; |
| 5129 | else if (isUnsigned) |
| 5130 | libCall = spv::GLSLstd450UMax; |
| 5131 | else |
| 5132 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5133 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5134 | break; |
| 5135 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5136 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5137 | break; |
| 5138 | case glslang::EOpDot: |
| 5139 | opCode = spv::OpDot; |
| 5140 | break; |
| 5141 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5142 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5143 | break; |
| 5144 | |
| 5145 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5146 | if (isFloat) |
| 5147 | libCall = spv::GLSLstd450FClamp; |
| 5148 | else if (isUnsigned) |
| 5149 | libCall = spv::GLSLstd450UClamp; |
| 5150 | else |
| 5151 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5152 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 5153 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5154 | break; |
| 5155 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5156 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 5157 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5158 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5159 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5160 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 5161 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5162 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5163 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5164 | break; |
| 5165 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5166 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5167 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5168 | break; |
| 5169 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5170 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 5171 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 5172 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5173 | break; |
| 5174 | |
| 5175 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5176 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5177 | break; |
| 5178 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5179 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5180 | break; |
| 5181 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5182 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5183 | break; |
| 5184 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5185 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5186 | break; |
| 5187 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5188 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5189 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5190 | case glslang::EOpInterpolateAtSample: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5191 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5192 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 5193 | break; |
| 5194 | case glslang::EOpInterpolateAtOffset: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5195 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 5196 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 5197 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5198 | case glslang::EOpAddCarry: |
| 5199 | opCode = spv::OpIAddCarry; |
| 5200 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5201 | consumedOperands = 2; |
| 5202 | break; |
| 5203 | case glslang::EOpSubBorrow: |
| 5204 | opCode = spv::OpISubBorrow; |
| 5205 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5206 | consumedOperands = 2; |
| 5207 | break; |
| 5208 | case glslang::EOpUMulExtended: |
| 5209 | opCode = spv::OpUMulExtended; |
| 5210 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5211 | consumedOperands = 2; |
| 5212 | break; |
| 5213 | case glslang::EOpIMulExtended: |
| 5214 | opCode = spv::OpSMulExtended; |
| 5215 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 5216 | consumedOperands = 2; |
| 5217 | break; |
| 5218 | case glslang::EOpBitfieldExtract: |
| 5219 | if (isUnsigned) |
| 5220 | opCode = spv::OpBitFieldUExtract; |
| 5221 | else |
| 5222 | opCode = spv::OpBitFieldSExtract; |
| 5223 | break; |
| 5224 | case glslang::EOpBitfieldInsert: |
| 5225 | opCode = spv::OpBitFieldInsert; |
| 5226 | break; |
| 5227 | |
| 5228 | case glslang::EOpFma: |
| 5229 | libCall = spv::GLSLstd450Fma; |
| 5230 | break; |
| 5231 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5232 | { |
| 5233 | libCall = spv::GLSLstd450FrexpStruct; |
| 5234 | assert(builder.isPointerType(typeId1)); |
| 5235 | typeId1 = builder.getContainedTypeId(typeId1); |
| 5236 | #ifdef AMD_EXTENSIONS |
| 5237 | int width = builder.getScalarTypeWidth(typeId1); |
| 5238 | #else |
| 5239 | int width = 32; |
| 5240 | #endif |
| 5241 | if (builder.getNumComponents(operands[0]) == 1) |
| 5242 | frexpIntType = builder.makeIntegerType(width, true); |
| 5243 | else |
| 5244 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 5245 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 5246 | consumedOperands = 1; |
| 5247 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5248 | break; |
| 5249 | case glslang::EOpLdexp: |
| 5250 | libCall = spv::GLSLstd450Ldexp; |
| 5251 | break; |
| 5252 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5253 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5254 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 5255 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5256 | #ifdef AMD_EXTENSIONS |
| 5257 | case glslang::EOpSwizzleInvocations: |
| 5258 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5259 | libCall = spv::SwizzleInvocationsAMD; |
| 5260 | break; |
| 5261 | case glslang::EOpSwizzleInvocationsMasked: |
| 5262 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5263 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 5264 | break; |
| 5265 | case glslang::EOpWriteInvocation: |
| 5266 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 5267 | libCall = spv::WriteInvocationAMD; |
| 5268 | break; |
| 5269 | |
| 5270 | case glslang::EOpMin3: |
| 5271 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5272 | if (isFloat) |
| 5273 | libCall = spv::FMin3AMD; |
| 5274 | else { |
| 5275 | if (isUnsigned) |
| 5276 | libCall = spv::UMin3AMD; |
| 5277 | else |
| 5278 | libCall = spv::SMin3AMD; |
| 5279 | } |
| 5280 | break; |
| 5281 | case glslang::EOpMax3: |
| 5282 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5283 | if (isFloat) |
| 5284 | libCall = spv::FMax3AMD; |
| 5285 | else { |
| 5286 | if (isUnsigned) |
| 5287 | libCall = spv::UMax3AMD; |
| 5288 | else |
| 5289 | libCall = spv::SMax3AMD; |
| 5290 | } |
| 5291 | break; |
| 5292 | case glslang::EOpMid3: |
| 5293 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 5294 | if (isFloat) |
| 5295 | libCall = spv::FMid3AMD; |
| 5296 | else { |
| 5297 | if (isUnsigned) |
| 5298 | libCall = spv::UMid3AMD; |
| 5299 | else |
| 5300 | libCall = spv::SMid3AMD; |
| 5301 | } |
| 5302 | break; |
| 5303 | |
| 5304 | case glslang::EOpInterpolateAtVertex: |
| 5305 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 5306 | libCall = spv::InterpolateAtVertexAMD; |
| 5307 | break; |
| 5308 | #endif |
| 5309 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5310 | default: |
| 5311 | return 0; |
| 5312 | } |
| 5313 | |
| 5314 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 5315 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 5316 | // Use an extended instruction from the standard library. |
| 5317 | // Construct the call arguments, without modifying the original operands vector. |
| 5318 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 5319 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5320 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 5321 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5322 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5323 | case 0: |
| 5324 | // should all be handled by visitAggregate and createNoArgOperation |
| 5325 | assert(0); |
| 5326 | return 0; |
| 5327 | case 1: |
| 5328 | // should all be handled by createUnaryOperation |
| 5329 | assert(0); |
| 5330 | return 0; |
| 5331 | case 2: |
| 5332 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 5333 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5334 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5335 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 5336 | assert(consumedOperands == operands.size()); |
| 5337 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5338 | break; |
| 5339 | } |
| 5340 | } |
| 5341 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5342 | // Decode the return types that were structures |
| 5343 | switch (op) { |
| 5344 | case glslang::EOpAddCarry: |
| 5345 | case glslang::EOpSubBorrow: |
| 5346 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 5347 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 5348 | break; |
| 5349 | case glslang::EOpUMulExtended: |
| 5350 | case glslang::EOpIMulExtended: |
| 5351 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 5352 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 5353 | break; |
| 5354 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 5355 | { |
| 5356 | assert(operands.size() == 2); |
| 5357 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 5358 | // "exp" is floating-point type (from HLSL intrinsic) |
| 5359 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 5360 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 5361 | builder.createStore(member1, operands[1]); |
| 5362 | } else |
| 5363 | // "exp" is integer type (from GLSL built-in function) |
| 5364 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 5365 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 5366 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5367 | break; |
| 5368 | default: |
| 5369 | break; |
| 5370 | } |
| 5371 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 5372 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5373 | } |
| 5374 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5375 | // Intrinsics with no arguments (or no return value, and no precision). |
| 5376 | 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] | 5377 | { |
| 5378 | // TODO: get the barrier operands correct |
| 5379 | |
| 5380 | switch (op) { |
| 5381 | case glslang::EOpEmitVertex: |
| 5382 | builder.createNoResultOp(spv::OpEmitVertex); |
| 5383 | return 0; |
| 5384 | case glslang::EOpEndPrimitive: |
| 5385 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 5386 | return 0; |
| 5387 | case glslang::EOpBarrier: |
chrgau01@arm.com | c3f1cdf | 2016-11-14 10:10:05 +0100 | [diff] [blame] | 5388 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsMaskNone); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5389 | return 0; |
| 5390 | case glslang::EOpMemoryBarrier: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5391 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAllMemory); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5392 | return 0; |
| 5393 | case glslang::EOpMemoryBarrierAtomicCounter: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5394 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAtomicCounterMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5395 | return 0; |
| 5396 | case glslang::EOpMemoryBarrierBuffer: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5397 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5398 | return 0; |
| 5399 | case glslang::EOpMemoryBarrierImage: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5400 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsImageMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5401 | return 0; |
| 5402 | case glslang::EOpMemoryBarrierShared: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5403 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsWorkgroupMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5404 | return 0; |
| 5405 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5406 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5407 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 5408 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 5409 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5410 | builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsAllMemory); |
| 5411 | return 0; |
| 5412 | case glslang::EOpGroupMemoryBarrierWithGroupSync: |
| 5413 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5414 | builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask); |
| 5415 | return 0; |
| 5416 | case glslang::EOpWorkgroupMemoryBarrier: |
| 5417 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask); |
| 5418 | return 0; |
| 5419 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
| 5420 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5421 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask); |
| 5422 | return 0; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5423 | #ifdef AMD_EXTENSIONS |
| 5424 | case glslang::EOpTime: |
| 5425 | { |
| 5426 | std::vector<spv::Id> args; // Dummy arguments |
| 5427 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 5428 | return builder.setPrecision(id, precision); |
| 5429 | } |
| 5430 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5431 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5432 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5433 | return 0; |
| 5434 | } |
| 5435 | } |
| 5436 | |
| 5437 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 5438 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 5439 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5440 | spv::Id id; |
| 5441 | if (symbolValues.end() != iter) { |
| 5442 | id = iter->second; |
| 5443 | return id; |
| 5444 | } |
| 5445 | |
| 5446 | // it was not found, create it |
| 5447 | id = createSpvVariable(symbol); |
| 5448 | symbolValues[symbol->getId()] = id; |
| 5449 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 5450 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5451 | addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 5452 | addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 5453 | addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5454 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
| 5455 | addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5456 | if (symbol->getQualifier().hasIndex()) |
| 5457 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 5458 | if (symbol->getQualifier().hasComponent()) |
| 5459 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 5460 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5461 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5462 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5463 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5464 | if (symbol->getQualifier().hasXfbBuffer()) |
| 5465 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 5466 | if (symbol->getQualifier().hasXfbOffset()) |
| 5467 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
| 5468 | } |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 5469 | // atomic counters use this: |
| 5470 | if (symbol->getQualifier().hasOffset()) |
| 5471 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5472 | } |
| 5473 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 5474 | if (symbol->getQualifier().hasLocation()) |
| 5475 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 5476 | addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 5477 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5478 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5479 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5480 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5481 | if (symbol->getQualifier().hasSet()) |
| 5482 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5483 | else if (IsDescriptorResource(symbol->getType())) { |
| 5484 | // default to 0 |
| 5485 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 5486 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5487 | if (symbol->getQualifier().hasBinding()) |
| 5488 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5489 | if (symbol->getQualifier().hasAttachment()) |
| 5490 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5491 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5492 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5493 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5494 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5495 | if (symbol->getQualifier().hasXfbBuffer()) |
| 5496 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 5497 | } |
| 5498 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 5499 | if (symbol->getType().isImage()) { |
| 5500 | std::vector<spv::Decoration> memory; |
| 5501 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory); |
| 5502 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 5503 | addDecoration(id, memory[i]); |
| 5504 | } |
| 5505 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5506 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 5507 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5508 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5509 | addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5510 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 5511 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5512 | if (builtIn == spv::BuiltInSampleMask) { |
| 5513 | spv::Decoration decoration; |
| 5514 | // GL_NV_sample_mask_override_coverage extension |
| 5515 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5516 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5517 | else |
| 5518 | decoration = (spv::Decoration)spv::DecorationMax; |
| 5519 | addDecoration(id, decoration); |
| 5520 | if (decoration != spv::DecorationMax) { |
| 5521 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 5522 | } |
| 5523 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5524 | else if (builtIn == spv::BuiltInLayer) { |
| 5525 | // SPV_NV_viewport_array2 extension |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 5526 | if (symbol->getQualifier().layoutViewportRelative) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5527 | addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 5528 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 5529 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 5530 | } |
John Kessenich | b41bff6 | 2017-08-11 13:07:17 -0600 | [diff] [blame] | 5531 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5532 | addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
| 5533 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 5534 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 5535 | } |
| 5536 | } |
| 5537 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 5538 | if (symbol->getQualifier().layoutPassthrough) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5539 | addDecoration(id, spv::DecorationPassthroughNV); |
| 5540 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 5541 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 5542 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5543 | #endif |
| 5544 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5545 | return id; |
| 5546 | } |
| 5547 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5548 | // If 'dec' is valid, add no-operand decoration to an object |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5549 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec) |
| 5550 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5551 | if (dec != spv::DecorationMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5552 | builder.addDecoration(id, dec); |
| 5553 | } |
| 5554 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5555 | // If 'dec' is valid, add a one-operand decoration to an object |
| 5556 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec, unsigned value) |
| 5557 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5558 | if (dec != spv::DecorationMax) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5559 | builder.addDecoration(id, dec, value); |
| 5560 | } |
| 5561 | |
| 5562 | // 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] | 5563 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec) |
| 5564 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5565 | if (dec != spv::DecorationMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5566 | builder.addMemberDecoration(id, (unsigned)member, dec); |
| 5567 | } |
| 5568 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5569 | // If 'dec' is valid, add a one-operand decoration to a struct member |
| 5570 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value) |
| 5571 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5572 | if (dec != spv::DecorationMax) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5573 | builder.addMemberDecoration(id, (unsigned)member, dec, value); |
| 5574 | } |
| 5575 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5576 | // 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] | 5577 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5578 | // |
| 5579 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 5580 | // |
| 5581 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 5582 | // regular constants, which themselves are trees that createSpvConstant() |
| 5583 | // recursively walks. So, this function walks the "top" of the tree: |
| 5584 | // - emit specialization constant-building instructions for specConstant |
| 5585 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5586 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5587 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 5588 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5589 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5590 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5591 | if (! node.getQualifier().specConstant) { |
| 5592 | // hand off to the non-spec-constant path |
| 5593 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 5594 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5595 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5596 | nextConst, false); |
| 5597 | } |
| 5598 | |
| 5599 | // We now know we have a specialization constant to build |
| 5600 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 5601 | // 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] | 5602 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 5603 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 5604 | std::vector<spv::Id> dimConstId; |
| 5605 | for (int dim = 0; dim < 3; ++dim) { |
| 5606 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 5607 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 5608 | if (specConst) |
| 5609 | addDecoration(dimConstId.back(), spv::DecorationSpecId, glslangIntermediate->getLocalSizeSpecId(dim)); |
| 5610 | } |
| 5611 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 5612 | } |
| 5613 | |
| 5614 | // An AST node labelled as specialization constant should be a symbol node. |
| 5615 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 5616 | if (auto* sn = node.getAsSymbolNode()) { |
| 5617 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 5618 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 5619 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 5620 | // will set the builder into spec constant op instruction generating mode. |
| 5621 | sub_tree->traverse(this); |
| 5622 | return accessChainLoad(sub_tree->getType()); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5623 | } else if (auto* const_union_array = &sn->getConstArray()){ |
| 5624 | int nextConst = 0; |
Endre Oma | ad58d45 | 2017-01-31 21:08:19 +0100 | [diff] [blame] | 5625 | spv::Id id = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
| 5626 | builder.addName(id, sn->getName().c_str()); |
| 5627 | return id; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5628 | } |
| 5629 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5630 | |
| 5631 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 5632 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5633 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5634 | exit(1); |
| 5635 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5636 | } |
| 5637 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5638 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 5639 | // build the aggregate SPIR-V constant. |
| 5640 | // |
| 5641 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 5642 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 5643 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5644 | 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] | 5645 | { |
| 5646 | // vector of constants for SPIR-V |
| 5647 | std::vector<spv::Id> spvConsts; |
| 5648 | |
| 5649 | // Type is used for struct and array constants |
| 5650 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 5651 | |
| 5652 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 5653 | glslang::TType elementType(glslangType, 0); |
| 5654 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5655 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5656 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 5657 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5658 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5659 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5660 | } else if (glslangType.getStruct()) { |
| 5661 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 5662 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5663 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5664 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5665 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 5666 | bool zero = nextConst >= consts.size(); |
| 5667 | switch (glslangType.getBasicType()) { |
| 5668 | case glslang::EbtInt: |
| 5669 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 5670 | break; |
| 5671 | case glslang::EbtUint: |
| 5672 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 5673 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5674 | case glslang::EbtInt64: |
| 5675 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 5676 | break; |
| 5677 | case glslang::EbtUint64: |
| 5678 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 5679 | break; |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5680 | #ifdef AMD_EXTENSIONS |
| 5681 | case glslang::EbtInt16: |
| 5682 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : (short)consts[nextConst].getIConst())); |
| 5683 | break; |
| 5684 | case glslang::EbtUint16: |
| 5685 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : (unsigned short)consts[nextConst].getUConst())); |
| 5686 | break; |
| 5687 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5688 | case glslang::EbtFloat: |
| 5689 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 5690 | break; |
| 5691 | case glslang::EbtDouble: |
| 5692 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 5693 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5694 | #ifdef AMD_EXTENSIONS |
| 5695 | case glslang::EbtFloat16: |
| 5696 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 5697 | break; |
| 5698 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5699 | case glslang::EbtBool: |
| 5700 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 5701 | break; |
| 5702 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5703 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5704 | break; |
| 5705 | } |
| 5706 | ++nextConst; |
| 5707 | } |
| 5708 | } else { |
| 5709 | // we have a non-aggregate (scalar) constant |
| 5710 | bool zero = nextConst >= consts.size(); |
| 5711 | spv::Id scalar = 0; |
| 5712 | switch (glslangType.getBasicType()) { |
| 5713 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5714 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5715 | break; |
| 5716 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5717 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5718 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5719 | case glslang::EbtInt64: |
| 5720 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 5721 | break; |
| 5722 | case glslang::EbtUint64: |
| 5723 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 5724 | break; |
Rex Xu | cabbb78 | 2017-03-24 13:41:14 +0800 | [diff] [blame] | 5725 | #ifdef AMD_EXTENSIONS |
| 5726 | case glslang::EbtInt16: |
| 5727 | scalar = builder.makeInt16Constant(zero ? 0 : (short)consts[nextConst].getIConst(), specConstant); |
| 5728 | break; |
| 5729 | case glslang::EbtUint16: |
| 5730 | scalar = builder.makeUint16Constant(zero ? 0 : (unsigned short)consts[nextConst].getUConst(), specConstant); |
| 5731 | break; |
| 5732 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5733 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5734 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5735 | break; |
| 5736 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5737 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5738 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5739 | #ifdef AMD_EXTENSIONS |
| 5740 | case glslang::EbtFloat16: |
| 5741 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 5742 | break; |
| 5743 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5744 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5745 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5746 | break; |
| 5747 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5748 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5749 | break; |
| 5750 | } |
| 5751 | ++nextConst; |
| 5752 | return scalar; |
| 5753 | } |
| 5754 | |
| 5755 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 5756 | } |
| 5757 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5758 | // Return true if the node is a constant or symbol whose reading has no |
| 5759 | // non-trivial observable cost or effect. |
| 5760 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 5761 | { |
| 5762 | // don't know what this is |
| 5763 | if (node == nullptr) |
| 5764 | return false; |
| 5765 | |
| 5766 | // a constant is safe |
| 5767 | if (node->getAsConstantUnion() != nullptr) |
| 5768 | return true; |
| 5769 | |
| 5770 | // not a symbol means non-trivial |
| 5771 | if (node->getAsSymbolNode() == nullptr) |
| 5772 | return false; |
| 5773 | |
| 5774 | // a symbol, depends on what's being read |
| 5775 | switch (node->getType().getQualifier().storage) { |
| 5776 | case glslang::EvqTemporary: |
| 5777 | case glslang::EvqGlobal: |
| 5778 | case glslang::EvqIn: |
| 5779 | case glslang::EvqInOut: |
| 5780 | case glslang::EvqConst: |
| 5781 | case glslang::EvqConstReadOnly: |
| 5782 | case glslang::EvqUniform: |
| 5783 | return true; |
| 5784 | default: |
| 5785 | return false; |
| 5786 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5787 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5788 | |
| 5789 | // 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] | 5790 | // 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] | 5791 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5792 | // Return true if trivial. |
| 5793 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 5794 | { |
| 5795 | if (node == nullptr) |
| 5796 | return false; |
| 5797 | |
John Kessenich | 84cc15f | 2017-05-24 16:44:47 -0600 | [diff] [blame] | 5798 | // count non scalars as trivial, as well as anything coming from HLSL |
| 5799 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 5800 | return true; |
| 5801 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5802 | // symbols and constants are trivial |
| 5803 | if (isTrivialLeaf(node)) |
| 5804 | return true; |
| 5805 | |
| 5806 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 5807 | |
| 5808 | // not a simple operation |
| 5809 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 5810 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 5811 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 5812 | return false; |
| 5813 | |
| 5814 | // not on leaf nodes |
| 5815 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 5816 | return false; |
| 5817 | |
| 5818 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 5819 | return false; |
| 5820 | } |
| 5821 | |
| 5822 | switch (node->getAsOperator()->getOp()) { |
| 5823 | case glslang::EOpLogicalNot: |
| 5824 | case glslang::EOpConvIntToBool: |
| 5825 | case glslang::EOpConvUintToBool: |
| 5826 | case glslang::EOpConvFloatToBool: |
| 5827 | case glslang::EOpConvDoubleToBool: |
| 5828 | case glslang::EOpEqual: |
| 5829 | case glslang::EOpNotEqual: |
| 5830 | case glslang::EOpLessThan: |
| 5831 | case glslang::EOpGreaterThan: |
| 5832 | case glslang::EOpLessThanEqual: |
| 5833 | case glslang::EOpGreaterThanEqual: |
| 5834 | case glslang::EOpIndexDirect: |
| 5835 | case glslang::EOpIndexDirectStruct: |
| 5836 | case glslang::EOpLogicalXor: |
| 5837 | case glslang::EOpAny: |
| 5838 | case glslang::EOpAll: |
| 5839 | return true; |
| 5840 | default: |
| 5841 | return false; |
| 5842 | } |
| 5843 | } |
| 5844 | |
| 5845 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 5846 | // the left side is true (for &&) or false (for ||). |
| 5847 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 5848 | { |
| 5849 | spv::Id boolTypeId = builder.makeBoolType(); |
| 5850 | |
| 5851 | // emit left operand |
| 5852 | builder.clearAccessChain(); |
| 5853 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 5854 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5855 | |
| 5856 | // Operands to accumulate OpPhi operands |
| 5857 | std::vector<spv::Id> phiOperands; |
| 5858 | // accumulate left operand's phi information |
| 5859 | phiOperands.push_back(leftId); |
| 5860 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 5861 | |
| 5862 | // Make the two kinds of operation symmetric with a "!" |
| 5863 | // || => emit "if (! left) result = right" |
| 5864 | // && => emit "if ( left) result = right" |
| 5865 | // |
| 5866 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 5867 | // to 'builder' to have an "else" without an "then" |
| 5868 | if (op == glslang::EOpLogicalOr) |
| 5869 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 5870 | |
| 5871 | // make an "if" based on the left value |
Rex Xu | 57e6592 | 2017-07-04 23:23:40 +0800 | [diff] [blame] | 5872 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5873 | |
| 5874 | // emit right operand as the "then" part of the "if" |
| 5875 | builder.clearAccessChain(); |
| 5876 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 5877 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5878 | |
| 5879 | // accumulate left operand's phi information |
| 5880 | phiOperands.push_back(rightId); |
| 5881 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 5882 | |
| 5883 | // finish the "if" |
| 5884 | ifBuilder.makeEndIf(); |
| 5885 | |
| 5886 | // phi together the two results |
| 5887 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 5888 | } |
| 5889 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5890 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 5891 | // Import this set if it has not been imported yet. |
| 5892 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 5893 | { |
| 5894 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 5895 | return extBuiltinMap[name]; |
| 5896 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5897 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5898 | spv::Id extBuiltins = builder.import(name); |
| 5899 | extBuiltinMap[name] = extBuiltins; |
| 5900 | return extBuiltins; |
| 5901 | } |
| 5902 | } |
| 5903 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5904 | }; // end anonymous namespace |
| 5905 | |
| 5906 | namespace glslang { |
| 5907 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5908 | void GetSpirvVersion(std::string& version) |
| 5909 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 5910 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 5911 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5912 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5913 | version = buf; |
| 5914 | } |
| 5915 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5916 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5917 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5918 | { |
| 5919 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5920 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 5921 | if (out.fail()) |
| 5922 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5923 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 5924 | unsigned int word = spirv[i]; |
| 5925 | out.write((const char*)&word, 4); |
| 5926 | } |
| 5927 | out.close(); |
| 5928 | } |
| 5929 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5930 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 5931 | 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] | 5932 | { |
| 5933 | std::ofstream out; |
| 5934 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 5935 | if (out.fail()) |
| 5936 | printf("ERROR: Failed to open file: %s\n", baseName); |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5937 | out << "\t// " GLSLANG_REVISION " " GLSLANG_DATE << std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 5938 | if (varName != nullptr) { |
| 5939 | out << "\t #pragma once" << std::endl; |
| 5940 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 5941 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5942 | const int WORDS_PER_LINE = 8; |
| 5943 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 5944 | out << "\t"; |
| 5945 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 5946 | const unsigned int word = spirv[i + j]; |
| 5947 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 5948 | if (i + j + 1 < (int)spirv.size()) { |
| 5949 | out << ","; |
| 5950 | } |
| 5951 | } |
| 5952 | out << std::endl; |
| 5953 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 5954 | if (varName != nullptr) { |
| 5955 | out << "};"; |
| 5956 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5957 | out.close(); |
| 5958 | } |
| 5959 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5960 | // |
| 5961 | // Set up the glslang traversal |
| 5962 | // |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5963 | 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] | 5964 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5965 | spv::SpvBuildLogger logger; |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5966 | GlslangToSpv(intermediate, spirv, &logger, options); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 5967 | } |
| 5968 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5969 | void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
| 5970 | spv::SpvBuildLogger* logger, SpvOptions* options) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 5971 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5972 | TIntermNode* root = intermediate.getTreeRoot(); |
| 5973 | |
| 5974 | if (root == 0) |
| 5975 | return; |
| 5976 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5977 | glslang::SpvOptions defaultOptions; |
| 5978 | if (options == nullptr) |
| 5979 | options = &defaultOptions; |
| 5980 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5981 | glslang::GetThreadPoolAllocator().push(); |
| 5982 | |
John Kessenich | 121853f | 2017-05-31 17:11:16 -0600 | [diff] [blame] | 5983 | TGlslangToSpvTraverser it(&intermediate, logger, *options); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5984 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 5985 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5986 | it.dumpSpv(spirv); |
| 5987 | |
| 5988 | glslang::GetThreadPoolAllocator().pop(); |
| 5989 | } |
| 5990 | |
| 5991 | }; // end namespace glslang |