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