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