| 1 | // |
| 2 | // Copyright (C) 2014-2016 LunarG, Inc. |
| 3 | // Copyright (C) 2015-2020 Google, Inc. |
| 4 | // Copyright (C) 2017 ARM Limited. |
| 5 | // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved. |
| 6 | // |
| 7 | // All rights reserved. |
| 8 | // |
| 9 | // Redistribution and use in source and binary forms, with or without |
| 10 | // modification, are permitted provided that the following conditions |
| 11 | // are met: |
| 12 | // |
| 13 | // Redistributions of source code must retain the above copyright |
| 14 | // notice, this list of conditions and the following disclaimer. |
| 15 | // |
| 16 | // Redistributions in binary form must reproduce the above |
| 17 | // copyright notice, this list of conditions and the following |
| 18 | // disclaimer in the documentation and/or other materials provided |
| 19 | // with the distribution. |
| 20 | // |
| 21 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 22 | // contributors may be used to endorse or promote products derived |
| 23 | // from this software without specific prior written permission. |
| 24 | // |
| 25 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 26 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 27 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 28 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 29 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 31 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 33 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 35 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | // POSSIBILITY OF SUCH DAMAGE. |
| 37 | |
| 38 | // |
| 39 | // Visit the nodes in the glslang intermediate tree representation to |
| 40 | // translate them to SPIR-V. |
| 41 | // |
| 42 | |
| 43 | #include "spirv.hpp" |
| 44 | #include "GlslangToSpv.h" |
| 45 | #include "SpvBuilder.h" |
| 46 | namespace spv { |
| 47 | #include "GLSL.std.450.h" |
| 48 | #include "GLSL.ext.KHR.h" |
| 49 | #include "GLSL.ext.EXT.h" |
| 50 | #include "GLSL.ext.AMD.h" |
| 51 | #include "GLSL.ext.NV.h" |
| 52 | #include "GLSL.ext.ARM.h" |
| 53 | #include "NonSemanticDebugPrintf.h" |
| 54 | } |
| 55 | |
| 56 | // Glslang includes |
| 57 | #include "../glslang/MachineIndependent/localintermediate.h" |
| 58 | #include "../glslang/MachineIndependent/SymbolTable.h" |
| 59 | #include "../glslang/Include/Common.h" |
| 60 | |
| 61 | // Build-time generated includes |
| 62 | #include "glslang/build_info.h" |
| 63 | |
| 64 | #include <fstream> |
| 65 | #include <iomanip> |
| 66 | #include <list> |
| 67 | #include <map> |
| 68 | #include <stack> |
| 69 | #include <string> |
| 70 | #include <vector> |
| 71 | |
| 72 | namespace { |
| 73 | |
| 74 | namespace { |
| 75 | class SpecConstantOpModeGuard { |
| 76 | public: |
| 77 | SpecConstantOpModeGuard(spv::Builder* builder) |
| 78 | : builder_(builder) { |
| 79 | previous_flag_ = builder->isInSpecConstCodeGenMode(); |
| 80 | } |
| 81 | ~SpecConstantOpModeGuard() { |
| 82 | previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 83 | : builder_->setToNormalCodeGenMode(); |
| 84 | } |
| 85 | void turnOnSpecConstantOpMode() { |
| 86 | builder_->setToSpecConstCodeGenMode(); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | spv::Builder* builder_; |
| 91 | bool previous_flag_; |
| 92 | }; |
| 93 | |
| 94 | struct OpDecorations { |
| 95 | public: |
| 96 | OpDecorations(spv::Decoration precision, spv::Decoration noContraction, spv::Decoration nonUniform) : |
| 97 | precision(precision) |
| 98 | #ifndef GLSLANG_WEB |
| 99 | , |
| 100 | noContraction(noContraction), |
| 101 | nonUniform(nonUniform) |
| 102 | #endif |
| 103 | { } |
| 104 | |
| 105 | spv::Decoration precision; |
| 106 | |
| 107 | #ifdef GLSLANG_WEB |
| 108 | void addNoContraction(spv::Builder&, spv::Id) const { } |
| 109 | void addNonUniform(spv::Builder&, spv::Id) const { } |
| 110 | #else |
| 111 | void addNoContraction(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, noContraction); } |
| 112 | void addNonUniform(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, nonUniform); } |
| 113 | protected: |
| 114 | spv::Decoration noContraction; |
| 115 | spv::Decoration nonUniform; |
| 116 | #endif |
| 117 | |
| 118 | }; |
| 119 | |
| 120 | } // namespace |
| 121 | |
| 122 | // |
| 123 | // The main holder of information for translating glslang to SPIR-V. |
| 124 | // |
| 125 | // Derives from the AST walking base class. |
| 126 | // |
| 127 | class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 128 | public: |
| 129 | TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 130 | glslang::SpvOptions& options); |
| 131 | virtual ~TGlslangToSpvTraverser() { } |
| 132 | |
| 133 | bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 134 | bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 135 | void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 136 | bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 137 | bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 138 | void visitSymbol(glslang::TIntermSymbol* symbol); |
| 139 | bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 140 | bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 141 | bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 142 | |
| 143 | void finishSpv(); |
| 144 | void dumpSpv(std::vector<unsigned int>& out); |
| 145 | |
| 146 | protected: |
| 147 | TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
| 148 | TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
| 149 | |
| 150 | spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
| 151 | spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
| 152 | spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
| 153 | spv::Decoration TranslateNonUniformDecoration(const spv::Builder::AccessChain::CoherentFlags& coherentFlags); |
| 154 | spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 155 | spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 156 | spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 157 | spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 158 | spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
| 159 | spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
| 160 | spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 161 | spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
| 162 | spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
| 163 | spv::StorageClass TranslateStorageClass(const glslang::TType&); |
| 164 | void TranslateLiterals(const glslang::TVector<const glslang::TIntermConstantUnion*>&, std::vector<unsigned>&) const; |
| 165 | void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
| 166 | spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
| 167 | spv::Id getSampledType(const glslang::TSampler&); |
| 168 | spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 169 | spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 170 | void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
| 171 | spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
| 172 | spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
| 173 | bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
| 174 | bool filterMember(const glslang::TType& member); |
| 175 | spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 176 | glslang::TLayoutPacking, const glslang::TQualifier&); |
| 177 | void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 178 | const glslang::TQualifier&, spv::Id, const std::vector<spv::Id>& spvMembers); |
| 179 | spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
| 180 | spv::Id accessChainLoad(const glslang::TType& type); |
| 181 | void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
| 182 | void multiTypeStore(const glslang::TType&, spv::Id rValue); |
| 183 | spv::Id convertLoadedBoolInUniformToUint(const glslang::TType& type, spv::Id nominalTypeId, spv::Id loadedId); |
| 184 | glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
| 185 | int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 186 | int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 187 | void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 188 | int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 189 | void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
| 190 | |
| 191 | bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
| 192 | bool writableParam(glslang::TStorageQualifier) const; |
| 193 | bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
| 194 | void makeFunctions(const glslang::TIntermSequence&); |
| 195 | void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 196 | void collectRayTracingLinkerObjects(); |
| 197 | void visitFunctions(const glslang::TIntermSequence&); |
| 198 | void handleFunctionEntry(const glslang::TIntermAggregate* node); |
| 199 | void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 200 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
| 201 | void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 202 | spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
| 203 | spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 204 | |
| 205 | spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 206 | glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 207 | spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 208 | spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 209 | glslang::TBasicType typeProxy, |
| 210 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
| 211 | spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 212 | glslang::TBasicType typeProxy); |
| 213 | spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 214 | glslang::TBasicType typeProxy); |
| 215 | spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
| 216 | spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
| 217 | spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
| 218 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 219 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
| 220 | spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
| 221 | glslang::TBasicType typeProxy); |
| 222 | spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 223 | spv::Id typeId, std::vector<spv::Id>& operands); |
| 224 | spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
| 225 | glslang::TBasicType typeProxy); |
| 226 | spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
| 227 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
| 228 | spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
| 229 | spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
| 230 | void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
| 231 | spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 232 | spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, |
| 233 | int& nextConst, bool specConstant); |
| 234 | bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 235 | bool isTrivial(const glslang::TIntermTyped* node); |
| 236 | spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
| 237 | spv::Id getExtBuiltins(const char* name); |
| 238 | std::pair<spv::Id, spv::Id> getForcedType(glslang::TBuiltInVariable builtIn, const glslang::TType&); |
| 239 | spv::Id translateForcedType(spv::Id object); |
| 240 | spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents); |
| 241 | |
| 242 | glslang::SpvOptions& options; |
| 243 | spv::Function* shaderEntry; |
| 244 | spv::Function* currentFunction; |
| 245 | spv::Instruction* entryPoint; |
| 246 | int sequenceDepth; |
| 247 | |
| 248 | spv::SpvBuildLogger* logger; |
| 249 | |
| 250 | // There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 251 | spv::Builder builder; |
| 252 | bool inEntryPoint; |
| 253 | bool entryPointTerminated; |
| 254 | bool linkageOnly; // true when visiting the set of objects in the AST present only for |
| 255 | // establishing interface, whether or not they were statically used |
| 256 | std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface |
| 257 | const glslang::TIntermediate* glslangIntermediate; |
| 258 | bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp |
| 259 | spv::Id stdBuiltins; |
| 260 | spv::Id nonSemanticDebugPrintf; |
| 261 | std::unordered_map<std::string, spv::Id> extBuiltinMap; |
| 262 | |
| 263 | std::unordered_map<long long, spv::Id> symbolValues; |
| 264 | std::unordered_map<uint32_t, spv::Id> builtInVariableIds; |
| 265 | std::unordered_set<long long> rValueParameters; // set of formal function parameters passed as rValues, |
| 266 | // rather than a pointer |
| 267 | std::unordered_map<std::string, spv::Function*> functionMap; |
| 268 | std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount]; |
| 269 | // for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 270 | std::unordered_map<long long, std::vector<int>> memberRemapper; |
| 271 | // for mapping glslang symbol struct to symbol Id |
| 272 | std::unordered_map<const glslang::TTypeList*, long long> glslangTypeToIdMap; |
| 273 | std::stack<bool> breakForLoop; // false means break for switch |
| 274 | std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
| 275 | // Map pointee types for EbtReference to their forward pointers |
| 276 | std::map<const glslang::TType *, spv::Id> forwardPointers; |
| 277 | // Type forcing, for when SPIR-V wants a different type than the AST, |
| 278 | // requiring local translation to and from SPIR-V type on every access. |
| 279 | // Maps <builtin-variable-id -> AST-required-type-id> |
| 280 | std::unordered_map<spv::Id, spv::Id> forceType; |
| 281 | // Used by Task shader while generating opearnds for OpEmitMeshTasksEXT |
| 282 | spv::Id taskPayloadID; |
| 283 | // Used later for generating OpTraceKHR/OpExecuteCallableKHR/OpHitObjectRecordHit*/OpHitObjectGetShaderBindingTableData |
| 284 | std::unordered_map<unsigned int, glslang::TIntermSymbol *> locationToSymbol[4]; |
| 285 | }; |
| 286 | |
| 287 | // |
| 288 | // Helper functions for translating glslang representations to SPIR-V enumerants. |
| 289 | // |
| 290 | |
| 291 | // Translate glslang profile to SPIR-V source language. |
| 292 | spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
| 293 | { |
| 294 | #ifdef GLSLANG_WEB |
| 295 | return spv::SourceLanguageESSL; |
| 296 | #endif |
| 297 | |
| 298 | switch (source) { |
| 299 | case glslang::EShSourceGlsl: |
| 300 | switch (profile) { |
| 301 | case ENoProfile: |
| 302 | case ECoreProfile: |
| 303 | case ECompatibilityProfile: |
| 304 | return spv::SourceLanguageGLSL; |
| 305 | case EEsProfile: |
| 306 | return spv::SourceLanguageESSL; |
| 307 | default: |
| 308 | return spv::SourceLanguageUnknown; |
| 309 | } |
| 310 | case glslang::EShSourceHlsl: |
| 311 | return spv::SourceLanguageHLSL; |
| 312 | default: |
| 313 | return spv::SourceLanguageUnknown; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Translate glslang language (stage) to SPIR-V execution model. |
| 318 | spv::ExecutionModel TranslateExecutionModel(EShLanguage stage, bool isMeshShaderEXT = false) |
| 319 | { |
| 320 | switch (stage) { |
| 321 | case EShLangVertex: return spv::ExecutionModelVertex; |
| 322 | case EShLangFragment: return spv::ExecutionModelFragment; |
| 323 | case EShLangCompute: return spv::ExecutionModelGLCompute; |
| 324 | #ifndef GLSLANG_WEB |
| 325 | case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 326 | case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 327 | case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 328 | case EShLangRayGen: return spv::ExecutionModelRayGenerationKHR; |
| 329 | case EShLangIntersect: return spv::ExecutionModelIntersectionKHR; |
| 330 | case EShLangAnyHit: return spv::ExecutionModelAnyHitKHR; |
| 331 | case EShLangClosestHit: return spv::ExecutionModelClosestHitKHR; |
| 332 | case EShLangMiss: return spv::ExecutionModelMissKHR; |
| 333 | case EShLangCallable: return spv::ExecutionModelCallableKHR; |
| 334 | case EShLangTask: return (isMeshShaderEXT)? spv::ExecutionModelTaskEXT : spv::ExecutionModelTaskNV; |
| 335 | case EShLangMesh: return (isMeshShaderEXT)? spv::ExecutionModelMeshEXT: spv::ExecutionModelMeshNV; |
| 336 | #endif |
| 337 | default: |
| 338 | assert(0); |
| 339 | return spv::ExecutionModelFragment; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Translate glslang sampler type to SPIR-V dimensionality. |
| 344 | spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 345 | { |
| 346 | switch (sampler.dim) { |
| 347 | case glslang::Esd1D: return spv::Dim1D; |
| 348 | case glslang::Esd2D: return spv::Dim2D; |
| 349 | case glslang::Esd3D: return spv::Dim3D; |
| 350 | case glslang::EsdCube: return spv::DimCube; |
| 351 | case glslang::EsdRect: return spv::DimRect; |
| 352 | case glslang::EsdBuffer: return spv::DimBuffer; |
| 353 | case glslang::EsdSubpass: return spv::DimSubpassData; |
| 354 | case glslang::EsdAttachmentEXT: return spv::DimTileImageDataEXT; |
| 355 | default: |
| 356 | assert(0); |
| 357 | return spv::Dim2D; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // Translate glslang precision to SPIR-V precision decorations. |
| 362 | spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
| 363 | { |
| 364 | switch (glslangPrecision) { |
| 365 | case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
| 366 | case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
| 367 | default: |
| 368 | return spv::NoPrecision; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Translate glslang type to SPIR-V precision decorations. |
| 373 | spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 374 | { |
| 375 | return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 376 | } |
| 377 | |
| 378 | // Translate glslang type to SPIR-V block decorations. |
| 379 | spv::Decoration TranslateBlockDecoration(const glslang::TStorageQualifier storage, bool useStorageBuffer) |
| 380 | { |
| 381 | switch (storage) { |
| 382 | case glslang::EvqUniform: return spv::DecorationBlock; |
| 383 | case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
| 384 | case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 385 | case glslang::EvqVaryingOut: return spv::DecorationBlock; |
| 386 | case glslang::EvqShared: return spv::DecorationBlock; |
| 387 | #ifndef GLSLANG_WEB |
| 388 | case glslang::EvqPayload: return spv::DecorationBlock; |
| 389 | case glslang::EvqPayloadIn: return spv::DecorationBlock; |
| 390 | case glslang::EvqHitAttr: return spv::DecorationBlock; |
| 391 | case glslang::EvqCallableData: return spv::DecorationBlock; |
| 392 | case glslang::EvqCallableDataIn: return spv::DecorationBlock; |
| 393 | case glslang::EvqHitObjectAttrNV: return spv::DecorationBlock; |
| 394 | #endif |
| 395 | default: |
| 396 | assert(0); |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | return spv::DecorationMax; |
| 401 | } |
| 402 | |
| 403 | // Translate glslang type to SPIR-V memory decorations. |
| 404 | void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, |
| 405 | bool useVulkanMemoryModel) |
| 406 | { |
| 407 | if (!useVulkanMemoryModel) { |
| 408 | if (qualifier.isCoherent()) |
| 409 | memory.push_back(spv::DecorationCoherent); |
| 410 | if (qualifier.isVolatile()) { |
| 411 | memory.push_back(spv::DecorationVolatile); |
| 412 | memory.push_back(spv::DecorationCoherent); |
| 413 | } |
| 414 | } |
| 415 | if (qualifier.isRestrict()) |
| 416 | memory.push_back(spv::DecorationRestrict); |
| 417 | if (qualifier.isReadOnly()) |
| 418 | memory.push_back(spv::DecorationNonWritable); |
| 419 | if (qualifier.isWriteOnly()) |
| 420 | memory.push_back(spv::DecorationNonReadable); |
| 421 | } |
| 422 | |
| 423 | // Translate glslang type to SPIR-V layout decorations. |
| 424 | spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
| 425 | { |
| 426 | if (type.isMatrix()) { |
| 427 | switch (matrixLayout) { |
| 428 | case glslang::ElmRowMajor: |
| 429 | return spv::DecorationRowMajor; |
| 430 | case glslang::ElmColumnMajor: |
| 431 | return spv::DecorationColMajor; |
| 432 | default: |
| 433 | // opaque layouts don't need a majorness |
| 434 | return spv::DecorationMax; |
| 435 | } |
| 436 | } else { |
| 437 | switch (type.getBasicType()) { |
| 438 | default: |
| 439 | return spv::DecorationMax; |
| 440 | break; |
| 441 | case glslang::EbtBlock: |
| 442 | switch (type.getQualifier().storage) { |
| 443 | case glslang::EvqShared: |
| 444 | case glslang::EvqUniform: |
| 445 | case glslang::EvqBuffer: |
| 446 | switch (type.getQualifier().layoutPacking) { |
| 447 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 448 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 449 | default: |
| 450 | return spv::DecorationMax; |
| 451 | } |
| 452 | case glslang::EvqVaryingIn: |
| 453 | case glslang::EvqVaryingOut: |
| 454 | if (type.getQualifier().isTaskMemory()) { |
| 455 | switch (type.getQualifier().layoutPacking) { |
| 456 | case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 457 | case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 458 | default: break; |
| 459 | } |
| 460 | } else { |
| 461 | assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 462 | } |
| 463 | return spv::DecorationMax; |
| 464 | #ifndef GLSLANG_WEB |
| 465 | case glslang::EvqPayload: |
| 466 | case glslang::EvqPayloadIn: |
| 467 | case glslang::EvqHitAttr: |
| 468 | case glslang::EvqCallableData: |
| 469 | case glslang::EvqCallableDataIn: |
| 470 | case glslang::EvqHitObjectAttrNV: |
| 471 | return spv::DecorationMax; |
| 472 | #endif |
| 473 | default: |
| 474 | assert(0); |
| 475 | return spv::DecorationMax; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Translate glslang type to SPIR-V interpolation decorations. |
| 482 | // Returns spv::DecorationMax when no decoration |
| 483 | // should be applied. |
| 484 | spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
| 485 | { |
| 486 | if (qualifier.smooth) |
| 487 | // Smooth decoration doesn't exist in SPIR-V 1.0 |
| 488 | return spv::DecorationMax; |
| 489 | else if (qualifier.isNonPerspective()) |
| 490 | return spv::DecorationNoPerspective; |
| 491 | else if (qualifier.flat) |
| 492 | return spv::DecorationFlat; |
| 493 | else if (qualifier.isExplicitInterpolation()) { |
| 494 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 495 | return spv::DecorationExplicitInterpAMD; |
| 496 | } |
| 497 | else |
| 498 | return spv::DecorationMax; |
| 499 | } |
| 500 | |
| 501 | // Translate glslang type to SPIR-V auxiliary storage decorations. |
| 502 | // Returns spv::DecorationMax when no decoration |
| 503 | // should be applied. |
| 504 | spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 505 | { |
| 506 | if (qualifier.centroid) |
| 507 | return spv::DecorationCentroid; |
| 508 | #ifndef GLSLANG_WEB |
| 509 | else if (qualifier.patch) |
| 510 | return spv::DecorationPatch; |
| 511 | else if (qualifier.sample) { |
| 512 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 513 | return spv::DecorationSample; |
| 514 | } |
| 515 | #endif |
| 516 | |
| 517 | return spv::DecorationMax; |
| 518 | } |
| 519 | |
| 520 | // If glslang type is invariant, return SPIR-V invariant decoration. |
| 521 | spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
| 522 | { |
| 523 | if (qualifier.invariant) |
| 524 | return spv::DecorationInvariant; |
| 525 | else |
| 526 | return spv::DecorationMax; |
| 527 | } |
| 528 | |
| 529 | // If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 530 | spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 531 | { |
| 532 | #ifndef GLSLANG_WEB |
| 533 | if (qualifier.isNoContraction()) |
| 534 | return spv::DecorationNoContraction; |
| 535 | else |
| 536 | #endif |
| 537 | return spv::DecorationMax; |
| 538 | } |
| 539 | |
| 540 | // If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 541 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 542 | { |
| 543 | #ifndef GLSLANG_WEB |
| 544 | if (qualifier.isNonUniform()) { |
| 545 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing" , spv::Spv_1_5); |
| 546 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 547 | return spv::DecorationNonUniformEXT; |
| 548 | } else |
| 549 | #endif |
| 550 | return spv::DecorationMax; |
| 551 | } |
| 552 | |
| 553 | // If lvalue flags contains nonUniform, return SPIR-V NonUniform decoration. |
| 554 | spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration( |
| 555 | const spv::Builder::AccessChain::CoherentFlags& coherentFlags) |
| 556 | { |
| 557 | #ifndef GLSLANG_WEB |
| 558 | if (coherentFlags.isNonUniform()) { |
| 559 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing" , spv::Spv_1_5); |
| 560 | builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 561 | return spv::DecorationNonUniformEXT; |
| 562 | } else |
| 563 | #endif |
| 564 | return spv::DecorationMax; |
| 565 | } |
| 566 | |
| 567 | spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess( |
| 568 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 569 | { |
| 570 | spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
| 571 | |
| 572 | #ifndef GLSLANG_WEB |
| 573 | if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) |
| 574 | return mask; |
| 575 | |
| 576 | if (coherentFlags.isVolatile() || coherentFlags.anyCoherent()) { |
| 577 | mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 578 | spv::MemoryAccessMakePointerVisibleKHRMask; |
| 579 | } |
| 580 | |
| 581 | if (coherentFlags.nonprivate) { |
| 582 | mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 583 | } |
| 584 | if (coherentFlags.volatil) { |
| 585 | mask = mask | spv::MemoryAccessVolatileMask; |
| 586 | } |
| 587 | if (mask != spv::MemoryAccessMaskNone) { |
| 588 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 589 | } |
| 590 | #endif |
| 591 | |
| 592 | return mask; |
| 593 | } |
| 594 | |
| 595 | spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands( |
| 596 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 597 | { |
| 598 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 599 | |
| 600 | #ifndef GLSLANG_WEB |
| 601 | if (!glslangIntermediate->usingVulkanMemoryModel()) |
| 602 | return mask; |
| 603 | |
| 604 | if (coherentFlags.volatil || |
| 605 | coherentFlags.anyCoherent()) { |
| 606 | mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 607 | spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 608 | } |
| 609 | if (coherentFlags.nonprivate) { |
| 610 | mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 611 | } |
| 612 | if (coherentFlags.volatil) { |
| 613 | mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 614 | } |
| 615 | if (mask != spv::ImageOperandsMaskNone) { |
| 616 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 617 | } |
| 618 | #endif |
| 619 | |
| 620 | return mask; |
| 621 | } |
| 622 | |
| 623 | spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 624 | { |
| 625 | spv::Builder::AccessChain::CoherentFlags flags = {}; |
| 626 | #ifndef GLSLANG_WEB |
| 627 | flags.coherent = type.getQualifier().coherent; |
| 628 | flags.devicecoherent = type.getQualifier().devicecoherent; |
| 629 | flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 630 | // shared variables are implicitly workgroupcoherent in GLSL. |
| 631 | flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 632 | type.getQualifier().storage == glslang::EvqShared; |
| 633 | flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
| 634 | flags.shadercallcoherent = type.getQualifier().shadercallcoherent; |
| 635 | flags.volatil = type.getQualifier().volatil; |
| 636 | // *coherent variables are implicitly nonprivate in GLSL |
| 637 | flags.nonprivate = type.getQualifier().nonprivate || |
| 638 | flags.anyCoherent() || |
| 639 | flags.volatil; |
| 640 | flags.isImage = type.getBasicType() == glslang::EbtSampler; |
| 641 | #endif |
| 642 | flags.nonUniform = type.getQualifier().nonUniform; |
| 643 | return flags; |
| 644 | } |
| 645 | |
| 646 | spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope( |
| 647 | const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 648 | { |
| 649 | spv::Scope scope = spv::ScopeMax; |
| 650 | |
| 651 | #ifndef GLSLANG_WEB |
| 652 | if (coherentFlags.volatil || coherentFlags.coherent) { |
| 653 | // coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 654 | scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 655 | } else if (coherentFlags.devicecoherent) { |
| 656 | scope = spv::ScopeDevice; |
| 657 | } else if (coherentFlags.queuefamilycoherent) { |
| 658 | scope = spv::ScopeQueueFamilyKHR; |
| 659 | } else if (coherentFlags.workgroupcoherent) { |
| 660 | scope = spv::ScopeWorkgroup; |
| 661 | } else if (coherentFlags.subgroupcoherent) { |
| 662 | scope = spv::ScopeSubgroup; |
| 663 | } else if (coherentFlags.shadercallcoherent) { |
| 664 | scope = spv::ScopeShaderCallKHR; |
| 665 | } |
| 666 | if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 667 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 668 | } |
| 669 | #endif |
| 670 | |
| 671 | return scope; |
| 672 | } |
| 673 | |
| 674 | // Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 675 | // associated capabilities when required. For some built-in variables, a capability |
| 676 | // is generated only when using the variable in an executable instruction, but not when |
| 677 | // just declaring a struct member variable with it. This is true for PointSize, |
| 678 | // ClipDistance, and CullDistance. |
| 679 | spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, |
| 680 | bool memberDeclaration) |
| 681 | { |
| 682 | switch (builtIn) { |
| 683 | case glslang::EbvPointSize: |
| 684 | #ifndef GLSLANG_WEB |
| 685 | // Defer adding the capability until the built-in is actually used. |
| 686 | if (! memberDeclaration) { |
| 687 | switch (glslangIntermediate->getStage()) { |
| 688 | case EShLangGeometry: |
| 689 | builder.addCapability(spv::CapabilityGeometryPointSize); |
| 690 | break; |
| 691 | case EShLangTessControl: |
| 692 | case EShLangTessEvaluation: |
| 693 | builder.addCapability(spv::CapabilityTessellationPointSize); |
| 694 | break; |
| 695 | default: |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | #endif |
| 700 | return spv::BuiltInPointSize; |
| 701 | |
| 702 | case glslang::EbvPosition: return spv::BuiltInPosition; |
| 703 | case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 704 | case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
| 705 | case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 706 | case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
| 707 | |
| 708 | case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 709 | case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 710 | case glslang::EbvFace: return spv::BuiltInFrontFacing; |
| 711 | case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 712 | |
| 713 | case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 714 | case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 715 | case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 716 | case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 717 | case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 718 | case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
| 719 | |
| 720 | #ifndef GLSLANG_WEB |
| 721 | // These *Distance capabilities logically belong here, but if the member is declared and |
| 722 | // then never used, consumers of SPIR-V prefer the capability not be declared. |
| 723 | // They are now generated when used, rather than here when declared. |
| 724 | // Potentially, the specification should be more clear what the minimum |
| 725 | // use needed is to trigger the capability. |
| 726 | // |
| 727 | case glslang::EbvClipDistance: |
| 728 | if (!memberDeclaration) |
| 729 | builder.addCapability(spv::CapabilityClipDistance); |
| 730 | return spv::BuiltInClipDistance; |
| 731 | |
| 732 | case glslang::EbvCullDistance: |
| 733 | if (!memberDeclaration) |
| 734 | builder.addCapability(spv::CapabilityCullDistance); |
| 735 | return spv::BuiltInCullDistance; |
| 736 | |
| 737 | case glslang::EbvViewportIndex: |
| 738 | if (glslangIntermediate->getStage() == EShLangGeometry || |
| 739 | glslangIntermediate->getStage() == EShLangFragment) { |
| 740 | builder.addCapability(spv::CapabilityMultiViewport); |
| 741 | } |
| 742 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 743 | glslangIntermediate->getStage() == EShLangTessControl || |
| 744 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
| 745 | |
| 746 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
| 747 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
| 748 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
| 749 | } |
| 750 | else |
| 751 | builder.addCapability(spv::CapabilityShaderViewportIndex); |
| 752 | } |
| 753 | return spv::BuiltInViewportIndex; |
| 754 | |
| 755 | case glslang::EbvSampleId: |
| 756 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 757 | return spv::BuiltInSampleId; |
| 758 | |
| 759 | case glslang::EbvSamplePosition: |
| 760 | builder.addCapability(spv::CapabilitySampleRateShading); |
| 761 | return spv::BuiltInSamplePosition; |
| 762 | |
| 763 | case glslang::EbvSampleMask: |
| 764 | return spv::BuiltInSampleMask; |
| 765 | |
| 766 | case glslang::EbvLayer: |
| 767 | if (glslangIntermediate->getStage() == EShLangMesh) { |
| 768 | return spv::BuiltInLayer; |
| 769 | } |
| 770 | if (glslangIntermediate->getStage() == EShLangGeometry || |
| 771 | glslangIntermediate->getStage() == EShLangFragment) { |
| 772 | builder.addCapability(spv::CapabilityGeometry); |
| 773 | } |
| 774 | if (glslangIntermediate->getStage() == EShLangVertex || |
| 775 | glslangIntermediate->getStage() == EShLangTessControl || |
| 776 | glslangIntermediate->getStage() == EShLangTessEvaluation) { |
| 777 | |
| 778 | if (builder.getSpvVersion() < spv::Spv_1_5) { |
| 779 | builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
| 780 | builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
| 781 | } else |
| 782 | builder.addCapability(spv::CapabilityShaderLayer); |
| 783 | } |
| 784 | return spv::BuiltInLayer; |
| 785 | |
| 786 | case glslang::EbvBaseVertex: |
| 787 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
| 788 | builder.addCapability(spv::CapabilityDrawParameters); |
| 789 | return spv::BuiltInBaseVertex; |
| 790 | |
| 791 | case glslang::EbvBaseInstance: |
| 792 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
| 793 | builder.addCapability(spv::CapabilityDrawParameters); |
| 794 | return spv::BuiltInBaseInstance; |
| 795 | |
| 796 | case glslang::EbvDrawId: |
| 797 | builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
| 798 | builder.addCapability(spv::CapabilityDrawParameters); |
| 799 | return spv::BuiltInDrawIndex; |
| 800 | |
| 801 | case glslang::EbvPrimitiveId: |
| 802 | if (glslangIntermediate->getStage() == EShLangFragment) |
| 803 | builder.addCapability(spv::CapabilityGeometry); |
| 804 | return spv::BuiltInPrimitiveId; |
| 805 | |
| 806 | case glslang::EbvFragStencilRef: |
| 807 | builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 808 | builder.addCapability(spv::CapabilityStencilExportEXT); |
| 809 | return spv::BuiltInFragStencilRefEXT; |
| 810 | |
| 811 | case glslang::EbvShadingRateKHR: |
| 812 | builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
| 813 | builder.addCapability(spv::CapabilityFragmentShadingRateKHR); |
| 814 | return spv::BuiltInShadingRateKHR; |
| 815 | |
| 816 | case glslang::EbvPrimitiveShadingRateKHR: |
| 817 | builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
| 818 | builder.addCapability(spv::CapabilityFragmentShadingRateKHR); |
| 819 | return spv::BuiltInPrimitiveShadingRateKHR; |
| 820 | |
| 821 | case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
| 822 | case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 823 | case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 824 | case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 825 | case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 826 | case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 827 | |
| 828 | case glslang::EbvSubGroupSize: |
| 829 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 830 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 831 | return spv::BuiltInSubgroupSize; |
| 832 | |
| 833 | case glslang::EbvSubGroupInvocation: |
| 834 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 835 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 836 | return spv::BuiltInSubgroupLocalInvocationId; |
| 837 | |
| 838 | case glslang::EbvSubGroupEqMask: |
| 839 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 840 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 841 | return spv::BuiltInSubgroupEqMask; |
| 842 | |
| 843 | case glslang::EbvSubGroupGeMask: |
| 844 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 845 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 846 | return spv::BuiltInSubgroupGeMask; |
| 847 | |
| 848 | case glslang::EbvSubGroupGtMask: |
| 849 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 850 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 851 | return spv::BuiltInSubgroupGtMask; |
| 852 | |
| 853 | case glslang::EbvSubGroupLeMask: |
| 854 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 855 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 856 | return spv::BuiltInSubgroupLeMask; |
| 857 | |
| 858 | case glslang::EbvSubGroupLtMask: |
| 859 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 860 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 861 | return spv::BuiltInSubgroupLtMask; |
| 862 | |
| 863 | case glslang::EbvNumSubgroups: |
| 864 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 865 | return spv::BuiltInNumSubgroups; |
| 866 | |
| 867 | case glslang::EbvSubgroupID: |
| 868 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 869 | return spv::BuiltInSubgroupId; |
| 870 | |
| 871 | case glslang::EbvSubgroupSize2: |
| 872 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 873 | return spv::BuiltInSubgroupSize; |
| 874 | |
| 875 | case glslang::EbvSubgroupInvocation2: |
| 876 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 877 | return spv::BuiltInSubgroupLocalInvocationId; |
| 878 | |
| 879 | case glslang::EbvSubgroupEqMask2: |
| 880 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 881 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 882 | return spv::BuiltInSubgroupEqMask; |
| 883 | |
| 884 | case glslang::EbvSubgroupGeMask2: |
| 885 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 886 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 887 | return spv::BuiltInSubgroupGeMask; |
| 888 | |
| 889 | case glslang::EbvSubgroupGtMask2: |
| 890 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 891 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 892 | return spv::BuiltInSubgroupGtMask; |
| 893 | |
| 894 | case glslang::EbvSubgroupLeMask2: |
| 895 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 896 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 897 | return spv::BuiltInSubgroupLeMask; |
| 898 | |
| 899 | case glslang::EbvSubgroupLtMask2: |
| 900 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 901 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 902 | return spv::BuiltInSubgroupLtMask; |
| 903 | |
| 904 | case glslang::EbvBaryCoordNoPersp: |
| 905 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 906 | return spv::BuiltInBaryCoordNoPerspAMD; |
| 907 | |
| 908 | case glslang::EbvBaryCoordNoPerspCentroid: |
| 909 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 910 | return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 911 | |
| 912 | case glslang::EbvBaryCoordNoPerspSample: |
| 913 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 914 | return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 915 | |
| 916 | case glslang::EbvBaryCoordSmooth: |
| 917 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 918 | return spv::BuiltInBaryCoordSmoothAMD; |
| 919 | |
| 920 | case glslang::EbvBaryCoordSmoothCentroid: |
| 921 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 922 | return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 923 | |
| 924 | case glslang::EbvBaryCoordSmoothSample: |
| 925 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 926 | return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 927 | |
| 928 | case glslang::EbvBaryCoordPullModel: |
| 929 | builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 930 | return spv::BuiltInBaryCoordPullModelAMD; |
| 931 | |
| 932 | case glslang::EbvDeviceIndex: |
| 933 | builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3); |
| 934 | builder.addCapability(spv::CapabilityDeviceGroup); |
| 935 | return spv::BuiltInDeviceIndex; |
| 936 | |
| 937 | case glslang::EbvViewIndex: |
| 938 | builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3); |
| 939 | builder.addCapability(spv::CapabilityMultiView); |
| 940 | return spv::BuiltInViewIndex; |
| 941 | |
| 942 | case glslang::EbvFragSizeEXT: |
| 943 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 944 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 945 | return spv::BuiltInFragSizeEXT; |
| 946 | |
| 947 | case glslang::EbvFragInvocationCountEXT: |
| 948 | builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 949 | builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 950 | return spv::BuiltInFragInvocationCountEXT; |
| 951 | |
| 952 | case glslang::EbvViewportMaskNV: |
| 953 | if (!memberDeclaration) { |
| 954 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 955 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 956 | } |
| 957 | return spv::BuiltInViewportMaskNV; |
| 958 | case glslang::EbvSecondaryPositionNV: |
| 959 | if (!memberDeclaration) { |
| 960 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 961 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 962 | } |
| 963 | return spv::BuiltInSecondaryPositionNV; |
| 964 | case glslang::EbvSecondaryViewportMaskNV: |
| 965 | if (!memberDeclaration) { |
| 966 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 967 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 968 | } |
| 969 | return spv::BuiltInSecondaryViewportMaskNV; |
| 970 | case glslang::EbvPositionPerViewNV: |
| 971 | if (!memberDeclaration) { |
| 972 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 973 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 974 | } |
| 975 | return spv::BuiltInPositionPerViewNV; |
| 976 | case glslang::EbvViewportMaskPerViewNV: |
| 977 | if (!memberDeclaration) { |
| 978 | builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 979 | builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 980 | } |
| 981 | return spv::BuiltInViewportMaskPerViewNV; |
| 982 | case glslang::EbvFragFullyCoveredNV: |
| 983 | builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 984 | builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 985 | return spv::BuiltInFullyCoveredEXT; |
| 986 | case glslang::EbvFragmentSizeNV: |
| 987 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 988 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 989 | return spv::BuiltInFragmentSizeNV; |
| 990 | case glslang::EbvInvocationsPerPixelNV: |
| 991 | builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 992 | builder.addCapability(spv::CapabilityShadingRateNV); |
| 993 | return spv::BuiltInInvocationsPerPixelNV; |
| 994 | |
| 995 | // ray tracing |
| 996 | case glslang::EbvLaunchId: |
| 997 | return spv::BuiltInLaunchIdKHR; |
| 998 | case glslang::EbvLaunchSize: |
| 999 | return spv::BuiltInLaunchSizeKHR; |
| 1000 | case glslang::EbvWorldRayOrigin: |
| 1001 | return spv::BuiltInWorldRayOriginKHR; |
| 1002 | case glslang::EbvWorldRayDirection: |
| 1003 | return spv::BuiltInWorldRayDirectionKHR; |
| 1004 | case glslang::EbvObjectRayOrigin: |
| 1005 | return spv::BuiltInObjectRayOriginKHR; |
| 1006 | case glslang::EbvObjectRayDirection: |
| 1007 | return spv::BuiltInObjectRayDirectionKHR; |
| 1008 | case glslang::EbvRayTmin: |
| 1009 | return spv::BuiltInRayTminKHR; |
| 1010 | case glslang::EbvRayTmax: |
| 1011 | return spv::BuiltInRayTmaxKHR; |
| 1012 | case glslang::EbvCullMask: |
| 1013 | return spv::BuiltInCullMaskKHR; |
| 1014 | case glslang::EbvPositionFetch: |
| 1015 | return spv::BuiltInHitTriangleVertexPositionsKHR; |
| 1016 | case glslang::EbvInstanceCustomIndex: |
| 1017 | return spv::BuiltInInstanceCustomIndexKHR; |
| 1018 | case glslang::EbvHitT: |
| 1019 | { |
| 1020 | // this is a GLSL alias of RayTmax |
| 1021 | // in SPV_NV_ray_tracing it has a dedicated builtin |
| 1022 | // but in SPV_KHR_ray_tracing it gets mapped to RayTmax |
| 1023 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 1024 | if (extensions.find("GL_NV_ray_tracing" ) != extensions.end()) { |
| 1025 | return spv::BuiltInHitTNV; |
| 1026 | } else { |
| 1027 | return spv::BuiltInRayTmaxKHR; |
| 1028 | } |
| 1029 | } |
| 1030 | case glslang::EbvHitKind: |
| 1031 | return spv::BuiltInHitKindKHR; |
| 1032 | case glslang::EbvObjectToWorld: |
| 1033 | case glslang::EbvObjectToWorld3x4: |
| 1034 | return spv::BuiltInObjectToWorldKHR; |
| 1035 | case glslang::EbvWorldToObject: |
| 1036 | case glslang::EbvWorldToObject3x4: |
| 1037 | return spv::BuiltInWorldToObjectKHR; |
| 1038 | case glslang::EbvIncomingRayFlags: |
| 1039 | return spv::BuiltInIncomingRayFlagsKHR; |
| 1040 | case glslang::EbvGeometryIndex: |
| 1041 | return spv::BuiltInRayGeometryIndexKHR; |
| 1042 | case glslang::EbvCurrentRayTimeNV: |
| 1043 | builder.addExtension(spv::E_SPV_NV_ray_tracing_motion_blur); |
| 1044 | builder.addCapability(spv::CapabilityRayTracingMotionBlurNV); |
| 1045 | return spv::BuiltInCurrentRayTimeNV; |
| 1046 | |
| 1047 | // barycentrics |
| 1048 | case glslang::EbvBaryCoordNV: |
| 1049 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 1050 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 1051 | return spv::BuiltInBaryCoordNV; |
| 1052 | case glslang::EbvBaryCoordNoPerspNV: |
| 1053 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 1054 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 1055 | return spv::BuiltInBaryCoordNoPerspNV; |
| 1056 | |
| 1057 | case glslang::EbvBaryCoordEXT: |
| 1058 | builder.addExtension(spv::E_SPV_KHR_fragment_shader_barycentric); |
| 1059 | builder.addCapability(spv::CapabilityFragmentBarycentricKHR); |
| 1060 | return spv::BuiltInBaryCoordKHR; |
| 1061 | case glslang::EbvBaryCoordNoPerspEXT: |
| 1062 | builder.addExtension(spv::E_SPV_KHR_fragment_shader_barycentric); |
| 1063 | builder.addCapability(spv::CapabilityFragmentBarycentricKHR); |
| 1064 | return spv::BuiltInBaryCoordNoPerspKHR; |
| 1065 | |
| 1066 | // mesh shaders |
| 1067 | case glslang::EbvTaskCountNV: |
| 1068 | return spv::BuiltInTaskCountNV; |
| 1069 | case glslang::EbvPrimitiveCountNV: |
| 1070 | return spv::BuiltInPrimitiveCountNV; |
| 1071 | case glslang::EbvPrimitiveIndicesNV: |
| 1072 | return spv::BuiltInPrimitiveIndicesNV; |
| 1073 | case glslang::EbvClipDistancePerViewNV: |
| 1074 | return spv::BuiltInClipDistancePerViewNV; |
| 1075 | case glslang::EbvCullDistancePerViewNV: |
| 1076 | return spv::BuiltInCullDistancePerViewNV; |
| 1077 | case glslang::EbvLayerPerViewNV: |
| 1078 | return spv::BuiltInLayerPerViewNV; |
| 1079 | case glslang::EbvMeshViewCountNV: |
| 1080 | return spv::BuiltInMeshViewCountNV; |
| 1081 | case glslang::EbvMeshViewIndicesNV: |
| 1082 | return spv::BuiltInMeshViewIndicesNV; |
| 1083 | |
| 1084 | // SPV_EXT_mesh_shader |
| 1085 | case glslang::EbvPrimitivePointIndicesEXT: |
| 1086 | return spv::BuiltInPrimitivePointIndicesEXT; |
| 1087 | case glslang::EbvPrimitiveLineIndicesEXT: |
| 1088 | return spv::BuiltInPrimitiveLineIndicesEXT; |
| 1089 | case glslang::EbvPrimitiveTriangleIndicesEXT: |
| 1090 | return spv::BuiltInPrimitiveTriangleIndicesEXT; |
| 1091 | case glslang::EbvCullPrimitiveEXT: |
| 1092 | return spv::BuiltInCullPrimitiveEXT; |
| 1093 | |
| 1094 | // sm builtins |
| 1095 | case glslang::EbvWarpsPerSM: |
| 1096 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1097 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1098 | return spv::BuiltInWarpsPerSMNV; |
| 1099 | case glslang::EbvSMCount: |
| 1100 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1101 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1102 | return spv::BuiltInSMCountNV; |
| 1103 | case glslang::EbvWarpID: |
| 1104 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1105 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1106 | return spv::BuiltInWarpIDNV; |
| 1107 | case glslang::EbvSMID: |
| 1108 | builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1109 | builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1110 | return spv::BuiltInSMIDNV; |
| 1111 | |
| 1112 | // ARM builtins |
| 1113 | case glslang::EbvCoreCountARM: |
| 1114 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
| 1115 | builder.addCapability(spv::CapabilityCoreBuiltinsARM); |
| 1116 | return spv::BuiltInCoreCountARM; |
| 1117 | case glslang::EbvCoreIDARM: |
| 1118 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
| 1119 | builder.addCapability(spv::CapabilityCoreBuiltinsARM); |
| 1120 | return spv::BuiltInCoreIDARM; |
| 1121 | case glslang::EbvCoreMaxIDARM: |
| 1122 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
| 1123 | builder.addCapability(spv::CapabilityCoreBuiltinsARM); |
| 1124 | return spv::BuiltInCoreMaxIDARM; |
| 1125 | case glslang::EbvWarpIDARM: |
| 1126 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
| 1127 | builder.addCapability(spv::CapabilityCoreBuiltinsARM); |
| 1128 | return spv::BuiltInWarpIDARM; |
| 1129 | case glslang::EbvWarpMaxIDARM: |
| 1130 | builder.addExtension(spv::E_SPV_ARM_core_builtins); |
| 1131 | builder.addCapability(spv::CapabilityCoreBuiltinsARM); |
| 1132 | return spv::BuiltInWarpMaxIDARM; |
| 1133 | #endif |
| 1134 | |
| 1135 | default: |
| 1136 | return spv::BuiltInMax; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | // Translate glslang image layout format to SPIR-V image format. |
| 1141 | spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
| 1142 | { |
| 1143 | assert(type.getBasicType() == glslang::EbtSampler); |
| 1144 | |
| 1145 | #ifdef GLSLANG_WEB |
| 1146 | return spv::ImageFormatUnknown; |
| 1147 | #endif |
| 1148 | |
| 1149 | // Check for capabilities |
| 1150 | switch (type.getQualifier().getFormat()) { |
| 1151 | case glslang::ElfRg32f: |
| 1152 | case glslang::ElfRg16f: |
| 1153 | case glslang::ElfR11fG11fB10f: |
| 1154 | case glslang::ElfR16f: |
| 1155 | case glslang::ElfRgba16: |
| 1156 | case glslang::ElfRgb10A2: |
| 1157 | case glslang::ElfRg16: |
| 1158 | case glslang::ElfRg8: |
| 1159 | case glslang::ElfR16: |
| 1160 | case glslang::ElfR8: |
| 1161 | case glslang::ElfRgba16Snorm: |
| 1162 | case glslang::ElfRg16Snorm: |
| 1163 | case glslang::ElfRg8Snorm: |
| 1164 | case glslang::ElfR16Snorm: |
| 1165 | case glslang::ElfR8Snorm: |
| 1166 | |
| 1167 | case glslang::ElfRg32i: |
| 1168 | case glslang::ElfRg16i: |
| 1169 | case glslang::ElfRg8i: |
| 1170 | case glslang::ElfR16i: |
| 1171 | case glslang::ElfR8i: |
| 1172 | |
| 1173 | case glslang::ElfRgb10a2ui: |
| 1174 | case glslang::ElfRg32ui: |
| 1175 | case glslang::ElfRg16ui: |
| 1176 | case glslang::ElfRg8ui: |
| 1177 | case glslang::ElfR16ui: |
| 1178 | case glslang::ElfR8ui: |
| 1179 | builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 1180 | break; |
| 1181 | |
| 1182 | case glslang::ElfR64ui: |
| 1183 | case glslang::ElfR64i: |
| 1184 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 1185 | builder.addCapability(spv::CapabilityInt64ImageEXT); |
| 1186 | default: |
| 1187 | break; |
| 1188 | } |
| 1189 | |
| 1190 | // do the translation |
| 1191 | switch (type.getQualifier().getFormat()) { |
| 1192 | case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1193 | case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1194 | case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1195 | case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1196 | case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1197 | case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1198 | case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1199 | case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1200 | case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1201 | case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1202 | case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1203 | case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1204 | case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1205 | case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1206 | case glslang::ElfR16: return spv::ImageFormatR16; |
| 1207 | case glslang::ElfR8: return spv::ImageFormatR8; |
| 1208 | case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1209 | case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1210 | case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1211 | case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1212 | case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1213 | case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1214 | case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1215 | case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1216 | case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1217 | case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1218 | case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1219 | case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1220 | case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1221 | case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1222 | case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1223 | case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1224 | case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1225 | case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1226 | case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1227 | case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1228 | case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1229 | case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1230 | case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1231 | case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
| 1232 | case glslang::ElfR64ui: return spv::ImageFormatR64ui; |
| 1233 | case glslang::ElfR64i: return spv::ImageFormatR64i; |
| 1234 | default: return spv::ImageFormatMax; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl( |
| 1239 | const glslang::TIntermSelection& selectionNode) const |
| 1240 | { |
| 1241 | if (selectionNode.getFlatten()) |
| 1242 | return spv::SelectionControlFlattenMask; |
| 1243 | if (selectionNode.getDontFlatten()) |
| 1244 | return spv::SelectionControlDontFlattenMask; |
| 1245 | return spv::SelectionControlMaskNone; |
| 1246 | } |
| 1247 | |
| 1248 | spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) |
| 1249 | const |
| 1250 | { |
| 1251 | if (switchNode.getFlatten()) |
| 1252 | return spv::SelectionControlFlattenMask; |
| 1253 | if (switchNode.getDontFlatten()) |
| 1254 | return spv::SelectionControlDontFlattenMask; |
| 1255 | return spv::SelectionControlMaskNone; |
| 1256 | } |
| 1257 | |
| 1258 | // return a non-0 dependency if the dependency argument must be set |
| 1259 | spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
| 1260 | std::vector<unsigned int>& operands) const |
| 1261 | { |
| 1262 | spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1263 | |
| 1264 | if (loopNode.getDontUnroll()) |
| 1265 | control = control | spv::LoopControlDontUnrollMask; |
| 1266 | if (loopNode.getUnroll()) |
| 1267 | control = control | spv::LoopControlUnrollMask; |
| 1268 | if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
| 1269 | control = control | spv::LoopControlDependencyInfiniteMask; |
| 1270 | else if (loopNode.getLoopDependency() > 0) { |
| 1271 | control = control | spv::LoopControlDependencyLengthMask; |
| 1272 | operands.push_back((unsigned int)loopNode.getLoopDependency()); |
| 1273 | } |
| 1274 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 1275 | if (loopNode.getMinIterations() > 0) { |
| 1276 | control = control | spv::LoopControlMinIterationsMask; |
| 1277 | operands.push_back(loopNode.getMinIterations()); |
| 1278 | } |
| 1279 | if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
| 1280 | control = control | spv::LoopControlMaxIterationsMask; |
| 1281 | operands.push_back(loopNode.getMaxIterations()); |
| 1282 | } |
| 1283 | if (loopNode.getIterationMultiple() > 1) { |
| 1284 | control = control | spv::LoopControlIterationMultipleMask; |
| 1285 | operands.push_back(loopNode.getIterationMultiple()); |
| 1286 | } |
| 1287 | if (loopNode.getPeelCount() > 0) { |
| 1288 | control = control | spv::LoopControlPeelCountMask; |
| 1289 | operands.push_back(loopNode.getPeelCount()); |
| 1290 | } |
| 1291 | if (loopNode.getPartialCount() > 0) { |
| 1292 | control = control | spv::LoopControlPartialCountMask; |
| 1293 | operands.push_back(loopNode.getPartialCount()); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | return control; |
| 1298 | } |
| 1299 | |
| 1300 | // Translate glslang type to SPIR-V storage class. |
| 1301 | spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1302 | { |
| 1303 | if (type.getBasicType() == glslang::EbtRayQuery || type.getBasicType() == glslang::EbtHitObjectNV) |
| 1304 | return spv::StorageClassPrivate; |
| 1305 | #ifndef GLSLANG_WEB |
| 1306 | if (type.getQualifier().isSpirvByReference()) { |
| 1307 | if (type.getQualifier().isParamInput() || type.getQualifier().isParamOutput()) |
| 1308 | return spv::StorageClassFunction; |
| 1309 | } |
| 1310 | #endif |
| 1311 | if (type.getQualifier().isPipeInput()) |
| 1312 | return spv::StorageClassInput; |
| 1313 | if (type.getQualifier().isPipeOutput()) |
| 1314 | return spv::StorageClassOutput; |
| 1315 | if (type.getQualifier().storage == glslang::EvqTileImageEXT || type.isAttachmentEXT()) { |
| 1316 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
| 1317 | builder.addCapability(spv::CapabilityTileImageColorReadAccessEXT); |
| 1318 | return spv::StorageClassTileImageEXT; |
| 1319 | } |
| 1320 | |
| 1321 | if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
| 1322 | type.getQualifier().storage == glslang::EvqUniform) { |
| 1323 | if (type.isAtomic()) |
| 1324 | return spv::StorageClassAtomicCounter; |
| 1325 | if (type.containsOpaque() && !glslangIntermediate->getBindlessMode()) |
| 1326 | return spv::StorageClassUniformConstant; |
| 1327 | } |
| 1328 | |
| 1329 | if (type.getQualifier().isUniformOrBuffer() && |
| 1330 | type.getQualifier().isShaderRecord()) { |
| 1331 | return spv::StorageClassShaderRecordBufferKHR; |
| 1332 | } |
| 1333 | |
| 1334 | if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
| 1335 | builder.addIncorporatedExtension(spv::E_SPV_KHR_storage_buffer_storage_class, spv::Spv_1_3); |
| 1336 | return spv::StorageClassStorageBuffer; |
| 1337 | } |
| 1338 | |
| 1339 | if (type.getQualifier().isUniformOrBuffer()) { |
| 1340 | if (type.getQualifier().isPushConstant()) |
| 1341 | return spv::StorageClassPushConstant; |
| 1342 | if (type.getBasicType() == glslang::EbtBlock) |
| 1343 | return spv::StorageClassUniform; |
| 1344 | return spv::StorageClassUniformConstant; |
| 1345 | } |
| 1346 | |
| 1347 | if (type.getQualifier().storage == glslang::EvqShared && type.getBasicType() == glslang::EbtBlock) { |
| 1348 | builder.addExtension(spv::E_SPV_KHR_workgroup_memory_explicit_layout); |
| 1349 | builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayoutKHR); |
| 1350 | return spv::StorageClassWorkgroup; |
| 1351 | } |
| 1352 | |
| 1353 | switch (type.getQualifier().storage) { |
| 1354 | case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1355 | case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1356 | case glslang::EvqTemporary: return spv::StorageClassFunction; |
| 1357 | case glslang::EvqShared: return spv::StorageClassWorkgroup; |
| 1358 | #ifndef GLSLANG_WEB |
| 1359 | case glslang::EvqPayload: return spv::StorageClassRayPayloadKHR; |
| 1360 | case glslang::EvqPayloadIn: return spv::StorageClassIncomingRayPayloadKHR; |
| 1361 | case glslang::EvqHitAttr: return spv::StorageClassHitAttributeKHR; |
| 1362 | case glslang::EvqCallableData: return spv::StorageClassCallableDataKHR; |
| 1363 | case glslang::EvqCallableDataIn: return spv::StorageClassIncomingCallableDataKHR; |
| 1364 | case glslang::EvqtaskPayloadSharedEXT : return spv::StorageClassTaskPayloadWorkgroupEXT; |
| 1365 | case glslang::EvqHitObjectAttrNV: return spv::StorageClassHitObjectAttributeNV; |
| 1366 | case glslang::EvqSpirvStorageClass: return static_cast<spv::StorageClass>(type.getQualifier().spirvStorageClass); |
| 1367 | #endif |
| 1368 | default: |
| 1369 | assert(0); |
| 1370 | break; |
| 1371 | } |
| 1372 | |
| 1373 | return spv::StorageClassFunction; |
| 1374 | } |
| 1375 | |
| 1376 | // Translate glslang constants to SPIR-V literals |
| 1377 | void TGlslangToSpvTraverser::TranslateLiterals(const glslang::TVector<const glslang::TIntermConstantUnion*>& constants, |
| 1378 | std::vector<unsigned>& literals) const |
| 1379 | { |
| 1380 | for (auto constant : constants) { |
| 1381 | if (constant->getBasicType() == glslang::EbtFloat) { |
| 1382 | float floatValue = static_cast<float>(constant->getConstArray()[0].getDConst()); |
| 1383 | unsigned literal; |
| 1384 | static_assert(sizeof(literal) == sizeof(floatValue), "sizeof(unsigned) != sizeof(float)" ); |
| 1385 | memcpy(&literal, &floatValue, sizeof(literal)); |
| 1386 | literals.push_back(literal); |
| 1387 | } else if (constant->getBasicType() == glslang::EbtInt) { |
| 1388 | unsigned literal = constant->getConstArray()[0].getIConst(); |
| 1389 | literals.push_back(literal); |
| 1390 | } else if (constant->getBasicType() == glslang::EbtUint) { |
| 1391 | unsigned literal = constant->getConstArray()[0].getUConst(); |
| 1392 | literals.push_back(literal); |
| 1393 | } else if (constant->getBasicType() == glslang::EbtBool) { |
| 1394 | unsigned literal = constant->getConstArray()[0].getBConst(); |
| 1395 | literals.push_back(literal); |
| 1396 | } else if (constant->getBasicType() == glslang::EbtString) { |
| 1397 | auto str = constant->getConstArray()[0].getSConst()->c_str(); |
| 1398 | unsigned literal = 0; |
| 1399 | char* literalPtr = reinterpret_cast<char*>(&literal); |
| 1400 | unsigned charCount = 0; |
| 1401 | char ch = 0; |
| 1402 | do { |
| 1403 | ch = *(str++); |
| 1404 | *(literalPtr++) = ch; |
| 1405 | ++charCount; |
| 1406 | if (charCount == 4) { |
| 1407 | literals.push_back(literal); |
| 1408 | literalPtr = reinterpret_cast<char*>(&literal); |
| 1409 | charCount = 0; |
| 1410 | } |
| 1411 | } while (ch != 0); |
| 1412 | |
| 1413 | // Partial literal is padded with 0 |
| 1414 | if (charCount > 0) { |
| 1415 | for (; charCount < 4; ++charCount) |
| 1416 | *(literalPtr++) = 0; |
| 1417 | literals.push_back(literal); |
| 1418 | } |
| 1419 | } else |
| 1420 | assert(0); // Unexpected type |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | // Add capabilities pertaining to how an array is indexed. |
| 1425 | void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1426 | const glslang::TType& indexType) |
| 1427 | { |
| 1428 | #ifndef GLSLANG_WEB |
| 1429 | if (indexType.getQualifier().isNonUniform()) { |
| 1430 | // deal with an asserted non-uniform index |
| 1431 | // SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
| 1432 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1433 | if (baseType.getQualifier().hasAttachment()) |
| 1434 | builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
| 1435 | else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
| 1436 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
| 1437 | else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
| 1438 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1439 | else if (baseType.isImage()) |
| 1440 | builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1441 | else if (baseType.isTexture()) |
| 1442 | builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1443 | } else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1444 | if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1445 | builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1446 | else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1447 | builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1448 | } |
| 1449 | } else { |
| 1450 | // assume a dynamically uniform index |
| 1451 | if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1452 | if (baseType.getQualifier().hasAttachment()) { |
| 1453 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing" , spv::Spv_1_5); |
| 1454 | builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
| 1455 | } else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
| 1456 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing" , spv::Spv_1_5); |
| 1457 | builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
| 1458 | } else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
| 1459 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing" , spv::Spv_1_5); |
| 1460 | builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | #endif |
| 1465 | } |
| 1466 | |
| 1467 | // Return whether or not the given type is something that should be tied to a |
| 1468 | // descriptor set. |
| 1469 | bool IsDescriptorResource(const glslang::TType& type) |
| 1470 | { |
| 1471 | // uniform and buffer blocks are included, unless it is a push_constant |
| 1472 | if (type.getBasicType() == glslang::EbtBlock) |
| 1473 | return type.getQualifier().isUniformOrBuffer() && |
| 1474 | ! type.getQualifier().isShaderRecord() && |
| 1475 | ! type.getQualifier().isPushConstant(); |
| 1476 | |
| 1477 | // non block... |
| 1478 | // basically samplerXXX/subpass/sampler/texture are all included |
| 1479 | // if they are the global-scope-class, not the function parameter |
| 1480 | // (or local, if they ever exist) class. |
| 1481 | if (type.getBasicType() == glslang::EbtSampler || |
| 1482 | type.getBasicType() == glslang::EbtAccStruct) |
| 1483 | return type.getQualifier().isUniformOrBuffer(); |
| 1484 | |
| 1485 | // None of the above. |
| 1486 | return false; |
| 1487 | } |
| 1488 | |
| 1489 | void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1490 | { |
| 1491 | if (child.layoutMatrix == glslang::ElmNone) |
| 1492 | child.layoutMatrix = parent.layoutMatrix; |
| 1493 | |
| 1494 | if (parent.invariant) |
| 1495 | child.invariant = true; |
| 1496 | if (parent.flat) |
| 1497 | child.flat = true; |
| 1498 | if (parent.centroid) |
| 1499 | child.centroid = true; |
| 1500 | #ifndef GLSLANG_WEB |
| 1501 | if (parent.nopersp) |
| 1502 | child.nopersp = true; |
| 1503 | if (parent.explicitInterp) |
| 1504 | child.explicitInterp = true; |
| 1505 | if (parent.perPrimitiveNV) |
| 1506 | child.perPrimitiveNV = true; |
| 1507 | if (parent.perViewNV) |
| 1508 | child.perViewNV = true; |
| 1509 | if (parent.perTaskNV) |
| 1510 | child.perTaskNV = true; |
| 1511 | if (parent.storage == glslang::EvqtaskPayloadSharedEXT) |
| 1512 | child.storage = glslang::EvqtaskPayloadSharedEXT; |
| 1513 | if (parent.patch) |
| 1514 | child.patch = true; |
| 1515 | if (parent.sample) |
| 1516 | child.sample = true; |
| 1517 | if (parent.coherent) |
| 1518 | child.coherent = true; |
| 1519 | if (parent.devicecoherent) |
| 1520 | child.devicecoherent = true; |
| 1521 | if (parent.queuefamilycoherent) |
| 1522 | child.queuefamilycoherent = true; |
| 1523 | if (parent.workgroupcoherent) |
| 1524 | child.workgroupcoherent = true; |
| 1525 | if (parent.subgroupcoherent) |
| 1526 | child.subgroupcoherent = true; |
| 1527 | if (parent.shadercallcoherent) |
| 1528 | child.shadercallcoherent = true; |
| 1529 | if (parent.nonprivate) |
| 1530 | child.nonprivate = true; |
| 1531 | if (parent.volatil) |
| 1532 | child.volatil = true; |
| 1533 | if (parent.restrict) |
| 1534 | child.restrict = true; |
| 1535 | if (parent.readonly) |
| 1536 | child.readonly = true; |
| 1537 | if (parent.writeonly) |
| 1538 | child.writeonly = true; |
| 1539 | #endif |
| 1540 | if (parent.nonUniform) |
| 1541 | child.nonUniform = true; |
| 1542 | } |
| 1543 | |
| 1544 | bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
| 1545 | { |
| 1546 | // This should list qualifiers that simultaneous satisfy: |
| 1547 | // - struct members might inherit from a struct declaration |
| 1548 | // (note that non-block structs don't explicitly inherit, |
| 1549 | // only implicitly, meaning no decoration involved) |
| 1550 | // - affect decorations on the struct members |
| 1551 | // (note smooth does not, and expecting something like volatile |
| 1552 | // to effect the whole object) |
| 1553 | // - are not part of the offset/st430/etc or row/column-major layout |
| 1554 | return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
| 1555 | } |
| 1556 | |
| 1557 | // |
| 1558 | // Implement the TGlslangToSpvTraverser class. |
| 1559 | // |
| 1560 | |
| 1561 | TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, |
| 1562 | const glslang::TIntermediate* glslangIntermediate, |
| 1563 | spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) : |
| 1564 | TIntermTraverser(true, false, true), |
| 1565 | options(options), |
| 1566 | shaderEntry(nullptr), currentFunction(nullptr), |
| 1567 | sequenceDepth(0), logger(buildLogger), |
| 1568 | builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
| 1569 | inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
| 1570 | glslangIntermediate(glslangIntermediate), |
| 1571 | nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()), |
| 1572 | nonSemanticDebugPrintf(0), |
| 1573 | taskPayloadID(0) |
| 1574 | { |
| 1575 | bool isMeshShaderExt = (glslangIntermediate->getRequestedExtensions().find(glslang::E_GL_EXT_mesh_shader) != |
| 1576 | glslangIntermediate->getRequestedExtensions().end()); |
| 1577 | spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage(), isMeshShaderExt); |
| 1578 | |
| 1579 | builder.clearAccessChain(); |
| 1580 | builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1581 | glslangIntermediate->getVersion()); |
| 1582 | |
| 1583 | if (options.generateDebugInfo) { |
| 1584 | builder.setEmitOpLines(); |
| 1585 | builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1586 | |
| 1587 | // Set the source shader's text. If for SPV version 1.0, include |
| 1588 | // a preamble in comments stating the OpModuleProcessed instructions. |
| 1589 | // Otherwise, emit those as actual instructions. |
| 1590 | std::string text; |
| 1591 | const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1592 | for (int p = 0; p < (int)processes.size(); ++p) { |
| 1593 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
| 1594 | text.append("// OpModuleProcessed " ); |
| 1595 | text.append(processes[p]); |
| 1596 | text.append("\n" ); |
| 1597 | } else |
| 1598 | builder.addModuleProcessed(processes[p]); |
| 1599 | } |
| 1600 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
| 1601 | text.append("#line 1\n" ); |
| 1602 | text.append(glslangIntermediate->getSourceText()); |
| 1603 | builder.setSourceText(text); |
| 1604 | // Pass name and text for all included files |
| 1605 | const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1606 | for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1607 | builder.addInclude(iItr->first, iItr->second); |
| 1608 | } |
| 1609 | |
| 1610 | builder.setEmitNonSemanticShaderDebugInfo(options.emitNonSemanticShaderDebugInfo); |
| 1611 | builder.setEmitNonSemanticShaderDebugSource(options.emitNonSemanticShaderDebugSource); |
| 1612 | |
| 1613 | stdBuiltins = builder.import("GLSL.std.450" ); |
| 1614 | |
| 1615 | spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1616 | spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1617 | |
| 1618 | if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1619 | addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
| 1620 | builder.addIncorporatedExtension(spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5); |
| 1621 | builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
| 1622 | } |
| 1623 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 1624 | memoryModel = spv::MemoryModelVulkanKHR; |
| 1625 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 1626 | builder.addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5); |
| 1627 | } |
| 1628 | builder.setMemoryModel(addressingModel, memoryModel); |
| 1629 | |
| 1630 | if (glslangIntermediate->usingVariablePointers()) { |
| 1631 | builder.addCapability(spv::CapabilityVariablePointers); |
| 1632 | } |
| 1633 | |
| 1634 | shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1635 | entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
| 1636 | |
| 1637 | // Add the source extensions |
| 1638 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1639 | for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
| 1640 | builder.addSourceExtension(it->c_str()); |
| 1641 | |
| 1642 | // Add the top-level modes for this shader. |
| 1643 | |
| 1644 | if (glslangIntermediate->getXfbMode()) { |
| 1645 | builder.addCapability(spv::CapabilityTransformFeedback); |
| 1646 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
| 1647 | } |
| 1648 | |
| 1649 | if (glslangIntermediate->getLayoutPrimitiveCulling()) { |
| 1650 | builder.addCapability(spv::CapabilityRayTraversalPrimitiveCullingKHR); |
| 1651 | } |
| 1652 | |
| 1653 | #ifndef GLSLANG_WEB |
| 1654 | if (glslangIntermediate->getSubgroupUniformControlFlow()) { |
| 1655 | builder.addExtension(spv::E_SPV_KHR_subgroup_uniform_control_flow); |
| 1656 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeSubgroupUniformControlFlowKHR); |
| 1657 | } |
| 1658 | #endif |
| 1659 | |
| 1660 | unsigned int mode; |
| 1661 | switch (glslangIntermediate->getStage()) { |
| 1662 | case EShLangVertex: |
| 1663 | builder.addCapability(spv::CapabilityShader); |
| 1664 | break; |
| 1665 | |
| 1666 | case EShLangFragment: |
| 1667 | builder.addCapability(spv::CapabilityShader); |
| 1668 | if (glslangIntermediate->getPixelCenterInteger()) |
| 1669 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
| 1670 | |
| 1671 | if (glslangIntermediate->getOriginUpperLeft()) |
| 1672 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
| 1673 | else |
| 1674 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
| 1675 | |
| 1676 | if (glslangIntermediate->getEarlyFragmentTests()) |
| 1677 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1678 | |
| 1679 | if (glslangIntermediate->getEarlyAndLateFragmentTestsAMD()) |
| 1680 | { |
| 1681 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyAndLateFragmentTestsAMD); |
| 1682 | builder.addExtension(spv::E_SPV_AMD_shader_early_and_late_fragment_tests); |
| 1683 | } |
| 1684 | |
| 1685 | if (glslangIntermediate->getPostDepthCoverage()) { |
| 1686 | builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1687 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1688 | builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1689 | } |
| 1690 | |
| 1691 | if (glslangIntermediate->getNonCoherentColorAttachmentReadEXT()) { |
| 1692 | builder.addCapability(spv::CapabilityTileImageColorReadAccessEXT); |
| 1693 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeNonCoherentColorAttachmentReadEXT); |
| 1694 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
| 1695 | } |
| 1696 | |
| 1697 | if (glslangIntermediate->getNonCoherentDepthAttachmentReadEXT()) { |
| 1698 | builder.addCapability(spv::CapabilityTileImageDepthReadAccessEXT); |
| 1699 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeNonCoherentDepthAttachmentReadEXT); |
| 1700 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
| 1701 | } |
| 1702 | |
| 1703 | if (glslangIntermediate->getNonCoherentStencilAttachmentReadEXT()) { |
| 1704 | builder.addCapability(spv::CapabilityTileImageStencilReadAccessEXT); |
| 1705 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeNonCoherentStencilAttachmentReadEXT); |
| 1706 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
| 1707 | } |
| 1708 | |
| 1709 | if (glslangIntermediate->isDepthReplacing()) |
| 1710 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
| 1711 | |
| 1712 | if (glslangIntermediate->isStencilReplacing()) |
| 1713 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeStencilRefReplacingEXT); |
| 1714 | |
| 1715 | #ifndef GLSLANG_WEB |
| 1716 | |
| 1717 | switch(glslangIntermediate->getDepth()) { |
| 1718 | case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1719 | case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
| 1720 | case glslang::EldUnchanged: mode = spv::ExecutionModeDepthUnchanged; break; |
| 1721 | default: mode = spv::ExecutionModeMax; break; |
| 1722 | } |
| 1723 | |
| 1724 | if (mode != spv::ExecutionModeMax) |
| 1725 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1726 | |
| 1727 | switch (glslangIntermediate->getStencil()) { |
| 1728 | case glslang::ElsRefUnchangedFrontAMD: mode = spv::ExecutionModeStencilRefUnchangedFrontAMD; break; |
| 1729 | case glslang::ElsRefGreaterFrontAMD: mode = spv::ExecutionModeStencilRefGreaterFrontAMD; break; |
| 1730 | case glslang::ElsRefLessFrontAMD: mode = spv::ExecutionModeStencilRefLessFrontAMD; break; |
| 1731 | case glslang::ElsRefUnchangedBackAMD: mode = spv::ExecutionModeStencilRefUnchangedBackAMD; break; |
| 1732 | case glslang::ElsRefGreaterBackAMD: mode = spv::ExecutionModeStencilRefGreaterBackAMD; break; |
| 1733 | case glslang::ElsRefLessBackAMD: mode = spv::ExecutionModeStencilRefLessBackAMD; break; |
| 1734 | default: mode = spv::ExecutionModeMax; break; |
| 1735 | } |
| 1736 | |
| 1737 | if (mode != spv::ExecutionModeMax) |
| 1738 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1739 | switch (glslangIntermediate->getInterlockOrdering()) { |
| 1740 | case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; |
| 1741 | break; |
| 1742 | case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; |
| 1743 | break; |
| 1744 | case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; |
| 1745 | break; |
| 1746 | case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; |
| 1747 | break; |
| 1748 | case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; |
| 1749 | break; |
| 1750 | case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; |
| 1751 | break; |
| 1752 | default: mode = spv::ExecutionModeMax; |
| 1753 | break; |
| 1754 | } |
| 1755 | if (mode != spv::ExecutionModeMax) { |
| 1756 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1757 | if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
| 1758 | mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
| 1759 | builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
| 1760 | } else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
| 1761 | mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
| 1762 | builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT); |
| 1763 | } else { |
| 1764 | builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT); |
| 1765 | } |
| 1766 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 1767 | } |
| 1768 | #endif |
| 1769 | break; |
| 1770 | |
| 1771 | case EShLangCompute: |
| 1772 | builder.addCapability(spv::CapabilityShader); |
| 1773 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
| 1774 | std::vector<spv::Id> dimConstId; |
| 1775 | for (int dim = 0; dim < 3; ++dim) { |
| 1776 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 1777 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 1778 | if (specConst) { |
| 1779 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 1780 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 1781 | } |
| 1782 | } |
| 1783 | builder.addExecutionModeId(shaderEntry, spv::ExecutionModeLocalSizeId, dimConstId); |
| 1784 | } else { |
| 1785 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1786 | glslangIntermediate->getLocalSize(1), |
| 1787 | glslangIntermediate->getLocalSize(2)); |
| 1788 | } |
| 1789 | if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1790 | builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1791 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1792 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1793 | } else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1794 | builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1795 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1796 | builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1797 | } |
| 1798 | break; |
| 1799 | #ifndef GLSLANG_WEB |
| 1800 | case EShLangTessEvaluation: |
| 1801 | case EShLangTessControl: |
| 1802 | builder.addCapability(spv::CapabilityTessellation); |
| 1803 | |
| 1804 | glslang::TLayoutGeometry primitive; |
| 1805 | |
| 1806 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1807 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1808 | glslangIntermediate->getVertices()); |
| 1809 | primitive = glslangIntermediate->getOutputPrimitive(); |
| 1810 | } else { |
| 1811 | primitive = glslangIntermediate->getInputPrimitive(); |
| 1812 | } |
| 1813 | |
| 1814 | switch (primitive) { |
| 1815 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1816 | case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1817 | case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
| 1818 | default: mode = spv::ExecutionModeMax; break; |
| 1819 | } |
| 1820 | if (mode != spv::ExecutionModeMax) |
| 1821 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1822 | |
| 1823 | switch (glslangIntermediate->getVertexSpacing()) { |
| 1824 | case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1825 | case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1826 | case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
| 1827 | default: mode = spv::ExecutionModeMax; break; |
| 1828 | } |
| 1829 | if (mode != spv::ExecutionModeMax) |
| 1830 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1831 | |
| 1832 | switch (glslangIntermediate->getVertexOrder()) { |
| 1833 | case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1834 | case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
| 1835 | default: mode = spv::ExecutionModeMax; break; |
| 1836 | } |
| 1837 | if (mode != spv::ExecutionModeMax) |
| 1838 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1839 | |
| 1840 | if (glslangIntermediate->getPointMode()) |
| 1841 | builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
| 1842 | break; |
| 1843 | |
| 1844 | case EShLangGeometry: |
| 1845 | builder.addCapability(spv::CapabilityGeometry); |
| 1846 | switch (glslangIntermediate->getInputPrimitive()) { |
| 1847 | case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1848 | case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1849 | case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
| 1850 | case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1851 | case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
| 1852 | default: mode = spv::ExecutionModeMax; break; |
| 1853 | } |
| 1854 | if (mode != spv::ExecutionModeMax) |
| 1855 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1856 | |
| 1857 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1858 | |
| 1859 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1860 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1861 | case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1862 | case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
| 1863 | default: mode = spv::ExecutionModeMax; break; |
| 1864 | } |
| 1865 | if (mode != spv::ExecutionModeMax) |
| 1866 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1867 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1868 | break; |
| 1869 | |
| 1870 | case EShLangRayGen: |
| 1871 | case EShLangIntersect: |
| 1872 | case EShLangAnyHit: |
| 1873 | case EShLangClosestHit: |
| 1874 | case EShLangMiss: |
| 1875 | case EShLangCallable: |
| 1876 | { |
| 1877 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 1878 | if (extensions.find("GL_NV_ray_tracing" ) == extensions.end()) { |
| 1879 | builder.addCapability(spv::CapabilityRayTracingKHR); |
| 1880 | builder.addExtension("SPV_KHR_ray_tracing" ); |
| 1881 | } |
| 1882 | else { |
| 1883 | builder.addCapability(spv::CapabilityRayTracingNV); |
| 1884 | builder.addExtension("SPV_NV_ray_tracing" ); |
| 1885 | } |
| 1886 | if (glslangIntermediate->getStage() != EShLangRayGen && glslangIntermediate->getStage() != EShLangCallable) { |
| 1887 | if (extensions.find("GL_EXT_ray_cull_mask" ) != extensions.end()) { |
| 1888 | builder.addCapability(spv::CapabilityRayCullMaskKHR); |
| 1889 | builder.addExtension("SPV_KHR_ray_cull_mask" ); |
| 1890 | } |
| 1891 | if (extensions.find("GL_EXT_ray_tracing_position_fetch" ) != extensions.end()) { |
| 1892 | builder.addCapability(spv::CapabilityRayTracingPositionFetchKHR); |
| 1893 | builder.addExtension("SPV_KHR_ray_tracing_position_fetch" ); |
| 1894 | } |
| 1895 | } |
| 1896 | break; |
| 1897 | } |
| 1898 | case EShLangTask: |
| 1899 | case EShLangMesh: |
| 1900 | if(isMeshShaderExt) { |
| 1901 | builder.addCapability(spv::CapabilityMeshShadingEXT); |
| 1902 | builder.addExtension(spv::E_SPV_EXT_mesh_shader); |
| 1903 | } else { |
| 1904 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1905 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1906 | } |
| 1907 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
| 1908 | std::vector<spv::Id> dimConstId; |
| 1909 | for (int dim = 0; dim < 3; ++dim) { |
| 1910 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 1911 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 1912 | if (specConst) { |
| 1913 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 1914 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 1915 | } |
| 1916 | } |
| 1917 | builder.addExecutionModeId(shaderEntry, spv::ExecutionModeLocalSizeId, dimConstId); |
| 1918 | } else { |
| 1919 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1920 | glslangIntermediate->getLocalSize(1), |
| 1921 | glslangIntermediate->getLocalSize(2)); |
| 1922 | } |
| 1923 | if (glslangIntermediate->getStage() == EShLangMesh) { |
| 1924 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1925 | glslangIntermediate->getVertices()); |
| 1926 | builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, |
| 1927 | glslangIntermediate->getPrimitives()); |
| 1928 | |
| 1929 | switch (glslangIntermediate->getOutputPrimitive()) { |
| 1930 | case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1931 | case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1932 | case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1933 | default: mode = spv::ExecutionModeMax; break; |
| 1934 | } |
| 1935 | if (mode != spv::ExecutionModeMax) |
| 1936 | builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1937 | } |
| 1938 | break; |
| 1939 | #endif |
| 1940 | |
| 1941 | default: |
| 1942 | break; |
| 1943 | } |
| 1944 | |
| 1945 | #ifndef GLSLANG_WEB |
| 1946 | // |
| 1947 | // Add SPIR-V requirements (GL_EXT_spirv_intrinsics) |
| 1948 | // |
| 1949 | if (glslangIntermediate->hasSpirvRequirement()) { |
| 1950 | const glslang::TSpirvRequirement& spirvRequirement = glslangIntermediate->getSpirvRequirement(); |
| 1951 | |
| 1952 | // Add SPIR-V extension requirement |
| 1953 | for (auto& extension : spirvRequirement.extensions) |
| 1954 | builder.addExtension(extension.c_str()); |
| 1955 | |
| 1956 | // Add SPIR-V capability requirement |
| 1957 | for (auto capability : spirvRequirement.capabilities) |
| 1958 | builder.addCapability(static_cast<spv::Capability>(capability)); |
| 1959 | } |
| 1960 | |
| 1961 | // |
| 1962 | // Add SPIR-V execution mode qualifiers (GL_EXT_spirv_intrinsics) |
| 1963 | // |
| 1964 | if (glslangIntermediate->hasSpirvExecutionMode()) { |
| 1965 | const glslang::TSpirvExecutionMode spirvExecutionMode = glslangIntermediate->getSpirvExecutionMode(); |
| 1966 | |
| 1967 | // Add spirv_execution_mode |
| 1968 | for (auto& mode : spirvExecutionMode.modes) { |
| 1969 | if (!mode.second.empty()) { |
| 1970 | std::vector<unsigned> literals; |
| 1971 | TranslateLiterals(mode.second, literals); |
| 1972 | builder.addExecutionMode(shaderEntry, static_cast<spv::ExecutionMode>(mode.first), literals); |
| 1973 | } else |
| 1974 | builder.addExecutionMode(shaderEntry, static_cast<spv::ExecutionMode>(mode.first)); |
| 1975 | } |
| 1976 | |
| 1977 | // Add spirv_execution_mode_id |
| 1978 | for (auto& modeId : spirvExecutionMode.modeIds) { |
| 1979 | std::vector<spv::Id> operandIds; |
| 1980 | assert(!modeId.second.empty()); |
| 1981 | for (auto extraOperand : modeId.second) { |
| 1982 | if (extraOperand->getType().getQualifier().isSpecConstant()) |
| 1983 | operandIds.push_back(getSymbolId(extraOperand->getAsSymbolNode())); |
| 1984 | else |
| 1985 | operandIds.push_back(createSpvConstant(*extraOperand)); |
| 1986 | } |
| 1987 | builder.addExecutionModeId(shaderEntry, static_cast<spv::ExecutionMode>(modeId.first), operandIds); |
| 1988 | } |
| 1989 | } |
| 1990 | #endif |
| 1991 | } |
| 1992 | |
| 1993 | // Finish creating SPV, after the traversal is complete. |
| 1994 | void TGlslangToSpvTraverser::finishSpv() |
| 1995 | { |
| 1996 | // Finish the entry point function |
| 1997 | if (! entryPointTerminated) { |
| 1998 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1999 | builder.leaveFunction(); |
| 2000 | } |
| 2001 | |
| 2002 | // finish off the entry-point SPV instruction by adding the Input/Output <id> |
| 2003 | for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 2004 | entryPoint->addIdOperand(*it); |
| 2005 | |
| 2006 | // Add capabilities, extensions, remove unneeded decorations, etc., |
| 2007 | // based on the resulting SPIR-V. |
| 2008 | // Note: WebGPU code generation must have the opportunity to aggressively |
| 2009 | // prune unreachable merge blocks and continue targets. |
| 2010 | builder.postProcess(); |
| 2011 | } |
| 2012 | |
| 2013 | // Write the SPV into 'out'. |
| 2014 | void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
| 2015 | { |
| 2016 | builder.dump(out); |
| 2017 | } |
| 2018 | |
| 2019 | // |
| 2020 | // Implement the traversal functions. |
| 2021 | // |
| 2022 | // Return true from interior nodes to have the external traversal |
| 2023 | // continue on to children. Return false if children were |
| 2024 | // already processed. |
| 2025 | // |
| 2026 | |
| 2027 | // |
| 2028 | // Symbols can turn into |
| 2029 | // - uniform/input reads |
| 2030 | // - output writes |
| 2031 | // - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 2032 | // - something simple that degenerates into the last bullet |
| 2033 | // |
| 2034 | void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 2035 | { |
| 2036 | // We update the line information even though no code might be generated here |
| 2037 | // This is helpful to yield correct lines for control flow instructions |
| 2038 | builder.setLine(symbol->getLoc().line, symbol->getLoc().getFilename()); |
| 2039 | |
| 2040 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2041 | if (symbol->getType().isStruct()) |
| 2042 | glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId(); |
| 2043 | |
| 2044 | if (symbol->getType().getQualifier().isSpecConstant()) |
| 2045 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2046 | #ifdef ENABLE_HLSL |
| 2047 | // Skip symbol handling if it is string-typed |
| 2048 | if (symbol->getBasicType() == glslang::EbtString) |
| 2049 | return; |
| 2050 | #endif |
| 2051 | |
| 2052 | // getSymbolId() will set up all the IO decorations on the first call. |
| 2053 | // Formal function parameters were mapped during makeFunctions(). |
| 2054 | spv::Id id = getSymbolId(symbol); |
| 2055 | |
| 2056 | if (symbol->getType().getQualifier().isTaskPayload()) |
| 2057 | taskPayloadID = id; // cache the taskPayloadID to be used it as operand for OpEmitMeshTasksEXT |
| 2058 | |
| 2059 | if (builder.isPointer(id)) { |
| 2060 | if (!symbol->getType().getQualifier().isParamInput() && |
| 2061 | !symbol->getType().getQualifier().isParamOutput()) { |
| 2062 | // Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 2063 | // Consider adding to the OpEntryPoint interface list. |
| 2064 | // Only looking at structures if they have at least one member. |
| 2065 | if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
| 2066 | spv::StorageClass sc = builder.getStorageClass(id); |
| 2067 | // Before SPIR-V 1.4, we only want to include Input and Output. |
| 2068 | // Starting with SPIR-V 1.4, we want all globals. |
| 2069 | if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(id)) || |
| 2070 | (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
| 2071 | iOSet.insert(id); |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | // If the SPIR-V type is required to be different than the AST type |
| 2077 | // (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices), |
| 2078 | // translate now from the SPIR-V type to the AST type, for the consuming |
| 2079 | // operation. |
| 2080 | // Note this turns it from an l-value to an r-value. |
| 2081 | // Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
| 2082 | if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
| 2083 | id = translateForcedType(id); |
| 2084 | } |
| 2085 | |
| 2086 | // Only process non-linkage-only nodes for generating actual static uses |
| 2087 | if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
| 2088 | // Prepare to generate code for the access |
| 2089 | |
| 2090 | // L-value chains will be computed left to right. We're on the symbol now, |
| 2091 | // which is the left-most part of the access chain, so now is "clear" time, |
| 2092 | // followed by setting the base. |
| 2093 | builder.clearAccessChain(); |
| 2094 | |
| 2095 | // For now, we consider all user variables as being in memory, so they are pointers, |
| 2096 | // except for |
| 2097 | // A) R-Value arguments to a function, which are an intermediate object. |
| 2098 | // See comments in handleUserFunctionCall(). |
| 2099 | // B) Specialization constants (normal constants don't even come in as a variable), |
| 2100 | // These are also pure R-values. |
| 2101 | // C) R-Values from type translation, see above call to translateForcedType() |
| 2102 | glslang::TQualifier qualifier = symbol->getQualifier(); |
| 2103 | if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
| 2104 | !builder.isPointerType(builder.getTypeId(id))) |
| 2105 | builder.setAccessChainRValue(id); |
| 2106 | else |
| 2107 | builder.setAccessChainLValue(id); |
| 2108 | } |
| 2109 | |
| 2110 | #ifdef ENABLE_HLSL |
| 2111 | // Process linkage-only nodes for any special additional interface work. |
| 2112 | if (linkageOnly) { |
| 2113 | if (glslangIntermediate->getHlslFunctionality1()) { |
| 2114 | // Map implicit counter buffers to their originating buffers, which should have been |
| 2115 | // seen by now, given earlier pruning of unused counters, and preservation of order |
| 2116 | // of declaration. |
| 2117 | if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 2118 | if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 2119 | // Save possible originating buffers for counter buffers, keyed by |
| 2120 | // making the potential counter-buffer name. |
| 2121 | std::string keyName = symbol->getName().c_str(); |
| 2122 | keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 2123 | counterOriginator[keyName] = symbol; |
| 2124 | } else { |
| 2125 | // Handle a counter buffer, by finding the saved originating buffer. |
| 2126 | std::string keyName = symbol->getName().c_str(); |
| 2127 | auto it = counterOriginator.find(keyName); |
| 2128 | if (it != counterOriginator.end()) { |
| 2129 | id = getSymbolId(it->second); |
| 2130 | if (id != spv::NoResult) { |
| 2131 | spv::Id counterId = getSymbolId(symbol); |
| 2132 | if (counterId != spv::NoResult) { |
| 2133 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1" ); |
| 2134 | builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | } |
| 2141 | } |
| 2142 | #endif |
| 2143 | } |
| 2144 | |
| 2145 | bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 2146 | { |
| 2147 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 2148 | if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) { |
| 2149 | glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId(); |
| 2150 | } |
| 2151 | if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) { |
| 2152 | glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId(); |
| 2153 | } |
| 2154 | |
| 2155 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2156 | if (node->getType().getQualifier().isSpecConstant()) |
| 2157 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2158 | |
| 2159 | // First, handle special cases |
| 2160 | switch (node->getOp()) { |
| 2161 | case glslang::EOpAssign: |
| 2162 | case glslang::EOpAddAssign: |
| 2163 | case glslang::EOpSubAssign: |
| 2164 | case glslang::EOpMulAssign: |
| 2165 | case glslang::EOpVectorTimesMatrixAssign: |
| 2166 | case glslang::EOpVectorTimesScalarAssign: |
| 2167 | case glslang::EOpMatrixTimesScalarAssign: |
| 2168 | case glslang::EOpMatrixTimesMatrixAssign: |
| 2169 | case glslang::EOpDivAssign: |
| 2170 | case glslang::EOpModAssign: |
| 2171 | case glslang::EOpAndAssign: |
| 2172 | case glslang::EOpInclusiveOrAssign: |
| 2173 | case glslang::EOpExclusiveOrAssign: |
| 2174 | case glslang::EOpLeftShiftAssign: |
| 2175 | case glslang::EOpRightShiftAssign: |
| 2176 | // A bin-op assign "a += b" means the same thing as "a = a + b" |
| 2177 | // where a is evaluated before b. For a simple assignment, GLSL |
| 2178 | // says to evaluate the left before the right. So, always, left |
| 2179 | // node then right node. |
| 2180 | { |
| 2181 | // get the left l-value, save it away |
| 2182 | builder.clearAccessChain(); |
| 2183 | node->getLeft()->traverse(this); |
| 2184 | spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 2185 | |
| 2186 | // evaluate the right |
| 2187 | builder.clearAccessChain(); |
| 2188 | node->getRight()->traverse(this); |
| 2189 | spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
| 2190 | |
| 2191 | // reset line number for assignment |
| 2192 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 2193 | |
| 2194 | if (node->getOp() != glslang::EOpAssign) { |
| 2195 | // the left is also an r-value |
| 2196 | builder.setAccessChain(lValue); |
| 2197 | spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
| 2198 | |
| 2199 | // do the operation |
| 2200 | spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(node->getLeft()->getType()); |
| 2201 | coherentFlags |= TranslateCoherent(node->getRight()->getType()); |
| 2202 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
| 2203 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2204 | TranslateNonUniformDecoration(coherentFlags) }; |
| 2205 | rValue = createBinaryOperation(node->getOp(), decorations, |
| 2206 | convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 2207 | node->getType().getBasicType()); |
| 2208 | |
| 2209 | // these all need their counterparts in createBinaryOperation() |
| 2210 | assert(rValue != spv::NoResult); |
| 2211 | } |
| 2212 | |
| 2213 | // store the result |
| 2214 | builder.setAccessChain(lValue); |
| 2215 | multiTypeStore(node->getLeft()->getType(), rValue); |
| 2216 | |
| 2217 | // assignments are expressions having an rValue after they are evaluated... |
| 2218 | builder.clearAccessChain(); |
| 2219 | builder.setAccessChainRValue(rValue); |
| 2220 | } |
| 2221 | return false; |
| 2222 | case glslang::EOpIndexDirect: |
| 2223 | case glslang::EOpIndexDirectStruct: |
| 2224 | { |
| 2225 | // Structure, array, matrix, or vector indirection with statically known index. |
| 2226 | // Get the left part of the access chain. |
| 2227 | node->getLeft()->traverse(this); |
| 2228 | |
| 2229 | // Add the next element in the chain |
| 2230 | |
| 2231 | const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
| 2232 | if (! node->getLeft()->getType().isArray() && |
| 2233 | node->getLeft()->getType().isVector() && |
| 2234 | node->getOp() == glslang::EOpIndexDirect) { |
| 2235 | // Swizzle is uniform so propagate uniform into access chain |
| 2236 | spv::Builder::AccessChain::CoherentFlags coherentFlags = TranslateCoherent(node->getLeft()->getType()); |
| 2237 | coherentFlags.nonUniform = 0; |
| 2238 | // This is essentially a hard-coded vector swizzle of size 1, |
| 2239 | // so short circuit the access-chain stuff with a swizzle. |
| 2240 | std::vector<unsigned> swizzle; |
| 2241 | swizzle.push_back(glslangIndex); |
| 2242 | int dummySize; |
| 2243 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 2244 | coherentFlags, |
| 2245 | glslangIntermediate->getBaseAlignmentScalar( |
| 2246 | node->getLeft()->getType(), dummySize)); |
| 2247 | } else { |
| 2248 | |
| 2249 | // Load through a block reference is performed with a dot operator that |
| 2250 | // is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 2251 | // do a load and reset the access chain. |
| 2252 | if (node->getLeft()->isReference() && |
| 2253 | !node->getLeft()->getType().isArray() && |
| 2254 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 2255 | { |
| 2256 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 2257 | builder.clearAccessChain(); |
| 2258 | builder.setAccessChainLValue(left); |
| 2259 | } |
| 2260 | |
| 2261 | int spvIndex = glslangIndex; |
| 2262 | if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 2263 | node->getOp() == glslang::EOpIndexDirectStruct) |
| 2264 | { |
| 2265 | // This may be, e.g., an anonymous block-member selection, which generally need |
| 2266 | // index remapping due to hidden members in anonymous blocks. |
| 2267 | long long glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()]; |
| 2268 | if (memberRemapper.find(glslangId) != memberRemapper.end()) { |
| 2269 | std::vector<int>& remapper = memberRemapper[glslangId]; |
| 2270 | assert(remapper.size() > 0); |
| 2271 | spvIndex = remapper[glslangIndex]; |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | // Struct reference propagates uniform lvalue |
| 2276 | spv::Builder::AccessChain::CoherentFlags coherentFlags = |
| 2277 | TranslateCoherent(node->getLeft()->getType()); |
| 2278 | coherentFlags.nonUniform = 0; |
| 2279 | |
| 2280 | // normal case for indexing array or structure or block |
| 2281 | builder.accessChainPush(builder.makeIntConstant(spvIndex), |
| 2282 | coherentFlags, |
| 2283 | node->getLeft()->getType().getBufferReferenceAlignment()); |
| 2284 | |
| 2285 | // Add capabilities here for accessing PointSize and clip/cull distance. |
| 2286 | // We have deferred generation of associated capabilities until now. |
| 2287 | if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
| 2288 | declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
| 2289 | } |
| 2290 | } |
| 2291 | return false; |
| 2292 | case glslang::EOpIndexIndirect: |
| 2293 | { |
| 2294 | // Array, matrix, or vector indirection with variable index. |
| 2295 | // Will use native SPIR-V access-chain for and array indirection; |
| 2296 | // matrices are arrays of vectors, so will also work for a matrix. |
| 2297 | // Will use the access chain's 'component' for variable index into a vector. |
| 2298 | |
| 2299 | // This adapter is building access chains left to right. |
| 2300 | // Set up the access chain to the left. |
| 2301 | node->getLeft()->traverse(this); |
| 2302 | |
| 2303 | // save it so that computing the right side doesn't trash it |
| 2304 | spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 2305 | |
| 2306 | // compute the next index in the chain |
| 2307 | builder.clearAccessChain(); |
| 2308 | node->getRight()->traverse(this); |
| 2309 | spv::Id index = accessChainLoad(node->getRight()->getType()); |
| 2310 | |
| 2311 | addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 2312 | |
| 2313 | // restore the saved access chain |
| 2314 | builder.setAccessChain(partial); |
| 2315 | |
| 2316 | // Only if index is nonUniform should we propagate nonUniform into access chain |
| 2317 | spv::Builder::AccessChain::CoherentFlags index_flags = TranslateCoherent(node->getRight()->getType()); |
| 2318 | spv::Builder::AccessChain::CoherentFlags coherent_flags = TranslateCoherent(node->getLeft()->getType()); |
| 2319 | coherent_flags.nonUniform = index_flags.nonUniform; |
| 2320 | |
| 2321 | if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 2322 | int dummySize; |
| 2323 | builder.accessChainPushComponent( |
| 2324 | index, convertGlslangToSpvType(node->getLeft()->getType()), coherent_flags, |
| 2325 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 2326 | dummySize)); |
| 2327 | } else |
| 2328 | builder.accessChainPush(index, coherent_flags, |
| 2329 | node->getLeft()->getType().getBufferReferenceAlignment()); |
| 2330 | } |
| 2331 | return false; |
| 2332 | case glslang::EOpVectorSwizzle: |
| 2333 | { |
| 2334 | node->getLeft()->traverse(this); |
| 2335 | std::vector<unsigned> swizzle; |
| 2336 | convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
| 2337 | int dummySize; |
| 2338 | builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 2339 | TranslateCoherent(node->getLeft()->getType()), |
| 2340 | glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 2341 | dummySize)); |
| 2342 | } |
| 2343 | return false; |
| 2344 | case glslang::EOpMatrixSwizzle: |
| 2345 | logger->missingFunctionality("matrix swizzle" ); |
| 2346 | return true; |
| 2347 | case glslang::EOpLogicalOr: |
| 2348 | case glslang::EOpLogicalAnd: |
| 2349 | { |
| 2350 | |
| 2351 | // These may require short circuiting, but can sometimes be done as straight |
| 2352 | // binary operations. The right operand must be short circuited if it has |
| 2353 | // side effects, and should probably be if it is complex. |
| 2354 | if (isTrivial(node->getRight()->getAsTyped())) |
| 2355 | break; // handle below as a normal binary operation |
| 2356 | // otherwise, we need to do dynamic short circuiting on the right operand |
| 2357 | spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), |
| 2358 | *node->getRight()->getAsTyped()); |
| 2359 | builder.clearAccessChain(); |
| 2360 | builder.setAccessChainRValue(result); |
| 2361 | } |
| 2362 | return false; |
| 2363 | default: |
| 2364 | break; |
| 2365 | } |
| 2366 | |
| 2367 | // Assume generic binary op... |
| 2368 | |
| 2369 | // get right operand |
| 2370 | builder.clearAccessChain(); |
| 2371 | node->getLeft()->traverse(this); |
| 2372 | spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 2373 | |
| 2374 | // get left operand |
| 2375 | builder.clearAccessChain(); |
| 2376 | node->getRight()->traverse(this); |
| 2377 | spv::Id right = accessChainLoad(node->getRight()->getType()); |
| 2378 | |
| 2379 | // get result |
| 2380 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
| 2381 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2382 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 2383 | spv::Id result = createBinaryOperation(node->getOp(), decorations, |
| 2384 | convertGlslangToSpvType(node->getType()), left, right, |
| 2385 | node->getLeft()->getType().getBasicType()); |
| 2386 | |
| 2387 | builder.clearAccessChain(); |
| 2388 | if (! result) { |
| 2389 | logger->missingFunctionality("unknown glslang binary operation" ); |
| 2390 | return true; // pick up a child as the place-holder result |
| 2391 | } else { |
| 2392 | builder.setAccessChainRValue(result); |
| 2393 | return false; |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | spv::Id TGlslangToSpvTraverser::convertLoadedBoolInUniformToUint(const glslang::TType& type, |
| 2398 | spv::Id nominalTypeId, |
| 2399 | spv::Id loadedId) |
| 2400 | { |
| 2401 | if (builder.isScalarType(nominalTypeId)) { |
| 2402 | // Conversion for bool |
| 2403 | spv::Id boolType = builder.makeBoolType(); |
| 2404 | if (nominalTypeId != boolType) |
| 2405 | return builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 2406 | } else if (builder.isVectorType(nominalTypeId)) { |
| 2407 | // Conversion for bvec |
| 2408 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 2409 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 2410 | if (nominalTypeId != bvecType) |
| 2411 | loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, |
| 2412 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 2413 | } else if (builder.isArrayType(nominalTypeId)) { |
| 2414 | // Conversion for bool array |
| 2415 | spv::Id boolArrayTypeId = convertGlslangToSpvType(type); |
| 2416 | if (nominalTypeId != boolArrayTypeId) |
| 2417 | { |
| 2418 | // Use OpCopyLogical from SPIR-V 1.4 if available. |
| 2419 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) |
| 2420 | return builder.createUnaryOp(spv::OpCopyLogical, boolArrayTypeId, loadedId); |
| 2421 | |
| 2422 | glslang::TType glslangElementType(type, 0); |
| 2423 | spv::Id elementNominalTypeId = builder.getContainedTypeId(nominalTypeId); |
| 2424 | std::vector<spv::Id> constituents; |
| 2425 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 2426 | // get the element |
| 2427 | spv::Id elementValue = builder.createCompositeExtract(loadedId, elementNominalTypeId, index); |
| 2428 | |
| 2429 | // recursively convert it |
| 2430 | spv::Id elementConvertedValue = convertLoadedBoolInUniformToUint(glslangElementType, elementNominalTypeId, elementValue); |
| 2431 | constituents.push_back(elementConvertedValue); |
| 2432 | } |
| 2433 | return builder.createCompositeConstruct(boolArrayTypeId, constituents); |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | return loadedId; |
| 2438 | } |
| 2439 | |
| 2440 | // Figure out what, if any, type changes are needed when accessing a specific built-in. |
| 2441 | // Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
| 2442 | // Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
| 2443 | std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn, |
| 2444 | const glslang::TType& glslangType) |
| 2445 | { |
| 2446 | switch(glslangBuiltIn) |
| 2447 | { |
| 2448 | case glslang::EbvSubGroupEqMask: |
| 2449 | case glslang::EbvSubGroupGeMask: |
| 2450 | case glslang::EbvSubGroupGtMask: |
| 2451 | case glslang::EbvSubGroupLeMask: |
| 2452 | case glslang::EbvSubGroupLtMask: { |
| 2453 | // these require changing a 64-bit scaler -> a vector of 32-bit components |
| 2454 | if (glslangType.isVector()) |
| 2455 | break; |
| 2456 | spv::Id ivec4_type = builder.makeVectorType(builder.makeUintType(32), 4); |
| 2457 | spv::Id uint64_type = builder.makeUintType(64); |
| 2458 | std::pair<spv::Id, spv::Id> ret(ivec4_type, uint64_type); |
| 2459 | return ret; |
| 2460 | } |
| 2461 | // There are no SPIR-V builtins defined for these and map onto original non-transposed |
| 2462 | // builtins. During visitBinary we insert a transpose |
| 2463 | case glslang::EbvWorldToObject3x4: |
| 2464 | case glslang::EbvObjectToWorld3x4: { |
| 2465 | spv::Id mat43 = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 2466 | spv::Id mat34 = builder.makeMatrixType(builder.makeFloatType(32), 3, 4); |
| 2467 | std::pair<spv::Id, spv::Id> ret(mat43, mat34); |
| 2468 | return ret; |
| 2469 | } |
| 2470 | default: |
| 2471 | break; |
| 2472 | } |
| 2473 | |
| 2474 | std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
| 2475 | return ret; |
| 2476 | } |
| 2477 | |
| 2478 | // For an object previously identified (see getForcedType() and forceType) |
| 2479 | // as needing type translations, do the translation needed for a load, turning |
| 2480 | // an L-value into in R-value. |
| 2481 | spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
| 2482 | { |
| 2483 | const auto forceIt = forceType.find(object); |
| 2484 | if (forceIt == forceType.end()) |
| 2485 | return object; |
| 2486 | |
| 2487 | spv::Id desiredTypeId = forceIt->second; |
| 2488 | spv::Id objectTypeId = builder.getTypeId(object); |
| 2489 | assert(builder.isPointerType(objectTypeId)); |
| 2490 | objectTypeId = builder.getContainedTypeId(objectTypeId); |
| 2491 | if (builder.isVectorType(objectTypeId) && |
| 2492 | builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
| 2493 | if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
| 2494 | // handle 32-bit v.xy* -> 64-bit |
| 2495 | builder.clearAccessChain(); |
| 2496 | builder.setAccessChainLValue(object); |
| 2497 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, spv::DecorationMax, objectTypeId); |
| 2498 | std::vector<spv::Id> components; |
| 2499 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
| 2500 | components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
| 2501 | |
| 2502 | spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
| 2503 | return builder.createUnaryOp(spv::OpBitcast, desiredTypeId, |
| 2504 | builder.createCompositeConstruct(vecType, components)); |
| 2505 | } else { |
| 2506 | logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar" ); |
| 2507 | } |
| 2508 | } else if (builder.isMatrixType(objectTypeId)) { |
| 2509 | // There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject |
| 2510 | // and we insert a transpose after loading the original non-transposed builtins |
| 2511 | builder.clearAccessChain(); |
| 2512 | builder.setAccessChainLValue(object); |
| 2513 | object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, spv::DecorationMax, objectTypeId); |
| 2514 | return builder.createUnaryOp(spv::OpTranspose, desiredTypeId, object); |
| 2515 | |
| 2516 | } else { |
| 2517 | logger->missingFunctionality("forcing non 32-bit vector type" ); |
| 2518 | } |
| 2519 | |
| 2520 | return object; |
| 2521 | } |
| 2522 | |
| 2523 | bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 2524 | { |
| 2525 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 2526 | |
| 2527 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2528 | if (node->getType().getQualifier().isSpecConstant()) |
| 2529 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2530 | |
| 2531 | spv::Id result = spv::NoResult; |
| 2532 | |
| 2533 | // try texturing first |
| 2534 | result = createImageTextureFunctionCall(node); |
| 2535 | if (result != spv::NoResult) { |
| 2536 | builder.clearAccessChain(); |
| 2537 | builder.setAccessChainRValue(result); |
| 2538 | |
| 2539 | return false; // done with this node |
| 2540 | } |
| 2541 | |
| 2542 | // Non-texturing. |
| 2543 | |
| 2544 | if (node->getOp() == glslang::EOpArrayLength) { |
| 2545 | // Quite special; won't want to evaluate the operand. |
| 2546 | |
| 2547 | // Currently, the front-end does not allow .length() on an array until it is sized, |
| 2548 | // except for the last block membeor of an SSBO. |
| 2549 | // TODO: If this changes, link-time sized arrays might show up here, and need their |
| 2550 | // size extracted. |
| 2551 | |
| 2552 | // Normal .length() would have been constant folded by the front-end. |
| 2553 | // So, this has to be block.lastMember.length(). |
| 2554 | // SPV wants "block" and member number as the operands, go get them. |
| 2555 | |
| 2556 | spv::Id length; |
| 2557 | if (node->getOperand()->getType().isCoopMat()) { |
| 2558 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2559 | |
| 2560 | spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
| 2561 | assert(builder.isCooperativeMatrixType(typeId)); |
| 2562 | |
| 2563 | length = builder.createCooperativeMatrixLength(typeId); |
| 2564 | } else { |
| 2565 | glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2566 | block->traverse(this); |
| 2567 | unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion() |
| 2568 | ->getConstArray()[0].getUConst(); |
| 2569 | length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
| 2570 | } |
| 2571 | |
| 2572 | // GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 2573 | // signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 2574 | // AST expectation of a signed result. |
| 2575 | if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
| 2576 | if (builder.isInSpecConstCodeGenMode()) { |
| 2577 | length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0)); |
| 2578 | } else { |
| 2579 | length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | builder.clearAccessChain(); |
| 2584 | builder.setAccessChainRValue(length); |
| 2585 | |
| 2586 | return false; |
| 2587 | } |
| 2588 | |
| 2589 | // Force variable declaration - Debug Mode Only |
| 2590 | if (node->getOp() == glslang::EOpDeclare) { |
| 2591 | builder.clearAccessChain(); |
| 2592 | node->getOperand()->traverse(this); |
| 2593 | builder.clearAccessChain(); |
| 2594 | return false; |
| 2595 | } |
| 2596 | |
| 2597 | // Start by evaluating the operand |
| 2598 | |
| 2599 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2600 | // operate first on the swizzle base, then apply the swizzle. |
| 2601 | spv::Id invertedType = spv::NoType; |
| 2602 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2603 | invertedType : convertGlslangToSpvType(node->getType()); }; |
| 2604 | if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 2605 | invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 2606 | |
| 2607 | builder.clearAccessChain(); |
| 2608 | TIntermNode *operandNode; |
| 2609 | if (invertedType != spv::NoType) |
| 2610 | operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2611 | else |
| 2612 | operandNode = node->getOperand(); |
| 2613 | |
| 2614 | operandNode->traverse(this); |
| 2615 | |
| 2616 | spv::Id operand = spv::NoResult; |
| 2617 | |
| 2618 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2619 | |
| 2620 | const auto hitObjectOpsWithLvalue = [](glslang::TOperator op) { |
| 2621 | switch(op) { |
| 2622 | case glslang::EOpReorderThreadNV: |
| 2623 | case glslang::EOpHitObjectGetCurrentTimeNV: |
| 2624 | case glslang::EOpHitObjectGetHitKindNV: |
| 2625 | case glslang::EOpHitObjectGetPrimitiveIndexNV: |
| 2626 | case glslang::EOpHitObjectGetGeometryIndexNV: |
| 2627 | case glslang::EOpHitObjectGetInstanceIdNV: |
| 2628 | case glslang::EOpHitObjectGetInstanceCustomIndexNV: |
| 2629 | case glslang::EOpHitObjectGetObjectRayDirectionNV: |
| 2630 | case glslang::EOpHitObjectGetObjectRayOriginNV: |
| 2631 | case glslang::EOpHitObjectGetWorldRayDirectionNV: |
| 2632 | case glslang::EOpHitObjectGetWorldRayOriginNV: |
| 2633 | case glslang::EOpHitObjectGetWorldToObjectNV: |
| 2634 | case glslang::EOpHitObjectGetObjectToWorldNV: |
| 2635 | case glslang::EOpHitObjectGetRayTMaxNV: |
| 2636 | case glslang::EOpHitObjectGetRayTMinNV: |
| 2637 | case glslang::EOpHitObjectIsEmptyNV: |
| 2638 | case glslang::EOpHitObjectIsHitNV: |
| 2639 | case glslang::EOpHitObjectIsMissNV: |
| 2640 | case glslang::EOpHitObjectRecordEmptyNV: |
| 2641 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexNV: |
| 2642 | case glslang::EOpHitObjectGetShaderRecordBufferHandleNV: |
| 2643 | return true; |
| 2644 | default: |
| 2645 | return false; |
| 2646 | } |
| 2647 | }; |
| 2648 | |
| 2649 | #ifndef GLSLANG_WEB |
| 2650 | if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 2651 | node->getOp() == glslang::EOpAtomicCounterDecrement || |
| 2652 | node->getOp() == glslang::EOpAtomicCounter || |
| 2653 | (node->getOp() == glslang::EOpInterpolateAtCentroid && |
| 2654 | glslangIntermediate->getSource() != glslang::EShSourceHlsl) || |
| 2655 | node->getOp() == glslang::EOpRayQueryProceed || |
| 2656 | node->getOp() == glslang::EOpRayQueryGetRayTMin || |
| 2657 | node->getOp() == glslang::EOpRayQueryGetRayFlags || |
| 2658 | node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin || |
| 2659 | node->getOp() == glslang::EOpRayQueryGetWorldRayDirection || |
| 2660 | node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque || |
| 2661 | node->getOp() == glslang::EOpRayQueryTerminate || |
| 2662 | node->getOp() == glslang::EOpRayQueryConfirmIntersection || |
| 2663 | (node->getOp() == glslang::EOpSpirvInst && operandNode->getAsTyped()->getQualifier().isSpirvByReference()) || |
| 2664 | hitObjectOpsWithLvalue(node->getOp())) { |
| 2665 | operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 2666 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2667 | lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
| 2668 | } else if (operandNode->getAsTyped()->getQualifier().isSpirvLiteral()) { |
| 2669 | // Will be translated to a literal value, make a placeholder here |
| 2670 | operand = spv::NoResult; |
| 2671 | } else |
| 2672 | #endif |
| 2673 | { |
| 2674 | operand = accessChainLoad(node->getOperand()->getType()); |
| 2675 | } |
| 2676 | |
| 2677 | OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
| 2678 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2679 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 2680 | |
| 2681 | // it could be a conversion |
| 2682 | if (! result) |
| 2683 | result = createConversion(node->getOp(), decorations, resultType(), operand, |
| 2684 | node->getOperand()->getBasicType()); |
| 2685 | |
| 2686 | // if not, then possibly an operation |
| 2687 | if (! result) |
| 2688 | result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, |
| 2689 | node->getOperand()->getBasicType(), lvalueCoherentFlags); |
| 2690 | |
| 2691 | #ifndef GLSLANG_WEB |
| 2692 | // it could be attached to a SPIR-V intruction |
| 2693 | if (!result) { |
| 2694 | if (node->getOp() == glslang::EOpSpirvInst) { |
| 2695 | const auto& spirvInst = node->getSpirvInstruction(); |
| 2696 | if (spirvInst.set == "" ) { |
| 2697 | spv::IdImmediate idImmOp = {true, operand}; |
| 2698 | if (operandNode->getAsTyped()->getQualifier().isSpirvLiteral()) { |
| 2699 | // Translate the constant to a literal value |
| 2700 | std::vector<unsigned> literals; |
| 2701 | glslang::TVector<const glslang::TIntermConstantUnion*> constants; |
| 2702 | constants.push_back(operandNode->getAsConstantUnion()); |
| 2703 | TranslateLiterals(constants, literals); |
| 2704 | idImmOp = {false, literals[0]}; |
| 2705 | } |
| 2706 | |
| 2707 | if (node->getBasicType() == glslang::EbtVoid) |
| 2708 | builder.createNoResultOp(static_cast<spv::Op>(spirvInst.id), {idImmOp}); |
| 2709 | else |
| 2710 | result = builder.createOp(static_cast<spv::Op>(spirvInst.id), resultType(), {idImmOp}); |
| 2711 | } else { |
| 2712 | result = builder.createBuiltinCall( |
| 2713 | resultType(), spirvInst.set == "GLSL.std.450" ? stdBuiltins : getExtBuiltins(spirvInst.set.c_str()), |
| 2714 | spirvInst.id, {operand}); |
| 2715 | } |
| 2716 | |
| 2717 | if (node->getBasicType() == glslang::EbtVoid) |
| 2718 | return false; // done with this node |
| 2719 | } |
| 2720 | } |
| 2721 | #endif |
| 2722 | |
| 2723 | if (result) { |
| 2724 | if (invertedType) { |
| 2725 | result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
| 2726 | decorations.addNonUniform(builder, result); |
| 2727 | } |
| 2728 | |
| 2729 | builder.clearAccessChain(); |
| 2730 | builder.setAccessChainRValue(result); |
| 2731 | |
| 2732 | return false; // done with this node |
| 2733 | } |
| 2734 | |
| 2735 | // it must be a special case, check... |
| 2736 | switch (node->getOp()) { |
| 2737 | case glslang::EOpPostIncrement: |
| 2738 | case glslang::EOpPostDecrement: |
| 2739 | case glslang::EOpPreIncrement: |
| 2740 | case glslang::EOpPreDecrement: |
| 2741 | { |
| 2742 | // we need the integer value "1" or the floating point "1.0" to add/subtract |
| 2743 | spv::Id one = 0; |
| 2744 | if (node->getBasicType() == glslang::EbtFloat) |
| 2745 | one = builder.makeFloatConstant(1.0F); |
| 2746 | #ifndef GLSLANG_WEB |
| 2747 | else if (node->getBasicType() == glslang::EbtDouble) |
| 2748 | one = builder.makeDoubleConstant(1.0); |
| 2749 | else if (node->getBasicType() == glslang::EbtFloat16) |
| 2750 | one = builder.makeFloat16Constant(1.0F); |
| 2751 | else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 2752 | one = builder.makeInt8Constant(1); |
| 2753 | else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 2754 | one = builder.makeInt16Constant(1); |
| 2755 | else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 2756 | one = builder.makeInt64Constant(1); |
| 2757 | #endif |
| 2758 | else |
| 2759 | one = builder.makeIntConstant(1); |
| 2760 | glslang::TOperator op; |
| 2761 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2762 | node->getOp() == glslang::EOpPostIncrement) |
| 2763 | op = glslang::EOpAdd; |
| 2764 | else |
| 2765 | op = glslang::EOpSub; |
| 2766 | |
| 2767 | spv::Id result = createBinaryOperation(op, decorations, |
| 2768 | convertGlslangToSpvType(node->getType()), operand, one, |
| 2769 | node->getType().getBasicType()); |
| 2770 | assert(result != spv::NoResult); |
| 2771 | |
| 2772 | // The result of operation is always stored, but conditionally the |
| 2773 | // consumed result. The consumed result is always an r-value. |
| 2774 | builder.accessChainStore(result, |
| 2775 | TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags)); |
| 2776 | builder.clearAccessChain(); |
| 2777 | if (node->getOp() == glslang::EOpPreIncrement || |
| 2778 | node->getOp() == glslang::EOpPreDecrement) |
| 2779 | builder.setAccessChainRValue(result); |
| 2780 | else |
| 2781 | builder.setAccessChainRValue(operand); |
| 2782 | } |
| 2783 | |
| 2784 | return false; |
| 2785 | |
| 2786 | #ifndef GLSLANG_WEB |
| 2787 | case glslang::EOpEmitStreamVertex: |
| 2788 | builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 2789 | return false; |
| 2790 | case glslang::EOpEndStreamPrimitive: |
| 2791 | builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 2792 | return false; |
| 2793 | case glslang::EOpRayQueryTerminate: |
| 2794 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operand); |
| 2795 | return false; |
| 2796 | case glslang::EOpRayQueryConfirmIntersection: |
| 2797 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operand); |
| 2798 | return false; |
| 2799 | case glslang::EOpReorderThreadNV: |
| 2800 | builder.createNoResultOp(spv::OpReorderThreadWithHitObjectNV, operand); |
| 2801 | return false; |
| 2802 | case glslang::EOpHitObjectRecordEmptyNV: |
| 2803 | builder.createNoResultOp(spv::OpHitObjectRecordEmptyNV, operand); |
| 2804 | return false; |
| 2805 | #endif |
| 2806 | |
| 2807 | default: |
| 2808 | logger->missingFunctionality("unknown glslang unary" ); |
| 2809 | return true; // pick up operand as placeholder result |
| 2810 | } |
| 2811 | } |
| 2812 | |
| 2813 | // Construct a composite object, recursively copying members if their types don't match |
| 2814 | spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
| 2815 | { |
| 2816 | for (int c = 0; c < (int)constituents.size(); ++c) { |
| 2817 | spv::Id& constituent = constituents[c]; |
| 2818 | spv::Id lType = builder.getContainedTypeId(resultTypeId, c); |
| 2819 | spv::Id rType = builder.getTypeId(constituent); |
| 2820 | if (lType != rType) { |
| 2821 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 2822 | constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent); |
| 2823 | } else if (builder.isStructType(rType)) { |
| 2824 | std::vector<spv::Id> rTypeConstituents; |
| 2825 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2826 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2827 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, |
| 2828 | builder.getContainedTypeId(rType, i), i)); |
| 2829 | } |
| 2830 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2831 | } else { |
| 2832 | assert(builder.isArrayType(rType)); |
| 2833 | std::vector<spv::Id> rTypeConstituents; |
| 2834 | int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2835 | |
| 2836 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2837 | for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2838 | rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i)); |
| 2839 | } |
| 2840 | constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2841 | } |
| 2842 | } |
| 2843 | } |
| 2844 | return builder.createCompositeConstruct(resultTypeId, constituents); |
| 2845 | } |
| 2846 | |
| 2847 | bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2848 | { |
| 2849 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2850 | if (node->getType().getQualifier().isSpecConstant()) |
| 2851 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2852 | |
| 2853 | spv::Id result = spv::NoResult; |
| 2854 | spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2855 | std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for |
| 2856 | // SPIR-V, for an out parameter |
| 2857 | std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues |
| 2858 | |
| 2859 | auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2860 | invertedType : |
| 2861 | convertGlslangToSpvType(node->getType()); }; |
| 2862 | |
| 2863 | // try texturing |
| 2864 | result = createImageTextureFunctionCall(node); |
| 2865 | if (result != spv::NoResult) { |
| 2866 | builder.clearAccessChain(); |
| 2867 | builder.setAccessChainRValue(result); |
| 2868 | |
| 2869 | return false; |
| 2870 | } |
| 2871 | #ifndef GLSLANG_WEB |
| 2872 | else if (node->getOp() == glslang::EOpImageStore || |
| 2873 | node->getOp() == glslang::EOpImageStoreLod || |
| 2874 | node->getOp() == glslang::EOpImageAtomicStore) { |
| 2875 | // "imageStore" is a special case, which has no result |
| 2876 | return false; |
| 2877 | } |
| 2878 | #endif |
| 2879 | |
| 2880 | glslang::TOperator binOp = glslang::EOpNull; |
| 2881 | bool reduceComparison = true; |
| 2882 | bool isMatrix = false; |
| 2883 | bool noReturnValue = false; |
| 2884 | bool atomic = false; |
| 2885 | |
| 2886 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2887 | |
| 2888 | assert(node->getOp()); |
| 2889 | |
| 2890 | spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
| 2891 | |
| 2892 | switch (node->getOp()) { |
| 2893 | case glslang::EOpScope: |
| 2894 | case glslang::EOpSequence: |
| 2895 | { |
| 2896 | if (visit == glslang::EvPreVisit) { |
| 2897 | ++sequenceDepth; |
| 2898 | if (sequenceDepth == 1) { |
| 2899 | // If this is the parent node of all the functions, we want to see them |
| 2900 | // early, so all call points have actual SPIR-V functions to reference. |
| 2901 | // In all cases, still let the traverser visit the children for us. |
| 2902 | makeFunctions(node->getAsAggregate()->getSequence()); |
| 2903 | |
| 2904 | // Also, we want all globals initializers to go into the beginning of the entry point, before |
| 2905 | // anything else gets there, so visit out of order, doing them all now. |
| 2906 | makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2907 | |
| 2908 | //Pre process linker objects for ray tracing stages |
| 2909 | if (glslangIntermediate->isRayTracingStage()) |
| 2910 | collectRayTracingLinkerObjects(); |
| 2911 | |
| 2912 | // Initializers are done, don't want to visit again, but functions and link objects need to be processed, |
| 2913 | // so do them manually. |
| 2914 | visitFunctions(node->getAsAggregate()->getSequence()); |
| 2915 | |
| 2916 | return false; |
| 2917 | } else { |
| 2918 | if (node->getOp() == glslang::EOpScope) |
| 2919 | builder.enterScope(0); |
| 2920 | } |
| 2921 | } else { |
| 2922 | if (sequenceDepth > 1 && node->getOp() == glslang::EOpScope) |
| 2923 | builder.leaveScope(); |
| 2924 | --sequenceDepth; |
| 2925 | } |
| 2926 | |
| 2927 | return true; |
| 2928 | } |
| 2929 | case glslang::EOpLinkerObjects: |
| 2930 | { |
| 2931 | if (visit == glslang::EvPreVisit) |
| 2932 | linkageOnly = true; |
| 2933 | else |
| 2934 | linkageOnly = false; |
| 2935 | |
| 2936 | return true; |
| 2937 | } |
| 2938 | case glslang::EOpComma: |
| 2939 | { |
| 2940 | // processing from left to right naturally leaves the right-most |
| 2941 | // lying around in the access chain |
| 2942 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2943 | for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2944 | glslangOperands[i]->traverse(this); |
| 2945 | |
| 2946 | return false; |
| 2947 | } |
| 2948 | case glslang::EOpFunction: |
| 2949 | if (visit == glslang::EvPreVisit) { |
| 2950 | if (isShaderEntryPoint(node)) { |
| 2951 | inEntryPoint = true; |
| 2952 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 2953 | builder.enterFunction(shaderEntry); |
| 2954 | currentFunction = shaderEntry; |
| 2955 | } else { |
| 2956 | handleFunctionEntry(node); |
| 2957 | } |
| 2958 | if (options.generateDebugInfo) { |
| 2959 | const auto& loc = node->getLoc(); |
| 2960 | const char* sourceFileName = loc.getFilename(); |
| 2961 | spv::Id sourceFileId = sourceFileName ? builder.getStringId(sourceFileName) : builder.getSourceFile(); |
| 2962 | currentFunction->setDebugLineInfo(sourceFileId, loc.line, loc.column); |
| 2963 | } |
| 2964 | } else { |
| 2965 | if (inEntryPoint) |
| 2966 | entryPointTerminated = true; |
| 2967 | builder.leaveFunction(); |
| 2968 | inEntryPoint = false; |
| 2969 | } |
| 2970 | |
| 2971 | return true; |
| 2972 | case glslang::EOpParameters: |
| 2973 | // Parameters will have been consumed by EOpFunction processing, but not |
| 2974 | // the body, so we still visited the function node's children, making this |
| 2975 | // child redundant. |
| 2976 | return false; |
| 2977 | case glslang::EOpFunctionCall: |
| 2978 | { |
| 2979 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 2980 | if (node->isUserDefined()) |
| 2981 | result = handleUserFunctionCall(node); |
| 2982 | if (result) { |
| 2983 | builder.clearAccessChain(); |
| 2984 | builder.setAccessChainRValue(result); |
| 2985 | } else |
| 2986 | logger->missingFunctionality("missing user function; linker needs to catch that" ); |
| 2987 | |
| 2988 | return false; |
| 2989 | } |
| 2990 | case glslang::EOpConstructMat2x2: |
| 2991 | case glslang::EOpConstructMat2x3: |
| 2992 | case glslang::EOpConstructMat2x4: |
| 2993 | case glslang::EOpConstructMat3x2: |
| 2994 | case glslang::EOpConstructMat3x3: |
| 2995 | case glslang::EOpConstructMat3x4: |
| 2996 | case glslang::EOpConstructMat4x2: |
| 2997 | case glslang::EOpConstructMat4x3: |
| 2998 | case glslang::EOpConstructMat4x4: |
| 2999 | case glslang::EOpConstructDMat2x2: |
| 3000 | case glslang::EOpConstructDMat2x3: |
| 3001 | case glslang::EOpConstructDMat2x4: |
| 3002 | case glslang::EOpConstructDMat3x2: |
| 3003 | case glslang::EOpConstructDMat3x3: |
| 3004 | case glslang::EOpConstructDMat3x4: |
| 3005 | case glslang::EOpConstructDMat4x2: |
| 3006 | case glslang::EOpConstructDMat4x3: |
| 3007 | case glslang::EOpConstructDMat4x4: |
| 3008 | case glslang::EOpConstructIMat2x2: |
| 3009 | case glslang::EOpConstructIMat2x3: |
| 3010 | case glslang::EOpConstructIMat2x4: |
| 3011 | case glslang::EOpConstructIMat3x2: |
| 3012 | case glslang::EOpConstructIMat3x3: |
| 3013 | case glslang::EOpConstructIMat3x4: |
| 3014 | case glslang::EOpConstructIMat4x2: |
| 3015 | case glslang::EOpConstructIMat4x3: |
| 3016 | case glslang::EOpConstructIMat4x4: |
| 3017 | case glslang::EOpConstructUMat2x2: |
| 3018 | case glslang::EOpConstructUMat2x3: |
| 3019 | case glslang::EOpConstructUMat2x4: |
| 3020 | case glslang::EOpConstructUMat3x2: |
| 3021 | case glslang::EOpConstructUMat3x3: |
| 3022 | case glslang::EOpConstructUMat3x4: |
| 3023 | case glslang::EOpConstructUMat4x2: |
| 3024 | case glslang::EOpConstructUMat4x3: |
| 3025 | case glslang::EOpConstructUMat4x4: |
| 3026 | case glslang::EOpConstructBMat2x2: |
| 3027 | case glslang::EOpConstructBMat2x3: |
| 3028 | case glslang::EOpConstructBMat2x4: |
| 3029 | case glslang::EOpConstructBMat3x2: |
| 3030 | case glslang::EOpConstructBMat3x3: |
| 3031 | case glslang::EOpConstructBMat3x4: |
| 3032 | case glslang::EOpConstructBMat4x2: |
| 3033 | case glslang::EOpConstructBMat4x3: |
| 3034 | case glslang::EOpConstructBMat4x4: |
| 3035 | case glslang::EOpConstructF16Mat2x2: |
| 3036 | case glslang::EOpConstructF16Mat2x3: |
| 3037 | case glslang::EOpConstructF16Mat2x4: |
| 3038 | case glslang::EOpConstructF16Mat3x2: |
| 3039 | case glslang::EOpConstructF16Mat3x3: |
| 3040 | case glslang::EOpConstructF16Mat3x4: |
| 3041 | case glslang::EOpConstructF16Mat4x2: |
| 3042 | case glslang::EOpConstructF16Mat4x3: |
| 3043 | case glslang::EOpConstructF16Mat4x4: |
| 3044 | isMatrix = true; |
| 3045 | // fall through |
| 3046 | case glslang::EOpConstructFloat: |
| 3047 | case glslang::EOpConstructVec2: |
| 3048 | case glslang::EOpConstructVec3: |
| 3049 | case glslang::EOpConstructVec4: |
| 3050 | case glslang::EOpConstructDouble: |
| 3051 | case glslang::EOpConstructDVec2: |
| 3052 | case glslang::EOpConstructDVec3: |
| 3053 | case glslang::EOpConstructDVec4: |
| 3054 | case glslang::EOpConstructFloat16: |
| 3055 | case glslang::EOpConstructF16Vec2: |
| 3056 | case glslang::EOpConstructF16Vec3: |
| 3057 | case glslang::EOpConstructF16Vec4: |
| 3058 | case glslang::EOpConstructBool: |
| 3059 | case glslang::EOpConstructBVec2: |
| 3060 | case glslang::EOpConstructBVec3: |
| 3061 | case glslang::EOpConstructBVec4: |
| 3062 | case glslang::EOpConstructInt8: |
| 3063 | case glslang::EOpConstructI8Vec2: |
| 3064 | case glslang::EOpConstructI8Vec3: |
| 3065 | case glslang::EOpConstructI8Vec4: |
| 3066 | case glslang::EOpConstructUint8: |
| 3067 | case glslang::EOpConstructU8Vec2: |
| 3068 | case glslang::EOpConstructU8Vec3: |
| 3069 | case glslang::EOpConstructU8Vec4: |
| 3070 | case glslang::EOpConstructInt16: |
| 3071 | case glslang::EOpConstructI16Vec2: |
| 3072 | case glslang::EOpConstructI16Vec3: |
| 3073 | case glslang::EOpConstructI16Vec4: |
| 3074 | case glslang::EOpConstructUint16: |
| 3075 | case glslang::EOpConstructU16Vec2: |
| 3076 | case glslang::EOpConstructU16Vec3: |
| 3077 | case glslang::EOpConstructU16Vec4: |
| 3078 | case glslang::EOpConstructInt: |
| 3079 | case glslang::EOpConstructIVec2: |
| 3080 | case glslang::EOpConstructIVec3: |
| 3081 | case glslang::EOpConstructIVec4: |
| 3082 | case glslang::EOpConstructUint: |
| 3083 | case glslang::EOpConstructUVec2: |
| 3084 | case glslang::EOpConstructUVec3: |
| 3085 | case glslang::EOpConstructUVec4: |
| 3086 | case glslang::EOpConstructInt64: |
| 3087 | case glslang::EOpConstructI64Vec2: |
| 3088 | case glslang::EOpConstructI64Vec3: |
| 3089 | case glslang::EOpConstructI64Vec4: |
| 3090 | case glslang::EOpConstructUint64: |
| 3091 | case glslang::EOpConstructU64Vec2: |
| 3092 | case glslang::EOpConstructU64Vec3: |
| 3093 | case glslang::EOpConstructU64Vec4: |
| 3094 | case glslang::EOpConstructStruct: |
| 3095 | case glslang::EOpConstructTextureSampler: |
| 3096 | case glslang::EOpConstructReference: |
| 3097 | case glslang::EOpConstructCooperativeMatrix: |
| 3098 | { |
| 3099 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3100 | std::vector<spv::Id> arguments; |
| 3101 | translateArguments(*node, arguments, lvalueCoherentFlags); |
| 3102 | spv::Id constructed; |
| 3103 | if (node->getOp() == glslang::EOpConstructTextureSampler) { |
| 3104 | const glslang::TType& texType = node->getSequence()[0]->getAsTyped()->getType(); |
| 3105 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6 && |
| 3106 | texType.getSampler().isBuffer()) { |
| 3107 | // SamplerBuffer is not supported in spirv1.6 so |
| 3108 | // `samplerBuffer(textureBuffer, sampler)` is a no-op |
| 3109 | // and textureBuffer is the result going forward |
| 3110 | constructed = arguments[0]; |
| 3111 | } else |
| 3112 | constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
| 3113 | } else if (node->getOp() == glslang::EOpConstructStruct || |
| 3114 | node->getOp() == glslang::EOpConstructCooperativeMatrix || |
| 3115 | node->getType().isArray()) { |
| 3116 | std::vector<spv::Id> constituents; |
| 3117 | for (int c = 0; c < (int)arguments.size(); ++c) |
| 3118 | constituents.push_back(arguments[c]); |
| 3119 | constructed = createCompositeConstruct(resultType(), constituents); |
| 3120 | } else if (isMatrix) |
| 3121 | constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
| 3122 | else |
| 3123 | constructed = builder.createConstructor(precision, arguments, resultType()); |
| 3124 | |
| 3125 | if (node->getType().getQualifier().isNonUniform()) { |
| 3126 | builder.addDecoration(constructed, spv::DecorationNonUniformEXT); |
| 3127 | } |
| 3128 | |
| 3129 | builder.clearAccessChain(); |
| 3130 | builder.setAccessChainRValue(constructed); |
| 3131 | |
| 3132 | return false; |
| 3133 | } |
| 3134 | |
| 3135 | // These six are component-wise compares with component-wise results. |
| 3136 | // Forward on to createBinaryOperation(), requesting a vector result. |
| 3137 | case glslang::EOpLessThan: |
| 3138 | case glslang::EOpGreaterThan: |
| 3139 | case glslang::EOpLessThanEqual: |
| 3140 | case glslang::EOpGreaterThanEqual: |
| 3141 | case glslang::EOpVectorEqual: |
| 3142 | case glslang::EOpVectorNotEqual: |
| 3143 | { |
| 3144 | // Map the operation to a binary |
| 3145 | binOp = node->getOp(); |
| 3146 | reduceComparison = false; |
| 3147 | switch (node->getOp()) { |
| 3148 | case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 3149 | case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 3150 | default: binOp = node->getOp(); break; |
| 3151 | } |
| 3152 | |
| 3153 | break; |
| 3154 | } |
| 3155 | case glslang::EOpMul: |
| 3156 | // component-wise matrix multiply |
| 3157 | binOp = glslang::EOpMul; |
| 3158 | break; |
| 3159 | case glslang::EOpOuterProduct: |
| 3160 | // two vectors multiplied to make a matrix |
| 3161 | binOp = glslang::EOpOuterProduct; |
| 3162 | break; |
| 3163 | case glslang::EOpDot: |
| 3164 | { |
| 3165 | // for scalar dot product, use multiply |
| 3166 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 3167 | if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
| 3168 | binOp = glslang::EOpMul; |
| 3169 | break; |
| 3170 | } |
| 3171 | case glslang::EOpMod: |
| 3172 | // when an aggregate, this is the floating-point mod built-in function, |
| 3173 | // which can be emitted by the one in createBinaryOperation() |
| 3174 | binOp = glslang::EOpMod; |
| 3175 | break; |
| 3176 | |
| 3177 | case glslang::EOpEmitVertex: |
| 3178 | case glslang::EOpEndPrimitive: |
| 3179 | case glslang::EOpBarrier: |
| 3180 | case glslang::EOpMemoryBarrier: |
| 3181 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 3182 | case glslang::EOpMemoryBarrierBuffer: |
| 3183 | case glslang::EOpMemoryBarrierImage: |
| 3184 | case glslang::EOpMemoryBarrierShared: |
| 3185 | case glslang::EOpGroupMemoryBarrier: |
| 3186 | case glslang::EOpDeviceMemoryBarrier: |
| 3187 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 3188 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 3189 | case glslang::EOpWorkgroupMemoryBarrier: |
| 3190 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
| 3191 | case glslang::EOpSubgroupBarrier: |
| 3192 | case glslang::EOpSubgroupMemoryBarrier: |
| 3193 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 3194 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 3195 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 3196 | noReturnValue = true; |
| 3197 | // These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 3198 | break; |
| 3199 | |
| 3200 | case glslang::EOpAtomicAdd: |
| 3201 | case glslang::EOpAtomicSubtract: |
| 3202 | case glslang::EOpAtomicMin: |
| 3203 | case glslang::EOpAtomicMax: |
| 3204 | case glslang::EOpAtomicAnd: |
| 3205 | case glslang::EOpAtomicOr: |
| 3206 | case glslang::EOpAtomicXor: |
| 3207 | case glslang::EOpAtomicExchange: |
| 3208 | case glslang::EOpAtomicCompSwap: |
| 3209 | atomic = true; |
| 3210 | break; |
| 3211 | |
| 3212 | #ifndef GLSLANG_WEB |
| 3213 | case glslang::EOpAtomicStore: |
| 3214 | noReturnValue = true; |
| 3215 | // fallthrough |
| 3216 | case glslang::EOpAtomicLoad: |
| 3217 | atomic = true; |
| 3218 | break; |
| 3219 | |
| 3220 | case glslang::EOpAtomicCounterAdd: |
| 3221 | case glslang::EOpAtomicCounterSubtract: |
| 3222 | case glslang::EOpAtomicCounterMin: |
| 3223 | case glslang::EOpAtomicCounterMax: |
| 3224 | case glslang::EOpAtomicCounterAnd: |
| 3225 | case glslang::EOpAtomicCounterOr: |
| 3226 | case glslang::EOpAtomicCounterXor: |
| 3227 | case glslang::EOpAtomicCounterExchange: |
| 3228 | case glslang::EOpAtomicCounterCompSwap: |
| 3229 | builder.addExtension("SPV_KHR_shader_atomic_counter_ops" ); |
| 3230 | builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 3231 | atomic = true; |
| 3232 | break; |
| 3233 | |
| 3234 | case glslang::EOpAbsDifference: |
| 3235 | case glslang::EOpAddSaturate: |
| 3236 | case glslang::EOpSubSaturate: |
| 3237 | case glslang::EOpAverage: |
| 3238 | case glslang::EOpAverageRounded: |
| 3239 | case glslang::EOpMul32x16: |
| 3240 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 3241 | builder.addExtension("SPV_INTEL_shader_integer_functions2" ); |
| 3242 | binOp = node->getOp(); |
| 3243 | break; |
| 3244 | |
| 3245 | case glslang::EOpIgnoreIntersectionNV: |
| 3246 | case glslang::EOpTerminateRayNV: |
| 3247 | case glslang::EOpTraceNV: |
| 3248 | case glslang::EOpTraceRayMotionNV: |
| 3249 | case glslang::EOpTraceKHR: |
| 3250 | case glslang::EOpExecuteCallableNV: |
| 3251 | case glslang::EOpExecuteCallableKHR: |
| 3252 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 3253 | case glslang::EOpEmitMeshTasksEXT: |
| 3254 | case glslang::EOpSetMeshOutputsEXT: |
| 3255 | noReturnValue = true; |
| 3256 | break; |
| 3257 | case glslang::EOpRayQueryInitialize: |
| 3258 | case glslang::EOpRayQueryTerminate: |
| 3259 | case glslang::EOpRayQueryGenerateIntersection: |
| 3260 | case glslang::EOpRayQueryConfirmIntersection: |
| 3261 | builder.addExtension("SPV_KHR_ray_query" ); |
| 3262 | builder.addCapability(spv::CapabilityRayQueryKHR); |
| 3263 | noReturnValue = true; |
| 3264 | break; |
| 3265 | case glslang::EOpRayQueryProceed: |
| 3266 | case glslang::EOpRayQueryGetIntersectionType: |
| 3267 | case glslang::EOpRayQueryGetRayTMin: |
| 3268 | case glslang::EOpRayQueryGetRayFlags: |
| 3269 | case glslang::EOpRayQueryGetIntersectionT: |
| 3270 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 3271 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 3272 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 3273 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 3274 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 3275 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 3276 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 3277 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 3278 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 3279 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 3280 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 3281 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 3282 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 3283 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 3284 | builder.addExtension("SPV_KHR_ray_query" ); |
| 3285 | builder.addCapability(spv::CapabilityRayQueryKHR); |
| 3286 | break; |
| 3287 | case glslang::EOpCooperativeMatrixLoad: |
| 3288 | case glslang::EOpCooperativeMatrixStore: |
| 3289 | noReturnValue = true; |
| 3290 | break; |
| 3291 | case glslang::EOpBeginInvocationInterlock: |
| 3292 | case glslang::EOpEndInvocationInterlock: |
| 3293 | builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 3294 | noReturnValue = true; |
| 3295 | break; |
| 3296 | |
| 3297 | case glslang::EOpHitObjectTraceRayNV: |
| 3298 | case glslang::EOpHitObjectTraceRayMotionNV: |
| 3299 | case glslang::EOpHitObjectGetAttributesNV: |
| 3300 | case glslang::EOpHitObjectExecuteShaderNV: |
| 3301 | case glslang::EOpHitObjectRecordEmptyNV: |
| 3302 | case glslang::EOpHitObjectRecordMissNV: |
| 3303 | case glslang::EOpHitObjectRecordMissMotionNV: |
| 3304 | case glslang::EOpHitObjectRecordHitNV: |
| 3305 | case glslang::EOpHitObjectRecordHitMotionNV: |
| 3306 | case glslang::EOpHitObjectRecordHitWithIndexNV: |
| 3307 | case glslang::EOpHitObjectRecordHitWithIndexMotionNV: |
| 3308 | case glslang::EOpReorderThreadNV: |
| 3309 | noReturnValue = true; |
| 3310 | //Fallthrough |
| 3311 | case glslang::EOpHitObjectIsEmptyNV: |
| 3312 | case glslang::EOpHitObjectIsMissNV: |
| 3313 | case glslang::EOpHitObjectIsHitNV: |
| 3314 | case glslang::EOpHitObjectGetRayTMinNV: |
| 3315 | case glslang::EOpHitObjectGetRayTMaxNV: |
| 3316 | case glslang::EOpHitObjectGetObjectRayOriginNV: |
| 3317 | case glslang::EOpHitObjectGetObjectRayDirectionNV: |
| 3318 | case glslang::EOpHitObjectGetWorldRayOriginNV: |
| 3319 | case glslang::EOpHitObjectGetWorldRayDirectionNV: |
| 3320 | case glslang::EOpHitObjectGetObjectToWorldNV: |
| 3321 | case glslang::EOpHitObjectGetWorldToObjectNV: |
| 3322 | case glslang::EOpHitObjectGetInstanceCustomIndexNV: |
| 3323 | case glslang::EOpHitObjectGetInstanceIdNV: |
| 3324 | case glslang::EOpHitObjectGetGeometryIndexNV: |
| 3325 | case glslang::EOpHitObjectGetPrimitiveIndexNV: |
| 3326 | case glslang::EOpHitObjectGetHitKindNV: |
| 3327 | case glslang::EOpHitObjectGetCurrentTimeNV: |
| 3328 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexNV: |
| 3329 | case glslang::EOpHitObjectGetShaderRecordBufferHandleNV: |
| 3330 | builder.addExtension(spv::E_SPV_NV_shader_invocation_reorder); |
| 3331 | builder.addCapability(spv::CapabilityShaderInvocationReorderNV); |
| 3332 | break; |
| 3333 | case glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT: |
| 3334 | builder.addExtension(spv::E_SPV_KHR_ray_tracing_position_fetch); |
| 3335 | builder.addCapability(spv::CapabilityRayQueryPositionFetchKHR); |
| 3336 | noReturnValue = true; |
| 3337 | break; |
| 3338 | #endif |
| 3339 | |
| 3340 | case glslang::EOpDebugPrintf: |
| 3341 | noReturnValue = true; |
| 3342 | break; |
| 3343 | |
| 3344 | default: |
| 3345 | break; |
| 3346 | } |
| 3347 | |
| 3348 | // |
| 3349 | // See if it maps to a regular operation. |
| 3350 | // |
| 3351 | if (binOp != glslang::EOpNull) { |
| 3352 | glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 3353 | glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 3354 | assert(left && right); |
| 3355 | |
| 3356 | builder.clearAccessChain(); |
| 3357 | left->traverse(this); |
| 3358 | spv::Id leftId = accessChainLoad(left->getType()); |
| 3359 | |
| 3360 | builder.clearAccessChain(); |
| 3361 | right->traverse(this); |
| 3362 | spv::Id rightId = accessChainLoad(right->getType()); |
| 3363 | |
| 3364 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3365 | OpDecorations decorations = { precision, |
| 3366 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 3367 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 3368 | result = createBinaryOperation(binOp, decorations, |
| 3369 | resultType(), leftId, rightId, |
| 3370 | left->getType().getBasicType(), reduceComparison); |
| 3371 | |
| 3372 | // code above should only make binOp that exists in createBinaryOperation |
| 3373 | assert(result != spv::NoResult); |
| 3374 | builder.clearAccessChain(); |
| 3375 | builder.setAccessChainRValue(result); |
| 3376 | |
| 3377 | return false; |
| 3378 | } |
| 3379 | |
| 3380 | // |
| 3381 | // Create the list of operands. |
| 3382 | // |
| 3383 | glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 3384 | std::vector<spv::Id> operands; |
| 3385 | std::vector<spv::IdImmediate> memoryAccessOperands; |
| 3386 | for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
| 3387 | // special case l-value operands; there are just a few |
| 3388 | bool lvalue = false; |
| 3389 | switch (node->getOp()) { |
| 3390 | case glslang::EOpModf: |
| 3391 | if (arg == 1) |
| 3392 | lvalue = true; |
| 3393 | break; |
| 3394 | |
| 3395 | |
| 3396 | |
| 3397 | case glslang::EOpHitObjectRecordHitNV: |
| 3398 | case glslang::EOpHitObjectRecordHitMotionNV: |
| 3399 | case glslang::EOpHitObjectRecordHitWithIndexNV: |
| 3400 | case glslang::EOpHitObjectRecordHitWithIndexMotionNV: |
| 3401 | case glslang::EOpHitObjectTraceRayNV: |
| 3402 | case glslang::EOpHitObjectTraceRayMotionNV: |
| 3403 | case glslang::EOpHitObjectExecuteShaderNV: |
| 3404 | case glslang::EOpHitObjectRecordMissNV: |
| 3405 | case glslang::EOpHitObjectRecordMissMotionNV: |
| 3406 | case glslang::EOpHitObjectGetAttributesNV: |
| 3407 | if (arg == 0) |
| 3408 | lvalue = true; |
| 3409 | break; |
| 3410 | |
| 3411 | case glslang::EOpRayQueryInitialize: |
| 3412 | case glslang::EOpRayQueryTerminate: |
| 3413 | case glslang::EOpRayQueryConfirmIntersection: |
| 3414 | case glslang::EOpRayQueryProceed: |
| 3415 | case glslang::EOpRayQueryGenerateIntersection: |
| 3416 | case glslang::EOpRayQueryGetIntersectionType: |
| 3417 | case glslang::EOpRayQueryGetIntersectionT: |
| 3418 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 3419 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 3420 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 3421 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 3422 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 3423 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 3424 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 3425 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 3426 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 3427 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 3428 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 3429 | if (arg == 0) |
| 3430 | lvalue = true; |
| 3431 | break; |
| 3432 | |
| 3433 | case glslang::EOpAtomicAdd: |
| 3434 | case glslang::EOpAtomicSubtract: |
| 3435 | case glslang::EOpAtomicMin: |
| 3436 | case glslang::EOpAtomicMax: |
| 3437 | case glslang::EOpAtomicAnd: |
| 3438 | case glslang::EOpAtomicOr: |
| 3439 | case glslang::EOpAtomicXor: |
| 3440 | case glslang::EOpAtomicExchange: |
| 3441 | case glslang::EOpAtomicCompSwap: |
| 3442 | if (arg == 0) |
| 3443 | lvalue = true; |
| 3444 | break; |
| 3445 | |
| 3446 | #ifndef GLSLANG_WEB |
| 3447 | case glslang::EOpFrexp: |
| 3448 | if (arg == 1) |
| 3449 | lvalue = true; |
| 3450 | break; |
| 3451 | case glslang::EOpInterpolateAtSample: |
| 3452 | case glslang::EOpInterpolateAtOffset: |
| 3453 | case glslang::EOpInterpolateAtVertex: |
| 3454 | if (arg == 0) { |
| 3455 | // If GLSL, use the address of the interpolant argument. |
| 3456 | // If HLSL, use an internal version of OpInterolates that takes |
| 3457 | // the rvalue of the interpolant. A fixup pass in spirv-opt |
| 3458 | // legalization will remove the OpLoad and convert to an lvalue. |
| 3459 | // Had to do this because legalization will only propagate a |
| 3460 | // builtin into an rvalue. |
| 3461 | lvalue = glslangIntermediate->getSource() != glslang::EShSourceHlsl; |
| 3462 | |
| 3463 | // Does it need a swizzle inversion? If so, evaluation is inverted; |
| 3464 | // operate first on the swizzle base, then apply the swizzle. |
| 3465 | // That is, we transform |
| 3466 | // |
| 3467 | // interpolate(v.zy) -> interpolate(v).zy |
| 3468 | // |
| 3469 | if (glslangOperands[0]->getAsOperator() && |
| 3470 | glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 3471 | invertedType = convertGlslangToSpvType( |
| 3472 | glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 3473 | } |
| 3474 | break; |
| 3475 | case glslang::EOpAtomicLoad: |
| 3476 | case glslang::EOpAtomicStore: |
| 3477 | case glslang::EOpAtomicCounterAdd: |
| 3478 | case glslang::EOpAtomicCounterSubtract: |
| 3479 | case glslang::EOpAtomicCounterMin: |
| 3480 | case glslang::EOpAtomicCounterMax: |
| 3481 | case glslang::EOpAtomicCounterAnd: |
| 3482 | case glslang::EOpAtomicCounterOr: |
| 3483 | case glslang::EOpAtomicCounterXor: |
| 3484 | case glslang::EOpAtomicCounterExchange: |
| 3485 | case glslang::EOpAtomicCounterCompSwap: |
| 3486 | if (arg == 0) |
| 3487 | lvalue = true; |
| 3488 | break; |
| 3489 | case glslang::EOpAddCarry: |
| 3490 | case glslang::EOpSubBorrow: |
| 3491 | if (arg == 2) |
| 3492 | lvalue = true; |
| 3493 | break; |
| 3494 | case glslang::EOpUMulExtended: |
| 3495 | case glslang::EOpIMulExtended: |
| 3496 | if (arg >= 2) |
| 3497 | lvalue = true; |
| 3498 | break; |
| 3499 | case glslang::EOpCooperativeMatrixLoad: |
| 3500 | if (arg == 0 || arg == 1) |
| 3501 | lvalue = true; |
| 3502 | break; |
| 3503 | case glslang::EOpCooperativeMatrixStore: |
| 3504 | if (arg == 1) |
| 3505 | lvalue = true; |
| 3506 | break; |
| 3507 | case glslang::EOpSpirvInst: |
| 3508 | if (glslangOperands[arg]->getAsTyped()->getQualifier().isSpirvByReference()) |
| 3509 | lvalue = true; |
| 3510 | break; |
| 3511 | case glslang::EOpReorderThreadNV: |
| 3512 | //Three variants of reorderThreadNV, two of them use hitObjectNV |
| 3513 | if (arg == 0 && glslangOperands.size() != 2) |
| 3514 | lvalue = true; |
| 3515 | break; |
| 3516 | case glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT: |
| 3517 | if (arg == 0 || arg == 2) |
| 3518 | lvalue = true; |
| 3519 | break; |
| 3520 | #endif |
| 3521 | default: |
| 3522 | break; |
| 3523 | } |
| 3524 | builder.clearAccessChain(); |
| 3525 | if (invertedType != spv::NoType && arg == 0) |
| 3526 | glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 3527 | else |
| 3528 | glslangOperands[arg]->traverse(this); |
| 3529 | |
| 3530 | #ifndef GLSLANG_WEB |
| 3531 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
| 3532 | node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 3533 | |
| 3534 | if (arg == 1) { |
| 3535 | // fold "element" parameter into the access chain |
| 3536 | spv::Builder::AccessChain save = builder.getAccessChain(); |
| 3537 | builder.clearAccessChain(); |
| 3538 | glslangOperands[2]->traverse(this); |
| 3539 | |
| 3540 | spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
| 3541 | |
| 3542 | builder.setAccessChain(save); |
| 3543 | |
| 3544 | // Point to the first element of the array. |
| 3545 | builder.accessChainPush(elementId, |
| 3546 | TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
| 3547 | glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
| 3548 | |
| 3549 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 3550 | unsigned int alignment = builder.getAccessChain().alignment; |
| 3551 | |
| 3552 | int memoryAccess = TranslateMemoryAccess(coherentFlags); |
| 3553 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
| 3554 | memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
| 3555 | if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
| 3556 | memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
| 3557 | if (builder.getStorageClass(builder.getAccessChain().base) == |
| 3558 | spv::StorageClassPhysicalStorageBufferEXT) { |
| 3559 | memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
| 3560 | } |
| 3561 | |
| 3562 | memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
| 3563 | |
| 3564 | if (memoryAccess & spv::MemoryAccessAlignedMask) { |
| 3565 | memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
| 3566 | } |
| 3567 | |
| 3568 | if (memoryAccess & |
| 3569 | (spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
| 3570 | memoryAccessOperands.push_back(spv::IdImmediate(true, |
| 3571 | builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
| 3572 | } |
| 3573 | } else if (arg == 2) { |
| 3574 | continue; |
| 3575 | } |
| 3576 | } |
| 3577 | #endif |
| 3578 | |
| 3579 | // for l-values, pass the address, for r-values, pass the value |
| 3580 | if (lvalue) { |
| 3581 | if (invertedType == spv::NoType && !builder.isSpvLvalue()) { |
| 3582 | // SPIR-V cannot represent an l-value containing a swizzle that doesn't |
| 3583 | // reduce to a simple access chain. So, we need a temporary vector to |
| 3584 | // receive the result, and must later swizzle that into the original |
| 3585 | // l-value. |
| 3586 | complexLvalues.push_back(builder.getAccessChain()); |
| 3587 | temporaryLvalues.push_back(builder.createVariable( |
| 3588 | spv::NoPrecision, spv::StorageClassFunction, |
| 3589 | builder.accessChainGetInferredType(), "swizzleTemp" )); |
| 3590 | operands.push_back(temporaryLvalues.back()); |
| 3591 | } else { |
| 3592 | operands.push_back(builder.accessChainGetLValue()); |
| 3593 | } |
| 3594 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 3595 | lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
| 3596 | } else { |
| 3597 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3598 | glslang::TOperator glslangOp = node->getOp(); |
| 3599 | if (arg == 1 && |
| 3600 | (glslangOp == glslang::EOpRayQueryGetIntersectionType || |
| 3601 | glslangOp == glslang::EOpRayQueryGetIntersectionT || |
| 3602 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex || |
| 3603 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId || |
| 3604 | glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset || |
| 3605 | glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex || |
| 3606 | glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex || |
| 3607 | glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics || |
| 3608 | glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace || |
| 3609 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection || |
| 3610 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin || |
| 3611 | glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld || |
| 3612 | glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject || |
| 3613 | glslangOp == glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT |
| 3614 | )) { |
| 3615 | bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst(); |
| 3616 | operands.push_back(builder.makeIntConstant(cond ? 1 : 0)); |
| 3617 | } else if ((arg == 10 && glslangOp == glslang::EOpTraceKHR) || |
| 3618 | (arg == 11 && glslangOp == glslang::EOpTraceRayMotionNV) || |
| 3619 | (arg == 1 && glslangOp == glslang::EOpExecuteCallableKHR) || |
| 3620 | (arg == 1 && glslangOp == glslang::EOpHitObjectExecuteShaderNV) || |
| 3621 | (arg == 11 && glslangOp == glslang::EOpHitObjectTraceRayNV) || |
| 3622 | (arg == 12 && glslangOp == glslang::EOpHitObjectTraceRayMotionNV)) { |
| 3623 | const int set = glslangOp == glslang::EOpExecuteCallableKHR ? 1 : 0; |
| 3624 | const int location = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 3625 | auto itNode = locationToSymbol[set].find(location); |
| 3626 | visitSymbol(itNode->second); |
| 3627 | spv::Id symId = getSymbolId(itNode->second); |
| 3628 | operands.push_back(symId); |
| 3629 | } else if ((arg == 12 && glslangOp == glslang::EOpHitObjectRecordHitNV) || |
| 3630 | (arg == 13 && glslangOp == glslang::EOpHitObjectRecordHitMotionNV) || |
| 3631 | (arg == 11 && glslangOp == glslang::EOpHitObjectRecordHitWithIndexNV) || |
| 3632 | (arg == 12 && glslangOp == glslang::EOpHitObjectRecordHitWithIndexMotionNV) || |
| 3633 | (arg == 1 && glslangOp == glslang::EOpHitObjectGetAttributesNV)) { |
| 3634 | const int location = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getUConst(); |
| 3635 | const int set = 2; |
| 3636 | auto itNode = locationToSymbol[set].find(location); |
| 3637 | visitSymbol(itNode->second); |
| 3638 | spv::Id symId = getSymbolId(itNode->second); |
| 3639 | operands.push_back(symId); |
| 3640 | #ifndef GLSLANG_WEB |
| 3641 | } else if (glslangOperands[arg]->getAsTyped()->getQualifier().isSpirvLiteral()) { |
| 3642 | // Will be translated to a literal value, make a placeholder here |
| 3643 | operands.push_back(spv::NoResult); |
| 3644 | #endif |
| 3645 | } else { |
| 3646 | operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
| 3647 | } |
| 3648 | } |
| 3649 | } |
| 3650 | |
| 3651 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3652 | #ifndef GLSLANG_WEB |
| 3653 | if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
| 3654 | std::vector<spv::IdImmediate> idImmOps; |
| 3655 | |
| 3656 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3657 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3658 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3659 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3660 | // get the pointee type |
| 3661 | spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
| 3662 | assert(builder.isCooperativeMatrixType(typeId)); |
| 3663 | // do the op |
| 3664 | spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
| 3665 | // store the result to the pointer (out param 'm') |
| 3666 | builder.createStore(result, operands[0]); |
| 3667 | result = 0; |
| 3668 | } else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 3669 | std::vector<spv::IdImmediate> idImmOps; |
| 3670 | |
| 3671 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3672 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
| 3673 | idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3674 | idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3675 | idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3676 | |
| 3677 | builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps); |
| 3678 | result = 0; |
| 3679 | } else if (node->getOp() == glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT) { |
| 3680 | std::vector<spv::IdImmediate> idImmOps; |
| 3681 | |
| 3682 | idImmOps.push_back(spv::IdImmediate(true, operands[0])); // q |
| 3683 | idImmOps.push_back(spv::IdImmediate(true, operands[1])); // committed |
| 3684 | |
| 3685 | spv::Id typeId = builder.makeArrayType(builder.makeVectorType(builder.makeFloatType(32), 3), |
| 3686 | builder.makeUintConstant(3), 0); |
| 3687 | // do the op |
| 3688 | spv::Id result = builder.createOp(spv::OpRayQueryGetIntersectionTriangleVertexPositionsKHR, typeId, idImmOps); |
| 3689 | // store the result to the pointer (out param 'm') |
| 3690 | builder.createStore(result, operands[2]); |
| 3691 | result = 0; |
| 3692 | } else |
| 3693 | #endif |
| 3694 | if (atomic) { |
| 3695 | // Handle all atomics |
| 3696 | glslang::TBasicType typeProxy = (node->getOp() == glslang::EOpAtomicStore) |
| 3697 | ? node->getSequence()[0]->getAsTyped()->getBasicType() : node->getBasicType(); |
| 3698 | result = createAtomicOperation(node->getOp(), precision, resultType(), operands, typeProxy, |
| 3699 | lvalueCoherentFlags); |
| 3700 | #ifndef GLSLANG_WEB |
| 3701 | } else if (node->getOp() == glslang::EOpSpirvInst) { |
| 3702 | const auto& spirvInst = node->getSpirvInstruction(); |
| 3703 | if (spirvInst.set == "" ) { |
| 3704 | std::vector<spv::IdImmediate> idImmOps; |
| 3705 | for (unsigned int i = 0; i < glslangOperands.size(); ++i) { |
| 3706 | if (glslangOperands[i]->getAsTyped()->getQualifier().isSpirvLiteral()) { |
| 3707 | // Translate the constant to a literal value |
| 3708 | std::vector<unsigned> literals; |
| 3709 | glslang::TVector<const glslang::TIntermConstantUnion*> constants; |
| 3710 | constants.push_back(glslangOperands[i]->getAsConstantUnion()); |
| 3711 | TranslateLiterals(constants, literals); |
| 3712 | idImmOps.push_back({false, literals[0]}); |
| 3713 | } else |
| 3714 | idImmOps.push_back({true, operands[i]}); |
| 3715 | } |
| 3716 | |
| 3717 | if (node->getBasicType() == glslang::EbtVoid) |
| 3718 | builder.createNoResultOp(static_cast<spv::Op>(spirvInst.id), idImmOps); |
| 3719 | else |
| 3720 | result = builder.createOp(static_cast<spv::Op>(spirvInst.id), resultType(), idImmOps); |
| 3721 | } else { |
| 3722 | result = builder.createBuiltinCall( |
| 3723 | resultType(), spirvInst.set == "GLSL.std.450" ? stdBuiltins : getExtBuiltins(spirvInst.set.c_str()), |
| 3724 | spirvInst.id, operands); |
| 3725 | } |
| 3726 | noReturnValue = node->getBasicType() == glslang::EbtVoid; |
| 3727 | #endif |
| 3728 | } else if (node->getOp() == glslang::EOpDebugPrintf) { |
| 3729 | if (!nonSemanticDebugPrintf) { |
| 3730 | nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf" ); |
| 3731 | } |
| 3732 | result = builder.createBuiltinCall(builder.makeVoidType(), nonSemanticDebugPrintf, spv::NonSemanticDebugPrintfDebugPrintf, operands); |
| 3733 | builder.addExtension(spv::E_SPV_KHR_non_semantic_info); |
| 3734 | } else { |
| 3735 | // Pass through to generic operations. |
| 3736 | switch (glslangOperands.size()) { |
| 3737 | case 0: |
| 3738 | result = createNoArgOperation(node->getOp(), precision, resultType()); |
| 3739 | break; |
| 3740 | case 1: |
| 3741 | { |
| 3742 | OpDecorations decorations = { precision, |
| 3743 | TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 3744 | TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 3745 | result = createUnaryOperation( |
| 3746 | node->getOp(), decorations, |
| 3747 | resultType(), operands.front(), |
| 3748 | glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
| 3749 | } |
| 3750 | break; |
| 3751 | default: |
| 3752 | result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
| 3753 | break; |
| 3754 | } |
| 3755 | |
| 3756 | if (invertedType != spv::NoResult) |
| 3757 | result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
| 3758 | |
| 3759 | for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) { |
| 3760 | builder.setAccessChain(complexLvalues[i]); |
| 3761 | builder.accessChainStore(builder.createLoad(temporaryLvalues[i], spv::NoPrecision), |
| 3762 | TranslateNonUniformDecoration(complexLvalues[i].coherentFlags)); |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | if (noReturnValue) |
| 3767 | return false; |
| 3768 | |
| 3769 | if (! result) { |
| 3770 | logger->missingFunctionality("unknown glslang aggregate" ); |
| 3771 | return true; // pick up a child as a placeholder operand |
| 3772 | } else { |
| 3773 | builder.clearAccessChain(); |
| 3774 | builder.setAccessChainRValue(result); |
| 3775 | return false; |
| 3776 | } |
| 3777 | } |
| 3778 | |
| 3779 | // This path handles both if-then-else and ?: |
| 3780 | // The if-then-else has a node type of void, while |
| 3781 | // ?: has either a void or a non-void node type |
| 3782 | // |
| 3783 | // Leaving the result, when not void: |
| 3784 | // GLSL only has r-values as the result of a :?, but |
| 3785 | // if we have an l-value, that can be more efficient if it will |
| 3786 | // become the base of a complex r-value expression, because the |
| 3787 | // next layer copies r-values into memory to use the access-chain mechanism |
| 3788 | bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 3789 | { |
| 3790 | // see if OpSelect can handle it |
| 3791 | const auto isOpSelectable = [&]() { |
| 3792 | if (node->getBasicType() == glslang::EbtVoid) |
| 3793 | return false; |
| 3794 | // OpSelect can do all other types starting with SPV 1.4 |
| 3795 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
| 3796 | // pre-1.4, only scalars and vectors can be handled |
| 3797 | if ((!node->getType().isScalar() && !node->getType().isVector())) |
| 3798 | return false; |
| 3799 | } |
| 3800 | return true; |
| 3801 | }; |
| 3802 | |
| 3803 | // See if it simple and safe, or required, to execute both sides. |
| 3804 | // Crucially, side effects must be either semantically required or avoided, |
| 3805 | // and there are performance trade-offs. |
| 3806 | // Return true if required or a good idea (and safe) to execute both sides, |
| 3807 | // false otherwise. |
| 3808 | const auto bothSidesPolicy = [&]() -> bool { |
| 3809 | // do we have both sides? |
| 3810 | if (node->getTrueBlock() == nullptr || |
| 3811 | node->getFalseBlock() == nullptr) |
| 3812 | return false; |
| 3813 | |
| 3814 | // required? (unless we write additional code to look for side effects |
| 3815 | // and make performance trade-offs if none are present) |
| 3816 | if (!node->getShortCircuit()) |
| 3817 | return true; |
| 3818 | |
| 3819 | // if not required to execute both, decide based on performance/practicality... |
| 3820 | |
| 3821 | if (!isOpSelectable()) |
| 3822 | return false; |
| 3823 | |
| 3824 | assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 3825 | node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 3826 | |
| 3827 | // return true if a single operand to ? : is okay for OpSelect |
| 3828 | const auto operandOkay = [](glslang::TIntermTyped* node) { |
| 3829 | return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
| 3830 | }; |
| 3831 | |
| 3832 | return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 3833 | operandOkay(node->getFalseBlock()->getAsTyped()); |
| 3834 | }; |
| 3835 | |
| 3836 | spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 3837 | // emit the condition before doing anything with selection |
| 3838 | node->getCondition()->traverse(this); |
| 3839 | spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 3840 | |
| 3841 | // Find a way of executing both sides and selecting the right result. |
| 3842 | const auto executeBothSides = [&]() -> void { |
| 3843 | // execute both sides |
| 3844 | spv::Id resultType = convertGlslangToSpvType(node->getType()); |
| 3845 | node->getTrueBlock()->traverse(this); |
| 3846 | spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3847 | node->getFalseBlock()->traverse(this); |
| 3848 | spv::Id falseValue = accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()); |
| 3849 | |
| 3850 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3851 | |
| 3852 | // done if void |
| 3853 | if (node->getBasicType() == glslang::EbtVoid) |
| 3854 | return; |
| 3855 | |
| 3856 | // emit code to select between trueValue and falseValue |
| 3857 | // see if OpSelect can handle the result type, and that the SPIR-V types |
| 3858 | // of the inputs match the result type. |
| 3859 | if (isOpSelectable()) { |
| 3860 | // Emit OpSelect for this selection. |
| 3861 | |
| 3862 | // smear condition to vector, if necessary (AST is always scalar) |
| 3863 | // Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
| 3864 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
| 3865 | condition = builder.smearScalar(spv::NoPrecision, condition, |
| 3866 | builder.makeVectorType(builder.makeBoolType(), |
| 3867 | builder.getNumComponents(trueValue))); |
| 3868 | } |
| 3869 | |
| 3870 | // If the types do not match, it is because of mismatched decorations on aggregates. |
| 3871 | // Since isOpSelectable only lets us get here for SPIR-V >= 1.4, we can use OpCopyObject |
| 3872 | // to get matching types. |
| 3873 | if (builder.getTypeId(trueValue) != resultType) { |
| 3874 | trueValue = builder.createUnaryOp(spv::OpCopyLogical, resultType, trueValue); |
| 3875 | } |
| 3876 | if (builder.getTypeId(falseValue) != resultType) { |
| 3877 | falseValue = builder.createUnaryOp(spv::OpCopyLogical, resultType, falseValue); |
| 3878 | } |
| 3879 | |
| 3880 | // OpSelect |
| 3881 | result = builder.createTriOp(spv::OpSelect, resultType, condition, trueValue, falseValue); |
| 3882 | |
| 3883 | builder.clearAccessChain(); |
| 3884 | builder.setAccessChainRValue(result); |
| 3885 | } else { |
| 3886 | // We need control flow to select the result. |
| 3887 | // TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 3888 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), |
| 3889 | spv::StorageClassFunction, resultType); |
| 3890 | |
| 3891 | // Selection control: |
| 3892 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3893 | |
| 3894 | // make an "if" based on the value created by the condition |
| 3895 | spv::Builder::If ifBuilder(condition, control, builder); |
| 3896 | |
| 3897 | // emit the "then" statement |
| 3898 | builder.clearAccessChain(); |
| 3899 | builder.setAccessChainLValue(result); |
| 3900 | multiTypeStore(node->getType(), trueValue); |
| 3901 | |
| 3902 | ifBuilder.makeBeginElse(); |
| 3903 | // emit the "else" statement |
| 3904 | builder.clearAccessChain(); |
| 3905 | builder.setAccessChainLValue(result); |
| 3906 | multiTypeStore(node->getType(), falseValue); |
| 3907 | |
| 3908 | // finish off the control flow |
| 3909 | ifBuilder.makeEndIf(); |
| 3910 | |
| 3911 | builder.clearAccessChain(); |
| 3912 | builder.setAccessChainLValue(result); |
| 3913 | } |
| 3914 | }; |
| 3915 | |
| 3916 | // Execute the one side needed, as per the condition |
| 3917 | const auto executeOneSide = [&]() { |
| 3918 | // Always emit control flow. |
| 3919 | if (node->getBasicType() != glslang::EbtVoid) { |
| 3920 | result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), spv::StorageClassFunction, |
| 3921 | convertGlslangToSpvType(node->getType())); |
| 3922 | } |
| 3923 | |
| 3924 | // Selection control: |
| 3925 | const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3926 | |
| 3927 | // make an "if" based on the value created by the condition |
| 3928 | spv::Builder::If ifBuilder(condition, control, builder); |
| 3929 | |
| 3930 | // emit the "then" statement |
| 3931 | if (node->getTrueBlock() != nullptr) { |
| 3932 | node->getTrueBlock()->traverse(this); |
| 3933 | if (result != spv::NoResult) { |
| 3934 | spv::Id load = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3935 | |
| 3936 | builder.clearAccessChain(); |
| 3937 | builder.setAccessChainLValue(result); |
| 3938 | multiTypeStore(node->getType(), load); |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | if (node->getFalseBlock() != nullptr) { |
| 3943 | ifBuilder.makeBeginElse(); |
| 3944 | // emit the "else" statement |
| 3945 | node->getFalseBlock()->traverse(this); |
| 3946 | if (result != spv::NoResult) { |
| 3947 | spv::Id load = accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()); |
| 3948 | |
| 3949 | builder.clearAccessChain(); |
| 3950 | builder.setAccessChainLValue(result); |
| 3951 | multiTypeStore(node->getType(), load); |
| 3952 | } |
| 3953 | } |
| 3954 | |
| 3955 | // finish off the control flow |
| 3956 | ifBuilder.makeEndIf(); |
| 3957 | |
| 3958 | if (result != spv::NoResult) { |
| 3959 | builder.clearAccessChain(); |
| 3960 | builder.setAccessChainLValue(result); |
| 3961 | } |
| 3962 | }; |
| 3963 | |
| 3964 | // Try for OpSelect (or a requirement to execute both sides) |
| 3965 | if (bothSidesPolicy()) { |
| 3966 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 3967 | if (node->getType().getQualifier().isSpecConstant()) |
| 3968 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 3969 | executeBothSides(); |
| 3970 | } else |
| 3971 | executeOneSide(); |
| 3972 | |
| 3973 | return false; |
| 3974 | } |
| 3975 | |
| 3976 | bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 3977 | { |
| 3978 | // emit and get the condition before doing anything with switch |
| 3979 | node->getCondition()->traverse(this); |
| 3980 | spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
| 3981 | |
| 3982 | // Selection control: |
| 3983 | const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
| 3984 | |
| 3985 | // browse the children to sort out code segments |
| 3986 | int defaultSegment = -1; |
| 3987 | std::vector<TIntermNode*> codeSegments; |
| 3988 | glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 3989 | std::vector<int> caseValues; |
| 3990 | std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 3991 | for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 3992 | TIntermNode* child = *c; |
| 3993 | if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
| 3994 | defaultSegment = (int)codeSegments.size(); |
| 3995 | else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
| 3996 | valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
| 3997 | caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion() |
| 3998 | ->getConstArray()[0].getIConst()); |
| 3999 | } else |
| 4000 | codeSegments.push_back(child); |
| 4001 | } |
| 4002 | |
| 4003 | // handle the case where the last code segment is missing, due to no code |
| 4004 | // statements between the last case and the end of the switch statement |
| 4005 | if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 4006 | (int)codeSegments.size() == defaultSegment) |
| 4007 | codeSegments.push_back(nullptr); |
| 4008 | |
| 4009 | // make the switch statement |
| 4010 | std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
| 4011 | builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, |
| 4012 | segmentBlocks); |
| 4013 | |
| 4014 | // emit all the code in the segments |
| 4015 | breakForLoop.push(false); |
| 4016 | for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 4017 | builder.nextSwitchSegment(segmentBlocks, s); |
| 4018 | if (codeSegments[s]) |
| 4019 | codeSegments[s]->traverse(this); |
| 4020 | else |
| 4021 | builder.addSwitchBreak(); |
| 4022 | } |
| 4023 | breakForLoop.pop(); |
| 4024 | |
| 4025 | builder.endSwitch(segmentBlocks); |
| 4026 | |
| 4027 | return false; |
| 4028 | } |
| 4029 | |
| 4030 | void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 4031 | { |
| 4032 | #ifndef GLSLANG_WEB |
| 4033 | if (node->getQualifier().isSpirvLiteral()) |
| 4034 | return; // Translated to a literal value, skip further processing |
| 4035 | #endif |
| 4036 | |
| 4037 | int nextConst = 0; |
| 4038 | spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
| 4039 | |
| 4040 | builder.clearAccessChain(); |
| 4041 | builder.setAccessChainRValue(constant); |
| 4042 | } |
| 4043 | |
| 4044 | bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 4045 | { |
| 4046 | auto blocks = builder.makeNewLoop(); |
| 4047 | builder.createBranch(&blocks.head); |
| 4048 | |
| 4049 | // Loop control: |
| 4050 | std::vector<unsigned int> operands; |
| 4051 | const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
| 4052 | |
| 4053 | // Spec requires back edges to target header blocks, and every header block |
| 4054 | // must dominate its merge block. Make a header block first to ensure these |
| 4055 | // conditions are met. By definition, it will contain OpLoopMerge, followed |
| 4056 | // by a block-ending branch. But we don't want to put any other body/test |
| 4057 | // instructions in it, since the body/test may have arbitrary instructions, |
| 4058 | // including merges of its own. |
| 4059 | builder.setBuildPoint(&blocks.head); |
| 4060 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 4061 | builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
| 4062 | if (node->testFirst() && node->getTest()) { |
| 4063 | spv::Block& test = builder.makeNewBlock(); |
| 4064 | builder.createBranch(&test); |
| 4065 | |
| 4066 | builder.setBuildPoint(&test); |
| 4067 | node->getTest()->traverse(this); |
| 4068 | spv::Id condition = accessChainLoad(node->getTest()->getType()); |
| 4069 | builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 4070 | |
| 4071 | builder.setBuildPoint(&blocks.body); |
| 4072 | breakForLoop.push(true); |
| 4073 | if (node->getBody()) |
| 4074 | node->getBody()->traverse(this); |
| 4075 | builder.createBranch(&blocks.continue_target); |
| 4076 | breakForLoop.pop(); |
| 4077 | |
| 4078 | builder.setBuildPoint(&blocks.continue_target); |
| 4079 | if (node->getTerminal()) |
| 4080 | node->getTerminal()->traverse(this); |
| 4081 | builder.createBranch(&blocks.head); |
| 4082 | } else { |
| 4083 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 4084 | builder.createBranch(&blocks.body); |
| 4085 | |
| 4086 | breakForLoop.push(true); |
| 4087 | builder.setBuildPoint(&blocks.body); |
| 4088 | if (node->getBody()) |
| 4089 | node->getBody()->traverse(this); |
| 4090 | builder.createBranch(&blocks.continue_target); |
| 4091 | breakForLoop.pop(); |
| 4092 | |
| 4093 | builder.setBuildPoint(&blocks.continue_target); |
| 4094 | if (node->getTerminal()) |
| 4095 | node->getTerminal()->traverse(this); |
| 4096 | if (node->getTest()) { |
| 4097 | node->getTest()->traverse(this); |
| 4098 | spv::Id condition = |
| 4099 | accessChainLoad(node->getTest()->getType()); |
| 4100 | builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
| 4101 | } else { |
| 4102 | // TODO: unless there was a break/return/discard instruction |
| 4103 | // somewhere in the body, this is an infinite loop, so we should |
| 4104 | // issue a warning. |
| 4105 | builder.createBranch(&blocks.head); |
| 4106 | } |
| 4107 | } |
| 4108 | builder.setBuildPoint(&blocks.merge); |
| 4109 | builder.closeLoop(); |
| 4110 | return false; |
| 4111 | } |
| 4112 | |
| 4113 | bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 4114 | { |
| 4115 | if (node->getExpression()) |
| 4116 | node->getExpression()->traverse(this); |
| 4117 | |
| 4118 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 4119 | |
| 4120 | switch (node->getFlowOp()) { |
| 4121 | case glslang::EOpKill: |
| 4122 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
| 4123 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 4124 | builder.addCapability(spv::CapabilityDemoteToHelperInvocation); |
| 4125 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 4126 | } else { |
| 4127 | builder.makeStatementTerminator(spv::OpTerminateInvocation, "post-terminate-invocation" ); |
| 4128 | } |
| 4129 | } else { |
| 4130 | builder.makeStatementTerminator(spv::OpKill, "post-discard" ); |
| 4131 | } |
| 4132 | break; |
| 4133 | case glslang::EOpTerminateInvocation: |
| 4134 | builder.addExtension(spv::E_SPV_KHR_terminate_invocation); |
| 4135 | builder.makeStatementTerminator(spv::OpTerminateInvocation, "post-terminate-invocation" ); |
| 4136 | break; |
| 4137 | case glslang::EOpBreak: |
| 4138 | if (breakForLoop.top()) |
| 4139 | builder.createLoopExit(); |
| 4140 | else |
| 4141 | builder.addSwitchBreak(); |
| 4142 | break; |
| 4143 | case glslang::EOpContinue: |
| 4144 | builder.createLoopContinue(); |
| 4145 | break; |
| 4146 | case glslang::EOpReturn: |
| 4147 | if (node->getExpression() != nullptr) { |
| 4148 | const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 4149 | spv::Id returnId = accessChainLoad(glslangReturnType); |
| 4150 | if (builder.getTypeId(returnId) != currentFunction->getReturnType() || |
| 4151 | TranslatePrecisionDecoration(glslangReturnType) != currentFunction->getReturnPrecision()) { |
| 4152 | builder.clearAccessChain(); |
| 4153 | spv::Id copyId = builder.createVariable(currentFunction->getReturnPrecision(), |
| 4154 | spv::StorageClassFunction, currentFunction->getReturnType()); |
| 4155 | builder.setAccessChainLValue(copyId); |
| 4156 | multiTypeStore(glslangReturnType, returnId); |
| 4157 | returnId = builder.createLoad(copyId, currentFunction->getReturnPrecision()); |
| 4158 | } |
| 4159 | builder.makeReturn(false, returnId); |
| 4160 | } else |
| 4161 | builder.makeReturn(false); |
| 4162 | |
| 4163 | builder.clearAccessChain(); |
| 4164 | break; |
| 4165 | |
| 4166 | #ifndef GLSLANG_WEB |
| 4167 | case glslang::EOpDemote: |
| 4168 | builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 4169 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 4170 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 4171 | break; |
| 4172 | case glslang::EOpTerminateRayKHR: |
| 4173 | builder.makeStatementTerminator(spv::OpTerminateRayKHR, "post-terminateRayKHR" ); |
| 4174 | break; |
| 4175 | case glslang::EOpIgnoreIntersectionKHR: |
| 4176 | builder.makeStatementTerminator(spv::OpIgnoreIntersectionKHR, "post-ignoreIntersectionKHR" ); |
| 4177 | break; |
| 4178 | #endif |
| 4179 | |
| 4180 | default: |
| 4181 | assert(0); |
| 4182 | break; |
| 4183 | } |
| 4184 | |
| 4185 | return false; |
| 4186 | } |
| 4187 | |
| 4188 | spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
| 4189 | { |
| 4190 | // First, steer off constants, which are not SPIR-V variables, but |
| 4191 | // can still have a mapping to a SPIR-V Id. |
| 4192 | // This includes specialization constants. |
| 4193 | if (node->getQualifier().isConstant()) { |
| 4194 | spv::Id result = createSpvConstant(*node); |
| 4195 | if (result != spv::NoResult) |
| 4196 | return result; |
| 4197 | } |
| 4198 | |
| 4199 | // Now, handle actual variables |
| 4200 | spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
| 4201 | spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
| 4202 | : forcedType; |
| 4203 | |
| 4204 | const bool contains16BitType = node->getType().contains16BitFloat() || |
| 4205 | node->getType().contains16BitInt(); |
| 4206 | if (contains16BitType) { |
| 4207 | switch (storageClass) { |
| 4208 | case spv::StorageClassInput: |
| 4209 | case spv::StorageClassOutput: |
| 4210 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| 4211 | builder.addCapability(spv::CapabilityStorageInputOutput16); |
| 4212 | break; |
| 4213 | case spv::StorageClassUniform: |
| 4214 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| 4215 | if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 4216 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 4217 | else |
| 4218 | builder.addCapability(spv::CapabilityStorageUniform16); |
| 4219 | break; |
| 4220 | #ifndef GLSLANG_WEB |
| 4221 | case spv::StorageClassPushConstant: |
| 4222 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| 4223 | builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 4224 | break; |
| 4225 | case spv::StorageClassStorageBuffer: |
| 4226 | case spv::StorageClassPhysicalStorageBufferEXT: |
| 4227 | builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| 4228 | builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 4229 | break; |
| 4230 | #endif |
| 4231 | default: |
| 4232 | if (storageClass == spv::StorageClassWorkgroup && |
| 4233 | node->getType().getBasicType() == glslang::EbtBlock) { |
| 4234 | builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayout16BitAccessKHR); |
| 4235 | break; |
| 4236 | } |
| 4237 | if (node->getType().contains16BitFloat()) |
| 4238 | builder.addCapability(spv::CapabilityFloat16); |
| 4239 | if (node->getType().contains16BitInt()) |
| 4240 | builder.addCapability(spv::CapabilityInt16); |
| 4241 | break; |
| 4242 | } |
| 4243 | } |
| 4244 | |
| 4245 | if (node->getType().contains8BitInt()) { |
| 4246 | if (storageClass == spv::StorageClassPushConstant) { |
| 4247 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
| 4248 | builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 4249 | } else if (storageClass == spv::StorageClassUniform) { |
| 4250 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
| 4251 | builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
| 4252 | } else if (storageClass == spv::StorageClassStorageBuffer) { |
| 4253 | builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
| 4254 | builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
| 4255 | } else if (storageClass == spv::StorageClassWorkgroup && |
| 4256 | node->getType().getBasicType() == glslang::EbtBlock) { |
| 4257 | builder.addCapability(spv::CapabilityWorkgroupMemoryExplicitLayout8BitAccessKHR); |
| 4258 | } else { |
| 4259 | builder.addCapability(spv::CapabilityInt8); |
| 4260 | } |
| 4261 | } |
| 4262 | |
| 4263 | const char* name = node->getName().c_str(); |
| 4264 | if (glslang::IsAnonymous(name)) |
| 4265 | name = "" ; |
| 4266 | |
| 4267 | spv::Id initializer = spv::NoResult; |
| 4268 | |
| 4269 | if (node->getType().getQualifier().storage == glslang::EvqUniform && !node->getConstArray().empty()) { |
| 4270 | int nextConst = 0; |
| 4271 | initializer = createSpvConstantFromConstUnionArray(node->getType(), |
| 4272 | node->getConstArray(), |
| 4273 | nextConst, |
| 4274 | false /* specConst */); |
| 4275 | } else if (node->getType().getQualifier().isNullInit()) { |
| 4276 | initializer = builder.makeNullConstant(spvType); |
| 4277 | } |
| 4278 | |
| 4279 | return builder.createVariable(spv::NoPrecision, storageClass, spvType, name, initializer, false); |
| 4280 | } |
| 4281 | |
| 4282 | // Return type Id of the sampled type. |
| 4283 | spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 4284 | { |
| 4285 | switch (sampler.type) { |
| 4286 | case glslang::EbtInt: return builder.makeIntType(32); |
| 4287 | case glslang::EbtUint: return builder.makeUintType(32); |
| 4288 | case glslang::EbtFloat: return builder.makeFloatType(32); |
| 4289 | #ifndef GLSLANG_WEB |
| 4290 | case glslang::EbtFloat16: |
| 4291 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 4292 | builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 4293 | return builder.makeFloatType(16); |
| 4294 | case glslang::EbtInt64: |
| 4295 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 4296 | builder.addCapability(spv::CapabilityInt64ImageEXT); |
| 4297 | return builder.makeIntType(64); |
| 4298 | case glslang::EbtUint64: |
| 4299 | builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 4300 | builder.addCapability(spv::CapabilityInt64ImageEXT); |
| 4301 | return builder.makeUintType(64); |
| 4302 | #endif |
| 4303 | default: |
| 4304 | assert(0); |
| 4305 | return builder.makeFloatType(32); |
| 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | // If node is a swizzle operation, return the type that should be used if |
| 4310 | // the swizzle base is first consumed by another operation, before the swizzle |
| 4311 | // is applied. |
| 4312 | spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 4313 | { |
| 4314 | if (node.getAsOperator() && |
| 4315 | node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 4316 | return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 4317 | else |
| 4318 | return spv::NoType; |
| 4319 | } |
| 4320 | |
| 4321 | // When inverting a swizzle with a parent op, this function |
| 4322 | // will apply the swizzle operation to a completed parent operation. |
| 4323 | spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, |
| 4324 | spv::Id parentResult) |
| 4325 | { |
| 4326 | std::vector<unsigned> swizzle; |
| 4327 | convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 4328 | return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 4329 | } |
| 4330 | |
| 4331 | // Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 4332 | void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 4333 | { |
| 4334 | const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 4335 | for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 4336 | swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 4337 | } |
| 4338 | |
| 4339 | // Convert from a glslang type to an SPV type, by calling into a |
| 4340 | // recursive version of this function. This establishes the inherited |
| 4341 | // layout state rooted from the top-level type. |
| 4342 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
| 4343 | { |
| 4344 | return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
| 4345 | } |
| 4346 | |
| 4347 | // Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id. |
| 4348 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 4349 | // Mutually recursive with convertGlslangStructToSpvType(). |
| 4350 | spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
| 4351 | glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 4352 | bool lastBufferBlockMember, bool forwardReferenceOnly) |
| 4353 | { |
| 4354 | spv::Id spvType = spv::NoResult; |
| 4355 | |
| 4356 | switch (type.getBasicType()) { |
| 4357 | case glslang::EbtVoid: |
| 4358 | spvType = builder.makeVoidType(); |
| 4359 | assert (! type.isArray()); |
| 4360 | break; |
| 4361 | case glslang::EbtBool: |
| 4362 | // "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 4363 | // a 32-bit int where non-0 means true. |
| 4364 | if (explicitLayout != glslang::ElpNone) |
| 4365 | spvType = builder.makeUintType(32); |
| 4366 | else |
| 4367 | spvType = builder.makeBoolType(false); |
| 4368 | break; |
| 4369 | case glslang::EbtInt: |
| 4370 | spvType = builder.makeIntType(32); |
| 4371 | break; |
| 4372 | case glslang::EbtUint: |
| 4373 | spvType = builder.makeUintType(32); |
| 4374 | break; |
| 4375 | case glslang::EbtFloat: |
| 4376 | spvType = builder.makeFloatType(32); |
| 4377 | break; |
| 4378 | #ifndef GLSLANG_WEB |
| 4379 | case glslang::EbtDouble: |
| 4380 | spvType = builder.makeFloatType(64); |
| 4381 | break; |
| 4382 | case glslang::EbtFloat16: |
| 4383 | spvType = builder.makeFloatType(16); |
| 4384 | break; |
| 4385 | case glslang::EbtInt8: |
| 4386 | spvType = builder.makeIntType(8); |
| 4387 | break; |
| 4388 | case glslang::EbtUint8: |
| 4389 | spvType = builder.makeUintType(8); |
| 4390 | break; |
| 4391 | case glslang::EbtInt16: |
| 4392 | spvType = builder.makeIntType(16); |
| 4393 | break; |
| 4394 | case glslang::EbtUint16: |
| 4395 | spvType = builder.makeUintType(16); |
| 4396 | break; |
| 4397 | case glslang::EbtInt64: |
| 4398 | spvType = builder.makeIntType(64); |
| 4399 | break; |
| 4400 | case glslang::EbtUint64: |
| 4401 | spvType = builder.makeUintType(64); |
| 4402 | break; |
| 4403 | case glslang::EbtAtomicUint: |
| 4404 | builder.addCapability(spv::CapabilityAtomicStorage); |
| 4405 | spvType = builder.makeUintType(32); |
| 4406 | break; |
| 4407 | case glslang::EbtAccStruct: |
| 4408 | switch (glslangIntermediate->getStage()) { |
| 4409 | case EShLangRayGen: |
| 4410 | case EShLangIntersect: |
| 4411 | case EShLangAnyHit: |
| 4412 | case EShLangClosestHit: |
| 4413 | case EShLangMiss: |
| 4414 | case EShLangCallable: |
| 4415 | // these all should have the RayTracingNV/KHR capability already |
| 4416 | break; |
| 4417 | default: |
| 4418 | { |
| 4419 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 4420 | if (extensions.find("GL_EXT_ray_query" ) != extensions.end()) { |
| 4421 | builder.addExtension(spv::E_SPV_KHR_ray_query); |
| 4422 | builder.addCapability(spv::CapabilityRayQueryKHR); |
| 4423 | } |
| 4424 | } |
| 4425 | break; |
| 4426 | } |
| 4427 | spvType = builder.makeAccelerationStructureType(); |
| 4428 | break; |
| 4429 | case glslang::EbtRayQuery: |
| 4430 | { |
| 4431 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 4432 | if (extensions.find("GL_EXT_ray_query" ) != extensions.end()) { |
| 4433 | builder.addExtension(spv::E_SPV_KHR_ray_query); |
| 4434 | builder.addCapability(spv::CapabilityRayQueryKHR); |
| 4435 | } |
| 4436 | spvType = builder.makeRayQueryType(); |
| 4437 | } |
| 4438 | break; |
| 4439 | case glslang::EbtReference: |
| 4440 | { |
| 4441 | // Make the forward pointer, then recurse to convert the structure type, then |
| 4442 | // patch up the forward pointer with a real pointer type. |
| 4443 | if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 4444 | spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 4445 | forwardPointers[type.getReferentType()] = forwardId; |
| 4446 | } |
| 4447 | spvType = forwardPointers[type.getReferentType()]; |
| 4448 | if (!forwardReferenceOnly) { |
| 4449 | spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 4450 | builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 4451 | forwardPointers[type.getReferentType()], |
| 4452 | referentType); |
| 4453 | } |
| 4454 | } |
| 4455 | break; |
| 4456 | #endif |
| 4457 | case glslang::EbtSampler: |
| 4458 | { |
| 4459 | const glslang::TSampler& sampler = type.getSampler(); |
| 4460 | if (sampler.isPureSampler()) { |
| 4461 | spvType = builder.makeSamplerType(); |
| 4462 | } else { |
| 4463 | // an image is present, make its type |
| 4464 | spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), |
| 4465 | sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), |
| 4466 | sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type)); |
| 4467 | if (sampler.isCombined() && |
| 4468 | (!sampler.isBuffer() || glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_6)) { |
| 4469 | // Already has both image and sampler, make the combined type. Only combine sampler to |
| 4470 | // buffer if before SPIR-V 1.6. |
| 4471 | spvType = builder.makeSampledImageType(spvType); |
| 4472 | } |
| 4473 | } |
| 4474 | } |
| 4475 | break; |
| 4476 | case glslang::EbtStruct: |
| 4477 | case glslang::EbtBlock: |
| 4478 | { |
| 4479 | // If we've seen this struct type, return it |
| 4480 | const glslang::TTypeList* glslangMembers = type.getStruct(); |
| 4481 | |
| 4482 | // Try to share structs for different layouts, but not yet for other |
| 4483 | // kinds of qualification (primarily not yet including interpolant qualification). |
| 4484 | if (! HasNonLayoutQualifiers(type, qualifier)) |
| 4485 | spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
| 4486 | if (spvType != spv::NoResult) |
| 4487 | break; |
| 4488 | |
| 4489 | // else, we haven't seen it... |
| 4490 | if (type.getBasicType() == glslang::EbtBlock) |
| 4491 | memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(glslangMembers->size()); |
| 4492 | spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
| 4493 | } |
| 4494 | break; |
| 4495 | case glslang::EbtString: |
| 4496 | // no type used for OpString |
| 4497 | return 0; |
| 4498 | |
| 4499 | case glslang::EbtHitObjectNV: { |
| 4500 | builder.addExtension(spv::E_SPV_NV_shader_invocation_reorder); |
| 4501 | builder.addCapability(spv::CapabilityShaderInvocationReorderNV); |
| 4502 | spvType = builder.makeHitObjectNVType(); |
| 4503 | } |
| 4504 | break; |
| 4505 | #ifndef GLSLANG_WEB |
| 4506 | case glslang::EbtSpirvType: { |
| 4507 | // GL_EXT_spirv_intrinsics |
| 4508 | const auto& spirvType = type.getSpirvType(); |
| 4509 | const auto& spirvInst = spirvType.spirvInst; |
| 4510 | |
| 4511 | std::vector<spv::IdImmediate> operands; |
| 4512 | for (const auto& typeParam : spirvType.typeParams) { |
| 4513 | // Constant expression |
| 4514 | if (typeParam.constant->isLiteral()) { |
| 4515 | if (typeParam.constant->getBasicType() == glslang::EbtFloat) { |
| 4516 | float floatValue = static_cast<float>(typeParam.constant->getConstArray()[0].getDConst()); |
| 4517 | unsigned literal; |
| 4518 | static_assert(sizeof(literal) == sizeof(floatValue), "sizeof(unsigned) != sizeof(float)" ); |
| 4519 | memcpy(&literal, &floatValue, sizeof(literal)); |
| 4520 | operands.push_back({false, literal}); |
| 4521 | } else if (typeParam.constant->getBasicType() == glslang::EbtInt) { |
| 4522 | unsigned literal = typeParam.constant->getConstArray()[0].getIConst(); |
| 4523 | operands.push_back({false, literal}); |
| 4524 | } else if (typeParam.constant->getBasicType() == glslang::EbtUint) { |
| 4525 | unsigned literal = typeParam.constant->getConstArray()[0].getUConst(); |
| 4526 | operands.push_back({false, literal}); |
| 4527 | } else if (typeParam.constant->getBasicType() == glslang::EbtBool) { |
| 4528 | unsigned literal = typeParam.constant->getConstArray()[0].getBConst(); |
| 4529 | operands.push_back({false, literal}); |
| 4530 | } else if (typeParam.constant->getBasicType() == glslang::EbtString) { |
| 4531 | auto str = typeParam.constant->getConstArray()[0].getSConst()->c_str(); |
| 4532 | unsigned literal = 0; |
| 4533 | char* literalPtr = reinterpret_cast<char*>(&literal); |
| 4534 | unsigned charCount = 0; |
| 4535 | char ch = 0; |
| 4536 | do { |
| 4537 | ch = *(str++); |
| 4538 | *(literalPtr++) = ch; |
| 4539 | ++charCount; |
| 4540 | if (charCount == 4) { |
| 4541 | operands.push_back({false, literal}); |
| 4542 | literalPtr = reinterpret_cast<char*>(&literal); |
| 4543 | charCount = 0; |
| 4544 | } |
| 4545 | } while (ch != 0); |
| 4546 | |
| 4547 | // Partial literal is padded with 0 |
| 4548 | if (charCount > 0) { |
| 4549 | for (; charCount < 4; ++charCount) |
| 4550 | *(literalPtr++) = 0; |
| 4551 | operands.push_back({false, literal}); |
| 4552 | } |
| 4553 | } else |
| 4554 | assert(0); // Unexpected type |
| 4555 | } else |
| 4556 | operands.push_back({true, createSpvConstant(*typeParam.constant)}); |
| 4557 | } |
| 4558 | |
| 4559 | assert(spirvInst.set == "" ); // Currently, couldn't be extended instructions. |
| 4560 | spvType = builder.makeGenericType(static_cast<spv::Op>(spirvInst.id), operands); |
| 4561 | |
| 4562 | break; |
| 4563 | } |
| 4564 | #endif |
| 4565 | default: |
| 4566 | assert(0); |
| 4567 | break; |
| 4568 | } |
| 4569 | |
| 4570 | if (type.isMatrix()) |
| 4571 | spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 4572 | else { |
| 4573 | // If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 4574 | if (type.getVectorSize() > 1) |
| 4575 | spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 4576 | } |
| 4577 | |
| 4578 | if (type.isCoopMat()) { |
| 4579 | builder.addCapability(spv::CapabilityCooperativeMatrixNV); |
| 4580 | builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
| 4581 | if (type.getBasicType() == glslang::EbtFloat16) |
| 4582 | builder.addCapability(spv::CapabilityFloat16); |
| 4583 | if (type.getBasicType() == glslang::EbtUint8 || |
| 4584 | type.getBasicType() == glslang::EbtInt8) { |
| 4585 | builder.addCapability(spv::CapabilityInt8); |
| 4586 | } |
| 4587 | |
| 4588 | spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1); |
| 4589 | spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2); |
| 4590 | spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3); |
| 4591 | |
| 4592 | spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols); |
| 4593 | } |
| 4594 | |
| 4595 | if (type.isArray()) { |
| 4596 | int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 4597 | |
| 4598 | // Do all but the outer dimension |
| 4599 | if (type.getArraySizes()->getNumDims() > 1) { |
| 4600 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 4601 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
| 4602 | // Use a dummy glslang type for querying internal strides of |
| 4603 | // arrays of arrays, but using just a one-dimensional array. |
| 4604 | glslang::TType simpleArrayType(type, 0); // deference type of the array |
| 4605 | while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 4606 | simpleArrayType.getArraySizes()->dereference(); |
| 4607 | |
| 4608 | // Will compute the higher-order strides here, rather than making a whole |
| 4609 | // pile of types and doing repetitive recursion on their contents. |
| 4610 | stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 4611 | } |
| 4612 | |
| 4613 | // make the arrays |
| 4614 | for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
| 4615 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
| 4616 | if (stride > 0) |
| 4617 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
| 4618 | stride *= type.getArraySizes()->getDimSize(dim); |
| 4619 | } |
| 4620 | } else { |
| 4621 | // single-dimensional array, and don't yet have stride |
| 4622 | |
| 4623 | // We need to decorate array strides for types needing explicit layout, except blocks. |
| 4624 | if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 4625 | stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
| 4626 | } |
| 4627 | |
| 4628 | // Do the outer dimension, which might not be known for a runtime-sized array. |
| 4629 | // (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 4630 | if (type.isSizedArray()) |
| 4631 | spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
| 4632 | else { |
| 4633 | #ifndef GLSLANG_WEB |
| 4634 | if (!lastBufferBlockMember) { |
| 4635 | builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing" , spv::Spv_1_5); |
| 4636 | builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 4637 | } |
| 4638 | #endif |
| 4639 | spvType = builder.makeRuntimeArray(spvType); |
| 4640 | } |
| 4641 | if (stride > 0) |
| 4642 | builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
| 4643 | } |
| 4644 | |
| 4645 | return spvType; |
| 4646 | } |
| 4647 | |
| 4648 | // TODO: this functionality should exist at a higher level, in creating the AST |
| 4649 | // |
| 4650 | // Identify interface members that don't have their required extension turned on. |
| 4651 | // |
| 4652 | bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 4653 | { |
| 4654 | #ifndef GLSLANG_WEB |
| 4655 | auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 4656 | |
| 4657 | if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 4658 | extensions.find("GL_NV_stereo_view_rendering" ) == extensions.end()) |
| 4659 | return true; |
| 4660 | if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 4661 | extensions.find("GL_NV_stereo_view_rendering" ) == extensions.end()) |
| 4662 | return true; |
| 4663 | |
| 4664 | if (glslangIntermediate->getStage() != EShLangMesh) { |
| 4665 | if (member.getFieldName() == "gl_ViewportMask" && |
| 4666 | extensions.find("GL_NV_viewport_array2" ) == extensions.end()) |
| 4667 | return true; |
| 4668 | if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 4669 | extensions.find("GL_NVX_multiview_per_view_attributes" ) == extensions.end()) |
| 4670 | return true; |
| 4671 | if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 4672 | extensions.find("GL_NVX_multiview_per_view_attributes" ) == extensions.end()) |
| 4673 | return true; |
| 4674 | } |
| 4675 | #endif |
| 4676 | |
| 4677 | return false; |
| 4678 | }; |
| 4679 | |
| 4680 | // Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 4681 | // explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 4682 | // Mutually recursive with convertGlslangToSpvType(). |
| 4683 | spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 4684 | const glslang::TTypeList* glslangMembers, |
| 4685 | glslang::TLayoutPacking explicitLayout, |
| 4686 | const glslang::TQualifier& qualifier) |
| 4687 | { |
| 4688 | // Create a vector of struct types for SPIR-V to consume |
| 4689 | std::vector<spv::Id> spvMembers; |
| 4690 | int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, |
| 4691 | // except sometimes for blocks |
| 4692 | std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
| 4693 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 4694 | auto& glslangMember = (*glslangMembers)[i]; |
| 4695 | if (glslangMember.type->hiddenMember()) { |
| 4696 | ++memberDelta; |
| 4697 | if (type.getBasicType() == glslang::EbtBlock) |
| 4698 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
| 4699 | } else { |
| 4700 | if (type.getBasicType() == glslang::EbtBlock) { |
| 4701 | if (filterMember(*glslangMember.type)) { |
| 4702 | memberDelta++; |
| 4703 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
| 4704 | continue; |
| 4705 | } |
| 4706 | memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta; |
| 4707 | } |
| 4708 | // modify just this child's view of the qualifier |
| 4709 | glslang::TQualifier memberQualifier = glslangMember.type->getQualifier(); |
| 4710 | InheritQualifiers(memberQualifier, qualifier); |
| 4711 | |
| 4712 | // manually inherit location |
| 4713 | if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
| 4714 | memberQualifier.layoutLocation = qualifier.layoutLocation; |
| 4715 | |
| 4716 | // recurse |
| 4717 | bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 4718 | i == (int)glslangMembers->size() - 1; |
| 4719 | |
| 4720 | // Make forward pointers for any pointer members. |
| 4721 | if (glslangMember.type->isReference() && |
| 4722 | forwardPointers.find(glslangMember.type->getReferentType()) == forwardPointers.end()) { |
| 4723 | deferredForwardPointers.push_back(std::make_pair(glslangMember.type, memberQualifier)); |
| 4724 | } |
| 4725 | |
| 4726 | // Create the member type. |
| 4727 | auto const spvMember = convertGlslangToSpvType(*glslangMember.type, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 4728 | glslangMember.type->isReference()); |
| 4729 | spvMembers.push_back(spvMember); |
| 4730 | |
| 4731 | // Update the builder with the type's location so that we can create debug types for the structure members. |
| 4732 | // There doesn't exist a "clean" entry point for this information to be passed along to the builder so, for now, |
| 4733 | // it is stored in the builder and consumed during the construction of composite debug types. |
| 4734 | // TODO: This probably warrants further investigation. This approach was decided to be the least ugly of the |
| 4735 | // quick and dirty approaches that were tried. |
| 4736 | // Advantages of this approach: |
| 4737 | // + Relatively clean. No direct calls into debug type system. |
| 4738 | // + Handles nested recursive structures. |
| 4739 | // Disadvantages of this approach: |
| 4740 | // + Not as clean as desired. Traverser queries/sets persistent state. This is fragile. |
| 4741 | // + Table lookup during creation of composite debug types. This really shouldn't be necessary. |
| 4742 | if(options.emitNonSemanticShaderDebugInfo) { |
| 4743 | builder.debugTypeLocs[spvMember].name = glslangMember.type->getFieldName().c_str(); |
| 4744 | builder.debugTypeLocs[spvMember].line = glslangMember.loc.line; |
| 4745 | builder.debugTypeLocs[spvMember].column = glslangMember.loc.column; |
| 4746 | } |
| 4747 | } |
| 4748 | } |
| 4749 | |
| 4750 | // Make the SPIR-V type |
| 4751 | spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str(), false); |
| 4752 | if (! HasNonLayoutQualifiers(type, qualifier)) |
| 4753 | structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 4754 | |
| 4755 | // Decorate it |
| 4756 | decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType, spvMembers); |
| 4757 | |
| 4758 | for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
| 4759 | auto it = deferredForwardPointers[i]; |
| 4760 | convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 4761 | } |
| 4762 | |
| 4763 | return spvType; |
| 4764 | } |
| 4765 | |
| 4766 | void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 4767 | const glslang::TTypeList* glslangMembers, |
| 4768 | glslang::TLayoutPacking explicitLayout, |
| 4769 | const glslang::TQualifier& qualifier, |
| 4770 | spv::Id spvType, |
| 4771 | const std::vector<spv::Id>& spvMembers) |
| 4772 | { |
| 4773 | // Name and decorate the non-hidden members |
| 4774 | int offset = -1; |
| 4775 | bool memberLocationInvalid = type.isArrayOfArrays() || |
| 4776 | (type.isArray() && (type.getQualifier().isArrayedIo(glslangIntermediate->getStage()) == false)); |
| 4777 | for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 4778 | glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 4779 | int member = i; |
| 4780 | if (type.getBasicType() == glslang::EbtBlock) { |
| 4781 | member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i]; |
| 4782 | if (filterMember(glslangMember)) |
| 4783 | continue; |
| 4784 | } |
| 4785 | |
| 4786 | // modify just this child's view of the qualifier |
| 4787 | glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 4788 | InheritQualifiers(memberQualifier, qualifier); |
| 4789 | |
| 4790 | // using -1 above to indicate a hidden member |
| 4791 | if (member < 0) |
| 4792 | continue; |
| 4793 | |
| 4794 | builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 4795 | builder.addMemberDecoration(spvType, member, |
| 4796 | TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 4797 | builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 4798 | // Add interpolation and auxiliary storage decorations only to |
| 4799 | // top-level members of Input and Output storage classes |
| 4800 | if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 4801 | type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 4802 | if (type.getBasicType() == glslang::EbtBlock || |
| 4803 | glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 4804 | builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 4805 | builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
| 4806 | #ifndef GLSLANG_WEB |
| 4807 | addMeshNVDecoration(spvType, member, memberQualifier); |
| 4808 | #endif |
| 4809 | } |
| 4810 | } |
| 4811 | builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
| 4812 | |
| 4813 | #ifndef GLSLANG_WEB |
| 4814 | if (type.getBasicType() == glslang::EbtBlock && |
| 4815 | qualifier.storage == glslang::EvqBuffer) { |
| 4816 | // Add memory decorations only to top-level members of shader storage block |
| 4817 | std::vector<spv::Decoration> memory; |
| 4818 | TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
| 4819 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 4820 | builder.addMemberDecoration(spvType, member, memory[i]); |
| 4821 | } |
| 4822 | |
| 4823 | #endif |
| 4824 | |
| 4825 | // Location assignment was already completed correctly by the front end, |
| 4826 | // just track whether a member needs to be decorated. |
| 4827 | // Ignore member locations if the container is an array, as that's |
| 4828 | // ill-specified and decisions have been made to not allow this. |
| 4829 | if (!memberLocationInvalid && memberQualifier.hasLocation()) |
| 4830 | builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
| 4831 | |
| 4832 | // component, XFB, others |
| 4833 | if (glslangMember.getQualifier().hasComponent()) |
| 4834 | builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 4835 | glslangMember.getQualifier().layoutComponent); |
| 4836 | if (glslangMember.getQualifier().hasXfbOffset()) |
| 4837 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 4838 | glslangMember.getQualifier().layoutXfbOffset); |
| 4839 | else if (explicitLayout != glslang::ElpNone) { |
| 4840 | // figure out what to do with offset, which is accumulating |
| 4841 | int nextOffset; |
| 4842 | updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 4843 | if (offset >= 0) |
| 4844 | builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 4845 | offset = nextOffset; |
| 4846 | } |
| 4847 | |
| 4848 | if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 4849 | builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 4850 | getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
| 4851 | |
| 4852 | // built-in variable decorations |
| 4853 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 4854 | if (builtIn != spv::BuiltInMax) |
| 4855 | builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
| 4856 | |
| 4857 | #ifndef GLSLANG_WEB |
| 4858 | // nonuniform |
| 4859 | builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 4860 | |
| 4861 | if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 4862 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1" ); |
| 4863 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 4864 | memberQualifier.semanticName); |
| 4865 | } |
| 4866 | |
| 4867 | if (builtIn == spv::BuiltInLayer) { |
| 4868 | // SPV_NV_viewport_array2 extension |
| 4869 | if (glslangMember.getQualifier().layoutViewportRelative){ |
| 4870 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 4871 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 4872 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 4873 | } |
| 4874 | if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 4875 | builder.addMemberDecoration(spvType, member, |
| 4876 | (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 4877 | glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 4878 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 4879 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 4880 | } |
| 4881 | } |
| 4882 | if (glslangMember.getQualifier().layoutPassthrough) { |
| 4883 | builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 4884 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 4885 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 4886 | } |
| 4887 | |
| 4888 | // |
| 4889 | // Add SPIR-V decorations for members (GL_EXT_spirv_intrinsics) |
| 4890 | // |
| 4891 | if (glslangMember.getQualifier().hasSprivDecorate()) { |
| 4892 | const glslang::TSpirvDecorate& spirvDecorate = glslangMember.getQualifier().getSpirvDecorate(); |
| 4893 | |
| 4894 | // Add spirv_decorate |
| 4895 | for (auto& decorate : spirvDecorate.decorates) { |
| 4896 | if (!decorate.second.empty()) { |
| 4897 | std::vector<unsigned> literals; |
| 4898 | TranslateLiterals(decorate.second, literals); |
| 4899 | builder.addMemberDecoration(spvType, member, static_cast<spv::Decoration>(decorate.first), literals); |
| 4900 | } |
| 4901 | else |
| 4902 | builder.addMemberDecoration(spvType, member, static_cast<spv::Decoration>(decorate.first)); |
| 4903 | } |
| 4904 | |
| 4905 | // spirv_decorate_id not applied to members |
| 4906 | assert(spirvDecorate.decorateIds.empty()); |
| 4907 | |
| 4908 | // Add spirv_decorate_string |
| 4909 | for (auto& decorateString : spirvDecorate.decorateStrings) { |
| 4910 | std::vector<const char*> strings; |
| 4911 | assert(!decorateString.second.empty()); |
| 4912 | for (auto extraOperand : decorateString.second) { |
| 4913 | const char* string = extraOperand->getConstArray()[0].getSConst()->c_str(); |
| 4914 | strings.push_back(string); |
| 4915 | } |
| 4916 | builder.addDecoration(spvType, static_cast<spv::Decoration>(decorateString.first), strings); |
| 4917 | } |
| 4918 | } |
| 4919 | #endif |
| 4920 | } |
| 4921 | |
| 4922 | // Decorate the structure |
| 4923 | builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 4924 | const auto basicType = type.getBasicType(); |
| 4925 | const auto typeStorageQualifier = type.getQualifier().storage; |
| 4926 | if (basicType == glslang::EbtBlock) { |
| 4927 | builder.addDecoration(spvType, TranslateBlockDecoration(typeStorageQualifier, glslangIntermediate->usingStorageBuffer())); |
| 4928 | } else if (basicType == glslang::EbtStruct && glslangIntermediate->getSpv().vulkan > 0) { |
| 4929 | const auto hasRuntimeArray = !spvMembers.empty() && builder.getOpCode(spvMembers.back()) == spv::OpTypeRuntimeArray; |
| 4930 | if (hasRuntimeArray) { |
| 4931 | builder.addDecoration(spvType, TranslateBlockDecoration(typeStorageQualifier, glslangIntermediate->usingStorageBuffer())); |
| 4932 | } |
| 4933 | } |
| 4934 | |
| 4935 | if (qualifier.hasHitObjectShaderRecordNV()) |
| 4936 | builder.addDecoration(spvType, spv::DecorationHitObjectShaderRecordBufferNV); |
| 4937 | } |
| 4938 | |
| 4939 | // Turn the expression forming the array size into an id. |
| 4940 | // This is not quite trivial, because of specialization constants. |
| 4941 | // Sometimes, a raw constant is turned into an Id, and sometimes |
| 4942 | // a specialization constant expression is. |
| 4943 | spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 4944 | { |
| 4945 | // First, see if this is sized with a node, meaning a specialization constant: |
| 4946 | glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 4947 | if (specNode != nullptr) { |
| 4948 | builder.clearAccessChain(); |
| 4949 | SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 4950 | spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 4951 | specNode->traverse(this); |
| 4952 | return accessChainLoad(specNode->getAsTyped()->getType()); |
| 4953 | } |
| 4954 | |
| 4955 | // Otherwise, need a compile-time (front end) size, get it: |
| 4956 | int size = arraySizes.getDimSize(dim); |
| 4957 | assert(size > 0); |
| 4958 | return builder.makeUintConstant(size); |
| 4959 | } |
| 4960 | |
| 4961 | // Wrap the builder's accessChainLoad to: |
| 4962 | // - localize handling of RelaxedPrecision |
| 4963 | // - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 4964 | // (avoids unnecessary work and possible type punning for structures) |
| 4965 | // - do conversion of concrete to abstract type |
| 4966 | spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 4967 | { |
| 4968 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 4969 | |
| 4970 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4971 | coherentFlags |= TranslateCoherent(type); |
| 4972 | |
| 4973 | spv::MemoryAccessMask accessMask = spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask); |
| 4974 | // If the value being loaded is HelperInvocation, SPIR-V 1.6 is being generated (so that |
| 4975 | // SPV_EXT_demote_to_helper_invocation is in core) and the memory model is in use, add |
| 4976 | // the Volatile MemoryAccess semantic. |
| 4977 | if (type.getQualifier().builtIn == glslang::EbvHelperInvocation && |
| 4978 | glslangIntermediate->usingVulkanMemoryModel() && |
| 4979 | glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
| 4980 | accessMask = spv::MemoryAccessMask(accessMask | spv::MemoryAccessVolatileMask); |
| 4981 | } |
| 4982 | |
| 4983 | unsigned int alignment = builder.getAccessChain().alignment; |
| 4984 | alignment |= type.getBufferReferenceAlignment(); |
| 4985 | |
| 4986 | spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
| 4987 | TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags), |
| 4988 | TranslateNonUniformDecoration(type.getQualifier()), |
| 4989 | nominalTypeId, |
| 4990 | accessMask, |
| 4991 | TranslateMemoryScope(coherentFlags), |
| 4992 | alignment); |
| 4993 | |
| 4994 | // Need to convert to abstract types when necessary |
| 4995 | if (type.getBasicType() == glslang::EbtBool) { |
| 4996 | loadedId = convertLoadedBoolInUniformToUint(type, nominalTypeId, loadedId); |
| 4997 | } |
| 4998 | |
| 4999 | return loadedId; |
| 5000 | } |
| 5001 | |
| 5002 | // Wrap the builder's accessChainStore to: |
| 5003 | // - do conversion of concrete to abstract type |
| 5004 | // |
| 5005 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 5006 | void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 5007 | { |
| 5008 | // Need to convert to abstract types when necessary |
| 5009 | if (type.getBasicType() == glslang::EbtBool) { |
| 5010 | spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 5011 | |
| 5012 | if (builder.isScalarType(nominalTypeId)) { |
| 5013 | // Conversion for bool |
| 5014 | spv::Id boolType = builder.makeBoolType(); |
| 5015 | if (nominalTypeId != boolType) { |
| 5016 | // keep these outside arguments, for determinant order-of-evaluation |
| 5017 | spv::Id one = builder.makeUintConstant(1); |
| 5018 | spv::Id zero = builder.makeUintConstant(0); |
| 5019 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 5020 | } else if (builder.getTypeId(rvalue) != boolType) |
| 5021 | rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
| 5022 | } else if (builder.isVectorType(nominalTypeId)) { |
| 5023 | // Conversion for bvec |
| 5024 | int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 5025 | spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 5026 | if (nominalTypeId != bvecType) { |
| 5027 | // keep these outside arguments, for determinant order-of-evaluation |
| 5028 | spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 5029 | spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 5030 | rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 5031 | } else if (builder.getTypeId(rvalue) != bvecType) |
| 5032 | rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 5033 | makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 5034 | } |
| 5035 | } |
| 5036 | |
| 5037 | spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 5038 | coherentFlags |= TranslateCoherent(type); |
| 5039 | |
| 5040 | unsigned int alignment = builder.getAccessChain().alignment; |
| 5041 | alignment |= type.getBufferReferenceAlignment(); |
| 5042 | |
| 5043 | builder.accessChainStore(rvalue, TranslateNonUniformDecoration(builder.getAccessChain().coherentFlags), |
| 5044 | spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & |
| 5045 | ~spv::MemoryAccessMakePointerVisibleKHRMask), |
| 5046 | TranslateMemoryScope(coherentFlags), alignment); |
| 5047 | } |
| 5048 | |
| 5049 | // For storing when types match at the glslang level, but not might match at the |
| 5050 | // SPIR-V level. |
| 5051 | // |
| 5052 | // This especially happens when a single glslang type expands to multiple |
| 5053 | // SPIR-V types, like a struct that is used in a member-undecorated way as well |
| 5054 | // as in a member-decorated way. |
| 5055 | // |
| 5056 | // NOTE: This function can handle any store request; if it's not special it |
| 5057 | // simplifies to a simple OpStore. |
| 5058 | // |
| 5059 | // Implicitly uses the existing builder.accessChain as the storage target. |
| 5060 | void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 5061 | { |
| 5062 | // we only do the complex path here if it's an aggregate |
| 5063 | if (! type.isStruct() && ! type.isArray()) { |
| 5064 | accessChainStore(type, rValue); |
| 5065 | return; |
| 5066 | } |
| 5067 | |
| 5068 | // and, it has to be a case of type aliasing |
| 5069 | spv::Id rType = builder.getTypeId(rValue); |
| 5070 | spv::Id lValue = builder.accessChainGetLValue(); |
| 5071 | spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 5072 | if (lType == rType) { |
| 5073 | accessChainStore(type, rValue); |
| 5074 | return; |
| 5075 | } |
| 5076 | |
| 5077 | // Recursively (as needed) copy an aggregate type to a different aggregate type, |
| 5078 | // where the two types were the same type in GLSL. This requires member |
| 5079 | // by member copy, recursively. |
| 5080 | |
| 5081 | // SPIR-V 1.4 added an instruction to do help do this. |
| 5082 | if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 5083 | // However, bool in uniform space is changed to int, so |
| 5084 | // OpCopyLogical does not work for that. |
| 5085 | // TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
| 5086 | bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0); |
| 5087 | bool lBool = builder.containsType(lType, spv::OpTypeBool, 0); |
| 5088 | if (lBool == rBool) { |
| 5089 | spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue); |
| 5090 | accessChainStore(type, logicalCopy); |
| 5091 | return; |
| 5092 | } |
| 5093 | } |
| 5094 | |
| 5095 | // If an array, copy element by element. |
| 5096 | if (type.isArray()) { |
| 5097 | glslang::TType glslangElementType(type, 0); |
| 5098 | spv::Id elementRType = builder.getContainedTypeId(rType); |
| 5099 | for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 5100 | // get the source member |
| 5101 | spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
| 5102 | |
| 5103 | // set up the target storage |
| 5104 | builder.clearAccessChain(); |
| 5105 | builder.setAccessChainLValue(lValue); |
| 5106 | builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), |
| 5107 | type.getBufferReferenceAlignment()); |
| 5108 | |
| 5109 | // store the member |
| 5110 | multiTypeStore(glslangElementType, elementRValue); |
| 5111 | } |
| 5112 | } else { |
| 5113 | assert(type.isStruct()); |
| 5114 | |
| 5115 | // loop over structure members |
| 5116 | const glslang::TTypeList& members = *type.getStruct(); |
| 5117 | for (int m = 0; m < (int)members.size(); ++m) { |
| 5118 | const glslang::TType& glslangMemberType = *members[m].type; |
| 5119 | |
| 5120 | // get the source member |
| 5121 | spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 5122 | spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 5123 | |
| 5124 | // set up the target storage |
| 5125 | builder.clearAccessChain(); |
| 5126 | builder.setAccessChainLValue(lValue); |
| 5127 | builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), |
| 5128 | type.getBufferReferenceAlignment()); |
| 5129 | |
| 5130 | // store the member |
| 5131 | multiTypeStore(glslangMemberType, memberRValue); |
| 5132 | } |
| 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | // Decide whether or not this type should be |
| 5137 | // decorated with offsets and strides, and if so |
| 5138 | // whether std140 or std430 rules should be applied. |
| 5139 | glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
| 5140 | { |
| 5141 | // has to be a block |
| 5142 | if (type.getBasicType() != glslang::EbtBlock) |
| 5143 | return glslang::ElpNone; |
| 5144 | |
| 5145 | // has to be a uniform or buffer block or task in/out blocks |
| 5146 | if (type.getQualifier().storage != glslang::EvqUniform && |
| 5147 | type.getQualifier().storage != glslang::EvqBuffer && |
| 5148 | type.getQualifier().storage != glslang::EvqShared && |
| 5149 | !type.getQualifier().isTaskMemory()) |
| 5150 | return glslang::ElpNone; |
| 5151 | |
| 5152 | // return the layout to use |
| 5153 | switch (type.getQualifier().layoutPacking) { |
| 5154 | case glslang::ElpStd140: |
| 5155 | case glslang::ElpStd430: |
| 5156 | case glslang::ElpScalar: |
| 5157 | return type.getQualifier().layoutPacking; |
| 5158 | default: |
| 5159 | return glslang::ElpNone; |
| 5160 | } |
| 5161 | } |
| 5162 | |
| 5163 | // Given an array type, returns the integer stride required for that array |
| 5164 | int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, |
| 5165 | glslang::TLayoutMatrix matrixLayout) |
| 5166 | { |
| 5167 | int size; |
| 5168 | int stride; |
| 5169 | glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, |
| 5170 | matrixLayout == glslang::ElmRowMajor); |
| 5171 | |
| 5172 | return stride; |
| 5173 | } |
| 5174 | |
| 5175 | // Given a matrix type, or array (of array) of matrixes type, returns the integer stride required for that matrix |
| 5176 | // when used as a member of an interface block |
| 5177 | int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, |
| 5178 | glslang::TLayoutMatrix matrixLayout) |
| 5179 | { |
| 5180 | glslang::TType elementType; |
| 5181 | elementType.shallowCopy(matrixType); |
| 5182 | elementType.clearArraySizes(); |
| 5183 | |
| 5184 | int size; |
| 5185 | int stride; |
| 5186 | glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, |
| 5187 | matrixLayout == glslang::ElmRowMajor); |
| 5188 | |
| 5189 | return stride; |
| 5190 | } |
| 5191 | |
| 5192 | // Given a member type of a struct, realign the current offset for it, and compute |
| 5193 | // the next (not yet aligned) offset for the next member, which will get aligned |
| 5194 | // on the next call. |
| 5195 | // 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 5196 | // the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 5197 | // -1 means a non-forced member offset (no decoration needed). |
| 5198 | void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, |
| 5199 | int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
| 5200 | { |
| 5201 | // this will get a positive value when deemed necessary |
| 5202 | nextOffset = -1; |
| 5203 | |
| 5204 | // override anything in currentOffset with user-set offset |
| 5205 | if (memberType.getQualifier().hasOffset()) |
| 5206 | currentOffset = memberType.getQualifier().layoutOffset; |
| 5207 | |
| 5208 | // It could be that current linker usage in glslang updated all the layoutOffset, |
| 5209 | // in which case the following code does not matter. But, that's not quite right |
| 5210 | // once cross-compilation unit GLSL validation is done, as the original user |
| 5211 | // settings are needed in layoutOffset, and then the following will come into play. |
| 5212 | |
| 5213 | if (explicitLayout == glslang::ElpNone) { |
| 5214 | if (! memberType.getQualifier().hasOffset()) |
| 5215 | currentOffset = -1; |
| 5216 | |
| 5217 | return; |
| 5218 | } |
| 5219 | |
| 5220 | // Getting this far means we need explicit offsets |
| 5221 | if (currentOffset < 0) |
| 5222 | currentOffset = 0; |
| 5223 | |
| 5224 | // Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 5225 | // but possibly not yet correctly aligned. |
| 5226 | |
| 5227 | int memberSize; |
| 5228 | int dummyStride; |
| 5229 | int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, |
| 5230 | matrixLayout == glslang::ElmRowMajor); |
| 5231 | |
| 5232 | // Adjust alignment for HLSL rules |
| 5233 | // TODO: make this consistent in early phases of code: |
| 5234 | // adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 5235 | // Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 5236 | // which is the most likely to rely on reflection, and least likely to rely implicit layouts |
| 5237 | if (glslangIntermediate->usingHlslOffsets() && |
| 5238 | ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global" ) != 0) { |
| 5239 | int dummySize; |
| 5240 | int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 5241 | if (componentAlignment <= 4) |
| 5242 | memberAlignment = componentAlignment; |
| 5243 | } |
| 5244 | |
| 5245 | // Bump up to member alignment |
| 5246 | glslang::RoundToPow2(currentOffset, memberAlignment); |
| 5247 | |
| 5248 | // Bump up to vec4 if there is a bad straddle |
| 5249 | if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, |
| 5250 | currentOffset)) |
| 5251 | glslang::RoundToPow2(currentOffset, 16); |
| 5252 | |
| 5253 | nextOffset = currentOffset + memberSize; |
| 5254 | } |
| 5255 | |
| 5256 | void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
| 5257 | { |
| 5258 | const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 5259 | switch (glslangBuiltIn) |
| 5260 | { |
| 5261 | case glslang::EbvPointSize: |
| 5262 | #ifndef GLSLANG_WEB |
| 5263 | case glslang::EbvClipDistance: |
| 5264 | case glslang::EbvCullDistance: |
| 5265 | case glslang::EbvViewportMaskNV: |
| 5266 | case glslang::EbvSecondaryPositionNV: |
| 5267 | case glslang::EbvSecondaryViewportMaskNV: |
| 5268 | case glslang::EbvPositionPerViewNV: |
| 5269 | case glslang::EbvViewportMaskPerViewNV: |
| 5270 | case glslang::EbvTaskCountNV: |
| 5271 | case glslang::EbvPrimitiveCountNV: |
| 5272 | case glslang::EbvPrimitiveIndicesNV: |
| 5273 | case glslang::EbvClipDistancePerViewNV: |
| 5274 | case glslang::EbvCullDistancePerViewNV: |
| 5275 | case glslang::EbvLayerPerViewNV: |
| 5276 | case glslang::EbvMeshViewCountNV: |
| 5277 | case glslang::EbvMeshViewIndicesNV: |
| 5278 | #endif |
| 5279 | // Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 5280 | // Alternately, we could just call this for any glslang built-in, since the |
| 5281 | // capability already guards against duplicates. |
| 5282 | TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 5283 | break; |
| 5284 | default: |
| 5285 | // Capabilities were already generated when the struct was declared. |
| 5286 | break; |
| 5287 | } |
| 5288 | } |
| 5289 | |
| 5290 | bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
| 5291 | { |
| 5292 | return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
| 5293 | } |
| 5294 | |
| 5295 | // Does parameter need a place to keep writes, separate from the original? |
| 5296 | // Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 5297 | // qualifiers such that we should have only in/out/inout/constreadonly here. |
| 5298 | bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
| 5299 | { |
| 5300 | assert(qualifier == glslang::EvqIn || |
| 5301 | qualifier == glslang::EvqOut || |
| 5302 | qualifier == glslang::EvqInOut || |
| 5303 | qualifier == glslang::EvqUniform || |
| 5304 | qualifier == glslang::EvqConstReadOnly); |
| 5305 | return qualifier != glslang::EvqConstReadOnly && |
| 5306 | qualifier != glslang::EvqUniform; |
| 5307 | } |
| 5308 | |
| 5309 | // Is parameter pass-by-original? |
| 5310 | bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 5311 | bool implicitThisParam) |
| 5312 | { |
| 5313 | if (implicitThisParam) // implicit this |
| 5314 | return true; |
| 5315 | if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
| 5316 | return paramType.getBasicType() == glslang::EbtBlock; |
| 5317 | return (paramType.containsOpaque() && !glslangIntermediate->getBindlessMode()) || // sampler, etc. |
| 5318 | #ifndef GLSLANG_WEB |
| 5319 | paramType.getQualifier().isSpirvByReference() || // spirv_by_reference |
| 5320 | #endif |
| 5321 | (paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 5322 | } |
| 5323 | |
| 5324 | // Make all the functions, skeletally, without actually visiting their bodies. |
| 5325 | void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 5326 | { |
| 5327 | const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, |
| 5328 | bool useVulkanMemoryModel) { |
| 5329 | spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 5330 | if (paramPrecision != spv::NoPrecision) |
| 5331 | decorations.push_back(paramPrecision); |
| 5332 | TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
| 5333 | if (type.isReference()) { |
| 5334 | // Original and non-writable params pass the pointer directly and |
| 5335 | // use restrict/aliased, others are stored to a pointer in Function |
| 5336 | // memory and use RestrictPointer/AliasedPointer. |
| 5337 | if (originalParam(type.getQualifier().storage, type, false) || |
| 5338 | !writableParam(type.getQualifier().storage)) { |
| 5339 | decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrict : |
| 5340 | spv::DecorationAliased); |
| 5341 | } else { |
| 5342 | decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrictPointerEXT : |
| 5343 | spv::DecorationAliasedPointerEXT); |
| 5344 | } |
| 5345 | } |
| 5346 | }; |
| 5347 | |
| 5348 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 5349 | glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
| 5350 | if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
| 5351 | continue; |
| 5352 | |
| 5353 | // We're on a user function. Set up the basic interface for the function now, |
| 5354 | // so that it's available to call. Translating the body will happen later. |
| 5355 | // |
| 5356 | // Typically (except for a "const in" parameter), an address will be passed to the |
| 5357 | // function. What it is an address of varies: |
| 5358 | // |
| 5359 | // - "in" parameters not marked as "const" can be written to without modifying the calling |
| 5360 | // argument so that write needs to be to a copy, hence the address of a copy works. |
| 5361 | // |
| 5362 | // - "const in" parameters can just be the r-value, as no writes need occur. |
| 5363 | // |
| 5364 | // - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 5365 | // GLSL has copy-in/copy-out semantics. They can be handled though with a pointer to a copy. |
| 5366 | |
| 5367 | std::vector<spv::Id> paramTypes; |
| 5368 | std::vector<char const*> paramNames; |
| 5369 | std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
| 5370 | glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 5371 | |
| 5372 | #ifdef ENABLE_HLSL |
| 5373 | bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 5374 | glslangIntermediate->implicitThisName; |
| 5375 | #else |
| 5376 | bool implicitThis = false; |
| 5377 | #endif |
| 5378 | |
| 5379 | paramDecorations.resize(parameters.size()); |
| 5380 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 5381 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 5382 | spv::Id typeId = convertGlslangToSpvType(paramType); |
| 5383 | if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
| 5384 | typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
| 5385 | else if (writableParam(paramType.getQualifier().storage)) |
| 5386 | typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 5387 | else |
| 5388 | rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
| 5389 | getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
| 5390 | paramTypes.push_back(typeId); |
| 5391 | } |
| 5392 | |
| 5393 | for (auto const parameter:parameters) { |
| 5394 | paramNames.push_back(parameter->getAsSymbolNode()->getName().c_str()); |
| 5395 | } |
| 5396 | |
| 5397 | spv::Block* functionBlock; |
| 5398 | spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 5399 | convertGlslangToSpvType(glslFunction->getType()), |
| 5400 | glslFunction->getName().c_str(), paramTypes, paramNames, |
| 5401 | paramDecorations, &functionBlock); |
| 5402 | if (implicitThis) |
| 5403 | function->setImplicitThis(); |
| 5404 | |
| 5405 | // Track function to emit/call later |
| 5406 | functionMap[glslFunction->getName().c_str()] = function; |
| 5407 | |
| 5408 | // Set the parameter id's |
| 5409 | for (int p = 0; p < (int)parameters.size(); ++p) { |
| 5410 | symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 5411 | // give a name too |
| 5412 | builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 5413 | |
| 5414 | const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 5415 | if (paramType.contains8BitInt()) |
| 5416 | builder.addCapability(spv::CapabilityInt8); |
| 5417 | if (paramType.contains16BitInt()) |
| 5418 | builder.addCapability(spv::CapabilityInt16); |
| 5419 | if (paramType.contains16BitFloat()) |
| 5420 | builder.addCapability(spv::CapabilityFloat16); |
| 5421 | } |
| 5422 | } |
| 5423 | } |
| 5424 | |
| 5425 | // Process all the initializers, while skipping the functions and link objects |
| 5426 | void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 5427 | { |
| 5428 | builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 5429 | for (int i = 0; i < (int)initializers.size(); ++i) { |
| 5430 | glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 5431 | if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != |
| 5432 | glslang::EOpLinkerObjects) { |
| 5433 | |
| 5434 | // We're on a top-level node that's not a function. Treat as an initializer, whose |
| 5435 | // code goes into the beginning of the entry point. |
| 5436 | initializer->traverse(this); |
| 5437 | } |
| 5438 | } |
| 5439 | } |
| 5440 | // Walk over all linker objects to create a map for payload and callable data linker objects |
| 5441 | // and their location to be used during codegen for OpTraceKHR and OpExecuteCallableKHR |
| 5442 | // This is done here since it is possible that these linker objects are not be referenced in the AST |
| 5443 | void TGlslangToSpvTraverser::collectRayTracingLinkerObjects() |
| 5444 | { |
| 5445 | glslang::TIntermAggregate* linkerObjects = glslangIntermediate->findLinkerObjects(); |
| 5446 | for (auto& objSeq : linkerObjects->getSequence()) { |
| 5447 | auto objNode = objSeq->getAsSymbolNode(); |
| 5448 | if (objNode != nullptr) { |
| 5449 | if (objNode->getQualifier().hasLocation()) { |
| 5450 | unsigned int location = objNode->getQualifier().layoutLocation; |
| 5451 | auto st = objNode->getQualifier().storage; |
| 5452 | int set; |
| 5453 | switch (st) |
| 5454 | { |
| 5455 | case glslang::EvqPayload: |
| 5456 | case glslang::EvqPayloadIn: |
| 5457 | set = 0; |
| 5458 | break; |
| 5459 | case glslang::EvqCallableData: |
| 5460 | case glslang::EvqCallableDataIn: |
| 5461 | set = 1; |
| 5462 | break; |
| 5463 | |
| 5464 | case glslang::EvqHitObjectAttrNV: |
| 5465 | set = 2; |
| 5466 | break; |
| 5467 | |
| 5468 | default: |
| 5469 | set = -1; |
| 5470 | } |
| 5471 | if (set != -1) |
| 5472 | locationToSymbol[set].insert(std::make_pair(location, objNode)); |
| 5473 | } |
| 5474 | } |
| 5475 | } |
| 5476 | } |
| 5477 | // Process all the functions, while skipping initializers. |
| 5478 | void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 5479 | { |
| 5480 | for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 5481 | glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
| 5482 | if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
| 5483 | node->traverse(this); |
| 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 5488 | { |
| 5489 | // SPIR-V functions should already be in the functionMap from the prepass |
| 5490 | // that called makeFunctions(). |
| 5491 | currentFunction = functionMap[node->getName().c_str()]; |
| 5492 | spv::Block* functionBlock = currentFunction->getEntryBlock(); |
| 5493 | builder.setBuildPoint(functionBlock); |
| 5494 | builder.enterFunction(currentFunction); |
| 5495 | } |
| 5496 | |
| 5497 | void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 5498 | spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
| 5499 | { |
| 5500 | const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
| 5501 | |
| 5502 | glslang::TSampler sampler = {}; |
| 5503 | bool cubeCompare = false; |
| 5504 | #ifndef GLSLANG_WEB |
| 5505 | bool f16ShadowCompare = false; |
| 5506 | #endif |
| 5507 | if (node.isTexture() || node.isImage()) { |
| 5508 | sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 5509 | cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 5510 | #ifndef GLSLANG_WEB |
| 5511 | f16ShadowCompare = sampler.shadow && |
| 5512 | glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 5513 | #endif |
| 5514 | } |
| 5515 | |
| 5516 | for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 5517 | builder.clearAccessChain(); |
| 5518 | glslangArguments[i]->traverse(this); |
| 5519 | |
| 5520 | #ifndef GLSLANG_WEB |
| 5521 | // Special case l-value operands |
| 5522 | bool lvalue = false; |
| 5523 | switch (node.getOp()) { |
| 5524 | case glslang::EOpImageAtomicAdd: |
| 5525 | case glslang::EOpImageAtomicMin: |
| 5526 | case glslang::EOpImageAtomicMax: |
| 5527 | case glslang::EOpImageAtomicAnd: |
| 5528 | case glslang::EOpImageAtomicOr: |
| 5529 | case glslang::EOpImageAtomicXor: |
| 5530 | case glslang::EOpImageAtomicExchange: |
| 5531 | case glslang::EOpImageAtomicCompSwap: |
| 5532 | case glslang::EOpImageAtomicLoad: |
| 5533 | case glslang::EOpImageAtomicStore: |
| 5534 | if (i == 0) |
| 5535 | lvalue = true; |
| 5536 | break; |
| 5537 | case glslang::EOpSparseImageLoad: |
| 5538 | if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 5539 | lvalue = true; |
| 5540 | break; |
| 5541 | case glslang::EOpSparseTexture: |
| 5542 | if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 5543 | lvalue = true; |
| 5544 | break; |
| 5545 | case glslang::EOpSparseTextureClamp: |
| 5546 | if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 5547 | lvalue = true; |
| 5548 | break; |
| 5549 | case glslang::EOpSparseTextureLod: |
| 5550 | case glslang::EOpSparseTextureOffset: |
| 5551 | if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 5552 | lvalue = true; |
| 5553 | break; |
| 5554 | case glslang::EOpSparseTextureFetch: |
| 5555 | if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 5556 | lvalue = true; |
| 5557 | break; |
| 5558 | case glslang::EOpSparseTextureFetchOffset: |
| 5559 | if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 5560 | lvalue = true; |
| 5561 | break; |
| 5562 | case glslang::EOpSparseTextureLodOffset: |
| 5563 | case glslang::EOpSparseTextureGrad: |
| 5564 | case glslang::EOpSparseTextureOffsetClamp: |
| 5565 | if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 5566 | lvalue = true; |
| 5567 | break; |
| 5568 | case glslang::EOpSparseTextureGradOffset: |
| 5569 | case glslang::EOpSparseTextureGradClamp: |
| 5570 | if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 5571 | lvalue = true; |
| 5572 | break; |
| 5573 | case glslang::EOpSparseTextureGradOffsetClamp: |
| 5574 | if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 5575 | lvalue = true; |
| 5576 | break; |
| 5577 | case glslang::EOpSparseTextureGather: |
| 5578 | if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 5579 | lvalue = true; |
| 5580 | break; |
| 5581 | case glslang::EOpSparseTextureGatherOffset: |
| 5582 | case glslang::EOpSparseTextureGatherOffsets: |
| 5583 | if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 5584 | lvalue = true; |
| 5585 | break; |
| 5586 | case glslang::EOpSparseTextureGatherLod: |
| 5587 | if (i == 3) |
| 5588 | lvalue = true; |
| 5589 | break; |
| 5590 | case glslang::EOpSparseTextureGatherLodOffset: |
| 5591 | case glslang::EOpSparseTextureGatherLodOffsets: |
| 5592 | if (i == 4) |
| 5593 | lvalue = true; |
| 5594 | break; |
| 5595 | case glslang::EOpSparseImageLoadLod: |
| 5596 | if (i == 3) |
| 5597 | lvalue = true; |
| 5598 | break; |
| 5599 | case glslang::EOpImageSampleFootprintNV: |
| 5600 | if (i == 4) |
| 5601 | lvalue = true; |
| 5602 | break; |
| 5603 | case glslang::EOpImageSampleFootprintClampNV: |
| 5604 | case glslang::EOpImageSampleFootprintLodNV: |
| 5605 | if (i == 5) |
| 5606 | lvalue = true; |
| 5607 | break; |
| 5608 | case glslang::EOpImageSampleFootprintGradNV: |
| 5609 | if (i == 6) |
| 5610 | lvalue = true; |
| 5611 | break; |
| 5612 | case glslang::EOpImageSampleFootprintGradClampNV: |
| 5613 | if (i == 7) |
| 5614 | lvalue = true; |
| 5615 | break; |
| 5616 | case glslang::EOpRayQueryGetIntersectionTriangleVertexPositionsEXT: |
| 5617 | if (i == 2) |
| 5618 | lvalue = true; |
| 5619 | break; |
| 5620 | default: |
| 5621 | break; |
| 5622 | } |
| 5623 | |
| 5624 | if (lvalue) { |
| 5625 | spv::Id lvalue_id = builder.accessChainGetLValue(); |
| 5626 | arguments.push_back(lvalue_id); |
| 5627 | lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 5628 | builder.addDecoration(lvalue_id, TranslateNonUniformDecoration(lvalueCoherentFlags)); |
| 5629 | lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
| 5630 | } else |
| 5631 | #endif |
| 5632 | arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
| 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
| 5637 | { |
| 5638 | builder.clearAccessChain(); |
| 5639 | node.getOperand()->traverse(this); |
| 5640 | arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
| 5641 | } |
| 5642 | |
| 5643 | spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 5644 | { |
| 5645 | if (! node->isImage() && ! node->isTexture()) |
| 5646 | return spv::NoResult; |
| 5647 | |
| 5648 | builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 5649 | |
| 5650 | // Process a GLSL texturing op (will be SPV image) |
| 5651 | |
| 5652 | const glslang::TType &imageType = node->getAsAggregate() |
| 5653 | ? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 5654 | : node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
| 5655 | const glslang::TSampler sampler = imageType.getSampler(); |
| 5656 | #ifdef GLSLANG_WEB |
| 5657 | const bool f16ShadowCompare = false; |
| 5658 | #else |
| 5659 | bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
| 5660 | ? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 5661 | : false; |
| 5662 | #endif |
| 5663 | |
| 5664 | const auto signExtensionMask = [&]() { |
| 5665 | if (builder.getSpvVersion() >= spv::Spv_1_4) { |
| 5666 | if (sampler.type == glslang::EbtUint) |
| 5667 | return spv::ImageOperandsZeroExtendMask; |
| 5668 | else if (sampler.type == glslang::EbtInt) |
| 5669 | return spv::ImageOperandsSignExtendMask; |
| 5670 | } |
| 5671 | return spv::ImageOperandsMaskNone; |
| 5672 | }; |
| 5673 | |
| 5674 | spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 5675 | |
| 5676 | std::vector<spv::Id> arguments; |
| 5677 | if (node->getAsAggregate()) |
| 5678 | translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
| 5679 | else |
| 5680 | translateArguments(*node->getAsUnaryNode(), arguments); |
| 5681 | spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
| 5682 | |
| 5683 | spv::Builder::TextureParameters params = { }; |
| 5684 | params.sampler = arguments[0]; |
| 5685 | |
| 5686 | glslang::TCrackedTextureOp cracked; |
| 5687 | node->crackTexture(sampler, cracked); |
| 5688 | |
| 5689 | const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
| 5690 | |
| 5691 | if (builder.isSampledImage(params.sampler) && |
| 5692 | ((cracked.query && node->getOp() != glslang::EOpTextureQueryLod) || cracked.fragMask || cracked.fetch)) { |
| 5693 | params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 5694 | if (imageType.getQualifier().isNonUniform()) { |
| 5695 | builder.addDecoration(params.sampler, spv::DecorationNonUniformEXT); |
| 5696 | } |
| 5697 | } |
| 5698 | // Check for queries |
| 5699 | if (cracked.query) { |
| 5700 | switch (node->getOp()) { |
| 5701 | case glslang::EOpImageQuerySize: |
| 5702 | case glslang::EOpTextureQuerySize: |
| 5703 | if (arguments.size() > 1) { |
| 5704 | params.lod = arguments[1]; |
| 5705 | return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
| 5706 | } else |
| 5707 | return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
| 5708 | #ifndef GLSLANG_WEB |
| 5709 | case glslang::EOpImageQuerySamples: |
| 5710 | case glslang::EOpTextureQuerySamples: |
| 5711 | return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
| 5712 | case glslang::EOpTextureQueryLod: |
| 5713 | params.coords = arguments[1]; |
| 5714 | return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
| 5715 | case glslang::EOpTextureQueryLevels: |
| 5716 | return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
| 5717 | case glslang::EOpSparseTexelsResident: |
| 5718 | return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
| 5719 | #endif |
| 5720 | default: |
| 5721 | assert(0); |
| 5722 | break; |
| 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | int components = node->getType().getVectorSize(); |
| 5727 | |
| 5728 | if (node->getOp() == glslang::EOpImageLoad || |
| 5729 | node->getOp() == glslang::EOpImageLoadLod || |
| 5730 | node->getOp() == glslang::EOpTextureFetch || |
| 5731 | node->getOp() == glslang::EOpTextureFetchOffset) { |
| 5732 | // These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 5733 | // This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 5734 | // the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 5735 | // here around e.g. which ones return scalars or other types. |
| 5736 | components = 4; |
| 5737 | } |
| 5738 | |
| 5739 | glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 5740 | |
| 5741 | auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 5742 | |
| 5743 | // Check for image functions other than queries |
| 5744 | if (node->isImage()) { |
| 5745 | std::vector<spv::IdImmediate> operands; |
| 5746 | auto opIt = arguments.begin(); |
| 5747 | spv::IdImmediate image = { true, *(opIt++) }; |
| 5748 | operands.push_back(image); |
| 5749 | |
| 5750 | // Handle subpass operations |
| 5751 | // TODO: GLSL should change to have the "MS" only on the type rather than the |
| 5752 | // built-in function. |
| 5753 | if (cracked.subpass) { |
| 5754 | // add on the (0,0) coordinate |
| 5755 | spv::Id zero = builder.makeIntConstant(0); |
| 5756 | std::vector<spv::Id> comps; |
| 5757 | comps.push_back(zero); |
| 5758 | comps.push_back(zero); |
| 5759 | spv::IdImmediate coord = { true, |
| 5760 | builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 5761 | operands.push_back(coord); |
| 5762 | spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
| 5763 | imageOperands.word = imageOperands.word | signExtensionMask(); |
| 5764 | if (sampler.isMultiSample()) { |
| 5765 | imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
| 5766 | } |
| 5767 | if (imageOperands.word != spv::ImageOperandsMaskNone) { |
| 5768 | operands.push_back(imageOperands); |
| 5769 | if (sampler.isMultiSample()) { |
| 5770 | spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 5771 | operands.push_back(imageOperand); |
| 5772 | } |
| 5773 | } |
| 5774 | spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 5775 | builder.setPrecision(result, precision); |
| 5776 | return result; |
| 5777 | } |
| 5778 | |
| 5779 | if (cracked.attachmentEXT) { |
| 5780 | if (opIt != arguments.end()) { |
| 5781 | spv::IdImmediate sample = { true, *opIt }; |
| 5782 | operands.push_back(sample); |
| 5783 | } |
| 5784 | spv::Id result = builder.createOp(spv::OpColorAttachmentReadEXT, resultType(), operands); |
| 5785 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
| 5786 | builder.setPrecision(result, precision); |
| 5787 | return result; |
| 5788 | } |
| 5789 | |
| 5790 | spv::IdImmediate coord = { true, *(opIt++) }; |
| 5791 | operands.push_back(coord); |
| 5792 | if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 5793 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 5794 | if (sampler.isMultiSample()) { |
| 5795 | mask = mask | spv::ImageOperandsSampleMask; |
| 5796 | } |
| 5797 | if (cracked.lod) { |
| 5798 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 5799 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 5800 | mask = mask | spv::ImageOperandsLodMask; |
| 5801 | } |
| 5802 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 5803 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 5804 | mask = mask | signExtensionMask(); |
| 5805 | if (mask != spv::ImageOperandsMaskNone) { |
| 5806 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 5807 | operands.push_back(imageOperands); |
| 5808 | } |
| 5809 | if (mask & spv::ImageOperandsSampleMask) { |
| 5810 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5811 | operands.push_back(imageOperand); |
| 5812 | } |
| 5813 | if (mask & spv::ImageOperandsLodMask) { |
| 5814 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5815 | operands.push_back(imageOperand); |
| 5816 | } |
| 5817 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 5818 | spv::IdImmediate imageOperand = { true, |
| 5819 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 5820 | operands.push_back(imageOperand); |
| 5821 | } |
| 5822 | |
| 5823 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
| 5824 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 5825 | |
| 5826 | std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
| 5827 | builder.setPrecision(result[0], precision); |
| 5828 | |
| 5829 | // If needed, add a conversion constructor to the proper size. |
| 5830 | if (components != node->getType().getVectorSize()) |
| 5831 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 5832 | |
| 5833 | return result[0]; |
| 5834 | } else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 5835 | |
| 5836 | // Push the texel value before the operands |
| 5837 | if (sampler.isMultiSample() || cracked.lod) { |
| 5838 | spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 5839 | operands.push_back(texel); |
| 5840 | } else { |
| 5841 | spv::IdImmediate texel = { true, *opIt }; |
| 5842 | operands.push_back(texel); |
| 5843 | } |
| 5844 | |
| 5845 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 5846 | if (sampler.isMultiSample()) { |
| 5847 | mask = mask | spv::ImageOperandsSampleMask; |
| 5848 | } |
| 5849 | if (cracked.lod) { |
| 5850 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 5851 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 5852 | mask = mask | spv::ImageOperandsLodMask; |
| 5853 | } |
| 5854 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 5855 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
| 5856 | mask = mask | signExtensionMask(); |
| 5857 | if (mask != spv::ImageOperandsMaskNone) { |
| 5858 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 5859 | operands.push_back(imageOperands); |
| 5860 | } |
| 5861 | if (mask & spv::ImageOperandsSampleMask) { |
| 5862 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5863 | operands.push_back(imageOperand); |
| 5864 | } |
| 5865 | if (mask & spv::ImageOperandsLodMask) { |
| 5866 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5867 | operands.push_back(imageOperand); |
| 5868 | } |
| 5869 | if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
| 5870 | spv::IdImmediate imageOperand = { true, |
| 5871 | builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 5872 | operands.push_back(imageOperand); |
| 5873 | } |
| 5874 | |
| 5875 | builder.createNoResultOp(spv::OpImageWrite, operands); |
| 5876 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
| 5877 | builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
| 5878 | return spv::NoResult; |
| 5879 | } else if (node->getOp() == glslang::EOpSparseImageLoad || |
| 5880 | node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 5881 | builder.addCapability(spv::CapabilitySparseResidency); |
| 5882 | if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
| 5883 | builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 5884 | |
| 5885 | spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 5886 | if (sampler.isMultiSample()) { |
| 5887 | mask = mask | spv::ImageOperandsSampleMask; |
| 5888 | } |
| 5889 | if (cracked.lod) { |
| 5890 | builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 5891 | builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 5892 | |
| 5893 | mask = mask | spv::ImageOperandsLodMask; |
| 5894 | } |
| 5895 | mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 5896 | mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 5897 | mask = mask | signExtensionMask(); |
| 5898 | if (mask != spv::ImageOperandsMaskNone) { |
| 5899 | spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 5900 | operands.push_back(imageOperands); |
| 5901 | } |
| 5902 | if (mask & spv::ImageOperandsSampleMask) { |
| 5903 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5904 | operands.push_back(imageOperand); |
| 5905 | } |
| 5906 | if (mask & spv::ImageOperandsLodMask) { |
| 5907 | spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5908 | operands.push_back(imageOperand); |
| 5909 | } |
| 5910 | if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 5911 | spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope( |
| 5912 | TranslateCoherent(imageType))) }; |
| 5913 | operands.push_back(imageOperand); |
| 5914 | } |
| 5915 | |
| 5916 | // Create the return type that was a special structure |
| 5917 | spv::Id texelOut = *opIt; |
| 5918 | spv::Id typeId0 = resultType(); |
| 5919 | spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 5920 | spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 5921 | |
| 5922 | spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 5923 | |
| 5924 | // Decode the return type |
| 5925 | builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 5926 | return builder.createCompositeExtract(resultId, typeId0, 0); |
| 5927 | } else { |
| 5928 | // Process image atomic operations |
| 5929 | |
| 5930 | // GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 5931 | // as the first source operand, is required by SPIR-V atomic operations. |
| 5932 | // For non-MS, the sample value should be 0 |
| 5933 | spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) }; |
| 5934 | operands.push_back(sample); |
| 5935 | |
| 5936 | spv::Id resultTypeId; |
| 5937 | glslang::TBasicType typeProxy = node->getBasicType(); |
| 5938 | // imageAtomicStore has a void return type so base the pointer type on |
| 5939 | // the type of the value operand. |
| 5940 | if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 5941 | resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(*opIt)); |
| 5942 | typeProxy = node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType().getSampler().type; |
| 5943 | } else { |
| 5944 | resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 5945 | } |
| 5946 | spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
| 5947 | if (imageType.getQualifier().nonUniform) { |
| 5948 | builder.addDecoration(pointer, spv::DecorationNonUniformEXT); |
| 5949 | } |
| 5950 | |
| 5951 | std::vector<spv::Id> operands; |
| 5952 | operands.push_back(pointer); |
| 5953 | for (; opIt != arguments.end(); ++opIt) |
| 5954 | operands.push_back(*opIt); |
| 5955 | |
| 5956 | return createAtomicOperation(node->getOp(), precision, resultType(), operands, typeProxy, |
| 5957 | lvalueCoherentFlags); |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | #ifndef GLSLANG_WEB |
| 5962 | // Check for fragment mask functions other than queries |
| 5963 | if (cracked.fragMask) { |
| 5964 | assert(sampler.ms); |
| 5965 | |
| 5966 | auto opIt = arguments.begin(); |
| 5967 | std::vector<spv::Id> operands; |
| 5968 | |
| 5969 | operands.push_back(params.sampler); |
| 5970 | ++opIt; |
| 5971 | |
| 5972 | if (sampler.isSubpass()) { |
| 5973 | // add on the (0,0) coordinate |
| 5974 | spv::Id zero = builder.makeIntConstant(0); |
| 5975 | std::vector<spv::Id> comps; |
| 5976 | comps.push_back(zero); |
| 5977 | comps.push_back(zero); |
| 5978 | operands.push_back(builder.makeCompositeConstant( |
| 5979 | builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 5980 | } |
| 5981 | |
| 5982 | for (; opIt != arguments.end(); ++opIt) |
| 5983 | operands.push_back(*opIt); |
| 5984 | |
| 5985 | spv::Op fragMaskOp = spv::OpNop; |
| 5986 | if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 5987 | fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 5988 | else if (node->getOp() == glslang::EOpFragmentFetch) |
| 5989 | fragMaskOp = spv::OpFragmentFetchAMD; |
| 5990 | |
| 5991 | builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 5992 | builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 5993 | return builder.createOp(fragMaskOp, resultType(), operands); |
| 5994 | } |
| 5995 | #endif |
| 5996 | |
| 5997 | // Check for texture functions other than queries |
| 5998 | bool sparse = node->isSparseTexture(); |
| 5999 | bool = node->isImageFootprint(); |
| 6000 | bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
| 6001 | |
| 6002 | // check for bias argument |
| 6003 | bool bias = false; |
| 6004 | if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 6005 | int nonBiasArgCount = 2; |
| 6006 | if (cracked.gather) |
| 6007 | ++nonBiasArgCount; // comp argument should be present when bias argument is present |
| 6008 | |
| 6009 | if (f16ShadowCompare) |
| 6010 | ++nonBiasArgCount; |
| 6011 | if (cracked.offset) |
| 6012 | ++nonBiasArgCount; |
| 6013 | else if (cracked.offsets) |
| 6014 | ++nonBiasArgCount; |
| 6015 | if (cracked.grad) |
| 6016 | nonBiasArgCount += 2; |
| 6017 | if (cracked.lodClamp) |
| 6018 | ++nonBiasArgCount; |
| 6019 | if (sparse) |
| 6020 | ++nonBiasArgCount; |
| 6021 | if (imageFootprint) |
| 6022 | //Following three extra arguments |
| 6023 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 6024 | nonBiasArgCount += 3; |
| 6025 | if ((int)arguments.size() > nonBiasArgCount) |
| 6026 | bias = true; |
| 6027 | } |
| 6028 | |
| 6029 | #ifndef GLSLANG_WEB |
| 6030 | if (cracked.gather) { |
| 6031 | const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 6032 | if (bias || cracked.lod || |
| 6033 | sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 6034 | builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
| 6035 | builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
| 6036 | } |
| 6037 | } |
| 6038 | #endif |
| 6039 | |
| 6040 | // set the rest of the arguments |
| 6041 | |
| 6042 | params.coords = arguments[1]; |
| 6043 | int = 0; |
| 6044 | bool noImplicitLod = false; |
| 6045 | |
| 6046 | // sort out where Dref is coming from |
| 6047 | if (cubeCompare || f16ShadowCompare) { |
| 6048 | params.Dref = arguments[2]; |
| 6049 | ++extraArgs; |
| 6050 | } else if (sampler.shadow && cracked.gather) { |
| 6051 | params.Dref = arguments[2]; |
| 6052 | ++extraArgs; |
| 6053 | } else if (sampler.shadow) { |
| 6054 | std::vector<spv::Id> indexes; |
| 6055 | int dRefComp; |
| 6056 | if (cracked.proj) |
| 6057 | dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref" |
| 6058 | else |
| 6059 | dRefComp = builder.getNumComponents(params.coords) - 1; |
| 6060 | indexes.push_back(dRefComp); |
| 6061 | params.Dref = builder.createCompositeExtract(params.coords, |
| 6062 | builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 6063 | } |
| 6064 | |
| 6065 | // lod |
| 6066 | if (cracked.lod) { |
| 6067 | params.lod = arguments[2 + extraArgs]; |
| 6068 | ++extraArgs; |
| 6069 | } else if (glslangIntermediate->getStage() != EShLangFragment && |
| 6070 | !(glslangIntermediate->getStage() == EShLangCompute && |
| 6071 | glslangIntermediate->hasLayoutDerivativeModeNone())) { |
| 6072 | // we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 6073 | noImplicitLod = true; |
| 6074 | } |
| 6075 | |
| 6076 | // multisample |
| 6077 | if (sampler.isMultiSample()) { |
| 6078 | params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
| 6079 | ++extraArgs; |
| 6080 | } |
| 6081 | |
| 6082 | // gradient |
| 6083 | if (cracked.grad) { |
| 6084 | params.gradX = arguments[2 + extraArgs]; |
| 6085 | params.gradY = arguments[3 + extraArgs]; |
| 6086 | extraArgs += 2; |
| 6087 | } |
| 6088 | |
| 6089 | // offset and offsets |
| 6090 | if (cracked.offset) { |
| 6091 | params.offset = arguments[2 + extraArgs]; |
| 6092 | ++extraArgs; |
| 6093 | } else if (cracked.offsets) { |
| 6094 | params.offsets = arguments[2 + extraArgs]; |
| 6095 | ++extraArgs; |
| 6096 | } |
| 6097 | |
| 6098 | #ifndef GLSLANG_WEB |
| 6099 | // lod clamp |
| 6100 | if (cracked.lodClamp) { |
| 6101 | params.lodClamp = arguments[2 + extraArgs]; |
| 6102 | ++extraArgs; |
| 6103 | } |
| 6104 | // sparse |
| 6105 | if (sparse) { |
| 6106 | params.texelOut = arguments[2 + extraArgs]; |
| 6107 | ++extraArgs; |
| 6108 | } |
| 6109 | // gather component |
| 6110 | if (cracked.gather && ! sampler.shadow) { |
| 6111 | // default component is 0, if missing, otherwise an argument |
| 6112 | if (2 + extraArgs < (int)arguments.size()) { |
| 6113 | params.component = arguments[2 + extraArgs]; |
| 6114 | ++extraArgs; |
| 6115 | } else |
| 6116 | params.component = builder.makeIntConstant(0); |
| 6117 | } |
| 6118 | spv::Id resultStruct = spv::NoResult; |
| 6119 | if (imageFootprint) { |
| 6120 | //Following three extra arguments |
| 6121 | // int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 6122 | params.granularity = arguments[2 + extraArgs]; |
| 6123 | params.coarse = arguments[3 + extraArgs]; |
| 6124 | resultStruct = arguments[4 + extraArgs]; |
| 6125 | extraArgs += 3; |
| 6126 | } |
| 6127 | #endif |
| 6128 | // bias |
| 6129 | if (bias) { |
| 6130 | params.bias = arguments[2 + extraArgs]; |
| 6131 | ++extraArgs; |
| 6132 | } |
| 6133 | |
| 6134 | #ifndef GLSLANG_WEB |
| 6135 | if (imageFootprint) { |
| 6136 | builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 6137 | builder.addCapability(spv::CapabilityImageFootprintNV); |
| 6138 | |
| 6139 | |
| 6140 | //resultStructType(OpenGL type) contains 5 elements: |
| 6141 | //struct gl_TextureFootprint2DNV { |
| 6142 | // uvec2 anchor; |
| 6143 | // uvec2 offset; |
| 6144 | // uvec2 mask; |
| 6145 | // uint lod; |
| 6146 | // uint granularity; |
| 6147 | //}; |
| 6148 | //or |
| 6149 | //struct gl_TextureFootprint3DNV { |
| 6150 | // uvec3 anchor; |
| 6151 | // uvec3 offset; |
| 6152 | // uvec2 mask; |
| 6153 | // uint lod; |
| 6154 | // uint granularity; |
| 6155 | //}; |
| 6156 | spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 6157 | assert(builder.isStructType(resultStructType)); |
| 6158 | |
| 6159 | //resType (SPIR-V type) contains 6 elements: |
| 6160 | //Member 0 must be a Boolean type scalar(LOD), |
| 6161 | //Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 6162 | //Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 6163 | //Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 6164 | //Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 6165 | //Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 6166 | std::vector<spv::Id> members; |
| 6167 | members.push_back(resultType()); |
| 6168 | for (int i = 0; i < 5; i++) { |
| 6169 | members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 6170 | } |
| 6171 | spv::Id resType = builder.makeStructType(members, "ResType" ); |
| 6172 | |
| 6173 | //call ImageFootprintNV |
| 6174 | spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
| 6175 | cracked.gather, noImplicitLod, params, signExtensionMask()); |
| 6176 | |
| 6177 | //copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 6178 | for (int i = 0; i < 5; i++) { |
| 6179 | builder.clearAccessChain(); |
| 6180 | builder.setAccessChainLValue(resultStruct); |
| 6181 | |
| 6182 | //Accessing to a struct we created, no coherent flag is set |
| 6183 | spv::Builder::AccessChain::CoherentFlags flags; |
| 6184 | flags.clear(); |
| 6185 | |
| 6186 | builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
| 6187 | builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), |
| 6188 | i+1), TranslateNonUniformDecoration(imageType.getQualifier())); |
| 6189 | } |
| 6190 | return builder.createCompositeExtract(res, resultType(), 0); |
| 6191 | } |
| 6192 | #endif |
| 6193 | |
| 6194 | // projective component (might not to move) |
| 6195 | // GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 6196 | // are divided by the last component of P." |
| 6197 | // SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 6198 | // unused components will appear after all used components." |
| 6199 | if (cracked.proj) { |
| 6200 | int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 6201 | int projTargetComp; |
| 6202 | switch (sampler.dim) { |
| 6203 | case glslang::Esd1D: projTargetComp = 1; break; |
| 6204 | case glslang::Esd2D: projTargetComp = 2; break; |
| 6205 | case glslang::EsdRect: projTargetComp = 2; break; |
| 6206 | default: projTargetComp = projSourceComp; break; |
| 6207 | } |
| 6208 | // copy the projective coordinate if we have to |
| 6209 | if (projTargetComp != projSourceComp) { |
| 6210 | spv::Id projComp = builder.createCompositeExtract(params.coords, |
| 6211 | builder.getScalarTypeId(builder.getTypeId(params.coords)), projSourceComp); |
| 6212 | params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 6213 | builder.getTypeId(params.coords), projTargetComp); |
| 6214 | } |
| 6215 | } |
| 6216 | |
| 6217 | #ifndef GLSLANG_WEB |
| 6218 | // nonprivate |
| 6219 | if (imageType.getQualifier().nonprivate) { |
| 6220 | params.nonprivate = true; |
| 6221 | } |
| 6222 | |
| 6223 | // volatile |
| 6224 | if (imageType.getQualifier().volatil) { |
| 6225 | params.volatil = true; |
| 6226 | } |
| 6227 | #endif |
| 6228 | |
| 6229 | std::vector<spv::Id> result( 1, |
| 6230 | builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
| 6231 | noImplicitLod, params, signExtensionMask()) |
| 6232 | ); |
| 6233 | |
| 6234 | if (components != node->getType().getVectorSize()) |
| 6235 | result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 6236 | |
| 6237 | return result[0]; |
| 6238 | } |
| 6239 | |
| 6240 | spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 6241 | { |
| 6242 | // Grab the function's pointer from the previously created function |
| 6243 | spv::Function* function = functionMap[node->getName().c_str()]; |
| 6244 | if (! function) |
| 6245 | return 0; |
| 6246 | |
| 6247 | const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 6248 | const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 6249 | |
| 6250 | // See comments in makeFunctions() for details about the semantics for parameter passing. |
| 6251 | // |
| 6252 | // These imply we need a four step process: |
| 6253 | // 1. Evaluate the arguments |
| 6254 | // 2. Allocate and make copies of in, out, and inout arguments |
| 6255 | // 3. Make the call |
| 6256 | // 4. Copy back the results |
| 6257 | |
| 6258 | // 1. Evaluate the arguments and their types |
| 6259 | std::vector<spv::Builder::AccessChain> lValues; |
| 6260 | std::vector<spv::Id> rValues; |
| 6261 | std::vector<const glslang::TType*> argTypes; |
| 6262 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 6263 | argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
| 6264 | // build l-value |
| 6265 | builder.clearAccessChain(); |
| 6266 | glslangArgs[a]->traverse(this); |
| 6267 | // keep outputs and pass-by-originals as l-values, evaluate others as r-values |
| 6268 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
| 6269 | writableParam(qualifiers[a])) { |
| 6270 | // save l-value |
| 6271 | lValues.push_back(builder.getAccessChain()); |
| 6272 | } else { |
| 6273 | // process r-value |
| 6274 | rValues.push_back(accessChainLoad(*argTypes.back())); |
| 6275 | } |
| 6276 | } |
| 6277 | |
| 6278 | // 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 6279 | // copy the original into that space. |
| 6280 | // |
| 6281 | // Also, build up the list of actual arguments to pass in for the call |
| 6282 | int lValueCount = 0; |
| 6283 | int rValueCount = 0; |
| 6284 | std::vector<spv::Id> spvArgs; |
| 6285 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 6286 | spv::Id arg; |
| 6287 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
| 6288 | builder.setAccessChain(lValues[lValueCount]); |
| 6289 | arg = builder.accessChainGetLValue(); |
| 6290 | ++lValueCount; |
| 6291 | } else if (writableParam(qualifiers[a])) { |
| 6292 | // need space to hold the copy |
| 6293 | arg = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, |
| 6294 | builder.getContainedTypeId(function->getParamType(a)), "param" ); |
| 6295 | if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 6296 | // need to copy the input into output space |
| 6297 | builder.setAccessChain(lValues[lValueCount]); |
| 6298 | spv::Id copy = accessChainLoad(*argTypes[a]); |
| 6299 | builder.clearAccessChain(); |
| 6300 | builder.setAccessChainLValue(arg); |
| 6301 | multiTypeStore(*argTypes[a], copy); |
| 6302 | } |
| 6303 | ++lValueCount; |
| 6304 | } else { |
| 6305 | // process r-value, which involves a copy for a type mismatch |
| 6306 | if (function->getParamType(a) != builder.getTypeId(rValues[rValueCount]) || |
| 6307 | TranslatePrecisionDecoration(*argTypes[a]) != function->getParamPrecision(a)) |
| 6308 | { |
| 6309 | spv::Id argCopy = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, function->getParamType(a), "arg" ); |
| 6310 | builder.clearAccessChain(); |
| 6311 | builder.setAccessChainLValue(argCopy); |
| 6312 | multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 6313 | arg = builder.createLoad(argCopy, function->getParamPrecision(a)); |
| 6314 | } else |
| 6315 | arg = rValues[rValueCount]; |
| 6316 | ++rValueCount; |
| 6317 | } |
| 6318 | spvArgs.push_back(arg); |
| 6319 | } |
| 6320 | |
| 6321 | // 3. Make the call. |
| 6322 | spv::Id result = builder.createFunctionCall(function, spvArgs); |
| 6323 | builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
| 6324 | builder.addDecoration(result, TranslateNonUniformDecoration(node->getType().getQualifier())); |
| 6325 | |
| 6326 | // 4. Copy back out an "out" arguments. |
| 6327 | lValueCount = 0; |
| 6328 | for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 6329 | if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
| 6330 | ++lValueCount; |
| 6331 | else if (writableParam(qualifiers[a])) { |
| 6332 | if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 6333 | spv::Id copy = builder.createLoad(spvArgs[a], spv::NoPrecision); |
| 6334 | builder.addDecoration(copy, TranslateNonUniformDecoration(argTypes[a]->getQualifier())); |
| 6335 | builder.setAccessChain(lValues[lValueCount]); |
| 6336 | multiTypeStore(*argTypes[a], copy); |
| 6337 | } |
| 6338 | ++lValueCount; |
| 6339 | } |
| 6340 | } |
| 6341 | |
| 6342 | return result; |
| 6343 | } |
| 6344 | |
| 6345 | // Translate AST operation to SPV operation, already having SPV-based operands/types. |
| 6346 | spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
| 6347 | spv::Id typeId, spv::Id left, spv::Id right, |
| 6348 | glslang::TBasicType typeProxy, bool reduceComparison) |
| 6349 | { |
| 6350 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6351 | bool isFloat = isTypeFloat(typeProxy); |
| 6352 | bool isBool = typeProxy == glslang::EbtBool; |
| 6353 | |
| 6354 | spv::Op binOp = spv::OpNop; |
| 6355 | bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector? |
| 6356 | bool comparison = false; |
| 6357 | |
| 6358 | switch (op) { |
| 6359 | case glslang::EOpAdd: |
| 6360 | case glslang::EOpAddAssign: |
| 6361 | if (isFloat) |
| 6362 | binOp = spv::OpFAdd; |
| 6363 | else |
| 6364 | binOp = spv::OpIAdd; |
| 6365 | break; |
| 6366 | case glslang::EOpSub: |
| 6367 | case glslang::EOpSubAssign: |
| 6368 | if (isFloat) |
| 6369 | binOp = spv::OpFSub; |
| 6370 | else |
| 6371 | binOp = spv::OpISub; |
| 6372 | break; |
| 6373 | case glslang::EOpMul: |
| 6374 | case glslang::EOpMulAssign: |
| 6375 | if (isFloat) |
| 6376 | binOp = spv::OpFMul; |
| 6377 | else |
| 6378 | binOp = spv::OpIMul; |
| 6379 | break; |
| 6380 | case glslang::EOpVectorTimesScalar: |
| 6381 | case glslang::EOpVectorTimesScalarAssign: |
| 6382 | if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
| 6383 | if (builder.isVector(right)) |
| 6384 | std::swap(left, right); |
| 6385 | assert(builder.isScalar(right)); |
| 6386 | needMatchingVectors = false; |
| 6387 | binOp = spv::OpVectorTimesScalar; |
| 6388 | } else if (isFloat) |
| 6389 | binOp = spv::OpFMul; |
| 6390 | else |
| 6391 | binOp = spv::OpIMul; |
| 6392 | break; |
| 6393 | case glslang::EOpVectorTimesMatrix: |
| 6394 | case glslang::EOpVectorTimesMatrixAssign: |
| 6395 | binOp = spv::OpVectorTimesMatrix; |
| 6396 | break; |
| 6397 | case glslang::EOpMatrixTimesVector: |
| 6398 | binOp = spv::OpMatrixTimesVector; |
| 6399 | break; |
| 6400 | case glslang::EOpMatrixTimesScalar: |
| 6401 | case glslang::EOpMatrixTimesScalarAssign: |
| 6402 | binOp = spv::OpMatrixTimesScalar; |
| 6403 | break; |
| 6404 | case glslang::EOpMatrixTimesMatrix: |
| 6405 | case glslang::EOpMatrixTimesMatrixAssign: |
| 6406 | binOp = spv::OpMatrixTimesMatrix; |
| 6407 | break; |
| 6408 | case glslang::EOpOuterProduct: |
| 6409 | binOp = spv::OpOuterProduct; |
| 6410 | needMatchingVectors = false; |
| 6411 | break; |
| 6412 | |
| 6413 | case glslang::EOpDiv: |
| 6414 | case glslang::EOpDivAssign: |
| 6415 | if (isFloat) |
| 6416 | binOp = spv::OpFDiv; |
| 6417 | else if (isUnsigned) |
| 6418 | binOp = spv::OpUDiv; |
| 6419 | else |
| 6420 | binOp = spv::OpSDiv; |
| 6421 | break; |
| 6422 | case glslang::EOpMod: |
| 6423 | case glslang::EOpModAssign: |
| 6424 | if (isFloat) |
| 6425 | binOp = spv::OpFMod; |
| 6426 | else if (isUnsigned) |
| 6427 | binOp = spv::OpUMod; |
| 6428 | else |
| 6429 | binOp = spv::OpSMod; |
| 6430 | break; |
| 6431 | case glslang::EOpRightShift: |
| 6432 | case glslang::EOpRightShiftAssign: |
| 6433 | if (isUnsigned) |
| 6434 | binOp = spv::OpShiftRightLogical; |
| 6435 | else |
| 6436 | binOp = spv::OpShiftRightArithmetic; |
| 6437 | break; |
| 6438 | case glslang::EOpLeftShift: |
| 6439 | case glslang::EOpLeftShiftAssign: |
| 6440 | binOp = spv::OpShiftLeftLogical; |
| 6441 | break; |
| 6442 | case glslang::EOpAnd: |
| 6443 | case glslang::EOpAndAssign: |
| 6444 | binOp = spv::OpBitwiseAnd; |
| 6445 | break; |
| 6446 | case glslang::EOpLogicalAnd: |
| 6447 | needMatchingVectors = false; |
| 6448 | binOp = spv::OpLogicalAnd; |
| 6449 | break; |
| 6450 | case glslang::EOpInclusiveOr: |
| 6451 | case glslang::EOpInclusiveOrAssign: |
| 6452 | binOp = spv::OpBitwiseOr; |
| 6453 | break; |
| 6454 | case glslang::EOpLogicalOr: |
| 6455 | needMatchingVectors = false; |
| 6456 | binOp = spv::OpLogicalOr; |
| 6457 | break; |
| 6458 | case glslang::EOpExclusiveOr: |
| 6459 | case glslang::EOpExclusiveOrAssign: |
| 6460 | binOp = spv::OpBitwiseXor; |
| 6461 | break; |
| 6462 | case glslang::EOpLogicalXor: |
| 6463 | needMatchingVectors = false; |
| 6464 | binOp = spv::OpLogicalNotEqual; |
| 6465 | break; |
| 6466 | |
| 6467 | case glslang::EOpAbsDifference: |
| 6468 | binOp = isUnsigned ? spv::OpAbsUSubINTEL : spv::OpAbsISubINTEL; |
| 6469 | break; |
| 6470 | |
| 6471 | case glslang::EOpAddSaturate: |
| 6472 | binOp = isUnsigned ? spv::OpUAddSatINTEL : spv::OpIAddSatINTEL; |
| 6473 | break; |
| 6474 | |
| 6475 | case glslang::EOpSubSaturate: |
| 6476 | binOp = isUnsigned ? spv::OpUSubSatINTEL : spv::OpISubSatINTEL; |
| 6477 | break; |
| 6478 | |
| 6479 | case glslang::EOpAverage: |
| 6480 | binOp = isUnsigned ? spv::OpUAverageINTEL : spv::OpIAverageINTEL; |
| 6481 | break; |
| 6482 | |
| 6483 | case glslang::EOpAverageRounded: |
| 6484 | binOp = isUnsigned ? spv::OpUAverageRoundedINTEL : spv::OpIAverageRoundedINTEL; |
| 6485 | break; |
| 6486 | |
| 6487 | case glslang::EOpMul32x16: |
| 6488 | binOp = isUnsigned ? spv::OpUMul32x16INTEL : spv::OpIMul32x16INTEL; |
| 6489 | break; |
| 6490 | |
| 6491 | case glslang::EOpLessThan: |
| 6492 | case glslang::EOpGreaterThan: |
| 6493 | case glslang::EOpLessThanEqual: |
| 6494 | case glslang::EOpGreaterThanEqual: |
| 6495 | case glslang::EOpEqual: |
| 6496 | case glslang::EOpNotEqual: |
| 6497 | case glslang::EOpVectorEqual: |
| 6498 | case glslang::EOpVectorNotEqual: |
| 6499 | comparison = true; |
| 6500 | break; |
| 6501 | default: |
| 6502 | break; |
| 6503 | } |
| 6504 | |
| 6505 | // handle mapped binary operations (should be non-comparison) |
| 6506 | if (binOp != spv::OpNop) { |
| 6507 | assert(comparison == false); |
| 6508 | if (builder.isMatrix(left) || builder.isMatrix(right) || |
| 6509 | builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 6510 | return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
| 6511 | |
| 6512 | // No matrix involved; make both operands be the same number of components, if needed |
| 6513 | if (needMatchingVectors) |
| 6514 | builder.promoteScalar(decorations.precision, left, right); |
| 6515 | |
| 6516 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 6517 | decorations.addNoContraction(builder, result); |
| 6518 | decorations.addNonUniform(builder, result); |
| 6519 | return builder.setPrecision(result, decorations.precision); |
| 6520 | } |
| 6521 | |
| 6522 | if (! comparison) |
| 6523 | return 0; |
| 6524 | |
| 6525 | // Handle comparison instructions |
| 6526 | |
| 6527 | if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
| 6528 | && (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 6529 | spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
| 6530 | decorations.addNonUniform(builder, result); |
| 6531 | return result; |
| 6532 | } |
| 6533 | |
| 6534 | switch (op) { |
| 6535 | case glslang::EOpLessThan: |
| 6536 | if (isFloat) |
| 6537 | binOp = spv::OpFOrdLessThan; |
| 6538 | else if (isUnsigned) |
| 6539 | binOp = spv::OpULessThan; |
| 6540 | else |
| 6541 | binOp = spv::OpSLessThan; |
| 6542 | break; |
| 6543 | case glslang::EOpGreaterThan: |
| 6544 | if (isFloat) |
| 6545 | binOp = spv::OpFOrdGreaterThan; |
| 6546 | else if (isUnsigned) |
| 6547 | binOp = spv::OpUGreaterThan; |
| 6548 | else |
| 6549 | binOp = spv::OpSGreaterThan; |
| 6550 | break; |
| 6551 | case glslang::EOpLessThanEqual: |
| 6552 | if (isFloat) |
| 6553 | binOp = spv::OpFOrdLessThanEqual; |
| 6554 | else if (isUnsigned) |
| 6555 | binOp = spv::OpULessThanEqual; |
| 6556 | else |
| 6557 | binOp = spv::OpSLessThanEqual; |
| 6558 | break; |
| 6559 | case glslang::EOpGreaterThanEqual: |
| 6560 | if (isFloat) |
| 6561 | binOp = spv::OpFOrdGreaterThanEqual; |
| 6562 | else if (isUnsigned) |
| 6563 | binOp = spv::OpUGreaterThanEqual; |
| 6564 | else |
| 6565 | binOp = spv::OpSGreaterThanEqual; |
| 6566 | break; |
| 6567 | case glslang::EOpEqual: |
| 6568 | case glslang::EOpVectorEqual: |
| 6569 | if (isFloat) |
| 6570 | binOp = spv::OpFOrdEqual; |
| 6571 | else if (isBool) |
| 6572 | binOp = spv::OpLogicalEqual; |
| 6573 | else |
| 6574 | binOp = spv::OpIEqual; |
| 6575 | break; |
| 6576 | case glslang::EOpNotEqual: |
| 6577 | case glslang::EOpVectorNotEqual: |
| 6578 | if (isFloat) |
| 6579 | binOp = spv::OpFUnordNotEqual; |
| 6580 | else if (isBool) |
| 6581 | binOp = spv::OpLogicalNotEqual; |
| 6582 | else |
| 6583 | binOp = spv::OpINotEqual; |
| 6584 | break; |
| 6585 | default: |
| 6586 | break; |
| 6587 | } |
| 6588 | |
| 6589 | if (binOp != spv::OpNop) { |
| 6590 | spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 6591 | decorations.addNoContraction(builder, result); |
| 6592 | decorations.addNonUniform(builder, result); |
| 6593 | return builder.setPrecision(result, decorations.precision); |
| 6594 | } |
| 6595 | |
| 6596 | return 0; |
| 6597 | } |
| 6598 | |
| 6599 | // |
| 6600 | // Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 6601 | // These can be any of: |
| 6602 | // |
| 6603 | // matrix * scalar |
| 6604 | // scalar * matrix |
| 6605 | // matrix * matrix linear algebraic |
| 6606 | // matrix * vector |
| 6607 | // vector * matrix |
| 6608 | // matrix * matrix componentwise |
| 6609 | // matrix op matrix op in {+, -, /} |
| 6610 | // matrix op scalar op in {+, -, /} |
| 6611 | // scalar op matrix op in {+, -, /} |
| 6612 | // |
| 6613 | spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 6614 | spv::Id left, spv::Id right) |
| 6615 | { |
| 6616 | bool firstClass = true; |
| 6617 | |
| 6618 | // First, handle first-class matrix operations (* and matrix/scalar) |
| 6619 | switch (op) { |
| 6620 | case spv::OpFDiv: |
| 6621 | if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 6622 | // turn matrix / scalar into a multiply... |
| 6623 | spv::Id resultType = builder.getTypeId(right); |
| 6624 | right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
| 6625 | op = spv::OpMatrixTimesScalar; |
| 6626 | } else |
| 6627 | firstClass = false; |
| 6628 | break; |
| 6629 | case spv::OpMatrixTimesScalar: |
| 6630 | if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
| 6631 | std::swap(left, right); |
| 6632 | assert(builder.isScalar(right)); |
| 6633 | break; |
| 6634 | case spv::OpVectorTimesMatrix: |
| 6635 | assert(builder.isVector(left)); |
| 6636 | assert(builder.isMatrix(right)); |
| 6637 | break; |
| 6638 | case spv::OpMatrixTimesVector: |
| 6639 | assert(builder.isMatrix(left)); |
| 6640 | assert(builder.isVector(right)); |
| 6641 | break; |
| 6642 | case spv::OpMatrixTimesMatrix: |
| 6643 | assert(builder.isMatrix(left)); |
| 6644 | assert(builder.isMatrix(right)); |
| 6645 | break; |
| 6646 | default: |
| 6647 | firstClass = false; |
| 6648 | break; |
| 6649 | } |
| 6650 | |
| 6651 | if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 6652 | firstClass = true; |
| 6653 | |
| 6654 | if (firstClass) { |
| 6655 | spv::Id result = builder.createBinOp(op, typeId, left, right); |
| 6656 | decorations.addNoContraction(builder, result); |
| 6657 | decorations.addNonUniform(builder, result); |
| 6658 | return builder.setPrecision(result, decorations.precision); |
| 6659 | } |
| 6660 | |
| 6661 | // Handle component-wise +, -, *, %, and / for all combinations of type. |
| 6662 | // The result type of all of them is the same type as the (a) matrix operand. |
| 6663 | // The algorithm is to: |
| 6664 | // - break the matrix(es) into vectors |
| 6665 | // - smear any scalar to a vector |
| 6666 | // - do vector operations |
| 6667 | // - make a matrix out the vector results |
| 6668 | switch (op) { |
| 6669 | case spv::OpFAdd: |
| 6670 | case spv::OpFSub: |
| 6671 | case spv::OpFDiv: |
| 6672 | case spv::OpFMod: |
| 6673 | case spv::OpFMul: |
| 6674 | { |
| 6675 | // one time set up... |
| 6676 | bool leftMat = builder.isMatrix(left); |
| 6677 | bool rightMat = builder.isMatrix(right); |
| 6678 | unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 6679 | int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 6680 | spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 6681 | spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 6682 | std::vector<spv::Id> results; |
| 6683 | spv::Id smearVec = spv::NoResult; |
| 6684 | if (builder.isScalar(left)) |
| 6685 | smearVec = builder.smearScalar(decorations.precision, left, vecType); |
| 6686 | else if (builder.isScalar(right)) |
| 6687 | smearVec = builder.smearScalar(decorations.precision, right, vecType); |
| 6688 | |
| 6689 | // do each vector op |
| 6690 | for (unsigned int c = 0; c < numCols; ++c) { |
| 6691 | std::vector<unsigned int> indexes; |
| 6692 | indexes.push_back(c); |
| 6693 | spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 6694 | spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
| 6695 | spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
| 6696 | decorations.addNoContraction(builder, result); |
| 6697 | decorations.addNonUniform(builder, result); |
| 6698 | results.push_back(builder.setPrecision(result, decorations.precision)); |
| 6699 | } |
| 6700 | |
| 6701 | // put the pieces together |
| 6702 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
| 6703 | decorations.addNonUniform(builder, result); |
| 6704 | return result; |
| 6705 | } |
| 6706 | default: |
| 6707 | assert(0); |
| 6708 | return spv::NoResult; |
| 6709 | } |
| 6710 | } |
| 6711 | |
| 6712 | spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
| 6713 | spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
| 6714 | { |
| 6715 | spv::Op unaryOp = spv::OpNop; |
| 6716 | int extBuiltins = -1; |
| 6717 | int libCall = -1; |
| 6718 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6719 | bool isFloat = isTypeFloat(typeProxy); |
| 6720 | |
| 6721 | switch (op) { |
| 6722 | case glslang::EOpNegative: |
| 6723 | if (isFloat) { |
| 6724 | unaryOp = spv::OpFNegate; |
| 6725 | if (builder.isMatrixType(typeId)) |
| 6726 | return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
| 6727 | } else |
| 6728 | unaryOp = spv::OpSNegate; |
| 6729 | break; |
| 6730 | |
| 6731 | case glslang::EOpLogicalNot: |
| 6732 | case glslang::EOpVectorLogicalNot: |
| 6733 | unaryOp = spv::OpLogicalNot; |
| 6734 | break; |
| 6735 | case glslang::EOpBitwiseNot: |
| 6736 | unaryOp = spv::OpNot; |
| 6737 | break; |
| 6738 | |
| 6739 | case glslang::EOpDeterminant: |
| 6740 | libCall = spv::GLSLstd450Determinant; |
| 6741 | break; |
| 6742 | case glslang::EOpMatrixInverse: |
| 6743 | libCall = spv::GLSLstd450MatrixInverse; |
| 6744 | break; |
| 6745 | case glslang::EOpTranspose: |
| 6746 | unaryOp = spv::OpTranspose; |
| 6747 | break; |
| 6748 | |
| 6749 | case glslang::EOpRadians: |
| 6750 | libCall = spv::GLSLstd450Radians; |
| 6751 | break; |
| 6752 | case glslang::EOpDegrees: |
| 6753 | libCall = spv::GLSLstd450Degrees; |
| 6754 | break; |
| 6755 | case glslang::EOpSin: |
| 6756 | libCall = spv::GLSLstd450Sin; |
| 6757 | break; |
| 6758 | case glslang::EOpCos: |
| 6759 | libCall = spv::GLSLstd450Cos; |
| 6760 | break; |
| 6761 | case glslang::EOpTan: |
| 6762 | libCall = spv::GLSLstd450Tan; |
| 6763 | break; |
| 6764 | case glslang::EOpAcos: |
| 6765 | libCall = spv::GLSLstd450Acos; |
| 6766 | break; |
| 6767 | case glslang::EOpAsin: |
| 6768 | libCall = spv::GLSLstd450Asin; |
| 6769 | break; |
| 6770 | case glslang::EOpAtan: |
| 6771 | libCall = spv::GLSLstd450Atan; |
| 6772 | break; |
| 6773 | |
| 6774 | case glslang::EOpAcosh: |
| 6775 | libCall = spv::GLSLstd450Acosh; |
| 6776 | break; |
| 6777 | case glslang::EOpAsinh: |
| 6778 | libCall = spv::GLSLstd450Asinh; |
| 6779 | break; |
| 6780 | case glslang::EOpAtanh: |
| 6781 | libCall = spv::GLSLstd450Atanh; |
| 6782 | break; |
| 6783 | case glslang::EOpTanh: |
| 6784 | libCall = spv::GLSLstd450Tanh; |
| 6785 | break; |
| 6786 | case glslang::EOpCosh: |
| 6787 | libCall = spv::GLSLstd450Cosh; |
| 6788 | break; |
| 6789 | case glslang::EOpSinh: |
| 6790 | libCall = spv::GLSLstd450Sinh; |
| 6791 | break; |
| 6792 | |
| 6793 | case glslang::EOpLength: |
| 6794 | libCall = spv::GLSLstd450Length; |
| 6795 | break; |
| 6796 | case glslang::EOpNormalize: |
| 6797 | libCall = spv::GLSLstd450Normalize; |
| 6798 | break; |
| 6799 | |
| 6800 | case glslang::EOpExp: |
| 6801 | libCall = spv::GLSLstd450Exp; |
| 6802 | break; |
| 6803 | case glslang::EOpLog: |
| 6804 | libCall = spv::GLSLstd450Log; |
| 6805 | break; |
| 6806 | case glslang::EOpExp2: |
| 6807 | libCall = spv::GLSLstd450Exp2; |
| 6808 | break; |
| 6809 | case glslang::EOpLog2: |
| 6810 | libCall = spv::GLSLstd450Log2; |
| 6811 | break; |
| 6812 | case glslang::EOpSqrt: |
| 6813 | libCall = spv::GLSLstd450Sqrt; |
| 6814 | break; |
| 6815 | case glslang::EOpInverseSqrt: |
| 6816 | libCall = spv::GLSLstd450InverseSqrt; |
| 6817 | break; |
| 6818 | |
| 6819 | case glslang::EOpFloor: |
| 6820 | libCall = spv::GLSLstd450Floor; |
| 6821 | break; |
| 6822 | case glslang::EOpTrunc: |
| 6823 | libCall = spv::GLSLstd450Trunc; |
| 6824 | break; |
| 6825 | case glslang::EOpRound: |
| 6826 | libCall = spv::GLSLstd450Round; |
| 6827 | break; |
| 6828 | case glslang::EOpRoundEven: |
| 6829 | libCall = spv::GLSLstd450RoundEven; |
| 6830 | break; |
| 6831 | case glslang::EOpCeil: |
| 6832 | libCall = spv::GLSLstd450Ceil; |
| 6833 | break; |
| 6834 | case glslang::EOpFract: |
| 6835 | libCall = spv::GLSLstd450Fract; |
| 6836 | break; |
| 6837 | |
| 6838 | case glslang::EOpIsNan: |
| 6839 | unaryOp = spv::OpIsNan; |
| 6840 | break; |
| 6841 | case glslang::EOpIsInf: |
| 6842 | unaryOp = spv::OpIsInf; |
| 6843 | break; |
| 6844 | case glslang::EOpIsFinite: |
| 6845 | unaryOp = spv::OpIsFinite; |
| 6846 | break; |
| 6847 | |
| 6848 | case glslang::EOpFloatBitsToInt: |
| 6849 | case glslang::EOpFloatBitsToUint: |
| 6850 | case glslang::EOpIntBitsToFloat: |
| 6851 | case glslang::EOpUintBitsToFloat: |
| 6852 | case glslang::EOpDoubleBitsToInt64: |
| 6853 | case glslang::EOpDoubleBitsToUint64: |
| 6854 | case glslang::EOpInt64BitsToDouble: |
| 6855 | case glslang::EOpUint64BitsToDouble: |
| 6856 | case glslang::EOpFloat16BitsToInt16: |
| 6857 | case glslang::EOpFloat16BitsToUint16: |
| 6858 | case glslang::EOpInt16BitsToFloat16: |
| 6859 | case glslang::EOpUint16BitsToFloat16: |
| 6860 | unaryOp = spv::OpBitcast; |
| 6861 | break; |
| 6862 | |
| 6863 | case glslang::EOpPackSnorm2x16: |
| 6864 | libCall = spv::GLSLstd450PackSnorm2x16; |
| 6865 | break; |
| 6866 | case glslang::EOpUnpackSnorm2x16: |
| 6867 | libCall = spv::GLSLstd450UnpackSnorm2x16; |
| 6868 | break; |
| 6869 | case glslang::EOpPackUnorm2x16: |
| 6870 | libCall = spv::GLSLstd450PackUnorm2x16; |
| 6871 | break; |
| 6872 | case glslang::EOpUnpackUnorm2x16: |
| 6873 | libCall = spv::GLSLstd450UnpackUnorm2x16; |
| 6874 | break; |
| 6875 | case glslang::EOpPackHalf2x16: |
| 6876 | libCall = spv::GLSLstd450PackHalf2x16; |
| 6877 | break; |
| 6878 | case glslang::EOpUnpackHalf2x16: |
| 6879 | libCall = spv::GLSLstd450UnpackHalf2x16; |
| 6880 | break; |
| 6881 | #ifndef GLSLANG_WEB |
| 6882 | case glslang::EOpPackSnorm4x8: |
| 6883 | libCall = spv::GLSLstd450PackSnorm4x8; |
| 6884 | break; |
| 6885 | case glslang::EOpUnpackSnorm4x8: |
| 6886 | libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 6887 | break; |
| 6888 | case glslang::EOpPackUnorm4x8: |
| 6889 | libCall = spv::GLSLstd450PackUnorm4x8; |
| 6890 | break; |
| 6891 | case glslang::EOpUnpackUnorm4x8: |
| 6892 | libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 6893 | break; |
| 6894 | case glslang::EOpPackDouble2x32: |
| 6895 | libCall = spv::GLSLstd450PackDouble2x32; |
| 6896 | break; |
| 6897 | case glslang::EOpUnpackDouble2x32: |
| 6898 | libCall = spv::GLSLstd450UnpackDouble2x32; |
| 6899 | break; |
| 6900 | #endif |
| 6901 | |
| 6902 | case glslang::EOpPackInt2x32: |
| 6903 | case glslang::EOpUnpackInt2x32: |
| 6904 | case glslang::EOpPackUint2x32: |
| 6905 | case glslang::EOpUnpackUint2x32: |
| 6906 | case glslang::EOpPack16: |
| 6907 | case glslang::EOpPack32: |
| 6908 | case glslang::EOpPack64: |
| 6909 | case glslang::EOpUnpack32: |
| 6910 | case glslang::EOpUnpack16: |
| 6911 | case glslang::EOpUnpack8: |
| 6912 | case glslang::EOpPackInt2x16: |
| 6913 | case glslang::EOpUnpackInt2x16: |
| 6914 | case glslang::EOpPackUint2x16: |
| 6915 | case glslang::EOpUnpackUint2x16: |
| 6916 | case glslang::EOpPackInt4x16: |
| 6917 | case glslang::EOpUnpackInt4x16: |
| 6918 | case glslang::EOpPackUint4x16: |
| 6919 | case glslang::EOpUnpackUint4x16: |
| 6920 | case glslang::EOpPackFloat2x16: |
| 6921 | case glslang::EOpUnpackFloat2x16: |
| 6922 | unaryOp = spv::OpBitcast; |
| 6923 | break; |
| 6924 | |
| 6925 | case glslang::EOpDPdx: |
| 6926 | unaryOp = spv::OpDPdx; |
| 6927 | break; |
| 6928 | case glslang::EOpDPdy: |
| 6929 | unaryOp = spv::OpDPdy; |
| 6930 | break; |
| 6931 | case glslang::EOpFwidth: |
| 6932 | unaryOp = spv::OpFwidth; |
| 6933 | break; |
| 6934 | |
| 6935 | case glslang::EOpAny: |
| 6936 | unaryOp = spv::OpAny; |
| 6937 | break; |
| 6938 | case glslang::EOpAll: |
| 6939 | unaryOp = spv::OpAll; |
| 6940 | break; |
| 6941 | |
| 6942 | case glslang::EOpAbs: |
| 6943 | if (isFloat) |
| 6944 | libCall = spv::GLSLstd450FAbs; |
| 6945 | else |
| 6946 | libCall = spv::GLSLstd450SAbs; |
| 6947 | break; |
| 6948 | case glslang::EOpSign: |
| 6949 | if (isFloat) |
| 6950 | libCall = spv::GLSLstd450FSign; |
| 6951 | else |
| 6952 | libCall = spv::GLSLstd450SSign; |
| 6953 | break; |
| 6954 | |
| 6955 | #ifndef GLSLANG_WEB |
| 6956 | case glslang::EOpDPdxFine: |
| 6957 | unaryOp = spv::OpDPdxFine; |
| 6958 | break; |
| 6959 | case glslang::EOpDPdyFine: |
| 6960 | unaryOp = spv::OpDPdyFine; |
| 6961 | break; |
| 6962 | case glslang::EOpFwidthFine: |
| 6963 | unaryOp = spv::OpFwidthFine; |
| 6964 | break; |
| 6965 | case glslang::EOpDPdxCoarse: |
| 6966 | unaryOp = spv::OpDPdxCoarse; |
| 6967 | break; |
| 6968 | case glslang::EOpDPdyCoarse: |
| 6969 | unaryOp = spv::OpDPdyCoarse; |
| 6970 | break; |
| 6971 | case glslang::EOpFwidthCoarse: |
| 6972 | unaryOp = spv::OpFwidthCoarse; |
| 6973 | break; |
| 6974 | case glslang::EOpRayQueryProceed: |
| 6975 | unaryOp = spv::OpRayQueryProceedKHR; |
| 6976 | break; |
| 6977 | case glslang::EOpRayQueryGetRayTMin: |
| 6978 | unaryOp = spv::OpRayQueryGetRayTMinKHR; |
| 6979 | break; |
| 6980 | case glslang::EOpRayQueryGetRayFlags: |
| 6981 | unaryOp = spv::OpRayQueryGetRayFlagsKHR; |
| 6982 | break; |
| 6983 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 6984 | unaryOp = spv::OpRayQueryGetWorldRayOriginKHR; |
| 6985 | break; |
| 6986 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 6987 | unaryOp = spv::OpRayQueryGetWorldRayDirectionKHR; |
| 6988 | break; |
| 6989 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 6990 | unaryOp = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
| 6991 | break; |
| 6992 | case glslang::EOpInterpolateAtCentroid: |
| 6993 | if (typeProxy == glslang::EbtFloat16) |
| 6994 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6995 | libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 6996 | break; |
| 6997 | case glslang::EOpAtomicCounterIncrement: |
| 6998 | case glslang::EOpAtomicCounterDecrement: |
| 6999 | case glslang::EOpAtomicCounter: |
| 7000 | { |
| 7001 | // Handle all of the atomics in one place, in createAtomicOperation() |
| 7002 | std::vector<spv::Id> operands; |
| 7003 | operands.push_back(operand); |
| 7004 | return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
| 7005 | } |
| 7006 | |
| 7007 | case glslang::EOpBitFieldReverse: |
| 7008 | unaryOp = spv::OpBitReverse; |
| 7009 | break; |
| 7010 | case glslang::EOpBitCount: |
| 7011 | unaryOp = spv::OpBitCount; |
| 7012 | break; |
| 7013 | case glslang::EOpFindLSB: |
| 7014 | libCall = spv::GLSLstd450FindILsb; |
| 7015 | break; |
| 7016 | case glslang::EOpFindMSB: |
| 7017 | if (isUnsigned) |
| 7018 | libCall = spv::GLSLstd450FindUMsb; |
| 7019 | else |
| 7020 | libCall = spv::GLSLstd450FindSMsb; |
| 7021 | break; |
| 7022 | |
| 7023 | case glslang::EOpCountLeadingZeros: |
| 7024 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 7025 | builder.addExtension("SPV_INTEL_shader_integer_functions2" ); |
| 7026 | unaryOp = spv::OpUCountLeadingZerosINTEL; |
| 7027 | break; |
| 7028 | |
| 7029 | case glslang::EOpCountTrailingZeros: |
| 7030 | builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 7031 | builder.addExtension("SPV_INTEL_shader_integer_functions2" ); |
| 7032 | unaryOp = spv::OpUCountTrailingZerosINTEL; |
| 7033 | break; |
| 7034 | |
| 7035 | case glslang::EOpBallot: |
| 7036 | case glslang::EOpReadFirstInvocation: |
| 7037 | case glslang::EOpAnyInvocation: |
| 7038 | case glslang::EOpAllInvocations: |
| 7039 | case glslang::EOpAllInvocationsEqual: |
| 7040 | case glslang::EOpMinInvocations: |
| 7041 | case glslang::EOpMaxInvocations: |
| 7042 | case glslang::EOpAddInvocations: |
| 7043 | case glslang::EOpMinInvocationsNonUniform: |
| 7044 | case glslang::EOpMaxInvocationsNonUniform: |
| 7045 | case glslang::EOpAddInvocationsNonUniform: |
| 7046 | case glslang::EOpMinInvocationsInclusiveScan: |
| 7047 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 7048 | case glslang::EOpAddInvocationsInclusiveScan: |
| 7049 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 7050 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 7051 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 7052 | case glslang::EOpMinInvocationsExclusiveScan: |
| 7053 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 7054 | case glslang::EOpAddInvocationsExclusiveScan: |
| 7055 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 7056 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 7057 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 7058 | { |
| 7059 | std::vector<spv::Id> operands; |
| 7060 | operands.push_back(operand); |
| 7061 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 7062 | } |
| 7063 | case glslang::EOpSubgroupAll: |
| 7064 | case glslang::EOpSubgroupAny: |
| 7065 | case glslang::EOpSubgroupAllEqual: |
| 7066 | case glslang::EOpSubgroupBroadcastFirst: |
| 7067 | case glslang::EOpSubgroupBallot: |
| 7068 | case glslang::EOpSubgroupInverseBallot: |
| 7069 | case glslang::EOpSubgroupBallotBitCount: |
| 7070 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7071 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7072 | case glslang::EOpSubgroupBallotFindLSB: |
| 7073 | case glslang::EOpSubgroupBallotFindMSB: |
| 7074 | case glslang::EOpSubgroupAdd: |
| 7075 | case glslang::EOpSubgroupMul: |
| 7076 | case glslang::EOpSubgroupMin: |
| 7077 | case glslang::EOpSubgroupMax: |
| 7078 | case glslang::EOpSubgroupAnd: |
| 7079 | case glslang::EOpSubgroupOr: |
| 7080 | case glslang::EOpSubgroupXor: |
| 7081 | case glslang::EOpSubgroupInclusiveAdd: |
| 7082 | case glslang::EOpSubgroupInclusiveMul: |
| 7083 | case glslang::EOpSubgroupInclusiveMin: |
| 7084 | case glslang::EOpSubgroupInclusiveMax: |
| 7085 | case glslang::EOpSubgroupInclusiveAnd: |
| 7086 | case glslang::EOpSubgroupInclusiveOr: |
| 7087 | case glslang::EOpSubgroupInclusiveXor: |
| 7088 | case glslang::EOpSubgroupExclusiveAdd: |
| 7089 | case glslang::EOpSubgroupExclusiveMul: |
| 7090 | case glslang::EOpSubgroupExclusiveMin: |
| 7091 | case glslang::EOpSubgroupExclusiveMax: |
| 7092 | case glslang::EOpSubgroupExclusiveAnd: |
| 7093 | case glslang::EOpSubgroupExclusiveOr: |
| 7094 | case glslang::EOpSubgroupExclusiveXor: |
| 7095 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7096 | case glslang::EOpSubgroupQuadSwapVertical: |
| 7097 | case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 7098 | std::vector<spv::Id> operands; |
| 7099 | operands.push_back(operand); |
| 7100 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 7101 | } |
| 7102 | case glslang::EOpMbcnt: |
| 7103 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7104 | libCall = spv::MbcntAMD; |
| 7105 | break; |
| 7106 | |
| 7107 | case glslang::EOpCubeFaceIndex: |
| 7108 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 7109 | libCall = spv::CubeFaceIndexAMD; |
| 7110 | break; |
| 7111 | |
| 7112 | case glslang::EOpCubeFaceCoord: |
| 7113 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 7114 | libCall = spv::CubeFaceCoordAMD; |
| 7115 | break; |
| 7116 | case glslang::EOpSubgroupPartition: |
| 7117 | unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 7118 | break; |
| 7119 | case glslang::EOpConstructReference: |
| 7120 | unaryOp = spv::OpBitcast; |
| 7121 | break; |
| 7122 | |
| 7123 | case glslang::EOpConvUint64ToAccStruct: |
| 7124 | case glslang::EOpConvUvec2ToAccStruct: |
| 7125 | unaryOp = spv::OpConvertUToAccelerationStructureKHR; |
| 7126 | break; |
| 7127 | |
| 7128 | case glslang::EOpHitObjectIsEmptyNV: |
| 7129 | unaryOp = spv::OpHitObjectIsEmptyNV; |
| 7130 | break; |
| 7131 | |
| 7132 | case glslang::EOpHitObjectIsMissNV: |
| 7133 | unaryOp = spv::OpHitObjectIsMissNV; |
| 7134 | break; |
| 7135 | |
| 7136 | case glslang::EOpHitObjectIsHitNV: |
| 7137 | unaryOp = spv::OpHitObjectIsHitNV; |
| 7138 | break; |
| 7139 | |
| 7140 | case glslang::EOpHitObjectGetObjectRayOriginNV: |
| 7141 | unaryOp = spv::OpHitObjectGetObjectRayOriginNV; |
| 7142 | break; |
| 7143 | |
| 7144 | case glslang::EOpHitObjectGetObjectRayDirectionNV: |
| 7145 | unaryOp = spv::OpHitObjectGetObjectRayDirectionNV; |
| 7146 | break; |
| 7147 | |
| 7148 | case glslang::EOpHitObjectGetWorldRayOriginNV: |
| 7149 | unaryOp = spv::OpHitObjectGetWorldRayOriginNV; |
| 7150 | break; |
| 7151 | |
| 7152 | case glslang::EOpHitObjectGetWorldRayDirectionNV: |
| 7153 | unaryOp = spv::OpHitObjectGetWorldRayDirectionNV; |
| 7154 | break; |
| 7155 | |
| 7156 | case glslang::EOpHitObjectGetObjectToWorldNV: |
| 7157 | unaryOp = spv::OpHitObjectGetObjectToWorldNV; |
| 7158 | break; |
| 7159 | |
| 7160 | case glslang::EOpHitObjectGetWorldToObjectNV: |
| 7161 | unaryOp = spv::OpHitObjectGetWorldToObjectNV; |
| 7162 | break; |
| 7163 | |
| 7164 | case glslang::EOpHitObjectGetRayTMinNV: |
| 7165 | unaryOp = spv::OpHitObjectGetRayTMinNV; |
| 7166 | break; |
| 7167 | |
| 7168 | case glslang::EOpHitObjectGetRayTMaxNV: |
| 7169 | unaryOp = spv::OpHitObjectGetRayTMaxNV; |
| 7170 | break; |
| 7171 | |
| 7172 | case glslang::EOpHitObjectGetPrimitiveIndexNV: |
| 7173 | unaryOp = spv::OpHitObjectGetPrimitiveIndexNV; |
| 7174 | break; |
| 7175 | |
| 7176 | case glslang::EOpHitObjectGetInstanceIdNV: |
| 7177 | unaryOp = spv::OpHitObjectGetInstanceIdNV; |
| 7178 | break; |
| 7179 | |
| 7180 | case glslang::EOpHitObjectGetInstanceCustomIndexNV: |
| 7181 | unaryOp = spv::OpHitObjectGetInstanceCustomIndexNV; |
| 7182 | break; |
| 7183 | |
| 7184 | case glslang::EOpHitObjectGetGeometryIndexNV: |
| 7185 | unaryOp = spv::OpHitObjectGetGeometryIndexNV; |
| 7186 | break; |
| 7187 | |
| 7188 | case glslang::EOpHitObjectGetHitKindNV: |
| 7189 | unaryOp = spv::OpHitObjectGetHitKindNV; |
| 7190 | break; |
| 7191 | |
| 7192 | case glslang::EOpHitObjectGetCurrentTimeNV: |
| 7193 | unaryOp = spv::OpHitObjectGetCurrentTimeNV; |
| 7194 | break; |
| 7195 | |
| 7196 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexNV: |
| 7197 | unaryOp = spv::OpHitObjectGetShaderBindingTableRecordIndexNV; |
| 7198 | break; |
| 7199 | |
| 7200 | case glslang::EOpHitObjectGetShaderRecordBufferHandleNV: |
| 7201 | unaryOp = spv::OpHitObjectGetShaderRecordBufferHandleNV; |
| 7202 | break; |
| 7203 | |
| 7204 | #endif |
| 7205 | |
| 7206 | case glslang::EOpCopyObject: |
| 7207 | unaryOp = spv::OpCopyObject; |
| 7208 | break; |
| 7209 | |
| 7210 | case glslang::EOpDepthAttachmentReadEXT: |
| 7211 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
| 7212 | builder.addCapability(spv::CapabilityTileImageDepthReadAccessEXT); |
| 7213 | unaryOp = spv::OpDepthAttachmentReadEXT; |
| 7214 | decorations.precision = spv::NoPrecision; |
| 7215 | break; |
| 7216 | case glslang::EOpStencilAttachmentReadEXT: |
| 7217 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
| 7218 | builder.addCapability(spv::CapabilityTileImageStencilReadAccessEXT); |
| 7219 | unaryOp = spv::OpStencilAttachmentReadEXT; |
| 7220 | decorations.precision = spv::DecorationRelaxedPrecision; |
| 7221 | break; |
| 7222 | |
| 7223 | default: |
| 7224 | return 0; |
| 7225 | } |
| 7226 | |
| 7227 | spv::Id id; |
| 7228 | if (libCall >= 0) { |
| 7229 | std::vector<spv::Id> args; |
| 7230 | args.push_back(operand); |
| 7231 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
| 7232 | } else { |
| 7233 | id = builder.createUnaryOp(unaryOp, typeId, operand); |
| 7234 | } |
| 7235 | |
| 7236 | decorations.addNoContraction(builder, id); |
| 7237 | decorations.addNonUniform(builder, id); |
| 7238 | return builder.setPrecision(id, decorations.precision); |
| 7239 | } |
| 7240 | |
| 7241 | // Create a unary operation on a matrix |
| 7242 | spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 7243 | spv::Id operand, glslang::TBasicType /* typeProxy */) |
| 7244 | { |
| 7245 | // Handle unary operations vector by vector. |
| 7246 | // The result type is the same type as the original type. |
| 7247 | // The algorithm is to: |
| 7248 | // - break the matrix into vectors |
| 7249 | // - apply the operation to each vector |
| 7250 | // - make a matrix out the vector results |
| 7251 | |
| 7252 | // get the types sorted out |
| 7253 | int numCols = builder.getNumColumns(operand); |
| 7254 | int numRows = builder.getNumRows(operand); |
| 7255 | spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 7256 | spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
| 7257 | std::vector<spv::Id> results; |
| 7258 | |
| 7259 | // do each vector op |
| 7260 | for (int c = 0; c < numCols; ++c) { |
| 7261 | std::vector<unsigned int> indexes; |
| 7262 | indexes.push_back(c); |
| 7263 | spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 7264 | spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
| 7265 | decorations.addNoContraction(builder, destVec); |
| 7266 | decorations.addNonUniform(builder, destVec); |
| 7267 | results.push_back(builder.setPrecision(destVec, decorations.precision)); |
| 7268 | } |
| 7269 | |
| 7270 | // put the pieces together |
| 7271 | spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
| 7272 | decorations.addNonUniform(builder, result); |
| 7273 | return result; |
| 7274 | } |
| 7275 | |
| 7276 | // For converting integers where both the bitwidth and the signedness could |
| 7277 | // change, but only do the width change here. The caller is still responsible |
| 7278 | // for the signedness conversion. |
| 7279 | spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
| 7280 | { |
| 7281 | // Get the result type width, based on the type to convert to. |
| 7282 | int width = 32; |
| 7283 | switch(op) { |
| 7284 | case glslang::EOpConvInt16ToUint8: |
| 7285 | case glslang::EOpConvIntToUint8: |
| 7286 | case glslang::EOpConvInt64ToUint8: |
| 7287 | case glslang::EOpConvUint16ToInt8: |
| 7288 | case glslang::EOpConvUintToInt8: |
| 7289 | case glslang::EOpConvUint64ToInt8: |
| 7290 | width = 8; |
| 7291 | break; |
| 7292 | case glslang::EOpConvInt8ToUint16: |
| 7293 | case glslang::EOpConvIntToUint16: |
| 7294 | case glslang::EOpConvInt64ToUint16: |
| 7295 | case glslang::EOpConvUint8ToInt16: |
| 7296 | case glslang::EOpConvUintToInt16: |
| 7297 | case glslang::EOpConvUint64ToInt16: |
| 7298 | width = 16; |
| 7299 | break; |
| 7300 | case glslang::EOpConvInt8ToUint: |
| 7301 | case glslang::EOpConvInt16ToUint: |
| 7302 | case glslang::EOpConvInt64ToUint: |
| 7303 | case glslang::EOpConvUint8ToInt: |
| 7304 | case glslang::EOpConvUint16ToInt: |
| 7305 | case glslang::EOpConvUint64ToInt: |
| 7306 | width = 32; |
| 7307 | break; |
| 7308 | case glslang::EOpConvInt8ToUint64: |
| 7309 | case glslang::EOpConvInt16ToUint64: |
| 7310 | case glslang::EOpConvIntToUint64: |
| 7311 | case glslang::EOpConvUint8ToInt64: |
| 7312 | case glslang::EOpConvUint16ToInt64: |
| 7313 | case glslang::EOpConvUintToInt64: |
| 7314 | width = 64; |
| 7315 | break; |
| 7316 | |
| 7317 | default: |
| 7318 | assert(false && "Default missing" ); |
| 7319 | break; |
| 7320 | } |
| 7321 | |
| 7322 | // Get the conversion operation and result type, |
| 7323 | // based on the target width, but the source type. |
| 7324 | spv::Id type = spv::NoType; |
| 7325 | spv::Op convOp = spv::OpNop; |
| 7326 | switch(op) { |
| 7327 | case glslang::EOpConvInt8ToUint16: |
| 7328 | case glslang::EOpConvInt8ToUint: |
| 7329 | case glslang::EOpConvInt8ToUint64: |
| 7330 | case glslang::EOpConvInt16ToUint8: |
| 7331 | case glslang::EOpConvInt16ToUint: |
| 7332 | case glslang::EOpConvInt16ToUint64: |
| 7333 | case glslang::EOpConvIntToUint8: |
| 7334 | case glslang::EOpConvIntToUint16: |
| 7335 | case glslang::EOpConvIntToUint64: |
| 7336 | case glslang::EOpConvInt64ToUint8: |
| 7337 | case glslang::EOpConvInt64ToUint16: |
| 7338 | case glslang::EOpConvInt64ToUint: |
| 7339 | convOp = spv::OpSConvert; |
| 7340 | type = builder.makeIntType(width); |
| 7341 | break; |
| 7342 | default: |
| 7343 | convOp = spv::OpUConvert; |
| 7344 | type = builder.makeUintType(width); |
| 7345 | break; |
| 7346 | } |
| 7347 | |
| 7348 | if (vectorSize > 0) |
| 7349 | type = builder.makeVectorType(type, vectorSize); |
| 7350 | |
| 7351 | return builder.createUnaryOp(convOp, type, operand); |
| 7352 | } |
| 7353 | |
| 7354 | spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 7355 | spv::Id operand, glslang::TBasicType typeProxy) |
| 7356 | { |
| 7357 | spv::Op convOp = spv::OpNop; |
| 7358 | spv::Id zero = 0; |
| 7359 | spv::Id one = 0; |
| 7360 | |
| 7361 | int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 7362 | |
| 7363 | switch (op) { |
| 7364 | case glslang::EOpConvIntToBool: |
| 7365 | case glslang::EOpConvUintToBool: |
| 7366 | zero = builder.makeUintConstant(0); |
| 7367 | zero = makeSmearedConstant(zero, vectorSize); |
| 7368 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 7369 | case glslang::EOpConvFloatToBool: |
| 7370 | zero = builder.makeFloatConstant(0.0F); |
| 7371 | zero = makeSmearedConstant(zero, vectorSize); |
| 7372 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
| 7373 | case glslang::EOpConvBoolToFloat: |
| 7374 | convOp = spv::OpSelect; |
| 7375 | zero = builder.makeFloatConstant(0.0F); |
| 7376 | one = builder.makeFloatConstant(1.0F); |
| 7377 | break; |
| 7378 | |
| 7379 | case glslang::EOpConvBoolToInt: |
| 7380 | case glslang::EOpConvBoolToInt64: |
| 7381 | #ifndef GLSLANG_WEB |
| 7382 | if (op == glslang::EOpConvBoolToInt64) { |
| 7383 | zero = builder.makeInt64Constant(0); |
| 7384 | one = builder.makeInt64Constant(1); |
| 7385 | } else |
| 7386 | #endif |
| 7387 | { |
| 7388 | zero = builder.makeIntConstant(0); |
| 7389 | one = builder.makeIntConstant(1); |
| 7390 | } |
| 7391 | |
| 7392 | convOp = spv::OpSelect; |
| 7393 | break; |
| 7394 | |
| 7395 | case glslang::EOpConvBoolToUint: |
| 7396 | case glslang::EOpConvBoolToUint64: |
| 7397 | #ifndef GLSLANG_WEB |
| 7398 | if (op == glslang::EOpConvBoolToUint64) { |
| 7399 | zero = builder.makeUint64Constant(0); |
| 7400 | one = builder.makeUint64Constant(1); |
| 7401 | } else |
| 7402 | #endif |
| 7403 | { |
| 7404 | zero = builder.makeUintConstant(0); |
| 7405 | one = builder.makeUintConstant(1); |
| 7406 | } |
| 7407 | |
| 7408 | convOp = spv::OpSelect; |
| 7409 | break; |
| 7410 | |
| 7411 | case glslang::EOpConvInt8ToFloat16: |
| 7412 | case glslang::EOpConvInt8ToFloat: |
| 7413 | case glslang::EOpConvInt8ToDouble: |
| 7414 | case glslang::EOpConvInt16ToFloat16: |
| 7415 | case glslang::EOpConvInt16ToFloat: |
| 7416 | case glslang::EOpConvInt16ToDouble: |
| 7417 | case glslang::EOpConvIntToFloat16: |
| 7418 | case glslang::EOpConvIntToFloat: |
| 7419 | case glslang::EOpConvIntToDouble: |
| 7420 | case glslang::EOpConvInt64ToFloat: |
| 7421 | case glslang::EOpConvInt64ToDouble: |
| 7422 | case glslang::EOpConvInt64ToFloat16: |
| 7423 | convOp = spv::OpConvertSToF; |
| 7424 | break; |
| 7425 | |
| 7426 | case glslang::EOpConvUint8ToFloat16: |
| 7427 | case glslang::EOpConvUint8ToFloat: |
| 7428 | case glslang::EOpConvUint8ToDouble: |
| 7429 | case glslang::EOpConvUint16ToFloat16: |
| 7430 | case glslang::EOpConvUint16ToFloat: |
| 7431 | case glslang::EOpConvUint16ToDouble: |
| 7432 | case glslang::EOpConvUintToFloat16: |
| 7433 | case glslang::EOpConvUintToFloat: |
| 7434 | case glslang::EOpConvUintToDouble: |
| 7435 | case glslang::EOpConvUint64ToFloat: |
| 7436 | case glslang::EOpConvUint64ToDouble: |
| 7437 | case glslang::EOpConvUint64ToFloat16: |
| 7438 | convOp = spv::OpConvertUToF; |
| 7439 | break; |
| 7440 | |
| 7441 | case glslang::EOpConvFloat16ToInt8: |
| 7442 | case glslang::EOpConvFloatToInt8: |
| 7443 | case glslang::EOpConvDoubleToInt8: |
| 7444 | case glslang::EOpConvFloat16ToInt16: |
| 7445 | case glslang::EOpConvFloatToInt16: |
| 7446 | case glslang::EOpConvDoubleToInt16: |
| 7447 | case glslang::EOpConvFloat16ToInt: |
| 7448 | case glslang::EOpConvFloatToInt: |
| 7449 | case glslang::EOpConvDoubleToInt: |
| 7450 | case glslang::EOpConvFloat16ToInt64: |
| 7451 | case glslang::EOpConvFloatToInt64: |
| 7452 | case glslang::EOpConvDoubleToInt64: |
| 7453 | convOp = spv::OpConvertFToS; |
| 7454 | break; |
| 7455 | |
| 7456 | case glslang::EOpConvUint8ToInt8: |
| 7457 | case glslang::EOpConvInt8ToUint8: |
| 7458 | case glslang::EOpConvUint16ToInt16: |
| 7459 | case glslang::EOpConvInt16ToUint16: |
| 7460 | case glslang::EOpConvUintToInt: |
| 7461 | case glslang::EOpConvIntToUint: |
| 7462 | case glslang::EOpConvUint64ToInt64: |
| 7463 | case glslang::EOpConvInt64ToUint64: |
| 7464 | if (builder.isInSpecConstCodeGenMode()) { |
| 7465 | // Build zero scalar or vector for OpIAdd. |
| 7466 | #ifndef GLSLANG_WEB |
| 7467 | if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 7468 | zero = builder.makeUint8Constant(0); |
| 7469 | } else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
| 7470 | zero = builder.makeUint16Constant(0); |
| 7471 | } else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 7472 | zero = builder.makeUint64Constant(0); |
| 7473 | } else |
| 7474 | #endif |
| 7475 | { |
| 7476 | zero = builder.makeUintConstant(0); |
| 7477 | } |
| 7478 | zero = makeSmearedConstant(zero, vectorSize); |
| 7479 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 7480 | // generating for OpSpecConstantOp instruction. |
| 7481 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 7482 | } |
| 7483 | // For normal run-time conversion instruction, use OpBitcast. |
| 7484 | convOp = spv::OpBitcast; |
| 7485 | break; |
| 7486 | |
| 7487 | case glslang::EOpConvFloat16ToUint8: |
| 7488 | case glslang::EOpConvFloatToUint8: |
| 7489 | case glslang::EOpConvDoubleToUint8: |
| 7490 | case glslang::EOpConvFloat16ToUint16: |
| 7491 | case glslang::EOpConvFloatToUint16: |
| 7492 | case glslang::EOpConvDoubleToUint16: |
| 7493 | case glslang::EOpConvFloat16ToUint: |
| 7494 | case glslang::EOpConvFloatToUint: |
| 7495 | case glslang::EOpConvDoubleToUint: |
| 7496 | case glslang::EOpConvFloatToUint64: |
| 7497 | case glslang::EOpConvDoubleToUint64: |
| 7498 | case glslang::EOpConvFloat16ToUint64: |
| 7499 | convOp = spv::OpConvertFToU; |
| 7500 | break; |
| 7501 | |
| 7502 | #ifndef GLSLANG_WEB |
| 7503 | case glslang::EOpConvInt8ToBool: |
| 7504 | case glslang::EOpConvUint8ToBool: |
| 7505 | zero = builder.makeUint8Constant(0); |
| 7506 | zero = makeSmearedConstant(zero, vectorSize); |
| 7507 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 7508 | case glslang::EOpConvInt16ToBool: |
| 7509 | case glslang::EOpConvUint16ToBool: |
| 7510 | zero = builder.makeUint16Constant(0); |
| 7511 | zero = makeSmearedConstant(zero, vectorSize); |
| 7512 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 7513 | case glslang::EOpConvInt64ToBool: |
| 7514 | case glslang::EOpConvUint64ToBool: |
| 7515 | zero = builder.makeUint64Constant(0); |
| 7516 | zero = makeSmearedConstant(zero, vectorSize); |
| 7517 | return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 7518 | case glslang::EOpConvDoubleToBool: |
| 7519 | zero = builder.makeDoubleConstant(0.0); |
| 7520 | zero = makeSmearedConstant(zero, vectorSize); |
| 7521 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
| 7522 | case glslang::EOpConvFloat16ToBool: |
| 7523 | zero = builder.makeFloat16Constant(0.0F); |
| 7524 | zero = makeSmearedConstant(zero, vectorSize); |
| 7525 | return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
| 7526 | case glslang::EOpConvBoolToDouble: |
| 7527 | convOp = spv::OpSelect; |
| 7528 | zero = builder.makeDoubleConstant(0.0); |
| 7529 | one = builder.makeDoubleConstant(1.0); |
| 7530 | break; |
| 7531 | case glslang::EOpConvBoolToFloat16: |
| 7532 | convOp = spv::OpSelect; |
| 7533 | zero = builder.makeFloat16Constant(0.0F); |
| 7534 | one = builder.makeFloat16Constant(1.0F); |
| 7535 | break; |
| 7536 | case glslang::EOpConvBoolToInt8: |
| 7537 | zero = builder.makeInt8Constant(0); |
| 7538 | one = builder.makeInt8Constant(1); |
| 7539 | convOp = spv::OpSelect; |
| 7540 | break; |
| 7541 | case glslang::EOpConvBoolToUint8: |
| 7542 | zero = builder.makeUint8Constant(0); |
| 7543 | one = builder.makeUint8Constant(1); |
| 7544 | convOp = spv::OpSelect; |
| 7545 | break; |
| 7546 | case glslang::EOpConvBoolToInt16: |
| 7547 | zero = builder.makeInt16Constant(0); |
| 7548 | one = builder.makeInt16Constant(1); |
| 7549 | convOp = spv::OpSelect; |
| 7550 | break; |
| 7551 | case glslang::EOpConvBoolToUint16: |
| 7552 | zero = builder.makeUint16Constant(0); |
| 7553 | one = builder.makeUint16Constant(1); |
| 7554 | convOp = spv::OpSelect; |
| 7555 | break; |
| 7556 | case glslang::EOpConvDoubleToFloat: |
| 7557 | case glslang::EOpConvFloatToDouble: |
| 7558 | case glslang::EOpConvDoubleToFloat16: |
| 7559 | case glslang::EOpConvFloat16ToDouble: |
| 7560 | case glslang::EOpConvFloatToFloat16: |
| 7561 | case glslang::EOpConvFloat16ToFloat: |
| 7562 | convOp = spv::OpFConvert; |
| 7563 | if (builder.isMatrixType(destType)) |
| 7564 | return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
| 7565 | break; |
| 7566 | |
| 7567 | case glslang::EOpConvInt8ToInt16: |
| 7568 | case glslang::EOpConvInt8ToInt: |
| 7569 | case glslang::EOpConvInt8ToInt64: |
| 7570 | case glslang::EOpConvInt16ToInt8: |
| 7571 | case glslang::EOpConvInt16ToInt: |
| 7572 | case glslang::EOpConvInt16ToInt64: |
| 7573 | case glslang::EOpConvIntToInt8: |
| 7574 | case glslang::EOpConvIntToInt16: |
| 7575 | case glslang::EOpConvIntToInt64: |
| 7576 | case glslang::EOpConvInt64ToInt8: |
| 7577 | case glslang::EOpConvInt64ToInt16: |
| 7578 | case glslang::EOpConvInt64ToInt: |
| 7579 | convOp = spv::OpSConvert; |
| 7580 | break; |
| 7581 | |
| 7582 | case glslang::EOpConvUint8ToUint16: |
| 7583 | case glslang::EOpConvUint8ToUint: |
| 7584 | case glslang::EOpConvUint8ToUint64: |
| 7585 | case glslang::EOpConvUint16ToUint8: |
| 7586 | case glslang::EOpConvUint16ToUint: |
| 7587 | case glslang::EOpConvUint16ToUint64: |
| 7588 | case glslang::EOpConvUintToUint8: |
| 7589 | case glslang::EOpConvUintToUint16: |
| 7590 | case glslang::EOpConvUintToUint64: |
| 7591 | case glslang::EOpConvUint64ToUint8: |
| 7592 | case glslang::EOpConvUint64ToUint16: |
| 7593 | case glslang::EOpConvUint64ToUint: |
| 7594 | convOp = spv::OpUConvert; |
| 7595 | break; |
| 7596 | |
| 7597 | case glslang::EOpConvInt8ToUint16: |
| 7598 | case glslang::EOpConvInt8ToUint: |
| 7599 | case glslang::EOpConvInt8ToUint64: |
| 7600 | case glslang::EOpConvInt16ToUint8: |
| 7601 | case glslang::EOpConvInt16ToUint: |
| 7602 | case glslang::EOpConvInt16ToUint64: |
| 7603 | case glslang::EOpConvIntToUint8: |
| 7604 | case glslang::EOpConvIntToUint16: |
| 7605 | case glslang::EOpConvIntToUint64: |
| 7606 | case glslang::EOpConvInt64ToUint8: |
| 7607 | case glslang::EOpConvInt64ToUint16: |
| 7608 | case glslang::EOpConvInt64ToUint: |
| 7609 | case glslang::EOpConvUint8ToInt16: |
| 7610 | case glslang::EOpConvUint8ToInt: |
| 7611 | case glslang::EOpConvUint8ToInt64: |
| 7612 | case glslang::EOpConvUint16ToInt8: |
| 7613 | case glslang::EOpConvUint16ToInt: |
| 7614 | case glslang::EOpConvUint16ToInt64: |
| 7615 | case glslang::EOpConvUintToInt8: |
| 7616 | case glslang::EOpConvUintToInt16: |
| 7617 | case glslang::EOpConvUintToInt64: |
| 7618 | case glslang::EOpConvUint64ToInt8: |
| 7619 | case glslang::EOpConvUint64ToInt16: |
| 7620 | case glslang::EOpConvUint64ToInt: |
| 7621 | // OpSConvert/OpUConvert + OpBitCast |
| 7622 | operand = createIntWidthConversion(op, operand, vectorSize); |
| 7623 | |
| 7624 | if (builder.isInSpecConstCodeGenMode()) { |
| 7625 | // Build zero scalar or vector for OpIAdd. |
| 7626 | switch(op) { |
| 7627 | case glslang::EOpConvInt16ToUint8: |
| 7628 | case glslang::EOpConvIntToUint8: |
| 7629 | case glslang::EOpConvInt64ToUint8: |
| 7630 | case glslang::EOpConvUint16ToInt8: |
| 7631 | case glslang::EOpConvUintToInt8: |
| 7632 | case glslang::EOpConvUint64ToInt8: |
| 7633 | zero = builder.makeUint8Constant(0); |
| 7634 | break; |
| 7635 | case glslang::EOpConvInt8ToUint16: |
| 7636 | case glslang::EOpConvIntToUint16: |
| 7637 | case glslang::EOpConvInt64ToUint16: |
| 7638 | case glslang::EOpConvUint8ToInt16: |
| 7639 | case glslang::EOpConvUintToInt16: |
| 7640 | case glslang::EOpConvUint64ToInt16: |
| 7641 | zero = builder.makeUint16Constant(0); |
| 7642 | break; |
| 7643 | case glslang::EOpConvInt8ToUint: |
| 7644 | case glslang::EOpConvInt16ToUint: |
| 7645 | case glslang::EOpConvInt64ToUint: |
| 7646 | case glslang::EOpConvUint8ToInt: |
| 7647 | case glslang::EOpConvUint16ToInt: |
| 7648 | case glslang::EOpConvUint64ToInt: |
| 7649 | zero = builder.makeUintConstant(0); |
| 7650 | break; |
| 7651 | case glslang::EOpConvInt8ToUint64: |
| 7652 | case glslang::EOpConvInt16ToUint64: |
| 7653 | case glslang::EOpConvIntToUint64: |
| 7654 | case glslang::EOpConvUint8ToInt64: |
| 7655 | case glslang::EOpConvUint16ToInt64: |
| 7656 | case glslang::EOpConvUintToInt64: |
| 7657 | zero = builder.makeUint64Constant(0); |
| 7658 | break; |
| 7659 | default: |
| 7660 | assert(false && "Default missing" ); |
| 7661 | break; |
| 7662 | } |
| 7663 | zero = makeSmearedConstant(zero, vectorSize); |
| 7664 | // Use OpIAdd, instead of OpBitcast to do the conversion when |
| 7665 | // generating for OpSpecConstantOp instruction. |
| 7666 | return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 7667 | } |
| 7668 | // For normal run-time conversion instruction, use OpBitcast. |
| 7669 | convOp = spv::OpBitcast; |
| 7670 | break; |
| 7671 | case glslang::EOpConvUint64ToPtr: |
| 7672 | convOp = spv::OpConvertUToPtr; |
| 7673 | break; |
| 7674 | case glslang::EOpConvPtrToUint64: |
| 7675 | convOp = spv::OpConvertPtrToU; |
| 7676 | break; |
| 7677 | case glslang::EOpConvPtrToUvec2: |
| 7678 | case glslang::EOpConvUvec2ToPtr: |
| 7679 | convOp = spv::OpBitcast; |
| 7680 | break; |
| 7681 | #endif |
| 7682 | |
| 7683 | default: |
| 7684 | break; |
| 7685 | } |
| 7686 | |
| 7687 | spv::Id result = 0; |
| 7688 | if (convOp == spv::OpNop) |
| 7689 | return result; |
| 7690 | |
| 7691 | if (convOp == spv::OpSelect) { |
| 7692 | zero = makeSmearedConstant(zero, vectorSize); |
| 7693 | one = makeSmearedConstant(one, vectorSize); |
| 7694 | result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 7695 | } else |
| 7696 | result = builder.createUnaryOp(convOp, destType, operand); |
| 7697 | |
| 7698 | result = builder.setPrecision(result, decorations.precision); |
| 7699 | decorations.addNonUniform(builder, result); |
| 7700 | return result; |
| 7701 | } |
| 7702 | |
| 7703 | spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 7704 | { |
| 7705 | if (vectorSize == 0) |
| 7706 | return constant; |
| 7707 | |
| 7708 | spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 7709 | std::vector<spv::Id> components; |
| 7710 | for (int c = 0; c < vectorSize; ++c) |
| 7711 | components.push_back(constant); |
| 7712 | return builder.makeCompositeConstant(vectorTypeId, components); |
| 7713 | } |
| 7714 | |
| 7715 | // For glslang ops that map to SPV atomic opCodes |
| 7716 | spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, |
| 7717 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 7718 | const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
| 7719 | { |
| 7720 | spv::Op opCode = spv::OpNop; |
| 7721 | |
| 7722 | switch (op) { |
| 7723 | case glslang::EOpAtomicAdd: |
| 7724 | case glslang::EOpImageAtomicAdd: |
| 7725 | case glslang::EOpAtomicCounterAdd: |
| 7726 | opCode = spv::OpAtomicIAdd; |
| 7727 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
| 7728 | opCode = spv::OpAtomicFAddEXT; |
| 7729 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_add); |
| 7730 | if (typeProxy == glslang::EbtFloat16) { |
| 7731 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float16_add); |
| 7732 | builder.addCapability(spv::CapabilityAtomicFloat16AddEXT); |
| 7733 | } else if (typeProxy == glslang::EbtFloat) { |
| 7734 | builder.addCapability(spv::CapabilityAtomicFloat32AddEXT); |
| 7735 | } else { |
| 7736 | builder.addCapability(spv::CapabilityAtomicFloat64AddEXT); |
| 7737 | } |
| 7738 | } |
| 7739 | break; |
| 7740 | case glslang::EOpAtomicSubtract: |
| 7741 | case glslang::EOpAtomicCounterSubtract: |
| 7742 | opCode = spv::OpAtomicISub; |
| 7743 | break; |
| 7744 | case glslang::EOpAtomicMin: |
| 7745 | case glslang::EOpImageAtomicMin: |
| 7746 | case glslang::EOpAtomicCounterMin: |
| 7747 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
| 7748 | opCode = spv::OpAtomicFMinEXT; |
| 7749 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_min_max); |
| 7750 | if (typeProxy == glslang::EbtFloat16) |
| 7751 | builder.addCapability(spv::CapabilityAtomicFloat16MinMaxEXT); |
| 7752 | else if (typeProxy == glslang::EbtFloat) |
| 7753 | builder.addCapability(spv::CapabilityAtomicFloat32MinMaxEXT); |
| 7754 | else |
| 7755 | builder.addCapability(spv::CapabilityAtomicFloat64MinMaxEXT); |
| 7756 | } else if (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) { |
| 7757 | opCode = spv::OpAtomicUMin; |
| 7758 | } else { |
| 7759 | opCode = spv::OpAtomicSMin; |
| 7760 | } |
| 7761 | break; |
| 7762 | case glslang::EOpAtomicMax: |
| 7763 | case glslang::EOpImageAtomicMax: |
| 7764 | case glslang::EOpAtomicCounterMax: |
| 7765 | if (typeProxy == glslang::EbtFloat16 || typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
| 7766 | opCode = spv::OpAtomicFMaxEXT; |
| 7767 | builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_min_max); |
| 7768 | if (typeProxy == glslang::EbtFloat16) |
| 7769 | builder.addCapability(spv::CapabilityAtomicFloat16MinMaxEXT); |
| 7770 | else if (typeProxy == glslang::EbtFloat) |
| 7771 | builder.addCapability(spv::CapabilityAtomicFloat32MinMaxEXT); |
| 7772 | else |
| 7773 | builder.addCapability(spv::CapabilityAtomicFloat64MinMaxEXT); |
| 7774 | } else if (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) { |
| 7775 | opCode = spv::OpAtomicUMax; |
| 7776 | } else { |
| 7777 | opCode = spv::OpAtomicSMax; |
| 7778 | } |
| 7779 | break; |
| 7780 | case glslang::EOpAtomicAnd: |
| 7781 | case glslang::EOpImageAtomicAnd: |
| 7782 | case glslang::EOpAtomicCounterAnd: |
| 7783 | opCode = spv::OpAtomicAnd; |
| 7784 | break; |
| 7785 | case glslang::EOpAtomicOr: |
| 7786 | case glslang::EOpImageAtomicOr: |
| 7787 | case glslang::EOpAtomicCounterOr: |
| 7788 | opCode = spv::OpAtomicOr; |
| 7789 | break; |
| 7790 | case glslang::EOpAtomicXor: |
| 7791 | case glslang::EOpImageAtomicXor: |
| 7792 | case glslang::EOpAtomicCounterXor: |
| 7793 | opCode = spv::OpAtomicXor; |
| 7794 | break; |
| 7795 | case glslang::EOpAtomicExchange: |
| 7796 | case glslang::EOpImageAtomicExchange: |
| 7797 | case glslang::EOpAtomicCounterExchange: |
| 7798 | opCode = spv::OpAtomicExchange; |
| 7799 | break; |
| 7800 | case glslang::EOpAtomicCompSwap: |
| 7801 | case glslang::EOpImageAtomicCompSwap: |
| 7802 | case glslang::EOpAtomicCounterCompSwap: |
| 7803 | opCode = spv::OpAtomicCompareExchange; |
| 7804 | break; |
| 7805 | case glslang::EOpAtomicCounterIncrement: |
| 7806 | opCode = spv::OpAtomicIIncrement; |
| 7807 | break; |
| 7808 | case glslang::EOpAtomicCounterDecrement: |
| 7809 | opCode = spv::OpAtomicIDecrement; |
| 7810 | break; |
| 7811 | case glslang::EOpAtomicCounter: |
| 7812 | case glslang::EOpImageAtomicLoad: |
| 7813 | case glslang::EOpAtomicLoad: |
| 7814 | opCode = spv::OpAtomicLoad; |
| 7815 | break; |
| 7816 | case glslang::EOpAtomicStore: |
| 7817 | case glslang::EOpImageAtomicStore: |
| 7818 | opCode = spv::OpAtomicStore; |
| 7819 | break; |
| 7820 | default: |
| 7821 | assert(0); |
| 7822 | break; |
| 7823 | } |
| 7824 | |
| 7825 | if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 7826 | builder.addCapability(spv::CapabilityInt64Atomics); |
| 7827 | |
| 7828 | // Sort out the operands |
| 7829 | // - mapping from glslang -> SPV |
| 7830 | // - there are extra SPV operands that are optional in glslang |
| 7831 | // - compare-exchange swaps the value and comparator |
| 7832 | // - compare-exchange has an extra memory semantics |
| 7833 | // - EOpAtomicCounterDecrement needs a post decrement |
| 7834 | spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 7835 | // scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 7836 | spv::Id scopeId; |
| 7837 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7838 | scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 7839 | } else { |
| 7840 | scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 7841 | } |
| 7842 | // semantics default to relaxed |
| 7843 | spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() && |
| 7844 | glslangIntermediate->usingVulkanMemoryModel() ? |
| 7845 | spv::MemorySemanticsVolatileMask : |
| 7846 | spv::MemorySemanticsMaskNone); |
| 7847 | spv::Id semanticsId2 = semanticsId; |
| 7848 | |
| 7849 | pointerId = operands[0]; |
| 7850 | if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 7851 | // no additional operands |
| 7852 | } else if (opCode == spv::OpAtomicCompareExchange) { |
| 7853 | compareId = operands[1]; |
| 7854 | valueId = operands[2]; |
| 7855 | if (operands.size() > 3) { |
| 7856 | scopeId = operands[3]; |
| 7857 | semanticsId = builder.makeUintConstant( |
| 7858 | builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 7859 | semanticsId2 = builder.makeUintConstant( |
| 7860 | builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 7861 | } |
| 7862 | } else if (opCode == spv::OpAtomicLoad) { |
| 7863 | if (operands.size() > 1) { |
| 7864 | scopeId = operands[1]; |
| 7865 | semanticsId = builder.makeUintConstant( |
| 7866 | builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 7867 | } |
| 7868 | } else { |
| 7869 | // atomic store or RMW |
| 7870 | valueId = operands[1]; |
| 7871 | if (operands.size() > 2) { |
| 7872 | scopeId = operands[2]; |
| 7873 | semanticsId = builder.makeUintConstant |
| 7874 | (builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 7875 | } |
| 7876 | } |
| 7877 | |
| 7878 | // Check for capabilities |
| 7879 | unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
| 7880 | if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7881 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 7882 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 7883 | spv::MemorySemanticsVolatileMask)) { |
| 7884 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7885 | } |
| 7886 | |
| 7887 | if (builder.getConstantScalar(scopeId) == spv::ScopeQueueFamily) { |
| 7888 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7889 | } |
| 7890 | |
| 7891 | if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 7892 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7893 | } |
| 7894 | |
| 7895 | std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 7896 | spvAtomicOperands.push_back(pointerId); |
| 7897 | spvAtomicOperands.push_back(scopeId); |
| 7898 | spvAtomicOperands.push_back(semanticsId); |
| 7899 | if (opCode == spv::OpAtomicCompareExchange) { |
| 7900 | spvAtomicOperands.push_back(semanticsId2); |
| 7901 | spvAtomicOperands.push_back(valueId); |
| 7902 | spvAtomicOperands.push_back(compareId); |
| 7903 | } else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 7904 | spvAtomicOperands.push_back(valueId); |
| 7905 | } |
| 7906 | |
| 7907 | if (opCode == spv::OpAtomicStore) { |
| 7908 | builder.createNoResultOp(opCode, spvAtomicOperands); |
| 7909 | return 0; |
| 7910 | } else { |
| 7911 | spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 7912 | |
| 7913 | // GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 7914 | // while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 7915 | if (op == glslang::EOpAtomicCounterDecrement) |
| 7916 | resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 7917 | |
| 7918 | return resultId; |
| 7919 | } |
| 7920 | } |
| 7921 | |
| 7922 | // Create group invocation operations. |
| 7923 | spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, |
| 7924 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
| 7925 | { |
| 7926 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7927 | bool isFloat = isTypeFloat(typeProxy); |
| 7928 | |
| 7929 | spv::Op opCode = spv::OpNop; |
| 7930 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 7931 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 7932 | |
| 7933 | if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 7934 | op == glslang::EOpReadInvocation) { |
| 7935 | builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 7936 | builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 7937 | } else if (op == glslang::EOpAnyInvocation || |
| 7938 | op == glslang::EOpAllInvocations || |
| 7939 | op == glslang::EOpAllInvocationsEqual) { |
| 7940 | builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 7941 | builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
| 7942 | } else { |
| 7943 | builder.addCapability(spv::CapabilityGroups); |
| 7944 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 7945 | op == glslang::EOpMaxInvocationsNonUniform || |
| 7946 | op == glslang::EOpAddInvocationsNonUniform || |
| 7947 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 7948 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 7949 | op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 7950 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 7951 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 7952 | op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
| 7953 | builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
| 7954 | |
| 7955 | switch (op) { |
| 7956 | case glslang::EOpMinInvocations: |
| 7957 | case glslang::EOpMaxInvocations: |
| 7958 | case glslang::EOpAddInvocations: |
| 7959 | case glslang::EOpMinInvocationsNonUniform: |
| 7960 | case glslang::EOpMaxInvocationsNonUniform: |
| 7961 | case glslang::EOpAddInvocationsNonUniform: |
| 7962 | groupOperation = spv::GroupOperationReduce; |
| 7963 | break; |
| 7964 | case glslang::EOpMinInvocationsInclusiveScan: |
| 7965 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 7966 | case glslang::EOpAddInvocationsInclusiveScan: |
| 7967 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 7968 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 7969 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 7970 | groupOperation = spv::GroupOperationInclusiveScan; |
| 7971 | break; |
| 7972 | case glslang::EOpMinInvocationsExclusiveScan: |
| 7973 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 7974 | case glslang::EOpAddInvocationsExclusiveScan: |
| 7975 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 7976 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 7977 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 7978 | groupOperation = spv::GroupOperationExclusiveScan; |
| 7979 | break; |
| 7980 | default: |
| 7981 | break; |
| 7982 | } |
| 7983 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7984 | spvGroupOperands.push_back(scope); |
| 7985 | if (groupOperation != spv::GroupOperationMax) { |
| 7986 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
| 7987 | spvGroupOperands.push_back(groupOp); |
| 7988 | } |
| 7989 | } |
| 7990 | |
| 7991 | for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 7992 | spv::IdImmediate op = { true, *opIt }; |
| 7993 | spvGroupOperands.push_back(op); |
| 7994 | } |
| 7995 | |
| 7996 | switch (op) { |
| 7997 | case glslang::EOpAnyInvocation: |
| 7998 | opCode = spv::OpSubgroupAnyKHR; |
| 7999 | break; |
| 8000 | case glslang::EOpAllInvocations: |
| 8001 | opCode = spv::OpSubgroupAllKHR; |
| 8002 | break; |
| 8003 | case glslang::EOpAllInvocationsEqual: |
| 8004 | opCode = spv::OpSubgroupAllEqualKHR; |
| 8005 | break; |
| 8006 | case glslang::EOpReadInvocation: |
| 8007 | opCode = spv::OpSubgroupReadInvocationKHR; |
| 8008 | if (builder.isVectorType(typeId)) |
| 8009 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
| 8010 | break; |
| 8011 | case glslang::EOpReadFirstInvocation: |
| 8012 | opCode = spv::OpSubgroupFirstInvocationKHR; |
| 8013 | if (builder.isVectorType(typeId)) |
| 8014 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
| 8015 | break; |
| 8016 | case glslang::EOpBallot: |
| 8017 | { |
| 8018 | // NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 8019 | // bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 8020 | // a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 8021 | // |
| 8022 | // result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 8023 | // |
| 8024 | spv::Id uintType = builder.makeUintType(32); |
| 8025 | spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 8026 | spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 8027 | |
| 8028 | std::vector<spv::Id> components; |
| 8029 | components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 8030 | components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 8031 | |
| 8032 | spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 8033 | return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 8034 | builder.createCompositeConstruct(uvec2Type, components)); |
| 8035 | } |
| 8036 | |
| 8037 | case glslang::EOpMinInvocations: |
| 8038 | case glslang::EOpMaxInvocations: |
| 8039 | case glslang::EOpAddInvocations: |
| 8040 | case glslang::EOpMinInvocationsInclusiveScan: |
| 8041 | case glslang::EOpMaxInvocationsInclusiveScan: |
| 8042 | case glslang::EOpAddInvocationsInclusiveScan: |
| 8043 | case glslang::EOpMinInvocationsExclusiveScan: |
| 8044 | case glslang::EOpMaxInvocationsExclusiveScan: |
| 8045 | case glslang::EOpAddInvocationsExclusiveScan: |
| 8046 | if (op == glslang::EOpMinInvocations || |
| 8047 | op == glslang::EOpMinInvocationsInclusiveScan || |
| 8048 | op == glslang::EOpMinInvocationsExclusiveScan) { |
| 8049 | if (isFloat) |
| 8050 | opCode = spv::OpGroupFMin; |
| 8051 | else { |
| 8052 | if (isUnsigned) |
| 8053 | opCode = spv::OpGroupUMin; |
| 8054 | else |
| 8055 | opCode = spv::OpGroupSMin; |
| 8056 | } |
| 8057 | } else if (op == glslang::EOpMaxInvocations || |
| 8058 | op == glslang::EOpMaxInvocationsInclusiveScan || |
| 8059 | op == glslang::EOpMaxInvocationsExclusiveScan) { |
| 8060 | if (isFloat) |
| 8061 | opCode = spv::OpGroupFMax; |
| 8062 | else { |
| 8063 | if (isUnsigned) |
| 8064 | opCode = spv::OpGroupUMax; |
| 8065 | else |
| 8066 | opCode = spv::OpGroupSMax; |
| 8067 | } |
| 8068 | } else { |
| 8069 | if (isFloat) |
| 8070 | opCode = spv::OpGroupFAdd; |
| 8071 | else |
| 8072 | opCode = spv::OpGroupIAdd; |
| 8073 | } |
| 8074 | |
| 8075 | if (builder.isVectorType(typeId)) |
| 8076 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
| 8077 | |
| 8078 | break; |
| 8079 | case glslang::EOpMinInvocationsNonUniform: |
| 8080 | case glslang::EOpMaxInvocationsNonUniform: |
| 8081 | case glslang::EOpAddInvocationsNonUniform: |
| 8082 | case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 8083 | case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 8084 | case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 8085 | case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 8086 | case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 8087 | case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 8088 | if (op == glslang::EOpMinInvocationsNonUniform || |
| 8089 | op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 8090 | op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
| 8091 | if (isFloat) |
| 8092 | opCode = spv::OpGroupFMinNonUniformAMD; |
| 8093 | else { |
| 8094 | if (isUnsigned) |
| 8095 | opCode = spv::OpGroupUMinNonUniformAMD; |
| 8096 | else |
| 8097 | opCode = spv::OpGroupSMinNonUniformAMD; |
| 8098 | } |
| 8099 | } |
| 8100 | else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 8101 | op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 8102 | op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
| 8103 | if (isFloat) |
| 8104 | opCode = spv::OpGroupFMaxNonUniformAMD; |
| 8105 | else { |
| 8106 | if (isUnsigned) |
| 8107 | opCode = spv::OpGroupUMaxNonUniformAMD; |
| 8108 | else |
| 8109 | opCode = spv::OpGroupSMaxNonUniformAMD; |
| 8110 | } |
| 8111 | } |
| 8112 | else { |
| 8113 | if (isFloat) |
| 8114 | opCode = spv::OpGroupFAddNonUniformAMD; |
| 8115 | else |
| 8116 | opCode = spv::OpGroupIAddNonUniformAMD; |
| 8117 | } |
| 8118 | |
| 8119 | if (builder.isVectorType(typeId)) |
| 8120 | return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
| 8121 | |
| 8122 | break; |
| 8123 | default: |
| 8124 | logger->missingFunctionality("invocation operation" ); |
| 8125 | return spv::NoResult; |
| 8126 | } |
| 8127 | |
| 8128 | assert(opCode != spv::OpNop); |
| 8129 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 8130 | } |
| 8131 | |
| 8132 | // Create group invocation operations on a vector |
| 8133 | spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 8134 | spv::Id typeId, std::vector<spv::Id>& operands) |
| 8135 | { |
| 8136 | assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 8137 | op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
| 8138 | op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 8139 | op == spv::OpSubgroupReadInvocationKHR || op == spv::OpSubgroupFirstInvocationKHR || |
| 8140 | op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || |
| 8141 | op == spv::OpGroupSMinNonUniformAMD || |
| 8142 | op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || |
| 8143 | op == spv::OpGroupSMaxNonUniformAMD || |
| 8144 | op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
| 8145 | |
| 8146 | // Handle group invocation operations scalar by scalar. |
| 8147 | // The result type is the same type as the original type. |
| 8148 | // The algorithm is to: |
| 8149 | // - break the vector into scalars |
| 8150 | // - apply the operation to each scalar |
| 8151 | // - make a vector out the scalar results |
| 8152 | |
| 8153 | // get the types sorted out |
| 8154 | int numComponents = builder.getNumComponents(operands[0]); |
| 8155 | spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
| 8156 | std::vector<spv::Id> results; |
| 8157 | |
| 8158 | // do each scalar op |
| 8159 | for (int comp = 0; comp < numComponents; ++comp) { |
| 8160 | std::vector<unsigned int> indexes; |
| 8161 | indexes.push_back(comp); |
| 8162 | spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 8163 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 8164 | if (op == spv::OpSubgroupReadInvocationKHR) { |
| 8165 | spvGroupOperands.push_back(scalar); |
| 8166 | spv::IdImmediate operand = { true, operands[1] }; |
| 8167 | spvGroupOperands.push_back(operand); |
| 8168 | } else if (op == spv::OpSubgroupFirstInvocationKHR) { |
| 8169 | spvGroupOperands.push_back(scalar); |
| 8170 | } else if (op == spv::OpGroupBroadcast) { |
| 8171 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 8172 | spvGroupOperands.push_back(scope); |
| 8173 | spvGroupOperands.push_back(scalar); |
| 8174 | spv::IdImmediate operand = { true, operands[1] }; |
| 8175 | spvGroupOperands.push_back(operand); |
| 8176 | } else { |
| 8177 | spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 8178 | spvGroupOperands.push_back(scope); |
| 8179 | spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
| 8180 | spvGroupOperands.push_back(groupOp); |
| 8181 | spvGroupOperands.push_back(scalar); |
| 8182 | } |
| 8183 | |
| 8184 | results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
| 8185 | } |
| 8186 | |
| 8187 | // put the pieces together |
| 8188 | return builder.createCompositeConstruct(typeId, results); |
| 8189 | } |
| 8190 | |
| 8191 | // Create subgroup invocation operations. |
| 8192 | spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 8193 | std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
| 8194 | { |
| 8195 | // Add the required capabilities. |
| 8196 | switch (op) { |
| 8197 | case glslang::EOpSubgroupElect: |
| 8198 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 8199 | break; |
| 8200 | case glslang::EOpSubgroupAll: |
| 8201 | case glslang::EOpSubgroupAny: |
| 8202 | case glslang::EOpSubgroupAllEqual: |
| 8203 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 8204 | builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 8205 | break; |
| 8206 | case glslang::EOpSubgroupBroadcast: |
| 8207 | case glslang::EOpSubgroupBroadcastFirst: |
| 8208 | case glslang::EOpSubgroupBallot: |
| 8209 | case glslang::EOpSubgroupInverseBallot: |
| 8210 | case glslang::EOpSubgroupBallotBitExtract: |
| 8211 | case glslang::EOpSubgroupBallotBitCount: |
| 8212 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 8213 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 8214 | case glslang::EOpSubgroupBallotFindLSB: |
| 8215 | case glslang::EOpSubgroupBallotFindMSB: |
| 8216 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 8217 | builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 8218 | break; |
| 8219 | case glslang::EOpSubgroupShuffle: |
| 8220 | case glslang::EOpSubgroupShuffleXor: |
| 8221 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 8222 | builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 8223 | break; |
| 8224 | case glslang::EOpSubgroupShuffleUp: |
| 8225 | case glslang::EOpSubgroupShuffleDown: |
| 8226 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 8227 | builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 8228 | break; |
| 8229 | case glslang::EOpSubgroupAdd: |
| 8230 | case glslang::EOpSubgroupMul: |
| 8231 | case glslang::EOpSubgroupMin: |
| 8232 | case glslang::EOpSubgroupMax: |
| 8233 | case glslang::EOpSubgroupAnd: |
| 8234 | case glslang::EOpSubgroupOr: |
| 8235 | case glslang::EOpSubgroupXor: |
| 8236 | case glslang::EOpSubgroupInclusiveAdd: |
| 8237 | case glslang::EOpSubgroupInclusiveMul: |
| 8238 | case glslang::EOpSubgroupInclusiveMin: |
| 8239 | case glslang::EOpSubgroupInclusiveMax: |
| 8240 | case glslang::EOpSubgroupInclusiveAnd: |
| 8241 | case glslang::EOpSubgroupInclusiveOr: |
| 8242 | case glslang::EOpSubgroupInclusiveXor: |
| 8243 | case glslang::EOpSubgroupExclusiveAdd: |
| 8244 | case glslang::EOpSubgroupExclusiveMul: |
| 8245 | case glslang::EOpSubgroupExclusiveMin: |
| 8246 | case glslang::EOpSubgroupExclusiveMax: |
| 8247 | case glslang::EOpSubgroupExclusiveAnd: |
| 8248 | case glslang::EOpSubgroupExclusiveOr: |
| 8249 | case glslang::EOpSubgroupExclusiveXor: |
| 8250 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 8251 | builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 8252 | break; |
| 8253 | case glslang::EOpSubgroupClusteredAdd: |
| 8254 | case glslang::EOpSubgroupClusteredMul: |
| 8255 | case glslang::EOpSubgroupClusteredMin: |
| 8256 | case glslang::EOpSubgroupClusteredMax: |
| 8257 | case glslang::EOpSubgroupClusteredAnd: |
| 8258 | case glslang::EOpSubgroupClusteredOr: |
| 8259 | case glslang::EOpSubgroupClusteredXor: |
| 8260 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 8261 | builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 8262 | break; |
| 8263 | case glslang::EOpSubgroupQuadBroadcast: |
| 8264 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 8265 | case glslang::EOpSubgroupQuadSwapVertical: |
| 8266 | case glslang::EOpSubgroupQuadSwapDiagonal: |
| 8267 | builder.addCapability(spv::CapabilityGroupNonUniform); |
| 8268 | builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 8269 | break; |
| 8270 | case glslang::EOpSubgroupPartitionedAdd: |
| 8271 | case glslang::EOpSubgroupPartitionedMul: |
| 8272 | case glslang::EOpSubgroupPartitionedMin: |
| 8273 | case glslang::EOpSubgroupPartitionedMax: |
| 8274 | case glslang::EOpSubgroupPartitionedAnd: |
| 8275 | case glslang::EOpSubgroupPartitionedOr: |
| 8276 | case glslang::EOpSubgroupPartitionedXor: |
| 8277 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 8278 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 8279 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 8280 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 8281 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 8282 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 8283 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 8284 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 8285 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 8286 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 8287 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 8288 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 8289 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 8290 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 8291 | builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 8292 | builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 8293 | break; |
| 8294 | default: assert(0 && "Unhandled subgroup operation!" ); |
| 8295 | } |
| 8296 | |
| 8297 | |
| 8298 | const bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 8299 | const bool isFloat = isTypeFloat(typeProxy); |
| 8300 | const bool isBool = typeProxy == glslang::EbtBool; |
| 8301 | |
| 8302 | spv::Op opCode = spv::OpNop; |
| 8303 | |
| 8304 | // Figure out which opcode to use. |
| 8305 | switch (op) { |
| 8306 | case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 8307 | case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 8308 | case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 8309 | case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 8310 | case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 8311 | case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 8312 | case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 8313 | case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 8314 | case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 8315 | case glslang::EOpSubgroupBallotBitCount: |
| 8316 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 8317 | case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 8318 | case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 8319 | case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 8320 | case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 8321 | case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 8322 | case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 8323 | case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 8324 | case glslang::EOpSubgroupAdd: |
| 8325 | case glslang::EOpSubgroupInclusiveAdd: |
| 8326 | case glslang::EOpSubgroupExclusiveAdd: |
| 8327 | case glslang::EOpSubgroupClusteredAdd: |
| 8328 | case glslang::EOpSubgroupPartitionedAdd: |
| 8329 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 8330 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 8331 | if (isFloat) { |
| 8332 | opCode = spv::OpGroupNonUniformFAdd; |
| 8333 | } else { |
| 8334 | opCode = spv::OpGroupNonUniformIAdd; |
| 8335 | } |
| 8336 | break; |
| 8337 | case glslang::EOpSubgroupMul: |
| 8338 | case glslang::EOpSubgroupInclusiveMul: |
| 8339 | case glslang::EOpSubgroupExclusiveMul: |
| 8340 | case glslang::EOpSubgroupClusteredMul: |
| 8341 | case glslang::EOpSubgroupPartitionedMul: |
| 8342 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 8343 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 8344 | if (isFloat) { |
| 8345 | opCode = spv::OpGroupNonUniformFMul; |
| 8346 | } else { |
| 8347 | opCode = spv::OpGroupNonUniformIMul; |
| 8348 | } |
| 8349 | break; |
| 8350 | case glslang::EOpSubgroupMin: |
| 8351 | case glslang::EOpSubgroupInclusiveMin: |
| 8352 | case glslang::EOpSubgroupExclusiveMin: |
| 8353 | case glslang::EOpSubgroupClusteredMin: |
| 8354 | case glslang::EOpSubgroupPartitionedMin: |
| 8355 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 8356 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 8357 | if (isFloat) { |
| 8358 | opCode = spv::OpGroupNonUniformFMin; |
| 8359 | } else if (isUnsigned) { |
| 8360 | opCode = spv::OpGroupNonUniformUMin; |
| 8361 | } else { |
| 8362 | opCode = spv::OpGroupNonUniformSMin; |
| 8363 | } |
| 8364 | break; |
| 8365 | case glslang::EOpSubgroupMax: |
| 8366 | case glslang::EOpSubgroupInclusiveMax: |
| 8367 | case glslang::EOpSubgroupExclusiveMax: |
| 8368 | case glslang::EOpSubgroupClusteredMax: |
| 8369 | case glslang::EOpSubgroupPartitionedMax: |
| 8370 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 8371 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 8372 | if (isFloat) { |
| 8373 | opCode = spv::OpGroupNonUniformFMax; |
| 8374 | } else if (isUnsigned) { |
| 8375 | opCode = spv::OpGroupNonUniformUMax; |
| 8376 | } else { |
| 8377 | opCode = spv::OpGroupNonUniformSMax; |
| 8378 | } |
| 8379 | break; |
| 8380 | case glslang::EOpSubgroupAnd: |
| 8381 | case glslang::EOpSubgroupInclusiveAnd: |
| 8382 | case glslang::EOpSubgroupExclusiveAnd: |
| 8383 | case glslang::EOpSubgroupClusteredAnd: |
| 8384 | case glslang::EOpSubgroupPartitionedAnd: |
| 8385 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 8386 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 8387 | if (isBool) { |
| 8388 | opCode = spv::OpGroupNonUniformLogicalAnd; |
| 8389 | } else { |
| 8390 | opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 8391 | } |
| 8392 | break; |
| 8393 | case glslang::EOpSubgroupOr: |
| 8394 | case glslang::EOpSubgroupInclusiveOr: |
| 8395 | case glslang::EOpSubgroupExclusiveOr: |
| 8396 | case glslang::EOpSubgroupClusteredOr: |
| 8397 | case glslang::EOpSubgroupPartitionedOr: |
| 8398 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 8399 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 8400 | if (isBool) { |
| 8401 | opCode = spv::OpGroupNonUniformLogicalOr; |
| 8402 | } else { |
| 8403 | opCode = spv::OpGroupNonUniformBitwiseOr; |
| 8404 | } |
| 8405 | break; |
| 8406 | case glslang::EOpSubgroupXor: |
| 8407 | case glslang::EOpSubgroupInclusiveXor: |
| 8408 | case glslang::EOpSubgroupExclusiveXor: |
| 8409 | case glslang::EOpSubgroupClusteredXor: |
| 8410 | case glslang::EOpSubgroupPartitionedXor: |
| 8411 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 8412 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 8413 | if (isBool) { |
| 8414 | opCode = spv::OpGroupNonUniformLogicalXor; |
| 8415 | } else { |
| 8416 | opCode = spv::OpGroupNonUniformBitwiseXor; |
| 8417 | } |
| 8418 | break; |
| 8419 | case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 8420 | case glslang::EOpSubgroupQuadSwapHorizontal: |
| 8421 | case glslang::EOpSubgroupQuadSwapVertical: |
| 8422 | case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 8423 | default: assert(0 && "Unhandled subgroup operation!" ); |
| 8424 | } |
| 8425 | |
| 8426 | // get the right Group Operation |
| 8427 | spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 8428 | switch (op) { |
| 8429 | default: |
| 8430 | break; |
| 8431 | case glslang::EOpSubgroupBallotBitCount: |
| 8432 | case glslang::EOpSubgroupAdd: |
| 8433 | case glslang::EOpSubgroupMul: |
| 8434 | case glslang::EOpSubgroupMin: |
| 8435 | case glslang::EOpSubgroupMax: |
| 8436 | case glslang::EOpSubgroupAnd: |
| 8437 | case glslang::EOpSubgroupOr: |
| 8438 | case glslang::EOpSubgroupXor: |
| 8439 | groupOperation = spv::GroupOperationReduce; |
| 8440 | break; |
| 8441 | case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 8442 | case glslang::EOpSubgroupInclusiveAdd: |
| 8443 | case glslang::EOpSubgroupInclusiveMul: |
| 8444 | case glslang::EOpSubgroupInclusiveMin: |
| 8445 | case glslang::EOpSubgroupInclusiveMax: |
| 8446 | case glslang::EOpSubgroupInclusiveAnd: |
| 8447 | case glslang::EOpSubgroupInclusiveOr: |
| 8448 | case glslang::EOpSubgroupInclusiveXor: |
| 8449 | groupOperation = spv::GroupOperationInclusiveScan; |
| 8450 | break; |
| 8451 | case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 8452 | case glslang::EOpSubgroupExclusiveAdd: |
| 8453 | case glslang::EOpSubgroupExclusiveMul: |
| 8454 | case glslang::EOpSubgroupExclusiveMin: |
| 8455 | case glslang::EOpSubgroupExclusiveMax: |
| 8456 | case glslang::EOpSubgroupExclusiveAnd: |
| 8457 | case glslang::EOpSubgroupExclusiveOr: |
| 8458 | case glslang::EOpSubgroupExclusiveXor: |
| 8459 | groupOperation = spv::GroupOperationExclusiveScan; |
| 8460 | break; |
| 8461 | case glslang::EOpSubgroupClusteredAdd: |
| 8462 | case glslang::EOpSubgroupClusteredMul: |
| 8463 | case glslang::EOpSubgroupClusteredMin: |
| 8464 | case glslang::EOpSubgroupClusteredMax: |
| 8465 | case glslang::EOpSubgroupClusteredAnd: |
| 8466 | case glslang::EOpSubgroupClusteredOr: |
| 8467 | case glslang::EOpSubgroupClusteredXor: |
| 8468 | groupOperation = spv::GroupOperationClusteredReduce; |
| 8469 | break; |
| 8470 | case glslang::EOpSubgroupPartitionedAdd: |
| 8471 | case glslang::EOpSubgroupPartitionedMul: |
| 8472 | case glslang::EOpSubgroupPartitionedMin: |
| 8473 | case glslang::EOpSubgroupPartitionedMax: |
| 8474 | case glslang::EOpSubgroupPartitionedAnd: |
| 8475 | case glslang::EOpSubgroupPartitionedOr: |
| 8476 | case glslang::EOpSubgroupPartitionedXor: |
| 8477 | groupOperation = spv::GroupOperationPartitionedReduceNV; |
| 8478 | break; |
| 8479 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 8480 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 8481 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 8482 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 8483 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 8484 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 8485 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 8486 | groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
| 8487 | break; |
| 8488 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 8489 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 8490 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 8491 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 8492 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 8493 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 8494 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 8495 | groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
| 8496 | break; |
| 8497 | } |
| 8498 | |
| 8499 | // build the instruction |
| 8500 | std::vector<spv::IdImmediate> spvGroupOperands; |
| 8501 | |
| 8502 | // Every operation begins with the Execution Scope operand. |
| 8503 | spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 8504 | spvGroupOperands.push_back(executionScope); |
| 8505 | |
| 8506 | // Next, for all operations that use a Group Operation, push that as an operand. |
| 8507 | if (groupOperation != spv::GroupOperationMax) { |
| 8508 | spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
| 8509 | spvGroupOperands.push_back(groupOperand); |
| 8510 | } |
| 8511 | |
| 8512 | // Push back the operands next. |
| 8513 | for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 8514 | spv::IdImmediate operand = { true, *opIt }; |
| 8515 | spvGroupOperands.push_back(operand); |
| 8516 | } |
| 8517 | |
| 8518 | // Some opcodes have additional operands. |
| 8519 | spv::Id directionId = spv::NoResult; |
| 8520 | switch (op) { |
| 8521 | default: break; |
| 8522 | case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 8523 | case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 8524 | case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 8525 | } |
| 8526 | if (directionId != spv::NoResult) { |
| 8527 | spv::IdImmediate direction = { true, directionId }; |
| 8528 | spvGroupOperands.push_back(direction); |
| 8529 | } |
| 8530 | |
| 8531 | return builder.createOp(opCode, typeId, spvGroupOperands); |
| 8532 | } |
| 8533 | |
| 8534 | spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, |
| 8535 | spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
| 8536 | { |
| 8537 | bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 8538 | bool isFloat = isTypeFloat(typeProxy); |
| 8539 | |
| 8540 | spv::Op opCode = spv::OpNop; |
| 8541 | int extBuiltins = -1; |
| 8542 | int libCall = -1; |
| 8543 | size_t consumedOperands = operands.size(); |
| 8544 | spv::Id typeId0 = 0; |
| 8545 | if (consumedOperands > 0) |
| 8546 | typeId0 = builder.getTypeId(operands[0]); |
| 8547 | spv::Id typeId1 = 0; |
| 8548 | if (consumedOperands > 1) |
| 8549 | typeId1 = builder.getTypeId(operands[1]); |
| 8550 | spv::Id frexpIntType = 0; |
| 8551 | |
| 8552 | switch (op) { |
| 8553 | case glslang::EOpMin: |
| 8554 | if (isFloat) |
| 8555 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
| 8556 | else if (isUnsigned) |
| 8557 | libCall = spv::GLSLstd450UMin; |
| 8558 | else |
| 8559 | libCall = spv::GLSLstd450SMin; |
| 8560 | builder.promoteScalar(precision, operands.front(), operands.back()); |
| 8561 | break; |
| 8562 | case glslang::EOpModf: |
| 8563 | libCall = spv::GLSLstd450Modf; |
| 8564 | break; |
| 8565 | case glslang::EOpMax: |
| 8566 | if (isFloat) |
| 8567 | libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
| 8568 | else if (isUnsigned) |
| 8569 | libCall = spv::GLSLstd450UMax; |
| 8570 | else |
| 8571 | libCall = spv::GLSLstd450SMax; |
| 8572 | builder.promoteScalar(precision, operands.front(), operands.back()); |
| 8573 | break; |
| 8574 | case glslang::EOpPow: |
| 8575 | libCall = spv::GLSLstd450Pow; |
| 8576 | break; |
| 8577 | case glslang::EOpDot: |
| 8578 | opCode = spv::OpDot; |
| 8579 | break; |
| 8580 | case glslang::EOpAtan: |
| 8581 | libCall = spv::GLSLstd450Atan2; |
| 8582 | break; |
| 8583 | |
| 8584 | case glslang::EOpClamp: |
| 8585 | if (isFloat) |
| 8586 | libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
| 8587 | else if (isUnsigned) |
| 8588 | libCall = spv::GLSLstd450UClamp; |
| 8589 | else |
| 8590 | libCall = spv::GLSLstd450SClamp; |
| 8591 | builder.promoteScalar(precision, operands.front(), operands[1]); |
| 8592 | builder.promoteScalar(precision, operands.front(), operands[2]); |
| 8593 | break; |
| 8594 | case glslang::EOpMix: |
| 8595 | if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 8596 | assert(isFloat); |
| 8597 | libCall = spv::GLSLstd450FMix; |
| 8598 | } else { |
| 8599 | opCode = spv::OpSelect; |
| 8600 | std::swap(operands.front(), operands.back()); |
| 8601 | } |
| 8602 | builder.promoteScalar(precision, operands.front(), operands.back()); |
| 8603 | break; |
| 8604 | case glslang::EOpStep: |
| 8605 | libCall = spv::GLSLstd450Step; |
| 8606 | builder.promoteScalar(precision, operands.front(), operands.back()); |
| 8607 | break; |
| 8608 | case glslang::EOpSmoothStep: |
| 8609 | libCall = spv::GLSLstd450SmoothStep; |
| 8610 | builder.promoteScalar(precision, operands[0], operands[2]); |
| 8611 | builder.promoteScalar(precision, operands[1], operands[2]); |
| 8612 | break; |
| 8613 | |
| 8614 | case glslang::EOpDistance: |
| 8615 | libCall = spv::GLSLstd450Distance; |
| 8616 | break; |
| 8617 | case glslang::EOpCross: |
| 8618 | libCall = spv::GLSLstd450Cross; |
| 8619 | break; |
| 8620 | case glslang::EOpFaceForward: |
| 8621 | libCall = spv::GLSLstd450FaceForward; |
| 8622 | break; |
| 8623 | case glslang::EOpReflect: |
| 8624 | libCall = spv::GLSLstd450Reflect; |
| 8625 | break; |
| 8626 | case glslang::EOpRefract: |
| 8627 | libCall = spv::GLSLstd450Refract; |
| 8628 | break; |
| 8629 | case glslang::EOpBarrier: |
| 8630 | { |
| 8631 | // This is for the extended controlBarrier function, with four operands. |
| 8632 | // The unextended barrier() goes through createNoArgOperation. |
| 8633 | assert(operands.size() == 4); |
| 8634 | unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 8635 | unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 8636 | unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 8637 | builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, |
| 8638 | (spv::MemorySemanticsMask)semantics); |
| 8639 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 8640 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 8641 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 8642 | spv::MemorySemanticsVolatileMask)) { |
| 8643 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 8644 | } |
| 8645 | if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || |
| 8646 | memoryScope == spv::ScopeDevice)) { |
| 8647 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 8648 | } |
| 8649 | return 0; |
| 8650 | } |
| 8651 | break; |
| 8652 | case glslang::EOpMemoryBarrier: |
| 8653 | { |
| 8654 | // This is for the extended memoryBarrier function, with three operands. |
| 8655 | // The unextended memoryBarrier() goes through createNoArgOperation. |
| 8656 | assert(operands.size() == 3); |
| 8657 | unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 8658 | unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 8659 | builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 8660 | if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 8661 | spv::MemorySemanticsMakeVisibleKHRMask | |
| 8662 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 8663 | spv::MemorySemanticsVolatileMask)) { |
| 8664 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 8665 | } |
| 8666 | if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 8667 | builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 8668 | } |
| 8669 | return 0; |
| 8670 | } |
| 8671 | break; |
| 8672 | |
| 8673 | #ifndef GLSLANG_WEB |
| 8674 | case glslang::EOpInterpolateAtSample: |
| 8675 | if (typeProxy == glslang::EbtFloat16) |
| 8676 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 8677 | libCall = spv::GLSLstd450InterpolateAtSample; |
| 8678 | break; |
| 8679 | case glslang::EOpInterpolateAtOffset: |
| 8680 | if (typeProxy == glslang::EbtFloat16) |
| 8681 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 8682 | libCall = spv::GLSLstd450InterpolateAtOffset; |
| 8683 | break; |
| 8684 | case glslang::EOpAddCarry: |
| 8685 | opCode = spv::OpIAddCarry; |
| 8686 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 8687 | consumedOperands = 2; |
| 8688 | break; |
| 8689 | case glslang::EOpSubBorrow: |
| 8690 | opCode = spv::OpISubBorrow; |
| 8691 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 8692 | consumedOperands = 2; |
| 8693 | break; |
| 8694 | case glslang::EOpUMulExtended: |
| 8695 | opCode = spv::OpUMulExtended; |
| 8696 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 8697 | consumedOperands = 2; |
| 8698 | break; |
| 8699 | case glslang::EOpIMulExtended: |
| 8700 | opCode = spv::OpSMulExtended; |
| 8701 | typeId = builder.makeStructResultType(typeId0, typeId0); |
| 8702 | consumedOperands = 2; |
| 8703 | break; |
| 8704 | case glslang::EOpBitfieldExtract: |
| 8705 | if (isUnsigned) |
| 8706 | opCode = spv::OpBitFieldUExtract; |
| 8707 | else |
| 8708 | opCode = spv::OpBitFieldSExtract; |
| 8709 | break; |
| 8710 | case glslang::EOpBitfieldInsert: |
| 8711 | opCode = spv::OpBitFieldInsert; |
| 8712 | break; |
| 8713 | |
| 8714 | case glslang::EOpFma: |
| 8715 | libCall = spv::GLSLstd450Fma; |
| 8716 | break; |
| 8717 | case glslang::EOpFrexp: |
| 8718 | { |
| 8719 | libCall = spv::GLSLstd450FrexpStruct; |
| 8720 | assert(builder.isPointerType(typeId1)); |
| 8721 | typeId1 = builder.getContainedTypeId(typeId1); |
| 8722 | int width = builder.getScalarTypeWidth(typeId1); |
| 8723 | if (width == 16) |
| 8724 | // Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 8725 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 8726 | if (builder.getNumComponents(operands[0]) == 1) |
| 8727 | frexpIntType = builder.makeIntegerType(width, true); |
| 8728 | else |
| 8729 | frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), |
| 8730 | builder.getNumComponents(operands[0])); |
| 8731 | typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 8732 | consumedOperands = 1; |
| 8733 | } |
| 8734 | break; |
| 8735 | case glslang::EOpLdexp: |
| 8736 | libCall = spv::GLSLstd450Ldexp; |
| 8737 | break; |
| 8738 | |
| 8739 | case glslang::EOpReadInvocation: |
| 8740 | return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 8741 | |
| 8742 | case glslang::EOpSubgroupBroadcast: |
| 8743 | case glslang::EOpSubgroupBallotBitExtract: |
| 8744 | case glslang::EOpSubgroupShuffle: |
| 8745 | case glslang::EOpSubgroupShuffleXor: |
| 8746 | case glslang::EOpSubgroupShuffleUp: |
| 8747 | case glslang::EOpSubgroupShuffleDown: |
| 8748 | case glslang::EOpSubgroupClusteredAdd: |
| 8749 | case glslang::EOpSubgroupClusteredMul: |
| 8750 | case glslang::EOpSubgroupClusteredMin: |
| 8751 | case glslang::EOpSubgroupClusteredMax: |
| 8752 | case glslang::EOpSubgroupClusteredAnd: |
| 8753 | case glslang::EOpSubgroupClusteredOr: |
| 8754 | case glslang::EOpSubgroupClusteredXor: |
| 8755 | case glslang::EOpSubgroupQuadBroadcast: |
| 8756 | case glslang::EOpSubgroupPartitionedAdd: |
| 8757 | case glslang::EOpSubgroupPartitionedMul: |
| 8758 | case glslang::EOpSubgroupPartitionedMin: |
| 8759 | case glslang::EOpSubgroupPartitionedMax: |
| 8760 | case glslang::EOpSubgroupPartitionedAnd: |
| 8761 | case glslang::EOpSubgroupPartitionedOr: |
| 8762 | case glslang::EOpSubgroupPartitionedXor: |
| 8763 | case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 8764 | case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 8765 | case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 8766 | case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 8767 | case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 8768 | case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 8769 | case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 8770 | case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 8771 | case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 8772 | case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 8773 | case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 8774 | case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 8775 | case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 8776 | case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 8777 | return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 8778 | |
| 8779 | case glslang::EOpSwizzleInvocations: |
| 8780 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 8781 | libCall = spv::SwizzleInvocationsAMD; |
| 8782 | break; |
| 8783 | case glslang::EOpSwizzleInvocationsMasked: |
| 8784 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 8785 | libCall = spv::SwizzleInvocationsMaskedAMD; |
| 8786 | break; |
| 8787 | case glslang::EOpWriteInvocation: |
| 8788 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 8789 | libCall = spv::WriteInvocationAMD; |
| 8790 | break; |
| 8791 | |
| 8792 | case glslang::EOpMin3: |
| 8793 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 8794 | if (isFloat) |
| 8795 | libCall = spv::FMin3AMD; |
| 8796 | else { |
| 8797 | if (isUnsigned) |
| 8798 | libCall = spv::UMin3AMD; |
| 8799 | else |
| 8800 | libCall = spv::SMin3AMD; |
| 8801 | } |
| 8802 | break; |
| 8803 | case glslang::EOpMax3: |
| 8804 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 8805 | if (isFloat) |
| 8806 | libCall = spv::FMax3AMD; |
| 8807 | else { |
| 8808 | if (isUnsigned) |
| 8809 | libCall = spv::UMax3AMD; |
| 8810 | else |
| 8811 | libCall = spv::SMax3AMD; |
| 8812 | } |
| 8813 | break; |
| 8814 | case glslang::EOpMid3: |
| 8815 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 8816 | if (isFloat) |
| 8817 | libCall = spv::FMid3AMD; |
| 8818 | else { |
| 8819 | if (isUnsigned) |
| 8820 | libCall = spv::UMid3AMD; |
| 8821 | else |
| 8822 | libCall = spv::SMid3AMD; |
| 8823 | } |
| 8824 | break; |
| 8825 | |
| 8826 | case glslang::EOpInterpolateAtVertex: |
| 8827 | if (typeProxy == glslang::EbtFloat16) |
| 8828 | builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 8829 | extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 8830 | libCall = spv::InterpolateAtVertexAMD; |
| 8831 | break; |
| 8832 | |
| 8833 | case glslang::EOpReportIntersection: |
| 8834 | typeId = builder.makeBoolType(); |
| 8835 | opCode = spv::OpReportIntersectionKHR; |
| 8836 | break; |
| 8837 | case glslang::EOpTraceNV: |
| 8838 | builder.createNoResultOp(spv::OpTraceNV, operands); |
| 8839 | return 0; |
| 8840 | case glslang::EOpTraceRayMotionNV: |
| 8841 | builder.addExtension(spv::E_SPV_NV_ray_tracing_motion_blur); |
| 8842 | builder.addCapability(spv::CapabilityRayTracingMotionBlurNV); |
| 8843 | builder.createNoResultOp(spv::OpTraceRayMotionNV, operands); |
| 8844 | return 0; |
| 8845 | case glslang::EOpTraceKHR: |
| 8846 | builder.createNoResultOp(spv::OpTraceRayKHR, operands); |
| 8847 | return 0; |
| 8848 | case glslang::EOpExecuteCallableNV: |
| 8849 | builder.createNoResultOp(spv::OpExecuteCallableNV, operands); |
| 8850 | return 0; |
| 8851 | case glslang::EOpExecuteCallableKHR: |
| 8852 | builder.createNoResultOp(spv::OpExecuteCallableKHR, operands); |
| 8853 | return 0; |
| 8854 | |
| 8855 | case glslang::EOpRayQueryInitialize: |
| 8856 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR, operands); |
| 8857 | return 0; |
| 8858 | case glslang::EOpRayQueryTerminate: |
| 8859 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operands); |
| 8860 | return 0; |
| 8861 | case glslang::EOpRayQueryGenerateIntersection: |
| 8862 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR, operands); |
| 8863 | return 0; |
| 8864 | case glslang::EOpRayQueryConfirmIntersection: |
| 8865 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operands); |
| 8866 | return 0; |
| 8867 | case glslang::EOpRayQueryProceed: |
| 8868 | typeId = builder.makeBoolType(); |
| 8869 | opCode = spv::OpRayQueryProceedKHR; |
| 8870 | break; |
| 8871 | case glslang::EOpRayQueryGetIntersectionType: |
| 8872 | typeId = builder.makeUintType(32); |
| 8873 | opCode = spv::OpRayQueryGetIntersectionTypeKHR; |
| 8874 | break; |
| 8875 | case glslang::EOpRayQueryGetRayTMin: |
| 8876 | typeId = builder.makeFloatType(32); |
| 8877 | opCode = spv::OpRayQueryGetRayTMinKHR; |
| 8878 | break; |
| 8879 | case glslang::EOpRayQueryGetRayFlags: |
| 8880 | typeId = builder.makeIntType(32); |
| 8881 | opCode = spv::OpRayQueryGetRayFlagsKHR; |
| 8882 | break; |
| 8883 | case glslang::EOpRayQueryGetIntersectionT: |
| 8884 | typeId = builder.makeFloatType(32); |
| 8885 | opCode = spv::OpRayQueryGetIntersectionTKHR; |
| 8886 | break; |
| 8887 | case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 8888 | typeId = builder.makeIntType(32); |
| 8889 | opCode = spv::OpRayQueryGetIntersectionInstanceCustomIndexKHR; |
| 8890 | break; |
| 8891 | case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 8892 | typeId = builder.makeIntType(32); |
| 8893 | opCode = spv::OpRayQueryGetIntersectionInstanceIdKHR; |
| 8894 | break; |
| 8895 | case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 8896 | typeId = builder.makeUintType(32); |
| 8897 | opCode = spv::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR; |
| 8898 | break; |
| 8899 | case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 8900 | typeId = builder.makeIntType(32); |
| 8901 | opCode = spv::OpRayQueryGetIntersectionGeometryIndexKHR; |
| 8902 | break; |
| 8903 | case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 8904 | typeId = builder.makeIntType(32); |
| 8905 | opCode = spv::OpRayQueryGetIntersectionPrimitiveIndexKHR; |
| 8906 | break; |
| 8907 | case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 8908 | typeId = builder.makeVectorType(builder.makeFloatType(32), 2); |
| 8909 | opCode = spv::OpRayQueryGetIntersectionBarycentricsKHR; |
| 8910 | break; |
| 8911 | case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 8912 | typeId = builder.makeBoolType(); |
| 8913 | opCode = spv::OpRayQueryGetIntersectionFrontFaceKHR; |
| 8914 | break; |
| 8915 | case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 8916 | typeId = builder.makeBoolType(); |
| 8917 | opCode = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
| 8918 | break; |
| 8919 | case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 8920 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 8921 | opCode = spv::OpRayQueryGetIntersectionObjectRayDirectionKHR; |
| 8922 | break; |
| 8923 | case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 8924 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 8925 | opCode = spv::OpRayQueryGetIntersectionObjectRayOriginKHR; |
| 8926 | break; |
| 8927 | case glslang::EOpRayQueryGetWorldRayDirection: |
| 8928 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 8929 | opCode = spv::OpRayQueryGetWorldRayDirectionKHR; |
| 8930 | break; |
| 8931 | case glslang::EOpRayQueryGetWorldRayOrigin: |
| 8932 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 8933 | opCode = spv::OpRayQueryGetWorldRayOriginKHR; |
| 8934 | break; |
| 8935 | case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 8936 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 8937 | opCode = spv::OpRayQueryGetIntersectionObjectToWorldKHR; |
| 8938 | break; |
| 8939 | case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 8940 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 8941 | opCode = spv::OpRayQueryGetIntersectionWorldToObjectKHR; |
| 8942 | break; |
| 8943 | case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 8944 | builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 8945 | return 0; |
| 8946 | case glslang::EOpEmitMeshTasksEXT: |
| 8947 | if (taskPayloadID) |
| 8948 | operands.push_back(taskPayloadID); |
| 8949 | // As per SPV_EXT_mesh_shader make it a terminating instruction in the current block |
| 8950 | builder.makeStatementTerminator(spv::OpEmitMeshTasksEXT, operands, "post-OpEmitMeshTasksEXT" ); |
| 8951 | return 0; |
| 8952 | case glslang::EOpSetMeshOutputsEXT: |
| 8953 | builder.createNoResultOp(spv::OpSetMeshOutputsEXT, operands); |
| 8954 | return 0; |
| 8955 | case glslang::EOpCooperativeMatrixMulAdd: |
| 8956 | opCode = spv::OpCooperativeMatrixMulAddNV; |
| 8957 | break; |
| 8958 | case glslang::EOpHitObjectTraceRayNV: |
| 8959 | builder.createNoResultOp(spv::OpHitObjectTraceRayNV, operands); |
| 8960 | return 0; |
| 8961 | case glslang::EOpHitObjectTraceRayMotionNV: |
| 8962 | builder.createNoResultOp(spv::OpHitObjectTraceRayMotionNV, operands); |
| 8963 | return 0; |
| 8964 | case glslang::EOpHitObjectRecordHitNV: |
| 8965 | builder.createNoResultOp(spv::OpHitObjectRecordHitNV, operands); |
| 8966 | return 0; |
| 8967 | case glslang::EOpHitObjectRecordHitMotionNV: |
| 8968 | builder.createNoResultOp(spv::OpHitObjectRecordHitMotionNV, operands); |
| 8969 | return 0; |
| 8970 | case glslang::EOpHitObjectRecordHitWithIndexNV: |
| 8971 | builder.createNoResultOp(spv::OpHitObjectRecordHitWithIndexNV, operands); |
| 8972 | return 0; |
| 8973 | case glslang::EOpHitObjectRecordHitWithIndexMotionNV: |
| 8974 | builder.createNoResultOp(spv::OpHitObjectRecordHitWithIndexMotionNV, operands); |
| 8975 | return 0; |
| 8976 | case glslang::EOpHitObjectRecordMissNV: |
| 8977 | builder.createNoResultOp(spv::OpHitObjectRecordMissNV, operands); |
| 8978 | return 0; |
| 8979 | case glslang::EOpHitObjectRecordMissMotionNV: |
| 8980 | builder.createNoResultOp(spv::OpHitObjectRecordMissMotionNV, operands); |
| 8981 | return 0; |
| 8982 | case glslang::EOpHitObjectExecuteShaderNV: |
| 8983 | builder.createNoResultOp(spv::OpHitObjectExecuteShaderNV, operands); |
| 8984 | return 0; |
| 8985 | case glslang::EOpHitObjectIsEmptyNV: |
| 8986 | typeId = builder.makeBoolType(); |
| 8987 | opCode = spv::OpHitObjectIsEmptyNV; |
| 8988 | break; |
| 8989 | case glslang::EOpHitObjectIsMissNV: |
| 8990 | typeId = builder.makeBoolType(); |
| 8991 | opCode = spv::OpHitObjectIsMissNV; |
| 8992 | break; |
| 8993 | case glslang::EOpHitObjectIsHitNV: |
| 8994 | typeId = builder.makeBoolType(); |
| 8995 | opCode = spv::OpHitObjectIsHitNV; |
| 8996 | break; |
| 8997 | case glslang::EOpHitObjectGetRayTMinNV: |
| 8998 | typeId = builder.makeFloatType(32); |
| 8999 | opCode = spv::OpHitObjectGetRayTMinNV; |
| 9000 | break; |
| 9001 | case glslang::EOpHitObjectGetRayTMaxNV: |
| 9002 | typeId = builder.makeFloatType(32); |
| 9003 | opCode = spv::OpHitObjectGetRayTMaxNV; |
| 9004 | break; |
| 9005 | case glslang::EOpHitObjectGetObjectRayOriginNV: |
| 9006 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 9007 | opCode = spv::OpHitObjectGetObjectRayOriginNV; |
| 9008 | break; |
| 9009 | case glslang::EOpHitObjectGetObjectRayDirectionNV: |
| 9010 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 9011 | opCode = spv::OpHitObjectGetObjectRayDirectionNV; |
| 9012 | break; |
| 9013 | case glslang::EOpHitObjectGetWorldRayOriginNV: |
| 9014 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 9015 | opCode = spv::OpHitObjectGetWorldRayOriginNV; |
| 9016 | break; |
| 9017 | case glslang::EOpHitObjectGetWorldRayDirectionNV: |
| 9018 | typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 9019 | opCode = spv::OpHitObjectGetWorldRayDirectionNV; |
| 9020 | break; |
| 9021 | case glslang::EOpHitObjectGetWorldToObjectNV: |
| 9022 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 9023 | opCode = spv::OpHitObjectGetWorldToObjectNV; |
| 9024 | break; |
| 9025 | case glslang::EOpHitObjectGetObjectToWorldNV: |
| 9026 | typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 9027 | opCode = spv::OpHitObjectGetObjectToWorldNV; |
| 9028 | break; |
| 9029 | case glslang::EOpHitObjectGetInstanceCustomIndexNV: |
| 9030 | typeId = builder.makeIntegerType(32, 1); |
| 9031 | opCode = spv::OpHitObjectGetInstanceCustomIndexNV; |
| 9032 | break; |
| 9033 | case glslang::EOpHitObjectGetInstanceIdNV: |
| 9034 | typeId = builder.makeIntegerType(32, 1); |
| 9035 | opCode = spv::OpHitObjectGetInstanceIdNV; |
| 9036 | break; |
| 9037 | case glslang::EOpHitObjectGetGeometryIndexNV: |
| 9038 | typeId = builder.makeIntegerType(32, 1); |
| 9039 | opCode = spv::OpHitObjectGetGeometryIndexNV; |
| 9040 | break; |
| 9041 | case glslang::EOpHitObjectGetPrimitiveIndexNV: |
| 9042 | typeId = builder.makeIntegerType(32, 1); |
| 9043 | opCode = spv::OpHitObjectGetPrimitiveIndexNV; |
| 9044 | break; |
| 9045 | case glslang::EOpHitObjectGetHitKindNV: |
| 9046 | typeId = builder.makeIntegerType(32, 0); |
| 9047 | opCode = spv::OpHitObjectGetHitKindNV; |
| 9048 | break; |
| 9049 | case glslang::EOpHitObjectGetCurrentTimeNV: |
| 9050 | typeId = builder.makeFloatType(32); |
| 9051 | opCode = spv::OpHitObjectGetCurrentTimeNV; |
| 9052 | break; |
| 9053 | case glslang::EOpHitObjectGetShaderBindingTableRecordIndexNV: |
| 9054 | typeId = builder.makeIntegerType(32, 0); |
| 9055 | opCode = spv::OpHitObjectGetShaderBindingTableRecordIndexNV; |
| 9056 | return 0; |
| 9057 | case glslang::EOpHitObjectGetAttributesNV: |
| 9058 | builder.createNoResultOp(spv::OpHitObjectGetAttributesNV, operands); |
| 9059 | return 0; |
| 9060 | case glslang::EOpHitObjectGetShaderRecordBufferHandleNV: |
| 9061 | typeId = builder.makeVectorType(builder.makeUintType(32), 2); |
| 9062 | opCode = spv::OpHitObjectGetShaderRecordBufferHandleNV; |
| 9063 | break; |
| 9064 | case glslang::EOpReorderThreadNV: { |
| 9065 | if (operands.size() == 2) { |
| 9066 | builder.createNoResultOp(spv::OpReorderThreadWithHintNV, operands); |
| 9067 | } else { |
| 9068 | builder.createNoResultOp(spv::OpReorderThreadWithHitObjectNV, operands); |
| 9069 | } |
| 9070 | return 0; |
| 9071 | |
| 9072 | } |
| 9073 | break; |
| 9074 | #endif // GLSLANG_WEB |
| 9075 | default: |
| 9076 | return 0; |
| 9077 | } |
| 9078 | |
| 9079 | spv::Id id = 0; |
| 9080 | if (libCall >= 0) { |
| 9081 | // Use an extended instruction from the standard library. |
| 9082 | // Construct the call arguments, without modifying the original operands vector. |
| 9083 | // We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 9084 | std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
| 9085 | id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
| 9086 | } else if (opCode == spv::OpDot && !isFloat) { |
| 9087 | // int dot(int, int) |
| 9088 | // NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 9089 | const int componentCount = builder.getNumComponents(operands[0]); |
| 9090 | spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 9091 | builder.setPrecision(mulOp, precision); |
| 9092 | id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 9093 | for (int i = 1; i < componentCount; ++i) { |
| 9094 | builder.setPrecision(id, precision); |
| 9095 | id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(mulOp, typeId, i)); |
| 9096 | } |
| 9097 | } else { |
| 9098 | switch (consumedOperands) { |
| 9099 | case 0: |
| 9100 | // should all be handled by visitAggregate and createNoArgOperation |
| 9101 | assert(0); |
| 9102 | return 0; |
| 9103 | case 1: |
| 9104 | // should all be handled by createUnaryOperation |
| 9105 | assert(0); |
| 9106 | return 0; |
| 9107 | case 2: |
| 9108 | id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 9109 | break; |
| 9110 | default: |
| 9111 | // anything 3 or over doesn't have l-value operands, so all should be consumed |
| 9112 | assert(consumedOperands == operands.size()); |
| 9113 | id = builder.createOp(opCode, typeId, operands); |
| 9114 | break; |
| 9115 | } |
| 9116 | } |
| 9117 | |
| 9118 | #ifndef GLSLANG_WEB |
| 9119 | // Decode the return types that were structures |
| 9120 | switch (op) { |
| 9121 | case glslang::EOpAddCarry: |
| 9122 | case glslang::EOpSubBorrow: |
| 9123 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 9124 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 9125 | break; |
| 9126 | case glslang::EOpUMulExtended: |
| 9127 | case glslang::EOpIMulExtended: |
| 9128 | builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 9129 | builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 9130 | break; |
| 9131 | case glslang::EOpFrexp: |
| 9132 | { |
| 9133 | assert(operands.size() == 2); |
| 9134 | if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 9135 | // "exp" is floating-point type (from HLSL intrinsic) |
| 9136 | spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 9137 | member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 9138 | builder.createStore(member1, operands[1]); |
| 9139 | } else |
| 9140 | // "exp" is integer type (from GLSL built-in function) |
| 9141 | builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 9142 | id = builder.createCompositeExtract(id, typeId0, 0); |
| 9143 | } |
| 9144 | break; |
| 9145 | default: |
| 9146 | break; |
| 9147 | } |
| 9148 | #endif |
| 9149 | |
| 9150 | return builder.setPrecision(id, precision); |
| 9151 | } |
| 9152 | |
| 9153 | // Intrinsics with no arguments (or no return value, and no precision). |
| 9154 | spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId) |
| 9155 | { |
| 9156 | // GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 9157 | spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? |
| 9158 | spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 9159 | |
| 9160 | switch (op) { |
| 9161 | case glslang::EOpBarrier: |
| 9162 | if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 9163 | if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 9164 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 9165 | spv::MemorySemanticsOutputMemoryKHRMask | |
| 9166 | spv::MemorySemanticsAcquireReleaseMask); |
| 9167 | builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 9168 | } else { |
| 9169 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 9170 | } |
| 9171 | } else { |
| 9172 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 9173 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 9174 | spv::MemorySemanticsAcquireReleaseMask); |
| 9175 | } |
| 9176 | return 0; |
| 9177 | case glslang::EOpMemoryBarrier: |
| 9178 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 9179 | spv::MemorySemanticsAcquireReleaseMask); |
| 9180 | return 0; |
| 9181 | case glslang::EOpMemoryBarrierBuffer: |
| 9182 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 9183 | spv::MemorySemanticsAcquireReleaseMask); |
| 9184 | return 0; |
| 9185 | case glslang::EOpMemoryBarrierShared: |
| 9186 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 9187 | spv::MemorySemanticsAcquireReleaseMask); |
| 9188 | return 0; |
| 9189 | case glslang::EOpGroupMemoryBarrier: |
| 9190 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 9191 | spv::MemorySemanticsAcquireReleaseMask); |
| 9192 | return 0; |
| 9193 | #ifndef GLSLANG_WEB |
| 9194 | case glslang::EOpMemoryBarrierAtomicCounter: |
| 9195 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 9196 | spv::MemorySemanticsAcquireReleaseMask); |
| 9197 | return 0; |
| 9198 | case glslang::EOpMemoryBarrierImage: |
| 9199 | builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 9200 | spv::MemorySemanticsAcquireReleaseMask); |
| 9201 | return 0; |
| 9202 | case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 9203 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
| 9204 | spv::MemorySemanticsAllMemory | |
| 9205 | spv::MemorySemanticsAcquireReleaseMask); |
| 9206 | return 0; |
| 9207 | case glslang::EOpDeviceMemoryBarrier: |
| 9208 | builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 9209 | spv::MemorySemanticsImageMemoryMask | |
| 9210 | spv::MemorySemanticsAcquireReleaseMask); |
| 9211 | return 0; |
| 9212 | case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 9213 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 9214 | spv::MemorySemanticsImageMemoryMask | |
| 9215 | spv::MemorySemanticsAcquireReleaseMask); |
| 9216 | return 0; |
| 9217 | case glslang::EOpWorkgroupMemoryBarrier: |
| 9218 | builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 9219 | spv::MemorySemanticsAcquireReleaseMask); |
| 9220 | return 0; |
| 9221 | case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
| 9222 | builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 9223 | spv::MemorySemanticsWorkgroupMemoryMask | |
| 9224 | spv::MemorySemanticsAcquireReleaseMask); |
| 9225 | return 0; |
| 9226 | case glslang::EOpSubgroupBarrier: |
| 9227 | builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 9228 | spv::MemorySemanticsAcquireReleaseMask); |
| 9229 | return spv::NoResult; |
| 9230 | case glslang::EOpSubgroupMemoryBarrier: |
| 9231 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 9232 | spv::MemorySemanticsAcquireReleaseMask); |
| 9233 | return spv::NoResult; |
| 9234 | case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 9235 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 9236 | spv::MemorySemanticsAcquireReleaseMask); |
| 9237 | return spv::NoResult; |
| 9238 | case glslang::EOpSubgroupMemoryBarrierImage: |
| 9239 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 9240 | spv::MemorySemanticsAcquireReleaseMask); |
| 9241 | return spv::NoResult; |
| 9242 | case glslang::EOpSubgroupMemoryBarrierShared: |
| 9243 | builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 9244 | spv::MemorySemanticsAcquireReleaseMask); |
| 9245 | return spv::NoResult; |
| 9246 | |
| 9247 | case glslang::EOpEmitVertex: |
| 9248 | builder.createNoResultOp(spv::OpEmitVertex); |
| 9249 | return 0; |
| 9250 | case glslang::EOpEndPrimitive: |
| 9251 | builder.createNoResultOp(spv::OpEndPrimitive); |
| 9252 | return 0; |
| 9253 | |
| 9254 | case glslang::EOpSubgroupElect: { |
| 9255 | std::vector<spv::Id> operands; |
| 9256 | return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 9257 | } |
| 9258 | case glslang::EOpTime: |
| 9259 | { |
| 9260 | std::vector<spv::Id> args; // Dummy arguments |
| 9261 | spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 9262 | return builder.setPrecision(id, precision); |
| 9263 | } |
| 9264 | case glslang::EOpIgnoreIntersectionNV: |
| 9265 | builder.createNoResultOp(spv::OpIgnoreIntersectionNV); |
| 9266 | return 0; |
| 9267 | case glslang::EOpTerminateRayNV: |
| 9268 | builder.createNoResultOp(spv::OpTerminateRayNV); |
| 9269 | return 0; |
| 9270 | case glslang::EOpRayQueryInitialize: |
| 9271 | builder.createNoResultOp(spv::OpRayQueryInitializeKHR); |
| 9272 | return 0; |
| 9273 | case glslang::EOpRayQueryTerminate: |
| 9274 | builder.createNoResultOp(spv::OpRayQueryTerminateKHR); |
| 9275 | return 0; |
| 9276 | case glslang::EOpRayQueryGenerateIntersection: |
| 9277 | builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR); |
| 9278 | return 0; |
| 9279 | case glslang::EOpRayQueryConfirmIntersection: |
| 9280 | builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR); |
| 9281 | return 0; |
| 9282 | case glslang::EOpBeginInvocationInterlock: |
| 9283 | builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
| 9284 | return 0; |
| 9285 | case glslang::EOpEndInvocationInterlock: |
| 9286 | builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
| 9287 | return 0; |
| 9288 | |
| 9289 | case glslang::EOpIsHelperInvocation: |
| 9290 | { |
| 9291 | std::vector<spv::Id> args; // Dummy arguments |
| 9292 | builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 9293 | builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 9294 | return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args); |
| 9295 | } |
| 9296 | |
| 9297 | case glslang::EOpReadClockSubgroupKHR: { |
| 9298 | std::vector<spv::Id> args; |
| 9299 | args.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 9300 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 9301 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 9302 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 9303 | } |
| 9304 | |
| 9305 | case glslang::EOpReadClockDeviceKHR: { |
| 9306 | std::vector<spv::Id> args; |
| 9307 | args.push_back(builder.makeUintConstant(spv::ScopeDevice)); |
| 9308 | builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 9309 | builder.addCapability(spv::CapabilityShaderClockKHR); |
| 9310 | return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 9311 | } |
| 9312 | #endif |
| 9313 | case glslang::EOpStencilAttachmentReadEXT: |
| 9314 | case glslang::EOpDepthAttachmentReadEXT: |
| 9315 | { |
| 9316 | builder.addExtension(spv::E_SPV_EXT_shader_tile_image); |
| 9317 | |
| 9318 | spv::Decoration precision; |
| 9319 | spv::Op spv_op; |
| 9320 | if (op == glslang::EOpStencilAttachmentReadEXT) |
| 9321 | { |
| 9322 | precision = spv::DecorationRelaxedPrecision; |
| 9323 | spv_op = spv::OpStencilAttachmentReadEXT; |
| 9324 | builder.addCapability(spv::CapabilityTileImageStencilReadAccessEXT); |
| 9325 | } |
| 9326 | else |
| 9327 | { |
| 9328 | precision = spv::NoPrecision; |
| 9329 | spv_op = spv::OpDepthAttachmentReadEXT; |
| 9330 | builder.addCapability(spv::CapabilityTileImageDepthReadAccessEXT); |
| 9331 | } |
| 9332 | |
| 9333 | std::vector<spv::Id> args; // Dummy args |
| 9334 | spv::Id result = builder.createOp(spv_op, typeId, args); |
| 9335 | return builder.setPrecision(result, precision); |
| 9336 | } |
| 9337 | default: |
| 9338 | break; |
| 9339 | } |
| 9340 | |
| 9341 | logger->missingFunctionality("unknown operation with no arguments" ); |
| 9342 | |
| 9343 | return 0; |
| 9344 | } |
| 9345 | |
| 9346 | spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 9347 | { |
| 9348 | auto iter = symbolValues.find(symbol->getId()); |
| 9349 | spv::Id id; |
| 9350 | if (symbolValues.end() != iter) { |
| 9351 | id = iter->second; |
| 9352 | return id; |
| 9353 | } |
| 9354 | |
| 9355 | // it was not found, create it |
| 9356 | spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
| 9357 | auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType()); |
| 9358 | |
| 9359 | // There are pairs of symbols that map to the same SPIR-V built-in: |
| 9360 | // gl_ObjectToWorldEXT and gl_ObjectToWorld3x4EXT, and gl_WorldToObjectEXT |
| 9361 | // and gl_WorldToObject3x4EXT. SPIR-V forbids having two OpVariables |
| 9362 | // with the same BuiltIn in the same storage class, so we must re-use one. |
| 9363 | const bool mayNeedToReuseBuiltIn = |
| 9364 | builtIn == spv::BuiltInObjectToWorldKHR || |
| 9365 | builtIn == spv::BuiltInWorldToObjectKHR; |
| 9366 | |
| 9367 | if (mayNeedToReuseBuiltIn) { |
| 9368 | auto iter = builtInVariableIds.find(uint32_t(builtIn)); |
| 9369 | if (builtInVariableIds.end() != iter) { |
| 9370 | id = iter->second; |
| 9371 | symbolValues[symbol->getId()] = id; |
| 9372 | if (forcedType.second != spv::NoType) |
| 9373 | forceType[id] = forcedType.second; |
| 9374 | return id; |
| 9375 | } |
| 9376 | } |
| 9377 | |
| 9378 | id = createSpvVariable(symbol, forcedType.first); |
| 9379 | |
| 9380 | if (mayNeedToReuseBuiltIn) { |
| 9381 | builtInVariableIds.insert({uint32_t(builtIn), id}); |
| 9382 | } |
| 9383 | |
| 9384 | symbolValues[symbol->getId()] = id; |
| 9385 | if (forcedType.second != spv::NoType) |
| 9386 | forceType[id] = forcedType.second; |
| 9387 | |
| 9388 | if (symbol->getBasicType() != glslang::EbtBlock) { |
| 9389 | builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 9390 | builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 9391 | builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
| 9392 | #ifndef GLSLANG_WEB |
| 9393 | addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
| 9394 | if (symbol->getQualifier().hasComponent()) |
| 9395 | builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 9396 | if (symbol->getQualifier().hasIndex()) |
| 9397 | builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 9398 | #endif |
| 9399 | if (symbol->getType().getQualifier().hasSpecConstantId()) |
| 9400 | builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
| 9401 | // atomic counters use this: |
| 9402 | if (symbol->getQualifier().hasOffset()) |
| 9403 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
| 9404 | } |
| 9405 | |
| 9406 | if (symbol->getQualifier().hasLocation()) { |
| 9407 | if (!(glslangIntermediate->isRayTracingStage() && |
| 9408 | (glslangIntermediate->IsRequestedExtension(glslang::E_GL_EXT_ray_tracing) || |
| 9409 | glslangIntermediate->IsRequestedExtension(glslang::E_GL_NV_shader_invocation_reorder)) |
| 9410 | && (builder.getStorageClass(id) == spv::StorageClassRayPayloadKHR || |
| 9411 | builder.getStorageClass(id) == spv::StorageClassIncomingRayPayloadKHR || |
| 9412 | builder.getStorageClass(id) == spv::StorageClassCallableDataKHR || |
| 9413 | builder.getStorageClass(id) == spv::StorageClassIncomingCallableDataKHR || |
| 9414 | builder.getStorageClass(id) == spv::StorageClassHitObjectAttributeNV))) { |
| 9415 | // Location values are used to link TraceRayKHR/ExecuteCallableKHR/HitObjectGetAttributesNV |
| 9416 | // to corresponding variables but are not valid in SPIRV since they are supported only |
| 9417 | // for Input/Output Storage classes. |
| 9418 | builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
| 9419 | } |
| 9420 | } |
| 9421 | |
| 9422 | builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
| 9423 | if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
| 9424 | builder.addCapability(spv::CapabilityGeometryStreams); |
| 9425 | builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
| 9426 | } |
| 9427 | if (symbol->getQualifier().hasSet()) |
| 9428 | builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
| 9429 | else if (IsDescriptorResource(symbol->getType())) { |
| 9430 | // default to 0 |
| 9431 | builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 9432 | } |
| 9433 | if (symbol->getQualifier().hasBinding()) |
| 9434 | builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
| 9435 | else if (IsDescriptorResource(symbol->getType())) { |
| 9436 | // default to 0 |
| 9437 | builder.addDecoration(id, spv::DecorationBinding, 0); |
| 9438 | } |
| 9439 | if (symbol->getQualifier().hasAttachment()) |
| 9440 | builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
| 9441 | if (glslangIntermediate->getXfbMode()) { |
| 9442 | builder.addCapability(spv::CapabilityTransformFeedback); |
| 9443 | if (symbol->getQualifier().hasXfbBuffer()) { |
| 9444 | builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 9445 | unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 9446 | if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 9447 | builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 9448 | } |
| 9449 | if (symbol->getQualifier().hasXfbOffset()) |
| 9450 | builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
| 9451 | } |
| 9452 | |
| 9453 | // add built-in variable decoration |
| 9454 | if (builtIn != spv::BuiltInMax) { |
| 9455 | // WorkgroupSize deprecated in spirv1.6 |
| 9456 | if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_6 || |
| 9457 | builtIn != spv::BuiltInWorkgroupSize) |
| 9458 | builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
| 9459 | } |
| 9460 | |
| 9461 | // Add volatile decoration to HelperInvocation for spirv1.6 and beyond |
| 9462 | if (builtIn == spv::BuiltInHelperInvocation && |
| 9463 | !glslangIntermediate->usingVulkanMemoryModel() && |
| 9464 | glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6) { |
| 9465 | builder.addDecoration(id, spv::DecorationVolatile); |
| 9466 | } |
| 9467 | |
| 9468 | #ifndef GLSLANG_WEB |
| 9469 | // Subgroup builtins which have input storage class are volatile for ray tracing stages. |
| 9470 | if (symbol->getType().isImage() || symbol->getQualifier().isPipeInput()) { |
| 9471 | std::vector<spv::Decoration> memory; |
| 9472 | TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, |
| 9473 | glslangIntermediate->usingVulkanMemoryModel()); |
| 9474 | for (unsigned int i = 0; i < memory.size(); ++i) |
| 9475 | builder.addDecoration(id, memory[i]); |
| 9476 | } |
| 9477 | |
| 9478 | if (builtIn == spv::BuiltInSampleMask) { |
| 9479 | spv::Decoration decoration; |
| 9480 | // GL_NV_sample_mask_override_coverage extension |
| 9481 | if (glslangIntermediate->getLayoutOverrideCoverage()) |
| 9482 | decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
| 9483 | else |
| 9484 | decoration = (spv::Decoration)spv::DecorationMax; |
| 9485 | builder.addDecoration(id, decoration); |
| 9486 | if (decoration != spv::DecorationMax) { |
| 9487 | builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV); |
| 9488 | builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 9489 | } |
| 9490 | } |
| 9491 | else if (builtIn == spv::BuiltInLayer) { |
| 9492 | // SPV_NV_viewport_array2 extension |
| 9493 | if (symbol->getQualifier().layoutViewportRelative) { |
| 9494 | builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 9495 | builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 9496 | builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 9497 | } |
| 9498 | if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
| 9499 | builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 9500 | symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
| 9501 | builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 9502 | builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 9503 | } |
| 9504 | } |
| 9505 | |
| 9506 | if (symbol->getQualifier().layoutPassthrough) { |
| 9507 | builder.addDecoration(id, spv::DecorationPassthroughNV); |
| 9508 | builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 9509 | builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 9510 | } |
| 9511 | if (symbol->getQualifier().pervertexNV) { |
| 9512 | builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 9513 | builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 9514 | builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 9515 | } |
| 9516 | |
| 9517 | if (symbol->getQualifier().pervertexEXT) { |
| 9518 | builder.addDecoration(id, spv::DecorationPerVertexKHR); |
| 9519 | builder.addCapability(spv::CapabilityFragmentBarycentricKHR); |
| 9520 | builder.addExtension(spv::E_SPV_KHR_fragment_shader_barycentric); |
| 9521 | } |
| 9522 | |
| 9523 | if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 9524 | builder.addExtension("SPV_GOOGLE_hlsl_functionality1" ); |
| 9525 | builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 9526 | symbol->getType().getQualifier().semanticName); |
| 9527 | } |
| 9528 | |
| 9529 | if (symbol->isReference()) { |
| 9530 | builder.addDecoration(id, symbol->getType().getQualifier().restrict ? |
| 9531 | spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 9532 | } |
| 9533 | |
| 9534 | // |
| 9535 | // Add SPIR-V decorations for structure (GL_EXT_spirv_intrinsics) |
| 9536 | // |
| 9537 | if (symbol->getType().getQualifier().hasSprivDecorate()) { |
| 9538 | const glslang::TSpirvDecorate& spirvDecorate = symbol->getType().getQualifier().getSpirvDecorate(); |
| 9539 | |
| 9540 | // Add spirv_decorate |
| 9541 | for (auto& decorate : spirvDecorate.decorates) { |
| 9542 | if (!decorate.second.empty()) { |
| 9543 | std::vector<unsigned> literals; |
| 9544 | TranslateLiterals(decorate.second, literals); |
| 9545 | builder.addDecoration(id, static_cast<spv::Decoration>(decorate.first), literals); |
| 9546 | } |
| 9547 | else |
| 9548 | builder.addDecoration(id, static_cast<spv::Decoration>(decorate.first)); |
| 9549 | } |
| 9550 | |
| 9551 | // Add spirv_decorate_id |
| 9552 | for (auto& decorateId : spirvDecorate.decorateIds) { |
| 9553 | std::vector<spv::Id> operandIds; |
| 9554 | assert(!decorateId.second.empty()); |
| 9555 | for (auto extraOperand : decorateId.second) { |
| 9556 | if (extraOperand->getQualifier().isFrontEndConstant()) |
| 9557 | operandIds.push_back(createSpvConstant(*extraOperand)); |
| 9558 | else |
| 9559 | operandIds.push_back(getSymbolId(extraOperand->getAsSymbolNode())); |
| 9560 | } |
| 9561 | builder.addDecorationId(id, static_cast<spv::Decoration>(decorateId.first), operandIds); |
| 9562 | } |
| 9563 | |
| 9564 | // Add spirv_decorate_string |
| 9565 | for (auto& decorateString : spirvDecorate.decorateStrings) { |
| 9566 | std::vector<const char*> strings; |
| 9567 | assert(!decorateString.second.empty()); |
| 9568 | for (auto extraOperand : decorateString.second) { |
| 9569 | const char* string = extraOperand->getConstArray()[0].getSConst()->c_str(); |
| 9570 | strings.push_back(string); |
| 9571 | } |
| 9572 | builder.addDecoration(id, static_cast<spv::Decoration>(decorateString.first), strings); |
| 9573 | } |
| 9574 | } |
| 9575 | #endif |
| 9576 | |
| 9577 | return id; |
| 9578 | } |
| 9579 | |
| 9580 | #ifndef GLSLANG_WEB |
| 9581 | // add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 9582 | void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 9583 | { |
| 9584 | bool isMeshShaderExt = (glslangIntermediate->getRequestedExtensions().find(glslang::E_GL_EXT_mesh_shader) != |
| 9585 | glslangIntermediate->getRequestedExtensions().end()); |
| 9586 | |
| 9587 | if (member >= 0) { |
| 9588 | if (qualifier.perPrimitiveNV) { |
| 9589 | // Need to add capability/extension for fragment shader. |
| 9590 | // Mesh shader already adds this by default. |
| 9591 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 9592 | if(isMeshShaderExt) { |
| 9593 | builder.addCapability(spv::CapabilityMeshShadingEXT); |
| 9594 | builder.addExtension(spv::E_SPV_EXT_mesh_shader); |
| 9595 | } else { |
| 9596 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 9597 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 9598 | } |
| 9599 | } |
| 9600 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
| 9601 | } |
| 9602 | if (qualifier.perViewNV) |
| 9603 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 9604 | if (qualifier.perTaskNV) |
| 9605 | builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 9606 | } else { |
| 9607 | if (qualifier.perPrimitiveNV) { |
| 9608 | // Need to add capability/extension for fragment shader. |
| 9609 | // Mesh shader already adds this by default. |
| 9610 | if (glslangIntermediate->getStage() == EShLangFragment) { |
| 9611 | if(isMeshShaderExt) { |
| 9612 | builder.addCapability(spv::CapabilityMeshShadingEXT); |
| 9613 | builder.addExtension(spv::E_SPV_EXT_mesh_shader); |
| 9614 | } else { |
| 9615 | builder.addCapability(spv::CapabilityMeshShadingNV); |
| 9616 | builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 9617 | } |
| 9618 | } |
| 9619 | builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
| 9620 | } |
| 9621 | if (qualifier.perViewNV) |
| 9622 | builder.addDecoration(id, spv::DecorationPerViewNV); |
| 9623 | if (qualifier.perTaskNV) |
| 9624 | builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 9625 | } |
| 9626 | } |
| 9627 | #endif |
| 9628 | |
| 9629 | // Make a full tree of instructions to build a SPIR-V specialization constant, |
| 9630 | // or regular constant if possible. |
| 9631 | // |
| 9632 | // TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 9633 | // |
| 9634 | // Recursively walk the nodes. The nodes form a tree whose leaves are |
| 9635 | // regular constants, which themselves are trees that createSpvConstant() |
| 9636 | // recursively walks. So, this function walks the "top" of the tree: |
| 9637 | // - emit specialization constant-building instructions for specConstant |
| 9638 | // - when running into a non-spec-constant, switch to createSpvConstant() |
| 9639 | spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
| 9640 | { |
| 9641 | assert(node.getQualifier().isConstant()); |
| 9642 | |
| 9643 | // Handle front-end constants first (non-specialization constants). |
| 9644 | if (! node.getQualifier().specConstant) { |
| 9645 | // hand off to the non-spec-constant path |
| 9646 | assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 9647 | int nextConst = 0; |
| 9648 | return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? |
| 9649 | node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
| 9650 | nextConst, false); |
| 9651 | } |
| 9652 | |
| 9653 | // We now know we have a specialization constant to build |
| 9654 | |
| 9655 | // Extra capabilities may be needed. |
| 9656 | if (node.getType().contains8BitInt()) |
| 9657 | builder.addCapability(spv::CapabilityInt8); |
| 9658 | if (node.getType().contains16BitFloat()) |
| 9659 | builder.addCapability(spv::CapabilityFloat16); |
| 9660 | if (node.getType().contains16BitInt()) |
| 9661 | builder.addCapability(spv::CapabilityInt16); |
| 9662 | if (node.getType().contains64BitInt()) |
| 9663 | builder.addCapability(spv::CapabilityInt64); |
| 9664 | if (node.getType().containsDouble()) |
| 9665 | builder.addCapability(spv::CapabilityFloat64); |
| 9666 | |
| 9667 | // gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants, |
| 9668 | // even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 9669 | if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 9670 | std::vector<spv::Id> dimConstId; |
| 9671 | for (int dim = 0; dim < 3; ++dim) { |
| 9672 | bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 9673 | dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 9674 | if (specConst) { |
| 9675 | builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 9676 | glslangIntermediate->getLocalSizeSpecId(dim)); |
| 9677 | } |
| 9678 | } |
| 9679 | return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 9680 | } |
| 9681 | |
| 9682 | // An AST node labelled as specialization constant should be a symbol node. |
| 9683 | // Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 9684 | if (auto* sn = node.getAsSymbolNode()) { |
| 9685 | spv::Id result; |
| 9686 | if (auto* sub_tree = sn->getConstSubtree()) { |
| 9687 | // Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 9688 | // During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 9689 | // will set the builder into spec constant op instruction generating mode. |
| 9690 | sub_tree->traverse(this); |
| 9691 | result = accessChainLoad(sub_tree->getType()); |
| 9692 | } else if (auto* const_union_array = &sn->getConstArray()) { |
| 9693 | int nextConst = 0; |
| 9694 | result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
| 9695 | } else { |
| 9696 | logger->missingFunctionality("Invalid initializer for spec onstant." ); |
| 9697 | return spv::NoResult; |
| 9698 | } |
| 9699 | builder.addName(result, sn->getName().c_str()); |
| 9700 | return result; |
| 9701 | } |
| 9702 | |
| 9703 | // Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 9704 | // constant sub tree as initializer. |
| 9705 | logger->missingFunctionality("Neither a front-end constant nor a spec constant." ); |
| 9706 | return spv::NoResult; |
| 9707 | } |
| 9708 | |
| 9709 | // Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 9710 | // build the aggregate SPIR-V constant. |
| 9711 | // |
| 9712 | // If there are not enough elements present in 'consts', 0 will be substituted; |
| 9713 | // an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 9714 | // |
| 9715 | spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, |
| 9716 | const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
| 9717 | { |
| 9718 | // vector of constants for SPIR-V |
| 9719 | std::vector<spv::Id> spvConsts; |
| 9720 | |
| 9721 | // Type is used for struct and array constants |
| 9722 | spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 9723 | |
| 9724 | if (glslangType.isArray()) { |
| 9725 | glslang::TType elementType(glslangType, 0); |
| 9726 | for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
| 9727 | spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
| 9728 | } else if (glslangType.isMatrix()) { |
| 9729 | glslang::TType vectorType(glslangType, 0); |
| 9730 | for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
| 9731 | spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
| 9732 | } else if (glslangType.isCoopMat()) { |
| 9733 | glslang::TType componentType(glslangType.getBasicType()); |
| 9734 | spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
| 9735 | } else if (glslangType.isStruct()) { |
| 9736 | glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 9737 | for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
| 9738 | spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
| 9739 | } else if (glslangType.getVectorSize() > 1) { |
| 9740 | for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 9741 | bool zero = nextConst >= consts.size(); |
| 9742 | switch (glslangType.getBasicType()) { |
| 9743 | case glslang::EbtInt: |
| 9744 | spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 9745 | break; |
| 9746 | case glslang::EbtUint: |
| 9747 | spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 9748 | break; |
| 9749 | case glslang::EbtFloat: |
| 9750 | spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 9751 | break; |
| 9752 | case glslang::EbtBool: |
| 9753 | spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 9754 | break; |
| 9755 | #ifndef GLSLANG_WEB |
| 9756 | case glslang::EbtInt8: |
| 9757 | builder.addCapability(spv::CapabilityInt8); |
| 9758 | spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 9759 | break; |
| 9760 | case glslang::EbtUint8: |
| 9761 | builder.addCapability(spv::CapabilityInt8); |
| 9762 | spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 9763 | break; |
| 9764 | case glslang::EbtInt16: |
| 9765 | builder.addCapability(spv::CapabilityInt16); |
| 9766 | spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 9767 | break; |
| 9768 | case glslang::EbtUint16: |
| 9769 | builder.addCapability(spv::CapabilityInt16); |
| 9770 | spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 9771 | break; |
| 9772 | case glslang::EbtInt64: |
| 9773 | spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 9774 | break; |
| 9775 | case glslang::EbtUint64: |
| 9776 | spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 9777 | break; |
| 9778 | case glslang::EbtDouble: |
| 9779 | spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 9780 | break; |
| 9781 | case glslang::EbtFloat16: |
| 9782 | builder.addCapability(spv::CapabilityFloat16); |
| 9783 | spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 9784 | break; |
| 9785 | #endif |
| 9786 | default: |
| 9787 | assert(0); |
| 9788 | break; |
| 9789 | } |
| 9790 | ++nextConst; |
| 9791 | } |
| 9792 | } else { |
| 9793 | // we have a non-aggregate (scalar) constant |
| 9794 | bool zero = nextConst >= consts.size(); |
| 9795 | spv::Id scalar = 0; |
| 9796 | switch (glslangType.getBasicType()) { |
| 9797 | case glslang::EbtInt: |
| 9798 | scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
| 9799 | break; |
| 9800 | case glslang::EbtUint: |
| 9801 | scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
| 9802 | break; |
| 9803 | case glslang::EbtFloat: |
| 9804 | scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 9805 | break; |
| 9806 | case glslang::EbtBool: |
| 9807 | scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
| 9808 | break; |
| 9809 | #ifndef GLSLANG_WEB |
| 9810 | case glslang::EbtInt8: |
| 9811 | builder.addCapability(spv::CapabilityInt8); |
| 9812 | scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 9813 | break; |
| 9814 | case glslang::EbtUint8: |
| 9815 | builder.addCapability(spv::CapabilityInt8); |
| 9816 | scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 9817 | break; |
| 9818 | case glslang::EbtInt16: |
| 9819 | builder.addCapability(spv::CapabilityInt16); |
| 9820 | scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 9821 | break; |
| 9822 | case glslang::EbtUint16: |
| 9823 | builder.addCapability(spv::CapabilityInt16); |
| 9824 | scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 9825 | break; |
| 9826 | case glslang::EbtInt64: |
| 9827 | scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 9828 | break; |
| 9829 | case glslang::EbtUint64: |
| 9830 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 9831 | break; |
| 9832 | case glslang::EbtDouble: |
| 9833 | scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
| 9834 | break; |
| 9835 | case glslang::EbtFloat16: |
| 9836 | builder.addCapability(spv::CapabilityFloat16); |
| 9837 | scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 9838 | break; |
| 9839 | case glslang::EbtReference: |
| 9840 | scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 9841 | scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar); |
| 9842 | break; |
| 9843 | #endif |
| 9844 | case glslang::EbtString: |
| 9845 | scalar = builder.getStringId(consts[nextConst].getSConst()->c_str()); |
| 9846 | break; |
| 9847 | default: |
| 9848 | assert(0); |
| 9849 | break; |
| 9850 | } |
| 9851 | ++nextConst; |
| 9852 | return scalar; |
| 9853 | } |
| 9854 | |
| 9855 | return builder.makeCompositeConstant(typeId, spvConsts); |
| 9856 | } |
| 9857 | |
| 9858 | // Return true if the node is a constant or symbol whose reading has no |
| 9859 | // non-trivial observable cost or effect. |
| 9860 | bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 9861 | { |
| 9862 | // don't know what this is |
| 9863 | if (node == nullptr) |
| 9864 | return false; |
| 9865 | |
| 9866 | // a constant is safe |
| 9867 | if (node->getAsConstantUnion() != nullptr) |
| 9868 | return true; |
| 9869 | |
| 9870 | // not a symbol means non-trivial |
| 9871 | if (node->getAsSymbolNode() == nullptr) |
| 9872 | return false; |
| 9873 | |
| 9874 | // a symbol, depends on what's being read |
| 9875 | switch (node->getType().getQualifier().storage) { |
| 9876 | case glslang::EvqTemporary: |
| 9877 | case glslang::EvqGlobal: |
| 9878 | case glslang::EvqIn: |
| 9879 | case glslang::EvqInOut: |
| 9880 | case glslang::EvqConst: |
| 9881 | case glslang::EvqConstReadOnly: |
| 9882 | case glslang::EvqUniform: |
| 9883 | return true; |
| 9884 | default: |
| 9885 | return false; |
| 9886 | } |
| 9887 | } |
| 9888 | |
| 9889 | // A node is trivial if it is a single operation with no side effects. |
| 9890 | // HLSL (and/or vectors) are always trivial, as it does not short circuit. |
| 9891 | // Otherwise, error on the side of saying non-trivial. |
| 9892 | // Return true if trivial. |
| 9893 | bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 9894 | { |
| 9895 | if (node == nullptr) |
| 9896 | return false; |
| 9897 | |
| 9898 | // count non scalars as trivial, as well as anything coming from HLSL |
| 9899 | if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
| 9900 | return true; |
| 9901 | |
| 9902 | // symbols and constants are trivial |
| 9903 | if (isTrivialLeaf(node)) |
| 9904 | return true; |
| 9905 | |
| 9906 | // otherwise, it needs to be a simple operation or one or two leaf nodes |
| 9907 | |
| 9908 | // not a simple operation |
| 9909 | const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 9910 | const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 9911 | if (binaryNode == nullptr && unaryNode == nullptr) |
| 9912 | return false; |
| 9913 | |
| 9914 | // not on leaf nodes |
| 9915 | if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 9916 | return false; |
| 9917 | |
| 9918 | if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 9919 | return false; |
| 9920 | } |
| 9921 | |
| 9922 | switch (node->getAsOperator()->getOp()) { |
| 9923 | case glslang::EOpLogicalNot: |
| 9924 | case glslang::EOpConvIntToBool: |
| 9925 | case glslang::EOpConvUintToBool: |
| 9926 | case glslang::EOpConvFloatToBool: |
| 9927 | case glslang::EOpConvDoubleToBool: |
| 9928 | case glslang::EOpEqual: |
| 9929 | case glslang::EOpNotEqual: |
| 9930 | case glslang::EOpLessThan: |
| 9931 | case glslang::EOpGreaterThan: |
| 9932 | case glslang::EOpLessThanEqual: |
| 9933 | case glslang::EOpGreaterThanEqual: |
| 9934 | case glslang::EOpIndexDirect: |
| 9935 | case glslang::EOpIndexDirectStruct: |
| 9936 | case glslang::EOpLogicalXor: |
| 9937 | case glslang::EOpAny: |
| 9938 | case glslang::EOpAll: |
| 9939 | return true; |
| 9940 | default: |
| 9941 | return false; |
| 9942 | } |
| 9943 | } |
| 9944 | |
| 9945 | // Emit short-circuiting code, where 'right' is never evaluated unless |
| 9946 | // the left side is true (for &&) or false (for ||). |
| 9947 | spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, |
| 9948 | glslang::TIntermTyped& right) |
| 9949 | { |
| 9950 | spv::Id boolTypeId = builder.makeBoolType(); |
| 9951 | |
| 9952 | // emit left operand |
| 9953 | builder.clearAccessChain(); |
| 9954 | left.traverse(this); |
| 9955 | spv::Id leftId = accessChainLoad(left.getType()); |
| 9956 | |
| 9957 | // Operands to accumulate OpPhi operands |
| 9958 | std::vector<spv::Id> phiOperands; |
| 9959 | // accumulate left operand's phi information |
| 9960 | phiOperands.push_back(leftId); |
| 9961 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 9962 | |
| 9963 | // Make the two kinds of operation symmetric with a "!" |
| 9964 | // || => emit "if (! left) result = right" |
| 9965 | // && => emit "if ( left) result = right" |
| 9966 | // |
| 9967 | // TODO: this runtime "not" for || could be avoided by adding functionality |
| 9968 | // to 'builder' to have an "else" without an "then" |
| 9969 | if (op == glslang::EOpLogicalOr) |
| 9970 | leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 9971 | |
| 9972 | // make an "if" based on the left value |
| 9973 | spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
| 9974 | |
| 9975 | // emit right operand as the "then" part of the "if" |
| 9976 | builder.clearAccessChain(); |
| 9977 | right.traverse(this); |
| 9978 | spv::Id rightId = accessChainLoad(right.getType()); |
| 9979 | |
| 9980 | // accumulate left operand's phi information |
| 9981 | phiOperands.push_back(rightId); |
| 9982 | phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 9983 | |
| 9984 | // finish the "if" |
| 9985 | ifBuilder.makeEndIf(); |
| 9986 | |
| 9987 | // phi together the two results |
| 9988 | return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 9989 | } |
| 9990 | |
| 9991 | #ifndef GLSLANG_WEB |
| 9992 | // Return type Id of the imported set of extended instructions corresponds to the name. |
| 9993 | // Import this set if it has not been imported yet. |
| 9994 | spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 9995 | { |
| 9996 | if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 9997 | return extBuiltinMap[name]; |
| 9998 | else { |
| 9999 | builder.addExtension(name); |
| 10000 | spv::Id extBuiltins = builder.import(name); |
| 10001 | extBuiltinMap[name] = extBuiltins; |
| 10002 | return extBuiltins; |
| 10003 | } |
| 10004 | } |
| 10005 | #endif |
| 10006 | |
| 10007 | }; // end anonymous namespace |
| 10008 | |
| 10009 | namespace glslang { |
| 10010 | |
| 10011 | void GetSpirvVersion(std::string& version) |
| 10012 | { |
| 10013 | const int bufSize = 100; |
| 10014 | char buf[bufSize]; |
| 10015 | snprintf(buf, bufSize, "0x%08x, Revision %d" , spv::Version, spv::Revision); |
| 10016 | version = buf; |
| 10017 | } |
| 10018 | |
| 10019 | // For low-order part of the generator's magic number. Bump up |
| 10020 | // when there is a change in the style (e.g., if SSA form changes, |
| 10021 | // or a different instruction sequence to do something gets used). |
| 10022 | int GetSpirvGeneratorVersion() |
| 10023 | { |
| 10024 | // return 1; // start |
| 10025 | // return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V |
| 10026 | // return 3; // change/correct barrier-instruction operands, to match memory model group decisions |
| 10027 | // return 4; // some deeper access chains: for dynamic vector component, and local Boolean component |
| 10028 | // return 5; // make OpArrayLength result type be an int with signedness of 0 |
| 10029 | // return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 10030 | // versions 4 and 6 each generate OpArrayLength as it has long been done |
| 10031 | // return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent |
| 10032 | // return 8; // switch to new dead block eliminator; use OpUnreachable |
| 10033 | // return 9; // don't include opaque function parameters in OpEntryPoint global's operand list |
| 10034 | // return 10; // Generate OpFUnordNotEqual for != comparisons |
| 10035 | return 11; // Make OpEmitMeshTasksEXT a terminal instruction |
| 10036 | } |
| 10037 | |
| 10038 | // Write SPIR-V out to a binary file |
| 10039 | void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
| 10040 | { |
| 10041 | std::ofstream out; |
| 10042 | out.open(baseName, std::ios::binary | std::ios::out); |
| 10043 | if (out.fail()) |
| 10044 | printf("ERROR: Failed to open file: %s\n" , baseName); |
| 10045 | for (int i = 0; i < (int)spirv.size(); ++i) { |
| 10046 | unsigned int word = spirv[i]; |
| 10047 | out.write((const char*)&word, 4); |
| 10048 | } |
| 10049 | out.close(); |
| 10050 | } |
| 10051 | |
| 10052 | // Write SPIR-V out to a text file with 32-bit hexadecimal words |
| 10053 | void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName) |
| 10054 | { |
| 10055 | #if !defined(GLSLANG_WEB) |
| 10056 | std::ofstream out; |
| 10057 | out.open(baseName, std::ios::binary | std::ios::out); |
| 10058 | if (out.fail()) |
| 10059 | printf("ERROR: Failed to open file: %s\n" , baseName); |
| 10060 | out << "\t// " << |
| 10061 | GetSpirvGeneratorVersion() << |
| 10062 | GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH << |
| 10063 | GLSLANG_VERSION_FLAVOR << std::endl; |
| 10064 | if (varName != nullptr) { |
| 10065 | out << "\t #pragma once" << std::endl; |
| 10066 | out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 10067 | } |
| 10068 | const int WORDS_PER_LINE = 8; |
| 10069 | for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 10070 | out << "\t" ; |
| 10071 | for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 10072 | const unsigned int word = spirv[i + j]; |
| 10073 | out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 10074 | if (i + j + 1 < (int)spirv.size()) { |
| 10075 | out << "," ; |
| 10076 | } |
| 10077 | } |
| 10078 | out << std::endl; |
| 10079 | } |
| 10080 | if (varName != nullptr) { |
| 10081 | out << "};" ; |
| 10082 | out << std::endl; |
| 10083 | } |
| 10084 | out.close(); |
| 10085 | #endif |
| 10086 | } |
| 10087 | |
| 10088 | // |
| 10089 | // Set up the glslang traversal |
| 10090 | // |
| 10091 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
| 10092 | { |
| 10093 | spv::SpvBuildLogger logger; |
| 10094 | GlslangToSpv(intermediate, spirv, &logger, options); |
| 10095 | } |
| 10096 | |
| 10097 | void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
| 10098 | spv::SpvBuildLogger* logger, SpvOptions* options) |
| 10099 | { |
| 10100 | TIntermNode* root = intermediate.getTreeRoot(); |
| 10101 | |
| 10102 | if (root == nullptr) |
| 10103 | return; |
| 10104 | |
| 10105 | SpvOptions defaultOptions; |
| 10106 | if (options == nullptr) |
| 10107 | options = &defaultOptions; |
| 10108 | |
| 10109 | GetThreadPoolAllocator().push(); |
| 10110 | |
| 10111 | TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
| 10112 | root->traverse(&it); |
| 10113 | it.finishSpv(); |
| 10114 | it.dumpSpv(spirv); |
| 10115 | |
| 10116 | #if ENABLE_OPT |
| 10117 | // If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 10118 | // eg. forward and remove memory writes of opaque types. |
| 10119 | bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
| 10120 | if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) { |
| 10121 | SpirvToolsTransform(intermediate, spirv, logger, options); |
| 10122 | prelegalization = false; |
| 10123 | } |
| 10124 | else if (options->stripDebugInfo) { |
| 10125 | // Strip debug info even if optimization is disabled. |
| 10126 | SpirvToolsStripDebugInfo(intermediate, spirv, logger); |
| 10127 | } |
| 10128 | |
| 10129 | if (options->validate) |
| 10130 | SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
| 10131 | |
| 10132 | if (options->disassemble) |
| 10133 | SpirvToolsDisassemble(std::cout, spirv); |
| 10134 | |
| 10135 | #endif |
| 10136 | |
| 10137 | GetThreadPoolAllocator().pop(); |
| 10138 | } |
| 10139 | |
| 10140 | }; // end namespace glslang |
| 10141 | |