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