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