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