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: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 104 | TGlslangToSpvTraverser(const glslang::TIntermediate*, spv::SpvBuildLogger* logger); |
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); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 125 | spv::LoopControlMask TranslateLoopControl(glslang::TLoopControl) const; |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 126 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 127 | spv::Id createSpvVariable(const glslang::TIntermSymbol*); |
| 128 | spv::Id getSampledType(const glslang::TSampler&); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 129 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 130 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 131 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 132 | spv::Id convertGlslangToSpvType(const glslang::TType& type); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 133 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&); |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 134 | bool filterMember(const glslang::TType& member); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 135 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 136 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 137 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 138 | const glslang::TQualifier&, spv::Id); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 139 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 140 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 141 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 142 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 143 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 144 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 145 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 146 | 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] | 147 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 148 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 149 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 150 | void makeFunctions(const glslang::TIntermSequence&); |
| 151 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 152 | void visitFunctions(const glslang::TIntermSequence&); |
| 153 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 154 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 155 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 156 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 157 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 158 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 159 | 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); |
| 160 | spv::Id createBinaryMatrixOperation(spv::Op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right); |
| 161 | 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] | 162 | 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] | 163 | 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] | 164 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 165 | 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] | 166 | 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] | 167 | 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] | 168 | 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] | 169 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 170 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
| 171 | void addDecoration(spv::Id id, spv::Decoration dec); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 172 | void addDecoration(spv::Id id, spv::Decoration dec, unsigned value); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 173 | void addMemberDecoration(spv::Id id, int member, spv::Decoration dec); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 174 | void addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 175 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 176 | 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] | 177 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 178 | bool isTrivial(const glslang::TIntermTyped* node); |
| 179 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 180 | spv::Id getExtBuiltins(const char* name); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 181 | |
| 182 | spv::Function* shaderEntry; |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 183 | spv::Function* currentFunction; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 184 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 185 | int sequenceDepth; |
| 186 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 187 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 188 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 189 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 190 | spv::Builder builder; |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 191 | bool inEntryPoint; |
| 192 | bool entryPointTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 193 | 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] | 194 | 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] | 195 | const glslang::TIntermediate* glslangIntermediate; |
| 196 | spv::Id stdBuiltins; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 197 | std::unordered_map<const char*, spv::Id> extBuiltinMap; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 198 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 199 | std::unordered_map<int, spv::Id> symbolValues; |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 200 | 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] | 201 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 202 | 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] | 203 | 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] | 204 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | // |
| 208 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 209 | // |
| 210 | |
| 211 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 212 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 213 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 214 | switch (source) { |
| 215 | case glslang::EShSourceGlsl: |
| 216 | switch (profile) { |
| 217 | case ENoProfile: |
| 218 | case ECoreProfile: |
| 219 | case ECompatibilityProfile: |
| 220 | return spv::SourceLanguageGLSL; |
| 221 | case EEsProfile: |
| 222 | return spv::SourceLanguageESSL; |
| 223 | default: |
| 224 | return spv::SourceLanguageUnknown; |
| 225 | } |
| 226 | case glslang::EShSourceHlsl: |
John Kessenich | 6fa1764 | 2017-04-07 15:33:08 -0600 | [diff] [blame] | 227 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 228 | default: |
| 229 | return spv::SourceLanguageUnknown; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Translate glslang language (stage) to SPIR-V execution model. |
| 234 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 235 | { |
| 236 | switch (stage) { |
| 237 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 238 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 239 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 240 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 241 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 242 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
| 243 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 244 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 245 | return spv::ExecutionModelFragment; |
| 246 | } |
| 247 | } |
| 248 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 249 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 250 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 251 | { |
| 252 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 253 | case glslang::Esd1D: return spv::Dim1D; |
| 254 | case glslang::Esd2D: return spv::Dim2D; |
| 255 | case glslang::Esd3D: return spv::Dim3D; |
| 256 | case glslang::EsdCube: return spv::DimCube; |
| 257 | case glslang::EsdRect: return spv::DimRect; |
| 258 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 259 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 260 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 261 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 262 | return spv::Dim2D; |
| 263 | } |
| 264 | } |
| 265 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 266 | // Translate glslang precision to SPIR-V precision decorations. |
| 267 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 268 | { |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 269 | switch (glslangPrecision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 270 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 271 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 272 | default: |
| 273 | return spv::NoPrecision; |
| 274 | } |
| 275 | } |
| 276 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 277 | // Translate glslang type to SPIR-V precision decorations. |
| 278 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 279 | { |
| 280 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 281 | } |
| 282 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 283 | // Translate glslang type to SPIR-V block decorations. |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 284 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 285 | { |
| 286 | if (type.getBasicType() == glslang::EbtBlock) { |
| 287 | switch (type.getQualifier().storage) { |
| 288 | case glslang::EvqUniform: return spv::DecorationBlock; |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 289 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 290 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 291 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
| 292 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 293 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 298 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 299 | } |
| 300 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 301 | // Translate glslang type to SPIR-V memory decorations. |
| 302 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory) |
| 303 | { |
| 304 | if (qualifier.coherent) |
| 305 | memory.push_back(spv::DecorationCoherent); |
| 306 | if (qualifier.volatil) |
| 307 | memory.push_back(spv::DecorationVolatile); |
| 308 | if (qualifier.restrict) |
| 309 | memory.push_back(spv::DecorationRestrict); |
| 310 | if (qualifier.readonly) |
| 311 | memory.push_back(spv::DecorationNonWritable); |
| 312 | if (qualifier.writeonly) |
| 313 | memory.push_back(spv::DecorationNonReadable); |
| 314 | } |
| 315 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 316 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 317 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 318 | { |
| 319 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 320 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 321 | case glslang::ElmRowMajor: |
| 322 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 323 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 324 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 325 | default: |
| 326 | // opaque layouts don't need a majorness |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 327 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 328 | } |
| 329 | } else { |
| 330 | switch (type.getBasicType()) { |
| 331 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 332 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 333 | break; |
| 334 | case glslang::EbtBlock: |
| 335 | switch (type.getQualifier().storage) { |
| 336 | case glslang::EvqUniform: |
| 337 | case glslang::EvqBuffer: |
| 338 | switch (type.getQualifier().layoutPacking) { |
| 339 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 340 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 341 | default: |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 342 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 343 | } |
| 344 | case glslang::EvqVaryingIn: |
| 345 | case glslang::EvqVaryingOut: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 346 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 347 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 348 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 349 | assert(0); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 350 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 357 | // Returns spv::DecorationMax when no decoration |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 358 | // should be applied. |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 359 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 360 | { |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 361 | if (qualifier.smooth) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 362 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 363 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 364 | else if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 365 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 366 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 367 | return spv::DecorationFlat; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 368 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 369 | else if (qualifier.explicitInterp) { |
| 370 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 371 | return spv::DecorationExplicitInterpAMD; |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 372 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 373 | #endif |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 374 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 375 | return spv::DecorationMax; |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 379 | // Returns spv::DecorationMax when no decoration |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 380 | // should be applied. |
| 381 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 382 | { |
| 383 | if (qualifier.patch) |
| 384 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 385 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 386 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 387 | else if (qualifier.sample) { |
| 388 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 389 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 390 | } else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 391 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 392 | } |
| 393 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 394 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 395 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 396 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 397 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 398 | return spv::DecorationInvariant; |
| 399 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 400 | return spv::DecorationMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 401 | } |
| 402 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 403 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 404 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 405 | { |
| 406 | if (qualifier.noContraction) |
| 407 | return spv::DecorationNoContraction; |
| 408 | else |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 409 | return spv::DecorationMax; |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 410 | } |
| 411 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 412 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 413 | // associated capabilities when required. For some built-in variables, a capability |
| 414 | // is generated only when using the variable in an executable instruction, but not when |
| 415 | // just declaring a struct member variable with it. This is true for PointSize, |
| 416 | // ClipDistance, and CullDistance. |
| 417 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool memberDeclaration) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 418 | { |
| 419 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 420 | case glslang::EbvPointSize: |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 421 | // Defer adding the capability until the built-in is actually used. |
| 422 | if (! memberDeclaration) { |
| 423 | switch (glslangIntermediate->getStage()) { |
| 424 | case EShLangGeometry: |
| 425 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 426 | break; |
| 427 | case EShLangTessControl: |
| 428 | case EShLangTessEvaluation: |
| 429 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 430 | break; |
| 431 | default: |
| 432 | break; |
| 433 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 434 | } |
| 435 | return spv::BuiltInPointSize; |
| 436 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 437 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 438 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 439 | // They are now generated when used, rather than here when declared. |
| 440 | // Potentially, the specification should be more clear what the minimum |
| 441 | // use needed is to trigger the capability. |
| 442 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 443 | case glslang::EbvClipDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 444 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 445 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 446 | return spv::BuiltInClipDistance; |
| 447 | |
| 448 | case glslang::EbvCullDistance: |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 449 | if (!memberDeclaration) |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 450 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 451 | return spv::BuiltInCullDistance; |
| 452 | |
| 453 | case glslang::EbvViewportIndex: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 454 | if (!memberDeclaration) { |
| 455 | builder.addCapability(spv::CapabilityMultiViewport); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 456 | #ifdef NV_EXTENSIONS |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 457 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 458 | glslangIntermediate->getStage() == EShLangTessControl || |
| 459 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
| 460 | |
| 461 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 462 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerNV); |
| 463 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 464 | #endif |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 465 | } |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 466 | return spv::BuiltInViewportIndex; |
| 467 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 468 | case glslang::EbvSampleId: |
| 469 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 470 | return spv::BuiltInSampleId; |
| 471 | |
| 472 | case glslang::EbvSamplePosition: |
| 473 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 474 | return spv::BuiltInSamplePosition; |
| 475 | |
| 476 | case glslang::EbvSampleMask: |
| 477 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 478 | return spv::BuiltInSampleMask; |
| 479 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 480 | case glslang::EbvLayer: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 481 | if (!memberDeclaration) { |
| 482 | builder.addCapability(spv::CapabilityGeometry); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 483 | #ifdef NV_EXTENSIONS |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 484 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 485 | glslangIntermediate->getStage() == EShLangTessControl || |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 486 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
| 487 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 488 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 489 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerNV); |
| 490 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 491 | #endif |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 492 | } |
| 493 | |
John Kessenich | 78a4557 | 2016-07-08 14:05:15 -0600 | [diff] [blame] | 494 | return spv::BuiltInLayer; |
| 495 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 496 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 497 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 498 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 499 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 500 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 501 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 502 | case glslang::EbvBaseVertex: |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 503 | builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters); |
| 504 | builder.addCapability(spv::CapabilityDrawParameters); |
| 505 | return spv::BuiltInBaseVertex; |
| 506 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 507 | case glslang::EbvBaseInstance: |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 508 | builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters); |
| 509 | builder.addCapability(spv::CapabilityDrawParameters); |
| 510 | return spv::BuiltInBaseInstance; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 511 | |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 512 | case glslang::EbvDrawId: |
Rex Xu | f3b2747 | 2016-07-22 18:15:31 +0800 | [diff] [blame] | 513 | builder.addExtension(spv::E_SPV_KHR_shader_draw_parameters); |
| 514 | builder.addCapability(spv::CapabilityDrawParameters); |
| 515 | return spv::BuiltInDrawIndex; |
Maciej Jesionowski | 04b3e87 | 2016-09-26 16:49:09 +0200 | [diff] [blame] | 516 | |
| 517 | case glslang::EbvPrimitiveId: |
| 518 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 519 | builder.addCapability(spv::CapabilityGeometry); |
| 520 | return spv::BuiltInPrimitiveId; |
| 521 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 522 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 523 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 524 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 525 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 526 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 527 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 528 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 529 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 530 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 531 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 532 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 533 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 534 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 535 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 536 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 537 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 538 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 539 | case glslang::EbvSubGroupSize: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 540 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 541 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 542 | return spv::BuiltInSubgroupSize; |
| 543 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 544 | case glslang::EbvSubGroupInvocation: |
Rex Xu | 36876e6 | 2016-09-23 22:13:43 +0800 | [diff] [blame] | 545 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 546 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 547 | return spv::BuiltInSubgroupLocalInvocationId; |
| 548 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 549 | case glslang::EbvSubGroupEqMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 550 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 551 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 552 | return spv::BuiltInSubgroupEqMaskKHR; |
| 553 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 554 | case glslang::EbvSubGroupGeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 555 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 556 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 557 | return spv::BuiltInSubgroupGeMaskKHR; |
| 558 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 559 | case glslang::EbvSubGroupGtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 560 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 561 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 562 | return spv::BuiltInSubgroupGtMaskKHR; |
| 563 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 564 | case glslang::EbvSubGroupLeMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 565 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 566 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 567 | return spv::BuiltInSubgroupLeMaskKHR; |
| 568 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 569 | case glslang::EbvSubGroupLtMask: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 570 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 571 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 572 | return spv::BuiltInSubgroupLtMaskKHR; |
| 573 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 574 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 575 | case glslang::EbvBaryCoordNoPersp: |
| 576 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 577 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 578 | |
| 579 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 580 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 581 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 582 | |
| 583 | case glslang::EbvBaryCoordNoPerspSample: |
| 584 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 585 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 586 | |
| 587 | case glslang::EbvBaryCoordSmooth: |
| 588 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 589 | return spv::BuiltInBaryCoordSmoothAMD; |
| 590 | |
| 591 | case glslang::EbvBaryCoordSmoothCentroid: |
| 592 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 593 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 594 | |
| 595 | case glslang::EbvBaryCoordSmoothSample: |
| 596 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 597 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 598 | |
| 599 | case glslang::EbvBaryCoordPullModel: |
| 600 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 601 | return spv::BuiltInBaryCoordPullModelAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 602 | #endif |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 603 | |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 604 | case glslang::EbvDeviceIndex: |
| 605 | builder.addExtension(spv::E_SPV_KHR_device_group); |
| 606 | builder.addCapability(spv::CapabilityDeviceGroup); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 607 | return spv::BuiltInDeviceIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 608 | |
| 609 | case glslang::EbvViewIndex: |
| 610 | builder.addExtension(spv::E_SPV_KHR_multiview); |
| 611 | builder.addCapability(spv::CapabilityMultiView); |
John Kessenich | 42e33c9 | 2017-02-27 01:50:28 -0700 | [diff] [blame] | 612 | return spv::BuiltInViewIndex; |
John Kessenich | 6c8aaac | 2017-02-27 01:20:51 -0700 | [diff] [blame] | 613 | |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 614 | #ifdef NV_EXTENSIONS |
| 615 | case glslang::EbvViewportMaskNV: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 616 | if (!memberDeclaration) { |
| 617 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 618 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 619 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 620 | return spv::BuiltInViewportMaskNV; |
| 621 | case glslang::EbvSecondaryPositionNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 622 | if (!memberDeclaration) { |
| 623 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 624 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 625 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 626 | return spv::BuiltInSecondaryPositionNV; |
| 627 | case glslang::EbvSecondaryViewportMaskNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 628 | if (!memberDeclaration) { |
| 629 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 630 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 631 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 632 | return spv::BuiltInSecondaryViewportMaskNV; |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 633 | case glslang::EbvPositionPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 634 | if (!memberDeclaration) { |
| 635 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 636 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 637 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 638 | return spv::BuiltInPositionPerViewNV; |
| 639 | case glslang::EbvViewportMaskPerViewNV: |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 640 | if (!memberDeclaration) { |
| 641 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 642 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 643 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 644 | return spv::BuiltInViewportMaskPerViewNV; |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 645 | #endif |
Rex Xu | 3e783f9 | 2017-02-22 16:44:48 +0800 | [diff] [blame] | 646 | default: |
| 647 | return spv::BuiltInMax; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 651 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 652 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 653 | { |
| 654 | assert(type.getBasicType() == glslang::EbtSampler); |
| 655 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 656 | // Check for capabilities |
| 657 | switch (type.getQualifier().layoutFormat) { |
| 658 | case glslang::ElfRg32f: |
| 659 | case glslang::ElfRg16f: |
| 660 | case glslang::ElfR11fG11fB10f: |
| 661 | case glslang::ElfR16f: |
| 662 | case glslang::ElfRgba16: |
| 663 | case glslang::ElfRgb10A2: |
| 664 | case glslang::ElfRg16: |
| 665 | case glslang::ElfRg8: |
| 666 | case glslang::ElfR16: |
| 667 | case glslang::ElfR8: |
| 668 | case glslang::ElfRgba16Snorm: |
| 669 | case glslang::ElfRg16Snorm: |
| 670 | case glslang::ElfRg8Snorm: |
| 671 | case glslang::ElfR16Snorm: |
| 672 | case glslang::ElfR8Snorm: |
| 673 | |
| 674 | case glslang::ElfRg32i: |
| 675 | case glslang::ElfRg16i: |
| 676 | case glslang::ElfRg8i: |
| 677 | case glslang::ElfR16i: |
| 678 | case glslang::ElfR8i: |
| 679 | |
| 680 | case glslang::ElfRgb10a2ui: |
| 681 | case glslang::ElfRg32ui: |
| 682 | case glslang::ElfRg16ui: |
| 683 | case glslang::ElfRg8ui: |
| 684 | case glslang::ElfR16ui: |
| 685 | case glslang::ElfR8ui: |
| 686 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 687 | break; |
| 688 | |
| 689 | default: |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 694 | switch (type.getQualifier().layoutFormat) { |
| 695 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 696 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 697 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 698 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 699 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 700 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 701 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 702 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 703 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 704 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 705 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 706 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 707 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 708 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 709 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 710 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 711 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 712 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 713 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 714 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 715 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 716 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 717 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 718 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 719 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 720 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 721 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 722 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 723 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 724 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 725 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 726 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 727 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 728 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 729 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 730 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 731 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 732 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 733 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 734 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 735 | default: return spv::ImageFormatMax; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 739 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(glslang::TLoopControl loopControl) const |
| 740 | { |
| 741 | switch (loopControl) { |
| 742 | case glslang::ELoopControlNone: return spv::LoopControlMaskNone; |
| 743 | case glslang::ELoopControlUnroll: return spv::LoopControlUnrollMask; |
| 744 | case glslang::ELoopControlDontUnroll: return spv::LoopControlDontUnrollMask; |
| 745 | // TODO: DependencyInfinite |
| 746 | // TODO: DependencyLength |
| 747 | default: return spv::LoopControlMaskNone; |
| 748 | } |
| 749 | } |
| 750 | |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 751 | // Translate glslang type to SPIR-V storage class. |
| 752 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 753 | { |
| 754 | if (type.getQualifier().isPipeInput()) |
| 755 | return spv::StorageClassInput; |
| 756 | else if (type.getQualifier().isPipeOutput()) |
| 757 | return spv::StorageClassOutput; |
| 758 | else if (type.getBasicType() == glslang::EbtAtomicUint) |
| 759 | return spv::StorageClassAtomicCounter; |
| 760 | else if (type.containsOpaque()) |
| 761 | return spv::StorageClassUniformConstant; |
| 762 | else if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
| 763 | builder.addExtension(spv::E_SPV_KHR_storage_buffer_storage_class); |
| 764 | return spv::StorageClassStorageBuffer; |
| 765 | } else if (type.getQualifier().isUniformOrBuffer()) { |
| 766 | if (type.getQualifier().layoutPushConstant) |
| 767 | return spv::StorageClassPushConstant; |
| 768 | if (type.getBasicType() == glslang::EbtBlock) |
| 769 | return spv::StorageClassUniform; |
| 770 | else |
| 771 | return spv::StorageClassUniformConstant; |
| 772 | } else { |
| 773 | switch (type.getQualifier().storage) { |
| 774 | case glslang::EvqShared: return spv::StorageClassWorkgroup; break; |
| 775 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 776 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 777 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
| 778 | default: |
| 779 | assert(0); |
| 780 | return spv::StorageClassFunction; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 785 | // 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] | 786 | // descriptor set. |
| 787 | bool IsDescriptorResource(const glslang::TType& type) |
| 788 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 789 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 790 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 791 | return type.getQualifier().isUniformOrBuffer() && ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 792 | |
| 793 | // non block... |
| 794 | // basically samplerXXX/subpass/sampler/texture are all included |
| 795 | // if they are the global-scope-class, not the function parameter |
| 796 | // (or local, if they ever exist) class. |
| 797 | if (type.getBasicType() == glslang::EbtSampler) |
| 798 | return type.getQualifier().isUniformOrBuffer(); |
| 799 | |
| 800 | // None of the above. |
| 801 | return false; |
| 802 | } |
| 803 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 804 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 805 | { |
| 806 | if (child.layoutMatrix == glslang::ElmNone) |
| 807 | child.layoutMatrix = parent.layoutMatrix; |
| 808 | |
| 809 | if (parent.invariant) |
| 810 | child.invariant = true; |
| 811 | if (parent.nopersp) |
| 812 | child.nopersp = true; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 813 | #ifdef AMD_EXTENSIONS |
| 814 | if (parent.explicitInterp) |
| 815 | child.explicitInterp = true; |
| 816 | #endif |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 817 | if (parent.flat) |
| 818 | child.flat = true; |
| 819 | if (parent.centroid) |
| 820 | child.centroid = true; |
| 821 | if (parent.patch) |
| 822 | child.patch = true; |
| 823 | if (parent.sample) |
| 824 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 825 | if (parent.coherent) |
| 826 | child.coherent = true; |
| 827 | if (parent.volatil) |
| 828 | child.volatil = true; |
| 829 | if (parent.restrict) |
| 830 | child.restrict = true; |
| 831 | if (parent.readonly) |
| 832 | child.readonly = true; |
| 833 | if (parent.writeonly) |
| 834 | child.writeonly = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 835 | } |
| 836 | |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 837 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 838 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 839 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 840 | // - struct members might inherit from a struct declaration |
| 841 | // (note that non-block structs don't explicitly inherit, |
| 842 | // only implicitly, meaning no decoration involved) |
| 843 | // - affect decorations on the struct members |
| 844 | // (note smooth does not, and expecting something like volatile |
| 845 | // to effect the whole object) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 846 | // - 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] | 847 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 848 | } |
| 849 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 850 | // |
| 851 | // Implement the TGlslangToSpvTraverser class. |
| 852 | // |
| 853 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 854 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(const glslang::TIntermediate* glslangIntermediate, spv::SpvBuildLogger* buildLogger) |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 855 | : TIntermTraverser(true, false, true), shaderEntry(nullptr), currentFunction(nullptr), |
| 856 | sequenceDepth(0), logger(buildLogger), |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 857 | builder((glslang::GetKhronosToolId() << 16) | GeneratorVersion, logger), |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 858 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 859 | glslangIntermediate(glslangIntermediate) |
| 860 | { |
| 861 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 862 | |
| 863 | builder.clearAccessChain(); |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 864 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), glslangIntermediate->getVersion()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 865 | stdBuiltins = builder.import("GLSL.std.450"); |
| 866 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 867 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 868 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 869 | |
| 870 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 871 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 872 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 873 | builder.addSourceExtension(it->c_str()); |
| 874 | |
| 875 | // Add the top-level modes for this shader. |
| 876 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 877 | if (glslangIntermediate->getXfbMode()) { |
| 878 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 879 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 880 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 881 | |
| 882 | unsigned int mode; |
| 883 | switch (glslangIntermediate->getStage()) { |
| 884 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 885 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 886 | break; |
| 887 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 888 | case EShLangTessEvaluation: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 889 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 890 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 891 | |
steve-lunarg | e741249 | 2017-03-23 11:56:07 -0600 | [diff] [blame] | 892 | glslang::TLayoutGeometry primitive; |
| 893 | |
| 894 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 895 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 896 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 897 | } else { |
| 898 | primitive = glslangIntermediate->getInputPrimitive(); |
| 899 | } |
| 900 | |
| 901 | switch (primitive) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 902 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 903 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 904 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 905 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 906 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 907 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 908 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 909 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 910 | switch (glslangIntermediate->getVertexSpacing()) { |
| 911 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 912 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 913 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 914 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 915 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 916 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 917 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 918 | |
| 919 | switch (glslangIntermediate->getVertexOrder()) { |
| 920 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 921 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 922 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 923 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 924 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 925 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 926 | |
| 927 | if (glslangIntermediate->getPointMode()) |
| 928 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 929 | break; |
| 930 | |
| 931 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 932 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 933 | switch (glslangIntermediate->getInputPrimitive()) { |
| 934 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 935 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 936 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 937 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 938 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 939 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 940 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 941 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 942 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 943 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 944 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 945 | |
| 946 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 947 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 948 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 949 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 950 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 951 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 952 | if (mode != spv::ExecutionModeMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 953 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 954 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 955 | break; |
| 956 | |
| 957 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 958 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 959 | if (glslangIntermediate->getPixelCenterInteger()) |
| 960 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 961 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 962 | if (glslangIntermediate->getOriginUpperLeft()) |
| 963 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 964 | else |
| 965 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 966 | |
| 967 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 968 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 969 | |
| 970 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 971 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 972 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 973 | default: mode = spv::ExecutionModeMax; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 974 | } |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 975 | if (mode != spv::ExecutionModeMax) |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 976 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 977 | |
| 978 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 979 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 980 | break; |
| 981 | |
| 982 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 983 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 984 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 985 | glslangIntermediate->getLocalSize(1), |
| 986 | glslangIntermediate->getLocalSize(2)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 987 | break; |
| 988 | |
| 989 | default: |
| 990 | break; |
| 991 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 992 | } |
| 993 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 994 | // Finish creating SPV, after the traversal is complete. |
| 995 | void TGlslangToSpvTraverser::finishSpv() |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 996 | { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 997 | if (! entryPointTerminated) { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 998 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 999 | builder.leaveFunction(); |
| 1000 | } |
| 1001 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1002 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 1003 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1004 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1005 | |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 1006 | builder.eliminateDeadDecorations(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1009 | // Write the SPV into 'out'. |
| 1010 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1011 | { |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 1012 | builder.dump(out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | // |
| 1016 | // Implement the traversal functions. |
| 1017 | // |
| 1018 | // Return true from interior nodes to have the external traversal |
| 1019 | // continue on to children. Return false if children were |
| 1020 | // already processed. |
| 1021 | // |
| 1022 | |
| 1023 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1024 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1025 | // - uniform/input reads |
| 1026 | // - output writes |
| 1027 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1028 | // - something simple that degenerates into the last bullet |
| 1029 | // |
| 1030 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1031 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 1032 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1033 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 1034 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1035 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1036 | // getSymbolId() will set up all the IO decorations on the first call. |
| 1037 | // Formal function parameters were mapped during makeFunctions(). |
| 1038 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 1039 | |
| 1040 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1041 | if (builder.isPointer(id)) { |
| 1042 | spv::StorageClass sc = builder.getStorageClass(id); |
| 1043 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) |
| 1044 | iOSet.insert(id); |
| 1045 | } |
| 1046 | |
| 1047 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1048 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1049 | // Prepare to generate code for the access |
| 1050 | |
| 1051 | // L-value chains will be computed left to right. We're on the symbol now, |
| 1052 | // which is the left-most part of the access chain, so now is "clear" time, |
| 1053 | // followed by setting the base. |
| 1054 | builder.clearAccessChain(); |
| 1055 | |
| 1056 | // 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] | 1057 | // except for |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1058 | // A) R-Value arguments to a function, which are an intermediate object. |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1059 | // See comments in handleUserFunctionCall(). |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1060 | // 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] | 1061 | // These are also pure R-values. |
| 1062 | glslang::TQualifier qualifier = symbol->getQualifier(); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1063 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1064 | builder.setAccessChainRValue(id); |
| 1065 | else |
| 1066 | builder.setAccessChainLValue(id); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1071 | { |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1072 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1073 | if (node->getType().getQualifier().isSpecConstant()) |
| 1074 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1075 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1076 | // First, handle special cases |
| 1077 | switch (node->getOp()) { |
| 1078 | case glslang::EOpAssign: |
| 1079 | case glslang::EOpAddAssign: |
| 1080 | case glslang::EOpSubAssign: |
| 1081 | case glslang::EOpMulAssign: |
| 1082 | case glslang::EOpVectorTimesMatrixAssign: |
| 1083 | case glslang::EOpVectorTimesScalarAssign: |
| 1084 | case glslang::EOpMatrixTimesScalarAssign: |
| 1085 | case glslang::EOpMatrixTimesMatrixAssign: |
| 1086 | case glslang::EOpDivAssign: |
| 1087 | case glslang::EOpModAssign: |
| 1088 | case glslang::EOpAndAssign: |
| 1089 | case glslang::EOpInclusiveOrAssign: |
| 1090 | case glslang::EOpExclusiveOrAssign: |
| 1091 | case glslang::EOpLeftShiftAssign: |
| 1092 | case glslang::EOpRightShiftAssign: |
| 1093 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1094 | // where a is evaluated before b. For a simple assignment, GLSL |
| 1095 | // says to evaluate the left before the right. So, always, left |
| 1096 | // node then right node. |
| 1097 | { |
| 1098 | // get the left l-value, save it away |
| 1099 | builder.clearAccessChain(); |
| 1100 | node->getLeft()->traverse(this); |
| 1101 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1102 | |
| 1103 | // evaluate the right |
| 1104 | builder.clearAccessChain(); |
| 1105 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1106 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1107 | |
| 1108 | if (node->getOp() != glslang::EOpAssign) { |
| 1109 | // the left is also an r-value |
| 1110 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1111 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1112 | |
| 1113 | // do the operation |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1114 | rValue = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1115 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1116 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1117 | node->getType().getBasicType()); |
| 1118 | |
| 1119 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1120 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | // store the result |
| 1124 | builder.setAccessChain(lValue); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 1125 | multiTypeStore(node->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1126 | |
| 1127 | // assignments are expressions having an rValue after they are evaluated... |
| 1128 | builder.clearAccessChain(); |
| 1129 | builder.setAccessChainRValue(rValue); |
| 1130 | } |
| 1131 | return false; |
| 1132 | case glslang::EOpIndexDirect: |
| 1133 | case glslang::EOpIndexDirectStruct: |
| 1134 | { |
| 1135 | // Get the left part of the access chain. |
| 1136 | node->getLeft()->traverse(this); |
| 1137 | |
| 1138 | // Add the next element in the chain |
| 1139 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1140 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1141 | if (! node->getLeft()->getType().isArray() && |
| 1142 | node->getLeft()->getType().isVector() && |
| 1143 | node->getOp() == glslang::EOpIndexDirect) { |
| 1144 | // This is essentially a hard-coded vector swizzle of size 1, |
| 1145 | // so short circuit the access-chain stuff with a swizzle. |
| 1146 | std::vector<unsigned> swizzle; |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1147 | swizzle.push_back(glslangIndex); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1148 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1149 | } else { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1150 | int spvIndex = glslangIndex; |
| 1151 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1152 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 1153 | { |
| 1154 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 1155 | // index remapping due to hidden members in anonymous blocks. |
| 1156 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
| 1157 | assert(remapper.size() > 0); |
| 1158 | spvIndex = remapper[glslangIndex]; |
| 1159 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1160 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1161 | // normal case for indexing array or structure or block |
| 1162 | builder.accessChainPush(builder.makeIntConstant(spvIndex)); |
| 1163 | |
| 1164 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 1165 | // We have deferred generation of associated capabilities until now. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1166 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 1167 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | return false; |
| 1171 | case glslang::EOpIndexIndirect: |
| 1172 | { |
| 1173 | // Structure or array or vector indirection. |
| 1174 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 1175 | // matrices are arrays of vectors, so will also work for a matrix. |
| 1176 | // Will use the access chain's 'component' for variable index into a vector. |
| 1177 | |
| 1178 | // This adapter is building access chains left to right. |
| 1179 | // Set up the access chain to the left. |
| 1180 | node->getLeft()->traverse(this); |
| 1181 | |
| 1182 | // save it so that computing the right side doesn't trash it |
| 1183 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1184 | |
| 1185 | // compute the next index in the chain |
| 1186 | builder.clearAccessChain(); |
| 1187 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1188 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1189 | |
| 1190 | // restore the saved access chain |
| 1191 | builder.setAccessChain(partial); |
| 1192 | |
| 1193 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1194 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1195 | else |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1196 | builder.accessChainPush(index); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1197 | } |
| 1198 | return false; |
| 1199 | case glslang::EOpVectorSwizzle: |
| 1200 | { |
| 1201 | node->getLeft()->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1202 | std::vector<unsigned> swizzle; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1203 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1204 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1205 | } |
| 1206 | return false; |
John Kessenich | fdf6347 | 2017-01-13 12:27:52 -0700 | [diff] [blame] | 1207 | case glslang::EOpMatrixSwizzle: |
| 1208 | logger->missingFunctionality("matrix swizzle"); |
| 1209 | return true; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 1210 | case glslang::EOpLogicalOr: |
| 1211 | case glslang::EOpLogicalAnd: |
| 1212 | { |
| 1213 | |
| 1214 | // These may require short circuiting, but can sometimes be done as straight |
| 1215 | // binary operations. The right operand must be short circuited if it has |
| 1216 | // side effects, and should probably be if it is complex. |
| 1217 | if (isTrivial(node->getRight()->getAsTyped())) |
| 1218 | break; // handle below as a normal binary operation |
| 1219 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 1220 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 1221 | builder.clearAccessChain(); |
| 1222 | builder.setAccessChainRValue(result); |
| 1223 | } |
| 1224 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1225 | default: |
| 1226 | break; |
| 1227 | } |
| 1228 | |
| 1229 | // Assume generic binary op... |
| 1230 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1231 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1232 | builder.clearAccessChain(); |
| 1233 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1234 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1235 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1236 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1237 | builder.clearAccessChain(); |
| 1238 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1239 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1240 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1241 | // get result |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1242 | spv::Id result = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getOperationPrecision()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1243 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1244 | convertGlslangToSpvType(node->getType()), left, right, |
| 1245 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1246 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1247 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1248 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1249 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1250 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1251 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1252 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1253 | return false; |
| 1254 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1258 | { |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1259 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1260 | if (node->getType().getQualifier().isSpecConstant()) |
| 1261 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1262 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1263 | spv::Id result = spv::NoResult; |
| 1264 | |
| 1265 | // try texturing first |
| 1266 | result = createImageTextureFunctionCall(node); |
| 1267 | if (result != spv::NoResult) { |
| 1268 | builder.clearAccessChain(); |
| 1269 | builder.setAccessChainRValue(result); |
| 1270 | |
| 1271 | return false; // done with this node |
| 1272 | } |
| 1273 | |
| 1274 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1275 | |
| 1276 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1277 | // Quite special; won't want to evaluate the operand. |
| 1278 | |
| 1279 | // Normal .length() would have been constant folded by the front-end. |
| 1280 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1281 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1282 | assert(node->getOperand()->getType().isRuntimeSizedArray()); |
| 1283 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1284 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1285 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1286 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1287 | |
| 1288 | builder.clearAccessChain(); |
| 1289 | builder.setAccessChainRValue(length); |
| 1290 | |
| 1291 | return false; |
| 1292 | } |
| 1293 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1294 | // Start by evaluating the operand |
| 1295 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1296 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1297 | // operate first on the swizzle base, then apply the swizzle. |
| 1298 | spv::Id invertedType = spv::NoType; |
| 1299 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? invertedType : convertGlslangToSpvType(node->getType()); }; |
| 1300 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 1301 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 1302 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1303 | builder.clearAccessChain(); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1304 | if (invertedType != spv::NoType) |
| 1305 | node->getOperand()->getAsBinaryNode()->getLeft()->traverse(this); |
| 1306 | else |
| 1307 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1308 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1309 | spv::Id operand = spv::NoResult; |
| 1310 | |
| 1311 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1312 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1313 | node->getOp() == glslang::EOpAtomicCounter || |
| 1314 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1315 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1316 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1317 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1318 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1319 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1320 | spv::Decoration noContraction = TranslateNoContractionDecoration(node->getType().getQualifier()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1321 | |
| 1322 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1323 | if (! result) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1324 | result = createConversion(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1325 | |
| 1326 | // if not, then possibly an operation |
| 1327 | if (! result) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1328 | result = createUnaryOperation(node->getOp(), precision, noContraction, resultType(), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1329 | |
| 1330 | if (result) { |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1331 | if (invertedType) |
| 1332 | result = createInvertedSwizzle(precision, *node->getOperand(), result); |
| 1333 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1334 | builder.clearAccessChain(); |
| 1335 | builder.setAccessChainRValue(result); |
| 1336 | |
| 1337 | return false; // done with this node |
| 1338 | } |
| 1339 | |
| 1340 | // it must be a special case, check... |
| 1341 | switch (node->getOp()) { |
| 1342 | case glslang::EOpPostIncrement: |
| 1343 | case glslang::EOpPostDecrement: |
| 1344 | case glslang::EOpPreIncrement: |
| 1345 | case glslang::EOpPreDecrement: |
| 1346 | { |
| 1347 | // 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] | 1348 | spv::Id one = 0; |
| 1349 | if (node->getBasicType() == glslang::EbtFloat) |
| 1350 | one = builder.makeFloatConstant(1.0F); |
Rex Xu | ce31aea | 2016-07-29 16:13:04 +0800 | [diff] [blame] | 1351 | else if (node->getBasicType() == glslang::EbtDouble) |
| 1352 | one = builder.makeDoubleConstant(1.0); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1353 | #ifdef AMD_EXTENSIONS |
| 1354 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 1355 | one = builder.makeFloat16Constant(1.0F); |
| 1356 | #endif |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1357 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1358 | one = builder.makeInt64Constant(1); |
| 1359 | else |
| 1360 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1361 | glslang::TOperator op; |
| 1362 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1363 | node->getOp() == glslang::EOpPostIncrement) |
| 1364 | op = glslang::EOpAdd; |
| 1365 | else |
| 1366 | op = glslang::EOpSub; |
| 1367 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1368 | spv::Id result = createBinaryOperation(op, precision, |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1369 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1370 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1371 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1372 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1373 | |
| 1374 | // The result of operation is always stored, but conditionally the |
| 1375 | // consumed result. The consumed result is always an r-value. |
| 1376 | builder.accessChainStore(result); |
| 1377 | builder.clearAccessChain(); |
| 1378 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1379 | node->getOp() == glslang::EOpPreDecrement) |
| 1380 | builder.setAccessChainRValue(result); |
| 1381 | else |
| 1382 | builder.setAccessChainRValue(operand); |
| 1383 | } |
| 1384 | |
| 1385 | return false; |
| 1386 | |
| 1387 | case glslang::EOpEmitStreamVertex: |
| 1388 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1389 | return false; |
| 1390 | case glslang::EOpEndStreamPrimitive: |
| 1391 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1392 | return false; |
| 1393 | |
| 1394 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1395 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1396 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1397 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 1401 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 1402 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1403 | if (node->getType().getQualifier().isSpecConstant()) |
| 1404 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1405 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1406 | spv::Id result = spv::NoResult; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1407 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 1408 | 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] | 1409 | |
| 1410 | // try texturing |
| 1411 | result = createImageTextureFunctionCall(node); |
| 1412 | if (result != spv::NoResult) { |
| 1413 | builder.clearAccessChain(); |
| 1414 | builder.setAccessChainRValue(result); |
| 1415 | |
| 1416 | return false; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 1417 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1418 | // "imageStore" is a special case, which has no result |
| 1419 | return false; |
| 1420 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1421 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1422 | glslang::TOperator binOp = glslang::EOpNull; |
| 1423 | bool reduceComparison = true; |
| 1424 | bool isMatrix = false; |
| 1425 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1426 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1427 | |
| 1428 | assert(node->getOp()); |
| 1429 | |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 1430 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1431 | |
| 1432 | switch (node->getOp()) { |
| 1433 | case glslang::EOpSequence: |
| 1434 | { |
| 1435 | if (preVisit) |
| 1436 | ++sequenceDepth; |
| 1437 | else |
| 1438 | --sequenceDepth; |
| 1439 | |
| 1440 | if (sequenceDepth == 1) { |
| 1441 | // If this is the parent node of all the functions, we want to see them |
| 1442 | // early, so all call points have actual SPIR-V functions to reference. |
| 1443 | // In all cases, still let the traverser visit the children for us. |
| 1444 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 1445 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1446 | // 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] | 1447 | // anything else gets there, so visit out of order, doing them all now. |
| 1448 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 1449 | |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 1450 | // 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] | 1451 | // so do them manually. |
| 1452 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 1453 | |
| 1454 | return false; |
| 1455 | } |
| 1456 | |
| 1457 | return true; |
| 1458 | } |
| 1459 | case glslang::EOpLinkerObjects: |
| 1460 | { |
| 1461 | if (visit == glslang::EvPreVisit) |
| 1462 | linkageOnly = true; |
| 1463 | else |
| 1464 | linkageOnly = false; |
| 1465 | |
| 1466 | return true; |
| 1467 | } |
| 1468 | case glslang::EOpComma: |
| 1469 | { |
| 1470 | // processing from left to right naturally leaves the right-most |
| 1471 | // lying around in the access chain |
| 1472 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1473 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 1474 | glslangOperands[i]->traverse(this); |
| 1475 | |
| 1476 | return false; |
| 1477 | } |
| 1478 | case glslang::EOpFunction: |
| 1479 | if (visit == glslang::EvPreVisit) { |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 1480 | if (isShaderEntryPoint(node)) { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1481 | inEntryPoint = true; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1482 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 1483 | currentFunction = shaderEntry; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1484 | } else { |
| 1485 | handleFunctionEntry(node); |
| 1486 | } |
| 1487 | } else { |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1488 | if (inEntryPoint) |
| 1489 | entryPointTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 1490 | builder.leaveFunction(); |
John Kessenich | 517fe7a | 2016-11-26 13:31:47 -0700 | [diff] [blame] | 1491 | inEntryPoint = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | return true; |
| 1495 | case glslang::EOpParameters: |
| 1496 | // Parameters will have been consumed by EOpFunction processing, but not |
| 1497 | // the body, so we still visited the function node's children, making this |
| 1498 | // child redundant. |
| 1499 | return false; |
| 1500 | case glslang::EOpFunctionCall: |
| 1501 | { |
| 1502 | if (node->isUserDefined()) |
| 1503 | result = handleUserFunctionCall(node); |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 1504 | // 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] | 1505 | if (result) { |
| 1506 | builder.clearAccessChain(); |
| 1507 | builder.setAccessChainRValue(result); |
| 1508 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1509 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1510 | |
| 1511 | return false; |
| 1512 | } |
| 1513 | case glslang::EOpConstructMat2x2: |
| 1514 | case glslang::EOpConstructMat2x3: |
| 1515 | case glslang::EOpConstructMat2x4: |
| 1516 | case glslang::EOpConstructMat3x2: |
| 1517 | case glslang::EOpConstructMat3x3: |
| 1518 | case glslang::EOpConstructMat3x4: |
| 1519 | case glslang::EOpConstructMat4x2: |
| 1520 | case glslang::EOpConstructMat4x3: |
| 1521 | case glslang::EOpConstructMat4x4: |
| 1522 | case glslang::EOpConstructDMat2x2: |
| 1523 | case glslang::EOpConstructDMat2x3: |
| 1524 | case glslang::EOpConstructDMat2x4: |
| 1525 | case glslang::EOpConstructDMat3x2: |
| 1526 | case glslang::EOpConstructDMat3x3: |
| 1527 | case glslang::EOpConstructDMat3x4: |
| 1528 | case glslang::EOpConstructDMat4x2: |
| 1529 | case glslang::EOpConstructDMat4x3: |
| 1530 | case glslang::EOpConstructDMat4x4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1531 | #ifdef AMD_EXTENSIONS |
| 1532 | case glslang::EOpConstructF16Mat2x2: |
| 1533 | case glslang::EOpConstructF16Mat2x3: |
| 1534 | case glslang::EOpConstructF16Mat2x4: |
| 1535 | case glslang::EOpConstructF16Mat3x2: |
| 1536 | case glslang::EOpConstructF16Mat3x3: |
| 1537 | case glslang::EOpConstructF16Mat3x4: |
| 1538 | case glslang::EOpConstructF16Mat4x2: |
| 1539 | case glslang::EOpConstructF16Mat4x3: |
| 1540 | case glslang::EOpConstructF16Mat4x4: |
| 1541 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1542 | isMatrix = true; |
| 1543 | // fall through |
| 1544 | case glslang::EOpConstructFloat: |
| 1545 | case glslang::EOpConstructVec2: |
| 1546 | case glslang::EOpConstructVec3: |
| 1547 | case glslang::EOpConstructVec4: |
| 1548 | case glslang::EOpConstructDouble: |
| 1549 | case glslang::EOpConstructDVec2: |
| 1550 | case glslang::EOpConstructDVec3: |
| 1551 | case glslang::EOpConstructDVec4: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 1552 | #ifdef AMD_EXTENSIONS |
| 1553 | case glslang::EOpConstructFloat16: |
| 1554 | case glslang::EOpConstructF16Vec2: |
| 1555 | case glslang::EOpConstructF16Vec3: |
| 1556 | case glslang::EOpConstructF16Vec4: |
| 1557 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1558 | case glslang::EOpConstructBool: |
| 1559 | case glslang::EOpConstructBVec2: |
| 1560 | case glslang::EOpConstructBVec3: |
| 1561 | case glslang::EOpConstructBVec4: |
| 1562 | case glslang::EOpConstructInt: |
| 1563 | case glslang::EOpConstructIVec2: |
| 1564 | case glslang::EOpConstructIVec3: |
| 1565 | case glslang::EOpConstructIVec4: |
| 1566 | case glslang::EOpConstructUint: |
| 1567 | case glslang::EOpConstructUVec2: |
| 1568 | case glslang::EOpConstructUVec3: |
| 1569 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1570 | case glslang::EOpConstructInt64: |
| 1571 | case glslang::EOpConstructI64Vec2: |
| 1572 | case glslang::EOpConstructI64Vec3: |
| 1573 | case glslang::EOpConstructI64Vec4: |
| 1574 | case glslang::EOpConstructUint64: |
| 1575 | case glslang::EOpConstructU64Vec2: |
| 1576 | case glslang::EOpConstructU64Vec3: |
| 1577 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1578 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1579 | case glslang::EOpConstructTextureSampler: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1580 | { |
| 1581 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1582 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1583 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1584 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1585 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1586 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1587 | std::vector<spv::Id> constituents; |
| 1588 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 1589 | constituents.push_back(arguments[c]); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1590 | constructed = builder.createCompositeConstruct(resultType(), constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1591 | } else if (isMatrix) |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1592 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1593 | else |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1594 | constructed = builder.createConstructor(precision, arguments, resultType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1595 | |
| 1596 | builder.clearAccessChain(); |
| 1597 | builder.setAccessChainRValue(constructed); |
| 1598 | |
| 1599 | return false; |
| 1600 | } |
| 1601 | |
| 1602 | // These six are component-wise compares with component-wise results. |
| 1603 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 1604 | case glslang::EOpLessThan: |
| 1605 | case glslang::EOpGreaterThan: |
| 1606 | case glslang::EOpLessThanEqual: |
| 1607 | case glslang::EOpGreaterThanEqual: |
| 1608 | case glslang::EOpVectorEqual: |
| 1609 | case glslang::EOpVectorNotEqual: |
| 1610 | { |
| 1611 | // Map the operation to a binary |
| 1612 | binOp = node->getOp(); |
| 1613 | reduceComparison = false; |
| 1614 | switch (node->getOp()) { |
| 1615 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 1616 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 1617 | default: binOp = node->getOp(); break; |
| 1618 | } |
| 1619 | |
| 1620 | break; |
| 1621 | } |
| 1622 | case glslang::EOpMul: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1623 | // component-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1624 | binOp = glslang::EOpMul; |
| 1625 | break; |
| 1626 | case glslang::EOpOuterProduct: |
| 1627 | // two vectors multiplied to make a matrix |
| 1628 | binOp = glslang::EOpOuterProduct; |
| 1629 | break; |
| 1630 | case glslang::EOpDot: |
| 1631 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1632 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1633 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 1634 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1635 | binOp = glslang::EOpMul; |
| 1636 | break; |
| 1637 | } |
| 1638 | case glslang::EOpMod: |
| 1639 | // when an aggregate, this is the floating-point mod built-in function, |
| 1640 | // which can be emitted by the one in createBinaryOperation() |
| 1641 | binOp = glslang::EOpMod; |
| 1642 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1643 | case glslang::EOpEmitVertex: |
| 1644 | case glslang::EOpEndPrimitive: |
| 1645 | case glslang::EOpBarrier: |
| 1646 | case glslang::EOpMemoryBarrier: |
| 1647 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 1648 | case glslang::EOpMemoryBarrierBuffer: |
| 1649 | case glslang::EOpMemoryBarrierImage: |
| 1650 | case glslang::EOpMemoryBarrierShared: |
| 1651 | case glslang::EOpGroupMemoryBarrier: |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 1652 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 1653 | case glslang::EOpGroupMemoryBarrierWithGroupSync: |
| 1654 | case glslang::EOpWorkgroupMemoryBarrier: |
| 1655 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1656 | noReturnValue = true; |
| 1657 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 1658 | break; |
| 1659 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1660 | case glslang::EOpAtomicAdd: |
| 1661 | case glslang::EOpAtomicMin: |
| 1662 | case glslang::EOpAtomicMax: |
| 1663 | case glslang::EOpAtomicAnd: |
| 1664 | case glslang::EOpAtomicOr: |
| 1665 | case glslang::EOpAtomicXor: |
| 1666 | case glslang::EOpAtomicExchange: |
| 1667 | case glslang::EOpAtomicCompSwap: |
| 1668 | atomic = true; |
| 1669 | break; |
| 1670 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1671 | default: |
| 1672 | break; |
| 1673 | } |
| 1674 | |
| 1675 | // |
| 1676 | // See if it maps to a regular operation. |
| 1677 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1678 | if (binOp != glslang::EOpNull) { |
| 1679 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 1680 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 1681 | assert(left && right); |
| 1682 | |
| 1683 | builder.clearAccessChain(); |
| 1684 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1685 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1686 | |
| 1687 | builder.clearAccessChain(); |
| 1688 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1689 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1690 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1691 | result = createBinaryOperation(binOp, precision, TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1692 | resultType(), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1693 | left->getType().getBasicType(), reduceComparison); |
| 1694 | |
| 1695 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1696 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1697 | builder.clearAccessChain(); |
| 1698 | builder.setAccessChainRValue(result); |
| 1699 | |
| 1700 | return false; |
| 1701 | } |
| 1702 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1703 | // |
| 1704 | // Create the list of operands. |
| 1705 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1706 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1707 | std::vector<spv::Id> operands; |
| 1708 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1709 | // special case l-value operands; there are just a few |
| 1710 | bool lvalue = false; |
| 1711 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1712 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1713 | case glslang::EOpModf: |
| 1714 | if (arg == 1) |
| 1715 | lvalue = true; |
| 1716 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1717 | case glslang::EOpInterpolateAtSample: |
| 1718 | case glslang::EOpInterpolateAtOffset: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 1719 | #ifdef AMD_EXTENSIONS |
| 1720 | case glslang::EOpInterpolateAtVertex: |
| 1721 | #endif |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1722 | if (arg == 0) { |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1723 | lvalue = true; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1724 | |
| 1725 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 1726 | // operate first on the swizzle base, then apply the swizzle. |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 1727 | if (glslangOperands[0]->getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1728 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 1729 | invertedType = convertGlslangToSpvType(glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 1730 | } |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1731 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 1732 | case glslang::EOpAtomicAdd: |
| 1733 | case glslang::EOpAtomicMin: |
| 1734 | case glslang::EOpAtomicMax: |
| 1735 | case glslang::EOpAtomicAnd: |
| 1736 | case glslang::EOpAtomicOr: |
| 1737 | case glslang::EOpAtomicXor: |
| 1738 | case glslang::EOpAtomicExchange: |
| 1739 | case glslang::EOpAtomicCompSwap: |
| 1740 | if (arg == 0) |
| 1741 | lvalue = true; |
| 1742 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1743 | case glslang::EOpAddCarry: |
| 1744 | case glslang::EOpSubBorrow: |
| 1745 | if (arg == 2) |
| 1746 | lvalue = true; |
| 1747 | break; |
| 1748 | case glslang::EOpUMulExtended: |
| 1749 | case glslang::EOpIMulExtended: |
| 1750 | if (arg >= 2) |
| 1751 | lvalue = true; |
| 1752 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1753 | default: |
| 1754 | break; |
| 1755 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1756 | builder.clearAccessChain(); |
| 1757 | if (invertedType != spv::NoType && arg == 0) |
| 1758 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 1759 | else |
| 1760 | glslangOperands[arg]->traverse(this); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1761 | if (lvalue) |
| 1762 | operands.push_back(builder.accessChainGetLValue()); |
| 1763 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1764 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1765 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1766 | |
| 1767 | if (atomic) { |
| 1768 | // Handle all atomics |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1769 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1770 | } else { |
| 1771 | // Pass through to generic operations. |
| 1772 | switch (glslangOperands.size()) { |
| 1773 | case 0: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1774 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1775 | break; |
| 1776 | case 1: |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1777 | result = createUnaryOperation( |
| 1778 | node->getOp(), precision, |
| 1779 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1780 | resultType(), operands.front(), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1781 | glslangOperands[0]->getAsTyped()->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1782 | break; |
| 1783 | default: |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1784 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1785 | break; |
| 1786 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 1787 | if (invertedType) |
| 1788 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | if (noReturnValue) |
| 1792 | return false; |
| 1793 | |
| 1794 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1795 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1796 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1797 | } else { |
| 1798 | builder.clearAccessChain(); |
| 1799 | builder.setAccessChainRValue(result); |
| 1800 | return false; |
| 1801 | } |
| 1802 | } |
| 1803 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1804 | // This path handles both if-then-else and ?: |
| 1805 | // The if-then-else has a node type of void, while |
| 1806 | // ?: has either a void or a non-void node type |
| 1807 | // |
| 1808 | // Leaving the result, when not void: |
| 1809 | // GLSL only has r-values as the result of a :?, but |
| 1810 | // if we have an l-value, that can be more efficient if it will |
| 1811 | // become the base of a complex r-value expression, because the |
| 1812 | // 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] | 1813 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 1814 | { |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1815 | // See if it simple and safe to generate OpSelect instead of using control flow. |
| 1816 | // Crucially, side effects must be avoided, and there are performance trade-offs. |
| 1817 | // Return true if good idea (and safe) for OpSelect, false otherwise. |
| 1818 | const auto selectPolicy = [&]() -> bool { |
John Kessenich | 0479437 | 2017-03-01 13:49:11 -0700 | [diff] [blame] | 1819 | if ((!node->getType().isScalar() && !node->getType().isVector()) || |
| 1820 | node->getBasicType() == glslang::EbtVoid) |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1821 | return false; |
| 1822 | |
| 1823 | if (node->getTrueBlock() == nullptr || |
| 1824 | node->getFalseBlock() == nullptr) |
| 1825 | return false; |
| 1826 | |
| 1827 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 1828 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 1829 | |
| 1830 | // return true if a single operand to ? : is okay for OpSelect |
| 1831 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 1832 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1833 | }; |
| 1834 | |
| 1835 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 1836 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 1837 | }; |
| 1838 | |
| 1839 | // Emit OpSelect for this selection. |
| 1840 | const auto handleAsOpSelect = [&]() { |
| 1841 | node->getCondition()->traverse(this); |
| 1842 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 1843 | node->getTrueBlock()->traverse(this); |
| 1844 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 1845 | node->getFalseBlock()->traverse(this); |
| 1846 | spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 1847 | |
John Kessenich | e434ad9 | 2017-03-30 10:09:28 -0600 | [diff] [blame] | 1848 | // smear condition to vector, if necessary (AST is always scalar) |
| 1849 | if (builder.isVector(trueValue)) |
| 1850 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 1851 | builder.makeVectorType(builder.makeBoolType(), |
| 1852 | builder.getNumComponents(trueValue))); |
| 1853 | |
| 1854 | spv::Id select = builder.createTriOp(spv::OpSelect, |
| 1855 | convertGlslangToSpvType(node->getType()), condition, |
| 1856 | trueValue, falseValue); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1857 | builder.clearAccessChain(); |
| 1858 | builder.setAccessChainRValue(select); |
| 1859 | }; |
| 1860 | |
| 1861 | // Try for OpSelect |
| 1862 | |
| 1863 | if (selectPolicy()) { |
John Kessenich | 8e6c6ce | 2017-01-28 19:29:42 -0700 | [diff] [blame] | 1864 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1865 | if (node->getType().getQualifier().isSpecConstant()) |
| 1866 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1867 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1868 | handleAsOpSelect(); |
| 1869 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1870 | } |
| 1871 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1872 | // Instead, emit control flow... |
| 1873 | |
| 1874 | // Don't handle results as temporaries, because there will be two names |
| 1875 | // and better to leave SSA to later passes. |
| 1876 | spv::Id result = (node->getBasicType() == glslang::EbtVoid) |
| 1877 | ? spv::NoResult |
| 1878 | : builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 1879 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1880 | // emit the condition before doing anything with selection |
| 1881 | node->getCondition()->traverse(this); |
| 1882 | |
| 1883 | // make an "if" based on the value created by the condition |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1884 | spv::Builder::If ifBuilder(accessChainLoad(node->getCondition()->getType()), builder); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1885 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1886 | // emit the "then" statement |
| 1887 | if (node->getTrueBlock() != nullptr) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1888 | node->getTrueBlock()->traverse(this); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1889 | if (result != spv::NoResult) |
| 1890 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1891 | } |
| 1892 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1893 | if (node->getFalseBlock() != nullptr) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1894 | ifBuilder.makeBeginElse(); |
| 1895 | // emit the "else" statement |
| 1896 | node->getFalseBlock()->traverse(this); |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1897 | if (result != spv::NoResult) |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1898 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1899 | } |
| 1900 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1901 | // finish off the control flow |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1902 | ifBuilder.makeEndIf(); |
| 1903 | |
John Kessenich | 433e9ff | 2017-01-26 20:31:11 -0700 | [diff] [blame] | 1904 | if (result != spv::NoResult) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1905 | // GLSL only has r-values as the result of a :?, but |
| 1906 | // if we have an l-value, that can be more efficient if it will |
| 1907 | // become the base of a complex r-value expression, because the |
| 1908 | // next layer copies r-values into memory to use the access-chain mechanism |
| 1909 | builder.clearAccessChain(); |
| 1910 | builder.setAccessChainLValue(result); |
| 1911 | } |
| 1912 | |
| 1913 | return false; |
| 1914 | } |
| 1915 | |
| 1916 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 1917 | { |
| 1918 | // emit and get the condition before doing anything with switch |
| 1919 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1920 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1921 | |
| 1922 | // browse the children to sort out code segments |
| 1923 | int defaultSegment = -1; |
| 1924 | std::vector<TIntermNode*> codeSegments; |
| 1925 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 1926 | std::vector<int> caseValues; |
| 1927 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 1928 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 1929 | TIntermNode* child = *c; |
| 1930 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 1931 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1932 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 1933 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1934 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 1935 | } else |
| 1936 | codeSegments.push_back(child); |
| 1937 | } |
| 1938 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1939 | // 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] | 1940 | // statements between the last case and the end of the switch statement |
| 1941 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 1942 | (int)codeSegments.size() == defaultSegment) |
| 1943 | codeSegments.push_back(nullptr); |
| 1944 | |
| 1945 | // make the switch statement |
| 1946 | std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 1947 | builder.makeSwitch(selector, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1948 | |
| 1949 | // emit all the code in the segments |
| 1950 | breakForLoop.push(false); |
| 1951 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 1952 | builder.nextSwitchSegment(segmentBlocks, s); |
| 1953 | if (codeSegments[s]) |
| 1954 | codeSegments[s]->traverse(this); |
| 1955 | else |
| 1956 | builder.addSwitchBreak(); |
| 1957 | } |
| 1958 | breakForLoop.pop(); |
| 1959 | |
| 1960 | builder.endSwitch(segmentBlocks); |
| 1961 | |
| 1962 | return false; |
| 1963 | } |
| 1964 | |
| 1965 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 1966 | { |
| 1967 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 1968 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1969 | |
| 1970 | builder.clearAccessChain(); |
| 1971 | builder.setAccessChainRValue(constant); |
| 1972 | } |
| 1973 | |
| 1974 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 1975 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1976 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 1977 | builder.createBranch(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1978 | |
| 1979 | // Loop control: |
| 1980 | const spv::LoopControlMask control = TranslateLoopControl(node->getLoopControl()); |
| 1981 | |
| 1982 | // TODO: dependency length |
| 1983 | |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 1984 | // Spec requires back edges to target header blocks, and every header block |
| 1985 | // must dominate its merge block. Make a header block first to ensure these |
| 1986 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 1987 | // by a block-ending branch. But we don't want to put any other body/test |
| 1988 | // instructions in it, since the body/test may have arbitrary instructions, |
| 1989 | // including merges of its own. |
| 1990 | builder.setBuildPoint(&blocks.head); |
steve-lunarg | f1709e7 | 2017-05-02 20:14:50 -0600 | [diff] [blame] | 1991 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1992 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 1993 | spv::Block& test = builder.makeNewBlock(); |
| 1994 | builder.createBranch(&test); |
| 1995 | |
| 1996 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1997 | node->getTest()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1998 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1999 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2000 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 2001 | |
| 2002 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2003 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2004 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2005 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2006 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2007 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2008 | |
| 2009 | builder.setBuildPoint(&blocks.continue_target); |
| 2010 | if (node->getTerminal()) |
| 2011 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2012 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 2013 | } else { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2014 | builder.createBranch(&blocks.body); |
| 2015 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2016 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2017 | builder.setBuildPoint(&blocks.body); |
| 2018 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 2019 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2020 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2021 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2022 | |
| 2023 | builder.setBuildPoint(&blocks.continue_target); |
| 2024 | if (node->getTerminal()) |
| 2025 | node->getTerminal()->traverse(this); |
| 2026 | if (node->getTest()) { |
| 2027 | node->getTest()->traverse(this); |
| 2028 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2029 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2030 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2031 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 2032 | // TODO: unless there was a break/return/discard instruction |
| 2033 | // somewhere in the body, this is an infinite loop, so we should |
| 2034 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 2035 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2036 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2037 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 2038 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 2039 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2040 | return false; |
| 2041 | } |
| 2042 | |
| 2043 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 2044 | { |
| 2045 | if (node->getExpression()) |
| 2046 | node->getExpression()->traverse(this); |
| 2047 | |
| 2048 | switch (node->getFlowOp()) { |
| 2049 | case glslang::EOpKill: |
| 2050 | builder.makeDiscard(); |
| 2051 | break; |
| 2052 | case glslang::EOpBreak: |
| 2053 | if (breakForLoop.top()) |
| 2054 | builder.createLoopExit(); |
| 2055 | else |
| 2056 | builder.addSwitchBreak(); |
| 2057 | break; |
| 2058 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2059 | builder.createLoopContinue(); |
| 2060 | break; |
| 2061 | case glslang::EOpReturn: |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2062 | if (node->getExpression()) { |
| 2063 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 2064 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 2065 | if (builder.getTypeId(returnId) != currentFunction->getReturnType()) { |
| 2066 | builder.clearAccessChain(); |
| 2067 | spv::Id copyId = builder.createVariable(spv::StorageClassFunction, currentFunction->getReturnType()); |
| 2068 | builder.setAccessChainLValue(copyId); |
| 2069 | multiTypeStore(glslangReturnType, returnId); |
| 2070 | returnId = builder.createLoad(copyId); |
| 2071 | } |
| 2072 | builder.makeReturn(false, returnId); |
| 2073 | } else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 2074 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2075 | |
| 2076 | builder.clearAccessChain(); |
| 2077 | break; |
| 2078 | |
| 2079 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2080 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2081 | break; |
| 2082 | } |
| 2083 | |
| 2084 | return false; |
| 2085 | } |
| 2086 | |
| 2087 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 2088 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2089 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2090 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2091 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 2092 | if (node->getQualifier().isConstant()) { |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 2093 | return createSpvConstant(*node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | // Now, handle actual variables |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2097 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2098 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 2099 | |
Rex Xu | f89ad98 | 2017-04-07 23:22:33 +0800 | [diff] [blame] | 2100 | #ifdef AMD_EXTENSIONS |
| 2101 | const bool contains16BitType = node->getType().containsBasicType(glslang::EbtFloat16); |
| 2102 | if (contains16BitType) { |
| 2103 | if (storageClass == spv::StorageClassInput || storageClass == spv::StorageClassOutput) { |
| 2104 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2105 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
| 2106 | } else if (storageClass == spv::StorageClassPushConstant) { |
| 2107 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2108 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 2109 | } else if (storageClass == spv::StorageClassUniform) { |
| 2110 | builder.addExtension(spv::E_SPV_KHR_16bit_storage); |
| 2111 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 2112 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 2113 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 2114 | } |
| 2115 | } |
| 2116 | #endif |
| 2117 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2118 | const char* name = node->getName().c_str(); |
| 2119 | if (glslang::IsAnonymous(name)) |
| 2120 | name = ""; |
| 2121 | |
| 2122 | return builder.createVariable(storageClass, spvType, name); |
| 2123 | } |
| 2124 | |
| 2125 | // Return type Id of the sampled type. |
| 2126 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 2127 | { |
| 2128 | switch (sampler.type) { |
| 2129 | case glslang::EbtFloat: return builder.makeFloatType(32); |
| 2130 | case glslang::EbtInt: return builder.makeIntType(32); |
| 2131 | case glslang::EbtUint: return builder.makeUintType(32); |
| 2132 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2133 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2134 | return builder.makeFloatType(32); |
| 2135 | } |
| 2136 | } |
| 2137 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2138 | // If node is a swizzle operation, return the type that should be used if |
| 2139 | // the swizzle base is first consumed by another operation, before the swizzle |
| 2140 | // is applied. |
| 2141 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 2142 | { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 2143 | if (node.getAsOperator() && |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2144 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2145 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 2146 | else |
| 2147 | return spv::NoType; |
| 2148 | } |
| 2149 | |
| 2150 | // When inverting a swizzle with a parent op, this function |
| 2151 | // will apply the swizzle operation to a completed parent operation. |
| 2152 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, spv::Id parentResult) |
| 2153 | { |
| 2154 | std::vector<unsigned> swizzle; |
| 2155 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 2156 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 2157 | } |
| 2158 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 2159 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 2160 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 2161 | { |
| 2162 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 2163 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 2164 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 2165 | } |
| 2166 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2167 | // Convert from a glslang type to an SPV type, by calling into a |
| 2168 | // recursive version of this function. This establishes the inherited |
| 2169 | // layout state rooted from the top-level type. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2170 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type) |
| 2171 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2172 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier()); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | // 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] | 2176 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2177 | // Mutually recursive with convertGlslangStructToSpvType(). |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2178 | 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] | 2179 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2180 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2181 | |
| 2182 | switch (type.getBasicType()) { |
| 2183 | case glslang::EbtVoid: |
| 2184 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2185 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2186 | break; |
| 2187 | case glslang::EbtFloat: |
| 2188 | spvType = builder.makeFloatType(32); |
| 2189 | break; |
| 2190 | case glslang::EbtDouble: |
| 2191 | spvType = builder.makeFloatType(64); |
| 2192 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2193 | #ifdef AMD_EXTENSIONS |
| 2194 | case glslang::EbtFloat16: |
| 2195 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 2196 | spvType = builder.makeFloatType(16); |
| 2197 | break; |
| 2198 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2199 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2200 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 2201 | // a 32-bit int where non-0 means true. |
| 2202 | if (explicitLayout != glslang::ElpNone) |
| 2203 | spvType = builder.makeUintType(32); |
| 2204 | else |
| 2205 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2206 | break; |
| 2207 | case glslang::EbtInt: |
| 2208 | spvType = builder.makeIntType(32); |
| 2209 | break; |
| 2210 | case glslang::EbtUint: |
| 2211 | spvType = builder.makeUintType(32); |
| 2212 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2213 | case glslang::EbtInt64: |
| 2214 | builder.addCapability(spv::CapabilityInt64); |
| 2215 | spvType = builder.makeIntType(64); |
| 2216 | break; |
| 2217 | case glslang::EbtUint64: |
| 2218 | builder.addCapability(spv::CapabilityInt64); |
| 2219 | spvType = builder.makeUintType(64); |
| 2220 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2221 | case glslang::EbtAtomicUint: |
John Kessenich | 2d0cc78 | 2016-07-07 13:20:00 -0600 | [diff] [blame] | 2222 | builder.addCapability(spv::CapabilityAtomicStorage); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2223 | spvType = builder.makeUintType(32); |
| 2224 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2225 | case glslang::EbtSampler: |
| 2226 | { |
| 2227 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2228 | if (sampler.sampler) { |
| 2229 | // pure sampler |
| 2230 | spvType = builder.makeSamplerType(); |
| 2231 | } else { |
| 2232 | // an image is present, make its type |
| 2233 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 2234 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 2235 | if (sampler.combined) { |
| 2236 | // already has both image and sampler, make the combined type |
| 2237 | spvType = builder.makeSampledImageType(spvType); |
| 2238 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2239 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2240 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2241 | break; |
| 2242 | case glslang::EbtStruct: |
| 2243 | case glslang::EbtBlock: |
| 2244 | { |
| 2245 | // If we've seen this struct type, return it |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2246 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2247 | |
| 2248 | // Try to share structs for different layouts, but not yet for other |
| 2249 | // kinds of qualification (primarily not yet including interpolant qualification). |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2250 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2251 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 2252 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2253 | break; |
| 2254 | |
| 2255 | // else, we haven't seen it... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2256 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2257 | memberRemapper[glslangMembers].resize(glslangMembers->size()); |
| 2258 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2259 | } |
| 2260 | break; |
| 2261 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2262 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2263 | break; |
| 2264 | } |
| 2265 | |
| 2266 | if (type.isMatrix()) |
| 2267 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 2268 | else { |
| 2269 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 2270 | if (type.getVectorSize() > 1) |
| 2271 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 2272 | } |
| 2273 | |
| 2274 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2275 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 2276 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2277 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2278 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2279 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 2280 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2281 | // Use a dummy glslang type for querying internal strides of |
| 2282 | // arrays of arrays, but using just a one-dimensional array. |
| 2283 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
| 2284 | while (simpleArrayType.getArraySizes().getNumDims() > 1) |
| 2285 | simpleArrayType.getArraySizes().dereference(); |
| 2286 | |
| 2287 | // Will compute the higher-order strides here, rather than making a whole |
| 2288 | // pile of types and doing repetitive recursion on their contents. |
| 2289 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 2290 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2291 | |
| 2292 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2293 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2294 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2295 | if (stride > 0) |
| 2296 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2297 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2298 | } |
| 2299 | } else { |
| 2300 | // single-dimensional array, and don't yet have stride |
| 2301 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2302 | // 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] | 2303 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 2304 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2305 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2306 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2307 | // Do the outer dimension, which might not be known for a runtime-sized array |
| 2308 | if (type.isRuntimeSizedArray()) { |
| 2309 | spvType = builder.makeRuntimeArray(spvType); |
| 2310 | } else { |
| 2311 | assert(type.getOuterArraySize() > 0); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2312 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2313 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2314 | if (stride > 0) |
| 2315 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | return spvType; |
| 2319 | } |
| 2320 | |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2321 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 2322 | // |
| 2323 | // Identify interface members that don't have their required extension turned on. |
| 2324 | // |
| 2325 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 2326 | { |
| 2327 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 2328 | |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2329 | if (member.getFieldName() == "gl_ViewportMask" && |
| 2330 | extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 2331 | return true; |
| 2332 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 2333 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2334 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2335 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 2336 | extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 2337 | return true; |
| 2338 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 2339 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2340 | return true; |
Rex Xu | bcf291a | 2017-03-29 23:01:36 +0800 | [diff] [blame] | 2341 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 2342 | extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 2343 | return true; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2344 | |
| 2345 | return false; |
| 2346 | }; |
| 2347 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2348 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 2349 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 2350 | // Mutually recursive with convertGlslangToSpvType(). |
| 2351 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 2352 | const glslang::TTypeList* glslangMembers, |
| 2353 | glslang::TLayoutPacking explicitLayout, |
| 2354 | const glslang::TQualifier& qualifier) |
| 2355 | { |
| 2356 | // Create a vector of struct types for SPIR-V to consume |
| 2357 | std::vector<spv::Id> spvMembers; |
| 2358 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks |
| 2359 | int locationOffset = 0; // for use across struct members, when they are called recursively |
| 2360 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2361 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2362 | if (glslangMember.hiddenMember()) { |
| 2363 | ++memberDelta; |
| 2364 | if (type.getBasicType() == glslang::EbtBlock) |
| 2365 | memberRemapper[glslangMembers][i] = -1; |
| 2366 | } else { |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2367 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2368 | memberRemapper[glslangMembers][i] = i - memberDelta; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2369 | if (filterMember(glslangMember)) |
| 2370 | continue; |
| 2371 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2372 | // modify just this child's view of the qualifier |
| 2373 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2374 | InheritQualifiers(memberQualifier, qualifier); |
| 2375 | |
| 2376 | // manually inherit location; it's more complex |
| 2377 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
| 2378 | memberQualifier.layoutLocation = qualifier.layoutLocation + locationOffset; |
| 2379 | if (qualifier.hasLocation()) |
| 2380 | locationOffset += glslangIntermediate->computeTypeLocationSize(glslangMember); |
| 2381 | |
| 2382 | // recurse |
| 2383 | spvMembers.push_back(convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier)); |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | // Make the SPIR-V type |
| 2388 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
John Kessenich | f2b7f33 | 2016-09-01 17:05:23 -0600 | [diff] [blame] | 2389 | if (! HasNonLayoutQualifiers(type, qualifier)) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2390 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 2391 | |
| 2392 | // Decorate it |
| 2393 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 2394 | |
| 2395 | return spvType; |
| 2396 | } |
| 2397 | |
| 2398 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 2399 | const glslang::TTypeList* glslangMembers, |
| 2400 | glslang::TLayoutPacking explicitLayout, |
| 2401 | const glslang::TQualifier& qualifier, |
| 2402 | spv::Id spvType) |
| 2403 | { |
| 2404 | // Name and decorate the non-hidden members |
| 2405 | int offset = -1; |
| 2406 | int locationOffset = 0; // for use within the members of this struct |
| 2407 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 2408 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 2409 | int member = i; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2410 | if (type.getBasicType() == glslang::EbtBlock) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2411 | member = memberRemapper[glslangMembers][i]; |
John Kessenich | 0e73784 | 2017-03-24 18:38:16 -0600 | [diff] [blame] | 2412 | if (filterMember(glslangMember)) |
| 2413 | continue; |
| 2414 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2415 | |
| 2416 | // modify just this child's view of the qualifier |
| 2417 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 2418 | InheritQualifiers(memberQualifier, qualifier); |
| 2419 | |
| 2420 | // using -1 above to indicate a hidden member |
| 2421 | if (member >= 0) { |
| 2422 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 2423 | addMemberDecoration(spvType, member, TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 2424 | addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 2425 | // 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] | 2426 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 2427 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 2428 | if (type.getBasicType() == glslang::EbtBlock || |
| 2429 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2430 | addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 2431 | addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
| 2432 | } |
| 2433 | } |
| 2434 | addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
| 2435 | |
| 2436 | if (qualifier.storage == glslang::EvqBuffer) { |
| 2437 | std::vector<spv::Decoration> memory; |
| 2438 | TranslateMemoryDecoration(memberQualifier, memory); |
| 2439 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 2440 | addMemberDecoration(spvType, member, memory[i]); |
| 2441 | } |
| 2442 | |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2443 | // Compute location decoration; tricky based on whether inheritance is at play and |
| 2444 | // what kind of container we have, etc. |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2445 | // TODO: This algorithm (and it's cousin above doing almost the same thing) should |
| 2446 | // probably move to the linker stage of the front end proper, and just have the |
| 2447 | // answer sitting already distributed throughout the individual member locations. |
| 2448 | int location = -1; // will only decorate if present or inherited |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2449 | // Ignore member locations if the container is an array, as that's |
| 2450 | // ill-specified and decisions have been made to not allow this anyway. |
| 2451 | // The object itself must have a location, and that comes out from decorating the object, |
| 2452 | // not the type (this code decorates types). |
| 2453 | if (! type.isArray()) { |
| 2454 | if (memberQualifier.hasLocation()) { // no inheritance, or override of inheritance |
| 2455 | // struct members should not have explicit locations |
| 2456 | assert(type.getBasicType() != glslang::EbtStruct); |
| 2457 | location = memberQualifier.layoutLocation; |
| 2458 | } else if (type.getBasicType() != glslang::EbtBlock) { |
| 2459 | // If it is a not a Block, (...) Its members are assigned consecutive locations (...) |
| 2460 | // The members, and their nested types, must not themselves have Location decorations. |
| 2461 | } else if (qualifier.hasLocation()) // inheritance |
| 2462 | location = qualifier.layoutLocation + locationOffset; |
| 2463 | } |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2464 | if (location >= 0) |
| 2465 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, location); |
| 2466 | |
John Kessenich | 2f47bc9 | 2016-06-30 21:47:35 -0600 | [diff] [blame] | 2467 | if (qualifier.hasLocation()) // track for upcoming inheritance |
| 2468 | locationOffset += glslangIntermediate->computeTypeLocationSize(glslangMember); |
| 2469 | |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2470 | // component, XFB, others |
| 2471 | if (glslangMember.getQualifier().hasComponent()) |
| 2472 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, glslangMember.getQualifier().layoutComponent); |
| 2473 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 2474 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, glslangMember.getQualifier().layoutXfbOffset); |
| 2475 | else if (explicitLayout != glslang::ElpNone) { |
| 2476 | // figure out what to do with offset, which is accumulating |
| 2477 | int nextOffset; |
| 2478 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 2479 | if (offset >= 0) |
| 2480 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 2481 | offset = nextOffset; |
| 2482 | } |
| 2483 | |
| 2484 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 2485 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
| 2486 | |
| 2487 | // built-in variable decorations |
| 2488 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 2489 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2490 | addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2491 | |
| 2492 | #ifdef NV_EXTENSIONS |
| 2493 | if (builtIn == spv::BuiltInLayer) { |
| 2494 | // SPV_NV_viewport_array2 extension |
| 2495 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 2496 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 2497 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 2498 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 2499 | } |
| 2500 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 2501 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 2502 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 2503 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 2504 | } |
| 2505 | } |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 2506 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 2507 | addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 2508 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 2509 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 2510 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2511 | #endif |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | // Decorate the structure |
| 2516 | addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
John Kessenich | 6702718 | 2017-04-19 18:34:49 -0600 | [diff] [blame] | 2517 | addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
John Kessenich | 6090df0 | 2016-06-30 21:18:02 -0600 | [diff] [blame] | 2518 | if (type.getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
| 2519 | builder.addCapability(spv::CapabilityGeometryStreams); |
| 2520 | builder.addDecoration(spvType, spv::DecorationStream, type.getQualifier().layoutStream); |
| 2521 | } |
| 2522 | if (glslangIntermediate->getXfbMode()) { |
| 2523 | builder.addCapability(spv::CapabilityTransformFeedback); |
| 2524 | if (type.getQualifier().hasXfbStride()) |
| 2525 | builder.addDecoration(spvType, spv::DecorationXfbStride, type.getQualifier().layoutXfbStride); |
| 2526 | if (type.getQualifier().hasXfbBuffer()) |
| 2527 | builder.addDecoration(spvType, spv::DecorationXfbBuffer, type.getQualifier().layoutXfbBuffer); |
| 2528 | } |
| 2529 | } |
| 2530 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2531 | // Turn the expression forming the array size into an id. |
| 2532 | // This is not quite trivial, because of specialization constants. |
| 2533 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 2534 | // a specialization constant expression is. |
| 2535 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 2536 | { |
| 2537 | // First, see if this is sized with a node, meaning a specialization constant: |
| 2538 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 2539 | if (specNode != nullptr) { |
| 2540 | builder.clearAccessChain(); |
| 2541 | specNode->traverse(this); |
| 2542 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 2543 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2544 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2545 | // Otherwise, need a compile-time (front end) size, get it: |
| 2546 | int size = arraySizes.getDimSize(dim); |
| 2547 | assert(size > 0); |
| 2548 | return builder.makeUintConstant(size); |
| 2549 | } |
| 2550 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2551 | // Wrap the builder's accessChainLoad to: |
| 2552 | // - localize handling of RelaxedPrecision |
| 2553 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 2554 | // (avoids unnecessary work and possible type punning for structures) |
| 2555 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2556 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 2557 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2558 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2559 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), nominalTypeId); |
| 2560 | |
| 2561 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2562 | if (type.getBasicType() == glslang::EbtBool) { |
| 2563 | if (builder.isScalarType(nominalTypeId)) { |
| 2564 | // Conversion for bool |
| 2565 | spv::Id boolType = builder.makeBoolType(); |
| 2566 | if (nominalTypeId != boolType) |
| 2567 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 2568 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2569 | // Conversion for bvec |
| 2570 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2571 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 2572 | if (nominalTypeId != bvecType) |
| 2573 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 2574 | } |
| 2575 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2576 | |
| 2577 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2580 | // Wrap the builder's accessChainStore to: |
| 2581 | // - do conversion of concrete to abstract type |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2582 | // |
| 2583 | // Implicitly uses the existing builder.accessChain as the storage target. |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2584 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 2585 | { |
| 2586 | // Need to convert to abstract types when necessary |
| 2587 | if (type.getBasicType() == glslang::EbtBool) { |
| 2588 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2589 | |
| 2590 | if (builder.isScalarType(nominalTypeId)) { |
| 2591 | // Conversion for bool |
| 2592 | spv::Id boolType = builder.makeBoolType(); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame^] | 2593 | if (nominalTypeId != boolType) { |
| 2594 | // keep these outside arguments, for determinant order-of-evaluation |
| 2595 | spv::Id one = builder.makeUintConstant(1); |
| 2596 | spv::Id zero = builder.makeUintConstant(0); |
| 2597 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 2598 | } else if (builder.getTypeId(rvalue) != boolType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2599 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2600 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2601 | // Conversion for bvec |
| 2602 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2603 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame^] | 2604 | if (nominalTypeId != bvecType) { |
| 2605 | // keep these outside arguments, for determinant order-of-evaluation |
| 2606 | spv::Id one = builder.makeUintConstant(1); |
| 2607 | spv::Id zero = builder.makeUintConstant(0); |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2608 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, |
John Kessenich | b6cabc4 | 2017-05-19 23:29:50 -0600 | [diff] [blame^] | 2609 | makeSmearedConstant(one, vecSize), |
| 2610 | makeSmearedConstant(zero, vecSize)); |
| 2611 | } else if (builder.getTypeId(rvalue) != bvecType) |
John Kessenich | 80f92a1 | 2017-05-19 23:00:13 -0600 | [diff] [blame] | 2612 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 2613 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | builder.accessChainStore(rvalue); |
| 2618 | } |
| 2619 | |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2620 | // For storing when types match at the glslang level, but not might match at the |
| 2621 | // SPIR-V level. |
| 2622 | // |
| 2623 | // This especially happens when a single glslang type expands to multiple |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2624 | // 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] | 2625 | // as in a member-decorated way. |
| 2626 | // |
| 2627 | // NOTE: This function can handle any store request; if it's not special it |
| 2628 | // simplifies to a simple OpStore. |
| 2629 | // |
| 2630 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 2631 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 2632 | { |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2633 | // we only do the complex path here if it's an aggregate |
| 2634 | if (! type.isStruct() && ! type.isArray()) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2635 | accessChainStore(type, rValue); |
| 2636 | return; |
| 2637 | } |
| 2638 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2639 | // and, it has to be a case of type aliasing |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2640 | spv::Id rType = builder.getTypeId(rValue); |
| 2641 | spv::Id lValue = builder.accessChainGetLValue(); |
| 2642 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 2643 | if (lType == rType) { |
| 2644 | accessChainStore(type, rValue); |
| 2645 | return; |
| 2646 | } |
| 2647 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2648 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2649 | // where the two types were the same type in GLSL. This requires member |
| 2650 | // by member copy, recursively. |
| 2651 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2652 | // If an array, copy element by element. |
| 2653 | if (type.isArray()) { |
| 2654 | glslang::TType glslangElementType(type, 0); |
| 2655 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2656 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 2657 | // get the source member |
| 2658 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2659 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2660 | // set up the target storage |
| 2661 | builder.clearAccessChain(); |
| 2662 | builder.setAccessChainLValue(lValue); |
| 2663 | builder.accessChainPush(builder.makeIntConstant(index)); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2664 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2665 | // store the member |
| 2666 | multiTypeStore(glslangElementType, elementRValue); |
| 2667 | } |
| 2668 | } else { |
| 2669 | assert(type.isStruct()); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2670 | |
John Kessenich | b3e24e4 | 2016-09-11 12:33:43 -0600 | [diff] [blame] | 2671 | // loop over structure members |
| 2672 | const glslang::TTypeList& members = *type.getStruct(); |
| 2673 | for (int m = 0; m < (int)members.size(); ++m) { |
| 2674 | const glslang::TType& glslangMemberType = *members[m].type; |
| 2675 | |
| 2676 | // get the source member |
| 2677 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 2678 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 2679 | |
| 2680 | // set up the target storage |
| 2681 | builder.clearAccessChain(); |
| 2682 | builder.setAccessChainLValue(lValue); |
| 2683 | builder.accessChainPush(builder.makeIntConstant(m)); |
| 2684 | |
| 2685 | // store the member |
| 2686 | multiTypeStore(glslangMemberType, memberRValue); |
| 2687 | } |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2688 | } |
| 2689 | } |
| 2690 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2691 | // Decide whether or not this type should be |
| 2692 | // decorated with offsets and strides, and if so |
| 2693 | // whether std140 or std430 rules should be applied. |
| 2694 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2695 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2696 | // has to be a block |
| 2697 | if (type.getBasicType() != glslang::EbtBlock) |
| 2698 | return glslang::ElpNone; |
| 2699 | |
| 2700 | // has to be a uniform or buffer block |
| 2701 | if (type.getQualifier().storage != glslang::EvqUniform && |
| 2702 | type.getQualifier().storage != glslang::EvqBuffer) |
| 2703 | return glslang::ElpNone; |
| 2704 | |
| 2705 | // return the layout to use |
| 2706 | switch (type.getQualifier().layoutPacking) { |
| 2707 | case glslang::ElpStd140: |
| 2708 | case glslang::ElpStd430: |
| 2709 | return type.getQualifier().layoutPacking; |
| 2710 | default: |
| 2711 | return glslang::ElpNone; |
| 2712 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2713 | } |
| 2714 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2715 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2716 | 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] | 2717 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2718 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2719 | int stride; |
| 2720 | glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 2721 | |
| 2722 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2725 | // 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] | 2726 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2727 | 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] | 2728 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2729 | glslang::TType elementType; |
| 2730 | elementType.shallowCopy(matrixType); |
| 2731 | elementType.clearArraySizes(); |
| 2732 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2733 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2734 | int stride; |
| 2735 | glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
| 2736 | |
| 2737 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2740 | // Given a member type of a struct, realign the current offset for it, and compute |
| 2741 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 2742 | // on the next call. |
| 2743 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 2744 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 2745 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2746 | 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] | 2747 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2748 | { |
| 2749 | // this will get a positive value when deemed necessary |
| 2750 | nextOffset = -1; |
| 2751 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2752 | // override anything in currentOffset with user-set offset |
| 2753 | if (memberType.getQualifier().hasOffset()) |
| 2754 | currentOffset = memberType.getQualifier().layoutOffset; |
| 2755 | |
| 2756 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 2757 | // in which case the following code does not matter. But, that's not quite right |
| 2758 | // once cross-compilation unit GLSL validation is done, as the original user |
| 2759 | // settings are needed in layoutOffset, and then the following will come into play. |
| 2760 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2761 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2762 | if (! memberType.getQualifier().hasOffset()) |
| 2763 | currentOffset = -1; |
| 2764 | |
| 2765 | return; |
| 2766 | } |
| 2767 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2768 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2769 | if (currentOffset < 0) |
| 2770 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2771 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2772 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 2773 | // but possibly not yet correctly aligned. |
| 2774 | |
| 2775 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2776 | int dummyStride; |
| 2777 | 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] | 2778 | |
| 2779 | // Adjust alignment for HLSL rules |
| 2780 | if (glslangIntermediate->usingHlslOFfsets() && |
| 2781 | ! memberType.isArray() && memberType.isVector()) { |
| 2782 | int dummySize; |
| 2783 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 2784 | if (componentAlignment <= 4) |
| 2785 | memberAlignment = componentAlignment; |
| 2786 | } |
| 2787 | |
| 2788 | // Bump up to member alignment |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2789 | glslang::RoundToPow2(currentOffset, memberAlignment); |
John Kessenich | 4f1403e | 2017-04-05 17:38:20 -0600 | [diff] [blame] | 2790 | |
| 2791 | // Bump up to vec4 if there is a bad straddle |
| 2792 | if (glslangIntermediate->improperStraddle(memberType, memberSize, currentOffset)) |
| 2793 | glslang::RoundToPow2(currentOffset, 16); |
| 2794 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2795 | nextOffset = currentOffset + memberSize; |
| 2796 | } |
| 2797 | |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2798 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2799 | { |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2800 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 2801 | switch (glslangBuiltIn) |
| 2802 | { |
| 2803 | case glslang::EbvClipDistance: |
| 2804 | case glslang::EbvCullDistance: |
| 2805 | case glslang::EbvPointSize: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2806 | #ifdef NV_EXTENSIONS |
| 2807 | case glslang::EbvLayer: |
Rex Xu | 5e317ff | 2017-03-16 23:02:39 +0800 | [diff] [blame] | 2808 | case glslang::EbvViewportIndex: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2809 | case glslang::EbvViewportMaskNV: |
| 2810 | case glslang::EbvSecondaryPositionNV: |
| 2811 | case glslang::EbvSecondaryViewportMaskNV: |
chaoc | df3956c | 2017-02-14 14:52:34 -0800 | [diff] [blame] | 2812 | case glslang::EbvPositionPerViewNV: |
| 2813 | case glslang::EbvViewportMaskPerViewNV: |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 2814 | #endif |
David Neto | a901ffe | 2016-06-08 14:11:40 +0100 | [diff] [blame] | 2815 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 2816 | // Alternately, we could just call this for any glslang built-in, since the |
| 2817 | // capability already guards against duplicates. |
| 2818 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 2819 | break; |
| 2820 | default: |
| 2821 | // Capabilities were already generated when the struct was declared. |
| 2822 | break; |
| 2823 | } |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2824 | } |
| 2825 | |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2826 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2827 | { |
John Kessenich | eee9d53 | 2016-09-19 18:09:30 -0600 | [diff] [blame] | 2828 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 2832 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 2833 | { |
| 2834 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 2835 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6fccb3c | 2016-09-19 16:01:41 -0600 | [diff] [blame] | 2836 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2837 | continue; |
| 2838 | |
| 2839 | // 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] | 2840 | // 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] | 2841 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2842 | // 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] | 2843 | // function. What it is an address of varies: |
| 2844 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2845 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 2846 | // 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] | 2847 | // |
| 2848 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 2849 | // |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2850 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 2851 | // 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] | 2852 | |
| 2853 | std::vector<spv::Id> paramTypes; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2854 | std::vector<spv::Decoration> paramPrecisions; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2855 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 2856 | |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 2857 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == glslangIntermediate->implicitThisName; |
| 2858 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2859 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 2860 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 2861 | spv::Id typeId = convertGlslangToSpvType(paramType); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 2862 | // can we pass by reference? |
| 2863 | if (paramType.containsOpaque() || // sampler, etc. |
John Kessenich | 4960baa | 2017-03-19 18:09:59 -0600 | [diff] [blame] | 2864 | (paramType.getBasicType() == glslang::EbtBlock && |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 2865 | paramType.getQualifier().storage == glslang::EvqBuffer) || // SSBO |
John Kessenich | aa3c64c | 2017-03-28 09:52:38 -0600 | [diff] [blame] | 2866 | (p == 0 && implicitThis)) // implicit 'this' |
John Kessenich | a5c5fb6 | 2017-05-05 05:09:58 -0600 | [diff] [blame] | 2867 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
Jason Ekstrand | ed15ef1 | 2016-06-08 13:54:48 -0700 | [diff] [blame] | 2868 | else if (paramType.getQualifier().storage != glslang::EvqConstReadOnly) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2869 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 2870 | else |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 2871 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2872 | paramPrecisions.push_back(TranslatePrecisionDecoration(paramType)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2873 | paramTypes.push_back(typeId); |
| 2874 | } |
| 2875 | |
| 2876 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2877 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 2878 | convertGlslangToSpvType(glslFunction->getType()), |
| 2879 | glslFunction->getName().c_str(), paramTypes, paramPrecisions, &functionBlock); |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 2880 | if (implicitThis) |
| 2881 | function->setImplicitThis(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2882 | |
| 2883 | // Track function to emit/call later |
| 2884 | functionMap[glslFunction->getName().c_str()] = function; |
| 2885 | |
| 2886 | // Set the parameter id's |
| 2887 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 2888 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 2889 | // give a name too |
| 2890 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 2891 | } |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | // Process all the initializers, while skipping the functions and link objects |
| 2896 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 2897 | { |
| 2898 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 2899 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 2900 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 2901 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 2902 | |
| 2903 | // 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] | 2904 | // code goes into the beginning of the entry point. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2905 | initializer->traverse(this); |
| 2906 | } |
| 2907 | } |
| 2908 | } |
| 2909 | |
| 2910 | // Process all the functions, while skipping initializers. |
| 2911 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 2912 | { |
| 2913 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 2914 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
John Kessenich | 6a60c2f | 2016-12-08 21:01:59 -0700 | [diff] [blame] | 2915 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2916 | node->traverse(this); |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 2921 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2922 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2923 | // that called makeFunctions(). |
John Kessenich | ed33e05 | 2016-10-06 12:59:51 -0600 | [diff] [blame] | 2924 | currentFunction = functionMap[node->getName().c_str()]; |
| 2925 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2926 | builder.setBuildPoint(functionBlock); |
| 2927 | } |
| 2928 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 2929 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2930 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2931 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2932 | |
| 2933 | glslang::TSampler sampler = {}; |
| 2934 | bool cubeCompare = false; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 2935 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2936 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 2937 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 2938 | } |
| 2939 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2940 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 2941 | builder.clearAccessChain(); |
| 2942 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2943 | |
| 2944 | // Special case l-value operands |
| 2945 | bool lvalue = false; |
| 2946 | switch (node.getOp()) { |
| 2947 | case glslang::EOpImageAtomicAdd: |
| 2948 | case glslang::EOpImageAtomicMin: |
| 2949 | case glslang::EOpImageAtomicMax: |
| 2950 | case glslang::EOpImageAtomicAnd: |
| 2951 | case glslang::EOpImageAtomicOr: |
| 2952 | case glslang::EOpImageAtomicXor: |
| 2953 | case glslang::EOpImageAtomicExchange: |
| 2954 | case glslang::EOpImageAtomicCompSwap: |
| 2955 | if (i == 0) |
| 2956 | lvalue = true; |
| 2957 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 2958 | case glslang::EOpSparseImageLoad: |
| 2959 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 2960 | lvalue = true; |
| 2961 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2962 | case glslang::EOpSparseTexture: |
| 2963 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 2964 | lvalue = true; |
| 2965 | break; |
| 2966 | case glslang::EOpSparseTextureClamp: |
| 2967 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 2968 | lvalue = true; |
| 2969 | break; |
| 2970 | case glslang::EOpSparseTextureLod: |
| 2971 | case glslang::EOpSparseTextureOffset: |
| 2972 | if (i == 3) |
| 2973 | lvalue = true; |
| 2974 | break; |
| 2975 | case glslang::EOpSparseTextureFetch: |
| 2976 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 2977 | lvalue = true; |
| 2978 | break; |
| 2979 | case glslang::EOpSparseTextureFetchOffset: |
| 2980 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 2981 | lvalue = true; |
| 2982 | break; |
| 2983 | case glslang::EOpSparseTextureLodOffset: |
| 2984 | case glslang::EOpSparseTextureGrad: |
| 2985 | case glslang::EOpSparseTextureOffsetClamp: |
| 2986 | if (i == 4) |
| 2987 | lvalue = true; |
| 2988 | break; |
| 2989 | case glslang::EOpSparseTextureGradOffset: |
| 2990 | case glslang::EOpSparseTextureGradClamp: |
| 2991 | if (i == 5) |
| 2992 | lvalue = true; |
| 2993 | break; |
| 2994 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 2995 | if (i == 6) |
| 2996 | lvalue = true; |
| 2997 | break; |
| 2998 | case glslang::EOpSparseTextureGather: |
| 2999 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 3000 | lvalue = true; |
| 3001 | break; |
| 3002 | case glslang::EOpSparseTextureGatherOffset: |
| 3003 | case glslang::EOpSparseTextureGatherOffsets: |
| 3004 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 3005 | lvalue = true; |
| 3006 | break; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3007 | default: |
| 3008 | break; |
| 3009 | } |
| 3010 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3011 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3012 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3013 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3014 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3018 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3019 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3020 | builder.clearAccessChain(); |
| 3021 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3022 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3023 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3024 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3025 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 3026 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3027 | if (! node->isImage() && ! node->isTexture()) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3028 | return spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3029 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3030 | auto resultType = [&node,this]{ return convertGlslangToSpvType(node->getType()); }; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3031 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3032 | // Process a GLSL texturing op (will be SPV image) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3033 | const glslang::TSampler sampler = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType().getSampler() |
| 3034 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType().getSampler(); |
| 3035 | std::vector<spv::Id> arguments; |
| 3036 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3037 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3038 | else |
| 3039 | translateArguments(*node->getAsUnaryNode(), arguments); |
John Kessenich | f664076 | 2016-08-01 19:44:00 -0600 | [diff] [blame] | 3040 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3041 | |
| 3042 | spv::Builder::TextureParameters params = { }; |
| 3043 | params.sampler = arguments[0]; |
| 3044 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3045 | glslang::TCrackedTextureOp cracked; |
| 3046 | node->crackTexture(sampler, cracked); |
| 3047 | |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3048 | const bool isUnsignedResult = |
| 3049 | node->getType().getBasicType() == glslang::EbtUint64 || |
| 3050 | node->getType().getBasicType() == glslang::EbtUint; |
| 3051 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3052 | // Check for queries |
| 3053 | if (cracked.query) { |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3054 | // OpImageQueryLod works on a sampled image, for other queries the image has to be extracted first |
| 3055 | if (node->getOp() != glslang::EOpTextureQueryLod && builder.isSampledImage(params.sampler)) |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 3056 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
Maciej Jesionowski | 7208a97 | 2016-10-12 15:40:37 +0200 | [diff] [blame] | 3057 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3058 | switch (node->getOp()) { |
| 3059 | case glslang::EOpImageQuerySize: |
| 3060 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3061 | if (arguments.size() > 1) { |
| 3062 | params.lod = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3063 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3064 | } else |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3065 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3066 | case glslang::EOpImageQuerySamples: |
| 3067 | case glslang::EOpTextureQuerySamples: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3068 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3069 | case glslang::EOpTextureQueryLod: |
| 3070 | params.coords = arguments[1]; |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3071 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3072 | case glslang::EOpTextureQueryLevels: |
steve-lunarg | 0b5c2ae | 2017-03-10 12:45:50 -0700 | [diff] [blame] | 3073 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3074 | case glslang::EOpSparseTexelsResident: |
| 3075 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3076 | default: |
| 3077 | assert(0); |
| 3078 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3079 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3080 | } |
| 3081 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3082 | // Check for image functions other than queries |
| 3083 | if (node->isImage()) { |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3084 | std::vector<spv::Id> operands; |
| 3085 | auto opIt = arguments.begin(); |
| 3086 | operands.push_back(*(opIt++)); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3087 | |
| 3088 | // Handle subpass operations |
| 3089 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 3090 | // built-in function. |
| 3091 | if (cracked.subpass) { |
| 3092 | // add on the (0,0) coordinate |
| 3093 | spv::Id zero = builder.makeIntConstant(0); |
| 3094 | std::vector<spv::Id> comps; |
| 3095 | comps.push_back(zero); |
| 3096 | comps.push_back(zero); |
| 3097 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 3098 | if (sampler.ms) { |
| 3099 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3100 | operands.push_back(*(opIt++)); |
| 3101 | } |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3102 | return builder.createOp(spv::OpImageRead, resultType(), operands); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3103 | } |
| 3104 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3105 | operands.push_back(*(opIt++)); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3106 | if (node->getOp() == glslang::EOpImageLoad) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3107 | if (sampler.ms) { |
| 3108 | operands.push_back(spv::ImageOperandsSampleMask); |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3109 | operands.push_back(*opIt); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3110 | } |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3111 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3112 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3113 | return builder.createOp(spv::OpImageRead, resultType(), operands); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3114 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 3115 | if (sampler.ms) { |
| 3116 | operands.push_back(*(opIt + 1)); |
| 3117 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3118 | operands.push_back(*opIt); |
| 3119 | } else |
| 3120 | operands.push_back(*opIt); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3121 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 3122 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3123 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3124 | return spv::NoResult; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3125 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
| 3126 | builder.addCapability(spv::CapabilitySparseResidency); |
| 3127 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 3128 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 3129 | |
| 3130 | if (sampler.ms) { |
| 3131 | operands.push_back(spv::ImageOperandsSampleMask); |
| 3132 | operands.push_back(*opIt++); |
| 3133 | } |
| 3134 | |
| 3135 | // Create the return type that was a special structure |
| 3136 | spv::Id texelOut = *opIt; |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3137 | spv::Id typeId0 = resultType(); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 3138 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 3139 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 3140 | |
| 3141 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 3142 | |
| 3143 | // Decode the return type |
| 3144 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 3145 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3146 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3147 | // Process image atomic operations |
| 3148 | |
| 3149 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 3150 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 3151 | 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] | 3152 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3153 | spv::Id resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 3154 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3155 | |
| 3156 | std::vector<spv::Id> operands; |
| 3157 | operands.push_back(pointer); |
| 3158 | for (; opIt != arguments.end(); ++opIt) |
| 3159 | operands.push_back(*opIt); |
| 3160 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3161 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3166 | bool sparse = node->isSparseTexture(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 3167 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 3168 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3169 | // check for bias argument |
| 3170 | bool bias = false; |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 3171 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3172 | int nonBiasArgCount = 2; |
| 3173 | if (cracked.offset) |
| 3174 | ++nonBiasArgCount; |
| 3175 | if (cracked.grad) |
| 3176 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3177 | if (cracked.lodClamp) |
| 3178 | ++nonBiasArgCount; |
| 3179 | if (sparse) |
| 3180 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3181 | |
| 3182 | if ((int)arguments.size() > nonBiasArgCount) |
| 3183 | bias = true; |
| 3184 | } |
| 3185 | |
John Kessenich | a5c33d6 | 2016-06-02 23:45:21 -0600 | [diff] [blame] | 3186 | // See if the sampler param should really be just the SPV image part |
| 3187 | if (cracked.fetch) { |
| 3188 | // a fetch needs to have the image extracted first |
| 3189 | if (builder.isSampledImage(params.sampler)) |
| 3190 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 3191 | } |
| 3192 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3193 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3194 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3195 | params.coords = arguments[1]; |
| 3196 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3197 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3198 | |
| 3199 | // sort out where Dref is coming from |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3200 | if (cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3201 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3202 | ++extraArgs; |
| 3203 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3204 | params.Dref = arguments[2]; |
| 3205 | ++extraArgs; |
| 3206 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3207 | std::vector<spv::Id> indexes; |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3208 | int dRefComp; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3209 | if (cracked.proj) |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3210 | 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] | 3211 | else |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3212 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 3213 | indexes.push_back(dRefComp); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3214 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 3215 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3216 | |
| 3217 | // lod |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3218 | if (cracked.lod) { |
| 3219 | params.lod = arguments[2]; |
| 3220 | ++extraArgs; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3221 | } else if (glslangIntermediate->getStage() != EShLangFragment) { |
| 3222 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 3223 | noImplicitLod = true; |
| 3224 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3225 | |
| 3226 | // multisample |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 3227 | if (sampler.ms) { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 3228 | params.sample = arguments[2]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3229 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3230 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3231 | |
| 3232 | // gradient |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3233 | if (cracked.grad) { |
| 3234 | params.gradX = arguments[2 + extraArgs]; |
| 3235 | params.gradY = arguments[3 + extraArgs]; |
| 3236 | extraArgs += 2; |
| 3237 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3238 | |
| 3239 | // offset and offsets |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3240 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3241 | params.offset = arguments[2 + extraArgs]; |
| 3242 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3243 | } else if (cracked.offsets) { |
| 3244 | params.offsets = arguments[2 + extraArgs]; |
| 3245 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3246 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3247 | |
| 3248 | // lod clamp |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3249 | if (cracked.lodClamp) { |
| 3250 | params.lodClamp = arguments[2 + extraArgs]; |
| 3251 | ++extraArgs; |
| 3252 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3253 | |
| 3254 | // sparse |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 3255 | if (sparse) { |
| 3256 | params.texelOut = arguments[2 + extraArgs]; |
| 3257 | ++extraArgs; |
| 3258 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3259 | |
| 3260 | // bias |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3261 | if (bias) { |
| 3262 | params.bias = arguments[2 + extraArgs]; |
| 3263 | ++extraArgs; |
| 3264 | } |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3265 | |
| 3266 | // gather component |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3267 | if (cracked.gather && ! sampler.shadow) { |
| 3268 | // default component is 0, if missing, otherwise an argument |
| 3269 | if (2 + extraArgs < (int)arguments.size()) { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3270 | params.component = arguments[2 + extraArgs]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3271 | ++extraArgs; |
| 3272 | } else { |
John Kessenich | 76d4dfc | 2016-06-16 12:43:23 -0600 | [diff] [blame] | 3273 | params.component = builder.makeIntConstant(0); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3274 | } |
| 3275 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3276 | |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 3277 | // projective component (might not to move) |
| 3278 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 3279 | // are divided by the last component of P." |
| 3280 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 3281 | // unused components will appear after all used components." |
| 3282 | if (cracked.proj) { |
| 3283 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 3284 | int projTargetComp; |
| 3285 | switch (sampler.dim) { |
| 3286 | case glslang::Esd1D: projTargetComp = 1; break; |
| 3287 | case glslang::Esd2D: projTargetComp = 2; break; |
| 3288 | case glslang::EsdRect: projTargetComp = 2; break; |
| 3289 | default: projTargetComp = projSourceComp; break; |
| 3290 | } |
| 3291 | // copy the projective coordinate if we have to |
| 3292 | if (projTargetComp != projSourceComp) { |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 3293 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
John Kessenich | 6533648 | 2016-06-16 14:06:26 -0600 | [diff] [blame] | 3294 | builder.getScalarTypeId(builder.getTypeId(params.coords)), |
| 3295 | projSourceComp); |
| 3296 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 3297 | builder.getTypeId(params.coords), projTargetComp); |
| 3298 | } |
| 3299 | } |
| 3300 | |
John Kessenich | 8c8505c | 2016-07-26 12:50:38 -0600 | [diff] [blame] | 3301 | 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] | 3302 | } |
| 3303 | |
| 3304 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 3305 | { |
| 3306 | // Grab the function's pointer from the previously created function |
| 3307 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 3308 | if (! function) |
| 3309 | return 0; |
| 3310 | |
| 3311 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 3312 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 3313 | |
| 3314 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 3315 | // |
| 3316 | // These imply we need a four step process: |
| 3317 | // 1. Evaluate the arguments |
| 3318 | // 2. Allocate and make copies of in, out, and inout arguments |
| 3319 | // 3. Make the call |
| 3320 | // 4. Copy back the results |
| 3321 | |
| 3322 | // 1. Evaluate the arguments |
| 3323 | std::vector<spv::Builder::AccessChain> lValues; |
| 3324 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3325 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3326 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3327 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3328 | // build l-value |
| 3329 | builder.clearAccessChain(); |
| 3330 | glslangArgs[a]->traverse(this); |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3331 | argTypes.push_back(¶mType); |
John Kessenich | 1176530 | 2016-07-31 12:39:46 -0600 | [diff] [blame] | 3332 | // keep outputs and opaque objects as l-values, evaluate input-only as r-values |
John Kessenich | 4a57dce | 2017-02-24 19:15:46 -0700 | [diff] [blame] | 3333 | if (qualifiers[a] != glslang::EvqConstReadOnly || paramType.containsOpaque()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3334 | // save l-value |
| 3335 | lValues.push_back(builder.getAccessChain()); |
| 3336 | } else { |
| 3337 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3338 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 3343 | // copy the original into that space. |
| 3344 | // |
| 3345 | // Also, build up the list of actual arguments to pass in for the call |
| 3346 | int lValueCount = 0; |
| 3347 | int rValueCount = 0; |
| 3348 | std::vector<spv::Id> spvArgs; |
| 3349 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3350 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3351 | spv::Id arg; |
steve-lunarg | dd8287a | 2017-02-23 18:04:12 -0700 | [diff] [blame] | 3352 | if (paramType.containsOpaque() || |
John Kessenich | 3778979 | 2017-03-21 23:56:40 -0600 | [diff] [blame] | 3353 | (paramType.getBasicType() == glslang::EbtBlock && qualifiers[a] == glslang::EvqBuffer) || |
| 3354 | (a == 0 && function->hasImplicitThis())) { |
Jason Ekstrand | 76d0ac1 | 2016-05-25 11:50:21 -0700 | [diff] [blame] | 3355 | builder.setAccessChain(lValues[lValueCount]); |
| 3356 | arg = builder.accessChainGetLValue(); |
| 3357 | ++lValueCount; |
| 3358 | } else if (qualifiers[a] != glslang::EvqConstReadOnly) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3359 | // need space to hold the copy |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3360 | arg = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(paramType), "param"); |
| 3361 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 3362 | // need to copy the input into output space |
| 3363 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3364 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3365 | builder.clearAccessChain(); |
| 3366 | builder.setAccessChainLValue(arg); |
| 3367 | multiTypeStore(paramType, copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3368 | } |
| 3369 | ++lValueCount; |
| 3370 | } else { |
| 3371 | arg = rValues[rValueCount]; |
| 3372 | ++rValueCount; |
| 3373 | } |
| 3374 | spvArgs.push_back(arg); |
| 3375 | } |
| 3376 | |
| 3377 | // 3. Make the call. |
| 3378 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3379 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3380 | |
| 3381 | // 4. Copy back out an "out" arguments. |
| 3382 | lValueCount = 0; |
| 3383 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3384 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3385 | if (qualifiers[a] != glslang::EvqConstReadOnly) { |
| 3386 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 3387 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 3388 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 4bf7155 | 2016-09-02 11:20:21 -0600 | [diff] [blame] | 3389 | multiTypeStore(paramType, copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3390 | } |
| 3391 | ++lValueCount; |
| 3392 | } |
| 3393 | } |
| 3394 | |
| 3395 | return result; |
| 3396 | } |
| 3397 | |
| 3398 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3399 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, spv::Decoration precision, |
| 3400 | spv::Decoration noContraction, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3401 | spv::Id typeId, spv::Id left, spv::Id right, |
| 3402 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 3403 | { |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3404 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3405 | #ifdef AMD_EXTENSIONS |
| 3406 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 3407 | #else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3408 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3409 | #endif |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3410 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3411 | |
| 3412 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3413 | 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] | 3414 | bool comparison = false; |
| 3415 | |
| 3416 | switch (op) { |
| 3417 | case glslang::EOpAdd: |
| 3418 | case glslang::EOpAddAssign: |
| 3419 | if (isFloat) |
| 3420 | binOp = spv::OpFAdd; |
| 3421 | else |
| 3422 | binOp = spv::OpIAdd; |
| 3423 | break; |
| 3424 | case glslang::EOpSub: |
| 3425 | case glslang::EOpSubAssign: |
| 3426 | if (isFloat) |
| 3427 | binOp = spv::OpFSub; |
| 3428 | else |
| 3429 | binOp = spv::OpISub; |
| 3430 | break; |
| 3431 | case glslang::EOpMul: |
| 3432 | case glslang::EOpMulAssign: |
| 3433 | if (isFloat) |
| 3434 | binOp = spv::OpFMul; |
| 3435 | else |
| 3436 | binOp = spv::OpIMul; |
| 3437 | break; |
| 3438 | case glslang::EOpVectorTimesScalar: |
| 3439 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 3440 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3441 | if (builder.isVector(right)) |
| 3442 | std::swap(left, right); |
| 3443 | assert(builder.isScalar(right)); |
| 3444 | needMatchingVectors = false; |
| 3445 | binOp = spv::OpVectorTimesScalar; |
| 3446 | } else |
| 3447 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3448 | break; |
| 3449 | case glslang::EOpVectorTimesMatrix: |
| 3450 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3451 | binOp = spv::OpVectorTimesMatrix; |
| 3452 | break; |
| 3453 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3454 | binOp = spv::OpMatrixTimesVector; |
| 3455 | break; |
| 3456 | case glslang::EOpMatrixTimesScalar: |
| 3457 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3458 | binOp = spv::OpMatrixTimesScalar; |
| 3459 | break; |
| 3460 | case glslang::EOpMatrixTimesMatrix: |
| 3461 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3462 | binOp = spv::OpMatrixTimesMatrix; |
| 3463 | break; |
| 3464 | case glslang::EOpOuterProduct: |
| 3465 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3466 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3467 | break; |
| 3468 | |
| 3469 | case glslang::EOpDiv: |
| 3470 | case glslang::EOpDivAssign: |
| 3471 | if (isFloat) |
| 3472 | binOp = spv::OpFDiv; |
| 3473 | else if (isUnsigned) |
| 3474 | binOp = spv::OpUDiv; |
| 3475 | else |
| 3476 | binOp = spv::OpSDiv; |
| 3477 | break; |
| 3478 | case glslang::EOpMod: |
| 3479 | case glslang::EOpModAssign: |
| 3480 | if (isFloat) |
| 3481 | binOp = spv::OpFMod; |
| 3482 | else if (isUnsigned) |
| 3483 | binOp = spv::OpUMod; |
| 3484 | else |
| 3485 | binOp = spv::OpSMod; |
| 3486 | break; |
| 3487 | case glslang::EOpRightShift: |
| 3488 | case glslang::EOpRightShiftAssign: |
| 3489 | if (isUnsigned) |
| 3490 | binOp = spv::OpShiftRightLogical; |
| 3491 | else |
| 3492 | binOp = spv::OpShiftRightArithmetic; |
| 3493 | break; |
| 3494 | case glslang::EOpLeftShift: |
| 3495 | case glslang::EOpLeftShiftAssign: |
| 3496 | binOp = spv::OpShiftLeftLogical; |
| 3497 | break; |
| 3498 | case glslang::EOpAnd: |
| 3499 | case glslang::EOpAndAssign: |
| 3500 | binOp = spv::OpBitwiseAnd; |
| 3501 | break; |
| 3502 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3503 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3504 | binOp = spv::OpLogicalAnd; |
| 3505 | break; |
| 3506 | case glslang::EOpInclusiveOr: |
| 3507 | case glslang::EOpInclusiveOrAssign: |
| 3508 | binOp = spv::OpBitwiseOr; |
| 3509 | break; |
| 3510 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3511 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3512 | binOp = spv::OpLogicalOr; |
| 3513 | break; |
| 3514 | case glslang::EOpExclusiveOr: |
| 3515 | case glslang::EOpExclusiveOrAssign: |
| 3516 | binOp = spv::OpBitwiseXor; |
| 3517 | break; |
| 3518 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 3519 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3520 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3521 | break; |
| 3522 | |
| 3523 | case glslang::EOpLessThan: |
| 3524 | case glslang::EOpGreaterThan: |
| 3525 | case glslang::EOpLessThanEqual: |
| 3526 | case glslang::EOpGreaterThanEqual: |
| 3527 | case glslang::EOpEqual: |
| 3528 | case glslang::EOpNotEqual: |
| 3529 | case glslang::EOpVectorEqual: |
| 3530 | case glslang::EOpVectorNotEqual: |
| 3531 | comparison = true; |
| 3532 | break; |
| 3533 | default: |
| 3534 | break; |
| 3535 | } |
| 3536 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3537 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3538 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3539 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3540 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3541 | return createBinaryMatrixOperation(binOp, precision, noContraction, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3542 | |
| 3543 | // 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] | 3544 | if (needMatchingVectors) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3545 | builder.promoteScalar(precision, left, right); |
| 3546 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3547 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 3548 | addDecoration(result, noContraction); |
| 3549 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | if (! comparison) |
| 3553 | return 0; |
| 3554 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 3555 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3556 | |
John Kessenich | 4583b61 | 2016-08-07 19:14:22 -0600 | [diff] [blame] | 3557 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
| 3558 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 3559 | return builder.createCompositeCompare(precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3560 | |
| 3561 | switch (op) { |
| 3562 | case glslang::EOpLessThan: |
| 3563 | if (isFloat) |
| 3564 | binOp = spv::OpFOrdLessThan; |
| 3565 | else if (isUnsigned) |
| 3566 | binOp = spv::OpULessThan; |
| 3567 | else |
| 3568 | binOp = spv::OpSLessThan; |
| 3569 | break; |
| 3570 | case glslang::EOpGreaterThan: |
| 3571 | if (isFloat) |
| 3572 | binOp = spv::OpFOrdGreaterThan; |
| 3573 | else if (isUnsigned) |
| 3574 | binOp = spv::OpUGreaterThan; |
| 3575 | else |
| 3576 | binOp = spv::OpSGreaterThan; |
| 3577 | break; |
| 3578 | case glslang::EOpLessThanEqual: |
| 3579 | if (isFloat) |
| 3580 | binOp = spv::OpFOrdLessThanEqual; |
| 3581 | else if (isUnsigned) |
| 3582 | binOp = spv::OpULessThanEqual; |
| 3583 | else |
| 3584 | binOp = spv::OpSLessThanEqual; |
| 3585 | break; |
| 3586 | case glslang::EOpGreaterThanEqual: |
| 3587 | if (isFloat) |
| 3588 | binOp = spv::OpFOrdGreaterThanEqual; |
| 3589 | else if (isUnsigned) |
| 3590 | binOp = spv::OpUGreaterThanEqual; |
| 3591 | else |
| 3592 | binOp = spv::OpSGreaterThanEqual; |
| 3593 | break; |
| 3594 | case glslang::EOpEqual: |
| 3595 | case glslang::EOpVectorEqual: |
| 3596 | if (isFloat) |
| 3597 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3598 | else if (isBool) |
| 3599 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3600 | else |
| 3601 | binOp = spv::OpIEqual; |
| 3602 | break; |
| 3603 | case glslang::EOpNotEqual: |
| 3604 | case glslang::EOpVectorNotEqual: |
| 3605 | if (isFloat) |
| 3606 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 3607 | else if (isBool) |
| 3608 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3609 | else |
| 3610 | binOp = spv::OpINotEqual; |
| 3611 | break; |
| 3612 | default: |
| 3613 | break; |
| 3614 | } |
| 3615 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3616 | if (binOp != spv::OpNop) { |
| 3617 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 3618 | addDecoration(result, noContraction); |
| 3619 | return builder.setPrecision(result, precision); |
| 3620 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3621 | |
| 3622 | return 0; |
| 3623 | } |
| 3624 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3625 | // |
| 3626 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 3627 | // These can be any of: |
| 3628 | // |
| 3629 | // matrix * scalar |
| 3630 | // scalar * matrix |
| 3631 | // matrix * matrix linear algebraic |
| 3632 | // matrix * vector |
| 3633 | // vector * matrix |
| 3634 | // matrix * matrix componentwise |
| 3635 | // matrix op matrix op in {+, -, /} |
| 3636 | // matrix op scalar op in {+, -, /} |
| 3637 | // scalar op matrix op in {+, -, /} |
| 3638 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3639 | 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] | 3640 | { |
| 3641 | bool firstClass = true; |
| 3642 | |
| 3643 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 3644 | switch (op) { |
| 3645 | case spv::OpFDiv: |
| 3646 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 3647 | // turn matrix / scalar into a multiply... |
| 3648 | right = builder.createBinOp(spv::OpFDiv, builder.getTypeId(right), builder.makeFloatConstant(1.0F), right); |
| 3649 | op = spv::OpMatrixTimesScalar; |
| 3650 | } else |
| 3651 | firstClass = false; |
| 3652 | break; |
| 3653 | case spv::OpMatrixTimesScalar: |
| 3654 | if (builder.isMatrix(right)) |
| 3655 | std::swap(left, right); |
| 3656 | assert(builder.isScalar(right)); |
| 3657 | break; |
| 3658 | case spv::OpVectorTimesMatrix: |
| 3659 | assert(builder.isVector(left)); |
| 3660 | assert(builder.isMatrix(right)); |
| 3661 | break; |
| 3662 | case spv::OpMatrixTimesVector: |
| 3663 | assert(builder.isMatrix(left)); |
| 3664 | assert(builder.isVector(right)); |
| 3665 | break; |
| 3666 | case spv::OpMatrixTimesMatrix: |
| 3667 | assert(builder.isMatrix(left)); |
| 3668 | assert(builder.isMatrix(right)); |
| 3669 | break; |
| 3670 | default: |
| 3671 | firstClass = false; |
| 3672 | break; |
| 3673 | } |
| 3674 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3675 | if (firstClass) { |
| 3676 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
| 3677 | addDecoration(result, noContraction); |
| 3678 | return builder.setPrecision(result, precision); |
| 3679 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3680 | |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 3681 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3682 | // The result type of all of them is the same type as the (a) matrix operand. |
| 3683 | // The algorithm is to: |
| 3684 | // - break the matrix(es) into vectors |
| 3685 | // - smear any scalar to a vector |
| 3686 | // - do vector operations |
| 3687 | // - make a matrix out the vector results |
| 3688 | switch (op) { |
| 3689 | case spv::OpFAdd: |
| 3690 | case spv::OpFSub: |
| 3691 | case spv::OpFDiv: |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 3692 | case spv::OpFMod: |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3693 | case spv::OpFMul: |
| 3694 | { |
| 3695 | // one time set up... |
| 3696 | bool leftMat = builder.isMatrix(left); |
| 3697 | bool rightMat = builder.isMatrix(right); |
| 3698 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 3699 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 3700 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 3701 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 3702 | std::vector<spv::Id> results; |
| 3703 | spv::Id smearVec = spv::NoResult; |
| 3704 | if (builder.isScalar(left)) |
| 3705 | smearVec = builder.smearScalar(precision, left, vecType); |
| 3706 | else if (builder.isScalar(right)) |
| 3707 | smearVec = builder.smearScalar(precision, right, vecType); |
| 3708 | |
| 3709 | // do each vector op |
| 3710 | for (unsigned int c = 0; c < numCols; ++c) { |
| 3711 | std::vector<unsigned int> indexes; |
| 3712 | indexes.push_back(c); |
| 3713 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 3714 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3715 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
| 3716 | addDecoration(result, noContraction); |
| 3717 | results.push_back(builder.setPrecision(result, precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3721 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3722 | } |
| 3723 | default: |
| 3724 | assert(0); |
| 3725 | return spv::NoResult; |
| 3726 | } |
| 3727 | } |
| 3728 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3729 | 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] | 3730 | { |
| 3731 | spv::Op unaryOp = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 3732 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3733 | int libCall = -1; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3734 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3735 | #ifdef AMD_EXTENSIONS |
| 3736 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 3737 | #else |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3738 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3739 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3740 | |
| 3741 | switch (op) { |
| 3742 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3743 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3744 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3745 | if (builder.isMatrixType(typeId)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3746 | return createUnaryMatrixOperation(unaryOp, precision, noContraction, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3747 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3748 | unaryOp = spv::OpSNegate; |
| 3749 | break; |
| 3750 | |
| 3751 | case glslang::EOpLogicalNot: |
| 3752 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3753 | unaryOp = spv::OpLogicalNot; |
| 3754 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3755 | case glslang::EOpBitwiseNot: |
| 3756 | unaryOp = spv::OpNot; |
| 3757 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3758 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3759 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3760 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3761 | break; |
| 3762 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3763 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3764 | break; |
| 3765 | case glslang::EOpTranspose: |
| 3766 | unaryOp = spv::OpTranspose; |
| 3767 | break; |
| 3768 | |
| 3769 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3770 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3771 | break; |
| 3772 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3773 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3774 | break; |
| 3775 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3776 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3777 | break; |
| 3778 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3779 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3780 | break; |
| 3781 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3782 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3783 | break; |
| 3784 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3785 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3786 | break; |
| 3787 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3788 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3789 | break; |
| 3790 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3791 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3792 | break; |
| 3793 | |
| 3794 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3795 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3796 | break; |
| 3797 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3798 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3799 | break; |
| 3800 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3801 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3802 | break; |
| 3803 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3804 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3805 | break; |
| 3806 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3807 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3808 | break; |
| 3809 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3810 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3811 | break; |
| 3812 | |
| 3813 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3814 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3815 | break; |
| 3816 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3817 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3818 | break; |
| 3819 | |
| 3820 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3821 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3822 | break; |
| 3823 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3824 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3825 | break; |
| 3826 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3827 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3828 | break; |
| 3829 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3830 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3831 | break; |
| 3832 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3833 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3834 | break; |
| 3835 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3836 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3837 | break; |
| 3838 | |
| 3839 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3840 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3841 | break; |
| 3842 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3843 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3844 | break; |
| 3845 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3846 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3847 | break; |
| 3848 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3849 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3850 | break; |
| 3851 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3852 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3853 | break; |
| 3854 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3855 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3856 | break; |
| 3857 | |
| 3858 | case glslang::EOpIsNan: |
| 3859 | unaryOp = spv::OpIsNan; |
| 3860 | break; |
| 3861 | case glslang::EOpIsInf: |
| 3862 | unaryOp = spv::OpIsInf; |
| 3863 | break; |
LoopDawg | 592860c | 2016-06-09 08:57:35 -0600 | [diff] [blame] | 3864 | case glslang::EOpIsFinite: |
| 3865 | unaryOp = spv::OpIsFinite; |
| 3866 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3867 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 3868 | case glslang::EOpFloatBitsToInt: |
| 3869 | case glslang::EOpFloatBitsToUint: |
| 3870 | case glslang::EOpIntBitsToFloat: |
| 3871 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3872 | case glslang::EOpDoubleBitsToInt64: |
| 3873 | case glslang::EOpDoubleBitsToUint64: |
| 3874 | case glslang::EOpInt64BitsToDouble: |
| 3875 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 3876 | unaryOp = spv::OpBitcast; |
| 3877 | break; |
| 3878 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3879 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3880 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3881 | break; |
| 3882 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3883 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3884 | break; |
| 3885 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3886 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3887 | break; |
| 3888 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3889 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3890 | break; |
| 3891 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3892 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3893 | break; |
| 3894 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3895 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3896 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3897 | case glslang::EOpPackSnorm4x8: |
| 3898 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 3899 | break; |
| 3900 | case glslang::EOpUnpackSnorm4x8: |
| 3901 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 3902 | break; |
| 3903 | case glslang::EOpPackUnorm4x8: |
| 3904 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 3905 | break; |
| 3906 | case glslang::EOpUnpackUnorm4x8: |
| 3907 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 3908 | break; |
| 3909 | case glslang::EOpPackDouble2x32: |
| 3910 | libCall = spv::GLSLstd450PackDouble2x32; |
| 3911 | break; |
| 3912 | case glslang::EOpUnpackDouble2x32: |
| 3913 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 3914 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3915 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3916 | case glslang::EOpPackInt2x32: |
| 3917 | case glslang::EOpUnpackInt2x32: |
| 3918 | case glslang::EOpPackUint2x32: |
| 3919 | case glslang::EOpUnpackUint2x32: |
Rex Xu | c9f3492 | 2016-09-09 17:50:07 +0800 | [diff] [blame] | 3920 | unaryOp = spv::OpBitcast; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3921 | break; |
| 3922 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 3923 | #ifdef AMD_EXTENSIONS |
| 3924 | case glslang::EOpPackFloat2x16: |
| 3925 | case glslang::EOpUnpackFloat2x16: |
| 3926 | unaryOp = spv::OpBitcast; |
| 3927 | break; |
| 3928 | #endif |
| 3929 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3930 | case glslang::EOpDPdx: |
| 3931 | unaryOp = spv::OpDPdx; |
| 3932 | break; |
| 3933 | case glslang::EOpDPdy: |
| 3934 | unaryOp = spv::OpDPdy; |
| 3935 | break; |
| 3936 | case glslang::EOpFwidth: |
| 3937 | unaryOp = spv::OpFwidth; |
| 3938 | break; |
| 3939 | case glslang::EOpDPdxFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3940 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3941 | unaryOp = spv::OpDPdxFine; |
| 3942 | break; |
| 3943 | case glslang::EOpDPdyFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3944 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3945 | unaryOp = spv::OpDPdyFine; |
| 3946 | break; |
| 3947 | case glslang::EOpFwidthFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3948 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3949 | unaryOp = spv::OpFwidthFine; |
| 3950 | break; |
| 3951 | case glslang::EOpDPdxCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3952 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3953 | unaryOp = spv::OpDPdxCoarse; |
| 3954 | break; |
| 3955 | case glslang::EOpDPdyCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3956 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3957 | unaryOp = spv::OpDPdyCoarse; |
| 3958 | break; |
| 3959 | case glslang::EOpFwidthCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3960 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3961 | unaryOp = spv::OpFwidthCoarse; |
| 3962 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 3963 | case glslang::EOpInterpolateAtCentroid: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3964 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 3965 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 3966 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3967 | case glslang::EOpAny: |
| 3968 | unaryOp = spv::OpAny; |
| 3969 | break; |
| 3970 | case glslang::EOpAll: |
| 3971 | unaryOp = spv::OpAll; |
| 3972 | break; |
| 3973 | |
| 3974 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3975 | if (isFloat) |
| 3976 | libCall = spv::GLSLstd450FAbs; |
| 3977 | else |
| 3978 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3979 | break; |
| 3980 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3981 | if (isFloat) |
| 3982 | libCall = spv::GLSLstd450FSign; |
| 3983 | else |
| 3984 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3985 | break; |
| 3986 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3987 | case glslang::EOpAtomicCounterIncrement: |
| 3988 | case glslang::EOpAtomicCounterDecrement: |
| 3989 | case glslang::EOpAtomicCounter: |
| 3990 | { |
| 3991 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 3992 | std::vector<spv::Id> operands; |
| 3993 | operands.push_back(operand); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3994 | return createAtomicOperation(op, precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3995 | } |
| 3996 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3997 | case glslang::EOpBitFieldReverse: |
| 3998 | unaryOp = spv::OpBitReverse; |
| 3999 | break; |
| 4000 | case glslang::EOpBitCount: |
| 4001 | unaryOp = spv::OpBitCount; |
| 4002 | break; |
| 4003 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4004 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4005 | break; |
| 4006 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4007 | if (isUnsigned) |
| 4008 | libCall = spv::GLSLstd450FindUMsb; |
| 4009 | else |
| 4010 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 4011 | break; |
| 4012 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 4013 | case glslang::EOpBallot: |
| 4014 | case glslang::EOpReadFirstInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4015 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4016 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4017 | case glslang::EOpAllInvocationsEqual: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4018 | #ifdef AMD_EXTENSIONS |
| 4019 | case glslang::EOpMinInvocations: |
| 4020 | case glslang::EOpMaxInvocations: |
| 4021 | case glslang::EOpAddInvocations: |
| 4022 | case glslang::EOpMinInvocationsNonUniform: |
| 4023 | case glslang::EOpMaxInvocationsNonUniform: |
| 4024 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4025 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4026 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4027 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4028 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4029 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4030 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4031 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4032 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4033 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4034 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4035 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4036 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4037 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4038 | { |
| 4039 | std::vector<spv::Id> operands; |
| 4040 | operands.push_back(operand); |
| 4041 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 4042 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4043 | |
| 4044 | #ifdef AMD_EXTENSIONS |
| 4045 | case glslang::EOpMbcnt: |
| 4046 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 4047 | libCall = spv::MbcntAMD; |
| 4048 | break; |
| 4049 | |
| 4050 | case glslang::EOpCubeFaceIndex: |
| 4051 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 4052 | libCall = spv::CubeFaceIndexAMD; |
| 4053 | break; |
| 4054 | |
| 4055 | case glslang::EOpCubeFaceCoord: |
| 4056 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 4057 | libCall = spv::CubeFaceCoordAMD; |
| 4058 | break; |
| 4059 | #endif |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4060 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4061 | default: |
| 4062 | return 0; |
| 4063 | } |
| 4064 | |
| 4065 | spv::Id id; |
| 4066 | if (libCall >= 0) { |
| 4067 | std::vector<spv::Id> args; |
| 4068 | args.push_back(operand); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4069 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4070 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4071 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 4072 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4073 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4074 | addDecoration(id, noContraction); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4075 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4076 | } |
| 4077 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4078 | // Create a unary operation on a matrix |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4079 | 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] | 4080 | { |
| 4081 | // Handle unary operations vector by vector. |
| 4082 | // The result type is the same type as the original type. |
| 4083 | // The algorithm is to: |
| 4084 | // - break the matrix into vectors |
| 4085 | // - apply the operation to each vector |
| 4086 | // - make a matrix out the vector results |
| 4087 | |
| 4088 | // get the types sorted out |
| 4089 | int numCols = builder.getNumColumns(operand); |
| 4090 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 4091 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 4092 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4093 | std::vector<spv::Id> results; |
| 4094 | |
| 4095 | // do each vector op |
| 4096 | for (int c = 0; c < numCols; ++c) { |
| 4097 | std::vector<unsigned int> indexes; |
| 4098 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 4099 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 4100 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
| 4101 | addDecoration(destVec, noContraction); |
| 4102 | results.push_back(builder.setPrecision(destVec, precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4106 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 4107 | } |
| 4108 | |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 4109 | 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] | 4110 | { |
| 4111 | spv::Op convOp = spv::OpNop; |
| 4112 | spv::Id zero = 0; |
| 4113 | spv::Id one = 0; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4114 | spv::Id type = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4115 | |
| 4116 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 4117 | |
| 4118 | switch (op) { |
| 4119 | case glslang::EOpConvIntToBool: |
| 4120 | case glslang::EOpConvUintToBool: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4121 | case glslang::EOpConvInt64ToBool: |
| 4122 | case glslang::EOpConvUint64ToBool: |
| 4123 | zero = (op == glslang::EOpConvInt64ToBool || |
| 4124 | op == glslang::EOpConvUint64ToBool) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4125 | zero = makeSmearedConstant(zero, vectorSize); |
| 4126 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 4127 | |
| 4128 | case glslang::EOpConvFloatToBool: |
| 4129 | zero = builder.makeFloatConstant(0.0F); |
| 4130 | zero = makeSmearedConstant(zero, vectorSize); |
| 4131 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4132 | |
| 4133 | case glslang::EOpConvDoubleToBool: |
| 4134 | zero = builder.makeDoubleConstant(0.0); |
| 4135 | zero = makeSmearedConstant(zero, vectorSize); |
| 4136 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4137 | |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4138 | #ifdef AMD_EXTENSIONS |
| 4139 | case glslang::EOpConvFloat16ToBool: |
| 4140 | zero = builder.makeFloat16Constant(0.0F); |
| 4141 | zero = makeSmearedConstant(zero, vectorSize); |
| 4142 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 4143 | #endif |
| 4144 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4145 | case glslang::EOpConvBoolToFloat: |
| 4146 | convOp = spv::OpSelect; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4147 | zero = builder.makeFloatConstant(0.0F); |
| 4148 | one = builder.makeFloatConstant(1.0F); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4149 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4150 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4151 | case glslang::EOpConvBoolToDouble: |
| 4152 | convOp = spv::OpSelect; |
| 4153 | zero = builder.makeDoubleConstant(0.0); |
| 4154 | one = builder.makeDoubleConstant(1.0); |
| 4155 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4156 | |
| 4157 | #ifdef AMD_EXTENSIONS |
| 4158 | case glslang::EOpConvBoolToFloat16: |
| 4159 | convOp = spv::OpSelect; |
| 4160 | zero = builder.makeFloat16Constant(0.0F); |
| 4161 | one = builder.makeFloat16Constant(1.0F); |
| 4162 | break; |
| 4163 | #endif |
| 4164 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4165 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4166 | case glslang::EOpConvBoolToInt64: |
| 4167 | zero = (op == glslang::EOpConvBoolToInt64) ? builder.makeInt64Constant(0) : builder.makeIntConstant(0); |
| 4168 | one = (op == glslang::EOpConvBoolToInt64) ? builder.makeInt64Constant(1) : builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4169 | convOp = spv::OpSelect; |
| 4170 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4171 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4172 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4173 | case glslang::EOpConvBoolToUint64: |
| 4174 | zero = (op == glslang::EOpConvBoolToUint64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0); |
| 4175 | one = (op == glslang::EOpConvBoolToUint64) ? builder.makeUint64Constant(1) : builder.makeUintConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4176 | convOp = spv::OpSelect; |
| 4177 | break; |
| 4178 | |
| 4179 | case glslang::EOpConvIntToFloat: |
| 4180 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4181 | case glslang::EOpConvInt64ToFloat: |
| 4182 | case glslang::EOpConvInt64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4183 | #ifdef AMD_EXTENSIONS |
| 4184 | case glslang::EOpConvIntToFloat16: |
| 4185 | case glslang::EOpConvInt64ToFloat16: |
| 4186 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4187 | convOp = spv::OpConvertSToF; |
| 4188 | break; |
| 4189 | |
| 4190 | case glslang::EOpConvUintToFloat: |
| 4191 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4192 | case glslang::EOpConvUint64ToFloat: |
| 4193 | case glslang::EOpConvUint64ToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4194 | #ifdef AMD_EXTENSIONS |
| 4195 | case glslang::EOpConvUintToFloat16: |
| 4196 | case glslang::EOpConvUint64ToFloat16: |
| 4197 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4198 | convOp = spv::OpConvertUToF; |
| 4199 | break; |
| 4200 | |
| 4201 | case glslang::EOpConvDoubleToFloat: |
| 4202 | case glslang::EOpConvFloatToDouble: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4203 | #ifdef AMD_EXTENSIONS |
| 4204 | case glslang::EOpConvDoubleToFloat16: |
| 4205 | case glslang::EOpConvFloat16ToDouble: |
| 4206 | case glslang::EOpConvFloatToFloat16: |
| 4207 | case glslang::EOpConvFloat16ToFloat: |
| 4208 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4209 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 4210 | if (builder.isMatrixType(destType)) |
| 4211 | return createUnaryMatrixOperation(convOp, precision, noContraction, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4212 | break; |
| 4213 | |
| 4214 | case glslang::EOpConvFloatToInt: |
| 4215 | case glslang::EOpConvDoubleToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4216 | case glslang::EOpConvFloatToInt64: |
| 4217 | case glslang::EOpConvDoubleToInt64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4218 | #ifdef AMD_EXTENSIONS |
| 4219 | case glslang::EOpConvFloat16ToInt: |
| 4220 | case glslang::EOpConvFloat16ToInt64: |
| 4221 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4222 | convOp = spv::OpConvertFToS; |
| 4223 | break; |
| 4224 | |
| 4225 | case glslang::EOpConvUintToInt: |
| 4226 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4227 | case glslang::EOpConvUint64ToInt64: |
| 4228 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 4229 | if (builder.isInSpecConstCodeGenMode()) { |
| 4230 | // Build zero scalar or vector for OpIAdd. |
Rex Xu | 64bcfdb | 2016-09-05 16:10:14 +0800 | [diff] [blame] | 4231 | zero = (op == glslang::EOpConvUint64ToInt64 || |
| 4232 | op == glslang::EOpConvInt64ToUint64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0); |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 4233 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 4234 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 4235 | // generating for OpSpecConstantOp instruction. |
| 4236 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 4237 | } |
| 4238 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4239 | convOp = spv::OpBitcast; |
| 4240 | break; |
| 4241 | |
| 4242 | case glslang::EOpConvFloatToUint: |
| 4243 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4244 | case glslang::EOpConvFloatToUint64: |
| 4245 | case glslang::EOpConvDoubleToUint64: |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4246 | #ifdef AMD_EXTENSIONS |
| 4247 | case glslang::EOpConvFloat16ToUint: |
| 4248 | case glslang::EOpConvFloat16ToUint64: |
| 4249 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4250 | convOp = spv::OpConvertFToU; |
| 4251 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4252 | |
| 4253 | case glslang::EOpConvIntToInt64: |
| 4254 | case glslang::EOpConvInt64ToInt: |
| 4255 | convOp = spv::OpSConvert; |
| 4256 | break; |
| 4257 | |
| 4258 | case glslang::EOpConvUintToUint64: |
| 4259 | case glslang::EOpConvUint64ToUint: |
| 4260 | convOp = spv::OpUConvert; |
| 4261 | break; |
| 4262 | |
| 4263 | case glslang::EOpConvIntToUint64: |
| 4264 | case glslang::EOpConvInt64ToUint: |
| 4265 | case glslang::EOpConvUint64ToInt: |
| 4266 | case glslang::EOpConvUintToInt64: |
| 4267 | // OpSConvert/OpUConvert + OpBitCast |
| 4268 | switch (op) { |
| 4269 | case glslang::EOpConvIntToUint64: |
| 4270 | convOp = spv::OpSConvert; |
| 4271 | type = builder.makeIntType(64); |
| 4272 | break; |
| 4273 | case glslang::EOpConvInt64ToUint: |
| 4274 | convOp = spv::OpSConvert; |
| 4275 | type = builder.makeIntType(32); |
| 4276 | break; |
| 4277 | case glslang::EOpConvUint64ToInt: |
| 4278 | convOp = spv::OpUConvert; |
| 4279 | type = builder.makeUintType(32); |
| 4280 | break; |
| 4281 | case glslang::EOpConvUintToInt64: |
| 4282 | convOp = spv::OpUConvert; |
| 4283 | type = builder.makeUintType(64); |
| 4284 | break; |
| 4285 | default: |
| 4286 | assert(0); |
| 4287 | break; |
| 4288 | } |
| 4289 | |
| 4290 | if (vectorSize > 0) |
| 4291 | type = builder.makeVectorType(type, vectorSize); |
| 4292 | |
| 4293 | operand = builder.createUnaryOp(convOp, type, operand); |
| 4294 | |
| 4295 | if (builder.isInSpecConstCodeGenMode()) { |
| 4296 | // Build zero scalar or vector for OpIAdd. |
| 4297 | zero = (op == glslang::EOpConvIntToUint64 || |
| 4298 | op == glslang::EOpConvUintToInt64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0); |
| 4299 | zero = makeSmearedConstant(zero, vectorSize); |
| 4300 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 4301 | // generating for OpSpecConstantOp instruction. |
| 4302 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 4303 | } |
| 4304 | // For normal run-time conversion instruction, use OpBitcast. |
| 4305 | convOp = spv::OpBitcast; |
| 4306 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4307 | default: |
| 4308 | break; |
| 4309 | } |
| 4310 | |
| 4311 | spv::Id result = 0; |
| 4312 | if (convOp == spv::OpNop) |
| 4313 | return result; |
| 4314 | |
| 4315 | if (convOp == spv::OpSelect) { |
| 4316 | zero = makeSmearedConstant(zero, vectorSize); |
| 4317 | one = makeSmearedConstant(one, vectorSize); |
| 4318 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 4319 | } else |
| 4320 | result = builder.createUnaryOp(convOp, destType, operand); |
| 4321 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4322 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 4326 | { |
| 4327 | if (vectorSize == 0) |
| 4328 | return constant; |
| 4329 | |
| 4330 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 4331 | std::vector<spv::Id> components; |
| 4332 | for (int c = 0; c < vectorSize; ++c) |
| 4333 | components.push_back(constant); |
| 4334 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 4335 | } |
| 4336 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4337 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4338 | 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] | 4339 | { |
| 4340 | spv::Op opCode = spv::OpNop; |
| 4341 | |
| 4342 | switch (op) { |
| 4343 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4344 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4345 | opCode = spv::OpAtomicIAdd; |
| 4346 | break; |
| 4347 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4348 | case glslang::EOpImageAtomicMin: |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4349 | opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4350 | break; |
| 4351 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4352 | case glslang::EOpImageAtomicMax: |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4353 | opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4354 | break; |
| 4355 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4356 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4357 | opCode = spv::OpAtomicAnd; |
| 4358 | break; |
| 4359 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4360 | case glslang::EOpImageAtomicOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4361 | opCode = spv::OpAtomicOr; |
| 4362 | break; |
| 4363 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4364 | case glslang::EOpImageAtomicXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4365 | opCode = spv::OpAtomicXor; |
| 4366 | break; |
| 4367 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4368 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4369 | opCode = spv::OpAtomicExchange; |
| 4370 | break; |
| 4371 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 4372 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4373 | opCode = spv::OpAtomicCompareExchange; |
| 4374 | break; |
| 4375 | case glslang::EOpAtomicCounterIncrement: |
| 4376 | opCode = spv::OpAtomicIIncrement; |
| 4377 | break; |
| 4378 | case glslang::EOpAtomicCounterDecrement: |
| 4379 | opCode = spv::OpAtomicIDecrement; |
| 4380 | break; |
| 4381 | case glslang::EOpAtomicCounter: |
| 4382 | opCode = spv::OpAtomicLoad; |
| 4383 | break; |
| 4384 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4385 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4386 | break; |
| 4387 | } |
| 4388 | |
| 4389 | // Sort out the operands |
| 4390 | // - mapping from glslang -> SPV |
| 4391 | // - there are extra SPV operands with no glslang source |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4392 | // - compare-exchange swaps the value and comparator |
| 4393 | // - compare-exchange has an extra memory semantics |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4394 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 4395 | auto opIt = operands.begin(); // walk the glslang operands |
| 4396 | spvAtomicOperands.push_back(*(opIt++)); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4397 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::ScopeDevice)); // TBD: what is the correct scope? |
| 4398 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); // TBD: what are the correct memory semantics? |
| 4399 | if (opCode == spv::OpAtomicCompareExchange) { |
Rex Xu | bba5c80 | 2015-09-16 13:20:37 +0800 | [diff] [blame] | 4400 | // There are 2 memory semantics for compare-exchange. And the operand order of "comparator" and "new value" in GLSL |
| 4401 | // differs from that in SPIR-V. Hence, special processing is required. |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4402 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4403 | spvAtomicOperands.push_back(*(opIt + 1)); |
| 4404 | spvAtomicOperands.push_back(*opIt); |
| 4405 | opIt += 2; |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 4406 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 4407 | |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 4408 | // 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] | 4409 | for (; opIt != operands.end(); ++opIt) |
| 4410 | spvAtomicOperands.push_back(*opIt); |
| 4411 | |
| 4412 | return builder.createOp(opCode, typeId, spvAtomicOperands); |
| 4413 | } |
| 4414 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4415 | // Create group invocation operations. |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4416 | 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] | 4417 | { |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4418 | #ifdef AMD_EXTENSIONS |
Jamie Madill | 57cb69a | 2016-11-09 13:49:24 -0500 | [diff] [blame] | 4419 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4420 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4421 | #endif |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4422 | |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4423 | spv::Op opCode = spv::OpNop; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4424 | std::vector<spv::Id> spvGroupOperands; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4425 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 4426 | |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4427 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 4428 | op == glslang::EOpReadInvocation) { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4429 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 4430 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4431 | } else if (op == glslang::EOpAnyInvocation || |
| 4432 | op == glslang::EOpAllInvocations || |
| 4433 | op == glslang::EOpAllInvocationsEqual) { |
| 4434 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 4435 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4436 | } else { |
| 4437 | builder.addCapability(spv::CapabilityGroups); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 4438 | #ifdef AMD_EXTENSIONS |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 4439 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 4440 | op == glslang::EOpMaxInvocationsNonUniform || |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4441 | op == glslang::EOpAddInvocationsNonUniform || |
| 4442 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 4443 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 4444 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 4445 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 4446 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 4447 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
Rex Xu | 17ff343 | 2016-10-14 17:41:45 +0800 | [diff] [blame] | 4448 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
David Neto | bb5c02f | 2016-10-19 10:16:29 -0400 | [diff] [blame] | 4449 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4450 | |
| 4451 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4452 | #ifdef AMD_EXTENSIONS |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4453 | switch (op) { |
| 4454 | case glslang::EOpMinInvocations: |
| 4455 | case glslang::EOpMaxInvocations: |
| 4456 | case glslang::EOpAddInvocations: |
| 4457 | case glslang::EOpMinInvocationsNonUniform: |
| 4458 | case glslang::EOpMaxInvocationsNonUniform: |
| 4459 | case glslang::EOpAddInvocationsNonUniform: |
| 4460 | groupOperation = spv::GroupOperationReduce; |
| 4461 | spvGroupOperands.push_back(groupOperation); |
| 4462 | break; |
| 4463 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4464 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4465 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4466 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4467 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4468 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4469 | groupOperation = spv::GroupOperationInclusiveScan; |
| 4470 | spvGroupOperands.push_back(groupOperation); |
| 4471 | break; |
| 4472 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4473 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4474 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4475 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4476 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4477 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 4478 | groupOperation = spv::GroupOperationExclusiveScan; |
| 4479 | spvGroupOperands.push_back(groupOperation); |
| 4480 | break; |
Mike Weiblen | 4e9e400 | 2017-01-20 13:34:10 -0700 | [diff] [blame] | 4481 | default: |
| 4482 | break; |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4483 | } |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4484 | #endif |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) |
| 4488 | spvGroupOperands.push_back(*opIt); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4489 | |
| 4490 | switch (op) { |
| 4491 | case glslang::EOpAnyInvocation: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4492 | opCode = spv::OpSubgroupAnyKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4493 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4494 | case glslang::EOpAllInvocations: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4495 | opCode = spv::OpSubgroupAllKHR; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4496 | break; |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4497 | case glslang::EOpAllInvocationsEqual: |
Ashwin Kolhe | c720f3e | 2017-01-18 14:16:49 -0800 | [diff] [blame] | 4498 | opCode = spv::OpSubgroupAllEqualKHR; |
| 4499 | break; |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4500 | case glslang::EOpReadInvocation: |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4501 | opCode = spv::OpSubgroupReadInvocationKHR; |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4502 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4503 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4504 | break; |
| 4505 | case glslang::EOpReadFirstInvocation: |
| 4506 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 4507 | break; |
| 4508 | case glslang::EOpBallot: |
| 4509 | { |
| 4510 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 4511 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 4512 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 4513 | // |
| 4514 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 4515 | // |
| 4516 | spv::Id uintType = builder.makeUintType(32); |
| 4517 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 4518 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 4519 | |
| 4520 | std::vector<spv::Id> components; |
| 4521 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 4522 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 4523 | |
| 4524 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 4525 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 4526 | builder.createCompositeConstruct(uvec2Type, components)); |
| 4527 | } |
| 4528 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4529 | #ifdef AMD_EXTENSIONS |
| 4530 | case glslang::EOpMinInvocations: |
| 4531 | case glslang::EOpMaxInvocations: |
| 4532 | case glslang::EOpAddInvocations: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4533 | case glslang::EOpMinInvocationsInclusiveScan: |
| 4534 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 4535 | case glslang::EOpAddInvocationsInclusiveScan: |
| 4536 | case glslang::EOpMinInvocationsExclusiveScan: |
| 4537 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 4538 | case glslang::EOpAddInvocationsExclusiveScan: |
| 4539 | if (op == glslang::EOpMinInvocations || |
| 4540 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 4541 | op == glslang::EOpMinInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4542 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4543 | opCode = spv::OpGroupFMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4544 | else { |
| 4545 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4546 | opCode = spv::OpGroupUMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4547 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4548 | opCode = spv::OpGroupSMin; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4549 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4550 | } else if (op == glslang::EOpMaxInvocations || |
| 4551 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 4552 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4553 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4554 | opCode = spv::OpGroupFMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4555 | else { |
| 4556 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4557 | opCode = spv::OpGroupUMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4558 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4559 | opCode = spv::OpGroupSMax; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4560 | } |
| 4561 | } else { |
| 4562 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4563 | opCode = spv::OpGroupFAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4564 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4565 | opCode = spv::OpGroupIAdd; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4566 | } |
| 4567 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4568 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4569 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4570 | |
| 4571 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4572 | case glslang::EOpMinInvocationsNonUniform: |
| 4573 | case glslang::EOpMaxInvocationsNonUniform: |
| 4574 | case glslang::EOpAddInvocationsNonUniform: |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4575 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 4576 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 4577 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 4578 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 4579 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 4580 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 4581 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 4582 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 4583 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4584 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4585 | opCode = spv::OpGroupFMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4586 | else { |
| 4587 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4588 | opCode = spv::OpGroupUMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4589 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4590 | opCode = spv::OpGroupSMinNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4591 | } |
| 4592 | } |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4593 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 4594 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 4595 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4596 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4597 | opCode = spv::OpGroupFMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4598 | else { |
| 4599 | if (isUnsigned) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4600 | opCode = spv::OpGroupUMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4601 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4602 | opCode = spv::OpGroupSMaxNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | else { |
| 4606 | if (isFloat) |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4607 | opCode = spv::OpGroupFAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4608 | else |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4609 | opCode = spv::OpGroupIAddNonUniformAMD; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4610 | } |
| 4611 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4612 | if (builder.isVectorType(typeId)) |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4613 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4614 | |
| 4615 | break; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4616 | #endif |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4617 | default: |
| 4618 | logger->missingFunctionality("invocation operation"); |
| 4619 | return spv::NoResult; |
| 4620 | } |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4621 | |
| 4622 | assert(opCode != spv::OpNop); |
| 4623 | return builder.createOp(opCode, typeId, spvGroupOperands); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 4624 | } |
| 4625 | |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4626 | // Create group invocation operations on a vector |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4627 | 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] | 4628 | { |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4629 | #ifdef AMD_EXTENSIONS |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4630 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 4631 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4632 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4633 | op == spv::OpSubgroupReadInvocationKHR || |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4634 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || op == spv::OpGroupSMinNonUniformAMD || |
| 4635 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || op == spv::OpGroupSMaxNonUniformAMD || |
| 4636 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4637 | #else |
| 4638 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 4639 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4640 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 4641 | op == spv::OpSubgroupReadInvocationKHR); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4642 | #endif |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4643 | |
| 4644 | // Handle group invocation operations scalar by scalar. |
| 4645 | // The result type is the same type as the original type. |
| 4646 | // The algorithm is to: |
| 4647 | // - break the vector into scalars |
| 4648 | // - apply the operation to each scalar |
| 4649 | // - make a vector out the scalar results |
| 4650 | |
| 4651 | // get the types sorted out |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4652 | int numComponents = builder.getNumComponents(operands[0]); |
| 4653 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4654 | std::vector<spv::Id> results; |
| 4655 | |
| 4656 | // do each scalar op |
| 4657 | for (int comp = 0; comp < numComponents; ++comp) { |
| 4658 | std::vector<unsigned int> indexes; |
| 4659 | indexes.push_back(comp); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4660 | spv::Id scalar = builder.createCompositeExtract(operands[0], scalarType, indexes); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4661 | std::vector<spv::Id> spvGroupOperands; |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4662 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 4663 | spvGroupOperands.push_back(scalar); |
| 4664 | spvGroupOperands.push_back(operands[1]); |
| 4665 | } else if (op == spv::OpGroupBroadcast) { |
| 4666 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4667 | spvGroupOperands.push_back(scalar); |
| 4668 | spvGroupOperands.push_back(operands[1]); |
| 4669 | } else { |
chaoc | f200da8 | 2016-12-20 12:44:35 -0800 | [diff] [blame] | 4670 | spvGroupOperands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
Rex Xu | 430ef40 | 2016-10-14 17:22:23 +0800 | [diff] [blame] | 4671 | spvGroupOperands.push_back(groupOperation); |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4672 | spvGroupOperands.push_back(scalar); |
| 4673 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4674 | |
Rex Xu | b707205 | 2016-09-26 15:53:40 +0800 | [diff] [blame] | 4675 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | // put the pieces together |
| 4679 | return builder.createCompositeConstruct(typeId, results); |
| 4680 | } |
Rex Xu | 2bbbe06 | 2016-08-23 15:41:05 +0800 | [diff] [blame] | 4681 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4682 | 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] | 4683 | { |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4684 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4685 | #ifdef AMD_EXTENSIONS |
| 4686 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble || typeProxy == glslang::EbtFloat16; |
| 4687 | #else |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4688 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 4689 | #endif |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4690 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4691 | spv::Op opCode = spv::OpNop; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4692 | int extBuiltins = -1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4693 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 4694 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4695 | spv::Id typeId0 = 0; |
| 4696 | if (consumedOperands > 0) |
| 4697 | typeId0 = builder.getTypeId(operands[0]); |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 4698 | spv::Id typeId1 = 0; |
| 4699 | if (consumedOperands > 1) |
| 4700 | typeId1 = builder.getTypeId(operands[1]); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4701 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4702 | |
| 4703 | switch (op) { |
| 4704 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4705 | if (isFloat) |
| 4706 | libCall = spv::GLSLstd450FMin; |
| 4707 | else if (isUnsigned) |
| 4708 | libCall = spv::GLSLstd450UMin; |
| 4709 | else |
| 4710 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 4711 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4712 | break; |
| 4713 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4714 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4715 | break; |
| 4716 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4717 | if (isFloat) |
| 4718 | libCall = spv::GLSLstd450FMax; |
| 4719 | else if (isUnsigned) |
| 4720 | libCall = spv::GLSLstd450UMax; |
| 4721 | else |
| 4722 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 4723 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4724 | break; |
| 4725 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4726 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4727 | break; |
| 4728 | case glslang::EOpDot: |
| 4729 | opCode = spv::OpDot; |
| 4730 | break; |
| 4731 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4732 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4733 | break; |
| 4734 | |
| 4735 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4736 | if (isFloat) |
| 4737 | libCall = spv::GLSLstd450FClamp; |
| 4738 | else if (isUnsigned) |
| 4739 | libCall = spv::GLSLstd450UClamp; |
| 4740 | else |
| 4741 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 4742 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 4743 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4744 | break; |
| 4745 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 4746 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 4747 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4748 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 4749 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4750 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 4751 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4752 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 4753 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4754 | break; |
| 4755 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4756 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 4757 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4758 | break; |
| 4759 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4760 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 4761 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 4762 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4763 | break; |
| 4764 | |
| 4765 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4766 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4767 | break; |
| 4768 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4769 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4770 | break; |
| 4771 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4772 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4773 | break; |
| 4774 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4775 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4776 | break; |
| 4777 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4778 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4779 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4780 | case glslang::EOpInterpolateAtSample: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4781 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4782 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 4783 | break; |
| 4784 | case glslang::EOpInterpolateAtOffset: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 4785 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 4786 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 4787 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4788 | case glslang::EOpAddCarry: |
| 4789 | opCode = spv::OpIAddCarry; |
| 4790 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 4791 | consumedOperands = 2; |
| 4792 | break; |
| 4793 | case glslang::EOpSubBorrow: |
| 4794 | opCode = spv::OpISubBorrow; |
| 4795 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 4796 | consumedOperands = 2; |
| 4797 | break; |
| 4798 | case glslang::EOpUMulExtended: |
| 4799 | opCode = spv::OpUMulExtended; |
| 4800 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 4801 | consumedOperands = 2; |
| 4802 | break; |
| 4803 | case glslang::EOpIMulExtended: |
| 4804 | opCode = spv::OpSMulExtended; |
| 4805 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 4806 | consumedOperands = 2; |
| 4807 | break; |
| 4808 | case glslang::EOpBitfieldExtract: |
| 4809 | if (isUnsigned) |
| 4810 | opCode = spv::OpBitFieldUExtract; |
| 4811 | else |
| 4812 | opCode = spv::OpBitFieldSExtract; |
| 4813 | break; |
| 4814 | case glslang::EOpBitfieldInsert: |
| 4815 | opCode = spv::OpBitFieldInsert; |
| 4816 | break; |
| 4817 | |
| 4818 | case glslang::EOpFma: |
| 4819 | libCall = spv::GLSLstd450Fma; |
| 4820 | break; |
| 4821 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 4822 | { |
| 4823 | libCall = spv::GLSLstd450FrexpStruct; |
| 4824 | assert(builder.isPointerType(typeId1)); |
| 4825 | typeId1 = builder.getContainedTypeId(typeId1); |
| 4826 | #ifdef AMD_EXTENSIONS |
| 4827 | int width = builder.getScalarTypeWidth(typeId1); |
| 4828 | #else |
| 4829 | int width = 32; |
| 4830 | #endif |
| 4831 | if (builder.getNumComponents(operands[0]) == 1) |
| 4832 | frexpIntType = builder.makeIntegerType(width, true); |
| 4833 | else |
| 4834 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), builder.getNumComponents(operands[0])); |
| 4835 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 4836 | consumedOperands = 1; |
| 4837 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4838 | break; |
| 4839 | case glslang::EOpLdexp: |
| 4840 | libCall = spv::GLSLstd450Ldexp; |
| 4841 | break; |
| 4842 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 4843 | case glslang::EOpReadInvocation: |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 4844 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 4845 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4846 | #ifdef AMD_EXTENSIONS |
| 4847 | case glslang::EOpSwizzleInvocations: |
| 4848 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 4849 | libCall = spv::SwizzleInvocationsAMD; |
| 4850 | break; |
| 4851 | case glslang::EOpSwizzleInvocationsMasked: |
| 4852 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 4853 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 4854 | break; |
| 4855 | case glslang::EOpWriteInvocation: |
| 4856 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 4857 | libCall = spv::WriteInvocationAMD; |
| 4858 | break; |
| 4859 | |
| 4860 | case glslang::EOpMin3: |
| 4861 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 4862 | if (isFloat) |
| 4863 | libCall = spv::FMin3AMD; |
| 4864 | else { |
| 4865 | if (isUnsigned) |
| 4866 | libCall = spv::UMin3AMD; |
| 4867 | else |
| 4868 | libCall = spv::SMin3AMD; |
| 4869 | } |
| 4870 | break; |
| 4871 | case glslang::EOpMax3: |
| 4872 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 4873 | if (isFloat) |
| 4874 | libCall = spv::FMax3AMD; |
| 4875 | else { |
| 4876 | if (isUnsigned) |
| 4877 | libCall = spv::UMax3AMD; |
| 4878 | else |
| 4879 | libCall = spv::SMax3AMD; |
| 4880 | } |
| 4881 | break; |
| 4882 | case glslang::EOpMid3: |
| 4883 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 4884 | if (isFloat) |
| 4885 | libCall = spv::FMid3AMD; |
| 4886 | else { |
| 4887 | if (isUnsigned) |
| 4888 | libCall = spv::UMid3AMD; |
| 4889 | else |
| 4890 | libCall = spv::SMid3AMD; |
| 4891 | } |
| 4892 | break; |
| 4893 | |
| 4894 | case glslang::EOpInterpolateAtVertex: |
| 4895 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 4896 | libCall = spv::InterpolateAtVertexAMD; |
| 4897 | break; |
| 4898 | #endif |
| 4899 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4900 | default: |
| 4901 | return 0; |
| 4902 | } |
| 4903 | |
| 4904 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 4905 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 4906 | // Use an extended instruction from the standard library. |
| 4907 | // Construct the call arguments, without modifying the original operands vector. |
| 4908 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 4909 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4910 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 4911 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4912 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4913 | case 0: |
| 4914 | // should all be handled by visitAggregate and createNoArgOperation |
| 4915 | assert(0); |
| 4916 | return 0; |
| 4917 | case 1: |
| 4918 | // should all be handled by createUnaryOperation |
| 4919 | assert(0); |
| 4920 | return 0; |
| 4921 | case 2: |
| 4922 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 4923 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4924 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4925 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 4926 | assert(consumedOperands == operands.size()); |
| 4927 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4928 | break; |
| 4929 | } |
| 4930 | } |
| 4931 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4932 | // Decode the return types that were structures |
| 4933 | switch (op) { |
| 4934 | case glslang::EOpAddCarry: |
| 4935 | case glslang::EOpSubBorrow: |
| 4936 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 4937 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 4938 | break; |
| 4939 | case glslang::EOpUMulExtended: |
| 4940 | case glslang::EOpIMulExtended: |
| 4941 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 4942 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 4943 | break; |
| 4944 | case glslang::EOpFrexp: |
Rex Xu | 470026f | 2017-03-29 17:12:40 +0800 | [diff] [blame] | 4945 | { |
| 4946 | assert(operands.size() == 2); |
| 4947 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 4948 | // "exp" is floating-point type (from HLSL intrinsic) |
| 4949 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 4950 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 4951 | builder.createStore(member1, operands[1]); |
| 4952 | } else |
| 4953 | // "exp" is integer type (from GLSL built-in function) |
| 4954 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 4955 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 4956 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4957 | break; |
| 4958 | default: |
| 4959 | break; |
| 4960 | } |
| 4961 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 4962 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4963 | } |
| 4964 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 4965 | // Intrinsics with no arguments (or no return value, and no precision). |
| 4966 | 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] | 4967 | { |
| 4968 | // TODO: get the barrier operands correct |
| 4969 | |
| 4970 | switch (op) { |
| 4971 | case glslang::EOpEmitVertex: |
| 4972 | builder.createNoResultOp(spv::OpEmitVertex); |
| 4973 | return 0; |
| 4974 | case glslang::EOpEndPrimitive: |
| 4975 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 4976 | return 0; |
| 4977 | case glslang::EOpBarrier: |
chrgau01@arm.com | c3f1cdf | 2016-11-14 10:10:05 +0100 | [diff] [blame] | 4978 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsMaskNone); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4979 | return 0; |
| 4980 | case glslang::EOpMemoryBarrier: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4981 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAllMemory); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4982 | return 0; |
| 4983 | case glslang::EOpMemoryBarrierAtomicCounter: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4984 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAtomicCounterMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4985 | return 0; |
| 4986 | case glslang::EOpMemoryBarrierBuffer: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4987 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4988 | return 0; |
| 4989 | case glslang::EOpMemoryBarrierImage: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 4990 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsImageMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4991 | return 0; |
| 4992 | case glslang::EOpMemoryBarrierShared: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4993 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsWorkgroupMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4994 | return 0; |
| 4995 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4996 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4997 | return 0; |
LoopDawg | 6e72fdd | 2016-06-15 09:50:24 -0600 | [diff] [blame] | 4998 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 4999 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5000 | builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsAllMemory); |
| 5001 | return 0; |
| 5002 | case glslang::EOpGroupMemoryBarrierWithGroupSync: |
| 5003 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5004 | builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask); |
| 5005 | return 0; |
| 5006 | case glslang::EOpWorkgroupMemoryBarrier: |
| 5007 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask); |
| 5008 | return 0; |
| 5009 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
| 5010 | // Control barrier with non-"None" semantic is also a memory barrier. |
| 5011 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask); |
| 5012 | return 0; |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5013 | #ifdef AMD_EXTENSIONS |
| 5014 | case glslang::EOpTime: |
| 5015 | { |
| 5016 | std::vector<spv::Id> args; // Dummy arguments |
| 5017 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 5018 | return builder.setPrecision(id, precision); |
| 5019 | } |
| 5020 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5021 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5022 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5023 | return 0; |
| 5024 | } |
| 5025 | } |
| 5026 | |
| 5027 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 5028 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 5029 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5030 | spv::Id id; |
| 5031 | if (symbolValues.end() != iter) { |
| 5032 | id = iter->second; |
| 5033 | return id; |
| 5034 | } |
| 5035 | |
| 5036 | // it was not found, create it |
| 5037 | id = createSpvVariable(symbol); |
| 5038 | symbolValues[symbol->getId()] = id; |
| 5039 | |
Rex Xu | c884b4a | 2016-06-29 15:03:44 +0800 | [diff] [blame] | 5040 | if (symbol->getBasicType() != glslang::EbtBlock) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5041 | addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 5042 | addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
Rex Xu | bbceed7 | 2016-05-21 09:40:44 +0800 | [diff] [blame] | 5043 | addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5044 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
| 5045 | addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5046 | if (symbol->getQualifier().hasIndex()) |
| 5047 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 5048 | if (symbol->getQualifier().hasComponent()) |
| 5049 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 5050 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5051 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5052 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5053 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5054 | if (symbol->getQualifier().hasXfbBuffer()) |
| 5055 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 5056 | if (symbol->getQualifier().hasXfbOffset()) |
| 5057 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
| 5058 | } |
John Kessenich | 91e4aa5 | 2016-07-07 17:46:42 -0600 | [diff] [blame] | 5059 | // atomic counters use this: |
| 5060 | if (symbol->getQualifier().hasOffset()) |
| 5061 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5062 | } |
| 5063 | |
scygan | 2c86427 | 2016-05-18 18:09:17 +0200 | [diff] [blame] | 5064 | if (symbol->getQualifier().hasLocation()) |
| 5065 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 5066 | addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 5067 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5068 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5069 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5070 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5071 | if (symbol->getQualifier().hasSet()) |
| 5072 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5073 | else if (IsDescriptorResource(symbol->getType())) { |
| 5074 | // default to 0 |
| 5075 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 5076 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5077 | if (symbol->getQualifier().hasBinding()) |
| 5078 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5079 | if (symbol->getQualifier().hasAttachment()) |
| 5080 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5081 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5082 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5083 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 5084 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5085 | if (symbol->getQualifier().hasXfbBuffer()) |
| 5086 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 5087 | } |
| 5088 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 5089 | if (symbol->getType().isImage()) { |
| 5090 | std::vector<spv::Decoration> memory; |
| 5091 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory); |
| 5092 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 5093 | addDecoration(id, memory[i]); |
| 5094 | } |
| 5095 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5096 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 5097 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5098 | if (builtIn != spv::BuiltInMax) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5099 | addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5100 | |
John Kessenich | ecba76f | 2017-01-06 00:34:48 -0700 | [diff] [blame] | 5101 | #ifdef NV_EXTENSIONS |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5102 | if (builtIn == spv::BuiltInSampleMask) { |
| 5103 | spv::Decoration decoration; |
| 5104 | // GL_NV_sample_mask_override_coverage extension |
| 5105 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5106 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5107 | else |
| 5108 | decoration = (spv::Decoration)spv::DecorationMax; |
| 5109 | addDecoration(id, decoration); |
| 5110 | if (decoration != spv::DecorationMax) { |
| 5111 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 5112 | } |
| 5113 | } |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5114 | else if (builtIn == spv::BuiltInLayer) { |
| 5115 | // SPV_NV_viewport_array2 extension |
| 5116 | if (symbol->getQualifier().layoutViewportRelative) |
| 5117 | { |
| 5118 | addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 5119 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 5120 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 5121 | } |
| 5122 | if(symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) |
| 5123 | { |
| 5124 | addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
| 5125 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 5126 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 5127 | } |
| 5128 | } |
| 5129 | |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 5130 | if (symbol->getQualifier().layoutPassthrough) { |
chaoc | 771d89f | 2017-01-13 01:10:53 -0800 | [diff] [blame] | 5131 | addDecoration(id, spv::DecorationPassthroughNV); |
| 5132 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
chaoc | 6e5acae | 2016-12-20 13:28:52 -0800 | [diff] [blame] | 5133 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 5134 | } |
chaoc | 0ad6a4e | 2016-12-19 16:29:34 -0800 | [diff] [blame] | 5135 | #endif |
| 5136 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5137 | return id; |
| 5138 | } |
| 5139 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5140 | // If 'dec' is valid, add no-operand decoration to an object |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5141 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec) |
| 5142 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5143 | if (dec != spv::DecorationMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5144 | builder.addDecoration(id, dec); |
| 5145 | } |
| 5146 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5147 | // If 'dec' is valid, add a one-operand decoration to an object |
| 5148 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec, unsigned value) |
| 5149 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5150 | if (dec != spv::DecorationMax) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5151 | builder.addDecoration(id, dec, value); |
| 5152 | } |
| 5153 | |
| 5154 | // 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] | 5155 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec) |
| 5156 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5157 | if (dec != spv::DecorationMax) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5158 | builder.addMemberDecoration(id, (unsigned)member, dec); |
| 5159 | } |
| 5160 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5161 | // If 'dec' is valid, add a one-operand decoration to a struct member |
| 5162 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value) |
| 5163 | { |
John Kessenich | 4016e38 | 2016-07-15 11:53:56 -0600 | [diff] [blame] | 5164 | if (dec != spv::DecorationMax) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 5165 | builder.addMemberDecoration(id, (unsigned)member, dec, value); |
| 5166 | } |
| 5167 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5168 | // 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] | 5169 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5170 | // |
| 5171 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 5172 | // |
| 5173 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 5174 | // regular constants, which themselves are trees that createSpvConstant() |
| 5175 | // recursively walks. So, this function walks the "top" of the tree: |
| 5176 | // - emit specialization constant-building instructions for specConstant |
| 5177 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5178 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5179 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 5180 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5181 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5182 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5183 | if (! node.getQualifier().specConstant) { |
| 5184 | // hand off to the non-spec-constant path |
| 5185 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 5186 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5187 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5188 | nextConst, false); |
| 5189 | } |
| 5190 | |
| 5191 | // We now know we have a specialization constant to build |
| 5192 | |
John Kessenich | d94c003 | 2016-05-30 19:29:40 -0600 | [diff] [blame] | 5193 | // 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] | 5194 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 5195 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 5196 | std::vector<spv::Id> dimConstId; |
| 5197 | for (int dim = 0; dim < 3; ++dim) { |
| 5198 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 5199 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 5200 | if (specConst) |
| 5201 | addDecoration(dimConstId.back(), spv::DecorationSpecId, glslangIntermediate->getLocalSizeSpecId(dim)); |
| 5202 | } |
| 5203 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 5204 | } |
| 5205 | |
| 5206 | // An AST node labelled as specialization constant should be a symbol node. |
| 5207 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 5208 | if (auto* sn = node.getAsSymbolNode()) { |
| 5209 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 5210 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 5211 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 5212 | // will set the builder into spec constant op instruction generating mode. |
| 5213 | sub_tree->traverse(this); |
| 5214 | return accessChainLoad(sub_tree->getType()); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5215 | } else if (auto* const_union_array = &sn->getConstArray()){ |
| 5216 | int nextConst = 0; |
Endre Oma | ad58d45 | 2017-01-31 21:08:19 +0100 | [diff] [blame] | 5217 | spv::Id id = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
| 5218 | builder.addName(id, sn->getName().c_str()); |
| 5219 | return id; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 5220 | } |
| 5221 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5222 | |
| 5223 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 5224 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5225 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 5226 | exit(1); |
| 5227 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5228 | } |
| 5229 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5230 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 5231 | // build the aggregate SPIR-V constant. |
| 5232 | // |
| 5233 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 5234 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 5235 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5236 | 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] | 5237 | { |
| 5238 | // vector of constants for SPIR-V |
| 5239 | std::vector<spv::Id> spvConsts; |
| 5240 | |
| 5241 | // Type is used for struct and array constants |
| 5242 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 5243 | |
| 5244 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 5245 | glslang::TType elementType(glslangType, 0); |
| 5246 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5247 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5248 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 5249 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5250 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5251 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5252 | } else if (glslangType.getStruct()) { |
| 5253 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 5254 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 5255 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 8d72f1a | 2016-05-20 12:06:03 -0600 | [diff] [blame] | 5256 | } else if (glslangType.getVectorSize() > 1) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5257 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 5258 | bool zero = nextConst >= consts.size(); |
| 5259 | switch (glslangType.getBasicType()) { |
| 5260 | case glslang::EbtInt: |
| 5261 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 5262 | break; |
| 5263 | case glslang::EbtUint: |
| 5264 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 5265 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5266 | case glslang::EbtInt64: |
| 5267 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 5268 | break; |
| 5269 | case glslang::EbtUint64: |
| 5270 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 5271 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5272 | case glslang::EbtFloat: |
| 5273 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 5274 | break; |
| 5275 | case glslang::EbtDouble: |
| 5276 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 5277 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5278 | #ifdef AMD_EXTENSIONS |
| 5279 | case glslang::EbtFloat16: |
| 5280 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 5281 | break; |
| 5282 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5283 | case glslang::EbtBool: |
| 5284 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 5285 | break; |
| 5286 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5287 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5288 | break; |
| 5289 | } |
| 5290 | ++nextConst; |
| 5291 | } |
| 5292 | } else { |
| 5293 | // we have a non-aggregate (scalar) constant |
| 5294 | bool zero = nextConst >= consts.size(); |
| 5295 | spv::Id scalar = 0; |
| 5296 | switch (glslangType.getBasicType()) { |
| 5297 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5298 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5299 | break; |
| 5300 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5301 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5302 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 5303 | case glslang::EbtInt64: |
| 5304 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 5305 | break; |
| 5306 | case glslang::EbtUint64: |
| 5307 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 5308 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5309 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5310 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5311 | break; |
| 5312 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5313 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5314 | break; |
Rex Xu | c9e3c3c | 2016-07-29 16:00:05 +0800 | [diff] [blame] | 5315 | #ifdef AMD_EXTENSIONS |
| 5316 | case glslang::EbtFloat16: |
| 5317 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 5318 | break; |
| 5319 | #endif |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5320 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5321 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5322 | break; |
| 5323 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5324 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5325 | break; |
| 5326 | } |
| 5327 | ++nextConst; |
| 5328 | return scalar; |
| 5329 | } |
| 5330 | |
| 5331 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 5332 | } |
| 5333 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5334 | // Return true if the node is a constant or symbol whose reading has no |
| 5335 | // non-trivial observable cost or effect. |
| 5336 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 5337 | { |
| 5338 | // don't know what this is |
| 5339 | if (node == nullptr) |
| 5340 | return false; |
| 5341 | |
| 5342 | // a constant is safe |
| 5343 | if (node->getAsConstantUnion() != nullptr) |
| 5344 | return true; |
| 5345 | |
| 5346 | // not a symbol means non-trivial |
| 5347 | if (node->getAsSymbolNode() == nullptr) |
| 5348 | return false; |
| 5349 | |
| 5350 | // a symbol, depends on what's being read |
| 5351 | switch (node->getType().getQualifier().storage) { |
| 5352 | case glslang::EvqTemporary: |
| 5353 | case glslang::EvqGlobal: |
| 5354 | case glslang::EvqIn: |
| 5355 | case glslang::EvqInOut: |
| 5356 | case glslang::EvqConst: |
| 5357 | case glslang::EvqConstReadOnly: |
| 5358 | case glslang::EvqUniform: |
| 5359 | return true; |
| 5360 | default: |
| 5361 | return false; |
| 5362 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 5363 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5364 | |
| 5365 | // A node is trivial if it is a single operation with no side effects. |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 5366 | // Vector results seem ill-defined, currently classifying them as trivial too, |
| 5367 | // to avoid scalar bool-based control-flow logic. |
| 5368 | // Otherwise, error on the side of saying non-trivial. |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5369 | // Return true if trivial. |
| 5370 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 5371 | { |
| 5372 | if (node == nullptr) |
| 5373 | return false; |
| 5374 | |
John Kessenich | 0d2b471 | 2017-05-19 20:19:00 -0600 | [diff] [blame] | 5375 | // count vectors as trivial |
| 5376 | if (node->getType().isVector()) |
| 5377 | return true; |
| 5378 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5379 | // symbols and constants are trivial |
| 5380 | if (isTrivialLeaf(node)) |
| 5381 | return true; |
| 5382 | |
| 5383 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 5384 | |
| 5385 | // not a simple operation |
| 5386 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 5387 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 5388 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 5389 | return false; |
| 5390 | |
| 5391 | // not on leaf nodes |
| 5392 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 5393 | return false; |
| 5394 | |
| 5395 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 5396 | return false; |
| 5397 | } |
| 5398 | |
| 5399 | switch (node->getAsOperator()->getOp()) { |
| 5400 | case glslang::EOpLogicalNot: |
| 5401 | case glslang::EOpConvIntToBool: |
| 5402 | case glslang::EOpConvUintToBool: |
| 5403 | case glslang::EOpConvFloatToBool: |
| 5404 | case glslang::EOpConvDoubleToBool: |
| 5405 | case glslang::EOpEqual: |
| 5406 | case glslang::EOpNotEqual: |
| 5407 | case glslang::EOpLessThan: |
| 5408 | case glslang::EOpGreaterThan: |
| 5409 | case glslang::EOpLessThanEqual: |
| 5410 | case glslang::EOpGreaterThanEqual: |
| 5411 | case glslang::EOpIndexDirect: |
| 5412 | case glslang::EOpIndexDirectStruct: |
| 5413 | case glslang::EOpLogicalXor: |
| 5414 | case glslang::EOpAny: |
| 5415 | case glslang::EOpAll: |
| 5416 | return true; |
| 5417 | default: |
| 5418 | return false; |
| 5419 | } |
| 5420 | } |
| 5421 | |
| 5422 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 5423 | // the left side is true (for &&) or false (for ||). |
| 5424 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 5425 | { |
| 5426 | spv::Id boolTypeId = builder.makeBoolType(); |
| 5427 | |
| 5428 | // emit left operand |
| 5429 | builder.clearAccessChain(); |
| 5430 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 5431 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5432 | |
| 5433 | // Operands to accumulate OpPhi operands |
| 5434 | std::vector<spv::Id> phiOperands; |
| 5435 | // accumulate left operand's phi information |
| 5436 | phiOperands.push_back(leftId); |
| 5437 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 5438 | |
| 5439 | // Make the two kinds of operation symmetric with a "!" |
| 5440 | // || => emit "if (! left) result = right" |
| 5441 | // && => emit "if ( left) result = right" |
| 5442 | // |
| 5443 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 5444 | // to 'builder' to have an "else" without an "then" |
| 5445 | if (op == glslang::EOpLogicalOr) |
| 5446 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 5447 | |
| 5448 | // make an "if" based on the left value |
| 5449 | spv::Builder::If ifBuilder(leftId, builder); |
| 5450 | |
| 5451 | // emit right operand as the "then" part of the "if" |
| 5452 | builder.clearAccessChain(); |
| 5453 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 5454 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 5455 | |
| 5456 | // accumulate left operand's phi information |
| 5457 | phiOperands.push_back(rightId); |
| 5458 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 5459 | |
| 5460 | // finish the "if" |
| 5461 | ifBuilder.makeEndIf(); |
| 5462 | |
| 5463 | // phi together the two results |
| 5464 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 5465 | } |
| 5466 | |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5467 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 5468 | // Import this set if it has not been imported yet. |
| 5469 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 5470 | { |
| 5471 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 5472 | return extBuiltinMap[name]; |
| 5473 | else { |
Rex Xu | 5159664 | 2016-09-21 18:56:12 +0800 | [diff] [blame] | 5474 | builder.addExtension(name); |
Rex Xu | 9d93a23 | 2016-05-05 12:30:44 +0800 | [diff] [blame] | 5475 | spv::Id extBuiltins = builder.import(name); |
| 5476 | extBuiltinMap[name] = extBuiltins; |
| 5477 | return extBuiltins; |
| 5478 | } |
| 5479 | } |
| 5480 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5481 | }; // end anonymous namespace |
| 5482 | |
| 5483 | namespace glslang { |
| 5484 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5485 | void GetSpirvVersion(std::string& version) |
| 5486 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 5487 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 5488 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 5489 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5490 | version = buf; |
| 5491 | } |
| 5492 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5493 | // Write SPIR-V out to a binary file |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5494 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5495 | { |
| 5496 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 5497 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 5498 | if (out.fail()) |
| 5499 | printf("ERROR: Failed to open file: %s\n", baseName); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5500 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 5501 | unsigned int word = spirv[i]; |
| 5502 | out.write((const char*)&word, 4); |
| 5503 | } |
| 5504 | out.close(); |
| 5505 | } |
| 5506 | |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5507 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
Flavio | aea3c89 | 2017-02-06 11:46:35 -0800 | [diff] [blame] | 5508 | 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] | 5509 | { |
| 5510 | std::ofstream out; |
| 5511 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 8f674e8 | 2017-02-18 09:45:40 -0700 | [diff] [blame] | 5512 | if (out.fail()) |
| 5513 | printf("ERROR: Failed to open file: %s\n", baseName); |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5514 | out << "\t// " GLSLANG_REVISION " " GLSLANG_DATE << std::endl; |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 5515 | if (varName != nullptr) { |
| 5516 | out << "\t #pragma once" << std::endl; |
| 5517 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 5518 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5519 | const int WORDS_PER_LINE = 8; |
| 5520 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 5521 | out << "\t"; |
| 5522 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 5523 | const unsigned int word = spirv[i + j]; |
| 5524 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 5525 | if (i + j + 1 < (int)spirv.size()) { |
| 5526 | out << ","; |
| 5527 | } |
| 5528 | } |
| 5529 | out << std::endl; |
| 5530 | } |
Flavio | 15017db | 2017-02-15 14:29:33 -0800 | [diff] [blame] | 5531 | if (varName != nullptr) { |
| 5532 | out << "};"; |
| 5533 | } |
Johannes van Waveren | ecb0f3b | 2016-05-27 12:55:53 -0500 | [diff] [blame] | 5534 | out.close(); |
| 5535 | } |
| 5536 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5537 | // |
| 5538 | // Set up the glslang traversal |
| 5539 | // |
| 5540 | void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv) |
| 5541 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5542 | spv::SpvBuildLogger logger; |
| 5543 | GlslangToSpv(intermediate, spirv, &logger); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 5544 | } |
| 5545 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5546 | void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv, spv::SpvBuildLogger* logger) |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 5547 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5548 | TIntermNode* root = intermediate.getTreeRoot(); |
| 5549 | |
| 5550 | if (root == 0) |
| 5551 | return; |
| 5552 | |
| 5553 | glslang::GetThreadPoolAllocator().push(); |
| 5554 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 5555 | TGlslangToSpvTraverser it(&intermediate, logger); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5556 | root->traverse(&it); |
John Kessenich | fca8262 | 2016-11-26 13:23:20 -0700 | [diff] [blame] | 5557 | it.finishSpv(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 5558 | it.dumpSpv(spirv); |
| 5559 | |
| 5560 | glslang::GetThreadPoolAllocator().pop(); |
| 5561 | } |
| 5562 | |
| 5563 | }; // end namespace glslang |