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