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