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