John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1 | // |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2 | //Copyright (C) 2014-2015 LunarG, Inc. |
| 3 | //Copyright (C) 2015-2016 Google, Inc. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4 | // |
| 5 | //All rights reserved. |
| 6 | // |
| 7 | //Redistribution and use in source and binary forms, with or without |
| 8 | //modification, are permitted provided that the following conditions |
| 9 | //are met: |
| 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 | // |
| 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. |
| 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 { |
| 45 | #include "GLSL.std.450.h" |
| 46 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 47 | |
| 48 | // Glslang includes |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 49 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 50 | #include "../glslang/MachineIndependent/SymbolTable.h" |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 51 | #include "../glslang/Include/Common.h" |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 52 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 53 | #include <fstream> |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 54 | #include <list> |
| 55 | #include <map> |
| 56 | #include <stack> |
| 57 | #include <string> |
| 58 | #include <vector> |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 59 | |
| 60 | namespace { |
| 61 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 62 | // For low-order part of the generator's magic number. Bump up |
| 63 | // when there is a change in the style (e.g., if SSA form changes, |
| 64 | // or a different instruction sequence to do something gets used). |
| 65 | const int GeneratorVersion = 1; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 66 | |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 67 | namespace { |
| 68 | class SpecConstantOpModeGuard { |
| 69 | public: |
| 70 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 71 | : builder_(builder) { |
| 72 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 73 | } |
| 74 | ~SpecConstantOpModeGuard() { |
| 75 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 76 | : builder_->setToNormalCodeGenMode(); |
| 77 | } |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 78 | void turnOnSpecConstantOpMode() { |
| 79 | builder_->setToSpecConstCodeGenMode(); |
| 80 | } |
qining | 4c91261 | 2016-04-01 10:35:16 -0400 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | spv::Builder* builder_; |
| 84 | bool previous_flag_; |
| 85 | }; |
| 86 | } |
| 87 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 88 | // |
| 89 | // The main holder of information for translating glslang to SPIR-V. |
| 90 | // |
| 91 | // Derives from the AST walking base class. |
| 92 | // |
| 93 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 94 | public: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 95 | TGlslangToSpvTraverser(const glslang::TIntermediate*, spv::SpvBuildLogger* logger); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 96 | virtual ~TGlslangToSpvTraverser(); |
| 97 | |
| 98 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 99 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 100 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 101 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 102 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 103 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 104 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 105 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 106 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 107 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 108 | void dumpSpv(std::vector<unsigned int>& out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 109 | |
| 110 | protected: |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 111 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 112 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool member); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 113 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 114 | spv::Id createSpvVariable(const glslang::TIntermSymbol*); |
| 115 | spv::Id getSampledType(const glslang::TSampler&); |
| 116 | spv::Id convertGlslangToSpvType(const glslang::TType& type); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 117 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 118 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 119 | spv::Id accessChainLoad(const glslang::TType& type); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 120 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 121 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 122 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 123 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 124 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 125 | void declareClipCullCapability(const glslang::TTypeList& members, int member); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 126 | |
| 127 | bool isShaderEntrypoint(const glslang::TIntermAggregate* node); |
| 128 | void makeFunctions(const glslang::TIntermSequence&); |
| 129 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 130 | void visitFunctions(const glslang::TIntermSequence&); |
| 131 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 132 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 133 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 134 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 135 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 136 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 137 | 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); |
| 138 | spv::Id createBinaryMatrixOperation(spv::Op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id left, spv::Id right); |
| 139 | spv::Id createUnaryOperation(glslang::TOperator op, spv::Decoration precision, spv::Decoration noContraction, spv::Id typeId, spv::Id operand,glslang::TBasicType typeProxy); |
| 140 | spv::Id createUnaryMatrixOperation(spv::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] | 141 | 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] | 142 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 143 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 144 | spv::Id createInvocationsOperation(glslang::TOperator, spv::Id typeId, spv::Id operand); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 145 | spv::Id 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] | 146 | spv::Id createNoArgOperation(glslang::TOperator op); |
| 147 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
| 148 | void addDecoration(spv::Id id, spv::Decoration dec); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 149 | void addDecoration(spv::Id id, spv::Decoration dec, unsigned value); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 150 | void addMemberDecoration(spv::Id id, int member, spv::Decoration dec); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 151 | void addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value); |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 152 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 153 | 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] | 154 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 155 | bool isTrivial(const glslang::TIntermTyped* node); |
| 156 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 157 | |
| 158 | spv::Function* shaderEntry; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 159 | spv::Instruction* entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 160 | int sequenceDepth; |
| 161 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 162 | spv::SpvBuildLogger* logger; |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 163 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 164 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 165 | spv::Builder builder; |
| 166 | bool inMain; |
| 167 | bool mainTerminated; |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 168 | 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] | 169 | 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] | 170 | const glslang::TIntermediate* glslangIntermediate; |
| 171 | spv::Id stdBuiltins; |
| 172 | |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 173 | std::unordered_map<int, spv::Id> symbolValues; |
| 174 | std::unordered_set<int> constReadOnlyParameters; // set of formal function parameters that have glslang qualifier constReadOnly, so we know they are not local function "const" that are write-once |
| 175 | std::unordered_map<std::string, spv::Function*> functionMap; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 176 | 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] | 177 | 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] | 178 | std::stack<bool> breakForLoop; // false means break for switch |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // |
| 182 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 183 | // |
| 184 | |
| 185 | // Translate glslang profile to SPIR-V source language. |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 186 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 187 | { |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 188 | switch (source) { |
| 189 | case glslang::EShSourceGlsl: |
| 190 | switch (profile) { |
| 191 | case ENoProfile: |
| 192 | case ECoreProfile: |
| 193 | case ECompatibilityProfile: |
| 194 | return spv::SourceLanguageGLSL; |
| 195 | case EEsProfile: |
| 196 | return spv::SourceLanguageESSL; |
| 197 | default: |
| 198 | return spv::SourceLanguageUnknown; |
| 199 | } |
| 200 | case glslang::EShSourceHlsl: |
| 201 | return spv::SourceLanguageHLSL; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 202 | default: |
| 203 | return spv::SourceLanguageUnknown; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Translate glslang language (stage) to SPIR-V execution model. |
| 208 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 209 | { |
| 210 | switch (stage) { |
| 211 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 212 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 213 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 214 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 215 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 216 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
| 217 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 218 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 219 | return spv::ExecutionModelFragment; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Translate glslang type to SPIR-V storage class. |
| 224 | spv::StorageClass TranslateStorageClass(const glslang::TType& type) |
| 225 | { |
| 226 | if (type.getQualifier().isPipeInput()) |
| 227 | return spv::StorageClassInput; |
| 228 | else if (type.getQualifier().isPipeOutput()) |
| 229 | return spv::StorageClassOutput; |
| 230 | else if (type.getQualifier().isUniformOrBuffer()) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 231 | if (type.getQualifier().layoutPushConstant) |
| 232 | return spv::StorageClassPushConstant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 233 | if (type.getBasicType() == glslang::EbtBlock) |
| 234 | return spv::StorageClassUniform; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 235 | else if (type.getBasicType() == glslang::EbtAtomicUint) |
| 236 | return spv::StorageClassAtomicCounter; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 237 | else |
| 238 | return spv::StorageClassUniformConstant; |
| 239 | // TODO: how are we distuingishing between default and non-default non-writable uniforms? Do default uniforms even exist? |
| 240 | } else { |
| 241 | switch (type.getQualifier().storage) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 242 | case glslang::EvqShared: return spv::StorageClassWorkgroup; break; |
| 243 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 244 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 245 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 246 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 247 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 248 | return spv::StorageClassFunction; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 254 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 255 | { |
| 256 | switch (sampler.dim) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 257 | case glslang::Esd1D: return spv::Dim1D; |
| 258 | case glslang::Esd2D: return spv::Dim2D; |
| 259 | case glslang::Esd3D: return spv::Dim3D; |
| 260 | case glslang::EsdCube: return spv::DimCube; |
| 261 | case glslang::EsdRect: return spv::DimRect; |
| 262 | case glslang::EsdBuffer: return spv::DimBuffer; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 263 | case glslang::EsdSubpass: return spv::DimSubpassData; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 264 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 265 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 266 | return spv::Dim2D; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Translate glslang type to SPIR-V precision decorations. |
| 271 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 272 | { |
| 273 | switch (type.getQualifier().precision) { |
John Kessenich | 61c47a9 | 2015-12-14 18:21:19 -0700 | [diff] [blame] | 274 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 275 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 276 | default: |
| 277 | return spv::NoPrecision; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // Translate glslang type to SPIR-V block decorations. |
| 282 | spv::Decoration TranslateBlockDecoration(const glslang::TType& type) |
| 283 | { |
| 284 | if (type.getBasicType() == glslang::EbtBlock) { |
| 285 | switch (type.getQualifier().storage) { |
| 286 | case glslang::EvqUniform: return spv::DecorationBlock; |
| 287 | case glslang::EvqBuffer: return spv::DecorationBufferBlock; |
| 288 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 289 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
| 290 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 291 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return (spv::Decoration)spv::BadValue; |
| 297 | } |
| 298 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 299 | // Translate glslang type to SPIR-V memory decorations. |
| 300 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory) |
| 301 | { |
| 302 | if (qualifier.coherent) |
| 303 | memory.push_back(spv::DecorationCoherent); |
| 304 | if (qualifier.volatil) |
| 305 | memory.push_back(spv::DecorationVolatile); |
| 306 | if (qualifier.restrict) |
| 307 | memory.push_back(spv::DecorationRestrict); |
| 308 | if (qualifier.readonly) |
| 309 | memory.push_back(spv::DecorationNonWritable); |
| 310 | if (qualifier.writeonly) |
| 311 | memory.push_back(spv::DecorationNonReadable); |
| 312 | } |
| 313 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 314 | // Translate glslang type to SPIR-V layout decorations. |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 315 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 316 | { |
| 317 | if (type.isMatrix()) { |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 318 | switch (matrixLayout) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 319 | case glslang::ElmRowMajor: |
| 320 | return spv::DecorationRowMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 321 | case glslang::ElmColumnMajor: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 322 | return spv::DecorationColMajor; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 323 | default: |
| 324 | // opaque layouts don't need a majorness |
| 325 | return (spv::Decoration)spv::BadValue; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 326 | } |
| 327 | } else { |
| 328 | switch (type.getBasicType()) { |
| 329 | default: |
| 330 | return (spv::Decoration)spv::BadValue; |
| 331 | break; |
| 332 | case glslang::EbtBlock: |
| 333 | switch (type.getQualifier().storage) { |
| 334 | case glslang::EvqUniform: |
| 335 | case glslang::EvqBuffer: |
| 336 | switch (type.getQualifier().layoutPacking) { |
| 337 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 338 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 339 | default: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 340 | return (spv::Decoration)spv::BadValue; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 341 | } |
| 342 | case glslang::EvqVaryingIn: |
| 343 | case glslang::EvqVaryingOut: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 344 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 345 | return (spv::Decoration)spv::BadValue; |
| 346 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 347 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 348 | return (spv::Decoration)spv::BadValue; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // Translate glslang type to SPIR-V interpolation decorations. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 355 | // Returns spv::Decoration(spv::BadValue) when no decoration |
| 356 | // should be applied. |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 357 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 358 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 359 | if (qualifier.smooth) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 360 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
| 361 | return (spv::Decoration)spv::BadValue; |
| 362 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 363 | if (qualifier.nopersp) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 364 | return spv::DecorationNoPerspective; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 365 | else if (qualifier.patch) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 366 | return spv::DecorationPatch; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 367 | else if (qualifier.flat) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 368 | return spv::DecorationFlat; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 369 | else if (qualifier.centroid) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 370 | return spv::DecorationCentroid; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 371 | else if (qualifier.sample) { |
| 372 | builder.addCapability(spv::CapabilitySampleRateShading); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 373 | return spv::DecorationSample; |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 374 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 375 | return (spv::Decoration)spv::BadValue; |
| 376 | } |
| 377 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 378 | // If glslang type is invariant, return SPIR-V invariant decoration. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 379 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 380 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 381 | if (qualifier.invariant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 382 | return spv::DecorationInvariant; |
| 383 | else |
| 384 | return (spv::Decoration)spv::BadValue; |
| 385 | } |
| 386 | |
qining | 9220dbb | 2016-05-04 17:34:38 -0400 | [diff] [blame] | 387 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 388 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 389 | { |
| 390 | if (qualifier.noContraction) |
| 391 | return spv::DecorationNoContraction; |
| 392 | else |
| 393 | return (spv::Decoration)spv::BadValue; |
| 394 | } |
| 395 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 396 | // Translate glslang built-in variable to SPIR-V built in decoration. |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 397 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, bool member) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 398 | { |
| 399 | switch (builtIn) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 400 | case glslang::EbvPointSize: |
| 401 | switch (glslangIntermediate->getStage()) { |
| 402 | case EShLangGeometry: |
| 403 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 404 | break; |
| 405 | case EShLangTessControl: |
| 406 | case EShLangTessEvaluation: |
| 407 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 408 | break; |
baldurk | 9cc6cd3 | 2016-02-10 20:04:20 +0100 | [diff] [blame] | 409 | default: |
| 410 | break; |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 411 | } |
| 412 | return spv::BuiltInPointSize; |
| 413 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 414 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 415 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 416 | // They are now generated when used, rather than here when declared. |
| 417 | // Potentially, the specification should be more clear what the minimum |
| 418 | // use needed is to trigger the capability. |
| 419 | // |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 420 | case glslang::EbvClipDistance: |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 421 | if (! member) |
| 422 | builder.addCapability(spv::CapabilityClipDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 423 | return spv::BuiltInClipDistance; |
| 424 | |
| 425 | case glslang::EbvCullDistance: |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 426 | if (! member) |
| 427 | builder.addCapability(spv::CapabilityCullDistance); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 428 | return spv::BuiltInCullDistance; |
| 429 | |
| 430 | case glslang::EbvViewportIndex: |
qining | 3d7b89a | 2016-03-07 21:32:15 -0500 | [diff] [blame] | 431 | builder.addCapability(spv::CapabilityMultiViewport); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 432 | return spv::BuiltInViewportIndex; |
| 433 | |
John Kessenich | 5e80113 | 2016-02-15 11:09:46 -0700 | [diff] [blame] | 434 | case glslang::EbvSampleId: |
| 435 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 436 | return spv::BuiltInSampleId; |
| 437 | |
| 438 | case glslang::EbvSamplePosition: |
| 439 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 440 | return spv::BuiltInSamplePosition; |
| 441 | |
| 442 | case glslang::EbvSampleMask: |
| 443 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 444 | return spv::BuiltInSampleMask; |
| 445 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 446 | case glslang::EbvPosition: return spv::BuiltInPosition; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 447 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 448 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 449 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 450 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 451 | case glslang::EbvBaseVertex: |
| 452 | case glslang::EbvBaseInstance: |
| 453 | case glslang::EbvDrawId: |
| 454 | // TODO: Add SPIR-V builtin ID. |
John Kessenich | c8a5676 | 2016-05-05 12:04:22 -0600 | [diff] [blame] | 455 | logger->missingFunctionality("shader draw parameters"); |
John Kessenich | da581a2 | 2015-10-14 14:10:30 -0600 | [diff] [blame] | 456 | return (spv::BuiltIn)spv::BadValue; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 457 | case glslang::EbvPrimitiveId: return spv::BuiltInPrimitiveId; |
| 458 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
| 459 | case glslang::EbvLayer: return spv::BuiltInLayer; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 460 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 461 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 462 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 463 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 464 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 465 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 466 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 467 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 468 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 469 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 470 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 471 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 472 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 473 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 474 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 475 | case glslang::EbvSubGroupSize: |
| 476 | case glslang::EbvSubGroupInvocation: |
| 477 | case glslang::EbvSubGroupEqMask: |
| 478 | case glslang::EbvSubGroupGeMask: |
| 479 | case glslang::EbvSubGroupGtMask: |
| 480 | case glslang::EbvSubGroupLeMask: |
| 481 | case glslang::EbvSubGroupLtMask: |
| 482 | // TODO: Add SPIR-V builtin ID. |
John Kessenich | c8a5676 | 2016-05-05 12:04:22 -0600 | [diff] [blame] | 483 | logger->missingFunctionality("shader ballot"); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 484 | return (spv::BuiltIn)spv::BadValue; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 485 | default: return (spv::BuiltIn)spv::BadValue; |
| 486 | } |
| 487 | } |
| 488 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 489 | // Translate glslang image layout format to SPIR-V image format. |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 490 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 491 | { |
| 492 | assert(type.getBasicType() == glslang::EbtSampler); |
| 493 | |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 494 | // Check for capabilities |
| 495 | switch (type.getQualifier().layoutFormat) { |
| 496 | case glslang::ElfRg32f: |
| 497 | case glslang::ElfRg16f: |
| 498 | case glslang::ElfR11fG11fB10f: |
| 499 | case glslang::ElfR16f: |
| 500 | case glslang::ElfRgba16: |
| 501 | case glslang::ElfRgb10A2: |
| 502 | case glslang::ElfRg16: |
| 503 | case glslang::ElfRg8: |
| 504 | case glslang::ElfR16: |
| 505 | case glslang::ElfR8: |
| 506 | case glslang::ElfRgba16Snorm: |
| 507 | case glslang::ElfRg16Snorm: |
| 508 | case glslang::ElfRg8Snorm: |
| 509 | case glslang::ElfR16Snorm: |
| 510 | case glslang::ElfR8Snorm: |
| 511 | |
| 512 | case glslang::ElfRg32i: |
| 513 | case glslang::ElfRg16i: |
| 514 | case glslang::ElfRg8i: |
| 515 | case glslang::ElfR16i: |
| 516 | case glslang::ElfR8i: |
| 517 | |
| 518 | case glslang::ElfRgb10a2ui: |
| 519 | case glslang::ElfRg32ui: |
| 520 | case glslang::ElfRg16ui: |
| 521 | case glslang::ElfRg8ui: |
| 522 | case glslang::ElfR16ui: |
| 523 | case glslang::ElfR8ui: |
| 524 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 525 | break; |
| 526 | |
| 527 | default: |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | // do the translation |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 532 | switch (type.getQualifier().layoutFormat) { |
| 533 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 534 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 535 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 536 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 537 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 538 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 539 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 540 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 541 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 542 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 543 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 544 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 545 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 546 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 547 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 548 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 549 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 550 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 551 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 552 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 553 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 554 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 555 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 556 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 557 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 558 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 559 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 560 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 561 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 562 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 563 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 564 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 565 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 566 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 567 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 568 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 569 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 570 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 571 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 572 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
| 573 | default: return (spv::ImageFormat)spv::BadValue; |
| 574 | } |
| 575 | } |
| 576 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 577 | // 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] | 578 | // descriptor set. |
| 579 | bool IsDescriptorResource(const glslang::TType& type) |
| 580 | { |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 581 | // uniform and buffer blocks are included, unless it is a push_constant |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 582 | if (type.getBasicType() == glslang::EbtBlock) |
John Kessenich | f7497e2 | 2016-03-08 21:36:22 -0700 | [diff] [blame] | 583 | return type.getQualifier().isUniformOrBuffer() && ! type.getQualifier().layoutPushConstant; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 584 | |
| 585 | // non block... |
| 586 | // basically samplerXXX/subpass/sampler/texture are all included |
| 587 | // if they are the global-scope-class, not the function parameter |
| 588 | // (or local, if they ever exist) class. |
| 589 | if (type.getBasicType() == glslang::EbtSampler) |
| 590 | return type.getQualifier().isUniformOrBuffer(); |
| 591 | |
| 592 | // None of the above. |
| 593 | return false; |
| 594 | } |
| 595 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 596 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 597 | { |
| 598 | if (child.layoutMatrix == glslang::ElmNone) |
| 599 | child.layoutMatrix = parent.layoutMatrix; |
| 600 | |
| 601 | if (parent.invariant) |
| 602 | child.invariant = true; |
| 603 | if (parent.nopersp) |
| 604 | child.nopersp = true; |
| 605 | if (parent.flat) |
| 606 | child.flat = true; |
| 607 | if (parent.centroid) |
| 608 | child.centroid = true; |
| 609 | if (parent.patch) |
| 610 | child.patch = true; |
| 611 | if (parent.sample) |
| 612 | child.sample = true; |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 613 | if (parent.coherent) |
| 614 | child.coherent = true; |
| 615 | if (parent.volatil) |
| 616 | child.volatil = true; |
| 617 | if (parent.restrict) |
| 618 | child.restrict = true; |
| 619 | if (parent.readonly) |
| 620 | child.readonly = true; |
| 621 | if (parent.writeonly) |
| 622 | child.writeonly = true; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | bool HasNonLayoutQualifiers(const glslang::TQualifier& qualifier) |
| 626 | { |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 627 | // This should list qualifiers that simultaneous satisfy: |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 628 | // - struct members can inherit from a struct declaration |
| 629 | // - effect decorations on the struct members (note smooth does not, and expecting something like volatile to effect the whole object) |
| 630 | // - are not part of the offset/st430/etc or row/column-major layout |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 631 | return qualifier.invariant || qualifier.nopersp || qualifier.flat || qualifier.centroid || qualifier.patch || qualifier.sample || qualifier.hasLocation(); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 632 | } |
| 633 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 634 | // |
| 635 | // Implement the TGlslangToSpvTraverser class. |
| 636 | // |
| 637 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 638 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(const glslang::TIntermediate* glslangIntermediate, spv::SpvBuildLogger* buildLogger) |
| 639 | : TIntermTraverser(true, false, true), shaderEntry(0), sequenceDepth(0), logger(buildLogger), |
| 640 | builder((glslang::GetKhronosToolId() << 16) | GeneratorVersion, logger), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 641 | inMain(false), mainTerminated(false), linkageOnly(false), |
| 642 | glslangIntermediate(glslangIntermediate) |
| 643 | { |
| 644 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 645 | |
| 646 | builder.clearAccessChain(); |
John Kessenich | 66e2faf | 2016-03-12 18:34:36 -0700 | [diff] [blame] | 647 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), glslangIntermediate->getVersion()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 648 | stdBuiltins = builder.import("GLSL.std.450"); |
| 649 | builder.setMemoryModel(spv::AddressingModelLogical, spv::MemoryModelGLSL450); |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 650 | shaderEntry = builder.makeEntrypoint(glslangIntermediate->getEntryPoint().c_str()); |
| 651 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPoint().c_str()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 652 | |
| 653 | // Add the source extensions |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 654 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 655 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 656 | builder.addSourceExtension(it->c_str()); |
| 657 | |
| 658 | // Add the top-level modes for this shader. |
| 659 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 660 | if (glslangIntermediate->getXfbMode()) { |
| 661 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 662 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 663 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 664 | |
| 665 | unsigned int mode; |
| 666 | switch (glslangIntermediate->getStage()) { |
| 667 | case EShLangVertex: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 668 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 669 | break; |
| 670 | |
| 671 | case EShLangTessControl: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 672 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 673 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 674 | break; |
| 675 | |
| 676 | case EShLangTessEvaluation: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 677 | builder.addCapability(spv::CapabilityTessellation); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 678 | switch (glslangIntermediate->getInputPrimitive()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 679 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 680 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 681 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 682 | default: mode = spv::BadValue; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 683 | } |
| 684 | if (mode != spv::BadValue) |
| 685 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 686 | |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 687 | switch (glslangIntermediate->getVertexSpacing()) { |
| 688 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 689 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 690 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
| 691 | default: mode = spv::BadValue; break; |
| 692 | } |
| 693 | if (mode != spv::BadValue) |
| 694 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 695 | |
| 696 | switch (glslangIntermediate->getVertexOrder()) { |
| 697 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 698 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
| 699 | default: mode = spv::BadValue; break; |
| 700 | } |
| 701 | if (mode != spv::BadValue) |
| 702 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 703 | |
| 704 | if (glslangIntermediate->getPointMode()) |
| 705 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 706 | break; |
| 707 | |
| 708 | case EShLangGeometry: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 709 | builder.addCapability(spv::CapabilityGeometry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 710 | switch (glslangIntermediate->getInputPrimitive()) { |
| 711 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 712 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 713 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 714 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 715 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
| 716 | default: mode = spv::BadValue; break; |
| 717 | } |
| 718 | if (mode != spv::BadValue) |
| 719 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 720 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 721 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 722 | |
| 723 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 724 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 725 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 726 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
| 727 | default: mode = spv::BadValue; break; |
| 728 | } |
| 729 | if (mode != spv::BadValue) |
| 730 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 731 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 732 | break; |
| 733 | |
| 734 | case EShLangFragment: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 735 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 736 | if (glslangIntermediate->getPixelCenterInteger()) |
| 737 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 738 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 739 | if (glslangIntermediate->getOriginUpperLeft()) |
| 740 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 741 | else |
| 742 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 743 | |
| 744 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 745 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 746 | |
| 747 | switch(glslangIntermediate->getDepth()) { |
John Kessenich | e690332 | 2015-10-13 16:29:02 -0600 | [diff] [blame] | 748 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 749 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
| 750 | default: mode = spv::BadValue; break; |
| 751 | } |
| 752 | if (mode != spv::BadValue) |
| 753 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 754 | |
| 755 | if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 756 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 757 | break; |
| 758 | |
| 759 | case EShLangCompute: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 760 | builder.addCapability(spv::CapabilityShader); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 761 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 762 | glslangIntermediate->getLocalSize(1), |
| 763 | glslangIntermediate->getLocalSize(2)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 764 | break; |
| 765 | |
| 766 | default: |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | } |
| 771 | |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 772 | // Finish everything and dump |
| 773 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
| 774 | { |
| 775 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
rdb | 32084e8 | 2016-02-23 22:17:38 +0100 | [diff] [blame] | 776 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 777 | entryPoint->addIdOperand(*it); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 778 | |
qining | da39733 | 2016-03-09 19:54:03 -0500 | [diff] [blame] | 779 | builder.eliminateDeadDecorations(); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 780 | builder.dump(out); |
| 781 | } |
| 782 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 783 | TGlslangToSpvTraverser::~TGlslangToSpvTraverser() |
| 784 | { |
| 785 | if (! mainTerminated) { |
| 786 | spv::Block* lastMainBlock = shaderEntry->getLastBlock(); |
| 787 | builder.setBuildPoint(lastMainBlock); |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 788 | builder.leaveFunction(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | // |
| 793 | // Implement the traversal functions. |
| 794 | // |
| 795 | // Return true from interior nodes to have the external traversal |
| 796 | // continue on to children. Return false if children were |
| 797 | // already processed. |
| 798 | // |
| 799 | |
| 800 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 801 | // Symbols can turn into |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 802 | // - uniform/input reads |
| 803 | // - output writes |
| 804 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 805 | // - something simple that degenerates into the last bullet |
| 806 | // |
| 807 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 808 | { |
qining | 75d1d80 | 2016-04-06 14:42:01 -0400 | [diff] [blame] | 809 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 810 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 811 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 812 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 813 | // getSymbolId() will set up all the IO decorations on the first call. |
| 814 | // Formal function parameters were mapped during makeFunctions(). |
| 815 | spv::Id id = getSymbolId(symbol); |
John Kessenich | 7ba6341 | 2015-12-20 17:37:07 -0700 | [diff] [blame] | 816 | |
| 817 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 818 | if (builder.isPointer(id)) { |
| 819 | spv::StorageClass sc = builder.getStorageClass(id); |
| 820 | if (sc == spv::StorageClassInput || sc == spv::StorageClassOutput) |
| 821 | iOSet.insert(id); |
| 822 | } |
| 823 | |
| 824 | // Only process non-linkage-only nodes for generating actual static uses |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 825 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 826 | // Prepare to generate code for the access |
| 827 | |
| 828 | // L-value chains will be computed left to right. We're on the symbol now, |
| 829 | // which is the left-most part of the access chain, so now is "clear" time, |
| 830 | // followed by setting the base. |
| 831 | builder.clearAccessChain(); |
| 832 | |
| 833 | // 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] | 834 | // except for |
| 835 | // A) "const in" arguments to a function, which are an intermediate object. |
| 836 | // See comments in handleUserFunctionCall(). |
| 837 | // B) Specialization constants (normal constant don't even come in as a variable), |
| 838 | // These are also pure R-values. |
| 839 | glslang::TQualifier qualifier = symbol->getQualifier(); |
| 840 | if ((qualifier.storage == glslang::EvqConstReadOnly && constReadOnlyParameters.find(symbol->getId()) != constReadOnlyParameters.end()) || |
| 841 | qualifier.isSpecConstant()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 842 | builder.setAccessChainRValue(id); |
| 843 | else |
| 844 | builder.setAccessChainLValue(id); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 849 | { |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 850 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 851 | if (node->getType().getQualifier().isSpecConstant()) |
| 852 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 853 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 854 | // First, handle special cases |
| 855 | switch (node->getOp()) { |
| 856 | case glslang::EOpAssign: |
| 857 | case glslang::EOpAddAssign: |
| 858 | case glslang::EOpSubAssign: |
| 859 | case glslang::EOpMulAssign: |
| 860 | case glslang::EOpVectorTimesMatrixAssign: |
| 861 | case glslang::EOpVectorTimesScalarAssign: |
| 862 | case glslang::EOpMatrixTimesScalarAssign: |
| 863 | case glslang::EOpMatrixTimesMatrixAssign: |
| 864 | case glslang::EOpDivAssign: |
| 865 | case glslang::EOpModAssign: |
| 866 | case glslang::EOpAndAssign: |
| 867 | case glslang::EOpInclusiveOrAssign: |
| 868 | case glslang::EOpExclusiveOrAssign: |
| 869 | case glslang::EOpLeftShiftAssign: |
| 870 | case glslang::EOpRightShiftAssign: |
| 871 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 872 | // where a is evaluated before b. For a simple assignment, GLSL |
| 873 | // says to evaluate the left before the right. So, always, left |
| 874 | // node then right node. |
| 875 | { |
| 876 | // get the left l-value, save it away |
| 877 | builder.clearAccessChain(); |
| 878 | node->getLeft()->traverse(this); |
| 879 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 880 | |
| 881 | // evaluate the right |
| 882 | builder.clearAccessChain(); |
| 883 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 884 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 885 | |
| 886 | if (node->getOp() != glslang::EOpAssign) { |
| 887 | // the left is also an r-value |
| 888 | builder.setAccessChain(lValue); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 889 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 890 | |
| 891 | // do the operation |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 892 | rValue = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getType()), |
| 893 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 894 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 895 | node->getType().getBasicType()); |
| 896 | |
| 897 | // these all need their counterparts in createBinaryOperation() |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 898 | assert(rValue != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | // store the result |
| 902 | builder.setAccessChain(lValue); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 903 | accessChainStore(node->getType(), rValue); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 904 | |
| 905 | // assignments are expressions having an rValue after they are evaluated... |
| 906 | builder.clearAccessChain(); |
| 907 | builder.setAccessChainRValue(rValue); |
| 908 | } |
| 909 | return false; |
| 910 | case glslang::EOpIndexDirect: |
| 911 | case glslang::EOpIndexDirectStruct: |
| 912 | { |
| 913 | // Get the left part of the access chain. |
| 914 | node->getLeft()->traverse(this); |
| 915 | |
| 916 | // Add the next element in the chain |
| 917 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 918 | int index = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 919 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && node->getOp() == glslang::EOpIndexDirectStruct) { |
| 920 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 921 | // index remapping due to hidden members in anonymous blocks. |
| 922 | std::vector<int>& remapper = memberRemapper[node->getLeft()->getType().getStruct()]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 923 | assert(remapper.size() > 0); |
| 924 | index = remapper[index]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | if (! node->getLeft()->getType().isArray() && |
| 928 | node->getLeft()->getType().isVector() && |
| 929 | node->getOp() == glslang::EOpIndexDirect) { |
| 930 | // This is essentially a hard-coded vector swizzle of size 1, |
| 931 | // so short circuit the access-chain stuff with a swizzle. |
| 932 | std::vector<unsigned> swizzle; |
| 933 | swizzle.push_back(node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 934 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 935 | } else { |
| 936 | // normal case for indexing array or structure or block |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 937 | builder.accessChainPush(builder.makeIntConstant(index)); |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 938 | |
| 939 | // Add capabilities here for accessing clip/cull distance |
| 940 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
| 941 | declareClipCullCapability(*node->getLeft()->getType().getStruct(), index); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | return false; |
| 945 | case glslang::EOpIndexIndirect: |
| 946 | { |
| 947 | // Structure or array or vector indirection. |
| 948 | // Will use native SPIR-V access-chain for struct and array indirection; |
| 949 | // matrices are arrays of vectors, so will also work for a matrix. |
| 950 | // Will use the access chain's 'component' for variable index into a vector. |
| 951 | |
| 952 | // This adapter is building access chains left to right. |
| 953 | // Set up the access chain to the left. |
| 954 | node->getLeft()->traverse(this); |
| 955 | |
| 956 | // save it so that computing the right side doesn't trash it |
| 957 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 958 | |
| 959 | // compute the next index in the chain |
| 960 | builder.clearAccessChain(); |
| 961 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 962 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 963 | |
| 964 | // restore the saved access chain |
| 965 | builder.setAccessChain(partial); |
| 966 | |
| 967 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 968 | builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 969 | else |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 970 | builder.accessChainPush(index); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 971 | } |
| 972 | return false; |
| 973 | case glslang::EOpVectorSwizzle: |
| 974 | { |
| 975 | node->getLeft()->traverse(this); |
| 976 | glslang::TIntermSequence& swizzleSequence = node->getRight()->getAsAggregate()->getSequence(); |
| 977 | std::vector<unsigned> swizzle; |
| 978 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 979 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 980 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 981 | } |
| 982 | return false; |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 983 | case glslang::EOpLogicalOr: |
| 984 | case glslang::EOpLogicalAnd: |
| 985 | { |
| 986 | |
| 987 | // These may require short circuiting, but can sometimes be done as straight |
| 988 | // binary operations. The right operand must be short circuited if it has |
| 989 | // side effects, and should probably be if it is complex. |
| 990 | if (isTrivial(node->getRight()->getAsTyped())) |
| 991 | break; // handle below as a normal binary operation |
| 992 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 993 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), *node->getRight()->getAsTyped()); |
| 994 | builder.clearAccessChain(); |
| 995 | builder.setAccessChainRValue(result); |
| 996 | } |
| 997 | return false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 998 | default: |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | // Assume generic binary op... |
| 1003 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1004 | // get right operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1005 | builder.clearAccessChain(); |
| 1006 | node->getLeft()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1007 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1008 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1009 | // get left operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1010 | builder.clearAccessChain(); |
| 1011 | node->getRight()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1012 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1013 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1014 | // get result |
| 1015 | spv::Id result = createBinaryOperation(node->getOp(), TranslatePrecisionDecoration(node->getType()), |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1016 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1017 | convertGlslangToSpvType(node->getType()), left, right, |
| 1018 | node->getLeft()->getType().getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1019 | |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1020 | builder.clearAccessChain(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1021 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1022 | logger->missingFunctionality("unknown glslang binary operation"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1023 | return true; // pick up a child as the place-holder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1024 | } else { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1025 | builder.setAccessChainRValue(result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1026 | return false; |
| 1027 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 1031 | { |
qining | 4088766 | 2016-04-03 22:20:42 -0400 | [diff] [blame] | 1032 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1033 | if (node->getType().getQualifier().isSpecConstant()) |
| 1034 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1035 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1036 | spv::Id result = spv::NoResult; |
| 1037 | |
| 1038 | // try texturing first |
| 1039 | result = createImageTextureFunctionCall(node); |
| 1040 | if (result != spv::NoResult) { |
| 1041 | builder.clearAccessChain(); |
| 1042 | builder.setAccessChainRValue(result); |
| 1043 | |
| 1044 | return false; // done with this node |
| 1045 | } |
| 1046 | |
| 1047 | // Non-texturing. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1048 | |
| 1049 | if (node->getOp() == glslang::EOpArrayLength) { |
| 1050 | // Quite special; won't want to evaluate the operand. |
| 1051 | |
| 1052 | // Normal .length() would have been constant folded by the front-end. |
| 1053 | // So, this has to be block.lastMember.length(). |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1054 | // SPV wants "block" and member number as the operands, go get them. |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1055 | assert(node->getOperand()->getType().isRuntimeSizedArray()); |
| 1056 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 1057 | block->traverse(this); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 1058 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 1059 | spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1060 | |
| 1061 | builder.clearAccessChain(); |
| 1062 | builder.setAccessChainRValue(length); |
| 1063 | |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1067 | // Start by evaluating the operand |
| 1068 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1069 | builder.clearAccessChain(); |
| 1070 | node->getOperand()->traverse(this); |
Rex Xu | 30f9258 | 2015-09-14 10:38:56 +0800 | [diff] [blame] | 1071 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1072 | spv::Id operand = spv::NoResult; |
| 1073 | |
| 1074 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 1075 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1076 | node->getOp() == glslang::EOpAtomicCounter || |
| 1077 | node->getOp() == glslang::EOpInterpolateAtCentroid) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1078 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 1079 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1080 | operand = accessChainLoad(node->getOperand()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1081 | |
| 1082 | spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1083 | spv::Decoration noContraction = TranslateNoContractionDecoration(node->getType().getQualifier()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1084 | |
| 1085 | // it could be a conversion |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1086 | if (! result) |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 1087 | result = createConversion(node->getOp(), precision, noContraction, convertGlslangToSpvType(node->getType()), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1088 | |
| 1089 | // if not, then possibly an operation |
| 1090 | if (! result) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1091 | result = createUnaryOperation(node->getOp(), precision, noContraction, convertGlslangToSpvType(node->getType()), operand, node->getOperand()->getBasicType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1092 | |
| 1093 | if (result) { |
| 1094 | builder.clearAccessChain(); |
| 1095 | builder.setAccessChainRValue(result); |
| 1096 | |
| 1097 | return false; // done with this node |
| 1098 | } |
| 1099 | |
| 1100 | // it must be a special case, check... |
| 1101 | switch (node->getOp()) { |
| 1102 | case glslang::EOpPostIncrement: |
| 1103 | case glslang::EOpPostDecrement: |
| 1104 | case glslang::EOpPreIncrement: |
| 1105 | case glslang::EOpPreDecrement: |
| 1106 | { |
| 1107 | // 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] | 1108 | spv::Id one = 0; |
| 1109 | if (node->getBasicType() == glslang::EbtFloat) |
| 1110 | one = builder.makeFloatConstant(1.0F); |
| 1111 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 1112 | one = builder.makeInt64Constant(1); |
| 1113 | else |
| 1114 | one = builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1115 | glslang::TOperator op; |
| 1116 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1117 | node->getOp() == glslang::EOpPostIncrement) |
| 1118 | op = glslang::EOpAdd; |
| 1119 | else |
| 1120 | op = glslang::EOpSub; |
| 1121 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1122 | spv::Id result = createBinaryOperation(op, TranslatePrecisionDecoration(node->getType()), |
| 1123 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1124 | convertGlslangToSpvType(node->getType()), operand, one, |
| 1125 | node->getType().getBasicType()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1126 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1127 | |
| 1128 | // The result of operation is always stored, but conditionally the |
| 1129 | // consumed result. The consumed result is always an r-value. |
| 1130 | builder.accessChainStore(result); |
| 1131 | builder.clearAccessChain(); |
| 1132 | if (node->getOp() == glslang::EOpPreIncrement || |
| 1133 | node->getOp() == glslang::EOpPreDecrement) |
| 1134 | builder.setAccessChainRValue(result); |
| 1135 | else |
| 1136 | builder.setAccessChainRValue(operand); |
| 1137 | } |
| 1138 | |
| 1139 | return false; |
| 1140 | |
| 1141 | case glslang::EOpEmitStreamVertex: |
| 1142 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 1143 | return false; |
| 1144 | case glslang::EOpEndStreamPrimitive: |
| 1145 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 1146 | return false; |
| 1147 | |
| 1148 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1149 | logger->missingFunctionality("unknown glslang unary"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1150 | return true; // pick up operand as placeholder result |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1151 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 1155 | { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 1156 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1157 | if (node->getType().getQualifier().isSpecConstant()) |
| 1158 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1159 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1160 | spv::Id result = spv::NoResult; |
| 1161 | |
| 1162 | // try texturing |
| 1163 | result = createImageTextureFunctionCall(node); |
| 1164 | if (result != spv::NoResult) { |
| 1165 | builder.clearAccessChain(); |
| 1166 | builder.setAccessChainRValue(result); |
| 1167 | |
| 1168 | return false; |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 1169 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1170 | // "imageStore" is a special case, which has no result |
| 1171 | return false; |
| 1172 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 1173 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1174 | glslang::TOperator binOp = glslang::EOpNull; |
| 1175 | bool reduceComparison = true; |
| 1176 | bool isMatrix = false; |
| 1177 | bool noReturnValue = false; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1178 | bool atomic = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1179 | |
| 1180 | assert(node->getOp()); |
| 1181 | |
| 1182 | spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
| 1183 | |
| 1184 | switch (node->getOp()) { |
| 1185 | case glslang::EOpSequence: |
| 1186 | { |
| 1187 | if (preVisit) |
| 1188 | ++sequenceDepth; |
| 1189 | else |
| 1190 | --sequenceDepth; |
| 1191 | |
| 1192 | if (sequenceDepth == 1) { |
| 1193 | // If this is the parent node of all the functions, we want to see them |
| 1194 | // early, so all call points have actual SPIR-V functions to reference. |
| 1195 | // In all cases, still let the traverser visit the children for us. |
| 1196 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 1197 | |
| 1198 | // Also, we want all globals initializers to go into the entry of main(), before |
| 1199 | // anything else gets there, so visit out of order, doing them all now. |
| 1200 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 1201 | |
| 1202 | // Initializers are done, don't want to visit again, but functions link objects need to be processed, |
| 1203 | // so do them manually. |
| 1204 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 1205 | |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
| 1209 | return true; |
| 1210 | } |
| 1211 | case glslang::EOpLinkerObjects: |
| 1212 | { |
| 1213 | if (visit == glslang::EvPreVisit) |
| 1214 | linkageOnly = true; |
| 1215 | else |
| 1216 | linkageOnly = false; |
| 1217 | |
| 1218 | return true; |
| 1219 | } |
| 1220 | case glslang::EOpComma: |
| 1221 | { |
| 1222 | // processing from left to right naturally leaves the right-most |
| 1223 | // lying around in the access chain |
| 1224 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1225 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 1226 | glslangOperands[i]->traverse(this); |
| 1227 | |
| 1228 | return false; |
| 1229 | } |
| 1230 | case glslang::EOpFunction: |
| 1231 | if (visit == glslang::EvPreVisit) { |
| 1232 | if (isShaderEntrypoint(node)) { |
| 1233 | inMain = true; |
| 1234 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1235 | } else { |
| 1236 | handleFunctionEntry(node); |
| 1237 | } |
| 1238 | } else { |
| 1239 | if (inMain) |
| 1240 | mainTerminated = true; |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 1241 | builder.leaveFunction(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1242 | inMain = false; |
| 1243 | } |
| 1244 | |
| 1245 | return true; |
| 1246 | case glslang::EOpParameters: |
| 1247 | // Parameters will have been consumed by EOpFunction processing, but not |
| 1248 | // the body, so we still visited the function node's children, making this |
| 1249 | // child redundant. |
| 1250 | return false; |
| 1251 | case glslang::EOpFunctionCall: |
| 1252 | { |
| 1253 | if (node->isUserDefined()) |
| 1254 | result = handleUserFunctionCall(node); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1255 | //assert(result); // this can happen for bad shaders because the call graph completeness checking is not yet done |
| 1256 | if (result) { |
| 1257 | builder.clearAccessChain(); |
| 1258 | builder.setAccessChainRValue(result); |
| 1259 | } else |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1260 | logger->missingFunctionality("missing user function; linker needs to catch that"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1261 | |
| 1262 | return false; |
| 1263 | } |
| 1264 | case glslang::EOpConstructMat2x2: |
| 1265 | case glslang::EOpConstructMat2x3: |
| 1266 | case glslang::EOpConstructMat2x4: |
| 1267 | case glslang::EOpConstructMat3x2: |
| 1268 | case glslang::EOpConstructMat3x3: |
| 1269 | case glslang::EOpConstructMat3x4: |
| 1270 | case glslang::EOpConstructMat4x2: |
| 1271 | case glslang::EOpConstructMat4x3: |
| 1272 | case glslang::EOpConstructMat4x4: |
| 1273 | case glslang::EOpConstructDMat2x2: |
| 1274 | case glslang::EOpConstructDMat2x3: |
| 1275 | case glslang::EOpConstructDMat2x4: |
| 1276 | case glslang::EOpConstructDMat3x2: |
| 1277 | case glslang::EOpConstructDMat3x3: |
| 1278 | case glslang::EOpConstructDMat3x4: |
| 1279 | case glslang::EOpConstructDMat4x2: |
| 1280 | case glslang::EOpConstructDMat4x3: |
| 1281 | case glslang::EOpConstructDMat4x4: |
| 1282 | isMatrix = true; |
| 1283 | // fall through |
| 1284 | case glslang::EOpConstructFloat: |
| 1285 | case glslang::EOpConstructVec2: |
| 1286 | case glslang::EOpConstructVec3: |
| 1287 | case glslang::EOpConstructVec4: |
| 1288 | case glslang::EOpConstructDouble: |
| 1289 | case glslang::EOpConstructDVec2: |
| 1290 | case glslang::EOpConstructDVec3: |
| 1291 | case glslang::EOpConstructDVec4: |
| 1292 | case glslang::EOpConstructBool: |
| 1293 | case glslang::EOpConstructBVec2: |
| 1294 | case glslang::EOpConstructBVec3: |
| 1295 | case glslang::EOpConstructBVec4: |
| 1296 | case glslang::EOpConstructInt: |
| 1297 | case glslang::EOpConstructIVec2: |
| 1298 | case glslang::EOpConstructIVec3: |
| 1299 | case glslang::EOpConstructIVec4: |
| 1300 | case glslang::EOpConstructUint: |
| 1301 | case glslang::EOpConstructUVec2: |
| 1302 | case glslang::EOpConstructUVec3: |
| 1303 | case glslang::EOpConstructUVec4: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1304 | case glslang::EOpConstructInt64: |
| 1305 | case glslang::EOpConstructI64Vec2: |
| 1306 | case glslang::EOpConstructI64Vec3: |
| 1307 | case glslang::EOpConstructI64Vec4: |
| 1308 | case glslang::EOpConstructUint64: |
| 1309 | case glslang::EOpConstructU64Vec2: |
| 1310 | case glslang::EOpConstructU64Vec3: |
| 1311 | case glslang::EOpConstructU64Vec4: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1312 | case glslang::EOpConstructStruct: |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1313 | case glslang::EOpConstructTextureSampler: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1314 | { |
| 1315 | std::vector<spv::Id> arguments; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1316 | translateArguments(*node, arguments); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1317 | spv::Id resultTypeId = convertGlslangToSpvType(node->getType()); |
| 1318 | spv::Id constructed; |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1319 | if (node->getOp() == glslang::EOpConstructTextureSampler) |
| 1320 | constructed = builder.createOp(spv::OpSampledImage, resultTypeId, arguments); |
| 1321 | else if (node->getOp() == glslang::EOpConstructStruct || node->getType().isArray()) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1322 | std::vector<spv::Id> constituents; |
| 1323 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 1324 | constituents.push_back(arguments[c]); |
| 1325 | constructed = builder.createCompositeConstruct(resultTypeId, constituents); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1326 | } else if (isMatrix) |
| 1327 | constructed = builder.createMatrixConstructor(precision, arguments, resultTypeId); |
| 1328 | else |
| 1329 | constructed = builder.createConstructor(precision, arguments, resultTypeId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1330 | |
| 1331 | builder.clearAccessChain(); |
| 1332 | builder.setAccessChainRValue(constructed); |
| 1333 | |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
| 1337 | // These six are component-wise compares with component-wise results. |
| 1338 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 1339 | case glslang::EOpLessThan: |
| 1340 | case glslang::EOpGreaterThan: |
| 1341 | case glslang::EOpLessThanEqual: |
| 1342 | case glslang::EOpGreaterThanEqual: |
| 1343 | case glslang::EOpVectorEqual: |
| 1344 | case glslang::EOpVectorNotEqual: |
| 1345 | { |
| 1346 | // Map the operation to a binary |
| 1347 | binOp = node->getOp(); |
| 1348 | reduceComparison = false; |
| 1349 | switch (node->getOp()) { |
| 1350 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 1351 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 1352 | default: binOp = node->getOp(); break; |
| 1353 | } |
| 1354 | |
| 1355 | break; |
| 1356 | } |
| 1357 | case glslang::EOpMul: |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1358 | // compontent-wise matrix multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1359 | binOp = glslang::EOpMul; |
| 1360 | break; |
| 1361 | case glslang::EOpOuterProduct: |
| 1362 | // two vectors multiplied to make a matrix |
| 1363 | binOp = glslang::EOpOuterProduct; |
| 1364 | break; |
| 1365 | case glslang::EOpDot: |
| 1366 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1367 | // for scalar dot product, use multiply |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1368 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1369 | if (! glslangOperands[0]->getAsTyped()->isVector()) |
| 1370 | binOp = glslang::EOpMul; |
| 1371 | break; |
| 1372 | } |
| 1373 | case glslang::EOpMod: |
| 1374 | // when an aggregate, this is the floating-point mod built-in function, |
| 1375 | // which can be emitted by the one in createBinaryOperation() |
| 1376 | binOp = glslang::EOpMod; |
| 1377 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1378 | case glslang::EOpEmitVertex: |
| 1379 | case glslang::EOpEndPrimitive: |
| 1380 | case glslang::EOpBarrier: |
| 1381 | case glslang::EOpMemoryBarrier: |
| 1382 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 1383 | case glslang::EOpMemoryBarrierBuffer: |
| 1384 | case glslang::EOpMemoryBarrierImage: |
| 1385 | case glslang::EOpMemoryBarrierShared: |
| 1386 | case glslang::EOpGroupMemoryBarrier: |
| 1387 | noReturnValue = true; |
| 1388 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 1389 | break; |
| 1390 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1391 | case glslang::EOpAtomicAdd: |
| 1392 | case glslang::EOpAtomicMin: |
| 1393 | case glslang::EOpAtomicMax: |
| 1394 | case glslang::EOpAtomicAnd: |
| 1395 | case glslang::EOpAtomicOr: |
| 1396 | case glslang::EOpAtomicXor: |
| 1397 | case glslang::EOpAtomicExchange: |
| 1398 | case glslang::EOpAtomicCompSwap: |
| 1399 | atomic = true; |
| 1400 | break; |
| 1401 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1402 | default: |
| 1403 | break; |
| 1404 | } |
| 1405 | |
| 1406 | // |
| 1407 | // See if it maps to a regular operation. |
| 1408 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1409 | if (binOp != glslang::EOpNull) { |
| 1410 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 1411 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 1412 | assert(left && right); |
| 1413 | |
| 1414 | builder.clearAccessChain(); |
| 1415 | left->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1416 | spv::Id leftId = accessChainLoad(left->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1417 | |
| 1418 | builder.clearAccessChain(); |
| 1419 | right->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1420 | spv::Id rightId = accessChainLoad(right->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1421 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1422 | result = createBinaryOperation(binOp, precision, TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1423 | convertGlslangToSpvType(node->getType()), leftId, rightId, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1424 | left->getType().getBasicType(), reduceComparison); |
| 1425 | |
| 1426 | // code above should only make binOp that exists in createBinaryOperation |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1427 | assert(result != spv::NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1428 | builder.clearAccessChain(); |
| 1429 | builder.setAccessChainRValue(result); |
| 1430 | |
| 1431 | return false; |
| 1432 | } |
| 1433 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1434 | // |
| 1435 | // Create the list of operands. |
| 1436 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1437 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 1438 | std::vector<spv::Id> operands; |
| 1439 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
| 1440 | builder.clearAccessChain(); |
| 1441 | glslangOperands[arg]->traverse(this); |
| 1442 | |
| 1443 | // special case l-value operands; there are just a few |
| 1444 | bool lvalue = false; |
| 1445 | switch (node->getOp()) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1446 | case glslang::EOpFrexp: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1447 | case glslang::EOpModf: |
| 1448 | if (arg == 1) |
| 1449 | lvalue = true; |
| 1450 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 1451 | case glslang::EOpInterpolateAtSample: |
| 1452 | case glslang::EOpInterpolateAtOffset: |
| 1453 | if (arg == 0) |
| 1454 | lvalue = true; |
| 1455 | break; |
Rex Xu | d4782c1 | 2015-09-06 16:30:11 +0800 | [diff] [blame] | 1456 | case glslang::EOpAtomicAdd: |
| 1457 | case glslang::EOpAtomicMin: |
| 1458 | case glslang::EOpAtomicMax: |
| 1459 | case glslang::EOpAtomicAnd: |
| 1460 | case glslang::EOpAtomicOr: |
| 1461 | case glslang::EOpAtomicXor: |
| 1462 | case glslang::EOpAtomicExchange: |
| 1463 | case glslang::EOpAtomicCompSwap: |
| 1464 | if (arg == 0) |
| 1465 | lvalue = true; |
| 1466 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1467 | case glslang::EOpAddCarry: |
| 1468 | case glslang::EOpSubBorrow: |
| 1469 | if (arg == 2) |
| 1470 | lvalue = true; |
| 1471 | break; |
| 1472 | case glslang::EOpUMulExtended: |
| 1473 | case glslang::EOpIMulExtended: |
| 1474 | if (arg >= 2) |
| 1475 | lvalue = true; |
| 1476 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1477 | default: |
| 1478 | break; |
| 1479 | } |
| 1480 | if (lvalue) |
| 1481 | operands.push_back(builder.accessChainGetLValue()); |
| 1482 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1483 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1484 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1485 | |
| 1486 | if (atomic) { |
| 1487 | // Handle all atomics |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 1488 | result = createAtomicOperation(node->getOp(), precision, convertGlslangToSpvType(node->getType()), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1489 | } else { |
| 1490 | // Pass through to generic operations. |
| 1491 | switch (glslangOperands.size()) { |
| 1492 | case 0: |
| 1493 | result = createNoArgOperation(node->getOp()); |
| 1494 | break; |
| 1495 | case 1: |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1496 | result = createUnaryOperation( |
| 1497 | node->getOp(), precision, |
| 1498 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1499 | convertGlslangToSpvType(node->getType()), operands.front(), |
| 1500 | glslangOperands[0]->getAsTyped()->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1501 | break; |
| 1502 | default: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1503 | result = createMiscOperation(node->getOp(), precision, convertGlslangToSpvType(node->getType()), operands, node->getBasicType()); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1504 | break; |
| 1505 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | if (noReturnValue) |
| 1509 | return false; |
| 1510 | |
| 1511 | if (! result) { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1512 | logger->missingFunctionality("unknown glslang aggregate"); |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1513 | return true; // pick up a child as a placeholder operand |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1514 | } else { |
| 1515 | builder.clearAccessChain(); |
| 1516 | builder.setAccessChainRValue(result); |
| 1517 | return false; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 1522 | { |
| 1523 | // This path handles both if-then-else and ?: |
| 1524 | // The if-then-else has a node type of void, while |
| 1525 | // ?: has a non-void node type |
| 1526 | spv::Id result = 0; |
| 1527 | if (node->getBasicType() != glslang::EbtVoid) { |
| 1528 | // don't handle this as just on-the-fly temporaries, because there will be two names |
| 1529 | // and better to leave SSA to later passes |
| 1530 | result = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 1531 | } |
| 1532 | |
| 1533 | // emit the condition before doing anything with selection |
| 1534 | node->getCondition()->traverse(this); |
| 1535 | |
| 1536 | // make an "if" based on the value created by the condition |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1537 | spv::Builder::If ifBuilder(accessChainLoad(node->getCondition()->getType()), builder); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1538 | |
| 1539 | if (node->getTrueBlock()) { |
| 1540 | // emit the "then" statement |
| 1541 | node->getTrueBlock()->traverse(this); |
| 1542 | if (result) |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1543 | builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | if (node->getFalseBlock()) { |
| 1547 | ifBuilder.makeBeginElse(); |
| 1548 | // emit the "else" statement |
| 1549 | node->getFalseBlock()->traverse(this); |
| 1550 | if (result) |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1551 | builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | ifBuilder.makeEndIf(); |
| 1555 | |
| 1556 | if (result) { |
| 1557 | // GLSL only has r-values as the result of a :?, but |
| 1558 | // if we have an l-value, that can be more efficient if it will |
| 1559 | // become the base of a complex r-value expression, because the |
| 1560 | // next layer copies r-values into memory to use the access-chain mechanism |
| 1561 | builder.clearAccessChain(); |
| 1562 | builder.setAccessChainLValue(result); |
| 1563 | } |
| 1564 | |
| 1565 | return false; |
| 1566 | } |
| 1567 | |
| 1568 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 1569 | { |
| 1570 | // emit and get the condition before doing anything with switch |
| 1571 | node->getCondition()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1572 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1573 | |
| 1574 | // browse the children to sort out code segments |
| 1575 | int defaultSegment = -1; |
| 1576 | std::vector<TIntermNode*> codeSegments; |
| 1577 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 1578 | std::vector<int> caseValues; |
| 1579 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 1580 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 1581 | TIntermNode* child = *c; |
| 1582 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 1583 | defaultSegment = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1584 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 1585 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1586 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 1587 | } else |
| 1588 | codeSegments.push_back(child); |
| 1589 | } |
| 1590 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1591 | // 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] | 1592 | // statements between the last case and the end of the switch statement |
| 1593 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 1594 | (int)codeSegments.size() == defaultSegment) |
| 1595 | codeSegments.push_back(nullptr); |
| 1596 | |
| 1597 | // make the switch statement |
| 1598 | std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 1599 | builder.makeSwitch(selector, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, segmentBlocks); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1600 | |
| 1601 | // emit all the code in the segments |
| 1602 | breakForLoop.push(false); |
| 1603 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 1604 | builder.nextSwitchSegment(segmentBlocks, s); |
| 1605 | if (codeSegments[s]) |
| 1606 | codeSegments[s]->traverse(this); |
| 1607 | else |
| 1608 | builder.addSwitchBreak(); |
| 1609 | } |
| 1610 | breakForLoop.pop(); |
| 1611 | |
| 1612 | builder.endSwitch(segmentBlocks); |
| 1613 | |
| 1614 | return false; |
| 1615 | } |
| 1616 | |
| 1617 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 1618 | { |
| 1619 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 1620 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1621 | |
| 1622 | builder.clearAccessChain(); |
| 1623 | builder.setAccessChainRValue(constant); |
| 1624 | } |
| 1625 | |
| 1626 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 1627 | { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1628 | auto blocks = builder.makeNewLoop(); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 1629 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 1630 | // Spec requires back edges to target header blocks, and every header block |
| 1631 | // must dominate its merge block. Make a header block first to ensure these |
| 1632 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 1633 | // by a block-ending branch. But we don't want to put any other body/test |
| 1634 | // instructions in it, since the body/test may have arbitrary instructions, |
| 1635 | // including merges of its own. |
| 1636 | builder.setBuildPoint(&blocks.head); |
| 1637 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, spv::LoopControlMaskNone); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1638 | if (node->testFirst() && node->getTest()) { |
Dejan Mircevski | 213bbbe | 2016-01-20 11:51:43 -0500 | [diff] [blame] | 1639 | spv::Block& test = builder.makeNewBlock(); |
| 1640 | builder.createBranch(&test); |
| 1641 | |
| 1642 | builder.setBuildPoint(&test); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1643 | node->getTest()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1644 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1645 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1646 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 1647 | |
| 1648 | builder.setBuildPoint(&blocks.body); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 1649 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1650 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 1651 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1652 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 1653 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1654 | |
| 1655 | builder.setBuildPoint(&blocks.continue_target); |
| 1656 | if (node->getTerminal()) |
| 1657 | node->getTerminal()->traverse(this); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 1658 | builder.createBranch(&blocks.head); |
David Neto | c22f37c | 2015-07-15 16:21:26 -0400 | [diff] [blame] | 1659 | } else { |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1660 | builder.createBranch(&blocks.body); |
| 1661 | |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 1662 | breakForLoop.push(true); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1663 | builder.setBuildPoint(&blocks.body); |
| 1664 | if (node->getBody()) |
Dejan Mircevski | e537b8b | 2016-01-10 19:37:00 -0500 | [diff] [blame] | 1665 | node->getBody()->traverse(this); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1666 | builder.createBranch(&blocks.continue_target); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 1667 | breakForLoop.pop(); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1668 | |
| 1669 | builder.setBuildPoint(&blocks.continue_target); |
| 1670 | if (node->getTerminal()) |
| 1671 | node->getTerminal()->traverse(this); |
| 1672 | if (node->getTest()) { |
| 1673 | node->getTest()->traverse(this); |
| 1674 | spv::Id condition = |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1675 | accessChainLoad(node->getTest()->getType()); |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 1676 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1677 | } else { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 1678 | // TODO: unless there was a break/return/discard instruction |
| 1679 | // somewhere in the body, this is an infinite loop, so we should |
| 1680 | // issue a warning. |
Dejan Mircevski | 832c65c | 2016-01-11 15:57:11 -0500 | [diff] [blame] | 1681 | builder.createBranch(&blocks.head); |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1682 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1683 | } |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1684 | builder.setBuildPoint(&blocks.merge); |
Dejan Mircevski | c8fbbab | 2016-01-11 14:48:36 -0500 | [diff] [blame] | 1685 | builder.closeLoop(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1686 | return false; |
| 1687 | } |
| 1688 | |
| 1689 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 1690 | { |
| 1691 | if (node->getExpression()) |
| 1692 | node->getExpression()->traverse(this); |
| 1693 | |
| 1694 | switch (node->getFlowOp()) { |
| 1695 | case glslang::EOpKill: |
| 1696 | builder.makeDiscard(); |
| 1697 | break; |
| 1698 | case glslang::EOpBreak: |
| 1699 | if (breakForLoop.top()) |
| 1700 | builder.createLoopExit(); |
| 1701 | else |
| 1702 | builder.addSwitchBreak(); |
| 1703 | break; |
| 1704 | case glslang::EOpContinue: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1705 | builder.createLoopContinue(); |
| 1706 | break; |
| 1707 | case glslang::EOpReturn: |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 1708 | if (node->getExpression()) |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 1709 | builder.makeReturn(false, accessChainLoad(node->getExpression()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1710 | else |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 1711 | builder.makeReturn(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1712 | |
| 1713 | builder.clearAccessChain(); |
| 1714 | break; |
| 1715 | |
| 1716 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1717 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1718 | break; |
| 1719 | } |
| 1720 | |
| 1721 | return false; |
| 1722 | } |
| 1723 | |
| 1724 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node) |
| 1725 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 1726 | // First, steer off constants, which are not SPIR-V variables, but |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1727 | // can still have a mapping to a SPIR-V Id. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1728 | // This includes specialization constants. |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 1729 | if (node->getQualifier().isConstant()) { |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 1730 | return createSpvConstant(*node); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | // Now, handle actual variables |
| 1734 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
| 1735 | spv::Id spvType = convertGlslangToSpvType(node->getType()); |
| 1736 | |
| 1737 | const char* name = node->getName().c_str(); |
| 1738 | if (glslang::IsAnonymous(name)) |
| 1739 | name = ""; |
| 1740 | |
| 1741 | return builder.createVariable(storageClass, spvType, name); |
| 1742 | } |
| 1743 | |
| 1744 | // Return type Id of the sampled type. |
| 1745 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 1746 | { |
| 1747 | switch (sampler.type) { |
| 1748 | case glslang::EbtFloat: return builder.makeFloatType(32); |
| 1749 | case glslang::EbtInt: return builder.makeIntType(32); |
| 1750 | case glslang::EbtUint: return builder.makeUintType(32); |
| 1751 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1752 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1753 | return builder.makeFloatType(32); |
| 1754 | } |
| 1755 | } |
| 1756 | |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 1757 | // Convert from a glslang type to an SPV type, by calling into a |
| 1758 | // recursive version of this function. This establishes the inherited |
| 1759 | // layout state rooted from the top-level type. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1760 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type) |
| 1761 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1762 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier()); |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | // 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] | 1766 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1767 | 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] | 1768 | { |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1769 | spv::Id spvType = spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1770 | |
| 1771 | switch (type.getBasicType()) { |
| 1772 | case glslang::EbtVoid: |
| 1773 | spvType = builder.makeVoidType(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1774 | assert (! type.isArray()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1775 | break; |
| 1776 | case glslang::EbtFloat: |
| 1777 | spvType = builder.makeFloatType(32); |
| 1778 | break; |
| 1779 | case glslang::EbtDouble: |
| 1780 | spvType = builder.makeFloatType(64); |
| 1781 | break; |
| 1782 | case glslang::EbtBool: |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 1783 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 1784 | // a 32-bit int where non-0 means true. |
| 1785 | if (explicitLayout != glslang::ElpNone) |
| 1786 | spvType = builder.makeUintType(32); |
| 1787 | else |
| 1788 | spvType = builder.makeBoolType(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1789 | break; |
| 1790 | case glslang::EbtInt: |
| 1791 | spvType = builder.makeIntType(32); |
| 1792 | break; |
| 1793 | case glslang::EbtUint: |
| 1794 | spvType = builder.makeUintType(32); |
| 1795 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 1796 | case glslang::EbtInt64: |
| 1797 | builder.addCapability(spv::CapabilityInt64); |
| 1798 | spvType = builder.makeIntType(64); |
| 1799 | break; |
| 1800 | case glslang::EbtUint64: |
| 1801 | builder.addCapability(spv::CapabilityInt64); |
| 1802 | spvType = builder.makeUintType(64); |
| 1803 | break; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1804 | case glslang::EbtAtomicUint: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 1805 | logger->tbdFunctionality("Is atomic_uint an opaque handle in the uniform storage class, or an addresses in the atomic storage class?"); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1806 | spvType = builder.makeUintType(32); |
| 1807 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1808 | case glslang::EbtSampler: |
| 1809 | { |
| 1810 | const glslang::TSampler& sampler = type.getSampler(); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1811 | if (sampler.sampler) { |
| 1812 | // pure sampler |
| 1813 | spvType = builder.makeSamplerType(); |
| 1814 | } else { |
| 1815 | // an image is present, make its type |
| 1816 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.shadow, sampler.arrayed, sampler.ms, |
| 1817 | sampler.image ? 2 : 1, TranslateImageFormat(type)); |
| 1818 | if (sampler.combined) { |
| 1819 | // already has both image and sampler, make the combined type |
| 1820 | spvType = builder.makeSampledImageType(spvType); |
| 1821 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1822 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1823 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1824 | break; |
| 1825 | case glslang::EbtStruct: |
| 1826 | case glslang::EbtBlock: |
| 1827 | { |
| 1828 | // If we've seen this struct type, return it |
| 1829 | const glslang::TTypeList* glslangStruct = type.getStruct(); |
| 1830 | std::vector<spv::Id> structFields; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1831 | |
| 1832 | // Try to share structs for different layouts, but not yet for other |
| 1833 | // kinds of qualification (primarily not yet including interpolant qualification). |
| 1834 | if (! HasNonLayoutQualifiers(qualifier)) |
| 1835 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangStruct]; |
| 1836 | if (spvType != spv::NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1837 | break; |
| 1838 | |
| 1839 | // else, we haven't seen it... |
| 1840 | |
| 1841 | // Create a vector of struct types for SPIR-V to consume |
| 1842 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, except sometimes for blocks |
| 1843 | if (type.getBasicType() == glslang::EbtBlock) |
| 1844 | memberRemapper[glslangStruct].resize(glslangStruct->size()); |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1845 | int locationOffset = 0; // for use across struct members, when they are called recursively |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1846 | for (int i = 0; i < (int)glslangStruct->size(); i++) { |
| 1847 | glslang::TType& glslangType = *(*glslangStruct)[i].type; |
| 1848 | if (glslangType.hiddenMember()) { |
| 1849 | ++memberDelta; |
| 1850 | if (type.getBasicType() == glslang::EbtBlock) |
| 1851 | memberRemapper[glslangStruct][i] = -1; |
| 1852 | } else { |
| 1853 | if (type.getBasicType() == glslang::EbtBlock) |
| 1854 | memberRemapper[glslangStruct][i] = i - memberDelta; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1855 | // modify just this child's view of the qualifier |
| 1856 | glslang::TQualifier subQualifier = glslangType.getQualifier(); |
| 1857 | InheritQualifiers(subQualifier, qualifier); |
John Kessenich | 0967748 | 2016-02-19 12:21:50 -0700 | [diff] [blame] | 1858 | |
| 1859 | // manually inherit location; it's more complex |
| 1860 | if (! subQualifier.hasLocation() && qualifier.hasLocation()) |
| 1861 | subQualifier.layoutLocation = qualifier.layoutLocation + locationOffset; |
| 1862 | if (qualifier.hasLocation()) |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1863 | locationOffset += glslangIntermediate->computeTypeLocationSize(glslangType); |
John Kessenich | 0967748 | 2016-02-19 12:21:50 -0700 | [diff] [blame] | 1864 | |
| 1865 | // recurse |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1866 | structFields.push_back(convertGlslangToSpvType(glslangType, explicitLayout, subQualifier)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | // Make the SPIR-V type |
| 1871 | spvType = builder.makeStructType(structFields, type.getTypeName().c_str()); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1872 | if (! HasNonLayoutQualifiers(qualifier)) |
| 1873 | structMap[explicitLayout][qualifier.layoutMatrix][glslangStruct] = spvType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1874 | |
| 1875 | // Name and decorate the non-hidden members |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1876 | int offset = -1; |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1877 | locationOffset = 0; // for use within the members of this struct, right now |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1878 | for (int i = 0; i < (int)glslangStruct->size(); i++) { |
| 1879 | glslang::TType& glslangType = *(*glslangStruct)[i].type; |
| 1880 | int member = i; |
| 1881 | if (type.getBasicType() == glslang::EbtBlock) |
| 1882 | member = memberRemapper[glslangStruct][i]; |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 1883 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1884 | // modify just this child's view of the qualifier |
| 1885 | glslang::TQualifier subQualifier = glslangType.getQualifier(); |
| 1886 | InheritQualifiers(subQualifier, qualifier); |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 1887 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1888 | // using -1 above to indicate a hidden member |
| 1889 | if (member >= 0) { |
| 1890 | builder.addMemberName(spvType, member, glslangType.getFieldName().c_str()); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1891 | addMemberDecoration(spvType, member, TranslateLayoutDecoration(glslangType, subQualifier.layoutMatrix)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1892 | addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangType)); |
scygan | 8add151 | 2016-05-06 16:54:54 +0200 | [diff] [blame] | 1893 | // Add interpolation decorations only to top-level members of Input and Output storage classes |
John Kessenich | 9af54c3 | 2016-05-17 10:24:00 -0600 | [diff] [blame^] | 1894 | if (type.getQualifier().storage == glslang::EvqVaryingIn || type.getQualifier().storage == glslang::EvqVaryingOut) { |
scygan | 8add151 | 2016-05-06 16:54:54 +0200 | [diff] [blame] | 1895 | addMemberDecoration(spvType, member, TranslateInterpolationDecoration(subQualifier)); |
| 1896 | } |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1897 | addMemberDecoration(spvType, member, TranslateInvariantDecoration(subQualifier)); |
John Kessenich | 0967748 | 2016-02-19 12:21:50 -0700 | [diff] [blame] | 1898 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 1899 | if (qualifier.storage == glslang::EvqBuffer) { |
| 1900 | std::vector<spv::Decoration> memory; |
| 1901 | TranslateMemoryDecoration(subQualifier, memory); |
| 1902 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 1903 | addMemberDecoration(spvType, member, memory[i]); |
| 1904 | } |
| 1905 | |
John Kessenich | 0967748 | 2016-02-19 12:21:50 -0700 | [diff] [blame] | 1906 | // compute location decoration; tricky based on whether inheritance is at play |
| 1907 | // TODO: This algorithm (and it's cousin above doing almost the same thing) should |
| 1908 | // probably move to the linker stage of the front end proper, and just have the |
| 1909 | // answer sitting already distributed throughout the individual member locations. |
| 1910 | int location = -1; // will only decorate if present or inherited |
John Kessenich | 9af54c3 | 2016-05-17 10:24:00 -0600 | [diff] [blame^] | 1911 | if (subQualifier.hasLocation()) { // no inheritance, or override of inheritance |
scygan | 8add151 | 2016-05-06 16:54:54 +0200 | [diff] [blame] | 1912 | // struct members should not have explicit locations |
| 1913 | assert(type.getBasicType() != glslang::EbtStruct); |
John Kessenich | 0967748 | 2016-02-19 12:21:50 -0700 | [diff] [blame] | 1914 | location = subQualifier.layoutLocation; |
John Kessenich | 9af54c3 | 2016-05-17 10:24:00 -0600 | [diff] [blame^] | 1915 | } else if (type.getBasicType() != glslang::EbtBlock) { |
scygan | 8add151 | 2016-05-06 16:54:54 +0200 | [diff] [blame] | 1916 | // If it is a not a Block, (...) Its members are assigned consecutive locations (...) |
| 1917 | // The members, and their nested types, must not themselves have Location decorations. |
| 1918 | } |
John Kessenich | 0967748 | 2016-02-19 12:21:50 -0700 | [diff] [blame] | 1919 | else if (qualifier.hasLocation()) // inheritance |
| 1920 | location = qualifier.layoutLocation + locationOffset; |
| 1921 | if (qualifier.hasLocation()) // track for upcoming inheritance |
John Kessenich | 7b9fa25 | 2016-01-21 18:56:57 -0700 | [diff] [blame] | 1922 | locationOffset += glslangIntermediate->computeTypeLocationSize(glslangType); |
John Kessenich | 0967748 | 2016-02-19 12:21:50 -0700 | [diff] [blame] | 1923 | if (location >= 0) |
| 1924 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, location); |
| 1925 | |
| 1926 | // component, XFB, others |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1927 | if (glslangType.getQualifier().hasComponent()) |
| 1928 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, glslangType.getQualifier().layoutComponent); |
| 1929 | if (glslangType.getQualifier().hasXfbOffset()) |
| 1930 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, glslangType.getQualifier().layoutXfbOffset); |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 1931 | else if (explicitLayout != glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1932 | // figure out what to do with offset, which is accumulating |
| 1933 | int nextOffset; |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1934 | updateMemberOffset(type, glslangType, offset, nextOffset, explicitLayout, subQualifier.layoutMatrix); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1935 | if (offset >= 0) |
John Kessenich | a06bd52 | 2015-09-11 15:15:23 -0600 | [diff] [blame] | 1936 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1937 | offset = nextOffset; |
| 1938 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1939 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 1940 | if (glslangType.isMatrix() && explicitLayout != glslang::ElpNone) |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1941 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, getMatrixStride(glslangType, explicitLayout, subQualifier.layoutMatrix)); |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 1942 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1943 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 1944 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangType.getQualifier().builtIn, true); |
John Kessenich | 3066953 | 2015-08-06 22:02:24 -0600 | [diff] [blame] | 1945 | if (builtIn != spv::BadValue) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1946 | addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | // Decorate the structure |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 1951 | addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1952 | addDecoration(spvType, TranslateBlockDecoration(type)); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 1953 | if (type.getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1954 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1955 | builder.addDecoration(spvType, spv::DecorationStream, type.getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1956 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1957 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 1958 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1959 | if (type.getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1960 | builder.addDecoration(spvType, spv::DecorationXfbStride, type.getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1961 | if (type.getQualifier().hasXfbBuffer()) |
| 1962 | builder.addDecoration(spvType, spv::DecorationXfbBuffer, type.getQualifier().layoutXfbBuffer); |
| 1963 | } |
| 1964 | } |
| 1965 | break; |
| 1966 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1967 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1968 | break; |
| 1969 | } |
| 1970 | |
| 1971 | if (type.isMatrix()) |
| 1972 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 1973 | else { |
| 1974 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 1975 | if (type.getVectorSize() > 1) |
| 1976 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 1977 | } |
| 1978 | |
| 1979 | if (type.isArray()) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 1980 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 1981 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 1982 | // Do all but the outer dimension |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 1983 | if (type.getArraySizes()->getNumDims() > 1) { |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 1984 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 1985 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 1986 | // Use a dummy glslang type for querying internal strides of |
| 1987 | // arrays of arrays, but using just a one-dimensional array. |
| 1988 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
| 1989 | while (simpleArrayType.getArraySizes().getNumDims() > 1) |
| 1990 | simpleArrayType.getArraySizes().dereference(); |
| 1991 | |
| 1992 | // Will compute the higher-order strides here, rather than making a whole |
| 1993 | // pile of types and doing repetitive recursion on their contents. |
| 1994 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 1995 | } |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 1996 | |
| 1997 | // make the arrays |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 1998 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 1999 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2000 | if (stride > 0) |
| 2001 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2002 | stride *= type.getArraySizes()->getDimSize(dim); |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2003 | } |
| 2004 | } else { |
| 2005 | // single-dimensional array, and don't yet have stride |
| 2006 | |
John Kessenich | f8842e5 | 2016-01-04 19:22:56 -0700 | [diff] [blame] | 2007 | // 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] | 2008 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 2009 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2010 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2011 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2012 | // Do the outer dimension, which might not be known for a runtime-sized array |
| 2013 | if (type.isRuntimeSizedArray()) { |
| 2014 | spvType = builder.makeRuntimeArray(spvType); |
| 2015 | } else { |
| 2016 | assert(type.getOuterArraySize() > 0); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2017 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 2018 | } |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 2019 | if (stride > 0) |
| 2020 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | return spvType; |
| 2024 | } |
| 2025 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2026 | // Turn the expression forming the array size into an id. |
| 2027 | // This is not quite trivial, because of specialization constants. |
| 2028 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 2029 | // a specialization constant expression is. |
| 2030 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 2031 | { |
| 2032 | // First, see if this is sized with a node, meaning a specialization constant: |
| 2033 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 2034 | if (specNode != nullptr) { |
| 2035 | builder.clearAccessChain(); |
| 2036 | specNode->traverse(this); |
| 2037 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 2038 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2039 | |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2040 | // Otherwise, need a compile-time (front end) size, get it: |
| 2041 | int size = arraySizes.getDimSize(dim); |
| 2042 | assert(size > 0); |
| 2043 | return builder.makeUintConstant(size); |
| 2044 | } |
| 2045 | |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2046 | // Wrap the builder's accessChainLoad to: |
| 2047 | // - localize handling of RelaxedPrecision |
| 2048 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 2049 | // (avoids unnecessary work and possible type punning for structures) |
| 2050 | // - do conversion of concrete to abstract type |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2051 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 2052 | { |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2053 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2054 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), nominalTypeId); |
| 2055 | |
| 2056 | // Need to convert to abstract types when necessary |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2057 | if (type.getBasicType() == glslang::EbtBool) { |
| 2058 | if (builder.isScalarType(nominalTypeId)) { |
| 2059 | // Conversion for bool |
| 2060 | spv::Id boolType = builder.makeBoolType(); |
| 2061 | if (nominalTypeId != boolType) |
| 2062 | loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 2063 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2064 | // Conversion for bvec |
| 2065 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2066 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 2067 | if (nominalTypeId != bvecType) |
| 2068 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 2069 | } |
| 2070 | } |
John Kessenich | 103bef9 | 2016-02-08 21:38:15 -0700 | [diff] [blame] | 2071 | |
| 2072 | return loadedId; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2075 | // Wrap the builder's accessChainStore to: |
| 2076 | // - do conversion of concrete to abstract type |
| 2077 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 2078 | { |
| 2079 | // Need to convert to abstract types when necessary |
| 2080 | if (type.getBasicType() == glslang::EbtBool) { |
| 2081 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 2082 | |
| 2083 | if (builder.isScalarType(nominalTypeId)) { |
| 2084 | // Conversion for bool |
| 2085 | spv::Id boolType = builder.makeBoolType(); |
| 2086 | if (nominalTypeId != boolType) { |
| 2087 | spv::Id zero = builder.makeUintConstant(0); |
| 2088 | spv::Id one = builder.makeUintConstant(1); |
| 2089 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 2090 | } |
| 2091 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2092 | // Conversion for bvec |
| 2093 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2094 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 2095 | if (nominalTypeId != bvecType) { |
| 2096 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 2097 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 2098 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 2099 | } |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | builder.accessChainStore(rvalue); |
| 2104 | } |
| 2105 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2106 | // Decide whether or not this type should be |
| 2107 | // decorated with offsets and strides, and if so |
| 2108 | // whether std140 or std430 rules should be applied. |
| 2109 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2110 | { |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2111 | // has to be a block |
| 2112 | if (type.getBasicType() != glslang::EbtBlock) |
| 2113 | return glslang::ElpNone; |
| 2114 | |
| 2115 | // has to be a uniform or buffer block |
| 2116 | if (type.getQualifier().storage != glslang::EvqUniform && |
| 2117 | type.getQualifier().storage != glslang::EvqBuffer) |
| 2118 | return glslang::ElpNone; |
| 2119 | |
| 2120 | // return the layout to use |
| 2121 | switch (type.getQualifier().layoutPacking) { |
| 2122 | case glslang::ElpStd140: |
| 2123 | case glslang::ElpStd430: |
| 2124 | return type.getQualifier().layoutPacking; |
| 2125 | default: |
| 2126 | return glslang::ElpNone; |
| 2127 | } |
John Kessenich | 31ed483 | 2015-09-09 17:51:38 -0600 | [diff] [blame] | 2128 | } |
| 2129 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2130 | // Given an array type, returns the integer stride required for that array |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2131 | 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] | 2132 | { |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2133 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2134 | int stride; |
| 2135 | glslangIntermediate->getBaseAlignment(arrayType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | e721f49 | 2015-12-06 19:17:49 -0700 | [diff] [blame] | 2136 | |
| 2137 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2140 | // 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] | 2141 | // when used as a member of an interface block |
John Kessenich | 3ac051e | 2015-12-20 11:29:16 -0700 | [diff] [blame] | 2142 | 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] | 2143 | { |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2144 | glslang::TType elementType; |
| 2145 | elementType.shallowCopy(matrixType); |
| 2146 | elementType.clearArraySizes(); |
| 2147 | |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2148 | int size; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2149 | int stride; |
| 2150 | glslangIntermediate->getBaseAlignment(elementType, size, stride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
| 2151 | |
| 2152 | return stride; |
Jason Ekstrand | 54aedf1 | 2015-09-05 09:50:58 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2155 | // Given a member type of a struct, realign the current offset for it, and compute |
| 2156 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 2157 | // on the next call. |
| 2158 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 2159 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 2160 | // -1 means a non-forced member offset (no decoration needed). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2161 | 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] | 2162 | glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2163 | { |
| 2164 | // this will get a positive value when deemed necessary |
| 2165 | nextOffset = -1; |
| 2166 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2167 | // override anything in currentOffset with user-set offset |
| 2168 | if (memberType.getQualifier().hasOffset()) |
| 2169 | currentOffset = memberType.getQualifier().layoutOffset; |
| 2170 | |
| 2171 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 2172 | // in which case the following code does not matter. But, that's not quite right |
| 2173 | // once cross-compilation unit GLSL validation is done, as the original user |
| 2174 | // settings are needed in layoutOffset, and then the following will come into play. |
| 2175 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2176 | if (explicitLayout == glslang::ElpNone) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2177 | if (! memberType.getQualifier().hasOffset()) |
| 2178 | currentOffset = -1; |
| 2179 | |
| 2180 | return; |
| 2181 | } |
| 2182 | |
John Kessenich | f85e806 | 2015-12-19 13:57:10 -0700 | [diff] [blame] | 2183 | // Getting this far means we need explicit offsets |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2184 | if (currentOffset < 0) |
| 2185 | currentOffset = 0; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2186 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2187 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 2188 | // but possibly not yet correctly aligned. |
| 2189 | |
| 2190 | int memberSize; |
John Kessenich | 4998789 | 2015-12-29 17:11:44 -0700 | [diff] [blame] | 2191 | int dummyStride; |
| 2192 | int memberAlignment = glslangIntermediate->getBaseAlignment(memberType, memberSize, dummyStride, explicitLayout == glslang::ElpStd140, matrixLayout == glslang::ElmRowMajor); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2193 | glslang::RoundToPow2(currentOffset, memberAlignment); |
| 2194 | nextOffset = currentOffset + memberSize; |
| 2195 | } |
| 2196 | |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 2197 | void TGlslangToSpvTraverser::declareClipCullCapability(const glslang::TTypeList& members, int member) |
| 2198 | { |
| 2199 | if (members[member].type->getQualifier().builtIn == glslang::EbvClipDistance) |
| 2200 | builder.addCapability(spv::CapabilityClipDistance); |
| 2201 | if (members[member].type->getQualifier().builtIn == glslang::EbvCullDistance) |
| 2202 | builder.addCapability(spv::CapabilityCullDistance); |
| 2203 | } |
| 2204 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2205 | bool TGlslangToSpvTraverser::isShaderEntrypoint(const glslang::TIntermAggregate* node) |
| 2206 | { |
John Kessenich | 4d65ee3 | 2016-03-12 18:17:47 -0700 | [diff] [blame] | 2207 | // have to ignore mangling and just look at the base name |
baldurk | 3cb57d3 | 2016-04-09 13:07:12 +0200 | [diff] [blame] | 2208 | size_t firstOpen = node->getName().find('('); |
John Kessenich | 7e3e486 | 2016-04-06 19:03:15 -0600 | [diff] [blame] | 2209 | return node->getName().compare(0, firstOpen, glslangIntermediate->getEntryPoint().c_str()) == 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 2213 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 2214 | { |
| 2215 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 2216 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
| 2217 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntrypoint(glslFunction)) |
| 2218 | continue; |
| 2219 | |
| 2220 | // We're on a user function. Set up the basic interface for the function now, |
| 2221 | // so that it's available to call. |
| 2222 | // Translating the body will happen later. |
| 2223 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2224 | // 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] | 2225 | // function. What it is an address of varies: |
| 2226 | // |
| 2227 | // - "in" parameters not marked as "const" can be written to without modifying the argument, |
| 2228 | // so that write needs to be to a copy, hence the address of a copy works. |
| 2229 | // |
| 2230 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 2231 | // |
| 2232 | // - "out" and "inout" arguments can't be done as direct pointers, because GLSL has |
| 2233 | // copy-in/copy-out semantics. They can be handled though with a pointer to a copy. |
| 2234 | |
| 2235 | std::vector<spv::Id> paramTypes; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2236 | std::vector<spv::Decoration> paramPrecisions; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2237 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 2238 | |
| 2239 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 2240 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 2241 | spv::Id typeId = convertGlslangToSpvType(paramType); |
| 2242 | if (paramType.getQualifier().storage != glslang::EvqConstReadOnly) |
| 2243 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 2244 | else |
| 2245 | constReadOnlyParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2246 | paramPrecisions.push_back(TranslatePrecisionDecoration(paramType)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2247 | paramTypes.push_back(typeId); |
| 2248 | } |
| 2249 | |
| 2250 | spv::Block* functionBlock; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2251 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 2252 | convertGlslangToSpvType(glslFunction->getType()), |
| 2253 | glslFunction->getName().c_str(), paramTypes, paramPrecisions, &functionBlock); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2254 | |
| 2255 | // Track function to emit/call later |
| 2256 | functionMap[glslFunction->getName().c_str()] = function; |
| 2257 | |
| 2258 | // Set the parameter id's |
| 2259 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 2260 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 2261 | // give a name too |
| 2262 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | |
| 2267 | // Process all the initializers, while skipping the functions and link objects |
| 2268 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 2269 | { |
| 2270 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 2271 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 2272 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 2273 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != glslang::EOpLinkerObjects) { |
| 2274 | |
| 2275 | // We're on a top-level node that's not a function. Treat as an initializer, whose |
| 2276 | // code goes into the beginning of main. |
| 2277 | initializer->traverse(this); |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | // Process all the functions, while skipping initializers. |
| 2283 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 2284 | { |
| 2285 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 2286 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
| 2287 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang ::EOpLinkerObjects)) |
| 2288 | node->traverse(this); |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 2293 | { |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2294 | // SPIR-V functions should already be in the functionMap from the prepass |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2295 | // that called makeFunctions(). |
| 2296 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 2297 | spv::Block* functionBlock = function->getEntryBlock(); |
| 2298 | builder.setBuildPoint(functionBlock); |
| 2299 | } |
| 2300 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 2301 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2302 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2303 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2304 | |
| 2305 | glslang::TSampler sampler = {}; |
| 2306 | bool cubeCompare = false; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 2307 | if (node.isTexture() || node.isImage()) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2308 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 2309 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 2310 | } |
| 2311 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2312 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 2313 | builder.clearAccessChain(); |
| 2314 | glslangArguments[i]->traverse(this); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2315 | |
| 2316 | // Special case l-value operands |
| 2317 | bool lvalue = false; |
| 2318 | switch (node.getOp()) { |
| 2319 | case glslang::EOpImageAtomicAdd: |
| 2320 | case glslang::EOpImageAtomicMin: |
| 2321 | case glslang::EOpImageAtomicMax: |
| 2322 | case glslang::EOpImageAtomicAnd: |
| 2323 | case glslang::EOpImageAtomicOr: |
| 2324 | case glslang::EOpImageAtomicXor: |
| 2325 | case glslang::EOpImageAtomicExchange: |
| 2326 | case glslang::EOpImageAtomicCompSwap: |
| 2327 | if (i == 0) |
| 2328 | lvalue = true; |
| 2329 | break; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 2330 | case glslang::EOpSparseImageLoad: |
| 2331 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 2332 | lvalue = true; |
| 2333 | break; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2334 | case glslang::EOpSparseTexture: |
| 2335 | if ((cubeCompare && i == 3) || (! cubeCompare && i == 2)) |
| 2336 | lvalue = true; |
| 2337 | break; |
| 2338 | case glslang::EOpSparseTextureClamp: |
| 2339 | if ((cubeCompare && i == 4) || (! cubeCompare && i == 3)) |
| 2340 | lvalue = true; |
| 2341 | break; |
| 2342 | case glslang::EOpSparseTextureLod: |
| 2343 | case glslang::EOpSparseTextureOffset: |
| 2344 | if (i == 3) |
| 2345 | lvalue = true; |
| 2346 | break; |
| 2347 | case glslang::EOpSparseTextureFetch: |
| 2348 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 2349 | lvalue = true; |
| 2350 | break; |
| 2351 | case glslang::EOpSparseTextureFetchOffset: |
| 2352 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 2353 | lvalue = true; |
| 2354 | break; |
| 2355 | case glslang::EOpSparseTextureLodOffset: |
| 2356 | case glslang::EOpSparseTextureGrad: |
| 2357 | case glslang::EOpSparseTextureOffsetClamp: |
| 2358 | if (i == 4) |
| 2359 | lvalue = true; |
| 2360 | break; |
| 2361 | case glslang::EOpSparseTextureGradOffset: |
| 2362 | case glslang::EOpSparseTextureGradClamp: |
| 2363 | if (i == 5) |
| 2364 | lvalue = true; |
| 2365 | break; |
| 2366 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 2367 | if (i == 6) |
| 2368 | lvalue = true; |
| 2369 | break; |
| 2370 | case glslang::EOpSparseTextureGather: |
| 2371 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 2372 | lvalue = true; |
| 2373 | break; |
| 2374 | case glslang::EOpSparseTextureGatherOffset: |
| 2375 | case glslang::EOpSparseTextureGatherOffsets: |
| 2376 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 2377 | lvalue = true; |
| 2378 | break; |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2379 | default: |
| 2380 | break; |
| 2381 | } |
| 2382 | |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 2383 | if (lvalue) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2384 | arguments.push_back(builder.accessChainGetLValue()); |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 2385 | else |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2386 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2387 | } |
| 2388 | } |
| 2389 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2390 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2391 | { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2392 | builder.clearAccessChain(); |
| 2393 | node.getOperand()->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2394 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2395 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2396 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2397 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 2398 | { |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2399 | if (! node->isImage() && ! node->isTexture()) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2400 | return spv::NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2401 | } |
| 2402 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2403 | // Process a GLSL texturing op (will be SPV image) |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2404 | const glslang::TSampler sampler = node->getAsAggregate() ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType().getSampler() |
| 2405 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType().getSampler(); |
| 2406 | std::vector<spv::Id> arguments; |
| 2407 | if (node->getAsAggregate()) |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2408 | translateArguments(*node->getAsAggregate(), arguments); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2409 | else |
| 2410 | translateArguments(*node->getAsUnaryNode(), arguments); |
| 2411 | spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
| 2412 | |
| 2413 | spv::Builder::TextureParameters params = { }; |
| 2414 | params.sampler = arguments[0]; |
| 2415 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 2416 | glslang::TCrackedTextureOp cracked; |
| 2417 | node->crackTexture(sampler, cracked); |
| 2418 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2419 | // Check for queries |
| 2420 | if (cracked.query) { |
John Kessenich | 3366145 | 2015-12-08 19:32:47 -0700 | [diff] [blame] | 2421 | // a sampled image needs to have the image extracted first |
| 2422 | if (builder.isSampledImage(params.sampler)) |
| 2423 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2424 | switch (node->getOp()) { |
| 2425 | case glslang::EOpImageQuerySize: |
| 2426 | case glslang::EOpTextureQuerySize: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2427 | if (arguments.size() > 1) { |
| 2428 | params.lod = arguments[1]; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2429 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2430 | } else |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2431 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2432 | case glslang::EOpImageQuerySamples: |
| 2433 | case glslang::EOpTextureQuerySamples: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2434 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2435 | case glslang::EOpTextureQueryLod: |
| 2436 | params.coords = arguments[1]; |
| 2437 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params); |
| 2438 | case glslang::EOpTextureQueryLevels: |
| 2439 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params); |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2440 | case glslang::EOpSparseTexelsResident: |
| 2441 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2442 | default: |
| 2443 | assert(0); |
| 2444 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2445 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2446 | } |
| 2447 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2448 | // Check for image functions other than queries |
| 2449 | if (node->isImage()) { |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 2450 | std::vector<spv::Id> operands; |
| 2451 | auto opIt = arguments.begin(); |
| 2452 | operands.push_back(*(opIt++)); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 2453 | |
| 2454 | // Handle subpass operations |
| 2455 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 2456 | // built-in function. |
| 2457 | if (cracked.subpass) { |
| 2458 | // add on the (0,0) coordinate |
| 2459 | spv::Id zero = builder.makeIntConstant(0); |
| 2460 | std::vector<spv::Id> comps; |
| 2461 | comps.push_back(zero); |
| 2462 | comps.push_back(zero); |
| 2463 | operands.push_back(builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 2464 | if (sampler.ms) { |
| 2465 | operands.push_back(spv::ImageOperandsSampleMask); |
| 2466 | operands.push_back(*(opIt++)); |
| 2467 | } |
| 2468 | return builder.createOp(spv::OpImageRead, convertGlslangToSpvType(node->getType()), operands); |
| 2469 | } |
| 2470 | |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 2471 | operands.push_back(*(opIt++)); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 2472 | if (node->getOp() == glslang::EOpImageLoad) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2473 | if (sampler.ms) { |
| 2474 | operands.push_back(spv::ImageOperandsSampleMask); |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 2475 | operands.push_back(*opIt); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2476 | } |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 2477 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 2478 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 2479 | return builder.createOp(spv::OpImageRead, convertGlslangToSpvType(node->getType()), operands); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 2480 | } else if (node->getOp() == glslang::EOpImageStore) { |
Rex Xu | 7beb441 | 2015-12-15 17:52:45 +0800 | [diff] [blame] | 2481 | if (sampler.ms) { |
| 2482 | operands.push_back(*(opIt + 1)); |
| 2483 | operands.push_back(spv::ImageOperandsSampleMask); |
| 2484 | operands.push_back(*opIt); |
| 2485 | } else |
| 2486 | operands.push_back(*opIt); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 2487 | builder.createNoResultOp(spv::OpImageWrite, operands); |
John Kessenich | 5d0fa97 | 2016-02-15 11:57:00 -0700 | [diff] [blame] | 2488 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 2489 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 2490 | return spv::NoResult; |
Rex Xu | 5eafa47 | 2016-02-19 22:24:03 +0800 | [diff] [blame] | 2491 | } else if (node->getOp() == glslang::EOpSparseImageLoad) { |
| 2492 | builder.addCapability(spv::CapabilitySparseResidency); |
| 2493 | if (builder.getImageTypeFormat(builder.getImageType(operands.front())) == spv::ImageFormatUnknown) |
| 2494 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 2495 | |
| 2496 | if (sampler.ms) { |
| 2497 | operands.push_back(spv::ImageOperandsSampleMask); |
| 2498 | operands.push_back(*opIt++); |
| 2499 | } |
| 2500 | |
| 2501 | // Create the return type that was a special structure |
| 2502 | spv::Id texelOut = *opIt; |
| 2503 | spv::Id typeId0 = convertGlslangToSpvType(node->getType()); |
| 2504 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 2505 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 2506 | |
| 2507 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 2508 | |
| 2509 | // Decode the return type |
| 2510 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 2511 | return builder.createCompositeExtract(resultId, typeId0, 0); |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 2512 | } else { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 2513 | // Process image atomic operations |
| 2514 | |
| 2515 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 2516 | // as the first source operand, is required by SPIR-V atomic operations. |
John Kessenich | cd26144 | 2016-01-22 09:54:12 -0700 | [diff] [blame] | 2517 | 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] | 2518 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2519 | spv::Id resultTypeId = builder.makePointer(spv::StorageClassImage, convertGlslangToSpvType(node->getType())); |
John Kessenich | 56bab04 | 2015-09-16 10:54:31 -0600 | [diff] [blame] | 2520 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2521 | |
| 2522 | std::vector<spv::Id> operands; |
| 2523 | operands.push_back(pointer); |
| 2524 | for (; opIt != arguments.end(); ++opIt) |
| 2525 | operands.push_back(*opIt); |
| 2526 | |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 2527 | return createAtomicOperation(node->getOp(), precision, convertGlslangToSpvType(node->getType()), operands, node->getBasicType()); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | // Check for texture functions other than queries |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2532 | bool sparse = node->isSparseTexture(); |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 2533 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 2534 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2535 | // check for bias argument |
| 2536 | bool bias = false; |
Rex Xu | 71519fe | 2015-11-11 15:35:47 +0800 | [diff] [blame] | 2537 | if (! cracked.lod && ! cracked.gather && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2538 | int nonBiasArgCount = 2; |
| 2539 | if (cracked.offset) |
| 2540 | ++nonBiasArgCount; |
| 2541 | if (cracked.grad) |
| 2542 | nonBiasArgCount += 2; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2543 | if (cracked.lodClamp) |
| 2544 | ++nonBiasArgCount; |
| 2545 | if (sparse) |
| 2546 | ++nonBiasArgCount; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2547 | |
| 2548 | if ((int)arguments.size() > nonBiasArgCount) |
| 2549 | bias = true; |
| 2550 | } |
| 2551 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2552 | // set the rest of the arguments |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2553 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2554 | params.coords = arguments[1]; |
| 2555 | int extraArgs = 0; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 2556 | bool noImplicitLod = false; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2557 | |
| 2558 | // sort out where Dref is coming from |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2559 | if (cubeCompare) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2560 | params.Dref = arguments[2]; |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2561 | ++extraArgs; |
| 2562 | } else if (sampler.shadow && cracked.gather) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2563 | params.Dref = arguments[2]; |
| 2564 | ++extraArgs; |
| 2565 | } else if (sampler.shadow) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2566 | std::vector<spv::Id> indexes; |
| 2567 | int comp; |
| 2568 | if (cracked.proj) |
John Kessenich | 6feb498 | 2015-12-13 12:23:33 -0700 | [diff] [blame] | 2569 | comp = 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] | 2570 | else |
| 2571 | comp = builder.getNumComponents(params.coords) - 1; |
| 2572 | indexes.push_back(comp); |
| 2573 | params.Dref = builder.createCompositeExtract(params.coords, builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 2574 | } |
| 2575 | if (cracked.lod) { |
| 2576 | params.lod = arguments[2]; |
| 2577 | ++extraArgs; |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 2578 | } else if (glslangIntermediate->getStage() != EShLangFragment) { |
| 2579 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 2580 | noImplicitLod = true; |
| 2581 | } |
| 2582 | if (sampler.ms) { |
Rex Xu | 6b86d49 | 2015-09-16 17:48:22 +0800 | [diff] [blame] | 2583 | params.sample = arguments[2]; // For MS, "sample" should be specified |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 2584 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2585 | } |
| 2586 | if (cracked.grad) { |
| 2587 | params.gradX = arguments[2 + extraArgs]; |
| 2588 | params.gradY = arguments[3 + extraArgs]; |
| 2589 | extraArgs += 2; |
| 2590 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2591 | if (cracked.offset) { |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2592 | params.offset = arguments[2 + extraArgs]; |
| 2593 | ++extraArgs; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2594 | } else if (cracked.offsets) { |
| 2595 | params.offsets = arguments[2 + extraArgs]; |
| 2596 | ++extraArgs; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2597 | } |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 2598 | if (cracked.lodClamp) { |
| 2599 | params.lodClamp = arguments[2 + extraArgs]; |
| 2600 | ++extraArgs; |
| 2601 | } |
| 2602 | if (sparse) { |
| 2603 | params.texelOut = arguments[2 + extraArgs]; |
| 2604 | ++extraArgs; |
| 2605 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2606 | if (bias) { |
| 2607 | params.bias = arguments[2 + extraArgs]; |
| 2608 | ++extraArgs; |
| 2609 | } |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2610 | if (cracked.gather && ! sampler.shadow) { |
| 2611 | // default component is 0, if missing, otherwise an argument |
| 2612 | if (2 + extraArgs < (int)arguments.size()) { |
| 2613 | params.comp = arguments[2 + extraArgs]; |
| 2614 | ++extraArgs; |
| 2615 | } else { |
| 2616 | params.comp = builder.makeIntConstant(0); |
| 2617 | } |
| 2618 | } |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 2619 | |
John Kessenich | 019f08f | 2016-02-15 15:40:42 -0700 | [diff] [blame] | 2620 | return builder.createTextureCall(precision, convertGlslangToSpvType(node->getType()), sparse, cracked.fetch, cracked.proj, cracked.gather, noImplicitLod, params); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 2624 | { |
| 2625 | // Grab the function's pointer from the previously created function |
| 2626 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 2627 | if (! function) |
| 2628 | return 0; |
| 2629 | |
| 2630 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 2631 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 2632 | |
| 2633 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 2634 | // |
| 2635 | // These imply we need a four step process: |
| 2636 | // 1. Evaluate the arguments |
| 2637 | // 2. Allocate and make copies of in, out, and inout arguments |
| 2638 | // 3. Make the call |
| 2639 | // 4. Copy back the results |
| 2640 | |
| 2641 | // 1. Evaluate the arguments |
| 2642 | std::vector<spv::Builder::AccessChain> lValues; |
| 2643 | std::vector<spv::Id> rValues; |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2644 | std::vector<const glslang::TType*> argTypes; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2645 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 2646 | // build l-value |
| 2647 | builder.clearAccessChain(); |
| 2648 | glslangArgs[a]->traverse(this); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2649 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2650 | // keep outputs as l-values, evaluate input-only as r-values |
| 2651 | if (qualifiers[a] != glslang::EvqConstReadOnly) { |
| 2652 | // save l-value |
| 2653 | lValues.push_back(builder.getAccessChain()); |
| 2654 | } else { |
| 2655 | // process r-value |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2656 | rValues.push_back(accessChainLoad(*argTypes.back())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | |
| 2660 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 2661 | // copy the original into that space. |
| 2662 | // |
| 2663 | // Also, build up the list of actual arguments to pass in for the call |
| 2664 | int lValueCount = 0; |
| 2665 | int rValueCount = 0; |
| 2666 | std::vector<spv::Id> spvArgs; |
| 2667 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 2668 | spv::Id arg; |
| 2669 | if (qualifiers[a] != glslang::EvqConstReadOnly) { |
| 2670 | // need space to hold the copy |
| 2671 | const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType(); |
| 2672 | arg = builder.createVariable(spv::StorageClassFunction, convertGlslangToSpvType(paramType), "param"); |
| 2673 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 2674 | // need to copy the input into output space |
| 2675 | builder.setAccessChain(lValues[lValueCount]); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2676 | spv::Id copy = accessChainLoad(*argTypes[a]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2677 | builder.createStore(copy, arg); |
| 2678 | } |
| 2679 | ++lValueCount; |
| 2680 | } else { |
| 2681 | arg = rValues[rValueCount]; |
| 2682 | ++rValueCount; |
| 2683 | } |
| 2684 | spvArgs.push_back(arg); |
| 2685 | } |
| 2686 | |
| 2687 | // 3. Make the call. |
| 2688 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 2689 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2690 | |
| 2691 | // 4. Copy back out an "out" arguments. |
| 2692 | lValueCount = 0; |
| 2693 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 2694 | if (qualifiers[a] != glslang::EvqConstReadOnly) { |
| 2695 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 2696 | spv::Id copy = builder.createLoad(spvArgs[a]); |
| 2697 | builder.setAccessChain(lValues[lValueCount]); |
Rex Xu | 2725323 | 2016-02-23 17:51:09 +0800 | [diff] [blame] | 2698 | accessChainStore(glslangArgs[a]->getAsTyped()->getType(), copy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2699 | } |
| 2700 | ++lValueCount; |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | return result; |
| 2705 | } |
| 2706 | |
| 2707 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2708 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, spv::Decoration precision, |
| 2709 | spv::Decoration noContraction, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2710 | spv::Id typeId, spv::Id left, spv::Id right, |
| 2711 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 2712 | { |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 2713 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2714 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 2715 | bool isBool = typeProxy == glslang::EbtBool; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2716 | |
| 2717 | spv::Op binOp = spv::OpNop; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 2718 | 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] | 2719 | bool comparison = false; |
| 2720 | |
| 2721 | switch (op) { |
| 2722 | case glslang::EOpAdd: |
| 2723 | case glslang::EOpAddAssign: |
| 2724 | if (isFloat) |
| 2725 | binOp = spv::OpFAdd; |
| 2726 | else |
| 2727 | binOp = spv::OpIAdd; |
| 2728 | break; |
| 2729 | case glslang::EOpSub: |
| 2730 | case glslang::EOpSubAssign: |
| 2731 | if (isFloat) |
| 2732 | binOp = spv::OpFSub; |
| 2733 | else |
| 2734 | binOp = spv::OpISub; |
| 2735 | break; |
| 2736 | case glslang::EOpMul: |
| 2737 | case glslang::EOpMulAssign: |
| 2738 | if (isFloat) |
| 2739 | binOp = spv::OpFMul; |
| 2740 | else |
| 2741 | binOp = spv::OpIMul; |
| 2742 | break; |
| 2743 | case glslang::EOpVectorTimesScalar: |
| 2744 | case glslang::EOpVectorTimesScalarAssign: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 2745 | if (isFloat) { |
| 2746 | if (builder.isVector(right)) |
| 2747 | std::swap(left, right); |
| 2748 | assert(builder.isScalar(right)); |
| 2749 | needMatchingVectors = false; |
| 2750 | binOp = spv::OpVectorTimesScalar; |
| 2751 | } else |
| 2752 | binOp = spv::OpIMul; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2753 | break; |
| 2754 | case glslang::EOpVectorTimesMatrix: |
| 2755 | case glslang::EOpVectorTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2756 | binOp = spv::OpVectorTimesMatrix; |
| 2757 | break; |
| 2758 | case glslang::EOpMatrixTimesVector: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2759 | binOp = spv::OpMatrixTimesVector; |
| 2760 | break; |
| 2761 | case glslang::EOpMatrixTimesScalar: |
| 2762 | case glslang::EOpMatrixTimesScalarAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2763 | binOp = spv::OpMatrixTimesScalar; |
| 2764 | break; |
| 2765 | case glslang::EOpMatrixTimesMatrix: |
| 2766 | case glslang::EOpMatrixTimesMatrixAssign: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2767 | binOp = spv::OpMatrixTimesMatrix; |
| 2768 | break; |
| 2769 | case glslang::EOpOuterProduct: |
| 2770 | binOp = spv::OpOuterProduct; |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 2771 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2772 | break; |
| 2773 | |
| 2774 | case glslang::EOpDiv: |
| 2775 | case glslang::EOpDivAssign: |
| 2776 | if (isFloat) |
| 2777 | binOp = spv::OpFDiv; |
| 2778 | else if (isUnsigned) |
| 2779 | binOp = spv::OpUDiv; |
| 2780 | else |
| 2781 | binOp = spv::OpSDiv; |
| 2782 | break; |
| 2783 | case glslang::EOpMod: |
| 2784 | case glslang::EOpModAssign: |
| 2785 | if (isFloat) |
| 2786 | binOp = spv::OpFMod; |
| 2787 | else if (isUnsigned) |
| 2788 | binOp = spv::OpUMod; |
| 2789 | else |
| 2790 | binOp = spv::OpSMod; |
| 2791 | break; |
| 2792 | case glslang::EOpRightShift: |
| 2793 | case glslang::EOpRightShiftAssign: |
| 2794 | if (isUnsigned) |
| 2795 | binOp = spv::OpShiftRightLogical; |
| 2796 | else |
| 2797 | binOp = spv::OpShiftRightArithmetic; |
| 2798 | break; |
| 2799 | case glslang::EOpLeftShift: |
| 2800 | case glslang::EOpLeftShiftAssign: |
| 2801 | binOp = spv::OpShiftLeftLogical; |
| 2802 | break; |
| 2803 | case glslang::EOpAnd: |
| 2804 | case glslang::EOpAndAssign: |
| 2805 | binOp = spv::OpBitwiseAnd; |
| 2806 | break; |
| 2807 | case glslang::EOpLogicalAnd: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 2808 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2809 | binOp = spv::OpLogicalAnd; |
| 2810 | break; |
| 2811 | case glslang::EOpInclusiveOr: |
| 2812 | case glslang::EOpInclusiveOrAssign: |
| 2813 | binOp = spv::OpBitwiseOr; |
| 2814 | break; |
| 2815 | case glslang::EOpLogicalOr: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 2816 | needMatchingVectors = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2817 | binOp = spv::OpLogicalOr; |
| 2818 | break; |
| 2819 | case glslang::EOpExclusiveOr: |
| 2820 | case glslang::EOpExclusiveOrAssign: |
| 2821 | binOp = spv::OpBitwiseXor; |
| 2822 | break; |
| 2823 | case glslang::EOpLogicalXor: |
John Kessenich | ec43d0a | 2015-07-04 17:17:31 -0600 | [diff] [blame] | 2824 | needMatchingVectors = false; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 2825 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2826 | break; |
| 2827 | |
| 2828 | case glslang::EOpLessThan: |
| 2829 | case glslang::EOpGreaterThan: |
| 2830 | case glslang::EOpLessThanEqual: |
| 2831 | case glslang::EOpGreaterThanEqual: |
| 2832 | case glslang::EOpEqual: |
| 2833 | case glslang::EOpNotEqual: |
| 2834 | case glslang::EOpVectorEqual: |
| 2835 | case glslang::EOpVectorNotEqual: |
| 2836 | comparison = true; |
| 2837 | break; |
| 2838 | default: |
| 2839 | break; |
| 2840 | } |
| 2841 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 2842 | // handle mapped binary operations (should be non-comparison) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2843 | if (binOp != spv::OpNop) { |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 2844 | assert(comparison == false); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 2845 | if (builder.isMatrix(left) || builder.isMatrix(right)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2846 | return createBinaryMatrixOperation(binOp, precision, noContraction, typeId, left, right); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2847 | |
| 2848 | // 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] | 2849 | if (needMatchingVectors) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2850 | builder.promoteScalar(precision, left, right); |
| 2851 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2852 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 2853 | addDecoration(result, noContraction); |
| 2854 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | if (! comparison) |
| 2858 | return 0; |
| 2859 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 2860 | // Handle comparison instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2861 | |
| 2862 | if (reduceComparison && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 2863 | assert(op == glslang::EOpEqual || op == glslang::EOpNotEqual); |
| 2864 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 2865 | return builder.createCompositeCompare(precision, left, right, op == glslang::EOpEqual); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | switch (op) { |
| 2869 | case glslang::EOpLessThan: |
| 2870 | if (isFloat) |
| 2871 | binOp = spv::OpFOrdLessThan; |
| 2872 | else if (isUnsigned) |
| 2873 | binOp = spv::OpULessThan; |
| 2874 | else |
| 2875 | binOp = spv::OpSLessThan; |
| 2876 | break; |
| 2877 | case glslang::EOpGreaterThan: |
| 2878 | if (isFloat) |
| 2879 | binOp = spv::OpFOrdGreaterThan; |
| 2880 | else if (isUnsigned) |
| 2881 | binOp = spv::OpUGreaterThan; |
| 2882 | else |
| 2883 | binOp = spv::OpSGreaterThan; |
| 2884 | break; |
| 2885 | case glslang::EOpLessThanEqual: |
| 2886 | if (isFloat) |
| 2887 | binOp = spv::OpFOrdLessThanEqual; |
| 2888 | else if (isUnsigned) |
| 2889 | binOp = spv::OpULessThanEqual; |
| 2890 | else |
| 2891 | binOp = spv::OpSLessThanEqual; |
| 2892 | break; |
| 2893 | case glslang::EOpGreaterThanEqual: |
| 2894 | if (isFloat) |
| 2895 | binOp = spv::OpFOrdGreaterThanEqual; |
| 2896 | else if (isUnsigned) |
| 2897 | binOp = spv::OpUGreaterThanEqual; |
| 2898 | else |
| 2899 | binOp = spv::OpSGreaterThanEqual; |
| 2900 | break; |
| 2901 | case glslang::EOpEqual: |
| 2902 | case glslang::EOpVectorEqual: |
| 2903 | if (isFloat) |
| 2904 | binOp = spv::OpFOrdEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 2905 | else if (isBool) |
| 2906 | binOp = spv::OpLogicalEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2907 | else |
| 2908 | binOp = spv::OpIEqual; |
| 2909 | break; |
| 2910 | case glslang::EOpNotEqual: |
| 2911 | case glslang::EOpVectorNotEqual: |
| 2912 | if (isFloat) |
| 2913 | binOp = spv::OpFOrdNotEqual; |
Rex Xu | c7d3656 | 2016-04-27 08:15:37 +0800 | [diff] [blame] | 2914 | else if (isBool) |
| 2915 | binOp = spv::OpLogicalNotEqual; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2916 | else |
| 2917 | binOp = spv::OpINotEqual; |
| 2918 | break; |
| 2919 | default: |
| 2920 | break; |
| 2921 | } |
| 2922 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2923 | if (binOp != spv::OpNop) { |
| 2924 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 2925 | addDecoration(result, noContraction); |
| 2926 | return builder.setPrecision(result, precision); |
| 2927 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2928 | |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 2932 | // |
| 2933 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 2934 | // These can be any of: |
| 2935 | // |
| 2936 | // matrix * scalar |
| 2937 | // scalar * matrix |
| 2938 | // matrix * matrix linear algebraic |
| 2939 | // matrix * vector |
| 2940 | // vector * matrix |
| 2941 | // matrix * matrix componentwise |
| 2942 | // matrix op matrix op in {+, -, /} |
| 2943 | // matrix op scalar op in {+, -, /} |
| 2944 | // scalar op matrix op in {+, -, /} |
| 2945 | // |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2946 | 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] | 2947 | { |
| 2948 | bool firstClass = true; |
| 2949 | |
| 2950 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 2951 | switch (op) { |
| 2952 | case spv::OpFDiv: |
| 2953 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 2954 | // turn matrix / scalar into a multiply... |
| 2955 | right = builder.createBinOp(spv::OpFDiv, builder.getTypeId(right), builder.makeFloatConstant(1.0F), right); |
| 2956 | op = spv::OpMatrixTimesScalar; |
| 2957 | } else |
| 2958 | firstClass = false; |
| 2959 | break; |
| 2960 | case spv::OpMatrixTimesScalar: |
| 2961 | if (builder.isMatrix(right)) |
| 2962 | std::swap(left, right); |
| 2963 | assert(builder.isScalar(right)); |
| 2964 | break; |
| 2965 | case spv::OpVectorTimesMatrix: |
| 2966 | assert(builder.isVector(left)); |
| 2967 | assert(builder.isMatrix(right)); |
| 2968 | break; |
| 2969 | case spv::OpMatrixTimesVector: |
| 2970 | assert(builder.isMatrix(left)); |
| 2971 | assert(builder.isVector(right)); |
| 2972 | break; |
| 2973 | case spv::OpMatrixTimesMatrix: |
| 2974 | assert(builder.isMatrix(left)); |
| 2975 | assert(builder.isMatrix(right)); |
| 2976 | break; |
| 2977 | default: |
| 2978 | firstClass = false; |
| 2979 | break; |
| 2980 | } |
| 2981 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 2982 | if (firstClass) { |
| 2983 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
| 2984 | addDecoration(result, noContraction); |
| 2985 | return builder.setPrecision(result, precision); |
| 2986 | } |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 2987 | |
| 2988 | // Handle component-wise +, -, *, and / for all combinations of type. |
| 2989 | // The result type of all of them is the same type as the (a) matrix operand. |
| 2990 | // The algorithm is to: |
| 2991 | // - break the matrix(es) into vectors |
| 2992 | // - smear any scalar to a vector |
| 2993 | // - do vector operations |
| 2994 | // - make a matrix out the vector results |
| 2995 | switch (op) { |
| 2996 | case spv::OpFAdd: |
| 2997 | case spv::OpFSub: |
| 2998 | case spv::OpFDiv: |
| 2999 | case spv::OpFMul: |
| 3000 | { |
| 3001 | // one time set up... |
| 3002 | bool leftMat = builder.isMatrix(left); |
| 3003 | bool rightMat = builder.isMatrix(right); |
| 3004 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 3005 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 3006 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 3007 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 3008 | std::vector<spv::Id> results; |
| 3009 | spv::Id smearVec = spv::NoResult; |
| 3010 | if (builder.isScalar(left)) |
| 3011 | smearVec = builder.smearScalar(precision, left, vecType); |
| 3012 | else if (builder.isScalar(right)) |
| 3013 | smearVec = builder.smearScalar(precision, right, vecType); |
| 3014 | |
| 3015 | // do each vector op |
| 3016 | for (unsigned int c = 0; c < numCols; ++c) { |
| 3017 | std::vector<unsigned int> indexes; |
| 3018 | indexes.push_back(c); |
| 3019 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 3020 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3021 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
| 3022 | addDecoration(result, noContraction); |
| 3023 | results.push_back(builder.setPrecision(result, precision)); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3027 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 04bb8a0 | 2015-12-12 12:28:14 -0700 | [diff] [blame] | 3028 | } |
| 3029 | default: |
| 3030 | assert(0); |
| 3031 | return spv::NoResult; |
| 3032 | } |
| 3033 | } |
| 3034 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3035 | 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] | 3036 | { |
| 3037 | spv::Op unaryOp = spv::OpNop; |
| 3038 | int libCall = -1; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3039 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3040 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3041 | |
| 3042 | switch (op) { |
| 3043 | case glslang::EOpNegative: |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3044 | if (isFloat) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3045 | unaryOp = spv::OpFNegate; |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3046 | if (builder.isMatrixType(typeId)) |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3047 | return createUnaryMatrixOperation(unaryOp, precision, noContraction, typeId, operand, typeProxy); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3048 | } else |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3049 | unaryOp = spv::OpSNegate; |
| 3050 | break; |
| 3051 | |
| 3052 | case glslang::EOpLogicalNot: |
| 3053 | case glslang::EOpVectorLogicalNot: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3054 | unaryOp = spv::OpLogicalNot; |
| 3055 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3056 | case glslang::EOpBitwiseNot: |
| 3057 | unaryOp = spv::OpNot; |
| 3058 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3059 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3060 | case glslang::EOpDeterminant: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3061 | libCall = spv::GLSLstd450Determinant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3062 | break; |
| 3063 | case glslang::EOpMatrixInverse: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3064 | libCall = spv::GLSLstd450MatrixInverse; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3065 | break; |
| 3066 | case glslang::EOpTranspose: |
| 3067 | unaryOp = spv::OpTranspose; |
| 3068 | break; |
| 3069 | |
| 3070 | case glslang::EOpRadians: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3071 | libCall = spv::GLSLstd450Radians; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3072 | break; |
| 3073 | case glslang::EOpDegrees: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3074 | libCall = spv::GLSLstd450Degrees; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3075 | break; |
| 3076 | case glslang::EOpSin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3077 | libCall = spv::GLSLstd450Sin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3078 | break; |
| 3079 | case glslang::EOpCos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3080 | libCall = spv::GLSLstd450Cos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3081 | break; |
| 3082 | case glslang::EOpTan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3083 | libCall = spv::GLSLstd450Tan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3084 | break; |
| 3085 | case glslang::EOpAcos: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3086 | libCall = spv::GLSLstd450Acos; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3087 | break; |
| 3088 | case glslang::EOpAsin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3089 | libCall = spv::GLSLstd450Asin; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3090 | break; |
| 3091 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3092 | libCall = spv::GLSLstd450Atan; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3093 | break; |
| 3094 | |
| 3095 | case glslang::EOpAcosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3096 | libCall = spv::GLSLstd450Acosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3097 | break; |
| 3098 | case glslang::EOpAsinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3099 | libCall = spv::GLSLstd450Asinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3100 | break; |
| 3101 | case glslang::EOpAtanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3102 | libCall = spv::GLSLstd450Atanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3103 | break; |
| 3104 | case glslang::EOpTanh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3105 | libCall = spv::GLSLstd450Tanh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3106 | break; |
| 3107 | case glslang::EOpCosh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3108 | libCall = spv::GLSLstd450Cosh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3109 | break; |
| 3110 | case glslang::EOpSinh: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3111 | libCall = spv::GLSLstd450Sinh; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3112 | break; |
| 3113 | |
| 3114 | case glslang::EOpLength: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3115 | libCall = spv::GLSLstd450Length; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3116 | break; |
| 3117 | case glslang::EOpNormalize: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3118 | libCall = spv::GLSLstd450Normalize; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3119 | break; |
| 3120 | |
| 3121 | case glslang::EOpExp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3122 | libCall = spv::GLSLstd450Exp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3123 | break; |
| 3124 | case glslang::EOpLog: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3125 | libCall = spv::GLSLstd450Log; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3126 | break; |
| 3127 | case glslang::EOpExp2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3128 | libCall = spv::GLSLstd450Exp2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3129 | break; |
| 3130 | case glslang::EOpLog2: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3131 | libCall = spv::GLSLstd450Log2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3132 | break; |
| 3133 | case glslang::EOpSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3134 | libCall = spv::GLSLstd450Sqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3135 | break; |
| 3136 | case glslang::EOpInverseSqrt: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3137 | libCall = spv::GLSLstd450InverseSqrt; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3138 | break; |
| 3139 | |
| 3140 | case glslang::EOpFloor: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3141 | libCall = spv::GLSLstd450Floor; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3142 | break; |
| 3143 | case glslang::EOpTrunc: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3144 | libCall = spv::GLSLstd450Trunc; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3145 | break; |
| 3146 | case glslang::EOpRound: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3147 | libCall = spv::GLSLstd450Round; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3148 | break; |
| 3149 | case glslang::EOpRoundEven: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3150 | libCall = spv::GLSLstd450RoundEven; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3151 | break; |
| 3152 | case glslang::EOpCeil: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3153 | libCall = spv::GLSLstd450Ceil; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3154 | break; |
| 3155 | case glslang::EOpFract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3156 | libCall = spv::GLSLstd450Fract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3157 | break; |
| 3158 | |
| 3159 | case glslang::EOpIsNan: |
| 3160 | unaryOp = spv::OpIsNan; |
| 3161 | break; |
| 3162 | case glslang::EOpIsInf: |
| 3163 | unaryOp = spv::OpIsInf; |
| 3164 | break; |
| 3165 | |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 3166 | case glslang::EOpFloatBitsToInt: |
| 3167 | case glslang::EOpFloatBitsToUint: |
| 3168 | case glslang::EOpIntBitsToFloat: |
| 3169 | case glslang::EOpUintBitsToFloat: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3170 | case glslang::EOpDoubleBitsToInt64: |
| 3171 | case glslang::EOpDoubleBitsToUint64: |
| 3172 | case glslang::EOpInt64BitsToDouble: |
| 3173 | case glslang::EOpUint64BitsToDouble: |
Rex Xu | cbc426e | 2015-12-15 16:03:10 +0800 | [diff] [blame] | 3174 | unaryOp = spv::OpBitcast; |
| 3175 | break; |
| 3176 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3177 | case glslang::EOpPackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3178 | libCall = spv::GLSLstd450PackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3179 | break; |
| 3180 | case glslang::EOpUnpackSnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3181 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3182 | break; |
| 3183 | case glslang::EOpPackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3184 | libCall = spv::GLSLstd450PackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3185 | break; |
| 3186 | case glslang::EOpUnpackUnorm2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3187 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3188 | break; |
| 3189 | case glslang::EOpPackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3190 | libCall = spv::GLSLstd450PackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3191 | break; |
| 3192 | case glslang::EOpUnpackHalf2x16: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3193 | libCall = spv::GLSLstd450UnpackHalf2x16; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3194 | break; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3195 | case glslang::EOpPackSnorm4x8: |
| 3196 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 3197 | break; |
| 3198 | case glslang::EOpUnpackSnorm4x8: |
| 3199 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 3200 | break; |
| 3201 | case glslang::EOpPackUnorm4x8: |
| 3202 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 3203 | break; |
| 3204 | case glslang::EOpUnpackUnorm4x8: |
| 3205 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 3206 | break; |
| 3207 | case glslang::EOpPackDouble2x32: |
| 3208 | libCall = spv::GLSLstd450PackDouble2x32; |
| 3209 | break; |
| 3210 | case glslang::EOpUnpackDouble2x32: |
| 3211 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 3212 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3213 | |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3214 | case glslang::EOpPackInt2x32: |
| 3215 | case glslang::EOpUnpackInt2x32: |
| 3216 | case glslang::EOpPackUint2x32: |
| 3217 | case glslang::EOpUnpackUint2x32: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 3218 | logger->missingFunctionality("shader int64"); |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3219 | libCall = spv::GLSLstd450Bad; // TODO: This is a placeholder. |
| 3220 | break; |
| 3221 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3222 | case glslang::EOpDPdx: |
| 3223 | unaryOp = spv::OpDPdx; |
| 3224 | break; |
| 3225 | case glslang::EOpDPdy: |
| 3226 | unaryOp = spv::OpDPdy; |
| 3227 | break; |
| 3228 | case glslang::EOpFwidth: |
| 3229 | unaryOp = spv::OpFwidth; |
| 3230 | break; |
| 3231 | case glslang::EOpDPdxFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3232 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3233 | unaryOp = spv::OpDPdxFine; |
| 3234 | break; |
| 3235 | case glslang::EOpDPdyFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3236 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3237 | unaryOp = spv::OpDPdyFine; |
| 3238 | break; |
| 3239 | case glslang::EOpFwidthFine: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3240 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3241 | unaryOp = spv::OpFwidthFine; |
| 3242 | break; |
| 3243 | case glslang::EOpDPdxCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3244 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3245 | unaryOp = spv::OpDPdxCoarse; |
| 3246 | break; |
| 3247 | case glslang::EOpDPdyCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3248 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3249 | unaryOp = spv::OpDPdyCoarse; |
| 3250 | break; |
| 3251 | case glslang::EOpFwidthCoarse: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3252 | builder.addCapability(spv::CapabilityDerivativeControl); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3253 | unaryOp = spv::OpFwidthCoarse; |
| 3254 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 3255 | case glslang::EOpInterpolateAtCentroid: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3256 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 3257 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 3258 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3259 | case glslang::EOpAny: |
| 3260 | unaryOp = spv::OpAny; |
| 3261 | break; |
| 3262 | case glslang::EOpAll: |
| 3263 | unaryOp = spv::OpAll; |
| 3264 | break; |
| 3265 | |
| 3266 | case glslang::EOpAbs: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3267 | if (isFloat) |
| 3268 | libCall = spv::GLSLstd450FAbs; |
| 3269 | else |
| 3270 | libCall = spv::GLSLstd450SAbs; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3271 | break; |
| 3272 | case glslang::EOpSign: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3273 | if (isFloat) |
| 3274 | libCall = spv::GLSLstd450FSign; |
| 3275 | else |
| 3276 | libCall = spv::GLSLstd450SSign; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3277 | break; |
| 3278 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3279 | case glslang::EOpAtomicCounterIncrement: |
| 3280 | case glslang::EOpAtomicCounterDecrement: |
| 3281 | case glslang::EOpAtomicCounter: |
| 3282 | { |
| 3283 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 3284 | std::vector<spv::Id> operands; |
| 3285 | operands.push_back(operand); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3286 | return createAtomicOperation(op, precision, typeId, operands, typeProxy); |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3287 | } |
| 3288 | |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3289 | case glslang::EOpBitFieldReverse: |
| 3290 | unaryOp = spv::OpBitReverse; |
| 3291 | break; |
| 3292 | case glslang::EOpBitCount: |
| 3293 | unaryOp = spv::OpBitCount; |
| 3294 | break; |
| 3295 | case glslang::EOpFindLSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3296 | libCall = spv::GLSLstd450FindILsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3297 | break; |
| 3298 | case glslang::EOpFindMSB: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3299 | if (isUnsigned) |
| 3300 | libCall = spv::GLSLstd450FindUMsb; |
| 3301 | else |
| 3302 | libCall = spv::GLSLstd450FindSMsb; |
John Kessenich | fc51d28 | 2015-08-19 13:34:18 -0600 | [diff] [blame] | 3303 | break; |
| 3304 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 3305 | case glslang::EOpBallot: |
| 3306 | case glslang::EOpReadFirstInvocation: |
John Kessenich | c8a5676 | 2016-05-05 12:04:22 -0600 | [diff] [blame] | 3307 | logger->missingFunctionality("shader ballot"); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 3308 | libCall = spv::GLSLstd450Bad; |
| 3309 | break; |
| 3310 | |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 3311 | case glslang::EOpAnyInvocation: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 3312 | case glslang::EOpAllInvocations: |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 3313 | case glslang::EOpAllInvocationsEqual: |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 3314 | return createInvocationsOperation(op, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 3315 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3316 | default: |
| 3317 | return 0; |
| 3318 | } |
| 3319 | |
| 3320 | spv::Id id; |
| 3321 | if (libCall >= 0) { |
| 3322 | std::vector<spv::Id> args; |
| 3323 | args.push_back(operand); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3324 | id = builder.createBuiltinCall(typeId, stdBuiltins, libCall, args); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 3325 | } else { |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 3326 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
Rex Xu | 338b185 | 2016-05-05 20:38:33 +0800 | [diff] [blame] | 3327 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3328 | |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3329 | addDecoration(id, noContraction); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3330 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3331 | } |
| 3332 | |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3333 | // Create a unary operation on a matrix |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 3334 | 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] | 3335 | { |
| 3336 | // Handle unary operations vector by vector. |
| 3337 | // The result type is the same type as the original type. |
| 3338 | // The algorithm is to: |
| 3339 | // - break the matrix into vectors |
| 3340 | // - apply the operation to each vector |
| 3341 | // - make a matrix out the vector results |
| 3342 | |
| 3343 | // get the types sorted out |
| 3344 | int numCols = builder.getNumColumns(operand); |
| 3345 | int numRows = builder.getNumRows(operand); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 3346 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 3347 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3348 | std::vector<spv::Id> results; |
| 3349 | |
| 3350 | // do each vector op |
| 3351 | for (int c = 0; c < numCols; ++c) { |
| 3352 | std::vector<unsigned int> indexes; |
| 3353 | indexes.push_back(c); |
Rex Xu | c1992e5 | 2016-05-17 18:57:18 +0800 | [diff] [blame] | 3354 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 3355 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
| 3356 | addDecoration(destVec, noContraction); |
| 3357 | results.push_back(builder.setPrecision(destVec, precision)); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | // put the pieces together |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3361 | return builder.setPrecision(builder.createCompositeConstruct(typeId, results), precision); |
John Kessenich | 7a53f76 | 2016-01-20 11:19:27 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 3364 | 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] | 3365 | { |
| 3366 | spv::Op convOp = spv::OpNop; |
| 3367 | spv::Id zero = 0; |
| 3368 | spv::Id one = 0; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3369 | spv::Id type = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3370 | |
| 3371 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 3372 | |
| 3373 | switch (op) { |
| 3374 | case glslang::EOpConvIntToBool: |
| 3375 | case glslang::EOpConvUintToBool: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3376 | case glslang::EOpConvInt64ToBool: |
| 3377 | case glslang::EOpConvUint64ToBool: |
| 3378 | zero = (op == glslang::EOpConvInt64ToBool || |
| 3379 | op == glslang::EOpConvUint64ToBool) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3380 | zero = makeSmearedConstant(zero, vectorSize); |
| 3381 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 3382 | |
| 3383 | case glslang::EOpConvFloatToBool: |
| 3384 | zero = builder.makeFloatConstant(0.0F); |
| 3385 | zero = makeSmearedConstant(zero, vectorSize); |
| 3386 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 3387 | |
| 3388 | case glslang::EOpConvDoubleToBool: |
| 3389 | zero = builder.makeDoubleConstant(0.0); |
| 3390 | zero = makeSmearedConstant(zero, vectorSize); |
| 3391 | return builder.createBinOp(spv::OpFOrdNotEqual, destType, operand, zero); |
| 3392 | |
| 3393 | case glslang::EOpConvBoolToFloat: |
| 3394 | convOp = spv::OpSelect; |
| 3395 | zero = builder.makeFloatConstant(0.0); |
| 3396 | one = builder.makeFloatConstant(1.0); |
| 3397 | break; |
| 3398 | case glslang::EOpConvBoolToDouble: |
| 3399 | convOp = spv::OpSelect; |
| 3400 | zero = builder.makeDoubleConstant(0.0); |
| 3401 | one = builder.makeDoubleConstant(1.0); |
| 3402 | break; |
| 3403 | case glslang::EOpConvBoolToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3404 | case glslang::EOpConvBoolToInt64: |
| 3405 | zero = (op == glslang::EOpConvBoolToInt64) ? builder.makeInt64Constant(0) : builder.makeIntConstant(0); |
| 3406 | one = (op == glslang::EOpConvBoolToInt64) ? builder.makeInt64Constant(1) : builder.makeIntConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3407 | convOp = spv::OpSelect; |
| 3408 | break; |
| 3409 | case glslang::EOpConvBoolToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3410 | case glslang::EOpConvBoolToUint64: |
| 3411 | zero = (op == glslang::EOpConvBoolToUint64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0); |
| 3412 | one = (op == glslang::EOpConvBoolToUint64) ? builder.makeUint64Constant(1) : builder.makeUintConstant(1); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3413 | convOp = spv::OpSelect; |
| 3414 | break; |
| 3415 | |
| 3416 | case glslang::EOpConvIntToFloat: |
| 3417 | case glslang::EOpConvIntToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3418 | case glslang::EOpConvInt64ToFloat: |
| 3419 | case glslang::EOpConvInt64ToDouble: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3420 | convOp = spv::OpConvertSToF; |
| 3421 | break; |
| 3422 | |
| 3423 | case glslang::EOpConvUintToFloat: |
| 3424 | case glslang::EOpConvUintToDouble: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3425 | case glslang::EOpConvUint64ToFloat: |
| 3426 | case glslang::EOpConvUint64ToDouble: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3427 | convOp = spv::OpConvertUToF; |
| 3428 | break; |
| 3429 | |
| 3430 | case glslang::EOpConvDoubleToFloat: |
| 3431 | case glslang::EOpConvFloatToDouble: |
| 3432 | convOp = spv::OpFConvert; |
Rex Xu | 73e3ce7 | 2016-04-27 18:48:17 +0800 | [diff] [blame] | 3433 | if (builder.isMatrixType(destType)) |
| 3434 | return createUnaryMatrixOperation(convOp, precision, noContraction, destType, operand, typeProxy); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3435 | break; |
| 3436 | |
| 3437 | case glslang::EOpConvFloatToInt: |
| 3438 | case glslang::EOpConvDoubleToInt: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3439 | case glslang::EOpConvFloatToInt64: |
| 3440 | case glslang::EOpConvDoubleToInt64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3441 | convOp = spv::OpConvertFToS; |
| 3442 | break; |
| 3443 | |
| 3444 | case glslang::EOpConvUintToInt: |
| 3445 | case glslang::EOpConvIntToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3446 | case glslang::EOpConvUint64ToInt64: |
| 3447 | case glslang::EOpConvInt64ToUint64: |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 3448 | if (builder.isInSpecConstCodeGenMode()) { |
| 3449 | // Build zero scalar or vector for OpIAdd. |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3450 | zero = (op == glslang::EOpConvUintToInt64 || |
| 3451 | op == glslang::EOpConvIntToUint64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0); |
qining | 189b203 | 2016-04-12 23:16:20 -0400 | [diff] [blame] | 3452 | zero = makeSmearedConstant(zero, vectorSize); |
qining | e24aa5e | 2016-04-07 15:40:27 -0400 | [diff] [blame] | 3453 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 3454 | // generating for OpSpecConstantOp instruction. |
| 3455 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 3456 | } |
| 3457 | // For normal run-time conversion instruction, use OpBitcast. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3458 | convOp = spv::OpBitcast; |
| 3459 | break; |
| 3460 | |
| 3461 | case glslang::EOpConvFloatToUint: |
| 3462 | case glslang::EOpConvDoubleToUint: |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3463 | case glslang::EOpConvFloatToUint64: |
| 3464 | case glslang::EOpConvDoubleToUint64: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3465 | convOp = spv::OpConvertFToU; |
| 3466 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3467 | |
| 3468 | case glslang::EOpConvIntToInt64: |
| 3469 | case glslang::EOpConvInt64ToInt: |
| 3470 | convOp = spv::OpSConvert; |
| 3471 | break; |
| 3472 | |
| 3473 | case glslang::EOpConvUintToUint64: |
| 3474 | case glslang::EOpConvUint64ToUint: |
| 3475 | convOp = spv::OpUConvert; |
| 3476 | break; |
| 3477 | |
| 3478 | case glslang::EOpConvIntToUint64: |
| 3479 | case glslang::EOpConvInt64ToUint: |
| 3480 | case glslang::EOpConvUint64ToInt: |
| 3481 | case glslang::EOpConvUintToInt64: |
| 3482 | // OpSConvert/OpUConvert + OpBitCast |
| 3483 | switch (op) { |
| 3484 | case glslang::EOpConvIntToUint64: |
| 3485 | convOp = spv::OpSConvert; |
| 3486 | type = builder.makeIntType(64); |
| 3487 | break; |
| 3488 | case glslang::EOpConvInt64ToUint: |
| 3489 | convOp = spv::OpSConvert; |
| 3490 | type = builder.makeIntType(32); |
| 3491 | break; |
| 3492 | case glslang::EOpConvUint64ToInt: |
| 3493 | convOp = spv::OpUConvert; |
| 3494 | type = builder.makeUintType(32); |
| 3495 | break; |
| 3496 | case glslang::EOpConvUintToInt64: |
| 3497 | convOp = spv::OpUConvert; |
| 3498 | type = builder.makeUintType(64); |
| 3499 | break; |
| 3500 | default: |
| 3501 | assert(0); |
| 3502 | break; |
| 3503 | } |
| 3504 | |
| 3505 | if (vectorSize > 0) |
| 3506 | type = builder.makeVectorType(type, vectorSize); |
| 3507 | |
| 3508 | operand = builder.createUnaryOp(convOp, type, operand); |
| 3509 | |
| 3510 | if (builder.isInSpecConstCodeGenMode()) { |
| 3511 | // Build zero scalar or vector for OpIAdd. |
| 3512 | zero = (op == glslang::EOpConvIntToUint64 || |
| 3513 | op == glslang::EOpConvUintToInt64) ? builder.makeUint64Constant(0) : builder.makeUintConstant(0); |
| 3514 | zero = makeSmearedConstant(zero, vectorSize); |
| 3515 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 3516 | // generating for OpSpecConstantOp instruction. |
| 3517 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 3518 | } |
| 3519 | // For normal run-time conversion instruction, use OpBitcast. |
| 3520 | convOp = spv::OpBitcast; |
| 3521 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3522 | default: |
| 3523 | break; |
| 3524 | } |
| 3525 | |
| 3526 | spv::Id result = 0; |
| 3527 | if (convOp == spv::OpNop) |
| 3528 | return result; |
| 3529 | |
| 3530 | if (convOp == spv::OpSelect) { |
| 3531 | zero = makeSmearedConstant(zero, vectorSize); |
| 3532 | one = makeSmearedConstant(one, vectorSize); |
| 3533 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 3534 | } else |
| 3535 | result = builder.createUnaryOp(convOp, destType, operand); |
| 3536 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3537 | return builder.setPrecision(result, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 3541 | { |
| 3542 | if (vectorSize == 0) |
| 3543 | return constant; |
| 3544 | |
| 3545 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 3546 | std::vector<spv::Id> components; |
| 3547 | for (int c = 0; c < vectorSize; ++c) |
| 3548 | components.push_back(constant); |
| 3549 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 3550 | } |
| 3551 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3552 | // For glslang ops that map to SPV atomic opCodes |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3553 | 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] | 3554 | { |
| 3555 | spv::Op opCode = spv::OpNop; |
| 3556 | |
| 3557 | switch (op) { |
| 3558 | case glslang::EOpAtomicAdd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3559 | case glslang::EOpImageAtomicAdd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3560 | opCode = spv::OpAtomicIAdd; |
| 3561 | break; |
| 3562 | case glslang::EOpAtomicMin: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3563 | case glslang::EOpImageAtomicMin: |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3564 | opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMin : spv::OpAtomicSMin; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3565 | break; |
| 3566 | case glslang::EOpAtomicMax: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3567 | case glslang::EOpImageAtomicMax: |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3568 | opCode = typeProxy == glslang::EbtUint ? spv::OpAtomicUMax : spv::OpAtomicSMax; |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3569 | break; |
| 3570 | case glslang::EOpAtomicAnd: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3571 | case glslang::EOpImageAtomicAnd: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3572 | opCode = spv::OpAtomicAnd; |
| 3573 | break; |
| 3574 | case glslang::EOpAtomicOr: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3575 | case glslang::EOpImageAtomicOr: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3576 | opCode = spv::OpAtomicOr; |
| 3577 | break; |
| 3578 | case glslang::EOpAtomicXor: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3579 | case glslang::EOpImageAtomicXor: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3580 | opCode = spv::OpAtomicXor; |
| 3581 | break; |
| 3582 | case glslang::EOpAtomicExchange: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3583 | case glslang::EOpImageAtomicExchange: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3584 | opCode = spv::OpAtomicExchange; |
| 3585 | break; |
| 3586 | case glslang::EOpAtomicCompSwap: |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 3587 | case glslang::EOpImageAtomicCompSwap: |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3588 | opCode = spv::OpAtomicCompareExchange; |
| 3589 | break; |
| 3590 | case glslang::EOpAtomicCounterIncrement: |
| 3591 | opCode = spv::OpAtomicIIncrement; |
| 3592 | break; |
| 3593 | case glslang::EOpAtomicCounterDecrement: |
| 3594 | opCode = spv::OpAtomicIDecrement; |
| 3595 | break; |
| 3596 | case glslang::EOpAtomicCounter: |
| 3597 | opCode = spv::OpAtomicLoad; |
| 3598 | break; |
| 3599 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3600 | assert(0); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3601 | break; |
| 3602 | } |
| 3603 | |
| 3604 | // Sort out the operands |
| 3605 | // - mapping from glslang -> SPV |
| 3606 | // - there are extra SPV operands with no glslang source |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 3607 | // - compare-exchange swaps the value and comparator |
| 3608 | // - compare-exchange has an extra memory semantics |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3609 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 3610 | auto opIt = operands.begin(); // walk the glslang operands |
| 3611 | spvAtomicOperands.push_back(*(opIt++)); |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3612 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::ScopeDevice)); // TBD: what is the correct scope? |
| 3613 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); // TBD: what are the correct memory semantics? |
| 3614 | if (opCode == spv::OpAtomicCompareExchange) { |
Rex Xu | bba5c80 | 2015-09-16 13:20:37 +0800 | [diff] [blame] | 3615 | // There are 2 memory semantics for compare-exchange. And the operand order of "comparator" and "new value" in GLSL |
| 3616 | // differs from that in SPIR-V. Hence, special processing is required. |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3617 | spvAtomicOperands.push_back(builder.makeUintConstant(spv::MemorySemanticsMaskNone)); |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 3618 | spvAtomicOperands.push_back(*(opIt + 1)); |
| 3619 | spvAtomicOperands.push_back(*opIt); |
| 3620 | opIt += 2; |
Rex Xu | 04db3f5 | 2015-09-16 11:44:02 +0800 | [diff] [blame] | 3621 | } |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 3622 | |
John Kessenich | 3e60a6f | 2015-09-14 22:45:16 -0600 | [diff] [blame] | 3623 | // 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] | 3624 | for (; opIt != operands.end(); ++opIt) |
| 3625 | spvAtomicOperands.push_back(*opIt); |
| 3626 | |
| 3627 | return builder.createOp(opCode, typeId, spvAtomicOperands); |
| 3628 | } |
| 3629 | |
John Kessenich | 91cef52 | 2016-05-05 16:45:40 -0600 | [diff] [blame] | 3630 | // Create group invocation operations. |
| 3631 | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, spv::Id operand) |
| 3632 | { |
| 3633 | builder.addCapability(spv::CapabilityGroups); |
| 3634 | |
| 3635 | std::vector<spv::Id> operands; |
| 3636 | operands.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 3637 | operands.push_back(operand); |
| 3638 | |
| 3639 | switch (op) { |
| 3640 | case glslang::EOpAnyInvocation: |
| 3641 | case glslang::EOpAllInvocations: |
| 3642 | return builder.createOp(op == glslang::EOpAnyInvocation ? spv::OpGroupAny : spv::OpGroupAll, typeId, operands); |
| 3643 | |
| 3644 | case glslang::EOpAllInvocationsEqual: |
| 3645 | { |
| 3646 | spv::Id groupAll = builder.createOp(spv::OpGroupAll, typeId, operands); |
| 3647 | spv::Id groupAny = builder.createOp(spv::OpGroupAny, typeId, operands); |
| 3648 | |
| 3649 | return builder.createBinOp(spv::OpLogicalOr, typeId, groupAll, |
| 3650 | builder.createUnaryOp(spv::OpLogicalNot, typeId, groupAny)); |
| 3651 | } |
| 3652 | default: |
| 3653 | logger->missingFunctionality("invocation operation"); |
| 3654 | return spv::NoResult; |
| 3655 | } |
| 3656 | } |
| 3657 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3658 | 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] | 3659 | { |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 3660 | bool isUnsigned = typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3661 | bool isFloat = typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble; |
| 3662 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3663 | spv::Op opCode = spv::OpNop; |
| 3664 | int libCall = -1; |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 3665 | size_t consumedOperands = operands.size(); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3666 | spv::Id typeId0 = 0; |
| 3667 | if (consumedOperands > 0) |
| 3668 | typeId0 = builder.getTypeId(operands[0]); |
| 3669 | spv::Id frexpIntType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3670 | |
| 3671 | switch (op) { |
| 3672 | case glslang::EOpMin: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3673 | if (isFloat) |
| 3674 | libCall = spv::GLSLstd450FMin; |
| 3675 | else if (isUnsigned) |
| 3676 | libCall = spv::GLSLstd450UMin; |
| 3677 | else |
| 3678 | libCall = spv::GLSLstd450SMin; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 3679 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3680 | break; |
| 3681 | case glslang::EOpModf: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3682 | libCall = spv::GLSLstd450Modf; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3683 | break; |
| 3684 | case glslang::EOpMax: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3685 | if (isFloat) |
| 3686 | libCall = spv::GLSLstd450FMax; |
| 3687 | else if (isUnsigned) |
| 3688 | libCall = spv::GLSLstd450UMax; |
| 3689 | else |
| 3690 | libCall = spv::GLSLstd450SMax; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 3691 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3692 | break; |
| 3693 | case glslang::EOpPow: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3694 | libCall = spv::GLSLstd450Pow; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3695 | break; |
| 3696 | case glslang::EOpDot: |
| 3697 | opCode = spv::OpDot; |
| 3698 | break; |
| 3699 | case glslang::EOpAtan: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3700 | libCall = spv::GLSLstd450Atan2; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3701 | break; |
| 3702 | |
| 3703 | case glslang::EOpClamp: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3704 | if (isFloat) |
| 3705 | libCall = spv::GLSLstd450FClamp; |
| 3706 | else if (isUnsigned) |
| 3707 | libCall = spv::GLSLstd450UClamp; |
| 3708 | else |
| 3709 | libCall = spv::GLSLstd450SClamp; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 3710 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 3711 | builder.promoteScalar(precision, operands.front(), operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3712 | break; |
| 3713 | case glslang::EOpMix: |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 3714 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 3715 | assert(isFloat); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3716 | libCall = spv::GLSLstd450FMix; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 3717 | } else { |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3718 | opCode = spv::OpSelect; |
Rex Xu | d715adc | 2016-03-15 12:08:31 +0800 | [diff] [blame] | 3719 | std::swap(operands.front(), operands.back()); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3720 | } |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 3721 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3722 | break; |
| 3723 | case glslang::EOpStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3724 | libCall = spv::GLSLstd450Step; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 3725 | builder.promoteScalar(precision, operands.front(), operands.back()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3726 | break; |
| 3727 | case glslang::EOpSmoothStep: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3728 | libCall = spv::GLSLstd450SmoothStep; |
John Kessenich | e7c83cf | 2015-12-13 13:34:37 -0700 | [diff] [blame] | 3729 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 3730 | builder.promoteScalar(precision, operands[1], operands[2]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3731 | break; |
| 3732 | |
| 3733 | case glslang::EOpDistance: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3734 | libCall = spv::GLSLstd450Distance; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3735 | break; |
| 3736 | case glslang::EOpCross: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3737 | libCall = spv::GLSLstd450Cross; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3738 | break; |
| 3739 | case glslang::EOpFaceForward: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3740 | libCall = spv::GLSLstd450FaceForward; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3741 | break; |
| 3742 | case glslang::EOpReflect: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3743 | libCall = spv::GLSLstd450Reflect; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3744 | break; |
| 3745 | case glslang::EOpRefract: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3746 | libCall = spv::GLSLstd450Refract; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3747 | break; |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 3748 | case glslang::EOpInterpolateAtSample: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3749 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 3750 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 3751 | break; |
| 3752 | case glslang::EOpInterpolateAtOffset: |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3753 | builder.addCapability(spv::CapabilityInterpolationFunction); |
Rex Xu | 7a26c17 | 2015-12-08 17:12:09 +0800 | [diff] [blame] | 3754 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 3755 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3756 | case glslang::EOpAddCarry: |
| 3757 | opCode = spv::OpIAddCarry; |
| 3758 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 3759 | consumedOperands = 2; |
| 3760 | break; |
| 3761 | case glslang::EOpSubBorrow: |
| 3762 | opCode = spv::OpISubBorrow; |
| 3763 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 3764 | consumedOperands = 2; |
| 3765 | break; |
| 3766 | case glslang::EOpUMulExtended: |
| 3767 | opCode = spv::OpUMulExtended; |
| 3768 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 3769 | consumedOperands = 2; |
| 3770 | break; |
| 3771 | case glslang::EOpIMulExtended: |
| 3772 | opCode = spv::OpSMulExtended; |
| 3773 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 3774 | consumedOperands = 2; |
| 3775 | break; |
| 3776 | case glslang::EOpBitfieldExtract: |
| 3777 | if (isUnsigned) |
| 3778 | opCode = spv::OpBitFieldUExtract; |
| 3779 | else |
| 3780 | opCode = spv::OpBitFieldSExtract; |
| 3781 | break; |
| 3782 | case glslang::EOpBitfieldInsert: |
| 3783 | opCode = spv::OpBitFieldInsert; |
| 3784 | break; |
| 3785 | |
| 3786 | case glslang::EOpFma: |
| 3787 | libCall = spv::GLSLstd450Fma; |
| 3788 | break; |
| 3789 | case glslang::EOpFrexp: |
| 3790 | libCall = spv::GLSLstd450FrexpStruct; |
| 3791 | if (builder.getNumComponents(operands[0]) == 1) |
| 3792 | frexpIntType = builder.makeIntegerType(32, true); |
| 3793 | else |
| 3794 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(32, true), builder.getNumComponents(operands[0])); |
| 3795 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 3796 | consumedOperands = 1; |
| 3797 | break; |
| 3798 | case glslang::EOpLdexp: |
| 3799 | libCall = spv::GLSLstd450Ldexp; |
| 3800 | break; |
| 3801 | |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 3802 | case glslang::EOpReadInvocation: |
John Kessenich | c8a5676 | 2016-05-05 12:04:22 -0600 | [diff] [blame] | 3803 | logger->missingFunctionality("shader ballot"); |
Rex Xu | 574ab04 | 2016-04-14 16:53:07 +0800 | [diff] [blame] | 3804 | libCall = spv::GLSLstd450Bad; |
| 3805 | break; |
| 3806 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3807 | default: |
| 3808 | return 0; |
| 3809 | } |
| 3810 | |
| 3811 | spv::Id id = 0; |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 3812 | if (libCall >= 0) { |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 3813 | // Use an extended instruction from the standard library. |
| 3814 | // Construct the call arguments, without modifying the original operands vector. |
| 3815 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 3816 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3817 | id = builder.createBuiltinCall(typeId, stdBuiltins, libCall, callArguments); |
John Kessenich | 2359bd0 | 2015-12-06 19:29:11 -0700 | [diff] [blame] | 3818 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3819 | switch (consumedOperands) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3820 | case 0: |
| 3821 | // should all be handled by visitAggregate and createNoArgOperation |
| 3822 | assert(0); |
| 3823 | return 0; |
| 3824 | case 1: |
| 3825 | // should all be handled by createUnaryOperation |
| 3826 | assert(0); |
| 3827 | return 0; |
| 3828 | case 2: |
| 3829 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 3830 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3831 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3832 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 3833 | assert(consumedOperands == operands.size()); |
| 3834 | id = builder.createOp(opCode, typeId, operands); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3835 | break; |
| 3836 | } |
| 3837 | } |
| 3838 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3839 | // Decode the return types that were structures |
| 3840 | switch (op) { |
| 3841 | case glslang::EOpAddCarry: |
| 3842 | case glslang::EOpSubBorrow: |
| 3843 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 3844 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 3845 | break; |
| 3846 | case glslang::EOpUMulExtended: |
| 3847 | case glslang::EOpIMulExtended: |
| 3848 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 3849 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 3850 | break; |
| 3851 | case glslang::EOpFrexp: |
David Neto | 8d63a3d | 2015-12-07 16:17:06 -0500 | [diff] [blame] | 3852 | assert(operands.size() == 2); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3853 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 3854 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 3855 | break; |
| 3856 | default: |
| 3857 | break; |
| 3858 | } |
| 3859 | |
John Kessenich | 32cfd49 | 2016-02-02 12:37:46 -0700 | [diff] [blame] | 3860 | return builder.setPrecision(id, precision); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | // Intrinsics with no arguments, no return value, and no precision. |
| 3864 | spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op) |
| 3865 | { |
| 3866 | // TODO: get the barrier operands correct |
| 3867 | |
| 3868 | switch (op) { |
| 3869 | case glslang::EOpEmitVertex: |
| 3870 | builder.createNoResultOp(spv::OpEmitVertex); |
| 3871 | return 0; |
| 3872 | case glslang::EOpEndPrimitive: |
| 3873 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 3874 | return 0; |
| 3875 | case glslang::EOpBarrier: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3876 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAllMemory); |
| 3877 | builder.createControlBarrier(spv::ScopeDevice, spv::ScopeDevice, spv::MemorySemanticsMaskNone); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3878 | return 0; |
| 3879 | case glslang::EOpMemoryBarrier: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3880 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAllMemory); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3881 | return 0; |
| 3882 | case glslang::EOpMemoryBarrierAtomicCounter: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3883 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsAtomicCounterMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3884 | return 0; |
| 3885 | case glslang::EOpMemoryBarrierBuffer: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3886 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3887 | return 0; |
| 3888 | case glslang::EOpMemoryBarrierImage: |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3889 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsImageMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3890 | return 0; |
| 3891 | case glslang::EOpMemoryBarrierShared: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3892 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsWorkgroupMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3893 | return 0; |
| 3894 | case glslang::EOpGroupMemoryBarrier: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3895 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsCrossWorkgroupMemoryMask); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3896 | return 0; |
| 3897 | default: |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 3898 | logger->missingFunctionality("unknown operation with no arguments"); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3899 | return 0; |
| 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 3904 | { |
John Kessenich | 2f27336 | 2015-07-18 22:34:27 -0600 | [diff] [blame] | 3905 | auto iter = symbolValues.find(symbol->getId()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3906 | spv::Id id; |
| 3907 | if (symbolValues.end() != iter) { |
| 3908 | id = iter->second; |
| 3909 | return id; |
| 3910 | } |
| 3911 | |
| 3912 | // it was not found, create it |
| 3913 | id = createSpvVariable(symbol); |
| 3914 | symbolValues[symbol->getId()] = id; |
| 3915 | |
| 3916 | if (! symbol->getType().isStruct()) { |
| 3917 | addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3918 | addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3919 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
| 3920 | addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3921 | if (symbol->getQualifier().hasLocation()) |
| 3922 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
| 3923 | if (symbol->getQualifier().hasIndex()) |
| 3924 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 3925 | if (symbol->getQualifier().hasComponent()) |
| 3926 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 3927 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3928 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3929 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3930 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3931 | if (symbol->getQualifier().hasXfbBuffer()) |
| 3932 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 3933 | if (symbol->getQualifier().hasXfbOffset()) |
| 3934 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
| 3935 | } |
| 3936 | } |
| 3937 | |
John Kessenich | e0b6cad | 2015-12-24 10:30:13 -0700 | [diff] [blame] | 3938 | addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
John Kessenich | f2d8a5c | 2016-03-03 22:29:11 -0700 | [diff] [blame] | 3939 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3940 | builder.addCapability(spv::CapabilityGeometryStreams); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3941 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3942 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3943 | if (symbol->getQualifier().hasSet()) |
| 3944 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3945 | else if (IsDescriptorResource(symbol->getType())) { |
| 3946 | // default to 0 |
| 3947 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 3948 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3949 | if (symbol->getQualifier().hasBinding()) |
| 3950 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 3951 | if (symbol->getQualifier().hasAttachment()) |
| 3952 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3953 | if (glslangIntermediate->getXfbMode()) { |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3954 | builder.addCapability(spv::CapabilityTransformFeedback); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3955 | if (symbol->getQualifier().hasXfbStride()) |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3956 | builder.addDecoration(id, spv::DecorationXfbStride, symbol->getQualifier().layoutXfbStride); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3957 | if (symbol->getQualifier().hasXfbBuffer()) |
| 3958 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 3959 | } |
| 3960 | |
Rex Xu | 1da878f | 2016-02-21 20:59:01 +0800 | [diff] [blame] | 3961 | if (symbol->getType().isImage()) { |
| 3962 | std::vector<spv::Decoration> memory; |
| 3963 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory); |
| 3964 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 3965 | addDecoration(id, memory[i]); |
| 3966 | } |
| 3967 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3968 | // built-in variable decorations |
John Kessenich | ebb5053 | 2016-05-16 19:22:05 -0600 | [diff] [blame] | 3969 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 3970 | if (builtIn != spv::BadValue) |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3971 | addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3972 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3973 | return id; |
| 3974 | } |
| 3975 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3976 | // If 'dec' is valid, add no-operand decoration to an object |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 3977 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec) |
| 3978 | { |
| 3979 | if (dec != spv::BadValue) |
| 3980 | builder.addDecoration(id, dec); |
| 3981 | } |
| 3982 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 3983 | // If 'dec' is valid, add a one-operand decoration to an object |
| 3984 | void TGlslangToSpvTraverser::addDecoration(spv::Id id, spv::Decoration dec, unsigned value) |
| 3985 | { |
| 3986 | if (dec != spv::BadValue) |
| 3987 | builder.addDecoration(id, dec, value); |
| 3988 | } |
| 3989 | |
| 3990 | // 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] | 3991 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec) |
| 3992 | { |
| 3993 | if (dec != spv::BadValue) |
| 3994 | builder.addMemberDecoration(id, (unsigned)member, dec); |
| 3995 | } |
| 3996 | |
John Kessenich | 9218759 | 2016-02-01 13:45:25 -0700 | [diff] [blame] | 3997 | // If 'dec' is valid, add a one-operand decoration to a struct member |
| 3998 | void TGlslangToSpvTraverser::addMemberDecoration(spv::Id id, int member, spv::Decoration dec, unsigned value) |
| 3999 | { |
| 4000 | if (dec != spv::BadValue) |
| 4001 | builder.addMemberDecoration(id, (unsigned)member, dec, value); |
| 4002 | } |
| 4003 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4004 | // 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] | 4005 | // or regular constant if possible. |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4006 | // |
| 4007 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 4008 | // |
| 4009 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 4010 | // regular constants, which themselves are trees that createSpvConstant() |
| 4011 | // recursively walks. So, this function walks the "top" of the tree: |
| 4012 | // - emit specialization constant-building instructions for specConstant |
| 4013 | // - when running into a non-spec-constant, switch to createSpvConstant() |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 4014 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4015 | { |
John Kessenich | 7cc0e28 | 2016-03-20 00:46:02 -0600 | [diff] [blame] | 4016 | assert(node.getQualifier().isConstant()); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4017 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 4018 | // Handle front-end constants first (non-specialization constants). |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4019 | if (! node.getQualifier().specConstant) { |
| 4020 | // hand off to the non-spec-constant path |
| 4021 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 4022 | int nextConst = 0; |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 4023 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4024 | nextConst, false); |
| 4025 | } |
| 4026 | |
| 4027 | // We now know we have a specialization constant to build |
| 4028 | |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 4029 | // gl_WorkgroupSize is a special case until the front-end handles hierarchical specialization constants, |
| 4030 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 4031 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 4032 | std::vector<spv::Id> dimConstId; |
| 4033 | for (int dim = 0; dim < 3; ++dim) { |
| 4034 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 4035 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 4036 | if (specConst) |
| 4037 | addDecoration(dimConstId.back(), spv::DecorationSpecId, glslangIntermediate->getLocalSizeSpecId(dim)); |
| 4038 | } |
| 4039 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 4040 | } |
| 4041 | |
| 4042 | // An AST node labelled as specialization constant should be a symbol node. |
| 4043 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 4044 | if (auto* sn = node.getAsSymbolNode()) { |
| 4045 | if (auto* sub_tree = sn->getConstSubtree()) { |
qining | 27e04a0 | 2016-04-14 16:40:20 -0400 | [diff] [blame] | 4046 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 4047 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 4048 | // will set the builder into spec constant op instruction generating mode. |
| 4049 | sub_tree->traverse(this); |
| 4050 | return accessChainLoad(sub_tree->getType()); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 4051 | } else if (auto* const_union_array = &sn->getConstArray()){ |
| 4052 | int nextConst = 0; |
| 4053 | return createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
John Kessenich | 6c292d3 | 2016-02-15 20:58:50 -0700 | [diff] [blame] | 4054 | } |
| 4055 | } |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 4056 | |
| 4057 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 4058 | // constant sub tree as initializer. |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 4059 | logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
qining | 4f4bb81 | 2016-04-03 23:55:17 -0400 | [diff] [blame] | 4060 | exit(1); |
| 4061 | return spv::NoResult; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4062 | } |
| 4063 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4064 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 4065 | // build the aggregate SPIR-V constant. |
| 4066 | // |
| 4067 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 4068 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 4069 | // |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 4070 | 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] | 4071 | { |
| 4072 | // vector of constants for SPIR-V |
| 4073 | std::vector<spv::Id> spvConsts; |
| 4074 | |
| 4075 | // Type is used for struct and array constants |
| 4076 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 4077 | |
| 4078 | if (glslangType.isArray()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 4079 | glslang::TType elementType(glslangType, 0); |
| 4080 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 4081 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4082 | } else if (glslangType.isMatrix()) { |
John Kessenich | 65c78a0 | 2015-08-10 17:08:55 -0600 | [diff] [blame] | 4083 | glslang::TType vectorType(glslangType, 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4084 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 4085 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4086 | } else if (glslangType.getStruct()) { |
| 4087 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 4088 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
qining | 0840838 | 2016-03-21 09:51:37 -0400 | [diff] [blame] | 4089 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4090 | } else if (glslangType.isVector()) { |
| 4091 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 4092 | bool zero = nextConst >= consts.size(); |
| 4093 | switch (glslangType.getBasicType()) { |
| 4094 | case glslang::EbtInt: |
| 4095 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 4096 | break; |
| 4097 | case glslang::EbtUint: |
| 4098 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 4099 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4100 | case glslang::EbtInt64: |
| 4101 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 4102 | break; |
| 4103 | case glslang::EbtUint64: |
| 4104 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 4105 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4106 | case glslang::EbtFloat: |
| 4107 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 4108 | break; |
| 4109 | case glslang::EbtDouble: |
| 4110 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 4111 | break; |
| 4112 | case glslang::EbtBool: |
| 4113 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 4114 | break; |
| 4115 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4116 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4117 | break; |
| 4118 | } |
| 4119 | ++nextConst; |
| 4120 | } |
| 4121 | } else { |
| 4122 | // we have a non-aggregate (scalar) constant |
| 4123 | bool zero = nextConst >= consts.size(); |
| 4124 | spv::Id scalar = 0; |
| 4125 | switch (glslangType.getBasicType()) { |
| 4126 | case glslang::EbtInt: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4127 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4128 | break; |
| 4129 | case glslang::EbtUint: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4130 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4131 | break; |
Rex Xu | 8ff43de | 2016-04-22 16:51:45 +0800 | [diff] [blame] | 4132 | case glslang::EbtInt64: |
| 4133 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 4134 | break; |
| 4135 | case glslang::EbtUint64: |
| 4136 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 4137 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4138 | case glslang::EbtFloat: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4139 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4140 | break; |
| 4141 | case glslang::EbtDouble: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4142 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4143 | break; |
| 4144 | case glslang::EbtBool: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4145 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4146 | break; |
| 4147 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4148 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4149 | break; |
| 4150 | } |
| 4151 | ++nextConst; |
| 4152 | return scalar; |
| 4153 | } |
| 4154 | |
| 4155 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 4156 | } |
| 4157 | |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4158 | // Return true if the node is a constant or symbol whose reading has no |
| 4159 | // non-trivial observable cost or effect. |
| 4160 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 4161 | { |
| 4162 | // don't know what this is |
| 4163 | if (node == nullptr) |
| 4164 | return false; |
| 4165 | |
| 4166 | // a constant is safe |
| 4167 | if (node->getAsConstantUnion() != nullptr) |
| 4168 | return true; |
| 4169 | |
| 4170 | // not a symbol means non-trivial |
| 4171 | if (node->getAsSymbolNode() == nullptr) |
| 4172 | return false; |
| 4173 | |
| 4174 | // a symbol, depends on what's being read |
| 4175 | switch (node->getType().getQualifier().storage) { |
| 4176 | case glslang::EvqTemporary: |
| 4177 | case glslang::EvqGlobal: |
| 4178 | case glslang::EvqIn: |
| 4179 | case glslang::EvqInOut: |
| 4180 | case glslang::EvqConst: |
| 4181 | case glslang::EvqConstReadOnly: |
| 4182 | case glslang::EvqUniform: |
| 4183 | return true; |
| 4184 | default: |
| 4185 | return false; |
| 4186 | } |
qining | 25262b3 | 2016-05-06 17:25:16 -0400 | [diff] [blame] | 4187 | } |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4188 | |
| 4189 | // A node is trivial if it is a single operation with no side effects. |
| 4190 | // Error on the side of saying non-trivial. |
| 4191 | // Return true if trivial. |
| 4192 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 4193 | { |
| 4194 | if (node == nullptr) |
| 4195 | return false; |
| 4196 | |
| 4197 | // symbols and constants are trivial |
| 4198 | if (isTrivialLeaf(node)) |
| 4199 | return true; |
| 4200 | |
| 4201 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 4202 | |
| 4203 | // not a simple operation |
| 4204 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 4205 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 4206 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 4207 | return false; |
| 4208 | |
| 4209 | // not on leaf nodes |
| 4210 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 4211 | return false; |
| 4212 | |
| 4213 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 4214 | return false; |
| 4215 | } |
| 4216 | |
| 4217 | switch (node->getAsOperator()->getOp()) { |
| 4218 | case glslang::EOpLogicalNot: |
| 4219 | case glslang::EOpConvIntToBool: |
| 4220 | case glslang::EOpConvUintToBool: |
| 4221 | case glslang::EOpConvFloatToBool: |
| 4222 | case glslang::EOpConvDoubleToBool: |
| 4223 | case glslang::EOpEqual: |
| 4224 | case glslang::EOpNotEqual: |
| 4225 | case glslang::EOpLessThan: |
| 4226 | case glslang::EOpGreaterThan: |
| 4227 | case glslang::EOpLessThanEqual: |
| 4228 | case glslang::EOpGreaterThanEqual: |
| 4229 | case glslang::EOpIndexDirect: |
| 4230 | case glslang::EOpIndexDirectStruct: |
| 4231 | case glslang::EOpLogicalXor: |
| 4232 | case glslang::EOpAny: |
| 4233 | case glslang::EOpAll: |
| 4234 | return true; |
| 4235 | default: |
| 4236 | return false; |
| 4237 | } |
| 4238 | } |
| 4239 | |
| 4240 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 4241 | // the left side is true (for &&) or false (for ||). |
| 4242 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, glslang::TIntermTyped& right) |
| 4243 | { |
| 4244 | spv::Id boolTypeId = builder.makeBoolType(); |
| 4245 | |
| 4246 | // emit left operand |
| 4247 | builder.clearAccessChain(); |
| 4248 | left.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 4249 | spv::Id leftId = accessChainLoad(left.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4250 | |
| 4251 | // Operands to accumulate OpPhi operands |
| 4252 | std::vector<spv::Id> phiOperands; |
| 4253 | // accumulate left operand's phi information |
| 4254 | phiOperands.push_back(leftId); |
| 4255 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 4256 | |
| 4257 | // Make the two kinds of operation symmetric with a "!" |
| 4258 | // || => emit "if (! left) result = right" |
| 4259 | // && => emit "if ( left) result = right" |
| 4260 | // |
| 4261 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 4262 | // to 'builder' to have an "else" without an "then" |
| 4263 | if (op == glslang::EOpLogicalOr) |
| 4264 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 4265 | |
| 4266 | // make an "if" based on the left value |
| 4267 | spv::Builder::If ifBuilder(leftId, builder); |
| 4268 | |
| 4269 | // emit right operand as the "then" part of the "if" |
| 4270 | builder.clearAccessChain(); |
| 4271 | right.traverse(this); |
Rex Xu | b4fd8d1 | 2016-03-03 14:38:51 +0800 | [diff] [blame] | 4272 | spv::Id rightId = accessChainLoad(right.getType()); |
John Kessenich | 7c1aa10 | 2015-10-15 13:29:11 -0600 | [diff] [blame] | 4273 | |
| 4274 | // accumulate left operand's phi information |
| 4275 | phiOperands.push_back(rightId); |
| 4276 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 4277 | |
| 4278 | // finish the "if" |
| 4279 | ifBuilder.makeEndIf(); |
| 4280 | |
| 4281 | // phi together the two results |
| 4282 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 4283 | } |
| 4284 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4285 | }; // end anonymous namespace |
| 4286 | |
| 4287 | namespace glslang { |
| 4288 | |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 4289 | void GetSpirvVersion(std::string& version) |
| 4290 | { |
John Kessenich | 9e55f63 | 2015-07-15 10:03:39 -0600 | [diff] [blame] | 4291 | const int bufSize = 100; |
John Kessenich | f98ee23 | 2015-07-12 19:39:51 -0600 | [diff] [blame] | 4292 | char buf[bufSize]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 4293 | snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 4294 | version = buf; |
| 4295 | } |
| 4296 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4297 | // Write SPIR-V out to a binary file |
| 4298 | void OutputSpv(const std::vector<unsigned int>& spirv, const char* baseName) |
| 4299 | { |
| 4300 | std::ofstream out; |
John Kessenich | 68d78fd | 2015-07-12 19:28:10 -0600 | [diff] [blame] | 4301 | out.open(baseName, std::ios::binary | std::ios::out); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4302 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 4303 | unsigned int word = spirv[i]; |
| 4304 | out.write((const char*)&word, 4); |
| 4305 | } |
| 4306 | out.close(); |
| 4307 | } |
| 4308 | |
| 4309 | // |
| 4310 | // Set up the glslang traversal |
| 4311 | // |
| 4312 | void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv) |
| 4313 | { |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 4314 | spv::SpvBuildLogger logger; |
| 4315 | GlslangToSpv(intermediate, spirv, &logger); |
Lei Zhang | 09caf12 | 2016-05-02 18:11:54 -0400 | [diff] [blame] | 4316 | } |
| 4317 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 4318 | 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] | 4319 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4320 | TIntermNode* root = intermediate.getTreeRoot(); |
| 4321 | |
| 4322 | if (root == 0) |
| 4323 | return; |
| 4324 | |
| 4325 | glslang::GetThreadPoolAllocator().push(); |
| 4326 | |
Lei Zhang | 17535f7 | 2016-05-04 15:55:59 -0400 | [diff] [blame] | 4327 | TGlslangToSpvTraverser it(&intermediate, logger); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 4328 | |
| 4329 | root->traverse(&it); |
| 4330 | |
| 4331 | it.dumpSpv(spirv); |
| 4332 | |
| 4333 | glslang::GetThreadPoolAllocator().pop(); |
| 4334 | } |
| 4335 | |
| 4336 | }; // end namespace glslang |