| 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 "NonSemanticDebugPrintf.h" |
| 53 |
} |
| 54 |
|
| 55 |
// Glslang includes |
| 56 |
#include "../glslang/MachineIndependent/localintermediate.h" |
| 57 |
#include "../glslang/MachineIndependent/SymbolTable.h" |
| 58 |
#include "../glslang/Include/Common.h" |
| 59 |
|
| 60 |
// Build-time generated includes |
| 61 |
#include "glslang/build_info.h" |
| 62 |
|
| 63 |
#include <fstream> |
| 64 |
#include <iomanip> |
| 65 |
#include <list> |
| 66 |
#include <map> |
| 67 |
#include <stack> |
| 68 |
#include <string> |
| 69 |
#include <vector> |
| 70 |
|
| 71 |
namespace { |
| 72 |
|
| 73 |
namespace { |
| 74 |
class SpecConstantOpModeGuard { |
| 75 |
public: |
| 76 |
SpecConstantOpModeGuard(spv::Builder* builder) |
| 77 |
: builder_(builder) { |
| 78 |
previous_flag_ = builder->isInSpecConstCodeGenMode(); |
| 79 |
} |
| 80 |
~SpecConstantOpModeGuard() { |
| 81 |
previous_flag_ ? builder_->setToSpecConstCodeGenMode() |
| 82 |
: builder_->setToNormalCodeGenMode(); |
| 83 |
} |
| 84 |
void turnOnSpecConstantOpMode() { |
| 85 |
builder_->setToSpecConstCodeGenMode(); |
| 86 |
} |
| 87 |
|
| 88 |
private: |
| 89 |
spv::Builder* builder_; |
| 90 |
bool previous_flag_; |
| 91 |
}; |
| 92 |
|
| 93 |
struct OpDecorations { |
| 94 |
public: |
| 95 |
OpDecorations(spv::Decoration precision, spv::Decoration noContraction, spv::Decoration nonUniform) : |
| 96 |
precision(precision) |
| 97 |
#ifndef GLSLANG_WEB |
| 98 |
, |
| 99 |
noContraction(noContraction), |
| 100 |
nonUniform(nonUniform) |
| 101 |
#endif |
| 102 |
{ } |
| 103 |
|
| 104 |
spv::Decoration precision; |
| 105 |
|
| 106 |
#ifdef GLSLANG_WEB |
| 107 |
void addNoContraction(spv::Builder&, spv::Id) const { } |
| 108 |
void addNonUniform(spv::Builder&, spv::Id) const { } |
| 109 |
#else |
| 110 |
void addNoContraction(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, noContraction); } |
| 111 |
void addNonUniform(spv::Builder& builder, spv::Id t) { builder.addDecoration(t, nonUniform); } |
| 112 |
protected: |
| 113 |
spv::Decoration noContraction; |
| 114 |
spv::Decoration nonUniform; |
| 115 |
#endif |
| 116 |
|
| 117 |
}; |
| 118 |
|
| 119 |
} // namespace |
| 120 |
|
| 121 |
// |
| 122 |
// The main holder of information for translating glslang to SPIR-V. |
| 123 |
// |
| 124 |
// Derives from the AST walking base class. |
| 125 |
// |
| 126 |
class TGlslangToSpvTraverser : public glslang::TIntermTraverser { |
| 127 |
public: |
| 128 |
TGlslangToSpvTraverser(unsigned int spvVersion, const glslang::TIntermediate*, spv::SpvBuildLogger* logger, |
| 129 |
glslang::SpvOptions& options); |
| 130 |
virtual ~TGlslangToSpvTraverser() { } |
| 131 |
|
| 132 |
bool visitAggregate(glslang::TVisit, glslang::TIntermAggregate*); |
| 133 |
bool visitBinary(glslang::TVisit, glslang::TIntermBinary*); |
| 134 |
void visitConstantUnion(glslang::TIntermConstantUnion*); |
| 135 |
bool visitSelection(glslang::TVisit, glslang::TIntermSelection*); |
| 136 |
bool visitSwitch(glslang::TVisit, glslang::TIntermSwitch*); |
| 137 |
void visitSymbol(glslang::TIntermSymbol* symbol); |
| 138 |
bool visitUnary(glslang::TVisit, glslang::TIntermUnary*); |
| 139 |
bool visitLoop(glslang::TVisit, glslang::TIntermLoop*); |
| 140 |
bool visitBranch(glslang::TVisit visit, glslang::TIntermBranch*); |
| 141 |
|
| 142 |
void finishSpv(); |
| 143 |
void dumpSpv(std::vector<unsigned int>& out); |
| 144 |
|
| 145 |
protected: |
| 146 |
TGlslangToSpvTraverser(TGlslangToSpvTraverser&); |
| 147 |
TGlslangToSpvTraverser& operator=(TGlslangToSpvTraverser&); |
| 148 |
|
| 149 |
spv::Decoration TranslateInterpolationDecoration(const glslang::TQualifier& qualifier); |
| 150 |
spv::Decoration TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier); |
| 151 |
spv::Decoration TranslateNonUniformDecoration(const glslang::TQualifier& qualifier); |
| 152 |
spv::Builder::AccessChain::CoherentFlags TranslateCoherent(const glslang::TType& type); |
| 153 |
spv::MemoryAccessMask TranslateMemoryAccess(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 154 |
spv::ImageOperandsMask TranslateImageOperands(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 155 |
spv::Scope TranslateMemoryScope(const spv::Builder::AccessChain::CoherentFlags &coherentFlags); |
| 156 |
spv::BuiltIn TranslateBuiltInDecoration(glslang::TBuiltInVariable, bool memberDeclaration); |
| 157 |
spv::ImageFormat TranslateImageFormat(const glslang::TType& type); |
| 158 |
spv::SelectionControlMask TranslateSelectionControl(const glslang::TIntermSelection&) const; |
| 159 |
spv::SelectionControlMask TranslateSwitchControl(const glslang::TIntermSwitch&) const; |
| 160 |
spv::LoopControlMask TranslateLoopControl(const glslang::TIntermLoop&, std::vector<unsigned int>& operands) const; |
| 161 |
spv::StorageClass TranslateStorageClass(const glslang::TType&); |
| 162 |
void addIndirectionIndexCapabilities(const glslang::TType& baseType, const glslang::TType& indexType); |
| 163 |
spv::Id createSpvVariable(const glslang::TIntermSymbol*, spv::Id forcedType); |
| 164 |
spv::Id getSampledType(const glslang::TSampler&); |
| 165 |
spv::Id getInvertedSwizzleType(const glslang::TIntermTyped&); |
| 166 |
spv::Id createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped&, spv::Id parentResult); |
| 167 |
void convertSwizzle(const glslang::TIntermAggregate&, std::vector<unsigned>& swizzle); |
| 168 |
spv::Id convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly = false); |
| 169 |
spv::Id convertGlslangToSpvType(const glslang::TType& type, glslang::TLayoutPacking, const glslang::TQualifier&, |
| 170 |
bool lastBufferBlockMember, bool forwardReferenceOnly = false); |
| 171 |
bool filterMember(const glslang::TType& member); |
| 172 |
spv::Id convertGlslangStructToSpvType(const glslang::TType&, const glslang::TTypeList* glslangStruct, |
| 173 |
glslang::TLayoutPacking, const glslang::TQualifier&); |
| 174 |
void decorateStructType(const glslang::TType&, const glslang::TTypeList* glslangStruct, glslang::TLayoutPacking, |
| 175 |
const glslang::TQualifier&, spv::Id); |
| 176 |
spv::Id makeArraySizeId(const glslang::TArraySizes&, int dim); |
| 177 |
spv::Id accessChainLoad(const glslang::TType& type); |
| 178 |
void accessChainStore(const glslang::TType& type, spv::Id rvalue); |
| 179 |
void multiTypeStore(const glslang::TType&, spv::Id rValue); |
| 180 |
glslang::TLayoutPacking getExplicitLayout(const glslang::TType& type) const; |
| 181 |
int getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 182 |
int getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 183 |
void updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, int& currentOffset, |
| 184 |
int& nextOffset, glslang::TLayoutPacking, glslang::TLayoutMatrix); |
| 185 |
void declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember); |
| 186 |
|
| 187 |
bool isShaderEntryPoint(const glslang::TIntermAggregate* node); |
| 188 |
bool writableParam(glslang::TStorageQualifier) const; |
| 189 |
bool originalParam(glslang::TStorageQualifier, const glslang::TType&, bool implicitThisParam); |
| 190 |
void makeFunctions(const glslang::TIntermSequence&); |
| 191 |
void makeGlobalInitializers(const glslang::TIntermSequence&); |
| 192 |
void visitFunctions(const glslang::TIntermSequence&); |
| 193 |
void handleFunctionEntry(const glslang::TIntermAggregate* node); |
| 194 |
void translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 195 |
spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
| 196 |
void translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments); |
| 197 |
spv::Id createImageTextureFunctionCall(glslang::TIntermOperator* node); |
| 198 |
spv::Id handleUserFunctionCall(const glslang::TIntermAggregate*); |
| 199 |
|
| 200 |
spv::Id createBinaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right, |
| 201 |
glslang::TBasicType typeProxy, bool reduceComparison = true); |
| 202 |
spv::Id createBinaryMatrixOperation(spv::Op, OpDecorations&, spv::Id typeId, spv::Id left, spv::Id right); |
| 203 |
spv::Id createUnaryOperation(glslang::TOperator op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 204 |
glslang::TBasicType typeProxy, |
| 205 |
const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
| 206 |
spv::Id createUnaryMatrixOperation(spv::Op op, OpDecorations&, spv::Id typeId, spv::Id operand, |
| 207 |
glslang::TBasicType typeProxy); |
| 208 |
spv::Id createConversion(glslang::TOperator op, OpDecorations&, spv::Id destTypeId, spv::Id operand, |
| 209 |
glslang::TBasicType typeProxy); |
| 210 |
spv::Id createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize); |
| 211 |
spv::Id makeSmearedConstant(spv::Id constant, int vectorSize); |
| 212 |
spv::Id createAtomicOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
| 213 |
std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 214 |
const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags); |
| 215 |
spv::Id createInvocationsOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
| 216 |
glslang::TBasicType typeProxy); |
| 217 |
spv::Id CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 218 |
spv::Id typeId, std::vector<spv::Id>& operands); |
| 219 |
spv::Id createSubgroupOperation(glslang::TOperator op, spv::Id typeId, std::vector<spv::Id>& operands, |
| 220 |
glslang::TBasicType typeProxy); |
| 221 |
spv::Id createMiscOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId, |
| 222 |
std::vector<spv::Id>& operands, glslang::TBasicType typeProxy); |
| 223 |
spv::Id createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId); |
| 224 |
spv::Id getSymbolId(const glslang::TIntermSymbol* node); |
| 225 |
void addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier & qualifier); |
| 226 |
spv::Id createSpvConstant(const glslang::TIntermTyped&); |
| 227 |
spv::Id createSpvConstantFromConstUnionArray(const glslang::TType& type, const glslang::TConstUnionArray&, |
| 228 |
int& nextConst, bool specConstant); |
| 229 |
bool isTrivialLeaf(const glslang::TIntermTyped* node); |
| 230 |
bool isTrivial(const glslang::TIntermTyped* node); |
| 231 |
spv::Id createShortCircuit(glslang::TOperator, glslang::TIntermTyped& left, glslang::TIntermTyped& right); |
| 232 |
spv::Id getExtBuiltins(const char* name); |
| 233 |
std::pair<spv::Id, spv::Id> getForcedType(glslang::TBuiltInVariable builtIn, const glslang::TType&); |
| 234 |
spv::Id translateForcedType(spv::Id object); |
| 235 |
spv::Id createCompositeConstruct(spv::Id typeId, std::vector<spv::Id> constituents); |
| 236 |
|
| 237 |
glslang::SpvOptions& options; |
| 238 |
spv::Function* shaderEntry; |
| 239 |
spv::Function* currentFunction; |
| 240 |
spv::Instruction* entryPoint; |
| 241 |
int sequenceDepth; |
| 242 |
|
| 243 |
spv::SpvBuildLogger* logger; |
| 244 |
|
| 245 |
// There is a 1:1 mapping between a spv builder and a module; this is thread safe |
| 246 |
spv::Builder builder; |
| 247 |
bool inEntryPoint; |
| 248 |
bool entryPointTerminated; |
| 249 |
bool linkageOnly; // true when visiting the set of objects in the AST present only for |
| 250 |
// establishing interface, whether or not they were statically used |
| 251 |
std::set<spv::Id> iOSet; // all input/output variables from either static use or declaration of interface |
| 252 |
const glslang::TIntermediate* glslangIntermediate; |
| 253 |
bool nanMinMaxClamp; // true if use NMin/NMax/NClamp instead of FMin/FMax/FClamp |
| 254 |
spv::Id stdBuiltins; |
| 255 |
spv::Id nonSemanticDebugPrintf; |
| 256 |
std::unordered_map<const char*, spv::Id> extBuiltinMap; |
| 257 |
|
| 258 |
std::unordered_map<int, spv::Id> symbolValues; |
| 259 |
std::unordered_set<int> rValueParameters; // set of formal function parameters passed as rValues, |
| 260 |
// rather than a pointer |
| 261 |
std::unordered_map<std::string, spv::Function*> functionMap; |
| 262 |
std::unordered_map<const glslang::TTypeList*, spv::Id> structMap[glslang::ElpCount][glslang::ElmCount]; |
| 263 |
// for mapping glslang block indices to spv indices (e.g., due to hidden members): |
| 264 |
std::unordered_map<int, std::vector<int>> memberRemapper; |
| 265 |
// for mapping glslang symbol struct to symbol Id |
| 266 |
std::unordered_map<const glslang::TTypeList*, int> glslangTypeToIdMap; |
| 267 |
std::stack<bool> breakForLoop; // false means break for switch |
| 268 |
std::unordered_map<std::string, const glslang::TIntermSymbol*> counterOriginator; |
| 269 |
// Map pointee types for EbtReference to their forward pointers |
| 270 |
std::map<const glslang::TType *, spv::Id> forwardPointers; |
| 271 |
// Type forcing, for when SPIR-V wants a different type than the AST, |
| 272 |
// requiring local translation to and from SPIR-V type on every access. |
| 273 |
// Maps <builtin-variable-id -> AST-required-type-id> |
| 274 |
std::unordered_map<spv::Id, spv::Id> forceType; |
| 275 |
}; |
| 276 |
|
| 277 |
// |
| 278 |
// Helper functions for translating glslang representations to SPIR-V enumerants. |
| 279 |
// |
| 280 |
|
| 281 |
// Translate glslang profile to SPIR-V source language. |
| 282 |
spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile) |
| 283 |
{ |
| 284 |
#ifdef GLSLANG_WEB |
| 285 |
return spv::SourceLanguageESSL; |
| 286 |
#elif defined(GLSLANG_ANGLE) |
| 287 |
return spv::SourceLanguageGLSL; |
| 288 |
#endif |
| 289 |
|
| 290 |
switch (source) { |
| 291 |
case glslang::EShSourceGlsl: |
| 292 |
switch (profile) { |
| 293 |
case ENoProfile: |
| 294 |
case ECoreProfile: |
| 295 |
case ECompatibilityProfile: |
| 296 |
return spv::SourceLanguageGLSL; |
| 297 |
case EEsProfile: |
| 298 |
return spv::SourceLanguageESSL; |
| 299 |
default: |
| 300 |
return spv::SourceLanguageUnknown; |
| 301 |
} |
| 302 |
case glslang::EShSourceHlsl: |
| 303 |
return spv::SourceLanguageHLSL; |
| 304 |
default: |
| 305 |
return spv::SourceLanguageUnknown; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
// Translate glslang language (stage) to SPIR-V execution model. |
| 310 |
spv::ExecutionModel TranslateExecutionModel(EShLanguage stage) |
| 311 |
{ |
| 312 |
switch (stage) { |
| 313 |
case EShLangVertex: return spv::ExecutionModelVertex; |
| 314 |
case EShLangFragment: return spv::ExecutionModelFragment; |
| 315 |
case EShLangCompute: return spv::ExecutionModelGLCompute; |
| 316 |
#ifndef GLSLANG_WEB |
| 317 |
case EShLangTessControl: return spv::ExecutionModelTessellationControl; |
| 318 |
case EShLangTessEvaluation: return spv::ExecutionModelTessellationEvaluation; |
| 319 |
case EShLangGeometry: return spv::ExecutionModelGeometry; |
| 320 |
case EShLangRayGen: return spv::ExecutionModelRayGenerationKHR; |
| 321 |
case EShLangIntersect: return spv::ExecutionModelIntersectionKHR; |
| 322 |
case EShLangAnyHit: return spv::ExecutionModelAnyHitKHR; |
| 323 |
case EShLangClosestHit: return spv::ExecutionModelClosestHitKHR; |
| 324 |
case EShLangMiss: return spv::ExecutionModelMissKHR; |
| 325 |
case EShLangCallable: return spv::ExecutionModelCallableKHR; |
| 326 |
case EShLangTaskNV: return spv::ExecutionModelTaskNV; |
| 327 |
case EShLangMeshNV: return spv::ExecutionModelMeshNV; |
| 328 |
#endif |
| 329 |
default: |
| 330 |
assert(0); |
| 331 |
return spv::ExecutionModelFragment; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
// Translate glslang sampler type to SPIR-V dimensionality. |
| 336 |
spv::Dim TranslateDimensionality(const glslang::TSampler& sampler) |
| 337 |
{ |
| 338 |
switch (sampler.dim) { |
| 339 |
case glslang::Esd1D: return spv::Dim1D; |
| 340 |
case glslang::Esd2D: return spv::Dim2D; |
| 341 |
case glslang::Esd3D: return spv::Dim3D; |
| 342 |
case glslang::EsdCube: return spv::DimCube; |
| 343 |
case glslang::EsdRect: return spv::DimRect; |
| 344 |
case glslang::EsdBuffer: return spv::DimBuffer; |
| 345 |
case glslang::EsdSubpass: return spv::DimSubpassData; |
| 346 |
default: |
| 347 |
assert(0); |
| 348 |
return spv::Dim2D; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
// Translate glslang precision to SPIR-V precision decorations. |
| 353 |
spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision) |
| 354 |
{ |
| 355 |
switch (glslangPrecision) { |
| 356 |
case glslang::EpqLow: return spv::DecorationRelaxedPrecision; |
| 357 |
case glslang::EpqMedium: return spv::DecorationRelaxedPrecision; |
| 358 |
default: |
| 359 |
return spv::NoPrecision; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// Translate glslang type to SPIR-V precision decorations. |
| 364 |
spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type) |
| 365 |
{ |
| 366 |
return TranslatePrecisionDecoration(type.getQualifier().precision); |
| 367 |
} |
| 368 |
|
| 369 |
// Translate glslang type to SPIR-V block decorations. |
| 370 |
spv::Decoration TranslateBlockDecoration(const glslang::TType& type, bool useStorageBuffer) |
| 371 |
{ |
| 372 |
if (type.getBasicType() == glslang::EbtBlock) { |
| 373 |
switch (type.getQualifier().storage) { |
| 374 |
case glslang::EvqUniform: return spv::DecorationBlock; |
| 375 |
case glslang::EvqBuffer: return useStorageBuffer ? spv::DecorationBlock : spv::DecorationBufferBlock; |
| 376 |
case glslang::EvqVaryingIn: return spv::DecorationBlock; |
| 377 |
case glslang::EvqVaryingOut: return spv::DecorationBlock; |
| 378 |
#ifndef GLSLANG_WEB |
| 379 |
case glslang::EvqPayload: return spv::DecorationBlock; |
| 380 |
case glslang::EvqPayloadIn: return spv::DecorationBlock; |
| 381 |
case glslang::EvqHitAttr: return spv::DecorationBlock; |
| 382 |
case glslang::EvqCallableData: return spv::DecorationBlock; |
| 383 |
case glslang::EvqCallableDataIn: return spv::DecorationBlock; |
| 384 |
#endif |
| 385 |
default: |
| 386 |
assert(0); |
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
return spv::DecorationMax; |
| 392 |
} |
| 393 |
|
| 394 |
// Translate glslang type to SPIR-V memory decorations. |
| 395 |
void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory, |
| 396 |
bool useVulkanMemoryModel) |
| 397 |
{ |
| 398 |
if (!useVulkanMemoryModel) { |
| 399 |
if (qualifier.isCoherent()) |
| 400 |
memory.push_back(spv::DecorationCoherent); |
| 401 |
if (qualifier.isVolatile()) { |
| 402 |
memory.push_back(spv::DecorationVolatile); |
| 403 |
memory.push_back(spv::DecorationCoherent); |
| 404 |
} |
| 405 |
} |
| 406 |
if (qualifier.isRestrict()) |
| 407 |
memory.push_back(spv::DecorationRestrict); |
| 408 |
if (qualifier.isReadOnly()) |
| 409 |
memory.push_back(spv::DecorationNonWritable); |
| 410 |
if (qualifier.isWriteOnly()) |
| 411 |
memory.push_back(spv::DecorationNonReadable); |
| 412 |
} |
| 413 |
|
| 414 |
// Translate glslang type to SPIR-V layout decorations. |
| 415 |
spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout) |
| 416 |
{ |
| 417 |
if (type.isMatrix()) { |
| 418 |
switch (matrixLayout) { |
| 419 |
case glslang::ElmRowMajor: |
| 420 |
return spv::DecorationRowMajor; |
| 421 |
case glslang::ElmColumnMajor: |
| 422 |
return spv::DecorationColMajor; |
| 423 |
default: |
| 424 |
// opaque layouts don't need a majorness |
| 425 |
return spv::DecorationMax; |
| 426 |
} |
| 427 |
} else { |
| 428 |
switch (type.getBasicType()) { |
| 429 |
default: |
| 430 |
return spv::DecorationMax; |
| 431 |
break; |
| 432 |
case glslang::EbtBlock: |
| 433 |
switch (type.getQualifier().storage) { |
| 434 |
case glslang::EvqUniform: |
| 435 |
case glslang::EvqBuffer: |
| 436 |
switch (type.getQualifier().layoutPacking) { |
| 437 |
case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 438 |
case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 439 |
default: |
| 440 |
return spv::DecorationMax; |
| 441 |
} |
| 442 |
case glslang::EvqVaryingIn: |
| 443 |
case glslang::EvqVaryingOut: |
| 444 |
if (type.getQualifier().isTaskMemory()) { |
| 445 |
switch (type.getQualifier().layoutPacking) { |
| 446 |
case glslang::ElpShared: return spv::DecorationGLSLShared; |
| 447 |
case glslang::ElpPacked: return spv::DecorationGLSLPacked; |
| 448 |
default: break; |
| 449 |
} |
| 450 |
} else { |
| 451 |
assert(type.getQualifier().layoutPacking == glslang::ElpNone); |
| 452 |
} |
| 453 |
return spv::DecorationMax; |
| 454 |
#ifndef GLSLANG_WEB |
| 455 |
case glslang::EvqPayload: |
| 456 |
case glslang::EvqPayloadIn: |
| 457 |
case glslang::EvqHitAttr: |
| 458 |
case glslang::EvqCallableData: |
| 459 |
case glslang::EvqCallableDataIn: |
| 460 |
return spv::DecorationMax; |
| 461 |
#endif |
| 462 |
default: |
| 463 |
assert(0); |
| 464 |
return spv::DecorationMax; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
// Translate glslang type to SPIR-V interpolation decorations. |
| 471 |
// Returns spv::DecorationMax when no decoration |
| 472 |
// should be applied. |
| 473 |
spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier) |
| 474 |
{ |
| 475 |
if (qualifier.smooth) |
| 476 |
// Smooth decoration doesn't exist in SPIR-V 1.0 |
| 477 |
return spv::DecorationMax; |
| 478 |
else if (qualifier.isNonPerspective()) |
| 479 |
return spv::DecorationNoPerspective; |
| 480 |
else if (qualifier.flat) |
| 481 |
return spv::DecorationFlat; |
| 482 |
else if (qualifier.isExplicitInterpolation()) { |
| 483 |
builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 484 |
return spv::DecorationExplicitInterpAMD; |
| 485 |
} |
| 486 |
else |
| 487 |
return spv::DecorationMax; |
| 488 |
} |
| 489 |
|
| 490 |
// Translate glslang type to SPIR-V auxiliary storage decorations. |
| 491 |
// Returns spv::DecorationMax when no decoration |
| 492 |
// should be applied. |
| 493 |
spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier) |
| 494 |
{ |
| 495 |
if (qualifier.centroid) |
| 496 |
return spv::DecorationCentroid; |
| 497 |
#ifndef GLSLANG_WEB |
| 498 |
else if (qualifier.patch) |
| 499 |
return spv::DecorationPatch; |
| 500 |
else if (qualifier.sample) { |
| 501 |
builder.addCapability(spv::CapabilitySampleRateShading); |
| 502 |
return spv::DecorationSample; |
| 503 |
} |
| 504 |
#endif |
| 505 |
|
| 506 |
return spv::DecorationMax; |
| 507 |
} |
| 508 |
|
| 509 |
// If glslang type is invariant, return SPIR-V invariant decoration. |
| 510 |
spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier) |
| 511 |
{ |
| 512 |
if (qualifier.invariant) |
| 513 |
return spv::DecorationInvariant; |
| 514 |
else |
| 515 |
return spv::DecorationMax; |
| 516 |
} |
| 517 |
|
| 518 |
// If glslang type is noContraction, return SPIR-V NoContraction decoration. |
| 519 |
spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier) |
| 520 |
{ |
| 521 |
#ifndef GLSLANG_WEB |
| 522 |
if (qualifier.isNoContraction()) |
| 523 |
return spv::DecorationNoContraction; |
| 524 |
else |
| 525 |
#endif |
| 526 |
return spv::DecorationMax; |
| 527 |
} |
| 528 |
|
| 529 |
// If glslang type is nonUniform, return SPIR-V NonUniform decoration. |
| 530 |
spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier) |
| 531 |
{ |
| 532 |
#ifndef GLSLANG_WEB |
| 533 |
if (qualifier.isNonUniform()) { |
| 534 |
builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
| 535 |
builder.addCapability(spv::CapabilityShaderNonUniformEXT); |
| 536 |
return spv::DecorationNonUniformEXT; |
| 537 |
} else |
| 538 |
#endif |
| 539 |
return spv::DecorationMax; |
| 540 |
} |
| 541 |
|
| 542 |
spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess( |
| 543 |
const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 544 |
{ |
| 545 |
spv::MemoryAccessMask mask = spv::MemoryAccessMaskNone; |
| 546 |
|
| 547 |
#ifndef GLSLANG_WEB |
| 548 |
if (!glslangIntermediate->usingVulkanMemoryModel() || coherentFlags.isImage) |
| 549 |
return mask; |
| 550 |
|
| 551 |
if (coherentFlags.isVolatile() || coherentFlags.anyCoherent()) { |
| 552 |
mask = mask | spv::MemoryAccessMakePointerAvailableKHRMask | |
| 553 |
spv::MemoryAccessMakePointerVisibleKHRMask; |
| 554 |
} |
| 555 |
|
| 556 |
if (coherentFlags.nonprivate) { |
| 557 |
mask = mask | spv::MemoryAccessNonPrivatePointerKHRMask; |
| 558 |
} |
| 559 |
if (coherentFlags.volatil) { |
| 560 |
mask = mask | spv::MemoryAccessVolatileMask; |
| 561 |
} |
| 562 |
if (mask != spv::MemoryAccessMaskNone) { |
| 563 |
builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 564 |
} |
| 565 |
#endif |
| 566 |
|
| 567 |
return mask; |
| 568 |
} |
| 569 |
|
| 570 |
spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands( |
| 571 |
const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 572 |
{ |
| 573 |
spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 574 |
|
| 575 |
#ifndef GLSLANG_WEB |
| 576 |
if (!glslangIntermediate->usingVulkanMemoryModel()) |
| 577 |
return mask; |
| 578 |
|
| 579 |
if (coherentFlags.volatil || |
| 580 |
coherentFlags.anyCoherent()) { |
| 581 |
mask = mask | spv::ImageOperandsMakeTexelAvailableKHRMask | |
| 582 |
spv::ImageOperandsMakeTexelVisibleKHRMask; |
| 583 |
} |
| 584 |
if (coherentFlags.nonprivate) { |
| 585 |
mask = mask | spv::ImageOperandsNonPrivateTexelKHRMask; |
| 586 |
} |
| 587 |
if (coherentFlags.volatil) { |
| 588 |
mask = mask | spv::ImageOperandsVolatileTexelKHRMask; |
| 589 |
} |
| 590 |
if (mask != spv::ImageOperandsMaskNone) { |
| 591 |
builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 592 |
} |
| 593 |
#endif |
| 594 |
|
| 595 |
return mask; |
| 596 |
} |
| 597 |
|
| 598 |
spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type) |
| 599 |
{ |
| 600 |
spv::Builder::AccessChain::CoherentFlags flags = {}; |
| 601 |
#ifndef GLSLANG_WEB |
| 602 |
flags.coherent = type.getQualifier().coherent; |
| 603 |
flags.devicecoherent = type.getQualifier().devicecoherent; |
| 604 |
flags.queuefamilycoherent = type.getQualifier().queuefamilycoherent; |
| 605 |
// shared variables are implicitly workgroupcoherent in GLSL. |
| 606 |
flags.workgroupcoherent = type.getQualifier().workgroupcoherent || |
| 607 |
type.getQualifier().storage == glslang::EvqShared; |
| 608 |
flags.subgroupcoherent = type.getQualifier().subgroupcoherent; |
| 609 |
flags.shadercallcoherent = type.getQualifier().shadercallcoherent; |
| 610 |
flags.volatil = type.getQualifier().volatil; |
| 611 |
// *coherent variables are implicitly nonprivate in GLSL |
| 612 |
flags.nonprivate = type.getQualifier().nonprivate || |
| 613 |
flags.anyCoherent() || |
| 614 |
flags.volatil; |
| 615 |
flags.isImage = type.getBasicType() == glslang::EbtSampler; |
| 616 |
#endif |
| 617 |
return flags; |
| 618 |
} |
| 619 |
|
| 620 |
spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope( |
| 621 |
const spv::Builder::AccessChain::CoherentFlags &coherentFlags) |
| 622 |
{ |
| 623 |
spv::Scope scope = spv::ScopeMax; |
| 624 |
|
| 625 |
#ifndef GLSLANG_WEB |
| 626 |
if (coherentFlags.volatil || coherentFlags.coherent) { |
| 627 |
// coherent defaults to Device scope in the old model, QueueFamilyKHR scope in the new model |
| 628 |
scope = glslangIntermediate->usingVulkanMemoryModel() ? spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 629 |
} else if (coherentFlags.devicecoherent) { |
| 630 |
scope = spv::ScopeDevice; |
| 631 |
} else if (coherentFlags.queuefamilycoherent) { |
| 632 |
scope = spv::ScopeQueueFamilyKHR; |
| 633 |
} else if (coherentFlags.workgroupcoherent) { |
| 634 |
scope = spv::ScopeWorkgroup; |
| 635 |
} else if (coherentFlags.subgroupcoherent) { |
| 636 |
scope = spv::ScopeSubgroup; |
| 637 |
} else if (coherentFlags.shadercallcoherent) { |
| 638 |
scope = spv::ScopeShaderCallKHR; |
| 639 |
} |
| 640 |
if (glslangIntermediate->usingVulkanMemoryModel() && scope == spv::ScopeDevice) { |
| 641 |
builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 642 |
} |
| 643 |
#endif |
| 644 |
|
| 645 |
return scope; |
| 646 |
} |
| 647 |
|
| 648 |
// Translate a glslang built-in variable to a SPIR-V built in decoration. Also generate |
| 649 |
// associated capabilities when required. For some built-in variables, a capability |
| 650 |
// is generated only when using the variable in an executable instruction, but not when |
| 651 |
// just declaring a struct member variable with it. This is true for PointSize, |
| 652 |
// ClipDistance, and CullDistance. |
| 653 |
spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn, |
| 654 |
bool memberDeclaration) |
| 655 |
{ |
| 656 |
switch (builtIn) { |
| 657 |
case glslang::EbvPointSize: |
| 658 |
#ifndef GLSLANG_WEB |
| 659 |
// Defer adding the capability until the built-in is actually used. |
| 660 |
if (! memberDeclaration) { |
| 661 |
switch (glslangIntermediate->getStage()) { |
| 662 |
case EShLangGeometry: |
| 663 |
builder.addCapability(spv::CapabilityGeometryPointSize); |
| 664 |
break; |
| 665 |
case EShLangTessControl: |
| 666 |
case EShLangTessEvaluation: |
| 667 |
builder.addCapability(spv::CapabilityTessellationPointSize); |
| 668 |
break; |
| 669 |
default: |
| 670 |
break; |
| 671 |
} |
| 672 |
} |
| 673 |
#endif |
| 674 |
return spv::BuiltInPointSize; |
| 675 |
|
| 676 |
case glslang::EbvPosition: return spv::BuiltInPosition; |
| 677 |
case glslang::EbvVertexId: return spv::BuiltInVertexId; |
| 678 |
case glslang::EbvInstanceId: return spv::BuiltInInstanceId; |
| 679 |
case glslang::EbvVertexIndex: return spv::BuiltInVertexIndex; |
| 680 |
case glslang::EbvInstanceIndex: return spv::BuiltInInstanceIndex; |
| 681 |
|
| 682 |
case glslang::EbvFragCoord: return spv::BuiltInFragCoord; |
| 683 |
case glslang::EbvPointCoord: return spv::BuiltInPointCoord; |
| 684 |
case glslang::EbvFace: return spv::BuiltInFrontFacing; |
| 685 |
case glslang::EbvFragDepth: return spv::BuiltInFragDepth; |
| 686 |
|
| 687 |
case glslang::EbvNumWorkGroups: return spv::BuiltInNumWorkgroups; |
| 688 |
case glslang::EbvWorkGroupSize: return spv::BuiltInWorkgroupSize; |
| 689 |
case glslang::EbvWorkGroupId: return spv::BuiltInWorkgroupId; |
| 690 |
case glslang::EbvLocalInvocationId: return spv::BuiltInLocalInvocationId; |
| 691 |
case glslang::EbvLocalInvocationIndex: return spv::BuiltInLocalInvocationIndex; |
| 692 |
case glslang::EbvGlobalInvocationId: return spv::BuiltInGlobalInvocationId; |
| 693 |
|
| 694 |
#ifndef GLSLANG_WEB |
| 695 |
// These *Distance capabilities logically belong here, but if the member is declared and |
| 696 |
// then never used, consumers of SPIR-V prefer the capability not be declared. |
| 697 |
// They are now generated when used, rather than here when declared. |
| 698 |
// Potentially, the specification should be more clear what the minimum |
| 699 |
// use needed is to trigger the capability. |
| 700 |
// |
| 701 |
case glslang::EbvClipDistance: |
| 702 |
if (!memberDeclaration) |
| 703 |
builder.addCapability(spv::CapabilityClipDistance); |
| 704 |
return spv::BuiltInClipDistance; |
| 705 |
|
| 706 |
case glslang::EbvCullDistance: |
| 707 |
if (!memberDeclaration) |
| 708 |
builder.addCapability(spv::CapabilityCullDistance); |
| 709 |
return spv::BuiltInCullDistance; |
| 710 |
|
| 711 |
case glslang::EbvViewportIndex: |
| 712 |
builder.addCapability(spv::CapabilityMultiViewport); |
| 713 |
if (glslangIntermediate->getStage() == EShLangVertex || |
| 714 |
glslangIntermediate->getStage() == EShLangTessControl || |
| 715 |
glslangIntermediate->getStage() == EShLangTessEvaluation) { |
| 716 |
|
| 717 |
if (builder.getSpvVersion() < spv::Spv_1_5) { |
| 718 |
builder.addIncorporatedExtension(spv::E_SPV_EXT_shader_viewport_index_layer, spv::Spv_1_5); |
| 719 |
builder.addCapability(spv::CapabilityShaderViewportIndexLayerEXT); |
| 720 |
} |
| 721 |
else |
| 722 |
builder.addCapability(spv::CapabilityShaderViewportIndex); |
| 723 |
} |
| 724 |
return spv::BuiltInViewportIndex; |
| 725 |
|
| 726 |
case glslang::EbvSampleId: |
| 727 |
builder.addCapability(spv::CapabilitySampleRateShading); |
| 728 |
return spv::BuiltInSampleId; |
| 729 |
|
| 730 |
case glslang::EbvSamplePosition: |
| 731 |
builder.addCapability(spv::CapabilitySampleRateShading); |
| 732 |
return spv::BuiltInSamplePosition; |
| 733 |
|
| 734 |
case glslang::EbvSampleMask: |
| 735 |
return spv::BuiltInSampleMask; |
| 736 |
|
| 737 |
case glslang::EbvLayer: |
| 738 |
if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 739 |
return spv::BuiltInLayer; |
| 740 |
} |
| 741 |
builder.addCapability(spv::CapabilityGeometry); |
| 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 |
} else |
| 750 |
builder.addCapability(spv::CapabilityShaderLayer); |
| 751 |
} |
| 752 |
return spv::BuiltInLayer; |
| 753 |
|
| 754 |
case glslang::EbvBaseVertex: |
| 755 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
| 756 |
builder.addCapability(spv::CapabilityDrawParameters); |
| 757 |
return spv::BuiltInBaseVertex; |
| 758 |
|
| 759 |
case glslang::EbvBaseInstance: |
| 760 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
| 761 |
builder.addCapability(spv::CapabilityDrawParameters); |
| 762 |
return spv::BuiltInBaseInstance; |
| 763 |
|
| 764 |
case glslang::EbvDrawId: |
| 765 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_shader_draw_parameters, spv::Spv_1_3); |
| 766 |
builder.addCapability(spv::CapabilityDrawParameters); |
| 767 |
return spv::BuiltInDrawIndex; |
| 768 |
|
| 769 |
case glslang::EbvPrimitiveId: |
| 770 |
if (glslangIntermediate->getStage() == EShLangFragment) |
| 771 |
builder.addCapability(spv::CapabilityGeometry); |
| 772 |
return spv::BuiltInPrimitiveId; |
| 773 |
|
| 774 |
case glslang::EbvFragStencilRef: |
| 775 |
builder.addExtension(spv::E_SPV_EXT_shader_stencil_export); |
| 776 |
builder.addCapability(spv::CapabilityStencilExportEXT); |
| 777 |
return spv::BuiltInFragStencilRefEXT; |
| 778 |
|
| 779 |
case glslang::EbvShadingRateKHR: |
| 780 |
builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
| 781 |
builder.addCapability(spv::CapabilityFragmentShadingRateKHR); |
| 782 |
return spv::BuiltInShadingRateKHR; |
| 783 |
|
| 784 |
case glslang::EbvPrimitiveShadingRateKHR: |
| 785 |
builder.addExtension(spv::E_SPV_KHR_fragment_shading_rate); |
| 786 |
builder.addCapability(spv::CapabilityFragmentShadingRateKHR); |
| 787 |
return spv::BuiltInPrimitiveShadingRateKHR; |
| 788 |
|
| 789 |
case glslang::EbvInvocationId: return spv::BuiltInInvocationId; |
| 790 |
case glslang::EbvTessLevelInner: return spv::BuiltInTessLevelInner; |
| 791 |
case glslang::EbvTessLevelOuter: return spv::BuiltInTessLevelOuter; |
| 792 |
case glslang::EbvTessCoord: return spv::BuiltInTessCoord; |
| 793 |
case glslang::EbvPatchVertices: return spv::BuiltInPatchVertices; |
| 794 |
case glslang::EbvHelperInvocation: return spv::BuiltInHelperInvocation; |
| 795 |
|
| 796 |
case glslang::EbvSubGroupSize: |
| 797 |
builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 798 |
builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 799 |
return spv::BuiltInSubgroupSize; |
| 800 |
|
| 801 |
case glslang::EbvSubGroupInvocation: |
| 802 |
builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 803 |
builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 804 |
return spv::BuiltInSubgroupLocalInvocationId; |
| 805 |
|
| 806 |
case glslang::EbvSubGroupEqMask: |
| 807 |
builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 808 |
builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 809 |
return spv::BuiltInSubgroupEqMask; |
| 810 |
|
| 811 |
case glslang::EbvSubGroupGeMask: |
| 812 |
builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 813 |
builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 814 |
return spv::BuiltInSubgroupGeMask; |
| 815 |
|
| 816 |
case glslang::EbvSubGroupGtMask: |
| 817 |
builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 818 |
builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 819 |
return spv::BuiltInSubgroupGtMask; |
| 820 |
|
| 821 |
case glslang::EbvSubGroupLeMask: |
| 822 |
builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 823 |
builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 824 |
return spv::BuiltInSubgroupLeMask; |
| 825 |
|
| 826 |
case glslang::EbvSubGroupLtMask: |
| 827 |
builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 828 |
builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 829 |
return spv::BuiltInSubgroupLtMask; |
| 830 |
|
| 831 |
case glslang::EbvNumSubgroups: |
| 832 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 833 |
return spv::BuiltInNumSubgroups; |
| 834 |
|
| 835 |
case glslang::EbvSubgroupID: |
| 836 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 837 |
return spv::BuiltInSubgroupId; |
| 838 |
|
| 839 |
case glslang::EbvSubgroupSize2: |
| 840 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 841 |
return spv::BuiltInSubgroupSize; |
| 842 |
|
| 843 |
case glslang::EbvSubgroupInvocation2: |
| 844 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 845 |
return spv::BuiltInSubgroupLocalInvocationId; |
| 846 |
|
| 847 |
case glslang::EbvSubgroupEqMask2: |
| 848 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 849 |
builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 850 |
return spv::BuiltInSubgroupEqMask; |
| 851 |
|
| 852 |
case glslang::EbvSubgroupGeMask2: |
| 853 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 854 |
builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 855 |
return spv::BuiltInSubgroupGeMask; |
| 856 |
|
| 857 |
case glslang::EbvSubgroupGtMask2: |
| 858 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 859 |
builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 860 |
return spv::BuiltInSubgroupGtMask; |
| 861 |
|
| 862 |
case glslang::EbvSubgroupLeMask2: |
| 863 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 864 |
builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 865 |
return spv::BuiltInSubgroupLeMask; |
| 866 |
|
| 867 |
case glslang::EbvSubgroupLtMask2: |
| 868 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 869 |
builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 870 |
return spv::BuiltInSubgroupLtMask; |
| 871 |
|
| 872 |
case glslang::EbvBaryCoordNoPersp: |
| 873 |
builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 874 |
return spv::BuiltInBaryCoordNoPerspAMD; |
| 875 |
|
| 876 |
case glslang::EbvBaryCoordNoPerspCentroid: |
| 877 |
builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 878 |
return spv::BuiltInBaryCoordNoPerspCentroidAMD; |
| 879 |
|
| 880 |
case glslang::EbvBaryCoordNoPerspSample: |
| 881 |
builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 882 |
return spv::BuiltInBaryCoordNoPerspSampleAMD; |
| 883 |
|
| 884 |
case glslang::EbvBaryCoordSmooth: |
| 885 |
builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 886 |
return spv::BuiltInBaryCoordSmoothAMD; |
| 887 |
|
| 888 |
case glslang::EbvBaryCoordSmoothCentroid: |
| 889 |
builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 890 |
return spv::BuiltInBaryCoordSmoothCentroidAMD; |
| 891 |
|
| 892 |
case glslang::EbvBaryCoordSmoothSample: |
| 893 |
builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 894 |
return spv::BuiltInBaryCoordSmoothSampleAMD; |
| 895 |
|
| 896 |
case glslang::EbvBaryCoordPullModel: |
| 897 |
builder.addExtension(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 898 |
return spv::BuiltInBaryCoordPullModelAMD; |
| 899 |
|
| 900 |
case glslang::EbvDeviceIndex: |
| 901 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_device_group, spv::Spv_1_3); |
| 902 |
builder.addCapability(spv::CapabilityDeviceGroup); |
| 903 |
return spv::BuiltInDeviceIndex; |
| 904 |
|
| 905 |
case glslang::EbvViewIndex: |
| 906 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_multiview, spv::Spv_1_3); |
| 907 |
builder.addCapability(spv::CapabilityMultiView); |
| 908 |
return spv::BuiltInViewIndex; |
| 909 |
|
| 910 |
case glslang::EbvFragSizeEXT: |
| 911 |
builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 912 |
builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 913 |
return spv::BuiltInFragSizeEXT; |
| 914 |
|
| 915 |
case glslang::EbvFragInvocationCountEXT: |
| 916 |
builder.addExtension(spv::E_SPV_EXT_fragment_invocation_density); |
| 917 |
builder.addCapability(spv::CapabilityFragmentDensityEXT); |
| 918 |
return spv::BuiltInFragInvocationCountEXT; |
| 919 |
|
| 920 |
case glslang::EbvViewportMaskNV: |
| 921 |
if (!memberDeclaration) { |
| 922 |
builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 923 |
builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 924 |
} |
| 925 |
return spv::BuiltInViewportMaskNV; |
| 926 |
case glslang::EbvSecondaryPositionNV: |
| 927 |
if (!memberDeclaration) { |
| 928 |
builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 929 |
builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 930 |
} |
| 931 |
return spv::BuiltInSecondaryPositionNV; |
| 932 |
case glslang::EbvSecondaryViewportMaskNV: |
| 933 |
if (!memberDeclaration) { |
| 934 |
builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 935 |
builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 936 |
} |
| 937 |
return spv::BuiltInSecondaryViewportMaskNV; |
| 938 |
case glslang::EbvPositionPerViewNV: |
| 939 |
if (!memberDeclaration) { |
| 940 |
builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 941 |
builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 942 |
} |
| 943 |
return spv::BuiltInPositionPerViewNV; |
| 944 |
case glslang::EbvViewportMaskPerViewNV: |
| 945 |
if (!memberDeclaration) { |
| 946 |
builder.addExtension(spv::E_SPV_NVX_multiview_per_view_attributes); |
| 947 |
builder.addCapability(spv::CapabilityPerViewAttributesNV); |
| 948 |
} |
| 949 |
return spv::BuiltInViewportMaskPerViewNV; |
| 950 |
case glslang::EbvFragFullyCoveredNV: |
| 951 |
builder.addExtension(spv::E_SPV_EXT_fragment_fully_covered); |
| 952 |
builder.addCapability(spv::CapabilityFragmentFullyCoveredEXT); |
| 953 |
return spv::BuiltInFullyCoveredEXT; |
| 954 |
case glslang::EbvFragmentSizeNV: |
| 955 |
builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 956 |
builder.addCapability(spv::CapabilityShadingRateNV); |
| 957 |
return spv::BuiltInFragmentSizeNV; |
| 958 |
case glslang::EbvInvocationsPerPixelNV: |
| 959 |
builder.addExtension(spv::E_SPV_NV_shading_rate); |
| 960 |
builder.addCapability(spv::CapabilityShadingRateNV); |
| 961 |
return spv::BuiltInInvocationsPerPixelNV; |
| 962 |
|
| 963 |
// ray tracing |
| 964 |
case glslang::EbvLaunchId: |
| 965 |
return spv::BuiltInLaunchIdKHR; |
| 966 |
case glslang::EbvLaunchSize: |
| 967 |
return spv::BuiltInLaunchSizeKHR; |
| 968 |
case glslang::EbvWorldRayOrigin: |
| 969 |
return spv::BuiltInWorldRayOriginKHR; |
| 970 |
case glslang::EbvWorldRayDirection: |
| 971 |
return spv::BuiltInWorldRayDirectionKHR; |
| 972 |
case glslang::EbvObjectRayOrigin: |
| 973 |
return spv::BuiltInObjectRayOriginKHR; |
| 974 |
case glslang::EbvObjectRayDirection: |
| 975 |
return spv::BuiltInObjectRayDirectionKHR; |
| 976 |
case glslang::EbvRayTmin: |
| 977 |
return spv::BuiltInRayTminKHR; |
| 978 |
case glslang::EbvRayTmax: |
| 979 |
return spv::BuiltInRayTmaxKHR; |
| 980 |
case glslang::EbvInstanceCustomIndex: |
| 981 |
return spv::BuiltInInstanceCustomIndexKHR; |
| 982 |
case glslang::EbvHitT: |
| 983 |
return spv::BuiltInHitTKHR; |
| 984 |
case glslang::EbvHitKind: |
| 985 |
return spv::BuiltInHitKindKHR; |
| 986 |
case glslang::EbvObjectToWorld: |
| 987 |
case glslang::EbvObjectToWorld3x4: |
| 988 |
return spv::BuiltInObjectToWorldKHR; |
| 989 |
case glslang::EbvWorldToObject: |
| 990 |
case glslang::EbvWorldToObject3x4: |
| 991 |
return spv::BuiltInWorldToObjectKHR; |
| 992 |
case glslang::EbvIncomingRayFlags: |
| 993 |
return spv::BuiltInIncomingRayFlagsKHR; |
| 994 |
case glslang::EbvGeometryIndex: |
| 995 |
return spv::BuiltInRayGeometryIndexKHR; |
| 996 |
|
| 997 |
// barycentrics |
| 998 |
case glslang::EbvBaryCoordNV: |
| 999 |
builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 1000 |
builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 1001 |
return spv::BuiltInBaryCoordNV; |
| 1002 |
case glslang::EbvBaryCoordNoPerspNV: |
| 1003 |
builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 1004 |
builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 1005 |
return spv::BuiltInBaryCoordNoPerspNV; |
| 1006 |
|
| 1007 |
// mesh shaders |
| 1008 |
case glslang::EbvTaskCountNV: |
| 1009 |
return spv::BuiltInTaskCountNV; |
| 1010 |
case glslang::EbvPrimitiveCountNV: |
| 1011 |
return spv::BuiltInPrimitiveCountNV; |
| 1012 |
case glslang::EbvPrimitiveIndicesNV: |
| 1013 |
return spv::BuiltInPrimitiveIndicesNV; |
| 1014 |
case glslang::EbvClipDistancePerViewNV: |
| 1015 |
return spv::BuiltInClipDistancePerViewNV; |
| 1016 |
case glslang::EbvCullDistancePerViewNV: |
| 1017 |
return spv::BuiltInCullDistancePerViewNV; |
| 1018 |
case glslang::EbvLayerPerViewNV: |
| 1019 |
return spv::BuiltInLayerPerViewNV; |
| 1020 |
case glslang::EbvMeshViewCountNV: |
| 1021 |
return spv::BuiltInMeshViewCountNV; |
| 1022 |
case glslang::EbvMeshViewIndicesNV: |
| 1023 |
return spv::BuiltInMeshViewIndicesNV; |
| 1024 |
|
| 1025 |
// sm builtins |
| 1026 |
case glslang::EbvWarpsPerSM: |
| 1027 |
builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1028 |
builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1029 |
return spv::BuiltInWarpsPerSMNV; |
| 1030 |
case glslang::EbvSMCount: |
| 1031 |
builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1032 |
builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1033 |
return spv::BuiltInSMCountNV; |
| 1034 |
case glslang::EbvWarpID: |
| 1035 |
builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1036 |
builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1037 |
return spv::BuiltInWarpIDNV; |
| 1038 |
case glslang::EbvSMID: |
| 1039 |
builder.addExtension(spv::E_SPV_NV_shader_sm_builtins); |
| 1040 |
builder.addCapability(spv::CapabilityShaderSMBuiltinsNV); |
| 1041 |
return spv::BuiltInSMIDNV; |
| 1042 |
#endif |
| 1043 |
|
| 1044 |
default: |
| 1045 |
return spv::BuiltInMax; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
// Translate glslang image layout format to SPIR-V image format. |
| 1050 |
spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type) |
| 1051 |
{ |
| 1052 |
assert(type.getBasicType() == glslang::EbtSampler); |
| 1053 |
|
| 1054 |
#ifdef GLSLANG_WEB |
| 1055 |
return spv::ImageFormatUnknown; |
| 1056 |
#endif |
| 1057 |
|
| 1058 |
// Check for capabilities |
| 1059 |
switch (type.getQualifier().getFormat()) { |
| 1060 |
case glslang::ElfRg32f: |
| 1061 |
case glslang::ElfRg16f: |
| 1062 |
case glslang::ElfR11fG11fB10f: |
| 1063 |
case glslang::ElfR16f: |
| 1064 |
case glslang::ElfRgba16: |
| 1065 |
case glslang::ElfRgb10A2: |
| 1066 |
case glslang::ElfRg16: |
| 1067 |
case glslang::ElfRg8: |
| 1068 |
case glslang::ElfR16: |
| 1069 |
case glslang::ElfR8: |
| 1070 |
case glslang::ElfRgba16Snorm: |
| 1071 |
case glslang::ElfRg16Snorm: |
| 1072 |
case glslang::ElfRg8Snorm: |
| 1073 |
case glslang::ElfR16Snorm: |
| 1074 |
case glslang::ElfR8Snorm: |
| 1075 |
|
| 1076 |
case glslang::ElfRg32i: |
| 1077 |
case glslang::ElfRg16i: |
| 1078 |
case glslang::ElfRg8i: |
| 1079 |
case glslang::ElfR16i: |
| 1080 |
case glslang::ElfR8i: |
| 1081 |
|
| 1082 |
case glslang::ElfRgb10a2ui: |
| 1083 |
case glslang::ElfRg32ui: |
| 1084 |
case glslang::ElfRg16ui: |
| 1085 |
case glslang::ElfRg8ui: |
| 1086 |
case glslang::ElfR16ui: |
| 1087 |
case glslang::ElfR8ui: |
| 1088 |
builder.addCapability(spv::CapabilityStorageImageExtendedFormats); |
| 1089 |
break; |
| 1090 |
|
| 1091 |
case glslang::ElfR64ui: |
| 1092 |
case glslang::ElfR64i: |
| 1093 |
builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 1094 |
builder.addCapability(spv::CapabilityInt64ImageEXT); |
| 1095 |
default: |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
|
| 1099 |
// do the translation |
| 1100 |
switch (type.getQualifier().getFormat()) { |
| 1101 |
case glslang::ElfNone: return spv::ImageFormatUnknown; |
| 1102 |
case glslang::ElfRgba32f: return spv::ImageFormatRgba32f; |
| 1103 |
case glslang::ElfRgba16f: return spv::ImageFormatRgba16f; |
| 1104 |
case glslang::ElfR32f: return spv::ImageFormatR32f; |
| 1105 |
case glslang::ElfRgba8: return spv::ImageFormatRgba8; |
| 1106 |
case glslang::ElfRgba8Snorm: return spv::ImageFormatRgba8Snorm; |
| 1107 |
case glslang::ElfRg32f: return spv::ImageFormatRg32f; |
| 1108 |
case glslang::ElfRg16f: return spv::ImageFormatRg16f; |
| 1109 |
case glslang::ElfR11fG11fB10f: return spv::ImageFormatR11fG11fB10f; |
| 1110 |
case glslang::ElfR16f: return spv::ImageFormatR16f; |
| 1111 |
case glslang::ElfRgba16: return spv::ImageFormatRgba16; |
| 1112 |
case glslang::ElfRgb10A2: return spv::ImageFormatRgb10A2; |
| 1113 |
case glslang::ElfRg16: return spv::ImageFormatRg16; |
| 1114 |
case glslang::ElfRg8: return spv::ImageFormatRg8; |
| 1115 |
case glslang::ElfR16: return spv::ImageFormatR16; |
| 1116 |
case glslang::ElfR8: return spv::ImageFormatR8; |
| 1117 |
case glslang::ElfRgba16Snorm: return spv::ImageFormatRgba16Snorm; |
| 1118 |
case glslang::ElfRg16Snorm: return spv::ImageFormatRg16Snorm; |
| 1119 |
case glslang::ElfRg8Snorm: return spv::ImageFormatRg8Snorm; |
| 1120 |
case glslang::ElfR16Snorm: return spv::ImageFormatR16Snorm; |
| 1121 |
case glslang::ElfR8Snorm: return spv::ImageFormatR8Snorm; |
| 1122 |
case glslang::ElfRgba32i: return spv::ImageFormatRgba32i; |
| 1123 |
case glslang::ElfRgba16i: return spv::ImageFormatRgba16i; |
| 1124 |
case glslang::ElfRgba8i: return spv::ImageFormatRgba8i; |
| 1125 |
case glslang::ElfR32i: return spv::ImageFormatR32i; |
| 1126 |
case glslang::ElfRg32i: return spv::ImageFormatRg32i; |
| 1127 |
case glslang::ElfRg16i: return spv::ImageFormatRg16i; |
| 1128 |
case glslang::ElfRg8i: return spv::ImageFormatRg8i; |
| 1129 |
case glslang::ElfR16i: return spv::ImageFormatR16i; |
| 1130 |
case glslang::ElfR8i: return spv::ImageFormatR8i; |
| 1131 |
case glslang::ElfRgba32ui: return spv::ImageFormatRgba32ui; |
| 1132 |
case glslang::ElfRgba16ui: return spv::ImageFormatRgba16ui; |
| 1133 |
case glslang::ElfRgba8ui: return spv::ImageFormatRgba8ui; |
| 1134 |
case glslang::ElfR32ui: return spv::ImageFormatR32ui; |
| 1135 |
case glslang::ElfRg32ui: return spv::ImageFormatRg32ui; |
| 1136 |
case glslang::ElfRg16ui: return spv::ImageFormatRg16ui; |
| 1137 |
case glslang::ElfRgb10a2ui: return spv::ImageFormatRgb10a2ui; |
| 1138 |
case glslang::ElfRg8ui: return spv::ImageFormatRg8ui; |
| 1139 |
case glslang::ElfR16ui: return spv::ImageFormatR16ui; |
| 1140 |
case glslang::ElfR8ui: return spv::ImageFormatR8ui; |
| 1141 |
case glslang::ElfR64ui: return spv::ImageFormatR64ui; |
| 1142 |
case glslang::ElfR64i: return spv::ImageFormatR64i; |
| 1143 |
default: return spv::ImageFormatMax; |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl( |
| 1148 |
const glslang::TIntermSelection& selectionNode) const |
| 1149 |
{ |
| 1150 |
if (selectionNode.getFlatten()) |
| 1151 |
return spv::SelectionControlFlattenMask; |
| 1152 |
if (selectionNode.getDontFlatten()) |
| 1153 |
return spv::SelectionControlDontFlattenMask; |
| 1154 |
return spv::SelectionControlMaskNone; |
| 1155 |
} |
| 1156 |
|
| 1157 |
spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode) |
| 1158 |
const |
| 1159 |
{ |
| 1160 |
if (switchNode.getFlatten()) |
| 1161 |
return spv::SelectionControlFlattenMask; |
| 1162 |
if (switchNode.getDontFlatten()) |
| 1163 |
return spv::SelectionControlDontFlattenMask; |
| 1164 |
return spv::SelectionControlMaskNone; |
| 1165 |
} |
| 1166 |
|
| 1167 |
// return a non-0 dependency if the dependency argument must be set |
| 1168 |
spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode, |
| 1169 |
std::vector<unsigned int>& operands) const |
| 1170 |
{ |
| 1171 |
spv::LoopControlMask control = spv::LoopControlMaskNone; |
| 1172 |
|
| 1173 |
if (loopNode.getDontUnroll()) |
| 1174 |
control = control | spv::LoopControlDontUnrollMask; |
| 1175 |
if (loopNode.getUnroll()) |
| 1176 |
control = control | spv::LoopControlUnrollMask; |
| 1177 |
if (unsigned(loopNode.getLoopDependency()) == glslang::TIntermLoop::dependencyInfinite) |
| 1178 |
control = control | spv::LoopControlDependencyInfiniteMask; |
| 1179 |
else if (loopNode.getLoopDependency() > 0) { |
| 1180 |
control = control | spv::LoopControlDependencyLengthMask; |
| 1181 |
operands.push_back((unsigned int)loopNode.getLoopDependency()); |
| 1182 |
} |
| 1183 |
if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 1184 |
if (loopNode.getMinIterations() > 0) { |
| 1185 |
control = control | spv::LoopControlMinIterationsMask; |
| 1186 |
operands.push_back(loopNode.getMinIterations()); |
| 1187 |
} |
| 1188 |
if (loopNode.getMaxIterations() < glslang::TIntermLoop::iterationsInfinite) { |
| 1189 |
control = control | spv::LoopControlMaxIterationsMask; |
| 1190 |
operands.push_back(loopNode.getMaxIterations()); |
| 1191 |
} |
| 1192 |
if (loopNode.getIterationMultiple() > 1) { |
| 1193 |
control = control | spv::LoopControlIterationMultipleMask; |
| 1194 |
operands.push_back(loopNode.getIterationMultiple()); |
| 1195 |
} |
| 1196 |
if (loopNode.getPeelCount() > 0) { |
| 1197 |
control = control | spv::LoopControlPeelCountMask; |
| 1198 |
operands.push_back(loopNode.getPeelCount()); |
| 1199 |
} |
| 1200 |
if (loopNode.getPartialCount() > 0) { |
| 1201 |
control = control | spv::LoopControlPartialCountMask; |
| 1202 |
operands.push_back(loopNode.getPartialCount()); |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
return control; |
| 1207 |
} |
| 1208 |
|
| 1209 |
// Translate glslang type to SPIR-V storage class. |
| 1210 |
spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type) |
| 1211 |
{ |
| 1212 |
if (type.getBasicType() == glslang::EbtRayQuery) |
| 1213 |
return spv::StorageClassFunction; |
| 1214 |
if (type.getQualifier().isPipeInput()) |
| 1215 |
return spv::StorageClassInput; |
| 1216 |
if (type.getQualifier().isPipeOutput()) |
| 1217 |
return spv::StorageClassOutput; |
| 1218 |
|
| 1219 |
if (glslangIntermediate->getSource() != glslang::EShSourceHlsl || |
| 1220 |
type.getQualifier().storage == glslang::EvqUniform) { |
| 1221 |
if (type.isAtomic()) |
| 1222 |
return spv::StorageClassAtomicCounter; |
| 1223 |
if (type.containsOpaque()) |
| 1224 |
return spv::StorageClassUniformConstant; |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (type.getQualifier().isUniformOrBuffer() && |
| 1228 |
type.getQualifier().isShaderRecord()) { |
| 1229 |
return spv::StorageClassShaderRecordBufferKHR; |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (glslangIntermediate->usingStorageBuffer() && type.getQualifier().storage == glslang::EvqBuffer) { |
| 1233 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_storage_buffer_storage_class, spv::Spv_1_3); |
| 1234 |
return spv::StorageClassStorageBuffer; |
| 1235 |
} |
| 1236 |
|
| 1237 |
if (type.getQualifier().isUniformOrBuffer()) { |
| 1238 |
if (type.getQualifier().isPushConstant()) |
| 1239 |
return spv::StorageClassPushConstant; |
| 1240 |
if (type.getBasicType() == glslang::EbtBlock) |
| 1241 |
return spv::StorageClassUniform; |
| 1242 |
return spv::StorageClassUniformConstant; |
| 1243 |
} |
| 1244 |
|
| 1245 |
switch (type.getQualifier().storage) { |
| 1246 |
case glslang::EvqGlobal: return spv::StorageClassPrivate; |
| 1247 |
case glslang::EvqConstReadOnly: return spv::StorageClassFunction; |
| 1248 |
case glslang::EvqTemporary: return spv::StorageClassFunction; |
| 1249 |
case glslang::EvqShared: return spv::StorageClassWorkgroup; |
| 1250 |
#ifndef GLSLANG_WEB |
| 1251 |
case glslang::EvqPayload: return spv::StorageClassRayPayloadKHR; |
| 1252 |
case glslang::EvqPayloadIn: return spv::StorageClassIncomingRayPayloadKHR; |
| 1253 |
case glslang::EvqHitAttr: return spv::StorageClassHitAttributeKHR; |
| 1254 |
case glslang::EvqCallableData: return spv::StorageClassCallableDataKHR; |
| 1255 |
case glslang::EvqCallableDataIn: return spv::StorageClassIncomingCallableDataKHR; |
| 1256 |
#endif |
| 1257 |
default: |
| 1258 |
assert(0); |
| 1259 |
break; |
| 1260 |
} |
| 1261 |
|
| 1262 |
return spv::StorageClassFunction; |
| 1263 |
} |
| 1264 |
|
| 1265 |
// Add capabilities pertaining to how an array is indexed. |
| 1266 |
void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType, |
| 1267 |
const glslang::TType& indexType) |
| 1268 |
{ |
| 1269 |
#ifndef GLSLANG_WEB |
| 1270 |
if (indexType.getQualifier().isNonUniform()) { |
| 1271 |
// deal with an asserted non-uniform index |
| 1272 |
// SPV_EXT_descriptor_indexing already added in TranslateNonUniformDecoration |
| 1273 |
if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1274 |
if (baseType.getQualifier().hasAttachment()) |
| 1275 |
builder.addCapability(spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT); |
| 1276 |
else if (baseType.isImage() && baseType.getSampler().isBuffer()) |
| 1277 |
builder.addCapability(spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT); |
| 1278 |
else if (baseType.isTexture() && baseType.getSampler().isBuffer()) |
| 1279 |
builder.addCapability(spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT); |
| 1280 |
else if (baseType.isImage()) |
| 1281 |
builder.addCapability(spv::CapabilityStorageImageArrayNonUniformIndexingEXT); |
| 1282 |
else if (baseType.isTexture()) |
| 1283 |
builder.addCapability(spv::CapabilitySampledImageArrayNonUniformIndexingEXT); |
| 1284 |
} else if (baseType.getBasicType() == glslang::EbtBlock) { |
| 1285 |
if (baseType.getQualifier().storage == glslang::EvqBuffer) |
| 1286 |
builder.addCapability(spv::CapabilityStorageBufferArrayNonUniformIndexingEXT); |
| 1287 |
else if (baseType.getQualifier().storage == glslang::EvqUniform) |
| 1288 |
builder.addCapability(spv::CapabilityUniformBufferArrayNonUniformIndexingEXT); |
| 1289 |
} |
| 1290 |
} else { |
| 1291 |
// assume a dynamically uniform index |
| 1292 |
if (baseType.getBasicType() == glslang::EbtSampler) { |
| 1293 |
if (baseType.getQualifier().hasAttachment()) { |
| 1294 |
builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
| 1295 |
builder.addCapability(spv::CapabilityInputAttachmentArrayDynamicIndexingEXT); |
| 1296 |
} else if (baseType.isImage() && baseType.getSampler().isBuffer()) { |
| 1297 |
builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
| 1298 |
builder.addCapability(spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT); |
| 1299 |
} else if (baseType.isTexture() && baseType.getSampler().isBuffer()) { |
| 1300 |
builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
| 1301 |
builder.addCapability(spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT); |
| 1302 |
} |
| 1303 |
} |
| 1304 |
} |
| 1305 |
#endif |
| 1306 |
} |
| 1307 |
|
| 1308 |
// Return whether or not the given type is something that should be tied to a |
| 1309 |
// descriptor set. |
| 1310 |
bool IsDescriptorResource(const glslang::TType& type) |
| 1311 |
{ |
| 1312 |
// uniform and buffer blocks are included, unless it is a push_constant |
| 1313 |
if (type.getBasicType() == glslang::EbtBlock) |
| 1314 |
return type.getQualifier().isUniformOrBuffer() && |
| 1315 |
! type.getQualifier().isShaderRecord() && |
| 1316 |
! type.getQualifier().isPushConstant(); |
| 1317 |
|
| 1318 |
// non block... |
| 1319 |
// basically samplerXXX/subpass/sampler/texture are all included |
| 1320 |
// if they are the global-scope-class, not the function parameter |
| 1321 |
// (or local, if they ever exist) class. |
| 1322 |
if (type.getBasicType() == glslang::EbtSampler || |
| 1323 |
type.getBasicType() == glslang::EbtAccStruct) |
| 1324 |
return type.getQualifier().isUniformOrBuffer(); |
| 1325 |
|
| 1326 |
// None of the above. |
| 1327 |
return false; |
| 1328 |
} |
| 1329 |
|
| 1330 |
void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent) |
| 1331 |
{ |
| 1332 |
if (child.layoutMatrix == glslang::ElmNone) |
| 1333 |
child.layoutMatrix = parent.layoutMatrix; |
| 1334 |
|
| 1335 |
if (parent.invariant) |
| 1336 |
child.invariant = true; |
| 1337 |
if (parent.flat) |
| 1338 |
child.flat = true; |
| 1339 |
if (parent.centroid) |
| 1340 |
child.centroid = true; |
| 1341 |
#ifndef GLSLANG_WEB |
| 1342 |
if (parent.nopersp) |
| 1343 |
child.nopersp = true; |
| 1344 |
if (parent.explicitInterp) |
| 1345 |
child.explicitInterp = true; |
| 1346 |
if (parent.perPrimitiveNV) |
| 1347 |
child.perPrimitiveNV = true; |
| 1348 |
if (parent.perViewNV) |
| 1349 |
child.perViewNV = true; |
| 1350 |
if (parent.perTaskNV) |
| 1351 |
child.perTaskNV = true; |
| 1352 |
if (parent.patch) |
| 1353 |
child.patch = true; |
| 1354 |
if (parent.sample) |
| 1355 |
child.sample = true; |
| 1356 |
if (parent.coherent) |
| 1357 |
child.coherent = true; |
| 1358 |
if (parent.devicecoherent) |
| 1359 |
child.devicecoherent = true; |
| 1360 |
if (parent.queuefamilycoherent) |
| 1361 |
child.queuefamilycoherent = true; |
| 1362 |
if (parent.workgroupcoherent) |
| 1363 |
child.workgroupcoherent = true; |
| 1364 |
if (parent.subgroupcoherent) |
| 1365 |
child.subgroupcoherent = true; |
| 1366 |
if (parent.shadercallcoherent) |
| 1367 |
child.shadercallcoherent = true; |
| 1368 |
if (parent.nonprivate) |
| 1369 |
child.nonprivate = true; |
| 1370 |
if (parent.volatil) |
| 1371 |
child.volatil = true; |
| 1372 |
if (parent.restrict) |
| 1373 |
child.restrict = true; |
| 1374 |
if (parent.readonly) |
| 1375 |
child.readonly = true; |
| 1376 |
if (parent.writeonly) |
| 1377 |
child.writeonly = true; |
| 1378 |
#endif |
| 1379 |
} |
| 1380 |
|
| 1381 |
bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier) |
| 1382 |
{ |
| 1383 |
// This should list qualifiers that simultaneous satisfy: |
| 1384 |
// - struct members might inherit from a struct declaration |
| 1385 |
// (note that non-block structs don't explicitly inherit, |
| 1386 |
// only implicitly, meaning no decoration involved) |
| 1387 |
// - affect decorations on the struct members |
| 1388 |
// (note smooth does not, and expecting something like volatile |
| 1389 |
// to effect the whole object) |
| 1390 |
// - are not part of the offset/st430/etc or row/column-major layout |
| 1391 |
return qualifier.invariant || (qualifier.hasLocation() && type.getBasicType() == glslang::EbtBlock); |
| 1392 |
} |
| 1393 |
|
| 1394 |
// |
| 1395 |
// Implement the TGlslangToSpvTraverser class. |
| 1396 |
// |
| 1397 |
|
| 1398 |
TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion, |
| 1399 |
const glslang::TIntermediate* glslangIntermediate, |
| 1400 |
spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) : |
| 1401 |
TIntermTraverser(true, false, true), |
| 1402 |
options(options), |
| 1403 |
shaderEntry(nullptr), currentFunction(nullptr), |
| 1404 |
sequenceDepth(0), logger(buildLogger), |
| 1405 |
builder(spvVersion, (glslang::GetKhronosToolId() << 16) | glslang::GetSpirvGeneratorVersion(), logger), |
| 1406 |
inEntryPoint(false), entryPointTerminated(false), linkageOnly(false), |
| 1407 |
glslangIntermediate(glslangIntermediate), |
| 1408 |
nanMinMaxClamp(glslangIntermediate->getNanMinMaxClamp()), |
| 1409 |
nonSemanticDebugPrintf(0) |
| 1410 |
{ |
| 1411 |
spv::ExecutionModel executionModel = TranslateExecutionModel(glslangIntermediate->getStage()); |
| 1412 |
|
| 1413 |
builder.clearAccessChain(); |
| 1414 |
builder.setSource(TranslateSourceLanguage(glslangIntermediate->getSource(), glslangIntermediate->getProfile()), |
| 1415 |
glslangIntermediate->getVersion()); |
| 1416 |
|
| 1417 |
if (options.generateDebugInfo) { |
| 1418 |
builder.setEmitOpLines(); |
| 1419 |
builder.setSourceFile(glslangIntermediate->getSourceFile()); |
| 1420 |
|
| 1421 |
// Set the source shader's text. If for SPV version 1.0, include |
| 1422 |
// a preamble in comments stating the OpModuleProcessed instructions. |
| 1423 |
// Otherwise, emit those as actual instructions. |
| 1424 |
std::string text; |
| 1425 |
const std::vector<std::string>& processes = glslangIntermediate->getProcesses(); |
| 1426 |
for (int p = 0; p < (int)processes.size(); ++p) { |
| 1427 |
if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1) { |
| 1428 |
text.append("// OpModuleProcessed "); |
| 1429 |
text.append(processes[p]); |
| 1430 |
text.append("\n"); |
| 1431 |
} else |
| 1432 |
builder.addModuleProcessed(processes[p]); |
| 1433 |
} |
| 1434 |
if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_1 && (int)processes.size() > 0) |
| 1435 |
text.append("#line 1\n"); |
| 1436 |
text.append(glslangIntermediate->getSourceText()); |
| 1437 |
builder.setSourceText(text); |
| 1438 |
// Pass name and text for all included files |
| 1439 |
const std::map<std::string, std::string>& include_txt = glslangIntermediate->getIncludeText(); |
| 1440 |
for (auto iItr = include_txt.begin(); iItr != include_txt.end(); ++iItr) |
| 1441 |
builder.addInclude(iItr->first, iItr->second); |
| 1442 |
} |
| 1443 |
stdBuiltins = builder.import("GLSL.std.450"); |
| 1444 |
|
| 1445 |
spv::AddressingModel addressingModel = spv::AddressingModelLogical; |
| 1446 |
spv::MemoryModel memoryModel = spv::MemoryModelGLSL450; |
| 1447 |
|
| 1448 |
if (glslangIntermediate->usingPhysicalStorageBuffer()) { |
| 1449 |
addressingModel = spv::AddressingModelPhysicalStorageBuffer64EXT; |
| 1450 |
builder.addIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, spv::Spv_1_5); |
| 1451 |
builder.addCapability(spv::CapabilityPhysicalStorageBufferAddressesEXT); |
| 1452 |
} |
| 1453 |
if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 1454 |
memoryModel = spv::MemoryModelVulkanKHR; |
| 1455 |
builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 1456 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5); |
| 1457 |
} |
| 1458 |
builder.setMemoryModel(addressingModel, memoryModel); |
| 1459 |
|
| 1460 |
if (glslangIntermediate->usingVariablePointers()) { |
| 1461 |
builder.addCapability(spv::CapabilityVariablePointers); |
| 1462 |
} |
| 1463 |
|
| 1464 |
shaderEntry = builder.makeEntryPoint(glslangIntermediate->getEntryPointName().c_str()); |
| 1465 |
entryPoint = builder.addEntryPoint(executionModel, shaderEntry, glslangIntermediate->getEntryPointName().c_str()); |
| 1466 |
|
| 1467 |
// Add the source extensions |
| 1468 |
const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 1469 |
for (auto it = sourceExtensions.begin(); it != sourceExtensions.end(); ++it) |
| 1470 |
builder.addSourceExtension(it->c_str()); |
| 1471 |
|
| 1472 |
// Add the top-level modes for this shader. |
| 1473 |
|
| 1474 |
if (glslangIntermediate->getXfbMode()) { |
| 1475 |
builder.addCapability(spv::CapabilityTransformFeedback); |
| 1476 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeXfb); |
| 1477 |
} |
| 1478 |
|
| 1479 |
if (glslangIntermediate->getLayoutPrimitiveCulling()) { |
| 1480 |
builder.addCapability(spv::CapabilityRayTraversalPrimitiveCullingProvisionalKHR); |
| 1481 |
} |
| 1482 |
|
| 1483 |
unsigned int mode; |
| 1484 |
switch (glslangIntermediate->getStage()) { |
| 1485 |
case EShLangVertex: |
| 1486 |
builder.addCapability(spv::CapabilityShader); |
| 1487 |
break; |
| 1488 |
|
| 1489 |
case EShLangFragment: |
| 1490 |
builder.addCapability(spv::CapabilityShader); |
| 1491 |
if (glslangIntermediate->getPixelCenterInteger()) |
| 1492 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModePixelCenterInteger); |
| 1493 |
|
| 1494 |
if (glslangIntermediate->getOriginUpperLeft()) |
| 1495 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginUpperLeft); |
| 1496 |
else |
| 1497 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeOriginLowerLeft); |
| 1498 |
|
| 1499 |
if (glslangIntermediate->getEarlyFragmentTests()) |
| 1500 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeEarlyFragmentTests); |
| 1501 |
|
| 1502 |
if (glslangIntermediate->getPostDepthCoverage()) { |
| 1503 |
builder.addCapability(spv::CapabilitySampleMaskPostDepthCoverage); |
| 1504 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModePostDepthCoverage); |
| 1505 |
builder.addExtension(spv::E_SPV_KHR_post_depth_coverage); |
| 1506 |
} |
| 1507 |
|
| 1508 |
if (glslangIntermediate->getDepth() != glslang::EldUnchanged && glslangIntermediate->isDepthReplacing()) |
| 1509 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeDepthReplacing); |
| 1510 |
|
| 1511 |
#ifndef GLSLANG_WEB |
| 1512 |
|
| 1513 |
switch(glslangIntermediate->getDepth()) { |
| 1514 |
case glslang::EldGreater: mode = spv::ExecutionModeDepthGreater; break; |
| 1515 |
case glslang::EldLess: mode = spv::ExecutionModeDepthLess; break; |
| 1516 |
default: mode = spv::ExecutionModeMax; break; |
| 1517 |
} |
| 1518 |
if (mode != spv::ExecutionModeMax) |
| 1519 |
builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1520 |
switch (glslangIntermediate->getInterlockOrdering()) { |
| 1521 |
case glslang::EioPixelInterlockOrdered: mode = spv::ExecutionModePixelInterlockOrderedEXT; |
| 1522 |
break; |
| 1523 |
case glslang::EioPixelInterlockUnordered: mode = spv::ExecutionModePixelInterlockUnorderedEXT; |
| 1524 |
break; |
| 1525 |
case glslang::EioSampleInterlockOrdered: mode = spv::ExecutionModeSampleInterlockOrderedEXT; |
| 1526 |
break; |
| 1527 |
case glslang::EioSampleInterlockUnordered: mode = spv::ExecutionModeSampleInterlockUnorderedEXT; |
| 1528 |
break; |
| 1529 |
case glslang::EioShadingRateInterlockOrdered: mode = spv::ExecutionModeShadingRateInterlockOrderedEXT; |
| 1530 |
break; |
| 1531 |
case glslang::EioShadingRateInterlockUnordered: mode = spv::ExecutionModeShadingRateInterlockUnorderedEXT; |
| 1532 |
break; |
| 1533 |
default: mode = spv::ExecutionModeMax; |
| 1534 |
break; |
| 1535 |
} |
| 1536 |
if (mode != spv::ExecutionModeMax) { |
| 1537 |
builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1538 |
if (mode == spv::ExecutionModeShadingRateInterlockOrderedEXT || |
| 1539 |
mode == spv::ExecutionModeShadingRateInterlockUnorderedEXT) { |
| 1540 |
builder.addCapability(spv::CapabilityFragmentShaderShadingRateInterlockEXT); |
| 1541 |
} else if (mode == spv::ExecutionModePixelInterlockOrderedEXT || |
| 1542 |
mode == spv::ExecutionModePixelInterlockUnorderedEXT) { |
| 1543 |
builder.addCapability(spv::CapabilityFragmentShaderPixelInterlockEXT); |
| 1544 |
} else { |
| 1545 |
builder.addCapability(spv::CapabilityFragmentShaderSampleInterlockEXT); |
| 1546 |
} |
| 1547 |
builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 1548 |
} |
| 1549 |
#endif |
| 1550 |
break; |
| 1551 |
|
| 1552 |
case EShLangCompute: |
| 1553 |
builder.addCapability(spv::CapabilityShader); |
| 1554 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1555 |
glslangIntermediate->getLocalSize(1), |
| 1556 |
glslangIntermediate->getLocalSize(2)); |
| 1557 |
if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupQuads) { |
| 1558 |
builder.addCapability(spv::CapabilityComputeDerivativeGroupQuadsNV); |
| 1559 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupQuadsNV); |
| 1560 |
builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1561 |
} else if (glslangIntermediate->getLayoutDerivativeModeNone() == glslang::LayoutDerivativeGroupLinear) { |
| 1562 |
builder.addCapability(spv::CapabilityComputeDerivativeGroupLinearNV); |
| 1563 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeDerivativeGroupLinearNV); |
| 1564 |
builder.addExtension(spv::E_SPV_NV_compute_shader_derivatives); |
| 1565 |
} |
| 1566 |
break; |
| 1567 |
#ifndef GLSLANG_WEB |
| 1568 |
case EShLangTessEvaluation: |
| 1569 |
case EShLangTessControl: |
| 1570 |
builder.addCapability(spv::CapabilityTessellation); |
| 1571 |
|
| 1572 |
glslang::TLayoutGeometry primitive; |
| 1573 |
|
| 1574 |
if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 1575 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1576 |
glslangIntermediate->getVertices()); |
| 1577 |
primitive = glslangIntermediate->getOutputPrimitive(); |
| 1578 |
} else { |
| 1579 |
primitive = glslangIntermediate->getInputPrimitive(); |
| 1580 |
} |
| 1581 |
|
| 1582 |
switch (primitive) { |
| 1583 |
case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1584 |
case glslang::ElgQuads: mode = spv::ExecutionModeQuads; break; |
| 1585 |
case glslang::ElgIsolines: mode = spv::ExecutionModeIsolines; break; |
| 1586 |
default: mode = spv::ExecutionModeMax; break; |
| 1587 |
} |
| 1588 |
if (mode != spv::ExecutionModeMax) |
| 1589 |
builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1590 |
|
| 1591 |
switch (glslangIntermediate->getVertexSpacing()) { |
| 1592 |
case glslang::EvsEqual: mode = spv::ExecutionModeSpacingEqual; break; |
| 1593 |
case glslang::EvsFractionalEven: mode = spv::ExecutionModeSpacingFractionalEven; break; |
| 1594 |
case glslang::EvsFractionalOdd: mode = spv::ExecutionModeSpacingFractionalOdd; break; |
| 1595 |
default: mode = spv::ExecutionModeMax; break; |
| 1596 |
} |
| 1597 |
if (mode != spv::ExecutionModeMax) |
| 1598 |
builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1599 |
|
| 1600 |
switch (glslangIntermediate->getVertexOrder()) { |
| 1601 |
case glslang::EvoCw: mode = spv::ExecutionModeVertexOrderCw; break; |
| 1602 |
case glslang::EvoCcw: mode = spv::ExecutionModeVertexOrderCcw; break; |
| 1603 |
default: mode = spv::ExecutionModeMax; break; |
| 1604 |
} |
| 1605 |
if (mode != spv::ExecutionModeMax) |
| 1606 |
builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1607 |
|
| 1608 |
if (glslangIntermediate->getPointMode()) |
| 1609 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModePointMode); |
| 1610 |
break; |
| 1611 |
|
| 1612 |
case EShLangGeometry: |
| 1613 |
builder.addCapability(spv::CapabilityGeometry); |
| 1614 |
switch (glslangIntermediate->getInputPrimitive()) { |
| 1615 |
case glslang::ElgPoints: mode = spv::ExecutionModeInputPoints; break; |
| 1616 |
case glslang::ElgLines: mode = spv::ExecutionModeInputLines; break; |
| 1617 |
case glslang::ElgLinesAdjacency: mode = spv::ExecutionModeInputLinesAdjacency; break; |
| 1618 |
case glslang::ElgTriangles: mode = spv::ExecutionModeTriangles; break; |
| 1619 |
case glslang::ElgTrianglesAdjacency: mode = spv::ExecutionModeInputTrianglesAdjacency; break; |
| 1620 |
default: mode = spv::ExecutionModeMax; break; |
| 1621 |
} |
| 1622 |
if (mode != spv::ExecutionModeMax) |
| 1623 |
builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1624 |
|
| 1625 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeInvocations, glslangIntermediate->getInvocations()); |
| 1626 |
|
| 1627 |
switch (glslangIntermediate->getOutputPrimitive()) { |
| 1628 |
case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1629 |
case glslang::ElgLineStrip: mode = spv::ExecutionModeOutputLineStrip; break; |
| 1630 |
case glslang::ElgTriangleStrip: mode = spv::ExecutionModeOutputTriangleStrip; break; |
| 1631 |
default: mode = spv::ExecutionModeMax; break; |
| 1632 |
} |
| 1633 |
if (mode != spv::ExecutionModeMax) |
| 1634 |
builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1635 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, glslangIntermediate->getVertices()); |
| 1636 |
break; |
| 1637 |
|
| 1638 |
case EShLangRayGen: |
| 1639 |
case EShLangIntersect: |
| 1640 |
case EShLangAnyHit: |
| 1641 |
case EShLangClosestHit: |
| 1642 |
case EShLangMiss: |
| 1643 |
case EShLangCallable: |
| 1644 |
{ |
| 1645 |
auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 1646 |
if (extensions.find("GL_NV_ray_tracing") == extensions.end()) { |
| 1647 |
builder.addCapability(spv::CapabilityRayTracingProvisionalKHR); |
| 1648 |
builder.addExtension("SPV_KHR_ray_tracing"); |
| 1649 |
} |
| 1650 |
else { |
| 1651 |
builder.addCapability(spv::CapabilityRayTracingNV); |
| 1652 |
builder.addExtension("SPV_NV_ray_tracing"); |
| 1653 |
} |
| 1654 |
break; |
| 1655 |
} |
| 1656 |
case EShLangTaskNV: |
| 1657 |
case EShLangMeshNV: |
| 1658 |
builder.addCapability(spv::CapabilityMeshShadingNV); |
| 1659 |
builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 1660 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeLocalSize, glslangIntermediate->getLocalSize(0), |
| 1661 |
glslangIntermediate->getLocalSize(1), |
| 1662 |
glslangIntermediate->getLocalSize(2)); |
| 1663 |
if (glslangIntermediate->getStage() == EShLangMeshNV) { |
| 1664 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputVertices, |
| 1665 |
glslangIntermediate->getVertices()); |
| 1666 |
builder.addExecutionMode(shaderEntry, spv::ExecutionModeOutputPrimitivesNV, |
| 1667 |
glslangIntermediate->getPrimitives()); |
| 1668 |
|
| 1669 |
switch (glslangIntermediate->getOutputPrimitive()) { |
| 1670 |
case glslang::ElgPoints: mode = spv::ExecutionModeOutputPoints; break; |
| 1671 |
case glslang::ElgLines: mode = spv::ExecutionModeOutputLinesNV; break; |
| 1672 |
case glslang::ElgTriangles: mode = spv::ExecutionModeOutputTrianglesNV; break; |
| 1673 |
default: mode = spv::ExecutionModeMax; break; |
| 1674 |
} |
| 1675 |
if (mode != spv::ExecutionModeMax) |
| 1676 |
builder.addExecutionMode(shaderEntry, (spv::ExecutionMode)mode); |
| 1677 |
} |
| 1678 |
break; |
| 1679 |
#endif |
| 1680 |
|
| 1681 |
default: |
| 1682 |
break; |
| 1683 |
} |
| 1684 |
} |
| 1685 |
|
| 1686 |
// Finish creating SPV, after the traversal is complete. |
| 1687 |
void TGlslangToSpvTraverser::finishSpv() |
| 1688 |
{ |
| 1689 |
// Finish the entry point function |
| 1690 |
if (! entryPointTerminated) { |
| 1691 |
builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 1692 |
builder.leaveFunction(); |
| 1693 |
} |
| 1694 |
|
| 1695 |
// finish off the entry-point SPV instruction by adding the Input/Output <id> |
| 1696 |
for (auto it = iOSet.cbegin(); it != iOSet.cend(); ++it) |
| 1697 |
entryPoint->addIdOperand(*it); |
| 1698 |
|
| 1699 |
// Add capabilities, extensions, remove unneeded decorations, etc., |
| 1700 |
// based on the resulting SPIR-V. |
| 1701 |
// Note: WebGPU code generation must have the opportunity to aggressively |
| 1702 |
// prune unreachable merge blocks and continue targets. |
| 1703 |
builder.postProcess(); |
| 1704 |
} |
| 1705 |
|
| 1706 |
// Write the SPV into 'out'. |
| 1707 |
void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out) |
| 1708 |
{ |
| 1709 |
builder.dump(out); |
| 1710 |
} |
| 1711 |
|
| 1712 |
// |
| 1713 |
// Implement the traversal functions. |
| 1714 |
// |
| 1715 |
// Return true from interior nodes to have the external traversal |
| 1716 |
// continue on to children. Return false if children were |
| 1717 |
// already processed. |
| 1718 |
// |
| 1719 |
|
| 1720 |
// |
| 1721 |
// Symbols can turn into |
| 1722 |
// - uniform/input reads |
| 1723 |
// - output writes |
| 1724 |
// - complex lvalue base setups: foo.bar[3].... , where we see foo and start up an access chain |
| 1725 |
// - something simple that degenerates into the last bullet |
| 1726 |
// |
| 1727 |
void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) |
| 1728 |
{ |
| 1729 |
SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1730 |
if (symbol->getType().isStruct()) |
| 1731 |
glslangTypeToIdMap[symbol->getType().getStruct()] = symbol->getId(); |
| 1732 |
|
| 1733 |
if (symbol->getType().getQualifier().isSpecConstant()) |
| 1734 |
spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1735 |
|
| 1736 |
#ifdef ENABLE_HLSL |
| 1737 |
// Skip symbol handling if it is string-typed |
| 1738 |
if (symbol->getBasicType() == glslang::EbtString) |
| 1739 |
return; |
| 1740 |
#endif |
| 1741 |
|
| 1742 |
// getSymbolId() will set up all the IO decorations on the first call. |
| 1743 |
// Formal function parameters were mapped during makeFunctions(). |
| 1744 |
spv::Id id = getSymbolId(symbol); |
| 1745 |
|
| 1746 |
if (builder.isPointer(id)) { |
| 1747 |
if (!symbol->getType().getQualifier().isParamInput() && |
| 1748 |
!symbol->getType().getQualifier().isParamOutput()) { |
| 1749 |
// Include all "static use" and "linkage only" interface variables on the OpEntryPoint instruction |
| 1750 |
// Consider adding to the OpEntryPoint interface list. |
| 1751 |
// Only looking at structures if they have at least one member. |
| 1752 |
if (!symbol->getType().isStruct() || symbol->getType().getStruct()->size() > 0) { |
| 1753 |
spv::StorageClass sc = builder.getStorageClass(id); |
| 1754 |
// Before SPIR-V 1.4, we only want to include Input and Output. |
| 1755 |
// Starting with SPIR-V 1.4, we want all globals. |
| 1756 |
if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(id)) || |
| 1757 |
(sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { |
| 1758 |
iOSet.insert(id); |
| 1759 |
} |
| 1760 |
} |
| 1761 |
} |
| 1762 |
|
| 1763 |
// If the SPIR-V type is required to be different than the AST type |
| 1764 |
// (for ex SubgroupMasks or 3x4 ObjectToWorld/WorldToObject matrices), |
| 1765 |
// translate now from the SPIR-V type to the AST type, for the consuming |
| 1766 |
// operation. |
| 1767 |
// Note this turns it from an l-value to an r-value. |
| 1768 |
// Currently, all symbols needing this are inputs; avoid the map lookup when non-input. |
| 1769 |
if (symbol->getType().getQualifier().storage == glslang::EvqVaryingIn) |
| 1770 |
id = translateForcedType(id); |
| 1771 |
} |
| 1772 |
|
| 1773 |
// Only process non-linkage-only nodes for generating actual static uses |
| 1774 |
if (! linkageOnly || symbol->getQualifier().isSpecConstant()) { |
| 1775 |
// Prepare to generate code for the access |
| 1776 |
|
| 1777 |
// L-value chains will be computed left to right. We're on the symbol now, |
| 1778 |
// which is the left-most part of the access chain, so now is "clear" time, |
| 1779 |
// followed by setting the base. |
| 1780 |
builder.clearAccessChain(); |
| 1781 |
|
| 1782 |
// For now, we consider all user variables as being in memory, so they are pointers, |
| 1783 |
// except for |
| 1784 |
// A) R-Value arguments to a function, which are an intermediate object. |
| 1785 |
// See comments in handleUserFunctionCall(). |
| 1786 |
// B) Specialization constants (normal constants don't even come in as a variable), |
| 1787 |
// These are also pure R-values. |
| 1788 |
// C) R-Values from type translation, see above call to translateForcedType() |
| 1789 |
glslang::TQualifier qualifier = symbol->getQualifier(); |
| 1790 |
if (qualifier.isSpecConstant() || rValueParameters.find(symbol->getId()) != rValueParameters.end() || |
| 1791 |
!builder.isPointerType(builder.getTypeId(id))) |
| 1792 |
builder.setAccessChainRValue(id); |
| 1793 |
else |
| 1794 |
builder.setAccessChainLValue(id); |
| 1795 |
} |
| 1796 |
|
| 1797 |
#ifdef ENABLE_HLSL |
| 1798 |
// Process linkage-only nodes for any special additional interface work. |
| 1799 |
if (linkageOnly) { |
| 1800 |
if (glslangIntermediate->getHlslFunctionality1()) { |
| 1801 |
// Map implicit counter buffers to their originating buffers, which should have been |
| 1802 |
// seen by now, given earlier pruning of unused counters, and preservation of order |
| 1803 |
// of declaration. |
| 1804 |
if (symbol->getType().getQualifier().isUniformOrBuffer()) { |
| 1805 |
if (!glslangIntermediate->hasCounterBufferName(symbol->getName())) { |
| 1806 |
// Save possible originating buffers for counter buffers, keyed by |
| 1807 |
// making the potential counter-buffer name. |
| 1808 |
std::string keyName = symbol->getName().c_str(); |
| 1809 |
keyName = glslangIntermediate->addCounterBufferName(keyName); |
| 1810 |
counterOriginator[keyName] = symbol; |
| 1811 |
} else { |
| 1812 |
// Handle a counter buffer, by finding the saved originating buffer. |
| 1813 |
std::string keyName = symbol->getName().c_str(); |
| 1814 |
auto it = counterOriginator.find(keyName); |
| 1815 |
if (it != counterOriginator.end()) { |
| 1816 |
id = getSymbolId(it->second); |
| 1817 |
if (id != spv::NoResult) { |
| 1818 |
spv::Id counterId = getSymbolId(symbol); |
| 1819 |
if (counterId != spv::NoResult) { |
| 1820 |
builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 1821 |
builder.addDecorationId(id, spv::DecorationHlslCounterBufferGOOGLE, counterId); |
| 1822 |
} |
| 1823 |
} |
| 1824 |
} |
| 1825 |
} |
| 1826 |
} |
| 1827 |
} |
| 1828 |
} |
| 1829 |
#endif |
| 1830 |
} |
| 1831 |
|
| 1832 |
bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit /* visit */, glslang::TIntermBinary* node) |
| 1833 |
{ |
| 1834 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 1835 |
if (node->getLeft()->getAsSymbolNode() != nullptr && node->getLeft()->getType().isStruct()) { |
| 1836 |
glslangTypeToIdMap[node->getLeft()->getType().getStruct()] = node->getLeft()->getAsSymbolNode()->getId(); |
| 1837 |
} |
| 1838 |
if (node->getRight()->getAsSymbolNode() != nullptr && node->getRight()->getType().isStruct()) { |
| 1839 |
glslangTypeToIdMap[node->getRight()->getType().getStruct()] = node->getRight()->getAsSymbolNode()->getId(); |
| 1840 |
} |
| 1841 |
|
| 1842 |
SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 1843 |
if (node->getType().getQualifier().isSpecConstant()) |
| 1844 |
spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 1845 |
|
| 1846 |
// First, handle special cases |
| 1847 |
switch (node->getOp()) { |
| 1848 |
case glslang::EOpAssign: |
| 1849 |
case glslang::EOpAddAssign: |
| 1850 |
case glslang::EOpSubAssign: |
| 1851 |
case glslang::EOpMulAssign: |
| 1852 |
case glslang::EOpVectorTimesMatrixAssign: |
| 1853 |
case glslang::EOpVectorTimesScalarAssign: |
| 1854 |
case glslang::EOpMatrixTimesScalarAssign: |
| 1855 |
case glslang::EOpMatrixTimesMatrixAssign: |
| 1856 |
case glslang::EOpDivAssign: |
| 1857 |
case glslang::EOpModAssign: |
| 1858 |
case glslang::EOpAndAssign: |
| 1859 |
case glslang::EOpInclusiveOrAssign: |
| 1860 |
case glslang::EOpExclusiveOrAssign: |
| 1861 |
case glslang::EOpLeftShiftAssign: |
| 1862 |
case glslang::EOpRightShiftAssign: |
| 1863 |
// A bin-op assign "a += b" means the same thing as "a = a + b" |
| 1864 |
// where a is evaluated before b. For a simple assignment, GLSL |
| 1865 |
// says to evaluate the left before the right. So, always, left |
| 1866 |
// node then right node. |
| 1867 |
{ |
| 1868 |
// get the left l-value, save it away |
| 1869 |
builder.clearAccessChain(); |
| 1870 |
node->getLeft()->traverse(this); |
| 1871 |
spv::Builder::AccessChain lValue = builder.getAccessChain(); |
| 1872 |
|
| 1873 |
// evaluate the right |
| 1874 |
builder.clearAccessChain(); |
| 1875 |
node->getRight()->traverse(this); |
| 1876 |
spv::Id rValue = accessChainLoad(node->getRight()->getType()); |
| 1877 |
|
| 1878 |
if (node->getOp() != glslang::EOpAssign) { |
| 1879 |
// the left is also an r-value |
| 1880 |
builder.setAccessChain(lValue); |
| 1881 |
spv::Id leftRValue = accessChainLoad(node->getLeft()->getType()); |
| 1882 |
|
| 1883 |
// do the operation |
| 1884 |
OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
| 1885 |
TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 1886 |
TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 1887 |
rValue = createBinaryOperation(node->getOp(), decorations, |
| 1888 |
convertGlslangToSpvType(node->getType()), leftRValue, rValue, |
| 1889 |
node->getType().getBasicType()); |
| 1890 |
|
| 1891 |
// these all need their counterparts in createBinaryOperation() |
| 1892 |
assert(rValue != spv::NoResult); |
| 1893 |
} |
| 1894 |
|
| 1895 |
// store the result |
| 1896 |
builder.setAccessChain(lValue); |
| 1897 |
multiTypeStore(node->getLeft()->getType(), rValue); |
| 1898 |
|
| 1899 |
// assignments are expressions having an rValue after they are evaluated... |
| 1900 |
builder.clearAccessChain(); |
| 1901 |
builder.setAccessChainRValue(rValue); |
| 1902 |
} |
| 1903 |
return false; |
| 1904 |
case glslang::EOpIndexDirect: |
| 1905 |
case glslang::EOpIndexDirectStruct: |
| 1906 |
{ |
| 1907 |
// Structure, array, matrix, or vector indirection with statically known index. |
| 1908 |
// Get the left part of the access chain. |
| 1909 |
node->getLeft()->traverse(this); |
| 1910 |
|
| 1911 |
// Add the next element in the chain |
| 1912 |
|
| 1913 |
const int glslangIndex = node->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); |
| 1914 |
if (! node->getLeft()->getType().isArray() && |
| 1915 |
node->getLeft()->getType().isVector() && |
| 1916 |
node->getOp() == glslang::EOpIndexDirect) { |
| 1917 |
// This is essentially a hard-coded vector swizzle of size 1, |
| 1918 |
// so short circuit the access-chain stuff with a swizzle. |
| 1919 |
std::vector<unsigned> swizzle; |
| 1920 |
swizzle.push_back(glslangIndex); |
| 1921 |
int dummySize; |
| 1922 |
builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1923 |
TranslateCoherent(node->getLeft()->getType()), |
| 1924 |
glslangIntermediate->getBaseAlignmentScalar( |
| 1925 |
node->getLeft()->getType(), dummySize)); |
| 1926 |
} else { |
| 1927 |
|
| 1928 |
// Load through a block reference is performed with a dot operator that |
| 1929 |
// is mapped to EOpIndexDirectStruct. When we get to the actual reference, |
| 1930 |
// do a load and reset the access chain. |
| 1931 |
if (node->getLeft()->isReference() && |
| 1932 |
!node->getLeft()->getType().isArray() && |
| 1933 |
node->getOp() == glslang::EOpIndexDirectStruct) |
| 1934 |
{ |
| 1935 |
spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 1936 |
builder.clearAccessChain(); |
| 1937 |
builder.setAccessChainLValue(left); |
| 1938 |
} |
| 1939 |
|
| 1940 |
int spvIndex = glslangIndex; |
| 1941 |
if (node->getLeft()->getBasicType() == glslang::EbtBlock && |
| 1942 |
node->getOp() == glslang::EOpIndexDirectStruct) |
| 1943 |
{ |
| 1944 |
// This may be, e.g., an anonymous block-member selection, which generally need |
| 1945 |
// index remapping due to hidden members in anonymous blocks. |
| 1946 |
int glslangId = glslangTypeToIdMap[node->getLeft()->getType().getStruct()]; |
| 1947 |
if (memberRemapper.find(glslangId) != memberRemapper.end()) { |
| 1948 |
std::vector<int>& remapper = memberRemapper[glslangId]; |
| 1949 |
assert(remapper.size() > 0); |
| 1950 |
spvIndex = remapper[glslangIndex]; |
| 1951 |
} |
| 1952 |
} |
| 1953 |
|
| 1954 |
// normal case for indexing array or structure or block |
| 1955 |
builder.accessChainPush(builder.makeIntConstant(spvIndex), |
| 1956 |
TranslateCoherent(node->getLeft()->getType()), |
| 1957 |
node->getLeft()->getType().getBufferReferenceAlignment()); |
| 1958 |
|
| 1959 |
// Add capabilities here for accessing PointSize and clip/cull distance. |
| 1960 |
// We have deferred generation of associated capabilities until now. |
| 1961 |
if (node->getLeft()->getType().isStruct() && ! node->getLeft()->getType().isArray()) |
| 1962 |
declareUseOfStructMember(*(node->getLeft()->getType().getStruct()), glslangIndex); |
| 1963 |
} |
| 1964 |
} |
| 1965 |
return false; |
| 1966 |
case glslang::EOpIndexIndirect: |
| 1967 |
{ |
| 1968 |
// Array, matrix, or vector indirection with variable index. |
| 1969 |
// Will use native SPIR-V access-chain for and array indirection; |
| 1970 |
// matrices are arrays of vectors, so will also work for a matrix. |
| 1971 |
// Will use the access chain's 'component' for variable index into a vector. |
| 1972 |
|
| 1973 |
// This adapter is building access chains left to right. |
| 1974 |
// Set up the access chain to the left. |
| 1975 |
node->getLeft()->traverse(this); |
| 1976 |
|
| 1977 |
// save it so that computing the right side doesn't trash it |
| 1978 |
spv::Builder::AccessChain partial = builder.getAccessChain(); |
| 1979 |
|
| 1980 |
// compute the next index in the chain |
| 1981 |
builder.clearAccessChain(); |
| 1982 |
node->getRight()->traverse(this); |
| 1983 |
spv::Id index = accessChainLoad(node->getRight()->getType()); |
| 1984 |
|
| 1985 |
addIndirectionIndexCapabilities(node->getLeft()->getType(), node->getRight()->getType()); |
| 1986 |
|
| 1987 |
// restore the saved access chain |
| 1988 |
builder.setAccessChain(partial); |
| 1989 |
|
| 1990 |
if (! node->getLeft()->getType().isArray() && node->getLeft()->getType().isVector()) { |
| 1991 |
int dummySize; |
| 1992 |
builder.accessChainPushComponent(index, convertGlslangToSpvType(node->getLeft()->getType()), |
| 1993 |
TranslateCoherent(node->getLeft()->getType()), |
| 1994 |
glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 1995 |
dummySize)); |
| 1996 |
} else |
| 1997 |
builder.accessChainPush(index, TranslateCoherent(node->getLeft()->getType()), |
| 1998 |
node->getLeft()->getType().getBufferReferenceAlignment()); |
| 1999 |
} |
| 2000 |
return false; |
| 2001 |
case glslang::EOpVectorSwizzle: |
| 2002 |
{ |
| 2003 |
node->getLeft()->traverse(this); |
| 2004 |
std::vector<unsigned> swizzle; |
| 2005 |
convertSwizzle(*node->getRight()->getAsAggregate(), swizzle); |
| 2006 |
int dummySize; |
| 2007 |
builder.accessChainPushSwizzle(swizzle, convertGlslangToSpvType(node->getLeft()->getType()), |
| 2008 |
TranslateCoherent(node->getLeft()->getType()), |
| 2009 |
glslangIntermediate->getBaseAlignmentScalar(node->getLeft()->getType(), |
| 2010 |
dummySize)); |
| 2011 |
} |
| 2012 |
return false; |
| 2013 |
case glslang::EOpMatrixSwizzle: |
| 2014 |
logger->missingFunctionality("matrix swizzle"); |
| 2015 |
return true; |
| 2016 |
case glslang::EOpLogicalOr: |
| 2017 |
case glslang::EOpLogicalAnd: |
| 2018 |
{ |
| 2019 |
|
| 2020 |
// These may require short circuiting, but can sometimes be done as straight |
| 2021 |
// binary operations. The right operand must be short circuited if it has |
| 2022 |
// side effects, and should probably be if it is complex. |
| 2023 |
if (isTrivial(node->getRight()->getAsTyped())) |
| 2024 |
break; // handle below as a normal binary operation |
| 2025 |
// otherwise, we need to do dynamic short circuiting on the right operand |
| 2026 |
spv::Id result = createShortCircuit(node->getOp(), *node->getLeft()->getAsTyped(), |
| 2027 |
*node->getRight()->getAsTyped()); |
| 2028 |
builder.clearAccessChain(); |
| 2029 |
builder.setAccessChainRValue(result); |
| 2030 |
} |
| 2031 |
return false; |
| 2032 |
default: |
| 2033 |
break; |
| 2034 |
} |
| 2035 |
|
| 2036 |
// Assume generic binary op... |
| 2037 |
|
| 2038 |
// get right operand |
| 2039 |
builder.clearAccessChain(); |
| 2040 |
node->getLeft()->traverse(this); |
| 2041 |
spv::Id left = accessChainLoad(node->getLeft()->getType()); |
| 2042 |
|
| 2043 |
// get left operand |
| 2044 |
builder.clearAccessChain(); |
| 2045 |
node->getRight()->traverse(this); |
| 2046 |
spv::Id right = accessChainLoad(node->getRight()->getType()); |
| 2047 |
|
| 2048 |
// get result |
| 2049 |
OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
| 2050 |
TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2051 |
TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 2052 |
spv::Id result = createBinaryOperation(node->getOp(), decorations, |
| 2053 |
convertGlslangToSpvType(node->getType()), left, right, |
| 2054 |
node->getLeft()->getType().getBasicType()); |
| 2055 |
|
| 2056 |
builder.clearAccessChain(); |
| 2057 |
if (! result) { |
| 2058 |
logger->missingFunctionality("unknown glslang binary operation"); |
| 2059 |
return true; // pick up a child as the place-holder result |
| 2060 |
} else { |
| 2061 |
builder.setAccessChainRValue(result); |
| 2062 |
return false; |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
// Figure out what, if any, type changes are needed when accessing a specific built-in. |
| 2067 |
// Returns <the type SPIR-V requires for declarion, the type to translate to on use>. |
| 2068 |
// Also see comment for 'forceType', regarding tracking SPIR-V-required types. |
| 2069 |
std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn, |
| 2070 |
const glslang::TType& glslangType) |
| 2071 |
{ |
| 2072 |
switch(glslangBuiltIn) |
| 2073 |
{ |
| 2074 |
case glslang::EbvSubGroupEqMask: |
| 2075 |
case glslang::EbvSubGroupGeMask: |
| 2076 |
case glslang::EbvSubGroupGtMask: |
| 2077 |
case glslang::EbvSubGroupLeMask: |
| 2078 |
case glslang::EbvSubGroupLtMask: { |
| 2079 |
// these require changing a 64-bit scaler -> a vector of 32-bit components |
| 2080 |
if (glslangType.isVector()) |
| 2081 |
break; |
| 2082 |
std::pair<spv::Id, spv::Id> ret(builder.makeVectorType(builder.makeUintType(32), 4), |
| 2083 |
builder.makeUintType(64)); |
| 2084 |
return ret; |
| 2085 |
} |
| 2086 |
// There are no SPIR-V builtins defined for these and map onto original non-transposed |
| 2087 |
// builtins. During visitBinary we insert a transpose |
| 2088 |
case glslang::EbvWorldToObject3x4: |
| 2089 |
case glslang::EbvObjectToWorld3x4: { |
| 2090 |
spv::Id mat43 = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 2091 |
spv::Id mat34 = builder.makeMatrixType(builder.makeFloatType(32), 3, 4); |
| 2092 |
std::pair<spv::Id, spv::Id> ret(mat43, mat34); |
| 2093 |
return ret; |
| 2094 |
} |
| 2095 |
default: |
| 2096 |
break; |
| 2097 |
} |
| 2098 |
|
| 2099 |
std::pair<spv::Id, spv::Id> ret(spv::NoType, spv::NoType); |
| 2100 |
return ret; |
| 2101 |
} |
| 2102 |
|
| 2103 |
// For an object previously identified (see getForcedType() and forceType) |
| 2104 |
// as needing type translations, do the translation needed for a load, turning |
| 2105 |
// an L-value into in R-value. |
| 2106 |
spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object) |
| 2107 |
{ |
| 2108 |
const auto forceIt = forceType.find(object); |
| 2109 |
if (forceIt == forceType.end()) |
| 2110 |
return object; |
| 2111 |
|
| 2112 |
spv::Id desiredTypeId = forceIt->second; |
| 2113 |
spv::Id objectTypeId = builder.getTypeId(object); |
| 2114 |
assert(builder.isPointerType(objectTypeId)); |
| 2115 |
objectTypeId = builder.getContainedTypeId(objectTypeId); |
| 2116 |
if (builder.isVectorType(objectTypeId) && |
| 2117 |
builder.getScalarTypeWidth(builder.getContainedTypeId(objectTypeId)) == 32) { |
| 2118 |
if (builder.getScalarTypeWidth(desiredTypeId) == 64) { |
| 2119 |
// handle 32-bit v.xy* -> 64-bit |
| 2120 |
builder.clearAccessChain(); |
| 2121 |
builder.setAccessChainLValue(object); |
| 2122 |
object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 2123 |
std::vector<spv::Id> components; |
| 2124 |
components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 0)); |
| 2125 |
components.push_back(builder.createCompositeExtract(object, builder.getContainedTypeId(objectTypeId), 1)); |
| 2126 |
|
| 2127 |
spv::Id vecType = builder.makeVectorType(builder.getContainedTypeId(objectTypeId), 2); |
| 2128 |
return builder.createUnaryOp(spv::OpBitcast, desiredTypeId, |
| 2129 |
builder.createCompositeConstruct(vecType, components)); |
| 2130 |
} else { |
| 2131 |
logger->missingFunctionality("forcing 32-bit vector type to non 64-bit scalar"); |
| 2132 |
} |
| 2133 |
} else if (builder.isMatrixType(objectTypeId)) { |
| 2134 |
// There are no SPIR-V builtins defined for 3x4 variants of ObjectToWorld/WorldToObject |
| 2135 |
// and we insert a transpose after loading the original non-transposed builtins |
| 2136 |
builder.clearAccessChain(); |
| 2137 |
builder.setAccessChainLValue(object); |
| 2138 |
object = builder.accessChainLoad(spv::NoPrecision, spv::DecorationMax, objectTypeId); |
| 2139 |
return builder.createUnaryOp(spv::OpTranspose, desiredTypeId, object); |
| 2140 |
|
| 2141 |
} else { |
| 2142 |
logger->missingFunctionality("forcing non 32-bit vector type"); |
| 2143 |
} |
| 2144 |
|
| 2145 |
return object; |
| 2146 |
} |
| 2147 |
|
| 2148 |
bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TIntermUnary* node) |
| 2149 |
{ |
| 2150 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 2151 |
|
| 2152 |
SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2153 |
if (node->getType().getQualifier().isSpecConstant()) |
| 2154 |
spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2155 |
|
| 2156 |
spv::Id result = spv::NoResult; |
| 2157 |
|
| 2158 |
// try texturing first |
| 2159 |
result = createImageTextureFunctionCall(node); |
| 2160 |
if (result != spv::NoResult) { |
| 2161 |
builder.clearAccessChain(); |
| 2162 |
builder.setAccessChainRValue(result); |
| 2163 |
|
| 2164 |
return false; // done with this node |
| 2165 |
} |
| 2166 |
|
| 2167 |
// Non-texturing. |
| 2168 |
|
| 2169 |
if (node->getOp() == glslang::EOpArrayLength) { |
| 2170 |
// Quite special; won't want to evaluate the operand. |
| 2171 |
|
| 2172 |
// Currently, the front-end does not allow .length() on an array until it is sized, |
| 2173 |
// except for the last block membeor of an SSBO. |
| 2174 |
// TODO: If this changes, link-time sized arrays might show up here, and need their |
| 2175 |
// size extracted. |
| 2176 |
|
| 2177 |
// Normal .length() would have been constant folded by the front-end. |
| 2178 |
// So, this has to be block.lastMember.length(). |
| 2179 |
// SPV wants "block" and member number as the operands, go get them. |
| 2180 |
|
| 2181 |
spv::Id length; |
| 2182 |
if (node->getOperand()->getType().isCoopMat()) { |
| 2183 |
spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2184 |
|
| 2185 |
spv::Id typeId = convertGlslangToSpvType(node->getOperand()->getType()); |
| 2186 |
assert(builder.isCooperativeMatrixType(typeId)); |
| 2187 |
|
| 2188 |
length = builder.createCooperativeMatrixLength(typeId); |
| 2189 |
} else { |
| 2190 |
glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2191 |
block->traverse(this); |
| 2192 |
unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion() |
| 2193 |
->getConstArray()[0].getUConst(); |
| 2194 |
length = builder.createArrayLength(builder.accessChainGetLValue(), member); |
| 2195 |
} |
| 2196 |
|
| 2197 |
// GLSL semantics say the result of .length() is an int, while SPIR-V says |
| 2198 |
// signedness must be 0. So, convert from SPIR-V unsigned back to GLSL's |
| 2199 |
// AST expectation of a signed result. |
| 2200 |
if (glslangIntermediate->getSource() == glslang::EShSourceGlsl) { |
| 2201 |
if (builder.isInSpecConstCodeGenMode()) { |
| 2202 |
length = builder.createBinOp(spv::OpIAdd, builder.makeIntType(32), length, builder.makeIntConstant(0)); |
| 2203 |
} else { |
| 2204 |
length = builder.createUnaryOp(spv::OpBitcast, builder.makeIntType(32), length); |
| 2205 |
} |
| 2206 |
} |
| 2207 |
|
| 2208 |
builder.clearAccessChain(); |
| 2209 |
builder.setAccessChainRValue(length); |
| 2210 |
|
| 2211 |
return false; |
| 2212 |
} |
| 2213 |
|
| 2214 |
// Start by evaluating the operand |
| 2215 |
|
| 2216 |
// Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2217 |
// operate first on the swizzle base, then apply the swizzle. |
| 2218 |
spv::Id invertedType = spv::NoType; |
| 2219 |
auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2220 |
invertedType : convertGlslangToSpvType(node->getType()); }; |
| 2221 |
if (node->getOp() == glslang::EOpInterpolateAtCentroid) |
| 2222 |
invertedType = getInvertedSwizzleType(*node->getOperand()); |
| 2223 |
|
| 2224 |
builder.clearAccessChain(); |
| 2225 |
TIntermNode *operandNode; |
| 2226 |
if (invertedType != spv::NoType) |
| 2227 |
operandNode = node->getOperand()->getAsBinaryNode()->getLeft(); |
| 2228 |
else |
| 2229 |
operandNode = node->getOperand(); |
| 2230 |
|
| 2231 |
operandNode->traverse(this); |
| 2232 |
|
| 2233 |
spv::Id operand = spv::NoResult; |
| 2234 |
|
| 2235 |
spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2236 |
|
| 2237 |
#ifndef GLSLANG_WEB |
| 2238 |
if (node->getOp() == glslang::EOpAtomicCounterIncrement || |
| 2239 |
node->getOp() == glslang::EOpAtomicCounterDecrement || |
| 2240 |
node->getOp() == glslang::EOpAtomicCounter || |
| 2241 |
node->getOp() == glslang::EOpInterpolateAtCentroid || |
| 2242 |
node->getOp() == glslang::EOpRayQueryProceed || |
| 2243 |
node->getOp() == glslang::EOpRayQueryGetRayTMin || |
| 2244 |
node->getOp() == glslang::EOpRayQueryGetRayFlags || |
| 2245 |
node->getOp() == glslang::EOpRayQueryGetWorldRayOrigin || |
| 2246 |
node->getOp() == glslang::EOpRayQueryGetWorldRayDirection || |
| 2247 |
node->getOp() == glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque || |
| 2248 |
node->getOp() == glslang::EOpRayQueryTerminate || |
| 2249 |
node->getOp() == glslang::EOpRayQueryConfirmIntersection) { |
| 2250 |
operand = builder.accessChainGetLValue(); // Special case l-value operands |
| 2251 |
lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 2252 |
lvalueCoherentFlags |= TranslateCoherent(operandNode->getAsTyped()->getType()); |
| 2253 |
} else |
| 2254 |
#endif |
| 2255 |
{ |
| 2256 |
operand = accessChainLoad(node->getOperand()->getType()); |
| 2257 |
} |
| 2258 |
|
| 2259 |
OpDecorations decorations = { TranslatePrecisionDecoration(node->getOperationPrecision()), |
| 2260 |
TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2261 |
TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 2262 |
|
| 2263 |
// it could be a conversion |
| 2264 |
if (! result) |
| 2265 |
result = createConversion(node->getOp(), decorations, resultType(), operand, |
| 2266 |
node->getOperand()->getBasicType()); |
| 2267 |
|
| 2268 |
// if not, then possibly an operation |
| 2269 |
if (! result) |
| 2270 |
result = createUnaryOperation(node->getOp(), decorations, resultType(), operand, |
| 2271 |
node->getOperand()->getBasicType(), lvalueCoherentFlags); |
| 2272 |
|
| 2273 |
if (result) { |
| 2274 |
if (invertedType) { |
| 2275 |
result = createInvertedSwizzle(decorations.precision, *node->getOperand(), result); |
| 2276 |
decorations.addNonUniform(builder, result); |
| 2277 |
} |
| 2278 |
|
| 2279 |
builder.clearAccessChain(); |
| 2280 |
builder.setAccessChainRValue(result); |
| 2281 |
|
| 2282 |
return false; // done with this node |
| 2283 |
} |
| 2284 |
|
| 2285 |
// it must be a special case, check... |
| 2286 |
switch (node->getOp()) { |
| 2287 |
case glslang::EOpPostIncrement: |
| 2288 |
case glslang::EOpPostDecrement: |
| 2289 |
case glslang::EOpPreIncrement: |
| 2290 |
case glslang::EOpPreDecrement: |
| 2291 |
{ |
| 2292 |
// we need the integer value "1" or the floating point "1.0" to add/subtract |
| 2293 |
spv::Id one = 0; |
| 2294 |
if (node->getBasicType() == glslang::EbtFloat) |
| 2295 |
one = builder.makeFloatConstant(1.0F); |
| 2296 |
#ifndef GLSLANG_WEB |
| 2297 |
else if (node->getBasicType() == glslang::EbtDouble) |
| 2298 |
one = builder.makeDoubleConstant(1.0); |
| 2299 |
else if (node->getBasicType() == glslang::EbtFloat16) |
| 2300 |
one = builder.makeFloat16Constant(1.0F); |
| 2301 |
else if (node->getBasicType() == glslang::EbtInt8 || node->getBasicType() == glslang::EbtUint8) |
| 2302 |
one = builder.makeInt8Constant(1); |
| 2303 |
else if (node->getBasicType() == glslang::EbtInt16 || node->getBasicType() == glslang::EbtUint16) |
| 2304 |
one = builder.makeInt16Constant(1); |
| 2305 |
else if (node->getBasicType() == glslang::EbtInt64 || node->getBasicType() == glslang::EbtUint64) |
| 2306 |
one = builder.makeInt64Constant(1); |
| 2307 |
#endif |
| 2308 |
else |
| 2309 |
one = builder.makeIntConstant(1); |
| 2310 |
glslang::TOperator op; |
| 2311 |
if (node->getOp() == glslang::EOpPreIncrement || |
| 2312 |
node->getOp() == glslang::EOpPostIncrement) |
| 2313 |
op = glslang::EOpAdd; |
| 2314 |
else |
| 2315 |
op = glslang::EOpSub; |
| 2316 |
|
| 2317 |
spv::Id result = createBinaryOperation(op, decorations, |
| 2318 |
convertGlslangToSpvType(node->getType()), operand, one, |
| 2319 |
node->getType().getBasicType()); |
| 2320 |
assert(result != spv::NoResult); |
| 2321 |
|
| 2322 |
// The result of operation is always stored, but conditionally the |
| 2323 |
// consumed result. The consumed result is always an r-value. |
| 2324 |
builder.accessChainStore(result, |
| 2325 |
TranslateNonUniformDecoration(node->getOperand()->getType().getQualifier())); |
| 2326 |
builder.clearAccessChain(); |
| 2327 |
if (node->getOp() == glslang::EOpPreIncrement || |
| 2328 |
node->getOp() == glslang::EOpPreDecrement) |
| 2329 |
builder.setAccessChainRValue(result); |
| 2330 |
else |
| 2331 |
builder.setAccessChainRValue(operand); |
| 2332 |
} |
| 2333 |
|
| 2334 |
return false; |
| 2335 |
|
| 2336 |
#ifndef GLSLANG_WEB |
| 2337 |
case glslang::EOpEmitStreamVertex: |
| 2338 |
builder.createNoResultOp(spv::OpEmitStreamVertex, operand); |
| 2339 |
return false; |
| 2340 |
case glslang::EOpEndStreamPrimitive: |
| 2341 |
builder.createNoResultOp(spv::OpEndStreamPrimitive, operand); |
| 2342 |
return false; |
| 2343 |
case glslang::EOpRayQueryTerminate: |
| 2344 |
builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operand); |
| 2345 |
return false; |
| 2346 |
case glslang::EOpRayQueryConfirmIntersection: |
| 2347 |
builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operand); |
| 2348 |
return false; |
| 2349 |
#endif |
| 2350 |
|
| 2351 |
default: |
| 2352 |
logger->missingFunctionality("unknown glslang unary"); |
| 2353 |
return true; // pick up operand as placeholder result |
| 2354 |
} |
| 2355 |
} |
| 2356 |
|
| 2357 |
// Construct a composite object, recursively copying members if their types don't match |
| 2358 |
spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents) |
| 2359 |
{ |
| 2360 |
for (int c = 0; c < (int)constituents.size(); ++c) { |
| 2361 |
spv::Id& constituent = constituents[c]; |
| 2362 |
spv::Id lType = builder.getContainedTypeId(resultTypeId, c); |
| 2363 |
spv::Id rType = builder.getTypeId(constituent); |
| 2364 |
if (lType != rType) { |
| 2365 |
if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 2366 |
constituent = builder.createUnaryOp(spv::OpCopyLogical, lType, constituent); |
| 2367 |
} else if (builder.isStructType(rType)) { |
| 2368 |
std::vector<spv::Id> rTypeConstituents; |
| 2369 |
int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2370 |
for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2371 |
rTypeConstituents.push_back(builder.createCompositeExtract(constituent, |
| 2372 |
builder.getContainedTypeId(rType, i), i)); |
| 2373 |
} |
| 2374 |
constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2375 |
} else { |
| 2376 |
assert(builder.isArrayType(rType)); |
| 2377 |
std::vector<spv::Id> rTypeConstituents; |
| 2378 |
int numrTypeConstituents = builder.getNumTypeConstituents(rType); |
| 2379 |
|
| 2380 |
spv::Id elementRType = builder.getContainedTypeId(rType); |
| 2381 |
for (int i = 0; i < numrTypeConstituents; ++i) { |
| 2382 |
rTypeConstituents.push_back(builder.createCompositeExtract(constituent, elementRType, i)); |
| 2383 |
} |
| 2384 |
constituents[c] = createCompositeConstruct(lType, rTypeConstituents); |
| 2385 |
} |
| 2386 |
} |
| 2387 |
} |
| 2388 |
return builder.createCompositeConstruct(resultTypeId, constituents); |
| 2389 |
} |
| 2390 |
|
| 2391 |
bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node) |
| 2392 |
{ |
| 2393 |
SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 2394 |
if (node->getType().getQualifier().isSpecConstant()) |
| 2395 |
spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 2396 |
|
| 2397 |
spv::Id result = spv::NoResult; |
| 2398 |
spv::Id invertedType = spv::NoType; // to use to override the natural type of the node |
| 2399 |
std::vector<spv::Builder::AccessChain> complexLvalues; // for holding swizzling l-values too complex for |
| 2400 |
// SPIR-V, for an out parameter |
| 2401 |
std::vector<glslang::TQualifier> complexLValueQualifiers; |
| 2402 |
std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues |
| 2403 |
|
| 2404 |
auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ? |
| 2405 |
invertedType : |
| 2406 |
convertGlslangToSpvType(node->getType()); }; |
| 2407 |
|
| 2408 |
// try texturing |
| 2409 |
result = createImageTextureFunctionCall(node); |
| 2410 |
if (result != spv::NoResult) { |
| 2411 |
builder.clearAccessChain(); |
| 2412 |
builder.setAccessChainRValue(result); |
| 2413 |
|
| 2414 |
return false; |
| 2415 |
} |
| 2416 |
#ifndef GLSLANG_WEB |
| 2417 |
else if (node->getOp() == glslang::EOpImageStore || |
| 2418 |
node->getOp() == glslang::EOpImageStoreLod || |
| 2419 |
node->getOp() == glslang::EOpImageAtomicStore) { |
| 2420 |
// "imageStore" is a special case, which has no result |
| 2421 |
return false; |
| 2422 |
} |
| 2423 |
#endif |
| 2424 |
|
| 2425 |
glslang::TOperator binOp = glslang::EOpNull; |
| 2426 |
bool reduceComparison = true; |
| 2427 |
bool isMatrix = false; |
| 2428 |
bool noReturnValue = false; |
| 2429 |
bool atomic = false; |
| 2430 |
|
| 2431 |
spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 2432 |
|
| 2433 |
assert(node->getOp()); |
| 2434 |
|
| 2435 |
spv::Decoration precision = TranslatePrecisionDecoration(node->getOperationPrecision()); |
| 2436 |
|
| 2437 |
switch (node->getOp()) { |
| 2438 |
case glslang::EOpSequence: |
| 2439 |
{ |
| 2440 |
if (preVisit) |
| 2441 |
++sequenceDepth; |
| 2442 |
else |
| 2443 |
--sequenceDepth; |
| 2444 |
|
| 2445 |
if (sequenceDepth == 1) { |
| 2446 |
// If this is the parent node of all the functions, we want to see them |
| 2447 |
// early, so all call points have actual SPIR-V functions to reference. |
| 2448 |
// In all cases, still let the traverser visit the children for us. |
| 2449 |
makeFunctions(node->getAsAggregate()->getSequence()); |
| 2450 |
|
| 2451 |
// Also, we want all globals initializers to go into the beginning of the entry point, before |
| 2452 |
// anything else gets there, so visit out of order, doing them all now. |
| 2453 |
makeGlobalInitializers(node->getAsAggregate()->getSequence()); |
| 2454 |
|
| 2455 |
// Initializers are done, don't want to visit again, but functions and link objects need to be processed, |
| 2456 |
// so do them manually. |
| 2457 |
visitFunctions(node->getAsAggregate()->getSequence()); |
| 2458 |
|
| 2459 |
return false; |
| 2460 |
} |
| 2461 |
|
| 2462 |
return true; |
| 2463 |
} |
| 2464 |
case glslang::EOpLinkerObjects: |
| 2465 |
{ |
| 2466 |
if (visit == glslang::EvPreVisit) |
| 2467 |
linkageOnly = true; |
| 2468 |
else |
| 2469 |
linkageOnly = false; |
| 2470 |
|
| 2471 |
return true; |
| 2472 |
} |
| 2473 |
case glslang::EOpComma: |
| 2474 |
{ |
| 2475 |
// processing from left to right naturally leaves the right-most |
| 2476 |
// lying around in the access chain |
| 2477 |
glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2478 |
for (int i = 0; i < (int)glslangOperands.size(); ++i) |
| 2479 |
glslangOperands[i]->traverse(this); |
| 2480 |
|
| 2481 |
return false; |
| 2482 |
} |
| 2483 |
case glslang::EOpFunction: |
| 2484 |
if (visit == glslang::EvPreVisit) { |
| 2485 |
if (isShaderEntryPoint(node)) { |
| 2486 |
inEntryPoint = true; |
| 2487 |
builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 2488 |
currentFunction = shaderEntry; |
| 2489 |
} else { |
| 2490 |
handleFunctionEntry(node); |
| 2491 |
} |
| 2492 |
} else { |
| 2493 |
if (inEntryPoint) |
| 2494 |
entryPointTerminated = true; |
| 2495 |
builder.leaveFunction(); |
| 2496 |
inEntryPoint = false; |
| 2497 |
} |
| 2498 |
|
| 2499 |
return true; |
| 2500 |
case glslang::EOpParameters: |
| 2501 |
// Parameters will have been consumed by EOpFunction processing, but not |
| 2502 |
// the body, so we still visited the function node's children, making this |
| 2503 |
// child redundant. |
| 2504 |
return false; |
| 2505 |
case glslang::EOpFunctionCall: |
| 2506 |
{ |
| 2507 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 2508 |
if (node->isUserDefined()) |
| 2509 |
result = handleUserFunctionCall(node); |
| 2510 |
if (result) { |
| 2511 |
builder.clearAccessChain(); |
| 2512 |
builder.setAccessChainRValue(result); |
| 2513 |
} else |
| 2514 |
logger->missingFunctionality("missing user function; linker needs to catch that"); |
| 2515 |
|
| 2516 |
return false; |
| 2517 |
} |
| 2518 |
case glslang::EOpConstructMat2x2: |
| 2519 |
case glslang::EOpConstructMat2x3: |
| 2520 |
case glslang::EOpConstructMat2x4: |
| 2521 |
case glslang::EOpConstructMat3x2: |
| 2522 |
case glslang::EOpConstructMat3x3: |
| 2523 |
case glslang::EOpConstructMat3x4: |
| 2524 |
case glslang::EOpConstructMat4x2: |
| 2525 |
case glslang::EOpConstructMat4x3: |
| 2526 |
case glslang::EOpConstructMat4x4: |
| 2527 |
case glslang::EOpConstructDMat2x2: |
| 2528 |
case glslang::EOpConstructDMat2x3: |
| 2529 |
case glslang::EOpConstructDMat2x4: |
| 2530 |
case glslang::EOpConstructDMat3x2: |
| 2531 |
case glslang::EOpConstructDMat3x3: |
| 2532 |
case glslang::EOpConstructDMat3x4: |
| 2533 |
case glslang::EOpConstructDMat4x2: |
| 2534 |
case glslang::EOpConstructDMat4x3: |
| 2535 |
case glslang::EOpConstructDMat4x4: |
| 2536 |
case glslang::EOpConstructIMat2x2: |
| 2537 |
case glslang::EOpConstructIMat2x3: |
| 2538 |
case glslang::EOpConstructIMat2x4: |
| 2539 |
case glslang::EOpConstructIMat3x2: |
| 2540 |
case glslang::EOpConstructIMat3x3: |
| 2541 |
case glslang::EOpConstructIMat3x4: |
| 2542 |
case glslang::EOpConstructIMat4x2: |
| 2543 |
case glslang::EOpConstructIMat4x3: |
| 2544 |
case glslang::EOpConstructIMat4x4: |
| 2545 |
case glslang::EOpConstructUMat2x2: |
| 2546 |
case glslang::EOpConstructUMat2x3: |
| 2547 |
case glslang::EOpConstructUMat2x4: |
| 2548 |
case glslang::EOpConstructUMat3x2: |
| 2549 |
case glslang::EOpConstructUMat3x3: |
| 2550 |
case glslang::EOpConstructUMat3x4: |
| 2551 |
case glslang::EOpConstructUMat4x2: |
| 2552 |
case glslang::EOpConstructUMat4x3: |
| 2553 |
case glslang::EOpConstructUMat4x4: |
| 2554 |
case glslang::EOpConstructBMat2x2: |
| 2555 |
case glslang::EOpConstructBMat2x3: |
| 2556 |
case glslang::EOpConstructBMat2x4: |
| 2557 |
case glslang::EOpConstructBMat3x2: |
| 2558 |
case glslang::EOpConstructBMat3x3: |
| 2559 |
case glslang::EOpConstructBMat3x4: |
| 2560 |
case glslang::EOpConstructBMat4x2: |
| 2561 |
case glslang::EOpConstructBMat4x3: |
| 2562 |
case glslang::EOpConstructBMat4x4: |
| 2563 |
case glslang::EOpConstructF16Mat2x2: |
| 2564 |
case glslang::EOpConstructF16Mat2x3: |
| 2565 |
case glslang::EOpConstructF16Mat2x4: |
| 2566 |
case glslang::EOpConstructF16Mat3x2: |
| 2567 |
case glslang::EOpConstructF16Mat3x3: |
| 2568 |
case glslang::EOpConstructF16Mat3x4: |
| 2569 |
case glslang::EOpConstructF16Mat4x2: |
| 2570 |
case glslang::EOpConstructF16Mat4x3: |
| 2571 |
case glslang::EOpConstructF16Mat4x4: |
| 2572 |
isMatrix = true; |
| 2573 |
// fall through |
| 2574 |
case glslang::EOpConstructFloat: |
| 2575 |
case glslang::EOpConstructVec2: |
| 2576 |
case glslang::EOpConstructVec3: |
| 2577 |
case glslang::EOpConstructVec4: |
| 2578 |
case glslang::EOpConstructDouble: |
| 2579 |
case glslang::EOpConstructDVec2: |
| 2580 |
case glslang::EOpConstructDVec3: |
| 2581 |
case glslang::EOpConstructDVec4: |
| 2582 |
case glslang::EOpConstructFloat16: |
| 2583 |
case glslang::EOpConstructF16Vec2: |
| 2584 |
case glslang::EOpConstructF16Vec3: |
| 2585 |
case glslang::EOpConstructF16Vec4: |
| 2586 |
case glslang::EOpConstructBool: |
| 2587 |
case glslang::EOpConstructBVec2: |
| 2588 |
case glslang::EOpConstructBVec3: |
| 2589 |
case glslang::EOpConstructBVec4: |
| 2590 |
case glslang::EOpConstructInt8: |
| 2591 |
case glslang::EOpConstructI8Vec2: |
| 2592 |
case glslang::EOpConstructI8Vec3: |
| 2593 |
case glslang::EOpConstructI8Vec4: |
| 2594 |
case glslang::EOpConstructUint8: |
| 2595 |
case glslang::EOpConstructU8Vec2: |
| 2596 |
case glslang::EOpConstructU8Vec3: |
| 2597 |
case glslang::EOpConstructU8Vec4: |
| 2598 |
case glslang::EOpConstructInt16: |
| 2599 |
case glslang::EOpConstructI16Vec2: |
| 2600 |
case glslang::EOpConstructI16Vec3: |
| 2601 |
case glslang::EOpConstructI16Vec4: |
| 2602 |
case glslang::EOpConstructUint16: |
| 2603 |
case glslang::EOpConstructU16Vec2: |
| 2604 |
case glslang::EOpConstructU16Vec3: |
| 2605 |
case glslang::EOpConstructU16Vec4: |
| 2606 |
case glslang::EOpConstructInt: |
| 2607 |
case glslang::EOpConstructIVec2: |
| 2608 |
case glslang::EOpConstructIVec3: |
| 2609 |
case glslang::EOpConstructIVec4: |
| 2610 |
case glslang::EOpConstructUint: |
| 2611 |
case glslang::EOpConstructUVec2: |
| 2612 |
case glslang::EOpConstructUVec3: |
| 2613 |
case glslang::EOpConstructUVec4: |
| 2614 |
case glslang::EOpConstructInt64: |
| 2615 |
case glslang::EOpConstructI64Vec2: |
| 2616 |
case glslang::EOpConstructI64Vec3: |
| 2617 |
case glslang::EOpConstructI64Vec4: |
| 2618 |
case glslang::EOpConstructUint64: |
| 2619 |
case glslang::EOpConstructU64Vec2: |
| 2620 |
case glslang::EOpConstructU64Vec3: |
| 2621 |
case glslang::EOpConstructU64Vec4: |
| 2622 |
case glslang::EOpConstructStruct: |
| 2623 |
case glslang::EOpConstructTextureSampler: |
| 2624 |
case glslang::EOpConstructReference: |
| 2625 |
case glslang::EOpConstructCooperativeMatrix: |
| 2626 |
{ |
| 2627 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 2628 |
std::vector<spv::Id> arguments; |
| 2629 |
translateArguments(*node, arguments, lvalueCoherentFlags); |
| 2630 |
spv::Id constructed; |
| 2631 |
if (node->getOp() == glslang::EOpConstructTextureSampler) |
| 2632 |
constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); |
| 2633 |
else if (node->getOp() == glslang::EOpConstructStruct || |
| 2634 |
node->getOp() == glslang::EOpConstructCooperativeMatrix || |
| 2635 |
node->getType().isArray()) { |
| 2636 |
std::vector<spv::Id> constituents; |
| 2637 |
for (int c = 0; c < (int)arguments.size(); ++c) |
| 2638 |
constituents.push_back(arguments[c]); |
| 2639 |
constructed = createCompositeConstruct(resultType(), constituents); |
| 2640 |
} else if (isMatrix) |
| 2641 |
constructed = builder.createMatrixConstructor(precision, arguments, resultType()); |
| 2642 |
else |
| 2643 |
constructed = builder.createConstructor(precision, arguments, resultType()); |
| 2644 |
|
| 2645 |
if (node->getType().getQualifier().isNonUniform()) { |
| 2646 |
builder.addDecoration(constructed, spv::DecorationNonUniformEXT); |
| 2647 |
} |
| 2648 |
|
| 2649 |
builder.clearAccessChain(); |
| 2650 |
builder.setAccessChainRValue(constructed); |
| 2651 |
|
| 2652 |
return false; |
| 2653 |
} |
| 2654 |
|
| 2655 |
// These six are component-wise compares with component-wise results. |
| 2656 |
// Forward on to createBinaryOperation(), requesting a vector result. |
| 2657 |
case glslang::EOpLessThan: |
| 2658 |
case glslang::EOpGreaterThan: |
| 2659 |
case glslang::EOpLessThanEqual: |
| 2660 |
case glslang::EOpGreaterThanEqual: |
| 2661 |
case glslang::EOpVectorEqual: |
| 2662 |
case glslang::EOpVectorNotEqual: |
| 2663 |
{ |
| 2664 |
// Map the operation to a binary |
| 2665 |
binOp = node->getOp(); |
| 2666 |
reduceComparison = false; |
| 2667 |
switch (node->getOp()) { |
| 2668 |
case glslang::EOpVectorEqual: binOp = glslang::EOpVectorEqual; break; |
| 2669 |
case glslang::EOpVectorNotEqual: binOp = glslang::EOpVectorNotEqual; break; |
| 2670 |
default: binOp = node->getOp(); break; |
| 2671 |
} |
| 2672 |
|
| 2673 |
break; |
| 2674 |
} |
| 2675 |
case glslang::EOpMul: |
| 2676 |
// component-wise matrix multiply |
| 2677 |
binOp = glslang::EOpMul; |
| 2678 |
break; |
| 2679 |
case glslang::EOpOuterProduct: |
| 2680 |
// two vectors multiplied to make a matrix |
| 2681 |
binOp = glslang::EOpOuterProduct; |
| 2682 |
break; |
| 2683 |
case glslang::EOpDot: |
| 2684 |
{ |
| 2685 |
// for scalar dot product, use multiply |
| 2686 |
glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2687 |
if (glslangOperands[0]->getAsTyped()->getVectorSize() == 1) |
| 2688 |
binOp = glslang::EOpMul; |
| 2689 |
break; |
| 2690 |
} |
| 2691 |
case glslang::EOpMod: |
| 2692 |
// when an aggregate, this is the floating-point mod built-in function, |
| 2693 |
// which can be emitted by the one in createBinaryOperation() |
| 2694 |
binOp = glslang::EOpMod; |
| 2695 |
break; |
| 2696 |
|
| 2697 |
case glslang::EOpEmitVertex: |
| 2698 |
case glslang::EOpEndPrimitive: |
| 2699 |
case glslang::EOpBarrier: |
| 2700 |
case glslang::EOpMemoryBarrier: |
| 2701 |
case glslang::EOpMemoryBarrierAtomicCounter: |
| 2702 |
case glslang::EOpMemoryBarrierBuffer: |
| 2703 |
case glslang::EOpMemoryBarrierImage: |
| 2704 |
case glslang::EOpMemoryBarrierShared: |
| 2705 |
case glslang::EOpGroupMemoryBarrier: |
| 2706 |
case glslang::EOpDeviceMemoryBarrier: |
| 2707 |
case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 2708 |
case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 2709 |
case glslang::EOpWorkgroupMemoryBarrier: |
| 2710 |
case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
| 2711 |
case glslang::EOpSubgroupBarrier: |
| 2712 |
case glslang::EOpSubgroupMemoryBarrier: |
| 2713 |
case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 2714 |
case glslang::EOpSubgroupMemoryBarrierImage: |
| 2715 |
case glslang::EOpSubgroupMemoryBarrierShared: |
| 2716 |
noReturnValue = true; |
| 2717 |
// These all have 0 operands and will naturally finish up in the code below for 0 operands |
| 2718 |
break; |
| 2719 |
|
| 2720 |
case glslang::EOpAtomicAdd: |
| 2721 |
case glslang::EOpAtomicMin: |
| 2722 |
case glslang::EOpAtomicMax: |
| 2723 |
case glslang::EOpAtomicAnd: |
| 2724 |
case glslang::EOpAtomicOr: |
| 2725 |
case glslang::EOpAtomicXor: |
| 2726 |
case glslang::EOpAtomicExchange: |
| 2727 |
case glslang::EOpAtomicCompSwap: |
| 2728 |
atomic = true; |
| 2729 |
break; |
| 2730 |
|
| 2731 |
#ifndef GLSLANG_WEB |
| 2732 |
case glslang::EOpAtomicStore: |
| 2733 |
noReturnValue = true; |
| 2734 |
// fallthrough |
| 2735 |
case glslang::EOpAtomicLoad: |
| 2736 |
atomic = true; |
| 2737 |
break; |
| 2738 |
|
| 2739 |
case glslang::EOpAtomicCounterAdd: |
| 2740 |
case glslang::EOpAtomicCounterSubtract: |
| 2741 |
case glslang::EOpAtomicCounterMin: |
| 2742 |
case glslang::EOpAtomicCounterMax: |
| 2743 |
case glslang::EOpAtomicCounterAnd: |
| 2744 |
case glslang::EOpAtomicCounterOr: |
| 2745 |
case glslang::EOpAtomicCounterXor: |
| 2746 |
case glslang::EOpAtomicCounterExchange: |
| 2747 |
case glslang::EOpAtomicCounterCompSwap: |
| 2748 |
builder.addExtension("SPV_KHR_shader_atomic_counter_ops"); |
| 2749 |
builder.addCapability(spv::CapabilityAtomicStorageOps); |
| 2750 |
atomic = true; |
| 2751 |
break; |
| 2752 |
|
| 2753 |
case glslang::EOpAbsDifference: |
| 2754 |
case glslang::EOpAddSaturate: |
| 2755 |
case glslang::EOpSubSaturate: |
| 2756 |
case glslang::EOpAverage: |
| 2757 |
case glslang::EOpAverageRounded: |
| 2758 |
case glslang::EOpMul32x16: |
| 2759 |
builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 2760 |
builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 2761 |
binOp = node->getOp(); |
| 2762 |
break; |
| 2763 |
|
| 2764 |
case glslang::EOpIgnoreIntersection: |
| 2765 |
case glslang::EOpTerminateRay: |
| 2766 |
case glslang::EOpTrace: |
| 2767 |
case glslang::EOpExecuteCallable: |
| 2768 |
case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 2769 |
noReturnValue = true; |
| 2770 |
break; |
| 2771 |
case glslang::EOpRayQueryInitialize: |
| 2772 |
case glslang::EOpRayQueryTerminate: |
| 2773 |
case glslang::EOpRayQueryGenerateIntersection: |
| 2774 |
case glslang::EOpRayQueryConfirmIntersection: |
| 2775 |
builder.addExtension("SPV_KHR_ray_query"); |
| 2776 |
builder.addCapability(spv::CapabilityRayQueryProvisionalKHR); |
| 2777 |
noReturnValue = true; |
| 2778 |
break; |
| 2779 |
case glslang::EOpRayQueryProceed: |
| 2780 |
case glslang::EOpRayQueryGetIntersectionType: |
| 2781 |
case glslang::EOpRayQueryGetRayTMin: |
| 2782 |
case glslang::EOpRayQueryGetRayFlags: |
| 2783 |
case glslang::EOpRayQueryGetIntersectionT: |
| 2784 |
case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 2785 |
case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 2786 |
case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 2787 |
case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 2788 |
case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 2789 |
case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 2790 |
case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 2791 |
case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 2792 |
case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 2793 |
case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 2794 |
case glslang::EOpRayQueryGetWorldRayDirection: |
| 2795 |
case glslang::EOpRayQueryGetWorldRayOrigin: |
| 2796 |
case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 2797 |
case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 2798 |
builder.addExtension("SPV_KHR_ray_query"); |
| 2799 |
builder.addCapability(spv::CapabilityRayQueryProvisionalKHR); |
| 2800 |
break; |
| 2801 |
case glslang::EOpCooperativeMatrixLoad: |
| 2802 |
case glslang::EOpCooperativeMatrixStore: |
| 2803 |
noReturnValue = true; |
| 2804 |
break; |
| 2805 |
case glslang::EOpBeginInvocationInterlock: |
| 2806 |
case glslang::EOpEndInvocationInterlock: |
| 2807 |
builder.addExtension(spv::E_SPV_EXT_fragment_shader_interlock); |
| 2808 |
noReturnValue = true; |
| 2809 |
break; |
| 2810 |
#endif |
| 2811 |
|
| 2812 |
case glslang::EOpDebugPrintf: |
| 2813 |
noReturnValue = true; |
| 2814 |
break; |
| 2815 |
|
| 2816 |
default: |
| 2817 |
break; |
| 2818 |
} |
| 2819 |
|
| 2820 |
// |
| 2821 |
// See if it maps to a regular operation. |
| 2822 |
// |
| 2823 |
if (binOp != glslang::EOpNull) { |
| 2824 |
glslang::TIntermTyped* left = node->getSequence()[0]->getAsTyped(); |
| 2825 |
glslang::TIntermTyped* right = node->getSequence()[1]->getAsTyped(); |
| 2826 |
assert(left && right); |
| 2827 |
|
| 2828 |
builder.clearAccessChain(); |
| 2829 |
left->traverse(this); |
| 2830 |
spv::Id leftId = accessChainLoad(left->getType()); |
| 2831 |
|
| 2832 |
builder.clearAccessChain(); |
| 2833 |
right->traverse(this); |
| 2834 |
spv::Id rightId = accessChainLoad(right->getType()); |
| 2835 |
|
| 2836 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 2837 |
OpDecorations decorations = { precision, |
| 2838 |
TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 2839 |
TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 2840 |
result = createBinaryOperation(binOp, decorations, |
| 2841 |
resultType(), leftId, rightId, |
| 2842 |
left->getType().getBasicType(), reduceComparison); |
| 2843 |
|
| 2844 |
// code above should only make binOp that exists in createBinaryOperation |
| 2845 |
assert(result != spv::NoResult); |
| 2846 |
builder.clearAccessChain(); |
| 2847 |
builder.setAccessChainRValue(result); |
| 2848 |
|
| 2849 |
return false; |
| 2850 |
} |
| 2851 |
|
| 2852 |
// |
| 2853 |
// Create the list of operands. |
| 2854 |
// |
| 2855 |
glslang::TIntermSequence& glslangOperands = node->getSequence(); |
| 2856 |
std::vector<spv::Id> operands; |
| 2857 |
std::vector<spv::IdImmediate> memoryAccessOperands; |
| 2858 |
for (int arg = 0; arg < (int)glslangOperands.size(); ++arg) { |
| 2859 |
// special case l-value operands; there are just a few |
| 2860 |
bool lvalue = false; |
| 2861 |
switch (node->getOp()) { |
| 2862 |
case glslang::EOpModf: |
| 2863 |
if (arg == 1) |
| 2864 |
lvalue = true; |
| 2865 |
break; |
| 2866 |
|
| 2867 |
case glslang::EOpRayQueryInitialize: |
| 2868 |
case glslang::EOpRayQueryTerminate: |
| 2869 |
case glslang::EOpRayQueryConfirmIntersection: |
| 2870 |
case glslang::EOpRayQueryProceed: |
| 2871 |
case glslang::EOpRayQueryGenerateIntersection: |
| 2872 |
case glslang::EOpRayQueryGetIntersectionType: |
| 2873 |
case glslang::EOpRayQueryGetIntersectionT: |
| 2874 |
case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 2875 |
case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 2876 |
case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 2877 |
case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 2878 |
case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 2879 |
case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 2880 |
case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 2881 |
case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 2882 |
case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 2883 |
case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 2884 |
case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 2885 |
if (arg == 0) |
| 2886 |
lvalue = true; |
| 2887 |
break; |
| 2888 |
|
| 2889 |
case glslang::EOpAtomicAdd: |
| 2890 |
case glslang::EOpAtomicMin: |
| 2891 |
case glslang::EOpAtomicMax: |
| 2892 |
case glslang::EOpAtomicAnd: |
| 2893 |
case glslang::EOpAtomicOr: |
| 2894 |
case glslang::EOpAtomicXor: |
| 2895 |
case glslang::EOpAtomicExchange: |
| 2896 |
case glslang::EOpAtomicCompSwap: |
| 2897 |
if (arg == 0) |
| 2898 |
lvalue = true; |
| 2899 |
break; |
| 2900 |
|
| 2901 |
#ifndef GLSLANG_WEB |
| 2902 |
case glslang::EOpFrexp: |
| 2903 |
if (arg == 1) |
| 2904 |
lvalue = true; |
| 2905 |
break; |
| 2906 |
case glslang::EOpInterpolateAtSample: |
| 2907 |
case glslang::EOpInterpolateAtOffset: |
| 2908 |
case glslang::EOpInterpolateAtVertex: |
| 2909 |
if (arg == 0) { |
| 2910 |
lvalue = true; |
| 2911 |
|
| 2912 |
// Does it need a swizzle inversion? If so, evaluation is inverted; |
| 2913 |
// operate first on the swizzle base, then apply the swizzle. |
| 2914 |
// That is, we transform |
| 2915 |
// |
| 2916 |
// interpolate(v.zy) -> interpolate(v).zy |
| 2917 |
// |
| 2918 |
if (glslangOperands[0]->getAsOperator() && |
| 2919 |
glslangOperands[0]->getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 2920 |
invertedType = convertGlslangToSpvType( |
| 2921 |
glslangOperands[0]->getAsBinaryNode()->getLeft()->getType()); |
| 2922 |
} |
| 2923 |
break; |
| 2924 |
case glslang::EOpAtomicLoad: |
| 2925 |
case glslang::EOpAtomicStore: |
| 2926 |
case glslang::EOpAtomicCounterAdd: |
| 2927 |
case glslang::EOpAtomicCounterSubtract: |
| 2928 |
case glslang::EOpAtomicCounterMin: |
| 2929 |
case glslang::EOpAtomicCounterMax: |
| 2930 |
case glslang::EOpAtomicCounterAnd: |
| 2931 |
case glslang::EOpAtomicCounterOr: |
| 2932 |
case glslang::EOpAtomicCounterXor: |
| 2933 |
case glslang::EOpAtomicCounterExchange: |
| 2934 |
case glslang::EOpAtomicCounterCompSwap: |
| 2935 |
if (arg == 0) |
| 2936 |
lvalue = true; |
| 2937 |
break; |
| 2938 |
case glslang::EOpAddCarry: |
| 2939 |
case glslang::EOpSubBorrow: |
| 2940 |
if (arg == 2) |
| 2941 |
lvalue = true; |
| 2942 |
break; |
| 2943 |
case glslang::EOpUMulExtended: |
| 2944 |
case glslang::EOpIMulExtended: |
| 2945 |
if (arg >= 2) |
| 2946 |
lvalue = true; |
| 2947 |
break; |
| 2948 |
case glslang::EOpCooperativeMatrixLoad: |
| 2949 |
if (arg == 0 || arg == 1) |
| 2950 |
lvalue = true; |
| 2951 |
break; |
| 2952 |
case glslang::EOpCooperativeMatrixStore: |
| 2953 |
if (arg == 1) |
| 2954 |
lvalue = true; |
| 2955 |
break; |
| 2956 |
#endif |
| 2957 |
default: |
| 2958 |
break; |
| 2959 |
} |
| 2960 |
builder.clearAccessChain(); |
| 2961 |
if (invertedType != spv::NoType && arg == 0) |
| 2962 |
glslangOperands[0]->getAsBinaryNode()->getLeft()->traverse(this); |
| 2963 |
else |
| 2964 |
glslangOperands[arg]->traverse(this); |
| 2965 |
|
| 2966 |
#ifndef GLSLANG_WEB |
| 2967 |
if (node->getOp() == glslang::EOpCooperativeMatrixLoad || |
| 2968 |
node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 2969 |
|
| 2970 |
if (arg == 1) { |
| 2971 |
// fold "element" parameter into the access chain |
| 2972 |
spv::Builder::AccessChain save = builder.getAccessChain(); |
| 2973 |
builder.clearAccessChain(); |
| 2974 |
glslangOperands[2]->traverse(this); |
| 2975 |
|
| 2976 |
spv::Id elementId = accessChainLoad(glslangOperands[2]->getAsTyped()->getType()); |
| 2977 |
|
| 2978 |
builder.setAccessChain(save); |
| 2979 |
|
| 2980 |
// Point to the first element of the array. |
| 2981 |
builder.accessChainPush(elementId, |
| 2982 |
TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()), |
| 2983 |
glslangOperands[arg]->getAsTyped()->getType().getBufferReferenceAlignment()); |
| 2984 |
|
| 2985 |
spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 2986 |
unsigned int alignment = builder.getAccessChain().alignment; |
| 2987 |
|
| 2988 |
int memoryAccess = TranslateMemoryAccess(coherentFlags); |
| 2989 |
if (node->getOp() == glslang::EOpCooperativeMatrixLoad) |
| 2990 |
memoryAccess &= ~spv::MemoryAccessMakePointerAvailableKHRMask; |
| 2991 |
if (node->getOp() == glslang::EOpCooperativeMatrixStore) |
| 2992 |
memoryAccess &= ~spv::MemoryAccessMakePointerVisibleKHRMask; |
| 2993 |
if (builder.getStorageClass(builder.getAccessChain().base) == |
| 2994 |
spv::StorageClassPhysicalStorageBufferEXT) { |
| 2995 |
memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask); |
| 2996 |
} |
| 2997 |
|
| 2998 |
memoryAccessOperands.push_back(spv::IdImmediate(false, memoryAccess)); |
| 2999 |
|
| 3000 |
if (memoryAccess & spv::MemoryAccessAlignedMask) { |
| 3001 |
memoryAccessOperands.push_back(spv::IdImmediate(false, alignment)); |
| 3002 |
} |
| 3003 |
|
| 3004 |
if (memoryAccess & |
| 3005 |
(spv::MemoryAccessMakePointerAvailableKHRMask | spv::MemoryAccessMakePointerVisibleKHRMask)) { |
| 3006 |
memoryAccessOperands.push_back(spv::IdImmediate(true, |
| 3007 |
builder.makeUintConstant(TranslateMemoryScope(coherentFlags)))); |
| 3008 |
} |
| 3009 |
} else if (arg == 2) { |
| 3010 |
continue; |
| 3011 |
} |
| 3012 |
} |
| 3013 |
#endif |
| 3014 |
|
| 3015 |
// for l-values, pass the address, for r-values, pass the value |
| 3016 |
if (lvalue) { |
| 3017 |
if (invertedType == spv::NoType && !builder.isSpvLvalue()) { |
| 3018 |
// SPIR-V cannot represent an l-value containing a swizzle that doesn't |
| 3019 |
// reduce to a simple access chain. So, we need a temporary vector to |
| 3020 |
// receive the result, and must later swizzle that into the original |
| 3021 |
// l-value. |
| 3022 |
complexLvalues.push_back(builder.getAccessChain()); |
| 3023 |
complexLValueQualifiers.push_back(glslangOperands[arg]->getAsTyped()->getType().getQualifier()); |
| 3024 |
temporaryLvalues.push_back(builder.createVariable( |
| 3025 |
spv::NoPrecision, spv::StorageClassFunction, |
| 3026 |
builder.accessChainGetInferredType(), "swizzleTemp")); |
| 3027 |
operands.push_back(temporaryLvalues.back()); |
| 3028 |
} else { |
| 3029 |
operands.push_back(builder.accessChainGetLValue()); |
| 3030 |
} |
| 3031 |
lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 3032 |
lvalueCoherentFlags |= TranslateCoherent(glslangOperands[arg]->getAsTyped()->getType()); |
| 3033 |
} else { |
| 3034 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3035 |
glslang::TOperator glslangOp = node->getOp(); |
| 3036 |
if (arg == 1 && |
| 3037 |
(glslangOp == glslang::EOpRayQueryGetIntersectionType || |
| 3038 |
glslangOp == glslang::EOpRayQueryGetIntersectionT || |
| 3039 |
glslangOp == glslang::EOpRayQueryGetIntersectionInstanceCustomIndex || |
| 3040 |
glslangOp == glslang::EOpRayQueryGetIntersectionInstanceId || |
| 3041 |
glslangOp == glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset || |
| 3042 |
glslangOp == glslang::EOpRayQueryGetIntersectionGeometryIndex || |
| 3043 |
glslangOp == glslang::EOpRayQueryGetIntersectionPrimitiveIndex || |
| 3044 |
glslangOp == glslang::EOpRayQueryGetIntersectionBarycentrics || |
| 3045 |
glslangOp == glslang::EOpRayQueryGetIntersectionFrontFace || |
| 3046 |
glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayDirection || |
| 3047 |
glslangOp == glslang::EOpRayQueryGetIntersectionObjectRayOrigin || |
| 3048 |
glslangOp == glslang::EOpRayQueryGetIntersectionObjectToWorld || |
| 3049 |
glslangOp == glslang::EOpRayQueryGetIntersectionWorldToObject |
| 3050 |
)) { |
| 3051 |
bool cond = glslangOperands[arg]->getAsConstantUnion()->getConstArray()[0].getBConst(); |
| 3052 |
operands.push_back(builder.makeIntConstant(cond ? 1 : 0)); |
| 3053 |
} |
| 3054 |
else { |
| 3055 |
operands.push_back(accessChainLoad(glslangOperands[arg]->getAsTyped()->getType())); |
| 3056 |
} |
| 3057 |
|
| 3058 |
} |
| 3059 |
} |
| 3060 |
|
| 3061 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3062 |
#ifndef GLSLANG_WEB |
| 3063 |
if (node->getOp() == glslang::EOpCooperativeMatrixLoad) { |
| 3064 |
std::vector<spv::IdImmediate> idImmOps; |
| 3065 |
|
| 3066 |
idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3067 |
idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3068 |
idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3069 |
idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3070 |
// get the pointee type |
| 3071 |
spv::Id typeId = builder.getContainedTypeId(builder.getTypeId(operands[0])); |
| 3072 |
assert(builder.isCooperativeMatrixType(typeId)); |
| 3073 |
// do the op |
| 3074 |
spv::Id result = builder.createOp(spv::OpCooperativeMatrixLoadNV, typeId, idImmOps); |
| 3075 |
// store the result to the pointer (out param 'm') |
| 3076 |
builder.createStore(result, operands[0]); |
| 3077 |
result = 0; |
| 3078 |
} else if (node->getOp() == glslang::EOpCooperativeMatrixStore) { |
| 3079 |
std::vector<spv::IdImmediate> idImmOps; |
| 3080 |
|
| 3081 |
idImmOps.push_back(spv::IdImmediate(true, operands[1])); // buf |
| 3082 |
idImmOps.push_back(spv::IdImmediate(true, operands[0])); // object |
| 3083 |
idImmOps.push_back(spv::IdImmediate(true, operands[2])); // stride |
| 3084 |
idImmOps.push_back(spv::IdImmediate(true, operands[3])); // colMajor |
| 3085 |
idImmOps.insert(idImmOps.end(), memoryAccessOperands.begin(), memoryAccessOperands.end()); |
| 3086 |
|
| 3087 |
builder.createNoResultOp(spv::OpCooperativeMatrixStoreNV, idImmOps); |
| 3088 |
result = 0; |
| 3089 |
} else |
| 3090 |
#endif |
| 3091 |
if (atomic) { |
| 3092 |
// Handle all atomics |
| 3093 |
result = createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), |
| 3094 |
lvalueCoherentFlags); |
| 3095 |
} else if (node->getOp() == glslang::EOpDebugPrintf) { |
| 3096 |
if (!nonSemanticDebugPrintf) { |
| 3097 |
nonSemanticDebugPrintf = builder.import("NonSemantic.DebugPrintf"); |
| 3098 |
} |
| 3099 |
result = builder.createBuiltinCall(builder.makeVoidType(), nonSemanticDebugPrintf, spv::NonSemanticDebugPrintfDebugPrintf, operands); |
| 3100 |
builder.addExtension(spv::E_SPV_KHR_non_semantic_info); |
| 3101 |
} else { |
| 3102 |
// Pass through to generic operations. |
| 3103 |
switch (glslangOperands.size()) { |
| 3104 |
case 0: |
| 3105 |
result = createNoArgOperation(node->getOp(), precision, resultType()); |
| 3106 |
break; |
| 3107 |
case 1: |
| 3108 |
{ |
| 3109 |
OpDecorations decorations = { precision, |
| 3110 |
TranslateNoContractionDecoration(node->getType().getQualifier()), |
| 3111 |
TranslateNonUniformDecoration(node->getType().getQualifier()) }; |
| 3112 |
result = createUnaryOperation( |
| 3113 |
node->getOp(), decorations, |
| 3114 |
resultType(), operands.front(), |
| 3115 |
glslangOperands[0]->getAsTyped()->getBasicType(), lvalueCoherentFlags); |
| 3116 |
} |
| 3117 |
break; |
| 3118 |
default: |
| 3119 |
result = createMiscOperation(node->getOp(), precision, resultType(), operands, node->getBasicType()); |
| 3120 |
break; |
| 3121 |
} |
| 3122 |
|
| 3123 |
if (invertedType != spv::NoResult) |
| 3124 |
result = createInvertedSwizzle(precision, *glslangOperands[0]->getAsBinaryNode(), result); |
| 3125 |
|
| 3126 |
for (unsigned int i = 0; i < temporaryLvalues.size(); ++i) { |
| 3127 |
builder.setAccessChain(complexLvalues[i]); |
| 3128 |
builder.accessChainStore(builder.createLoad(temporaryLvalues[i], spv::NoPrecision), TranslateNonUniformDecoration(complexLValueQualifiers[i])); |
| 3129 |
} |
| 3130 |
} |
| 3131 |
|
| 3132 |
if (noReturnValue) |
| 3133 |
return false; |
| 3134 |
|
| 3135 |
if (! result) { |
| 3136 |
logger->missingFunctionality("unknown glslang aggregate"); |
| 3137 |
return true; // pick up a child as a placeholder operand |
| 3138 |
} else { |
| 3139 |
builder.clearAccessChain(); |
| 3140 |
builder.setAccessChainRValue(result); |
| 3141 |
return false; |
| 3142 |
} |
| 3143 |
} |
| 3144 |
|
| 3145 |
// This path handles both if-then-else and ?: |
| 3146 |
// The if-then-else has a node type of void, while |
| 3147 |
// ?: has either a void or a non-void node type |
| 3148 |
// |
| 3149 |
// Leaving the result, when not void: |
| 3150 |
// GLSL only has r-values as the result of a :?, but |
| 3151 |
// if we have an l-value, that can be more efficient if it will |
| 3152 |
// become the base of a complex r-value expression, because the |
| 3153 |
// next layer copies r-values into memory to use the access-chain mechanism |
| 3154 |
bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit /* visit */, glslang::TIntermSelection* node) |
| 3155 |
{ |
| 3156 |
// see if OpSelect can handle it |
| 3157 |
const auto isOpSelectable = [&]() { |
| 3158 |
if (node->getBasicType() == glslang::EbtVoid) |
| 3159 |
return false; |
| 3160 |
// OpSelect can do all other types starting with SPV 1.4 |
| 3161 |
if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4) { |
| 3162 |
// pre-1.4, only scalars and vectors can be handled |
| 3163 |
if ((!node->getType().isScalar() && !node->getType().isVector())) |
| 3164 |
return false; |
| 3165 |
} |
| 3166 |
return true; |
| 3167 |
}; |
| 3168 |
|
| 3169 |
// See if it simple and safe, or required, to execute both sides. |
| 3170 |
// Crucially, side effects must be either semantically required or avoided, |
| 3171 |
// and there are performance trade-offs. |
| 3172 |
// Return true if required or a good idea (and safe) to execute both sides, |
| 3173 |
// false otherwise. |
| 3174 |
const auto bothSidesPolicy = [&]() -> bool { |
| 3175 |
// do we have both sides? |
| 3176 |
if (node->getTrueBlock() == nullptr || |
| 3177 |
node->getFalseBlock() == nullptr) |
| 3178 |
return false; |
| 3179 |
|
| 3180 |
// required? (unless we write additional code to look for side effects |
| 3181 |
// and make performance trade-offs if none are present) |
| 3182 |
if (!node->getShortCircuit()) |
| 3183 |
return true; |
| 3184 |
|
| 3185 |
// if not required to execute both, decide based on performance/practicality... |
| 3186 |
|
| 3187 |
if (!isOpSelectable()) |
| 3188 |
return false; |
| 3189 |
|
| 3190 |
assert(node->getType() == node->getTrueBlock() ->getAsTyped()->getType() && |
| 3191 |
node->getType() == node->getFalseBlock()->getAsTyped()->getType()); |
| 3192 |
|
| 3193 |
// return true if a single operand to ? : is okay for OpSelect |
| 3194 |
const auto operandOkay = [](glslang::TIntermTyped* node) { |
| 3195 |
return node->getAsSymbolNode() || node->getType().getQualifier().isConstant(); |
| 3196 |
}; |
| 3197 |
|
| 3198 |
return operandOkay(node->getTrueBlock() ->getAsTyped()) && |
| 3199 |
operandOkay(node->getFalseBlock()->getAsTyped()); |
| 3200 |
}; |
| 3201 |
|
| 3202 |
spv::Id result = spv::NoResult; // upcoming result selecting between trueValue and falseValue |
| 3203 |
// emit the condition before doing anything with selection |
| 3204 |
node->getCondition()->traverse(this); |
| 3205 |
spv::Id condition = accessChainLoad(node->getCondition()->getType()); |
| 3206 |
|
| 3207 |
// Find a way of executing both sides and selecting the right result. |
| 3208 |
const auto executeBothSides = [&]() -> void { |
| 3209 |
// execute both sides |
| 3210 |
node->getTrueBlock()->traverse(this); |
| 3211 |
spv::Id trueValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3212 |
node->getFalseBlock()->traverse(this); |
| 3213 |
spv::Id falseValue = accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()); |
| 3214 |
|
| 3215 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3216 |
|
| 3217 |
// done if void |
| 3218 |
if (node->getBasicType() == glslang::EbtVoid) |
| 3219 |
return; |
| 3220 |
|
| 3221 |
// emit code to select between trueValue and falseValue |
| 3222 |
|
| 3223 |
// see if OpSelect can handle it |
| 3224 |
if (isOpSelectable()) { |
| 3225 |
// Emit OpSelect for this selection. |
| 3226 |
|
| 3227 |
// smear condition to vector, if necessary (AST is always scalar) |
| 3228 |
// Before 1.4, smear like for mix(), starting with 1.4, keep it scalar |
| 3229 |
if (glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_4 && builder.isVector(trueValue)) { |
| 3230 |
condition = builder.smearScalar(spv::NoPrecision, condition, |
| 3231 |
builder.makeVectorType(builder.makeBoolType(), |
| 3232 |
builder.getNumComponents(trueValue))); |
| 3233 |
} |
| 3234 |
|
| 3235 |
// OpSelect |
| 3236 |
result = builder.createTriOp(spv::OpSelect, |
| 3237 |
convertGlslangToSpvType(node->getType()), condition, |
| 3238 |
trueValue, falseValue); |
| 3239 |
|
| 3240 |
builder.clearAccessChain(); |
| 3241 |
builder.setAccessChainRValue(result); |
| 3242 |
} else { |
| 3243 |
// We need control flow to select the result. |
| 3244 |
// TODO: Once SPIR-V OpSelect allows arbitrary types, eliminate this path. |
| 3245 |
result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), |
| 3246 |
spv::StorageClassFunction, convertGlslangToSpvType(node->getType())); |
| 3247 |
|
| 3248 |
// Selection control: |
| 3249 |
const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3250 |
|
| 3251 |
// make an "if" based on the value created by the condition |
| 3252 |
spv::Builder::If ifBuilder(condition, control, builder); |
| 3253 |
|
| 3254 |
// emit the "then" statement |
| 3255 |
builder.createStore(trueValue, result); |
| 3256 |
ifBuilder.makeBeginElse(); |
| 3257 |
// emit the "else" statement |
| 3258 |
builder.createStore(falseValue, result); |
| 3259 |
|
| 3260 |
// finish off the control flow |
| 3261 |
ifBuilder.makeEndIf(); |
| 3262 |
|
| 3263 |
builder.clearAccessChain(); |
| 3264 |
builder.setAccessChainLValue(result); |
| 3265 |
} |
| 3266 |
}; |
| 3267 |
|
| 3268 |
// Execute the one side needed, as per the condition |
| 3269 |
const auto executeOneSide = [&]() { |
| 3270 |
// Always emit control flow. |
| 3271 |
if (node->getBasicType() != glslang::EbtVoid) { |
| 3272 |
result = builder.createVariable(TranslatePrecisionDecoration(node->getType()), spv::StorageClassFunction, |
| 3273 |
convertGlslangToSpvType(node->getType())); |
| 3274 |
} |
| 3275 |
|
| 3276 |
// Selection control: |
| 3277 |
const spv::SelectionControlMask control = TranslateSelectionControl(*node); |
| 3278 |
|
| 3279 |
// make an "if" based on the value created by the condition |
| 3280 |
spv::Builder::If ifBuilder(condition, control, builder); |
| 3281 |
|
| 3282 |
// emit the "then" statement |
| 3283 |
if (node->getTrueBlock() != nullptr) { |
| 3284 |
node->getTrueBlock()->traverse(this); |
| 3285 |
if (result != spv::NoResult) |
| 3286 |
builder.createStore(accessChainLoad(node->getTrueBlock()->getAsTyped()->getType()), result); |
| 3287 |
} |
| 3288 |
|
| 3289 |
if (node->getFalseBlock() != nullptr) { |
| 3290 |
ifBuilder.makeBeginElse(); |
| 3291 |
// emit the "else" statement |
| 3292 |
node->getFalseBlock()->traverse(this); |
| 3293 |
if (result != spv::NoResult) |
| 3294 |
builder.createStore(accessChainLoad(node->getFalseBlock()->getAsTyped()->getType()), result); |
| 3295 |
} |
| 3296 |
|
| 3297 |
// finish off the control flow |
| 3298 |
ifBuilder.makeEndIf(); |
| 3299 |
|
| 3300 |
if (result != spv::NoResult) { |
| 3301 |
builder.clearAccessChain(); |
| 3302 |
builder.setAccessChainLValue(result); |
| 3303 |
} |
| 3304 |
}; |
| 3305 |
|
| 3306 |
// Try for OpSelect (or a requirement to execute both sides) |
| 3307 |
if (bothSidesPolicy()) { |
| 3308 |
SpecConstantOpModeGuard spec_constant_op_mode_setter(&builder); |
| 3309 |
if (node->getType().getQualifier().isSpecConstant()) |
| 3310 |
spec_constant_op_mode_setter.turnOnSpecConstantOpMode(); |
| 3311 |
executeBothSides(); |
| 3312 |
} else |
| 3313 |
executeOneSide(); |
| 3314 |
|
| 3315 |
return false; |
| 3316 |
} |
| 3317 |
|
| 3318 |
bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit /* visit */, glslang::TIntermSwitch* node) |
| 3319 |
{ |
| 3320 |
// emit and get the condition before doing anything with switch |
| 3321 |
node->getCondition()->traverse(this); |
| 3322 |
spv::Id selector = accessChainLoad(node->getCondition()->getAsTyped()->getType()); |
| 3323 |
|
| 3324 |
// Selection control: |
| 3325 |
const spv::SelectionControlMask control = TranslateSwitchControl(*node); |
| 3326 |
|
| 3327 |
// browse the children to sort out code segments |
| 3328 |
int defaultSegment = -1; |
| 3329 |
std::vector<TIntermNode*> codeSegments; |
| 3330 |
glslang::TIntermSequence& sequence = node->getBody()->getSequence(); |
| 3331 |
std::vector<int> caseValues; |
| 3332 |
std::vector<int> valueIndexToSegment(sequence.size()); // note: probably not all are used, it is an overestimate |
| 3333 |
for (glslang::TIntermSequence::iterator c = sequence.begin(); c != sequence.end(); ++c) { |
| 3334 |
TIntermNode* child = *c; |
| 3335 |
if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpDefault) |
| 3336 |
defaultSegment = (int)codeSegments.size(); |
| 3337 |
else if (child->getAsBranchNode() && child->getAsBranchNode()->getFlowOp() == glslang::EOpCase) { |
| 3338 |
valueIndexToSegment[caseValues.size()] = (int)codeSegments.size(); |
| 3339 |
caseValues.push_back(child->getAsBranchNode()->getExpression()->getAsConstantUnion() |
| 3340 |
->getConstArray()[0].getIConst()); |
| 3341 |
} else |
| 3342 |
codeSegments.push_back(child); |
| 3343 |
} |
| 3344 |
|
| 3345 |
// handle the case where the last code segment is missing, due to no code |
| 3346 |
// statements between the last case and the end of the switch statement |
| 3347 |
if ((caseValues.size() && (int)codeSegments.size() == valueIndexToSegment[caseValues.size() - 1]) || |
| 3348 |
(int)codeSegments.size() == defaultSegment) |
| 3349 |
codeSegments.push_back(nullptr); |
| 3350 |
|
| 3351 |
// make the switch statement |
| 3352 |
std::vector<spv::Block*> segmentBlocks; // returned, as the blocks allocated in the call |
| 3353 |
builder.makeSwitch(selector, control, (int)codeSegments.size(), caseValues, valueIndexToSegment, defaultSegment, |
| 3354 |
segmentBlocks); |
| 3355 |
|
| 3356 |
// emit all the code in the segments |
| 3357 |
breakForLoop.push(false); |
| 3358 |
for (unsigned int s = 0; s < codeSegments.size(); ++s) { |
| 3359 |
builder.nextSwitchSegment(segmentBlocks, s); |
| 3360 |
if (codeSegments[s]) |
| 3361 |
codeSegments[s]->traverse(this); |
| 3362 |
else |
| 3363 |
builder.addSwitchBreak(); |
| 3364 |
} |
| 3365 |
breakForLoop.pop(); |
| 3366 |
|
| 3367 |
builder.endSwitch(segmentBlocks); |
| 3368 |
|
| 3369 |
return false; |
| 3370 |
} |
| 3371 |
|
| 3372 |
void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node) |
| 3373 |
{ |
| 3374 |
int nextConst = 0; |
| 3375 |
spv::Id constant = createSpvConstantFromConstUnionArray(node->getType(), node->getConstArray(), nextConst, false); |
| 3376 |
|
| 3377 |
builder.clearAccessChain(); |
| 3378 |
builder.setAccessChainRValue(constant); |
| 3379 |
} |
| 3380 |
|
| 3381 |
bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node) |
| 3382 |
{ |
| 3383 |
auto blocks = builder.makeNewLoop(); |
| 3384 |
builder.createBranch(&blocks.head); |
| 3385 |
|
| 3386 |
// Loop control: |
| 3387 |
std::vector<unsigned int> operands; |
| 3388 |
const spv::LoopControlMask control = TranslateLoopControl(*node, operands); |
| 3389 |
|
| 3390 |
// Spec requires back edges to target header blocks, and every header block |
| 3391 |
// must dominate its merge block. Make a header block first to ensure these |
| 3392 |
// conditions are met. By definition, it will contain OpLoopMerge, followed |
| 3393 |
// by a block-ending branch. But we don't want to put any other body/test |
| 3394 |
// instructions in it, since the body/test may have arbitrary instructions, |
| 3395 |
// including merges of its own. |
| 3396 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3397 |
builder.setBuildPoint(&blocks.head); |
| 3398 |
builder.createLoopMerge(&blocks.merge, &blocks.continue_target, control, operands); |
| 3399 |
if (node->testFirst() && node->getTest()) { |
| 3400 |
spv::Block& test = builder.makeNewBlock(); |
| 3401 |
builder.createBranch(&test); |
| 3402 |
|
| 3403 |
builder.setBuildPoint(&test); |
| 3404 |
node->getTest()->traverse(this); |
| 3405 |
spv::Id condition = accessChainLoad(node->getTest()->getType()); |
| 3406 |
builder.createConditionalBranch(condition, &blocks.body, &blocks.merge); |
| 3407 |
|
| 3408 |
builder.setBuildPoint(&blocks.body); |
| 3409 |
breakForLoop.push(true); |
| 3410 |
if (node->getBody()) |
| 3411 |
node->getBody()->traverse(this); |
| 3412 |
builder.createBranch(&blocks.continue_target); |
| 3413 |
breakForLoop.pop(); |
| 3414 |
|
| 3415 |
builder.setBuildPoint(&blocks.continue_target); |
| 3416 |
if (node->getTerminal()) |
| 3417 |
node->getTerminal()->traverse(this); |
| 3418 |
builder.createBranch(&blocks.head); |
| 3419 |
} else { |
| 3420 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3421 |
builder.createBranch(&blocks.body); |
| 3422 |
|
| 3423 |
breakForLoop.push(true); |
| 3424 |
builder.setBuildPoint(&blocks.body); |
| 3425 |
if (node->getBody()) |
| 3426 |
node->getBody()->traverse(this); |
| 3427 |
builder.createBranch(&blocks.continue_target); |
| 3428 |
breakForLoop.pop(); |
| 3429 |
|
| 3430 |
builder.setBuildPoint(&blocks.continue_target); |
| 3431 |
if (node->getTerminal()) |
| 3432 |
node->getTerminal()->traverse(this); |
| 3433 |
if (node->getTest()) { |
| 3434 |
node->getTest()->traverse(this); |
| 3435 |
spv::Id condition = |
| 3436 |
accessChainLoad(node->getTest()->getType()); |
| 3437 |
builder.createConditionalBranch(condition, &blocks.head, &blocks.merge); |
| 3438 |
} else { |
| 3439 |
// TODO: unless there was a break/return/discard instruction |
| 3440 |
// somewhere in the body, this is an infinite loop, so we should |
| 3441 |
// issue a warning. |
| 3442 |
builder.createBranch(&blocks.head); |
| 3443 |
} |
| 3444 |
} |
| 3445 |
builder.setBuildPoint(&blocks.merge); |
| 3446 |
builder.closeLoop(); |
| 3447 |
return false; |
| 3448 |
} |
| 3449 |
|
| 3450 |
bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit /* visit */, glslang::TIntermBranch* node) |
| 3451 |
{ |
| 3452 |
if (node->getExpression()) |
| 3453 |
node->getExpression()->traverse(this); |
| 3454 |
|
| 3455 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 3456 |
|
| 3457 |
switch (node->getFlowOp()) { |
| 3458 |
case glslang::EOpKill: |
| 3459 |
builder.makeDiscard(); |
| 3460 |
break; |
| 3461 |
case glslang::EOpTerminateInvocation: |
| 3462 |
builder.addExtension(spv::E_SPV_KHR_terminate_invocation); |
| 3463 |
builder.makeTerminateInvocation(); |
| 3464 |
break; |
| 3465 |
case glslang::EOpBreak: |
| 3466 |
if (breakForLoop.top()) |
| 3467 |
builder.createLoopExit(); |
| 3468 |
else |
| 3469 |
builder.addSwitchBreak(); |
| 3470 |
break; |
| 3471 |
case glslang::EOpContinue: |
| 3472 |
builder.createLoopContinue(); |
| 3473 |
break; |
| 3474 |
case glslang::EOpReturn: |
| 3475 |
if (node->getExpression() != nullptr) { |
| 3476 |
const glslang::TType& glslangReturnType = node->getExpression()->getType(); |
| 3477 |
spv::Id returnId = accessChainLoad(glslangReturnType); |
| 3478 |
if (builder.getTypeId(returnId) != currentFunction->getReturnType() || |
| 3479 |
TranslatePrecisionDecoration(glslangReturnType) != currentFunction->getReturnPrecision()) { |
| 3480 |
builder.clearAccessChain(); |
| 3481 |
spv::Id copyId = builder.createVariable(currentFunction->getReturnPrecision(), |
| 3482 |
spv::StorageClassFunction, currentFunction->getReturnType()); |
| 3483 |
builder.setAccessChainLValue(copyId); |
| 3484 |
multiTypeStore(glslangReturnType, returnId); |
| 3485 |
returnId = builder.createLoad(copyId, currentFunction->getReturnPrecision()); |
| 3486 |
} |
| 3487 |
builder.makeReturn(false, returnId); |
| 3488 |
} else |
| 3489 |
builder.makeReturn(false); |
| 3490 |
|
| 3491 |
builder.clearAccessChain(); |
| 3492 |
break; |
| 3493 |
|
| 3494 |
#ifndef GLSLANG_WEB |
| 3495 |
case glslang::EOpDemote: |
| 3496 |
builder.createNoResultOp(spv::OpDemoteToHelperInvocationEXT); |
| 3497 |
builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 3498 |
builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 3499 |
break; |
| 3500 |
#endif |
| 3501 |
|
| 3502 |
default: |
| 3503 |
assert(0); |
| 3504 |
break; |
| 3505 |
} |
| 3506 |
|
| 3507 |
return false; |
| 3508 |
} |
| 3509 |
|
| 3510 |
spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType) |
| 3511 |
{ |
| 3512 |
// First, steer off constants, which are not SPIR-V variables, but |
| 3513 |
// can still have a mapping to a SPIR-V Id. |
| 3514 |
// This includes specialization constants. |
| 3515 |
if (node->getQualifier().isConstant()) { |
| 3516 |
spv::Id result = createSpvConstant(*node); |
| 3517 |
if (result != spv::NoResult) |
| 3518 |
return result; |
| 3519 |
} |
| 3520 |
|
| 3521 |
// Now, handle actual variables |
| 3522 |
spv::StorageClass storageClass = TranslateStorageClass(node->getType()); |
| 3523 |
spv::Id spvType = forcedType == spv::NoType ? convertGlslangToSpvType(node->getType()) |
| 3524 |
: forcedType; |
| 3525 |
|
| 3526 |
const bool contains16BitType = node->getType().contains16BitFloat() || |
| 3527 |
node->getType().contains16BitInt(); |
| 3528 |
if (contains16BitType) { |
| 3529 |
switch (storageClass) { |
| 3530 |
case spv::StorageClassInput: |
| 3531 |
case spv::StorageClassOutput: |
| 3532 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| 3533 |
builder.addCapability(spv::CapabilityStorageInputOutput16); |
| 3534 |
break; |
| 3535 |
case spv::StorageClassUniform: |
| 3536 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| 3537 |
if (node->getType().getQualifier().storage == glslang::EvqBuffer) |
| 3538 |
builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 3539 |
else |
| 3540 |
builder.addCapability(spv::CapabilityStorageUniform16); |
| 3541 |
break; |
| 3542 |
#ifndef GLSLANG_WEB |
| 3543 |
case spv::StorageClassPushConstant: |
| 3544 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| 3545 |
builder.addCapability(spv::CapabilityStoragePushConstant16); |
| 3546 |
break; |
| 3547 |
case spv::StorageClassStorageBuffer: |
| 3548 |
case spv::StorageClassPhysicalStorageBufferEXT: |
| 3549 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| 3550 |
builder.addCapability(spv::CapabilityStorageUniformBufferBlock16); |
| 3551 |
break; |
| 3552 |
#endif |
| 3553 |
default: |
| 3554 |
if (node->getType().contains16BitFloat()) |
| 3555 |
builder.addCapability(spv::CapabilityFloat16); |
| 3556 |
if (node->getType().contains16BitInt()) |
| 3557 |
builder.addCapability(spv::CapabilityInt16); |
| 3558 |
break; |
| 3559 |
} |
| 3560 |
} |
| 3561 |
|
| 3562 |
if (node->getType().contains8BitInt()) { |
| 3563 |
if (storageClass == spv::StorageClassPushConstant) { |
| 3564 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
| 3565 |
builder.addCapability(spv::CapabilityStoragePushConstant8); |
| 3566 |
} else if (storageClass == spv::StorageClassUniform) { |
| 3567 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
| 3568 |
builder.addCapability(spv::CapabilityUniformAndStorageBuffer8BitAccess); |
| 3569 |
} else if (storageClass == spv::StorageClassStorageBuffer) { |
| 3570 |
builder.addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
| 3571 |
builder.addCapability(spv::CapabilityStorageBuffer8BitAccess); |
| 3572 |
} else { |
| 3573 |
builder.addCapability(spv::CapabilityInt8); |
| 3574 |
} |
| 3575 |
} |
| 3576 |
|
| 3577 |
const char* name = node->getName().c_str(); |
| 3578 |
if (glslang::IsAnonymous(name)) |
| 3579 |
name = ""; |
| 3580 |
|
| 3581 |
spv::Id initializer = spv::NoResult; |
| 3582 |
|
| 3583 |
if (node->getType().getQualifier().storage == glslang::EvqUniform && |
| 3584 |
!node->getConstArray().empty()) { |
| 3585 |
int nextConst = 0; |
| 3586 |
initializer = createSpvConstantFromConstUnionArray(node->getType(), |
| 3587 |
node->getConstArray(), |
| 3588 |
nextConst, |
| 3589 |
false /* specConst */); |
| 3590 |
} |
| 3591 |
|
| 3592 |
return builder.createVariable(spv::NoPrecision, storageClass, spvType, name, initializer); |
| 3593 |
} |
| 3594 |
|
| 3595 |
// Return type Id of the sampled type. |
| 3596 |
spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler) |
| 3597 |
{ |
| 3598 |
switch (sampler.type) { |
| 3599 |
case glslang::EbtInt: return builder.makeIntType(32); |
| 3600 |
case glslang::EbtUint: return builder.makeUintType(32); |
| 3601 |
case glslang::EbtFloat: return builder.makeFloatType(32); |
| 3602 |
#ifndef GLSLANG_WEB |
| 3603 |
case glslang::EbtFloat16: |
| 3604 |
builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float_fetch); |
| 3605 |
builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3606 |
return builder.makeFloatType(16); |
| 3607 |
case glslang::EbtInt64: return builder.makeIntType(64); |
| 3608 |
builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 3609 |
builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3610 |
case glslang::EbtUint64: return builder.makeUintType(64); |
| 3611 |
builder.addExtension(spv::E_SPV_EXT_shader_image_int64); |
| 3612 |
builder.addCapability(spv::CapabilityFloat16ImageAMD); |
| 3613 |
#endif |
| 3614 |
default: |
| 3615 |
assert(0); |
| 3616 |
return builder.makeFloatType(32); |
| 3617 |
} |
| 3618 |
} |
| 3619 |
|
| 3620 |
// If node is a swizzle operation, return the type that should be used if |
| 3621 |
// the swizzle base is first consumed by another operation, before the swizzle |
| 3622 |
// is applied. |
| 3623 |
spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node) |
| 3624 |
{ |
| 3625 |
if (node.getAsOperator() && |
| 3626 |
node.getAsOperator()->getOp() == glslang::EOpVectorSwizzle) |
| 3627 |
return convertGlslangToSpvType(node.getAsBinaryNode()->getLeft()->getType()); |
| 3628 |
else |
| 3629 |
return spv::NoType; |
| 3630 |
} |
| 3631 |
|
| 3632 |
// When inverting a swizzle with a parent op, this function |
| 3633 |
// will apply the swizzle operation to a completed parent operation. |
| 3634 |
spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node, |
| 3635 |
spv::Id parentResult) |
| 3636 |
{ |
| 3637 |
std::vector<unsigned> swizzle; |
| 3638 |
convertSwizzle(*node.getAsBinaryNode()->getRight()->getAsAggregate(), swizzle); |
| 3639 |
return builder.createRvalueSwizzle(precision, convertGlslangToSpvType(node.getType()), parentResult, swizzle); |
| 3640 |
} |
| 3641 |
|
| 3642 |
// Convert a glslang AST swizzle node to a swizzle vector for building SPIR-V. |
| 3643 |
void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle) |
| 3644 |
{ |
| 3645 |
const glslang::TIntermSequence& swizzleSequence = node.getSequence(); |
| 3646 |
for (int i = 0; i < (int)swizzleSequence.size(); ++i) |
| 3647 |
swizzle.push_back(swizzleSequence[i]->getAsConstantUnion()->getConstArray()[0].getIConst()); |
| 3648 |
} |
| 3649 |
|
| 3650 |
// Convert from a glslang type to an SPV type, by calling into a |
| 3651 |
// recursive version of this function. This establishes the inherited |
| 3652 |
// layout state rooted from the top-level type. |
| 3653 |
spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly) |
| 3654 |
{ |
| 3655 |
return convertGlslangToSpvType(type, getExplicitLayout(type), type.getQualifier(), false, forwardReferenceOnly); |
| 3656 |
} |
| 3657 |
|
| 3658 |
// Do full recursive conversion of an arbitrary glslang type to a SPIR-V Id. |
| 3659 |
// explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3660 |
// Mutually recursive with convertGlslangStructToSpvType(). |
| 3661 |
spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, |
| 3662 |
glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier, |
| 3663 |
bool lastBufferBlockMember, bool forwardReferenceOnly) |
| 3664 |
{ |
| 3665 |
spv::Id spvType = spv::NoResult; |
| 3666 |
|
| 3667 |
switch (type.getBasicType()) { |
| 3668 |
case glslang::EbtVoid: |
| 3669 |
spvType = builder.makeVoidType(); |
| 3670 |
assert (! type.isArray()); |
| 3671 |
break; |
| 3672 |
case glslang::EbtBool: |
| 3673 |
// "transparent" bool doesn't exist in SPIR-V. The GLSL convention is |
| 3674 |
// a 32-bit int where non-0 means true. |
| 3675 |
if (explicitLayout != glslang::ElpNone) |
| 3676 |
spvType = builder.makeUintType(32); |
| 3677 |
else |
| 3678 |
spvType = builder.makeBoolType(); |
| 3679 |
break; |
| 3680 |
case glslang::EbtInt: |
| 3681 |
spvType = builder.makeIntType(32); |
| 3682 |
break; |
| 3683 |
case glslang::EbtUint: |
| 3684 |
spvType = builder.makeUintType(32); |
| 3685 |
break; |
| 3686 |
case glslang::EbtFloat: |
| 3687 |
spvType = builder.makeFloatType(32); |
| 3688 |
break; |
| 3689 |
#ifndef GLSLANG_WEB |
| 3690 |
case glslang::EbtDouble: |
| 3691 |
spvType = builder.makeFloatType(64); |
| 3692 |
break; |
| 3693 |
case glslang::EbtFloat16: |
| 3694 |
spvType = builder.makeFloatType(16); |
| 3695 |
break; |
| 3696 |
case glslang::EbtInt8: |
| 3697 |
spvType = builder.makeIntType(8); |
| 3698 |
break; |
| 3699 |
case glslang::EbtUint8: |
| 3700 |
spvType = builder.makeUintType(8); |
| 3701 |
break; |
| 3702 |
case glslang::EbtInt16: |
| 3703 |
spvType = builder.makeIntType(16); |
| 3704 |
break; |
| 3705 |
case glslang::EbtUint16: |
| 3706 |
spvType = builder.makeUintType(16); |
| 3707 |
break; |
| 3708 |
case glslang::EbtInt64: |
| 3709 |
spvType = builder.makeIntType(64); |
| 3710 |
break; |
| 3711 |
case glslang::EbtUint64: |
| 3712 |
spvType = builder.makeUintType(64); |
| 3713 |
break; |
| 3714 |
case glslang::EbtAtomicUint: |
| 3715 |
builder.addCapability(spv::CapabilityAtomicStorage); |
| 3716 |
spvType = builder.makeUintType(32); |
| 3717 |
break; |
| 3718 |
case glslang::EbtAccStruct: |
| 3719 |
spvType = builder.makeAccelerationStructureType(); |
| 3720 |
break; |
| 3721 |
case glslang::EbtRayQuery: |
| 3722 |
spvType = builder.makeRayQueryType(); |
| 3723 |
break; |
| 3724 |
case glslang::EbtReference: |
| 3725 |
{ |
| 3726 |
// Make the forward pointer, then recurse to convert the structure type, then |
| 3727 |
// patch up the forward pointer with a real pointer type. |
| 3728 |
if (forwardPointers.find(type.getReferentType()) == forwardPointers.end()) { |
| 3729 |
spv::Id forwardId = builder.makeForwardPointer(spv::StorageClassPhysicalStorageBufferEXT); |
| 3730 |
forwardPointers[type.getReferentType()] = forwardId; |
| 3731 |
} |
| 3732 |
spvType = forwardPointers[type.getReferentType()]; |
| 3733 |
if (!forwardReferenceOnly) { |
| 3734 |
spv::Id referentType = convertGlslangToSpvType(*type.getReferentType()); |
| 3735 |
builder.makePointerFromForwardPointer(spv::StorageClassPhysicalStorageBufferEXT, |
| 3736 |
forwardPointers[type.getReferentType()], |
| 3737 |
referentType); |
| 3738 |
} |
| 3739 |
} |
| 3740 |
break; |
| 3741 |
#endif |
| 3742 |
case glslang::EbtSampler: |
| 3743 |
{ |
| 3744 |
const glslang::TSampler& sampler = type.getSampler(); |
| 3745 |
if (sampler.isPureSampler()) { |
| 3746 |
spvType = builder.makeSamplerType(); |
| 3747 |
} else { |
| 3748 |
// an image is present, make its type |
| 3749 |
spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), |
| 3750 |
sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), |
| 3751 |
sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type)); |
| 3752 |
if (sampler.isCombined()) { |
| 3753 |
// already has both image and sampler, make the combined type |
| 3754 |
spvType = builder.makeSampledImageType(spvType); |
| 3755 |
} |
| 3756 |
} |
| 3757 |
} |
| 3758 |
break; |
| 3759 |
case glslang::EbtStruct: |
| 3760 |
case glslang::EbtBlock: |
| 3761 |
{ |
| 3762 |
// If we've seen this struct type, return it |
| 3763 |
const glslang::TTypeList* glslangMembers = type.getStruct(); |
| 3764 |
|
| 3765 |
// Try to share structs for different layouts, but not yet for other |
| 3766 |
// kinds of qualification (primarily not yet including interpolant qualification). |
| 3767 |
if (! HasNonLayoutQualifiers(type, qualifier)) |
| 3768 |
spvType = structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers]; |
| 3769 |
if (spvType != spv::NoResult) |
| 3770 |
break; |
| 3771 |
|
| 3772 |
// else, we haven't seen it... |
| 3773 |
if (type.getBasicType() == glslang::EbtBlock) |
| 3774 |
memberRemapper[glslangTypeToIdMap[glslangMembers]].resize(glslangMembers->size()); |
| 3775 |
spvType = convertGlslangStructToSpvType(type, glslangMembers, explicitLayout, qualifier); |
| 3776 |
} |
| 3777 |
break; |
| 3778 |
case glslang::EbtString: |
| 3779 |
// no type used for OpString |
| 3780 |
return 0; |
| 3781 |
default: |
| 3782 |
assert(0); |
| 3783 |
break; |
| 3784 |
} |
| 3785 |
|
| 3786 |
if (type.isMatrix()) |
| 3787 |
spvType = builder.makeMatrixType(spvType, type.getMatrixCols(), type.getMatrixRows()); |
| 3788 |
else { |
| 3789 |
// If this variable has a vector element count greater than 1, create a SPIR-V vector |
| 3790 |
if (type.getVectorSize() > 1) |
| 3791 |
spvType = builder.makeVectorType(spvType, type.getVectorSize()); |
| 3792 |
} |
| 3793 |
|
| 3794 |
if (type.isCoopMat()) { |
| 3795 |
builder.addCapability(spv::CapabilityCooperativeMatrixNV); |
| 3796 |
builder.addExtension(spv::E_SPV_NV_cooperative_matrix); |
| 3797 |
if (type.getBasicType() == glslang::EbtFloat16) |
| 3798 |
builder.addCapability(spv::CapabilityFloat16); |
| 3799 |
if (type.getBasicType() == glslang::EbtUint8 || |
| 3800 |
type.getBasicType() == glslang::EbtInt8) { |
| 3801 |
builder.addCapability(spv::CapabilityInt8); |
| 3802 |
} |
| 3803 |
|
| 3804 |
spv::Id scope = makeArraySizeId(*type.getTypeParameters(), 1); |
| 3805 |
spv::Id rows = makeArraySizeId(*type.getTypeParameters(), 2); |
| 3806 |
spv::Id cols = makeArraySizeId(*type.getTypeParameters(), 3); |
| 3807 |
|
| 3808 |
spvType = builder.makeCooperativeMatrixType(spvType, scope, rows, cols); |
| 3809 |
} |
| 3810 |
|
| 3811 |
if (type.isArray()) { |
| 3812 |
int stride = 0; // keep this 0 unless doing an explicit layout; 0 will mean no decoration, no stride |
| 3813 |
|
| 3814 |
// Do all but the outer dimension |
| 3815 |
if (type.getArraySizes()->getNumDims() > 1) { |
| 3816 |
// We need to decorate array strides for types needing explicit layout, except blocks. |
| 3817 |
if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) { |
| 3818 |
// Use a dummy glslang type for querying internal strides of |
| 3819 |
// arrays of arrays, but using just a one-dimensional array. |
| 3820 |
glslang::TType simpleArrayType(type, 0); // deference type of the array |
| 3821 |
while (simpleArrayType.getArraySizes()->getNumDims() > 1) |
| 3822 |
simpleArrayType.getArraySizes()->dereference(); |
| 3823 |
|
| 3824 |
// Will compute the higher-order strides here, rather than making a whole |
| 3825 |
// pile of types and doing repetitive recursion on their contents. |
| 3826 |
stride = getArrayStride(simpleArrayType, explicitLayout, qualifier.layoutMatrix); |
| 3827 |
} |
| 3828 |
|
| 3829 |
// make the arrays |
| 3830 |
for (int dim = type.getArraySizes()->getNumDims() - 1; dim > 0; --dim) { |
| 3831 |
spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), dim), stride); |
| 3832 |
if (stride > 0) |
| 3833 |
builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
| 3834 |
stride *= type.getArraySizes()->getDimSize(dim); |
| 3835 |
} |
| 3836 |
} else { |
| 3837 |
// single-dimensional array, and don't yet have stride |
| 3838 |
|
| 3839 |
// We need to decorate array strides for types needing explicit layout, except blocks. |
| 3840 |
if (explicitLayout != glslang::ElpNone && type.getBasicType() != glslang::EbtBlock) |
| 3841 |
stride = getArrayStride(type, explicitLayout, qualifier.layoutMatrix); |
| 3842 |
} |
| 3843 |
|
| 3844 |
// Do the outer dimension, which might not be known for a runtime-sized array. |
| 3845 |
// (Unsized arrays that survive through linking will be runtime-sized arrays) |
| 3846 |
if (type.isSizedArray()) |
| 3847 |
spvType = builder.makeArrayType(spvType, makeArraySizeId(*type.getArraySizes(), 0), stride); |
| 3848 |
else { |
| 3849 |
#ifndef GLSLANG_WEB |
| 3850 |
if (!lastBufferBlockMember) { |
| 3851 |
builder.addIncorporatedExtension("SPV_EXT_descriptor_indexing", spv::Spv_1_5); |
| 3852 |
builder.addCapability(spv::CapabilityRuntimeDescriptorArrayEXT); |
| 3853 |
} |
| 3854 |
#endif |
| 3855 |
spvType = builder.makeRuntimeArray(spvType); |
| 3856 |
} |
| 3857 |
if (stride > 0) |
| 3858 |
builder.addDecoration(spvType, spv::DecorationArrayStride, stride); |
| 3859 |
} |
| 3860 |
|
| 3861 |
return spvType; |
| 3862 |
} |
| 3863 |
|
| 3864 |
// TODO: this functionality should exist at a higher level, in creating the AST |
| 3865 |
// |
| 3866 |
// Identify interface members that don't have their required extension turned on. |
| 3867 |
// |
| 3868 |
bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member) |
| 3869 |
{ |
| 3870 |
#ifndef GLSLANG_WEB |
| 3871 |
auto& extensions = glslangIntermediate->getRequestedExtensions(); |
| 3872 |
|
| 3873 |
if (member.getFieldName() == "gl_SecondaryViewportMaskNV" && |
| 3874 |
extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3875 |
return true; |
| 3876 |
if (member.getFieldName() == "gl_SecondaryPositionNV" && |
| 3877 |
extensions.find("GL_NV_stereo_view_rendering") == extensions.end()) |
| 3878 |
return true; |
| 3879 |
|
| 3880 |
if (glslangIntermediate->getStage() != EShLangMeshNV) { |
| 3881 |
if (member.getFieldName() == "gl_ViewportMask" && |
| 3882 |
extensions.find("GL_NV_viewport_array2") == extensions.end()) |
| 3883 |
return true; |
| 3884 |
if (member.getFieldName() == "gl_PositionPerViewNV" && |
| 3885 |
extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3886 |
return true; |
| 3887 |
if (member.getFieldName() == "gl_ViewportMaskPerViewNV" && |
| 3888 |
extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end()) |
| 3889 |
return true; |
| 3890 |
} |
| 3891 |
#endif |
| 3892 |
|
| 3893 |
return false; |
| 3894 |
}; |
| 3895 |
|
| 3896 |
// Do full recursive conversion of a glslang structure (or block) type to a SPIR-V Id. |
| 3897 |
// explicitLayout can be kept the same throughout the hierarchical recursive walk. |
| 3898 |
// Mutually recursive with convertGlslangToSpvType(). |
| 3899 |
spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type, |
| 3900 |
const glslang::TTypeList* glslangMembers, |
| 3901 |
glslang::TLayoutPacking explicitLayout, |
| 3902 |
const glslang::TQualifier& qualifier) |
| 3903 |
{ |
| 3904 |
// Create a vector of struct types for SPIR-V to consume |
| 3905 |
std::vector<spv::Id> spvMembers; |
| 3906 |
int memberDelta = 0; // how much the member's index changes from glslang to SPIR-V, normally 0, |
| 3907 |
// except sometimes for blocks |
| 3908 |
std::vector<std::pair<glslang::TType*, glslang::TQualifier> > deferredForwardPointers; |
| 3909 |
for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3910 |
glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3911 |
if (glslangMember.hiddenMember()) { |
| 3912 |
++memberDelta; |
| 3913 |
if (type.getBasicType() == glslang::EbtBlock) |
| 3914 |
memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
| 3915 |
} else { |
| 3916 |
if (type.getBasicType() == glslang::EbtBlock) { |
| 3917 |
if (filterMember(glslangMember)) { |
| 3918 |
memberDelta++; |
| 3919 |
memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = -1; |
| 3920 |
continue; |
| 3921 |
} |
| 3922 |
memberRemapper[glslangTypeToIdMap[glslangMembers]][i] = i - memberDelta; |
| 3923 |
} |
| 3924 |
// modify just this child's view of the qualifier |
| 3925 |
glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3926 |
InheritQualifiers(memberQualifier, qualifier); |
| 3927 |
|
| 3928 |
// manually inherit location |
| 3929 |
if (! memberQualifier.hasLocation() && qualifier.hasLocation()) |
| 3930 |
memberQualifier.layoutLocation = qualifier.layoutLocation; |
| 3931 |
|
| 3932 |
// recurse |
| 3933 |
bool lastBufferBlockMember = qualifier.storage == glslang::EvqBuffer && |
| 3934 |
i == (int)glslangMembers->size() - 1; |
| 3935 |
|
| 3936 |
// Make forward pointers for any pointer members, and create a list of members to |
| 3937 |
// convert to spirv types after creating the struct. |
| 3938 |
if (glslangMember.isReference()) { |
| 3939 |
if (forwardPointers.find(glslangMember.getReferentType()) == forwardPointers.end()) { |
| 3940 |
deferredForwardPointers.push_back(std::make_pair(&glslangMember, memberQualifier)); |
| 3941 |
} |
| 3942 |
spvMembers.push_back( |
| 3943 |
convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 3944 |
true)); |
| 3945 |
} else { |
| 3946 |
spvMembers.push_back( |
| 3947 |
convertGlslangToSpvType(glslangMember, explicitLayout, memberQualifier, lastBufferBlockMember, |
| 3948 |
false)); |
| 3949 |
} |
| 3950 |
} |
| 3951 |
} |
| 3952 |
|
| 3953 |
// Make the SPIR-V type |
| 3954 |
spv::Id spvType = builder.makeStructType(spvMembers, type.getTypeName().c_str()); |
| 3955 |
if (! HasNonLayoutQualifiers(type, qualifier)) |
| 3956 |
structMap[explicitLayout][qualifier.layoutMatrix][glslangMembers] = spvType; |
| 3957 |
|
| 3958 |
// Decorate it |
| 3959 |
decorateStructType(type, glslangMembers, explicitLayout, qualifier, spvType); |
| 3960 |
|
| 3961 |
for (int i = 0; i < (int)deferredForwardPointers.size(); ++i) { |
| 3962 |
auto it = deferredForwardPointers[i]; |
| 3963 |
convertGlslangToSpvType(*it.first, explicitLayout, it.second, false); |
| 3964 |
} |
| 3965 |
|
| 3966 |
return spvType; |
| 3967 |
} |
| 3968 |
|
| 3969 |
void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type, |
| 3970 |
const glslang::TTypeList* glslangMembers, |
| 3971 |
glslang::TLayoutPacking explicitLayout, |
| 3972 |
const glslang::TQualifier& qualifier, |
| 3973 |
spv::Id spvType) |
| 3974 |
{ |
| 3975 |
// Name and decorate the non-hidden members |
| 3976 |
int offset = -1; |
| 3977 |
int locationOffset = 0; // for use within the members of this struct |
| 3978 |
bool memberLocationInvalid = type.isArrayOfArrays() || |
| 3979 |
(type.isArray() && (type.getQualifier().isArrayedIo(glslangIntermediate->getStage()) == false)); |
| 3980 |
for (int i = 0; i < (int)glslangMembers->size(); i++) { |
| 3981 |
glslang::TType& glslangMember = *(*glslangMembers)[i].type; |
| 3982 |
int member = i; |
| 3983 |
if (type.getBasicType() == glslang::EbtBlock) { |
| 3984 |
member = memberRemapper[glslangTypeToIdMap[glslangMembers]][i]; |
| 3985 |
if (filterMember(glslangMember)) |
| 3986 |
continue; |
| 3987 |
} |
| 3988 |
|
| 3989 |
// modify just this child's view of the qualifier |
| 3990 |
glslang::TQualifier memberQualifier = glslangMember.getQualifier(); |
| 3991 |
InheritQualifiers(memberQualifier, qualifier); |
| 3992 |
|
| 3993 |
// using -1 above to indicate a hidden member |
| 3994 |
if (member < 0) |
| 3995 |
continue; |
| 3996 |
|
| 3997 |
builder.addMemberName(spvType, member, glslangMember.getFieldName().c_str()); |
| 3998 |
builder.addMemberDecoration(spvType, member, |
| 3999 |
TranslateLayoutDecoration(glslangMember, memberQualifier.layoutMatrix)); |
| 4000 |
builder.addMemberDecoration(spvType, member, TranslatePrecisionDecoration(glslangMember)); |
| 4001 |
// Add interpolation and auxiliary storage decorations only to |
| 4002 |
// top-level members of Input and Output storage classes |
| 4003 |
if (type.getQualifier().storage == glslang::EvqVaryingIn || |
| 4004 |
type.getQualifier().storage == glslang::EvqVaryingOut) { |
| 4005 |
if (type.getBasicType() == glslang::EbtBlock || |
| 4006 |
glslangIntermediate->getSource() == glslang::EShSourceHlsl) { |
| 4007 |
builder.addMemberDecoration(spvType, member, TranslateInterpolationDecoration(memberQualifier)); |
| 4008 |
builder.addMemberDecoration(spvType, member, TranslateAuxiliaryStorageDecoration(memberQualifier)); |
| 4009 |
#ifndef GLSLANG_WEB |
| 4010 |
addMeshNVDecoration(spvType, member, memberQualifier); |
| 4011 |
#endif |
| 4012 |
} |
| 4013 |
} |
| 4014 |
builder.addMemberDecoration(spvType, member, TranslateInvariantDecoration(memberQualifier)); |
| 4015 |
|
| 4016 |
#ifndef GLSLANG_WEB |
| 4017 |
if (type.getBasicType() == glslang::EbtBlock && |
| 4018 |
qualifier.storage == glslang::EvqBuffer) { |
| 4019 |
// Add memory decorations only to top-level members of shader storage block |
| 4020 |
std::vector<spv::Decoration> memory; |
| 4021 |
TranslateMemoryDecoration(memberQualifier, memory, glslangIntermediate->usingVulkanMemoryModel()); |
| 4022 |
for (unsigned int i = 0; i < memory.size(); ++i) |
| 4023 |
builder.addMemberDecoration(spvType, member, memory[i]); |
| 4024 |
} |
| 4025 |
|
| 4026 |
#endif |
| 4027 |
|
| 4028 |
// Location assignment was already completed correctly by the front end, |
| 4029 |
// just track whether a member needs to be decorated. |
| 4030 |
// Ignore member locations if the container is an array, as that's |
| 4031 |
// ill-specified and decisions have been made to not allow this. |
| 4032 |
if (!memberLocationInvalid && memberQualifier.hasLocation()) |
| 4033 |
builder.addMemberDecoration(spvType, member, spv::DecorationLocation, memberQualifier.layoutLocation); |
| 4034 |
|
| 4035 |
if (qualifier.hasLocation()) // track for upcoming inheritance |
| 4036 |
locationOffset += glslangIntermediate->computeTypeLocationSize( |
| 4037 |
glslangMember, glslangIntermediate->getStage()); |
| 4038 |
|
| 4039 |
// component, XFB, others |
| 4040 |
if (glslangMember.getQualifier().hasComponent()) |
| 4041 |
builder.addMemberDecoration(spvType, member, spv::DecorationComponent, |
| 4042 |
glslangMember.getQualifier().layoutComponent); |
| 4043 |
if (glslangMember.getQualifier().hasXfbOffset()) |
| 4044 |
builder.addMemberDecoration(spvType, member, spv::DecorationOffset, |
| 4045 |
glslangMember.getQualifier().layoutXfbOffset); |
| 4046 |
else if (explicitLayout != glslang::ElpNone) { |
| 4047 |
// figure out what to do with offset, which is accumulating |
| 4048 |
int nextOffset; |
| 4049 |
updateMemberOffset(type, glslangMember, offset, nextOffset, explicitLayout, memberQualifier.layoutMatrix); |
| 4050 |
if (offset >= 0) |
| 4051 |
builder.addMemberDecoration(spvType, member, spv::DecorationOffset, offset); |
| 4052 |
offset = nextOffset; |
| 4053 |
} |
| 4054 |
|
| 4055 |
if (glslangMember.isMatrix() && explicitLayout != glslang::ElpNone) |
| 4056 |
builder.addMemberDecoration(spvType, member, spv::DecorationMatrixStride, |
| 4057 |
getMatrixStride(glslangMember, explicitLayout, memberQualifier.layoutMatrix)); |
| 4058 |
|
| 4059 |
// built-in variable decorations |
| 4060 |
spv::BuiltIn builtIn = TranslateBuiltInDecoration(glslangMember.getQualifier().builtIn, true); |
| 4061 |
if (builtIn != spv::BuiltInMax) |
| 4062 |
builder.addMemberDecoration(spvType, member, spv::DecorationBuiltIn, (int)builtIn); |
| 4063 |
|
| 4064 |
#ifndef GLSLANG_WEB |
| 4065 |
// nonuniform |
| 4066 |
builder.addMemberDecoration(spvType, member, TranslateNonUniformDecoration(glslangMember.getQualifier())); |
| 4067 |
|
| 4068 |
if (glslangIntermediate->getHlslFunctionality1() && memberQualifier.semanticName != nullptr) { |
| 4069 |
builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 4070 |
builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 4071 |
memberQualifier.semanticName); |
| 4072 |
} |
| 4073 |
|
| 4074 |
if (builtIn == spv::BuiltInLayer) { |
| 4075 |
// SPV_NV_viewport_array2 extension |
| 4076 |
if (glslangMember.getQualifier().layoutViewportRelative){ |
| 4077 |
builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 4078 |
builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 4079 |
builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 4080 |
} |
| 4081 |
if (glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset != -2048){ |
| 4082 |
builder.addMemberDecoration(spvType, member, |
| 4083 |
(spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 4084 |
glslangMember.getQualifier().layoutSecondaryViewportRelativeOffset); |
| 4085 |
builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 4086 |
builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 4087 |
} |
| 4088 |
} |
| 4089 |
if (glslangMember.getQualifier().layoutPassthrough) { |
| 4090 |
builder.addMemberDecoration(spvType, member, (spv::Decoration)spv::DecorationPassthroughNV); |
| 4091 |
builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 4092 |
builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 4093 |
} |
| 4094 |
#endif |
| 4095 |
} |
| 4096 |
|
| 4097 |
// Decorate the structure |
| 4098 |
builder.addDecoration(spvType, TranslateLayoutDecoration(type, qualifier.layoutMatrix)); |
| 4099 |
builder.addDecoration(spvType, TranslateBlockDecoration(type, glslangIntermediate->usingStorageBuffer())); |
| 4100 |
} |
| 4101 |
|
| 4102 |
// Turn the expression forming the array size into an id. |
| 4103 |
// This is not quite trivial, because of specialization constants. |
| 4104 |
// Sometimes, a raw constant is turned into an Id, and sometimes |
| 4105 |
// a specialization constant expression is. |
| 4106 |
spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim) |
| 4107 |
{ |
| 4108 |
// First, see if this is sized with a node, meaning a specialization constant: |
| 4109 |
glslang::TIntermTyped* specNode = arraySizes.getDimNode(dim); |
| 4110 |
if (specNode != nullptr) { |
| 4111 |
builder.clearAccessChain(); |
| 4112 |
specNode->traverse(this); |
| 4113 |
return accessChainLoad(specNode->getAsTyped()->getType()); |
| 4114 |
} |
| 4115 |
|
| 4116 |
// Otherwise, need a compile-time (front end) size, get it: |
| 4117 |
int size = arraySizes.getDimSize(dim); |
| 4118 |
assert(size > 0); |
| 4119 |
return builder.makeUintConstant(size); |
| 4120 |
} |
| 4121 |
|
| 4122 |
// Wrap the builder's accessChainLoad to: |
| 4123 |
// - localize handling of RelaxedPrecision |
| 4124 |
// - use the SPIR-V inferred type instead of another conversion of the glslang type |
| 4125 |
// (avoids unnecessary work and possible type punning for structures) |
| 4126 |
// - do conversion of concrete to abstract type |
| 4127 |
spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type) |
| 4128 |
{ |
| 4129 |
spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 4130 |
|
| 4131 |
spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4132 |
coherentFlags |= TranslateCoherent(type); |
| 4133 |
|
| 4134 |
unsigned int alignment = builder.getAccessChain().alignment; |
| 4135 |
alignment |= type.getBufferReferenceAlignment(); |
| 4136 |
|
| 4137 |
spv::Id loadedId = builder.accessChainLoad(TranslatePrecisionDecoration(type), |
| 4138 |
TranslateNonUniformDecoration(type.getQualifier()), |
| 4139 |
nominalTypeId, |
| 4140 |
spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & ~spv::MemoryAccessMakePointerAvailableKHRMask), |
| 4141 |
TranslateMemoryScope(coherentFlags), |
| 4142 |
alignment); |
| 4143 |
|
| 4144 |
// Need to convert to abstract types when necessary |
| 4145 |
if (type.getBasicType() == glslang::EbtBool) { |
| 4146 |
if (builder.isScalarType(nominalTypeId)) { |
| 4147 |
// Conversion for bool |
| 4148 |
spv::Id boolType = builder.makeBoolType(); |
| 4149 |
if (nominalTypeId != boolType) |
| 4150 |
loadedId = builder.createBinOp(spv::OpINotEqual, boolType, loadedId, builder.makeUintConstant(0)); |
| 4151 |
} else if (builder.isVectorType(nominalTypeId)) { |
| 4152 |
// Conversion for bvec |
| 4153 |
int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 4154 |
spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 4155 |
if (nominalTypeId != bvecType) |
| 4156 |
loadedId = builder.createBinOp(spv::OpINotEqual, bvecType, loadedId, |
| 4157 |
makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 4158 |
} |
| 4159 |
} |
| 4160 |
|
| 4161 |
return loadedId; |
| 4162 |
} |
| 4163 |
|
| 4164 |
// Wrap the builder's accessChainStore to: |
| 4165 |
// - do conversion of concrete to abstract type |
| 4166 |
// |
| 4167 |
// Implicitly uses the existing builder.accessChain as the storage target. |
| 4168 |
void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue) |
| 4169 |
{ |
| 4170 |
// Need to convert to abstract types when necessary |
| 4171 |
if (type.getBasicType() == glslang::EbtBool) { |
| 4172 |
spv::Id nominalTypeId = builder.accessChainGetInferredType(); |
| 4173 |
|
| 4174 |
if (builder.isScalarType(nominalTypeId)) { |
| 4175 |
// Conversion for bool |
| 4176 |
spv::Id boolType = builder.makeBoolType(); |
| 4177 |
if (nominalTypeId != boolType) { |
| 4178 |
// keep these outside arguments, for determinant order-of-evaluation |
| 4179 |
spv::Id one = builder.makeUintConstant(1); |
| 4180 |
spv::Id zero = builder.makeUintConstant(0); |
| 4181 |
rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 4182 |
} else if (builder.getTypeId(rvalue) != boolType) |
| 4183 |
rvalue = builder.createBinOp(spv::OpINotEqual, boolType, rvalue, builder.makeUintConstant(0)); |
| 4184 |
} else if (builder.isVectorType(nominalTypeId)) { |
| 4185 |
// Conversion for bvec |
| 4186 |
int vecSize = builder.getNumTypeComponents(nominalTypeId); |
| 4187 |
spv::Id bvecType = builder.makeVectorType(builder.makeBoolType(), vecSize); |
| 4188 |
if (nominalTypeId != bvecType) { |
| 4189 |
// keep these outside arguments, for determinant order-of-evaluation |
| 4190 |
spv::Id one = makeSmearedConstant(builder.makeUintConstant(1), vecSize); |
| 4191 |
spv::Id zero = makeSmearedConstant(builder.makeUintConstant(0), vecSize); |
| 4192 |
rvalue = builder.createTriOp(spv::OpSelect, nominalTypeId, rvalue, one, zero); |
| 4193 |
} else if (builder.getTypeId(rvalue) != bvecType) |
| 4194 |
rvalue = builder.createBinOp(spv::OpINotEqual, bvecType, rvalue, |
| 4195 |
makeSmearedConstant(builder.makeUintConstant(0), vecSize)); |
| 4196 |
} |
| 4197 |
} |
| 4198 |
|
| 4199 |
spv::Builder::AccessChain::CoherentFlags coherentFlags = builder.getAccessChain().coherentFlags; |
| 4200 |
coherentFlags |= TranslateCoherent(type); |
| 4201 |
|
| 4202 |
unsigned int alignment = builder.getAccessChain().alignment; |
| 4203 |
alignment |= type.getBufferReferenceAlignment(); |
| 4204 |
|
| 4205 |
builder.accessChainStore(rvalue, TranslateNonUniformDecoration(type.getQualifier()), |
| 4206 |
spv::MemoryAccessMask(TranslateMemoryAccess(coherentFlags) & |
| 4207 |
~spv::MemoryAccessMakePointerVisibleKHRMask), |
| 4208 |
TranslateMemoryScope(coherentFlags), alignment); |
| 4209 |
} |
| 4210 |
|
| 4211 |
// For storing when types match at the glslang level, but not might match at the |
| 4212 |
// SPIR-V level. |
| 4213 |
// |
| 4214 |
// This especially happens when a single glslang type expands to multiple |
| 4215 |
// SPIR-V types, like a struct that is used in a member-undecorated way as well |
| 4216 |
// as in a member-decorated way. |
| 4217 |
// |
| 4218 |
// NOTE: This function can handle any store request; if it's not special it |
| 4219 |
// simplifies to a simple OpStore. |
| 4220 |
// |
| 4221 |
// Implicitly uses the existing builder.accessChain as the storage target. |
| 4222 |
void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue) |
| 4223 |
{ |
| 4224 |
// we only do the complex path here if it's an aggregate |
| 4225 |
if (! type.isStruct() && ! type.isArray()) { |
| 4226 |
accessChainStore(type, rValue); |
| 4227 |
return; |
| 4228 |
} |
| 4229 |
|
| 4230 |
// and, it has to be a case of type aliasing |
| 4231 |
spv::Id rType = builder.getTypeId(rValue); |
| 4232 |
spv::Id lValue = builder.accessChainGetLValue(); |
| 4233 |
spv::Id lType = builder.getContainedTypeId(builder.getTypeId(lValue)); |
| 4234 |
if (lType == rType) { |
| 4235 |
accessChainStore(type, rValue); |
| 4236 |
return; |
| 4237 |
} |
| 4238 |
|
| 4239 |
// Recursively (as needed) copy an aggregate type to a different aggregate type, |
| 4240 |
// where the two types were the same type in GLSL. This requires member |
| 4241 |
// by member copy, recursively. |
| 4242 |
|
| 4243 |
// SPIR-V 1.4 added an instruction to do help do this. |
| 4244 |
if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4) { |
| 4245 |
// However, bool in uniform space is changed to int, so |
| 4246 |
// OpCopyLogical does not work for that. |
| 4247 |
// TODO: It would be more robust to do a full recursive verification of the types satisfying SPIR-V rules. |
| 4248 |
bool rBool = builder.containsType(builder.getTypeId(rValue), spv::OpTypeBool, 0); |
| 4249 |
bool lBool = builder.containsType(lType, spv::OpTypeBool, 0); |
| 4250 |
if (lBool == rBool) { |
| 4251 |
spv::Id logicalCopy = builder.createUnaryOp(spv::OpCopyLogical, lType, rValue); |
| 4252 |
accessChainStore(type, logicalCopy); |
| 4253 |
return; |
| 4254 |
} |
| 4255 |
} |
| 4256 |
|
| 4257 |
// If an array, copy element by element. |
| 4258 |
if (type.isArray()) { |
| 4259 |
glslang::TType glslangElementType(type, 0); |
| 4260 |
spv::Id elementRType = builder.getContainedTypeId(rType); |
| 4261 |
for (int index = 0; index < type.getOuterArraySize(); ++index) { |
| 4262 |
// get the source member |
| 4263 |
spv::Id elementRValue = builder.createCompositeExtract(rValue, elementRType, index); |
| 4264 |
|
| 4265 |
// set up the target storage |
| 4266 |
builder.clearAccessChain(); |
| 4267 |
builder.setAccessChainLValue(lValue); |
| 4268 |
builder.accessChainPush(builder.makeIntConstant(index), TranslateCoherent(type), |
| 4269 |
type.getBufferReferenceAlignment()); |
| 4270 |
|
| 4271 |
// store the member |
| 4272 |
multiTypeStore(glslangElementType, elementRValue); |
| 4273 |
} |
| 4274 |
} else { |
| 4275 |
assert(type.isStruct()); |
| 4276 |
|
| 4277 |
// loop over structure members |
| 4278 |
const glslang::TTypeList& members = *type.getStruct(); |
| 4279 |
for (int m = 0; m < (int)members.size(); ++m) { |
| 4280 |
const glslang::TType& glslangMemberType = *members[m].type; |
| 4281 |
|
| 4282 |
// get the source member |
| 4283 |
spv::Id memberRType = builder.getContainedTypeId(rType, m); |
| 4284 |
spv::Id memberRValue = builder.createCompositeExtract(rValue, memberRType, m); |
| 4285 |
|
| 4286 |
// set up the target storage |
| 4287 |
builder.clearAccessChain(); |
| 4288 |
builder.setAccessChainLValue(lValue); |
| 4289 |
builder.accessChainPush(builder.makeIntConstant(m), TranslateCoherent(type), |
| 4290 |
type.getBufferReferenceAlignment()); |
| 4291 |
|
| 4292 |
// store the member |
| 4293 |
multiTypeStore(glslangMemberType, memberRValue); |
| 4294 |
} |
| 4295 |
} |
| 4296 |
} |
| 4297 |
|
| 4298 |
// Decide whether or not this type should be |
| 4299 |
// decorated with offsets and strides, and if so |
| 4300 |
// whether std140 or std430 rules should be applied. |
| 4301 |
glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const |
| 4302 |
{ |
| 4303 |
// has to be a block |
| 4304 |
if (type.getBasicType() != glslang::EbtBlock) |
| 4305 |
return glslang::ElpNone; |
| 4306 |
|
| 4307 |
// has to be a uniform or buffer block or task in/out blocks |
| 4308 |
if (type.getQualifier().storage != glslang::EvqUniform && |
| 4309 |
type.getQualifier().storage != glslang::EvqBuffer && |
| 4310 |
!type.getQualifier().isTaskMemory()) |
| 4311 |
return glslang::ElpNone; |
| 4312 |
|
| 4313 |
// return the layout to use |
| 4314 |
switch (type.getQualifier().layoutPacking) { |
| 4315 |
case glslang::ElpStd140: |
| 4316 |
case glslang::ElpStd430: |
| 4317 |
case glslang::ElpScalar: |
| 4318 |
return type.getQualifier().layoutPacking; |
| 4319 |
default: |
| 4320 |
return glslang::ElpNone; |
| 4321 |
} |
| 4322 |
} |
| 4323 |
|
| 4324 |
// Given an array type, returns the integer stride required for that array |
| 4325 |
int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout, |
| 4326 |
glslang::TLayoutMatrix matrixLayout) |
| 4327 |
{ |
| 4328 |
int size; |
| 4329 |
int stride; |
| 4330 |
glslangIntermediate->getMemberAlignment(arrayType, size, stride, explicitLayout, |
| 4331 |
matrixLayout == glslang::ElmRowMajor); |
| 4332 |
|
| 4333 |
return stride; |
| 4334 |
} |
| 4335 |
|
| 4336 |
// Given a matrix type, or array (of array) of matrixes type, returns the integer stride required for that matrix |
| 4337 |
// when used as a member of an interface block |
| 4338 |
int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout, |
| 4339 |
glslang::TLayoutMatrix matrixLayout) |
| 4340 |
{ |
| 4341 |
glslang::TType elementType; |
| 4342 |
elementType.shallowCopy(matrixType); |
| 4343 |
elementType.clearArraySizes(); |
| 4344 |
|
| 4345 |
int size; |
| 4346 |
int stride; |
| 4347 |
glslangIntermediate->getMemberAlignment(elementType, size, stride, explicitLayout, |
| 4348 |
matrixLayout == glslang::ElmRowMajor); |
| 4349 |
|
| 4350 |
return stride; |
| 4351 |
} |
| 4352 |
|
| 4353 |
// Given a member type of a struct, realign the current offset for it, and compute |
| 4354 |
// the next (not yet aligned) offset for the next member, which will get aligned |
| 4355 |
// on the next call. |
| 4356 |
// 'currentOffset' should be passed in already initialized, ready to modify, and reflecting |
| 4357 |
// the migration of data from nextOffset -> currentOffset. It should be -1 on the first call. |
| 4358 |
// -1 means a non-forced member offset (no decoration needed). |
| 4359 |
void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType, |
| 4360 |
int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout) |
| 4361 |
{ |
| 4362 |
// this will get a positive value when deemed necessary |
| 4363 |
nextOffset = -1; |
| 4364 |
|
| 4365 |
// override anything in currentOffset with user-set offset |
| 4366 |
if (memberType.getQualifier().hasOffset()) |
| 4367 |
currentOffset = memberType.getQualifier().layoutOffset; |
| 4368 |
|
| 4369 |
// It could be that current linker usage in glslang updated all the layoutOffset, |
| 4370 |
// in which case the following code does not matter. But, that's not quite right |
| 4371 |
// once cross-compilation unit GLSL validation is done, as the original user |
| 4372 |
// settings are needed in layoutOffset, and then the following will come into play. |
| 4373 |
|
| 4374 |
if (explicitLayout == glslang::ElpNone) { |
| 4375 |
if (! memberType.getQualifier().hasOffset()) |
| 4376 |
currentOffset = -1; |
| 4377 |
|
| 4378 |
return; |
| 4379 |
} |
| 4380 |
|
| 4381 |
// Getting this far means we need explicit offsets |
| 4382 |
if (currentOffset < 0) |
| 4383 |
currentOffset = 0; |
| 4384 |
|
| 4385 |
// Now, currentOffset is valid (either 0, or from a previous nextOffset), |
| 4386 |
// but possibly not yet correctly aligned. |
| 4387 |
|
| 4388 |
int memberSize; |
| 4389 |
int dummyStride; |
| 4390 |
int memberAlignment = glslangIntermediate->getMemberAlignment(memberType, memberSize, dummyStride, explicitLayout, |
| 4391 |
matrixLayout == glslang::ElmRowMajor); |
| 4392 |
|
| 4393 |
// Adjust alignment for HLSL rules |
| 4394 |
// TODO: make this consistent in early phases of code: |
| 4395 |
// adjusting this late means inconsistencies with earlier code, which for reflection is an issue |
| 4396 |
// Until reflection is brought in sync with these adjustments, don't apply to $Global, |
| 4397 |
// which is the most likely to rely on reflection, and least likely to rely implicit layouts |
| 4398 |
if (glslangIntermediate->usingHlslOffsets() && |
| 4399 |
! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { |
| 4400 |
int dummySize; |
| 4401 |
int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); |
| 4402 |
if (componentAlignment <= 4) |
| 4403 |
memberAlignment = componentAlignment; |
| 4404 |
} |
| 4405 |
|
| 4406 |
// Bump up to member alignment |
| 4407 |
glslang::RoundToPow2(currentOffset, memberAlignment); |
| 4408 |
|
| 4409 |
// Bump up to vec4 if there is a bad straddle |
| 4410 |
if (explicitLayout != glslang::ElpScalar && glslangIntermediate->improperStraddle(memberType, memberSize, |
| 4411 |
currentOffset)) |
| 4412 |
glslang::RoundToPow2(currentOffset, 16); |
| 4413 |
|
| 4414 |
nextOffset = currentOffset + memberSize; |
| 4415 |
} |
| 4416 |
|
| 4417 |
void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember) |
| 4418 |
{ |
| 4419 |
const glslang::TBuiltInVariable glslangBuiltIn = members[glslangMember].type->getQualifier().builtIn; |
| 4420 |
switch (glslangBuiltIn) |
| 4421 |
{ |
| 4422 |
case glslang::EbvPointSize: |
| 4423 |
#ifndef GLSLANG_WEB |
| 4424 |
case glslang::EbvClipDistance: |
| 4425 |
case glslang::EbvCullDistance: |
| 4426 |
case glslang::EbvViewportMaskNV: |
| 4427 |
case glslang::EbvSecondaryPositionNV: |
| 4428 |
case glslang::EbvSecondaryViewportMaskNV: |
| 4429 |
case glslang::EbvPositionPerViewNV: |
| 4430 |
case glslang::EbvViewportMaskPerViewNV: |
| 4431 |
case glslang::EbvTaskCountNV: |
| 4432 |
case glslang::EbvPrimitiveCountNV: |
| 4433 |
case glslang::EbvPrimitiveIndicesNV: |
| 4434 |
case glslang::EbvClipDistancePerViewNV: |
| 4435 |
case glslang::EbvCullDistancePerViewNV: |
| 4436 |
case glslang::EbvLayerPerViewNV: |
| 4437 |
case glslang::EbvMeshViewCountNV: |
| 4438 |
case glslang::EbvMeshViewIndicesNV: |
| 4439 |
#endif |
| 4440 |
// Generate the associated capability. Delegate to TranslateBuiltInDecoration. |
| 4441 |
// Alternately, we could just call this for any glslang built-in, since the |
| 4442 |
// capability already guards against duplicates. |
| 4443 |
TranslateBuiltInDecoration(glslangBuiltIn, false); |
| 4444 |
break; |
| 4445 |
default: |
| 4446 |
// Capabilities were already generated when the struct was declared. |
| 4447 |
break; |
| 4448 |
} |
| 4449 |
} |
| 4450 |
|
| 4451 |
bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node) |
| 4452 |
{ |
| 4453 |
return node->getName().compare(glslangIntermediate->getEntryPointMangledName().c_str()) == 0; |
| 4454 |
} |
| 4455 |
|
| 4456 |
// Does parameter need a place to keep writes, separate from the original? |
| 4457 |
// Assumes called after originalParam(), which filters out block/buffer/opaque-based |
| 4458 |
// qualifiers such that we should have only in/out/inout/constreadonly here. |
| 4459 |
bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const |
| 4460 |
{ |
| 4461 |
assert(qualifier == glslang::EvqIn || |
| 4462 |
qualifier == glslang::EvqOut || |
| 4463 |
qualifier == glslang::EvqInOut || |
| 4464 |
qualifier == glslang::EvqUniform || |
| 4465 |
qualifier == glslang::EvqConstReadOnly); |
| 4466 |
return qualifier != glslang::EvqConstReadOnly && |
| 4467 |
qualifier != glslang::EvqUniform; |
| 4468 |
} |
| 4469 |
|
| 4470 |
// Is parameter pass-by-original? |
| 4471 |
bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType, |
| 4472 |
bool implicitThisParam) |
| 4473 |
{ |
| 4474 |
if (implicitThisParam) // implicit this |
| 4475 |
return true; |
| 4476 |
if (glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
| 4477 |
return paramType.getBasicType() == glslang::EbtBlock; |
| 4478 |
return paramType.containsOpaque() || // sampler, etc. |
| 4479 |
(paramType.getBasicType() == glslang::EbtBlock && qualifier == glslang::EvqBuffer); // SSBO |
| 4480 |
} |
| 4481 |
|
| 4482 |
// Make all the functions, skeletally, without actually visiting their bodies. |
| 4483 |
void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4484 |
{ |
| 4485 |
const auto getParamDecorations = [&](std::vector<spv::Decoration>& decorations, const glslang::TType& type, |
| 4486 |
bool useVulkanMemoryModel) { |
| 4487 |
spv::Decoration paramPrecision = TranslatePrecisionDecoration(type); |
| 4488 |
if (paramPrecision != spv::NoPrecision) |
| 4489 |
decorations.push_back(paramPrecision); |
| 4490 |
TranslateMemoryDecoration(type.getQualifier(), decorations, useVulkanMemoryModel); |
| 4491 |
if (type.isReference()) { |
| 4492 |
// Original and non-writable params pass the pointer directly and |
| 4493 |
// use restrict/aliased, others are stored to a pointer in Function |
| 4494 |
// memory and use RestrictPointer/AliasedPointer. |
| 4495 |
if (originalParam(type.getQualifier().storage, type, false) || |
| 4496 |
!writableParam(type.getQualifier().storage)) { |
| 4497 |
decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrict : |
| 4498 |
spv::DecorationAliased); |
| 4499 |
} else { |
| 4500 |
decorations.push_back(type.getQualifier().isRestrict() ? spv::DecorationRestrictPointerEXT : |
| 4501 |
spv::DecorationAliasedPointerEXT); |
| 4502 |
} |
| 4503 |
} |
| 4504 |
}; |
| 4505 |
|
| 4506 |
for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4507 |
glslang::TIntermAggregate* glslFunction = glslFunctions[f]->getAsAggregate(); |
| 4508 |
if (! glslFunction || glslFunction->getOp() != glslang::EOpFunction || isShaderEntryPoint(glslFunction)) |
| 4509 |
continue; |
| 4510 |
|
| 4511 |
// We're on a user function. Set up the basic interface for the function now, |
| 4512 |
// so that it's available to call. Translating the body will happen later. |
| 4513 |
// |
| 4514 |
// Typically (except for a "const in" parameter), an address will be passed to the |
| 4515 |
// function. What it is an address of varies: |
| 4516 |
// |
| 4517 |
// - "in" parameters not marked as "const" can be written to without modifying the calling |
| 4518 |
// argument so that write needs to be to a copy, hence the address of a copy works. |
| 4519 |
// |
| 4520 |
// - "const in" parameters can just be the r-value, as no writes need occur. |
| 4521 |
// |
| 4522 |
// - "out" and "inout" arguments can't be done as pointers to the calling argument, because |
| 4523 |
// GLSL has copy-in/copy-out semantics. They can be handled though with a pointer to a copy. |
| 4524 |
|
| 4525 |
std::vector<spv::Id> paramTypes; |
| 4526 |
std::vector<std::vector<spv::Decoration>> paramDecorations; // list of decorations per parameter |
| 4527 |
glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence(); |
| 4528 |
|
| 4529 |
#ifdef ENABLE_HLSL |
| 4530 |
bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == |
| 4531 |
glslangIntermediate->implicitThisName; |
| 4532 |
#else |
| 4533 |
bool implicitThis = false; |
| 4534 |
#endif |
| 4535 |
|
| 4536 |
paramDecorations.resize(parameters.size()); |
| 4537 |
for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4538 |
const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4539 |
spv::Id typeId = convertGlslangToSpvType(paramType); |
| 4540 |
if (originalParam(paramType.getQualifier().storage, paramType, implicitThis && p == 0)) |
| 4541 |
typeId = builder.makePointer(TranslateStorageClass(paramType), typeId); |
| 4542 |
else if (writableParam(paramType.getQualifier().storage)) |
| 4543 |
typeId = builder.makePointer(spv::StorageClassFunction, typeId); |
| 4544 |
else |
| 4545 |
rValueParameters.insert(parameters[p]->getAsSymbolNode()->getId()); |
| 4546 |
getParamDecorations(paramDecorations[p], paramType, glslangIntermediate->usingVulkanMemoryModel()); |
| 4547 |
paramTypes.push_back(typeId); |
| 4548 |
} |
| 4549 |
|
| 4550 |
spv::Block* functionBlock; |
| 4551 |
spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()), |
| 4552 |
convertGlslangToSpvType(glslFunction->getType()), |
| 4553 |
glslFunction->getName().c_str(), paramTypes, |
| 4554 |
paramDecorations, &functionBlock); |
| 4555 |
if (implicitThis) |
| 4556 |
function->setImplicitThis(); |
| 4557 |
|
| 4558 |
// Track function to emit/call later |
| 4559 |
functionMap[glslFunction->getName().c_str()] = function; |
| 4560 |
|
| 4561 |
// Set the parameter id's |
| 4562 |
for (int p = 0; p < (int)parameters.size(); ++p) { |
| 4563 |
symbolValues[parameters[p]->getAsSymbolNode()->getId()] = function->getParamId(p); |
| 4564 |
// give a name too |
| 4565 |
builder.addName(function->getParamId(p), parameters[p]->getAsSymbolNode()->getName().c_str()); |
| 4566 |
|
| 4567 |
const glslang::TType& paramType = parameters[p]->getAsTyped()->getType(); |
| 4568 |
if (paramType.contains8BitInt()) |
| 4569 |
builder.addCapability(spv::CapabilityInt8); |
| 4570 |
if (paramType.contains16BitInt()) |
| 4571 |
builder.addCapability(spv::CapabilityInt16); |
| 4572 |
if (paramType.contains16BitFloat()) |
| 4573 |
builder.addCapability(spv::CapabilityFloat16); |
| 4574 |
} |
| 4575 |
} |
| 4576 |
} |
| 4577 |
|
| 4578 |
// Process all the initializers, while skipping the functions and link objects |
| 4579 |
void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers) |
| 4580 |
{ |
| 4581 |
builder.setBuildPoint(shaderEntry->getLastBlock()); |
| 4582 |
for (int i = 0; i < (int)initializers.size(); ++i) { |
| 4583 |
glslang::TIntermAggregate* initializer = initializers[i]->getAsAggregate(); |
| 4584 |
if (initializer && initializer->getOp() != glslang::EOpFunction && initializer->getOp() != |
| 4585 |
glslang::EOpLinkerObjects) { |
| 4586 |
|
| 4587 |
// We're on a top-level node that's not a function. Treat as an initializer, whose |
| 4588 |
// code goes into the beginning of the entry point. |
| 4589 |
initializer->traverse(this); |
| 4590 |
} |
| 4591 |
} |
| 4592 |
} |
| 4593 |
|
| 4594 |
// Process all the functions, while skipping initializers. |
| 4595 |
void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions) |
| 4596 |
{ |
| 4597 |
for (int f = 0; f < (int)glslFunctions.size(); ++f) { |
| 4598 |
glslang::TIntermAggregate* node = glslFunctions[f]->getAsAggregate(); |
| 4599 |
if (node && (node->getOp() == glslang::EOpFunction || node->getOp() == glslang::EOpLinkerObjects)) |
| 4600 |
node->traverse(this); |
| 4601 |
} |
| 4602 |
} |
| 4603 |
|
| 4604 |
void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node) |
| 4605 |
{ |
| 4606 |
// SPIR-V functions should already be in the functionMap from the prepass |
| 4607 |
// that called makeFunctions(). |
| 4608 |
currentFunction = functionMap[node->getName().c_str()]; |
| 4609 |
spv::Block* functionBlock = currentFunction->getEntryBlock(); |
| 4610 |
builder.setBuildPoint(functionBlock); |
| 4611 |
} |
| 4612 |
|
| 4613 |
void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments, |
| 4614 |
spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
| 4615 |
{ |
| 4616 |
const glslang::TIntermSequence& glslangArguments = node.getSequence(); |
| 4617 |
|
| 4618 |
glslang::TSampler sampler = {}; |
| 4619 |
bool cubeCompare = false; |
| 4620 |
#ifndef GLSLANG_WEB |
| 4621 |
bool f16ShadowCompare = false; |
| 4622 |
#endif |
| 4623 |
if (node.isTexture() || node.isImage()) { |
| 4624 |
sampler = glslangArguments[0]->getAsTyped()->getType().getSampler(); |
| 4625 |
cubeCompare = sampler.dim == glslang::EsdCube && sampler.arrayed && sampler.shadow; |
| 4626 |
#ifndef GLSLANG_WEB |
| 4627 |
f16ShadowCompare = sampler.shadow && |
| 4628 |
glslangArguments[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16; |
| 4629 |
#endif |
| 4630 |
} |
| 4631 |
|
| 4632 |
for (int i = 0; i < (int)glslangArguments.size(); ++i) { |
| 4633 |
builder.clearAccessChain(); |
| 4634 |
glslangArguments[i]->traverse(this); |
| 4635 |
|
| 4636 |
#ifndef GLSLANG_WEB |
| 4637 |
// Special case l-value operands |
| 4638 |
bool lvalue = false; |
| 4639 |
switch (node.getOp()) { |
| 4640 |
case glslang::EOpImageAtomicAdd: |
| 4641 |
case glslang::EOpImageAtomicMin: |
| 4642 |
case glslang::EOpImageAtomicMax: |
| 4643 |
case glslang::EOpImageAtomicAnd: |
| 4644 |
case glslang::EOpImageAtomicOr: |
| 4645 |
case glslang::EOpImageAtomicXor: |
| 4646 |
case glslang::EOpImageAtomicExchange: |
| 4647 |
case glslang::EOpImageAtomicCompSwap: |
| 4648 |
case glslang::EOpImageAtomicLoad: |
| 4649 |
case glslang::EOpImageAtomicStore: |
| 4650 |
if (i == 0) |
| 4651 |
lvalue = true; |
| 4652 |
break; |
| 4653 |
case glslang::EOpSparseImageLoad: |
| 4654 |
if ((sampler.ms && i == 3) || (! sampler.ms && i == 2)) |
| 4655 |
lvalue = true; |
| 4656 |
break; |
| 4657 |
case glslang::EOpSparseTexture: |
| 4658 |
if (((cubeCompare || f16ShadowCompare) && i == 3) || (! (cubeCompare || f16ShadowCompare) && i == 2)) |
| 4659 |
lvalue = true; |
| 4660 |
break; |
| 4661 |
case glslang::EOpSparseTextureClamp: |
| 4662 |
if (((cubeCompare || f16ShadowCompare) && i == 4) || (! (cubeCompare || f16ShadowCompare) && i == 3)) |
| 4663 |
lvalue = true; |
| 4664 |
break; |
| 4665 |
case glslang::EOpSparseTextureLod: |
| 4666 |
case glslang::EOpSparseTextureOffset: |
| 4667 |
if ((f16ShadowCompare && i == 4) || (! f16ShadowCompare && i == 3)) |
| 4668 |
lvalue = true; |
| 4669 |
break; |
| 4670 |
case glslang::EOpSparseTextureFetch: |
| 4671 |
if ((sampler.dim != glslang::EsdRect && i == 3) || (sampler.dim == glslang::EsdRect && i == 2)) |
| 4672 |
lvalue = true; |
| 4673 |
break; |
| 4674 |
case glslang::EOpSparseTextureFetchOffset: |
| 4675 |
if ((sampler.dim != glslang::EsdRect && i == 4) || (sampler.dim == glslang::EsdRect && i == 3)) |
| 4676 |
lvalue = true; |
| 4677 |
break; |
| 4678 |
case glslang::EOpSparseTextureLodOffset: |
| 4679 |
case glslang::EOpSparseTextureGrad: |
| 4680 |
case glslang::EOpSparseTextureOffsetClamp: |
| 4681 |
if ((f16ShadowCompare && i == 5) || (! f16ShadowCompare && i == 4)) |
| 4682 |
lvalue = true; |
| 4683 |
break; |
| 4684 |
case glslang::EOpSparseTextureGradOffset: |
| 4685 |
case glslang::EOpSparseTextureGradClamp: |
| 4686 |
if ((f16ShadowCompare && i == 6) || (! f16ShadowCompare && i == 5)) |
| 4687 |
lvalue = true; |
| 4688 |
break; |
| 4689 |
case glslang::EOpSparseTextureGradOffsetClamp: |
| 4690 |
if ((f16ShadowCompare && i == 7) || (! f16ShadowCompare && i == 6)) |
| 4691 |
lvalue = true; |
| 4692 |
break; |
| 4693 |
case glslang::EOpSparseTextureGather: |
| 4694 |
if ((sampler.shadow && i == 3) || (! sampler.shadow && i == 2)) |
| 4695 |
lvalue = true; |
| 4696 |
break; |
| 4697 |
case glslang::EOpSparseTextureGatherOffset: |
| 4698 |
case glslang::EOpSparseTextureGatherOffsets: |
| 4699 |
if ((sampler.shadow && i == 4) || (! sampler.shadow && i == 3)) |
| 4700 |
lvalue = true; |
| 4701 |
break; |
| 4702 |
case glslang::EOpSparseTextureGatherLod: |
| 4703 |
if (i == 3) |
| 4704 |
lvalue = true; |
| 4705 |
break; |
| 4706 |
case glslang::EOpSparseTextureGatherLodOffset: |
| 4707 |
case glslang::EOpSparseTextureGatherLodOffsets: |
| 4708 |
if (i == 4) |
| 4709 |
lvalue = true; |
| 4710 |
break; |
| 4711 |
case glslang::EOpSparseImageLoadLod: |
| 4712 |
if (i == 3) |
| 4713 |
lvalue = true; |
| 4714 |
break; |
| 4715 |
case glslang::EOpImageSampleFootprintNV: |
| 4716 |
if (i == 4) |
| 4717 |
lvalue = true; |
| 4718 |
break; |
| 4719 |
case glslang::EOpImageSampleFootprintClampNV: |
| 4720 |
case glslang::EOpImageSampleFootprintLodNV: |
| 4721 |
if (i == 5) |
| 4722 |
lvalue = true; |
| 4723 |
break; |
| 4724 |
case glslang::EOpImageSampleFootprintGradNV: |
| 4725 |
if (i == 6) |
| 4726 |
lvalue = true; |
| 4727 |
break; |
| 4728 |
case glslang::EOpImageSampleFootprintGradClampNV: |
| 4729 |
if (i == 7) |
| 4730 |
lvalue = true; |
| 4731 |
break; |
| 4732 |
default: |
| 4733 |
break; |
| 4734 |
} |
| 4735 |
|
| 4736 |
if (lvalue) { |
| 4737 |
arguments.push_back(builder.accessChainGetLValue()); |
| 4738 |
lvalueCoherentFlags = builder.getAccessChain().coherentFlags; |
| 4739 |
lvalueCoherentFlags |= TranslateCoherent(glslangArguments[i]->getAsTyped()->getType()); |
| 4740 |
} else |
| 4741 |
#endif |
| 4742 |
arguments.push_back(accessChainLoad(glslangArguments[i]->getAsTyped()->getType())); |
| 4743 |
} |
| 4744 |
} |
| 4745 |
|
| 4746 |
void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments) |
| 4747 |
{ |
| 4748 |
builder.clearAccessChain(); |
| 4749 |
node.getOperand()->traverse(this); |
| 4750 |
arguments.push_back(accessChainLoad(node.getOperand()->getType())); |
| 4751 |
} |
| 4752 |
|
| 4753 |
spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node) |
| 4754 |
{ |
| 4755 |
if (! node->isImage() && ! node->isTexture()) |
| 4756 |
return spv::NoResult; |
| 4757 |
|
| 4758 |
builder.setLine(node->getLoc().line, node->getLoc().getFilename()); |
| 4759 |
|
| 4760 |
// Process a GLSL texturing op (will be SPV image) |
| 4761 |
|
| 4762 |
const glslang::TType &imageType = node->getAsAggregate() |
| 4763 |
? node->getAsAggregate()->getSequence()[0]->getAsTyped()->getType() |
| 4764 |
: node->getAsUnaryNode()->getOperand()->getAsTyped()->getType(); |
| 4765 |
const glslang::TSampler sampler = imageType.getSampler(); |
| 4766 |
#ifdef GLSLANG_WEB |
| 4767 |
const bool f16ShadowCompare = false; |
| 4768 |
#else |
| 4769 |
bool f16ShadowCompare = (sampler.shadow && node->getAsAggregate()) |
| 4770 |
? node->getAsAggregate()->getSequence()[1]->getAsTyped()->getType().getBasicType() == glslang::EbtFloat16 |
| 4771 |
: false; |
| 4772 |
#endif |
| 4773 |
|
| 4774 |
const auto signExtensionMask = [&]() { |
| 4775 |
if (builder.getSpvVersion() >= spv::Spv_1_4) { |
| 4776 |
if (sampler.type == glslang::EbtUint) |
| 4777 |
return spv::ImageOperandsZeroExtendMask; |
| 4778 |
else if (sampler.type == glslang::EbtInt) |
| 4779 |
return spv::ImageOperandsSignExtendMask; |
| 4780 |
} |
| 4781 |
return spv::ImageOperandsMaskNone; |
| 4782 |
}; |
| 4783 |
|
| 4784 |
spv::Builder::AccessChain::CoherentFlags lvalueCoherentFlags; |
| 4785 |
|
| 4786 |
std::vector<spv::Id> arguments; |
| 4787 |
if (node->getAsAggregate()) |
| 4788 |
translateArguments(*node->getAsAggregate(), arguments, lvalueCoherentFlags); |
| 4789 |
else |
| 4790 |
translateArguments(*node->getAsUnaryNode(), arguments); |
| 4791 |
spv::Decoration precision = TranslatePrecisionDecoration(node->getType()); |
| 4792 |
|
| 4793 |
spv::Builder::TextureParameters params = { }; |
| 4794 |
params.sampler = arguments[0]; |
| 4795 |
|
| 4796 |
glslang::TCrackedTextureOp cracked; |
| 4797 |
node->crackTexture(sampler, cracked); |
| 4798 |
|
| 4799 |
const bool isUnsignedResult = node->getType().getBasicType() == glslang::EbtUint; |
| 4800 |
|
| 4801 |
if (builder.isSampledImage(params.sampler) && |
| 4802 |
((cracked.query && node->getOp() != glslang::EOpTextureQueryLod) || cracked.fragMask || cracked.fetch)) { |
| 4803 |
params.sampler = builder.createUnaryOp(spv::OpImage, builder.getImageType(params.sampler), params.sampler); |
| 4804 |
if (imageType.getQualifier().isNonUniform()) { |
| 4805 |
builder.addDecoration(params.sampler, spv::DecorationNonUniformEXT); |
| 4806 |
} |
| 4807 |
} |
| 4808 |
// Check for queries |
| 4809 |
if (cracked.query) { |
| 4810 |
switch (node->getOp()) { |
| 4811 |
case glslang::EOpImageQuerySize: |
| 4812 |
case glslang::EOpTextureQuerySize: |
| 4813 |
if (arguments.size() > 1) { |
| 4814 |
params.lod = arguments[1]; |
| 4815 |
return builder.createTextureQueryCall(spv::OpImageQuerySizeLod, params, isUnsignedResult); |
| 4816 |
} else |
| 4817 |
return builder.createTextureQueryCall(spv::OpImageQuerySize, params, isUnsignedResult); |
| 4818 |
#ifndef GLSLANG_WEB |
| 4819 |
case glslang::EOpImageQuerySamples: |
| 4820 |
case glslang::EOpTextureQuerySamples: |
| 4821 |
return builder.createTextureQueryCall(spv::OpImageQuerySamples, params, isUnsignedResult); |
| 4822 |
case glslang::EOpTextureQueryLod: |
| 4823 |
params.coords = arguments[1]; |
| 4824 |
return builder.createTextureQueryCall(spv::OpImageQueryLod, params, isUnsignedResult); |
| 4825 |
case glslang::EOpTextureQueryLevels: |
| 4826 |
return builder.createTextureQueryCall(spv::OpImageQueryLevels, params, isUnsignedResult); |
| 4827 |
case glslang::EOpSparseTexelsResident: |
| 4828 |
return builder.createUnaryOp(spv::OpImageSparseTexelsResident, builder.makeBoolType(), arguments[0]); |
| 4829 |
#endif |
| 4830 |
default: |
| 4831 |
assert(0); |
| 4832 |
break; |
| 4833 |
} |
| 4834 |
} |
| 4835 |
|
| 4836 |
int components = node->getType().getVectorSize(); |
| 4837 |
|
| 4838 |
if (node->getOp() == glslang::EOpTextureFetch) { |
| 4839 |
// These must produce 4 components, per SPIR-V spec. We'll add a conversion constructor if needed. |
| 4840 |
// This will only happen through the HLSL path for operator[], so we do not have to handle e.g. |
| 4841 |
// the EOpTexture/Proj/Lod/etc family. It would be harmless to do so, but would need more logic |
| 4842 |
// here around e.g. which ones return scalars or other types. |
| 4843 |
components = 4; |
| 4844 |
} |
| 4845 |
|
| 4846 |
glslang::TType returnType(node->getType().getBasicType(), glslang::EvqTemporary, components); |
| 4847 |
|
| 4848 |
auto resultType = [&returnType,this]{ return convertGlslangToSpvType(returnType); }; |
| 4849 |
|
| 4850 |
// Check for image functions other than queries |
| 4851 |
if (node->isImage()) { |
| 4852 |
std::vector<spv::IdImmediate> operands; |
| 4853 |
auto opIt = arguments.begin(); |
| 4854 |
spv::IdImmediate image = { true, *(opIt++) }; |
| 4855 |
operands.push_back(image); |
| 4856 |
|
| 4857 |
// Handle subpass operations |
| 4858 |
// TODO: GLSL should change to have the "MS" only on the type rather than the |
| 4859 |
// built-in function. |
| 4860 |
if (cracked.subpass) { |
| 4861 |
// add on the (0,0) coordinate |
| 4862 |
spv::Id zero = builder.makeIntConstant(0); |
| 4863 |
std::vector<spv::Id> comps; |
| 4864 |
comps.push_back(zero); |
| 4865 |
comps.push_back(zero); |
| 4866 |
spv::IdImmediate coord = { true, |
| 4867 |
builder.makeCompositeConstant(builder.makeVectorType(builder.makeIntType(32), 2), comps) }; |
| 4868 |
operands.push_back(coord); |
| 4869 |
spv::IdImmediate imageOperands = { false, spv::ImageOperandsMaskNone }; |
| 4870 |
imageOperands.word = imageOperands.word | signExtensionMask(); |
| 4871 |
if (sampler.isMultiSample()) { |
| 4872 |
imageOperands.word = imageOperands.word | spv::ImageOperandsSampleMask; |
| 4873 |
} |
| 4874 |
if (imageOperands.word != spv::ImageOperandsMaskNone) { |
| 4875 |
operands.push_back(imageOperands); |
| 4876 |
if (sampler.isMultiSample()) { |
| 4877 |
spv::IdImmediate imageOperand = { true, *(opIt++) }; |
| 4878 |
operands.push_back(imageOperand); |
| 4879 |
} |
| 4880 |
} |
| 4881 |
spv::Id result = builder.createOp(spv::OpImageRead, resultType(), operands); |
| 4882 |
builder.setPrecision(result, precision); |
| 4883 |
return result; |
| 4884 |
} |
| 4885 |
|
| 4886 |
spv::IdImmediate coord = { true, *(opIt++) }; |
| 4887 |
operands.push_back(coord); |
| 4888 |
if (node->getOp() == glslang::EOpImageLoad || node->getOp() == glslang::EOpImageLoadLod) { |
| 4889 |
spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 4890 |
if (sampler.isMultiSample()) { |
| 4891 |
mask = mask | spv::ImageOperandsSampleMask; |
| 4892 |
} |
| 4893 |
if (cracked.lod) { |
| 4894 |
builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4895 |
builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4896 |
mask = mask | spv::ImageOperandsLodMask; |
| 4897 |
} |
| 4898 |
mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4899 |
mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 4900 |
mask = mask | signExtensionMask(); |
| 4901 |
if (mask != spv::ImageOperandsMaskNone) { |
| 4902 |
spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4903 |
operands.push_back(imageOperands); |
| 4904 |
} |
| 4905 |
if (mask & spv::ImageOperandsSampleMask) { |
| 4906 |
spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4907 |
operands.push_back(imageOperand); |
| 4908 |
} |
| 4909 |
if (mask & spv::ImageOperandsLodMask) { |
| 4910 |
spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4911 |
operands.push_back(imageOperand); |
| 4912 |
} |
| 4913 |
if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 4914 |
spv::IdImmediate imageOperand = { true, |
| 4915 |
builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4916 |
operands.push_back(imageOperand); |
| 4917 |
} |
| 4918 |
|
| 4919 |
if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
| 4920 |
builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4921 |
|
| 4922 |
std::vector<spv::Id> result(1, builder.createOp(spv::OpImageRead, resultType(), operands)); |
| 4923 |
builder.setPrecision(result[0], precision); |
| 4924 |
|
| 4925 |
// If needed, add a conversion constructor to the proper size. |
| 4926 |
if (components != node->getType().getVectorSize()) |
| 4927 |
result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 4928 |
|
| 4929 |
return result[0]; |
| 4930 |
} else if (node->getOp() == glslang::EOpImageStore || node->getOp() == glslang::EOpImageStoreLod) { |
| 4931 |
|
| 4932 |
// Push the texel value before the operands |
| 4933 |
if (sampler.isMultiSample() || cracked.lod) { |
| 4934 |
spv::IdImmediate texel = { true, *(opIt + 1) }; |
| 4935 |
operands.push_back(texel); |
| 4936 |
} else { |
| 4937 |
spv::IdImmediate texel = { true, *opIt }; |
| 4938 |
operands.push_back(texel); |
| 4939 |
} |
| 4940 |
|
| 4941 |
spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 4942 |
if (sampler.isMultiSample()) { |
| 4943 |
mask = mask | spv::ImageOperandsSampleMask; |
| 4944 |
} |
| 4945 |
if (cracked.lod) { |
| 4946 |
builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4947 |
builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4948 |
mask = mask | spv::ImageOperandsLodMask; |
| 4949 |
} |
| 4950 |
mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4951 |
mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelVisibleKHRMask); |
| 4952 |
mask = mask | signExtensionMask(); |
| 4953 |
if (mask != spv::ImageOperandsMaskNone) { |
| 4954 |
spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4955 |
operands.push_back(imageOperands); |
| 4956 |
} |
| 4957 |
if (mask & spv::ImageOperandsSampleMask) { |
| 4958 |
spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4959 |
operands.push_back(imageOperand); |
| 4960 |
} |
| 4961 |
if (mask & spv::ImageOperandsLodMask) { |
| 4962 |
spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 4963 |
operands.push_back(imageOperand); |
| 4964 |
} |
| 4965 |
if (mask & spv::ImageOperandsMakeTexelAvailableKHRMask) { |
| 4966 |
spv::IdImmediate imageOperand = { true, |
| 4967 |
builder.makeUintConstant(TranslateMemoryScope(TranslateCoherent(imageType))) }; |
| 4968 |
operands.push_back(imageOperand); |
| 4969 |
} |
| 4970 |
|
| 4971 |
builder.createNoResultOp(spv::OpImageWrite, operands); |
| 4972 |
if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
| 4973 |
builder.addCapability(spv::CapabilityStorageImageWriteWithoutFormat); |
| 4974 |
return spv::NoResult; |
| 4975 |
} else if (node->getOp() == glslang::EOpSparseImageLoad || |
| 4976 |
node->getOp() == glslang::EOpSparseImageLoadLod) { |
| 4977 |
builder.addCapability(spv::CapabilitySparseResidency); |
| 4978 |
if (builder.getImageTypeFormat(builder.getImageType(operands.front().word)) == spv::ImageFormatUnknown) |
| 4979 |
builder.addCapability(spv::CapabilityStorageImageReadWithoutFormat); |
| 4980 |
|
| 4981 |
spv::ImageOperandsMask mask = spv::ImageOperandsMaskNone; |
| 4982 |
if (sampler.isMultiSample()) { |
| 4983 |
mask = mask | spv::ImageOperandsSampleMask; |
| 4984 |
} |
| 4985 |
if (cracked.lod) { |
| 4986 |
builder.addExtension(spv::E_SPV_AMD_shader_image_load_store_lod); |
| 4987 |
builder.addCapability(spv::CapabilityImageReadWriteLodAMD); |
| 4988 |
|
| 4989 |
mask = mask | spv::ImageOperandsLodMask; |
| 4990 |
} |
| 4991 |
mask = mask | TranslateImageOperands(TranslateCoherent(imageType)); |
| 4992 |
mask = (spv::ImageOperandsMask)(mask & ~spv::ImageOperandsMakeTexelAvailableKHRMask); |
| 4993 |
mask = mask | signExtensionMask(); |
| 4994 |
if (mask != spv::ImageOperandsMaskNone) { |
| 4995 |
spv::IdImmediate imageOperands = { false, (unsigned int)mask }; |
| 4996 |
operands.push_back(imageOperands); |
| 4997 |
} |
| 4998 |
if (mask & spv::ImageOperandsSampleMask) { |
| 4999 |
spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5000 |
operands.push_back(imageOperand); |
| 5001 |
} |
| 5002 |
if (mask & spv::ImageOperandsLodMask) { |
| 5003 |
spv::IdImmediate imageOperand = { true, *opIt++ }; |
| 5004 |
operands.push_back(imageOperand); |
| 5005 |
} |
| 5006 |
if (mask & spv::ImageOperandsMakeTexelVisibleKHRMask) { |
| 5007 |
spv::IdImmediate imageOperand = { true, builder.makeUintConstant(TranslateMemoryScope( |
| 5008 |
TranslateCoherent(imageType))) }; |
| 5009 |
operands.push_back(imageOperand); |
| 5010 |
} |
| 5011 |
|
| 5012 |
// Create the return type that was a special structure |
| 5013 |
spv::Id texelOut = *opIt; |
| 5014 |
spv::Id typeId0 = resultType(); |
| 5015 |
spv::Id typeId1 = builder.getDerefTypeId(texelOut); |
| 5016 |
spv::Id resultTypeId = builder.makeStructResultType(typeId0, typeId1); |
| 5017 |
|
| 5018 |
spv::Id resultId = builder.createOp(spv::OpImageSparseRead, resultTypeId, operands); |
| 5019 |
|
| 5020 |
// Decode the return type |
| 5021 |
builder.createStore(builder.createCompositeExtract(resultId, typeId1, 1), texelOut); |
| 5022 |
return builder.createCompositeExtract(resultId, typeId0, 0); |
| 5023 |
} else { |
| 5024 |
// Process image atomic operations |
| 5025 |
|
| 5026 |
// GLSL "IMAGE_PARAMS" will involve in constructing an image texel pointer and this pointer, |
| 5027 |
// as the first source operand, is required by SPIR-V atomic operations. |
| 5028 |
// For non-MS, the sample value should be 0 |
| 5029 |
spv::IdImmediate sample = { true, sampler.isMultiSample() ? *(opIt++) : builder.makeUintConstant(0) }; |
| 5030 |
operands.push_back(sample); |
| 5031 |
|
| 5032 |
spv::Id resultTypeId; |
| 5033 |
// imageAtomicStore has a void return type so base the pointer type on |
| 5034 |
// the type of the value operand. |
| 5035 |
if (node->getOp() == glslang::EOpImageAtomicStore) { |
| 5036 |
resultTypeId = builder.makePointer(spv::StorageClassImage, builder.getTypeId(*opIt)); |
| 5037 |
} else { |
| 5038 |
resultTypeId = builder.makePointer(spv::StorageClassImage, resultType()); |
| 5039 |
} |
| 5040 |
spv::Id pointer = builder.createOp(spv::OpImageTexelPointer, resultTypeId, operands); |
| 5041 |
if (imageType.getQualifier().nonUniform) { |
| 5042 |
builder.addDecoration(pointer, spv::DecorationNonUniformEXT); |
| 5043 |
} |
| 5044 |
|
| 5045 |
std::vector<spv::Id> operands; |
| 5046 |
operands.push_back(pointer); |
| 5047 |
for (; opIt != arguments.end(); ++opIt) |
| 5048 |
operands.push_back(*opIt); |
| 5049 |
|
| 5050 |
return createAtomicOperation(node->getOp(), precision, resultType(), operands, node->getBasicType(), |
| 5051 |
lvalueCoherentFlags); |
| 5052 |
} |
| 5053 |
} |
| 5054 |
|
| 5055 |
#ifndef GLSLANG_WEB |
| 5056 |
// Check for fragment mask functions other than queries |
| 5057 |
if (cracked.fragMask) { |
| 5058 |
assert(sampler.ms); |
| 5059 |
|
| 5060 |
auto opIt = arguments.begin(); |
| 5061 |
std::vector<spv::Id> operands; |
| 5062 |
|
| 5063 |
operands.push_back(params.sampler); |
| 5064 |
++opIt; |
| 5065 |
|
| 5066 |
if (sampler.isSubpass()) { |
| 5067 |
// add on the (0,0) coordinate |
| 5068 |
spv::Id zero = builder.makeIntConstant(0); |
| 5069 |
std::vector<spv::Id> comps; |
| 5070 |
comps.push_back(zero); |
| 5071 |
comps.push_back(zero); |
| 5072 |
operands.push_back(builder.makeCompositeConstant( |
| 5073 |
builder.makeVectorType(builder.makeIntType(32), 2), comps)); |
| 5074 |
} |
| 5075 |
|
| 5076 |
for (; opIt != arguments.end(); ++opIt) |
| 5077 |
operands.push_back(*opIt); |
| 5078 |
|
| 5079 |
spv::Op fragMaskOp = spv::OpNop; |
| 5080 |
if (node->getOp() == glslang::EOpFragmentMaskFetch) |
| 5081 |
fragMaskOp = spv::OpFragmentMaskFetchAMD; |
| 5082 |
else if (node->getOp() == glslang::EOpFragmentFetch) |
| 5083 |
fragMaskOp = spv::OpFragmentFetchAMD; |
| 5084 |
|
| 5085 |
builder.addExtension(spv::E_SPV_AMD_shader_fragment_mask); |
| 5086 |
builder.addCapability(spv::CapabilityFragmentMaskAMD); |
| 5087 |
return builder.createOp(fragMaskOp, resultType(), operands); |
| 5088 |
} |
| 5089 |
#endif |
| 5090 |
|
| 5091 |
// Check for texture functions other than queries |
| 5092 |
bool sparse = node->isSparseTexture(); |
| 5093 |
bool imageFootprint = node->isImageFootprint(); |
| 5094 |
bool cubeCompare = sampler.dim == glslang::EsdCube && sampler.isArrayed() && sampler.isShadow(); |
| 5095 |
|
| 5096 |
// check for bias argument |
| 5097 |
bool bias = false; |
| 5098 |
if (! cracked.lod && ! cracked.grad && ! cracked.fetch && ! cubeCompare) { |
| 5099 |
int nonBiasArgCount = 2; |
| 5100 |
if (cracked.gather) |
| 5101 |
++nonBiasArgCount; // comp argument should be present when bias argument is present |
| 5102 |
|
| 5103 |
if (f16ShadowCompare) |
| 5104 |
++nonBiasArgCount; |
| 5105 |
if (cracked.offset) |
| 5106 |
++nonBiasArgCount; |
| 5107 |
else if (cracked.offsets) |
| 5108 |
++nonBiasArgCount; |
| 5109 |
if (cracked.grad) |
| 5110 |
nonBiasArgCount += 2; |
| 5111 |
if (cracked.lodClamp) |
| 5112 |
++nonBiasArgCount; |
| 5113 |
if (sparse) |
| 5114 |
++nonBiasArgCount; |
| 5115 |
if (imageFootprint) |
| 5116 |
//Following three extra arguments |
| 5117 |
// int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 5118 |
nonBiasArgCount += 3; |
| 5119 |
if ((int)arguments.size() > nonBiasArgCount) |
| 5120 |
bias = true; |
| 5121 |
} |
| 5122 |
|
| 5123 |
#ifndef GLSLANG_WEB |
| 5124 |
if (cracked.gather) { |
| 5125 |
const auto& sourceExtensions = glslangIntermediate->getRequestedExtensions(); |
| 5126 |
if (bias || cracked.lod || |
| 5127 |
sourceExtensions.find(glslang::E_GL_AMD_texture_gather_bias_lod) != sourceExtensions.end()) { |
| 5128 |
builder.addExtension(spv::E_SPV_AMD_texture_gather_bias_lod); |
| 5129 |
builder.addCapability(spv::CapabilityImageGatherBiasLodAMD); |
| 5130 |
} |
| 5131 |
} |
| 5132 |
#endif |
| 5133 |
|
| 5134 |
// set the rest of the arguments |
| 5135 |
|
| 5136 |
params.coords = arguments[1]; |
| 5137 |
int extraArgs = 0; |
| 5138 |
bool noImplicitLod = false; |
| 5139 |
|
| 5140 |
// sort out where Dref is coming from |
| 5141 |
if (cubeCompare || f16ShadowCompare) { |
| 5142 |
params.Dref = arguments[2]; |
| 5143 |
++extraArgs; |
| 5144 |
} else if (sampler.shadow && cracked.gather) { |
| 5145 |
params.Dref = arguments[2]; |
| 5146 |
++extraArgs; |
| 5147 |
} else if (sampler.shadow) { |
| 5148 |
std::vector<spv::Id> indexes; |
| 5149 |
int dRefComp; |
| 5150 |
if (cracked.proj) |
| 5151 |
dRefComp = 2; // "The resulting 3rd component of P in the shadow forms is used as Dref" |
| 5152 |
else |
| 5153 |
dRefComp = builder.getNumComponents(params.coords) - 1; |
| 5154 |
indexes.push_back(dRefComp); |
| 5155 |
params.Dref = builder.createCompositeExtract(params.coords, |
| 5156 |
builder.getScalarTypeId(builder.getTypeId(params.coords)), indexes); |
| 5157 |
} |
| 5158 |
|
| 5159 |
// lod |
| 5160 |
if (cracked.lod) { |
| 5161 |
params.lod = arguments[2 + extraArgs]; |
| 5162 |
++extraArgs; |
| 5163 |
} else if (glslangIntermediate->getStage() != EShLangFragment && |
| 5164 |
!(glslangIntermediate->getStage() == EShLangCompute && |
| 5165 |
glslangIntermediate->hasLayoutDerivativeModeNone())) { |
| 5166 |
// we need to invent the default lod for an explicit lod instruction for a non-fragment stage |
| 5167 |
noImplicitLod = true; |
| 5168 |
} |
| 5169 |
|
| 5170 |
// multisample |
| 5171 |
if (sampler.isMultiSample()) { |
| 5172 |
params.sample = arguments[2 + extraArgs]; // For MS, "sample" should be specified |
| 5173 |
++extraArgs; |
| 5174 |
} |
| 5175 |
|
| 5176 |
// gradient |
| 5177 |
if (cracked.grad) { |
| 5178 |
params.gradX = arguments[2 + extraArgs]; |
| 5179 |
params.gradY = arguments[3 + extraArgs]; |
| 5180 |
extraArgs += 2; |
| 5181 |
} |
| 5182 |
|
| 5183 |
// offset and offsets |
| 5184 |
if (cracked.offset) { |
| 5185 |
params.offset = arguments[2 + extraArgs]; |
| 5186 |
++extraArgs; |
| 5187 |
} else if (cracked.offsets) { |
| 5188 |
params.offsets = arguments[2 + extraArgs]; |
| 5189 |
++extraArgs; |
| 5190 |
} |
| 5191 |
|
| 5192 |
#ifndef GLSLANG_WEB |
| 5193 |
// lod clamp |
| 5194 |
if (cracked.lodClamp) { |
| 5195 |
params.lodClamp = arguments[2 + extraArgs]; |
| 5196 |
++extraArgs; |
| 5197 |
} |
| 5198 |
// sparse |
| 5199 |
if (sparse) { |
| 5200 |
params.texelOut = arguments[2 + extraArgs]; |
| 5201 |
++extraArgs; |
| 5202 |
} |
| 5203 |
// gather component |
| 5204 |
if (cracked.gather && ! sampler.shadow) { |
| 5205 |
// default component is 0, if missing, otherwise an argument |
| 5206 |
if (2 + extraArgs < (int)arguments.size()) { |
| 5207 |
params.component = arguments[2 + extraArgs]; |
| 5208 |
++extraArgs; |
| 5209 |
} else |
| 5210 |
params.component = builder.makeIntConstant(0); |
| 5211 |
} |
| 5212 |
spv::Id resultStruct = spv::NoResult; |
| 5213 |
if (imageFootprint) { |
| 5214 |
//Following three extra arguments |
| 5215 |
// int granularity, bool coarse, out gl_TextureFootprint2DNV footprint |
| 5216 |
params.granularity = arguments[2 + extraArgs]; |
| 5217 |
params.coarse = arguments[3 + extraArgs]; |
| 5218 |
resultStruct = arguments[4 + extraArgs]; |
| 5219 |
extraArgs += 3; |
| 5220 |
} |
| 5221 |
#endif |
| 5222 |
// bias |
| 5223 |
if (bias) { |
| 5224 |
params.bias = arguments[2 + extraArgs]; |
| 5225 |
++extraArgs; |
| 5226 |
} |
| 5227 |
|
| 5228 |
#ifndef GLSLANG_WEB |
| 5229 |
if (imageFootprint) { |
| 5230 |
builder.addExtension(spv::E_SPV_NV_shader_image_footprint); |
| 5231 |
builder.addCapability(spv::CapabilityImageFootprintNV); |
| 5232 |
|
| 5233 |
|
| 5234 |
//resultStructType(OpenGL type) contains 5 elements: |
| 5235 |
//struct gl_TextureFootprint2DNV { |
| 5236 |
// uvec2 anchor; |
| 5237 |
// uvec2 offset; |
| 5238 |
// uvec2 mask; |
| 5239 |
// uint lod; |
| 5240 |
// uint granularity; |
| 5241 |
//}; |
| 5242 |
//or |
| 5243 |
//struct gl_TextureFootprint3DNV { |
| 5244 |
// uvec3 anchor; |
| 5245 |
// uvec3 offset; |
| 5246 |
// uvec2 mask; |
| 5247 |
// uint lod; |
| 5248 |
// uint granularity; |
| 5249 |
//}; |
| 5250 |
spv::Id resultStructType = builder.getContainedTypeId(builder.getTypeId(resultStruct)); |
| 5251 |
assert(builder.isStructType(resultStructType)); |
| 5252 |
|
| 5253 |
//resType (SPIR-V type) contains 6 elements: |
| 5254 |
//Member 0 must be a Boolean type scalar(LOD), |
| 5255 |
//Member 1 must be a vector of integer type, whose Signedness operand is 0(anchor), |
| 5256 |
//Member 2 must be a vector of integer type, whose Signedness operand is 0(offset), |
| 5257 |
//Member 3 must be a vector of integer type, whose Signedness operand is 0(mask), |
| 5258 |
//Member 4 must be a scalar of integer type, whose Signedness operand is 0(lod), |
| 5259 |
//Member 5 must be a scalar of integer type, whose Signedness operand is 0(granularity). |
| 5260 |
std::vector<spv::Id> members; |
| 5261 |
members.push_back(resultType()); |
| 5262 |
for (int i = 0; i < 5; i++) { |
| 5263 |
members.push_back(builder.getContainedTypeId(resultStructType, i)); |
| 5264 |
} |
| 5265 |
spv::Id resType = builder.makeStructType(members, "ResType"); |
| 5266 |
|
| 5267 |
//call ImageFootprintNV |
| 5268 |
spv::Id res = builder.createTextureCall(precision, resType, sparse, cracked.fetch, cracked.proj, |
| 5269 |
cracked.gather, noImplicitLod, params, signExtensionMask()); |
| 5270 |
|
| 5271 |
//copy resType (SPIR-V type) to resultStructType(OpenGL type) |
| 5272 |
for (int i = 0; i < 5; i++) { |
| 5273 |
builder.clearAccessChain(); |
| 5274 |
builder.setAccessChainLValue(resultStruct); |
| 5275 |
|
| 5276 |
//Accessing to a struct we created, no coherent flag is set |
| 5277 |
spv::Builder::AccessChain::CoherentFlags flags; |
| 5278 |
flags.clear(); |
| 5279 |
|
| 5280 |
builder.accessChainPush(builder.makeIntConstant(i), flags, 0); |
| 5281 |
builder.accessChainStore(builder.createCompositeExtract(res, builder.getContainedTypeId(resType, i+1), |
| 5282 |
i+1), TranslateNonUniformDecoration(imageType.getQualifier())); |
| 5283 |
} |
| 5284 |
return builder.createCompositeExtract(res, resultType(), 0); |
| 5285 |
} |
| 5286 |
#endif |
| 5287 |
|
| 5288 |
// projective component (might not to move) |
| 5289 |
// GLSL: "The texture coordinates consumed from P, not including the last component of P, |
| 5290 |
// are divided by the last component of P." |
| 5291 |
// SPIR-V: "... (u [, v] [, w], q)... It may be a vector larger than needed, but all |
| 5292 |
// unused components will appear after all used components." |
| 5293 |
if (cracked.proj) { |
| 5294 |
int projSourceComp = builder.getNumComponents(params.coords) - 1; |
| 5295 |
int projTargetComp; |
| 5296 |
switch (sampler.dim) { |
| 5297 |
case glslang::Esd1D: projTargetComp = 1; break; |
| 5298 |
case glslang::Esd2D: projTargetComp = 2; break; |
| 5299 |
case glslang::EsdRect: projTargetComp = 2; break; |
| 5300 |
default: projTargetComp = projSourceComp; break; |
| 5301 |
} |
| 5302 |
// copy the projective coordinate if we have to |
| 5303 |
if (projTargetComp != projSourceComp) { |
| 5304 |
spv::Id projComp = builder.createCompositeExtract(params.coords, |
| 5305 |
builder.getScalarTypeId(builder.getTypeId(params.coords)), projSourceComp); |
| 5306 |
params.coords = builder.createCompositeInsert(projComp, params.coords, |
| 5307 |
builder.getTypeId(params.coords), projTargetComp); |
| 5308 |
} |
| 5309 |
} |
| 5310 |
|
| 5311 |
#ifndef GLSLANG_WEB |
| 5312 |
// nonprivate |
| 5313 |
if (imageType.getQualifier().nonprivate) { |
| 5314 |
params.nonprivate = true; |
| 5315 |
} |
| 5316 |
|
| 5317 |
// volatile |
| 5318 |
if (imageType.getQualifier().volatil) { |
| 5319 |
params.volatil = true; |
| 5320 |
} |
| 5321 |
#endif |
| 5322 |
|
| 5323 |
std::vector<spv::Id> result( 1, |
| 5324 |
builder.createTextureCall(precision, resultType(), sparse, cracked.fetch, cracked.proj, cracked.gather, |
| 5325 |
noImplicitLod, params, signExtensionMask()) |
| 5326 |
); |
| 5327 |
|
| 5328 |
if (components != node->getType().getVectorSize()) |
| 5329 |
result[0] = builder.createConstructor(precision, result, convertGlslangToSpvType(node->getType())); |
| 5330 |
|
| 5331 |
return result[0]; |
| 5332 |
} |
| 5333 |
|
| 5334 |
spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node) |
| 5335 |
{ |
| 5336 |
// Grab the function's pointer from the previously created function |
| 5337 |
spv::Function* function = functionMap[node->getName().c_str()]; |
| 5338 |
if (! function) |
| 5339 |
return 0; |
| 5340 |
|
| 5341 |
const glslang::TIntermSequence& glslangArgs = node->getSequence(); |
| 5342 |
const glslang::TQualifierList& qualifiers = node->getQualifierList(); |
| 5343 |
|
| 5344 |
// See comments in makeFunctions() for details about the semantics for parameter passing. |
| 5345 |
// |
| 5346 |
// These imply we need a four step process: |
| 5347 |
// 1. Evaluate the arguments |
| 5348 |
// 2. Allocate and make copies of in, out, and inout arguments |
| 5349 |
// 3. Make the call |
| 5350 |
// 4. Copy back the results |
| 5351 |
|
| 5352 |
// 1. Evaluate the arguments and their types |
| 5353 |
std::vector<spv::Builder::AccessChain> lValues; |
| 5354 |
std::vector<spv::Id> rValues; |
| 5355 |
std::vector<const glslang::TType*> argTypes; |
| 5356 |
for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 5357 |
argTypes.push_back(&glslangArgs[a]->getAsTyped()->getType()); |
| 5358 |
// build l-value |
| 5359 |
builder.clearAccessChain(); |
| 5360 |
glslangArgs[a]->traverse(this); |
| 5361 |
// keep outputs and pass-by-originals as l-values, evaluate others as r-values |
| 5362 |
if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0) || |
| 5363 |
writableParam(qualifiers[a])) { |
| 5364 |
// save l-value |
| 5365 |
lValues.push_back(builder.getAccessChain()); |
| 5366 |
} else { |
| 5367 |
// process r-value |
| 5368 |
rValues.push_back(accessChainLoad(*argTypes.back())); |
| 5369 |
} |
| 5370 |
} |
| 5371 |
|
| 5372 |
// 2. Allocate space for anything needing a copy, and if it's "in" or "inout" |
| 5373 |
// copy the original into that space. |
| 5374 |
// |
| 5375 |
// Also, build up the list of actual arguments to pass in for the call |
| 5376 |
int lValueCount = 0; |
| 5377 |
int rValueCount = 0; |
| 5378 |
std::vector<spv::Id> spvArgs; |
| 5379 |
for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 5380 |
spv::Id arg; |
| 5381 |
if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) { |
| 5382 |
builder.setAccessChain(lValues[lValueCount]); |
| 5383 |
arg = builder.accessChainGetLValue(); |
| 5384 |
++lValueCount; |
| 5385 |
} else if (writableParam(qualifiers[a])) { |
| 5386 |
// need space to hold the copy |
| 5387 |
arg = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, |
| 5388 |
builder.getContainedTypeId(function->getParamType(a)), "param"); |
| 5389 |
if (qualifiers[a] == glslang::EvqIn || qualifiers[a] == glslang::EvqInOut) { |
| 5390 |
// need to copy the input into output space |
| 5391 |
builder.setAccessChain(lValues[lValueCount]); |
| 5392 |
spv::Id copy = accessChainLoad(*argTypes[a]); |
| 5393 |
builder.clearAccessChain(); |
| 5394 |
builder.setAccessChainLValue(arg); |
| 5395 |
multiTypeStore(*argTypes[a], copy); |
| 5396 |
} |
| 5397 |
++lValueCount; |
| 5398 |
} else { |
| 5399 |
// process r-value, which involves a copy for a type mismatch |
| 5400 |
if (function->getParamType(a) != convertGlslangToSpvType(*argTypes[a]) || |
| 5401 |
TranslatePrecisionDecoration(*argTypes[a]) != function->getParamPrecision(a)) |
| 5402 |
{ |
| 5403 |
spv::Id argCopy = builder.createVariable(function->getParamPrecision(a), spv::StorageClassFunction, function->getParamType(a), "arg"); |
| 5404 |
builder.clearAccessChain(); |
| 5405 |
builder.setAccessChainLValue(argCopy); |
| 5406 |
multiTypeStore(*argTypes[a], rValues[rValueCount]); |
| 5407 |
arg = builder.createLoad(argCopy, function->getParamPrecision(a)); |
| 5408 |
} else |
| 5409 |
arg = rValues[rValueCount]; |
| 5410 |
++rValueCount; |
| 5411 |
} |
| 5412 |
spvArgs.push_back(arg); |
| 5413 |
} |
| 5414 |
|
| 5415 |
// 3. Make the call. |
| 5416 |
spv::Id result = builder.createFunctionCall(function, spvArgs); |
| 5417 |
builder.setPrecision(result, TranslatePrecisionDecoration(node->getType())); |
| 5418 |
|
| 5419 |
// 4. Copy back out an "out" arguments. |
| 5420 |
lValueCount = 0; |
| 5421 |
for (int a = 0; a < (int)glslangArgs.size(); ++a) { |
| 5422 |
if (originalParam(qualifiers[a], *argTypes[a], function->hasImplicitThis() && a == 0)) |
| 5423 |
++lValueCount; |
| 5424 |
else if (writableParam(qualifiers[a])) { |
| 5425 |
if (qualifiers[a] == glslang::EvqOut || qualifiers[a] == glslang::EvqInOut) { |
| 5426 |
spv::Id copy = builder.createLoad(spvArgs[a], spv::NoPrecision); |
| 5427 |
builder.setAccessChain(lValues[lValueCount]); |
| 5428 |
multiTypeStore(*argTypes[a], copy); |
| 5429 |
} |
| 5430 |
++lValueCount; |
| 5431 |
} |
| 5432 |
} |
| 5433 |
|
| 5434 |
return result; |
| 5435 |
} |
| 5436 |
|
| 5437 |
// Translate AST operation to SPV operation, already having SPV-based operands/types. |
| 5438 |
spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations, |
| 5439 |
spv::Id typeId, spv::Id left, spv::Id right, |
| 5440 |
glslang::TBasicType typeProxy, bool reduceComparison) |
| 5441 |
{ |
| 5442 |
bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5443 |
bool isFloat = isTypeFloat(typeProxy); |
| 5444 |
bool isBool = typeProxy == glslang::EbtBool; |
| 5445 |
|
| 5446 |
spv::Op binOp = spv::OpNop; |
| 5447 |
bool needMatchingVectors = true; // for non-matrix ops, would a scalar need to smear to match a vector? |
| 5448 |
bool comparison = false; |
| 5449 |
|
| 5450 |
switch (op) { |
| 5451 |
case glslang::EOpAdd: |
| 5452 |
case glslang::EOpAddAssign: |
| 5453 |
if (isFloat) |
| 5454 |
binOp = spv::OpFAdd; |
| 5455 |
else |
| 5456 |
binOp = spv::OpIAdd; |
| 5457 |
break; |
| 5458 |
case glslang::EOpSub: |
| 5459 |
case glslang::EOpSubAssign: |
| 5460 |
if (isFloat) |
| 5461 |
binOp = spv::OpFSub; |
| 5462 |
else |
| 5463 |
binOp = spv::OpISub; |
| 5464 |
break; |
| 5465 |
case glslang::EOpMul: |
| 5466 |
case glslang::EOpMulAssign: |
| 5467 |
if (isFloat) |
| 5468 |
binOp = spv::OpFMul; |
| 5469 |
else |
| 5470 |
binOp = spv::OpIMul; |
| 5471 |
break; |
| 5472 |
case glslang::EOpVectorTimesScalar: |
| 5473 |
case glslang::EOpVectorTimesScalarAssign: |
| 5474 |
if (isFloat && (builder.isVector(left) || builder.isVector(right))) { |
| 5475 |
if (builder.isVector(right)) |
| 5476 |
std::swap(left, right); |
| 5477 |
assert(builder.isScalar(right)); |
| 5478 |
needMatchingVectors = false; |
| 5479 |
binOp = spv::OpVectorTimesScalar; |
| 5480 |
} else if (isFloat) |
| 5481 |
binOp = spv::OpFMul; |
| 5482 |
else |
| 5483 |
binOp = spv::OpIMul; |
| 5484 |
break; |
| 5485 |
case glslang::EOpVectorTimesMatrix: |
| 5486 |
case glslang::EOpVectorTimesMatrixAssign: |
| 5487 |
binOp = spv::OpVectorTimesMatrix; |
| 5488 |
break; |
| 5489 |
case glslang::EOpMatrixTimesVector: |
| 5490 |
binOp = spv::OpMatrixTimesVector; |
| 5491 |
break; |
| 5492 |
case glslang::EOpMatrixTimesScalar: |
| 5493 |
case glslang::EOpMatrixTimesScalarAssign: |
| 5494 |
binOp = spv::OpMatrixTimesScalar; |
| 5495 |
break; |
| 5496 |
case glslang::EOpMatrixTimesMatrix: |
| 5497 |
case glslang::EOpMatrixTimesMatrixAssign: |
| 5498 |
binOp = spv::OpMatrixTimesMatrix; |
| 5499 |
break; |
| 5500 |
case glslang::EOpOuterProduct: |
| 5501 |
binOp = spv::OpOuterProduct; |
| 5502 |
needMatchingVectors = false; |
| 5503 |
break; |
| 5504 |
|
| 5505 |
case glslang::EOpDiv: |
| 5506 |
case glslang::EOpDivAssign: |
| 5507 |
if (isFloat) |
| 5508 |
binOp = spv::OpFDiv; |
| 5509 |
else if (isUnsigned) |
| 5510 |
binOp = spv::OpUDiv; |
| 5511 |
else |
| 5512 |
binOp = spv::OpSDiv; |
| 5513 |
break; |
| 5514 |
case glslang::EOpMod: |
| 5515 |
case glslang::EOpModAssign: |
| 5516 |
if (isFloat) |
| 5517 |
binOp = spv::OpFMod; |
| 5518 |
else if (isUnsigned) |
| 5519 |
binOp = spv::OpUMod; |
| 5520 |
else |
| 5521 |
binOp = spv::OpSMod; |
| 5522 |
break; |
| 5523 |
case glslang::EOpRightShift: |
| 5524 |
case glslang::EOpRightShiftAssign: |
| 5525 |
if (isUnsigned) |
| 5526 |
binOp = spv::OpShiftRightLogical; |
| 5527 |
else |
| 5528 |
binOp = spv::OpShiftRightArithmetic; |
| 5529 |
break; |
| 5530 |
case glslang::EOpLeftShift: |
| 5531 |
case glslang::EOpLeftShiftAssign: |
| 5532 |
binOp = spv::OpShiftLeftLogical; |
| 5533 |
break; |
| 5534 |
case glslang::EOpAnd: |
| 5535 |
case glslang::EOpAndAssign: |
| 5536 |
binOp = spv::OpBitwiseAnd; |
| 5537 |
break; |
| 5538 |
case glslang::EOpLogicalAnd: |
| 5539 |
needMatchingVectors = false; |
| 5540 |
binOp = spv::OpLogicalAnd; |
| 5541 |
break; |
| 5542 |
case glslang::EOpInclusiveOr: |
| 5543 |
case glslang::EOpInclusiveOrAssign: |
| 5544 |
binOp = spv::OpBitwiseOr; |
| 5545 |
break; |
| 5546 |
case glslang::EOpLogicalOr: |
| 5547 |
needMatchingVectors = false; |
| 5548 |
binOp = spv::OpLogicalOr; |
| 5549 |
break; |
| 5550 |
case glslang::EOpExclusiveOr: |
| 5551 |
case glslang::EOpExclusiveOrAssign: |
| 5552 |
binOp = spv::OpBitwiseXor; |
| 5553 |
break; |
| 5554 |
case glslang::EOpLogicalXor: |
| 5555 |
needMatchingVectors = false; |
| 5556 |
binOp = spv::OpLogicalNotEqual; |
| 5557 |
break; |
| 5558 |
|
| 5559 |
case glslang::EOpAbsDifference: |
| 5560 |
binOp = isUnsigned ? spv::OpAbsUSubINTEL : spv::OpAbsISubINTEL; |
| 5561 |
break; |
| 5562 |
|
| 5563 |
case glslang::EOpAddSaturate: |
| 5564 |
binOp = isUnsigned ? spv::OpUAddSatINTEL : spv::OpIAddSatINTEL; |
| 5565 |
break; |
| 5566 |
|
| 5567 |
case glslang::EOpSubSaturate: |
| 5568 |
binOp = isUnsigned ? spv::OpUSubSatINTEL : spv::OpISubSatINTEL; |
| 5569 |
break; |
| 5570 |
|
| 5571 |
case glslang::EOpAverage: |
| 5572 |
binOp = isUnsigned ? spv::OpUAverageINTEL : spv::OpIAverageINTEL; |
| 5573 |
break; |
| 5574 |
|
| 5575 |
case glslang::EOpAverageRounded: |
| 5576 |
binOp = isUnsigned ? spv::OpUAverageRoundedINTEL : spv::OpIAverageRoundedINTEL; |
| 5577 |
break; |
| 5578 |
|
| 5579 |
case glslang::EOpMul32x16: |
| 5580 |
binOp = isUnsigned ? spv::OpUMul32x16INTEL : spv::OpIMul32x16INTEL; |
| 5581 |
break; |
| 5582 |
|
| 5583 |
case glslang::EOpLessThan: |
| 5584 |
case glslang::EOpGreaterThan: |
| 5585 |
case glslang::EOpLessThanEqual: |
| 5586 |
case glslang::EOpGreaterThanEqual: |
| 5587 |
case glslang::EOpEqual: |
| 5588 |
case glslang::EOpNotEqual: |
| 5589 |
case glslang::EOpVectorEqual: |
| 5590 |
case glslang::EOpVectorNotEqual: |
| 5591 |
comparison = true; |
| 5592 |
break; |
| 5593 |
default: |
| 5594 |
break; |
| 5595 |
} |
| 5596 |
|
| 5597 |
// handle mapped binary operations (should be non-comparison) |
| 5598 |
if (binOp != spv::OpNop) { |
| 5599 |
assert(comparison == false); |
| 5600 |
if (builder.isMatrix(left) || builder.isMatrix(right) || |
| 5601 |
builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 5602 |
return createBinaryMatrixOperation(binOp, decorations, typeId, left, right); |
| 5603 |
|
| 5604 |
// No matrix involved; make both operands be the same number of components, if needed |
| 5605 |
if (needMatchingVectors) |
| 5606 |
builder.promoteScalar(decorations.precision, left, right); |
| 5607 |
|
| 5608 |
spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 5609 |
decorations.addNoContraction(builder, result); |
| 5610 |
decorations.addNonUniform(builder, result); |
| 5611 |
return builder.setPrecision(result, decorations.precision); |
| 5612 |
} |
| 5613 |
|
| 5614 |
if (! comparison) |
| 5615 |
return 0; |
| 5616 |
|
| 5617 |
// Handle comparison instructions |
| 5618 |
|
| 5619 |
if (reduceComparison && (op == glslang::EOpEqual || op == glslang::EOpNotEqual) |
| 5620 |
&& (builder.isVector(left) || builder.isMatrix(left) || builder.isAggregate(left))) { |
| 5621 |
spv::Id result = builder.createCompositeCompare(decorations.precision, left, right, op == glslang::EOpEqual); |
| 5622 |
decorations.addNonUniform(builder, result); |
| 5623 |
return result; |
| 5624 |
} |
| 5625 |
|
| 5626 |
switch (op) { |
| 5627 |
case glslang::EOpLessThan: |
| 5628 |
if (isFloat) |
| 5629 |
binOp = spv::OpFOrdLessThan; |
| 5630 |
else if (isUnsigned) |
| 5631 |
binOp = spv::OpULessThan; |
| 5632 |
else |
| 5633 |
binOp = spv::OpSLessThan; |
| 5634 |
break; |
| 5635 |
case glslang::EOpGreaterThan: |
| 5636 |
if (isFloat) |
| 5637 |
binOp = spv::OpFOrdGreaterThan; |
| 5638 |
else if (isUnsigned) |
| 5639 |
binOp = spv::OpUGreaterThan; |
| 5640 |
else |
| 5641 |
binOp = spv::OpSGreaterThan; |
| 5642 |
break; |
| 5643 |
case glslang::EOpLessThanEqual: |
| 5644 |
if (isFloat) |
| 5645 |
binOp = spv::OpFOrdLessThanEqual; |
| 5646 |
else if (isUnsigned) |
| 5647 |
binOp = spv::OpULessThanEqual; |
| 5648 |
else |
| 5649 |
binOp = spv::OpSLessThanEqual; |
| 5650 |
break; |
| 5651 |
case glslang::EOpGreaterThanEqual: |
| 5652 |
if (isFloat) |
| 5653 |
binOp = spv::OpFOrdGreaterThanEqual; |
| 5654 |
else if (isUnsigned) |
| 5655 |
binOp = spv::OpUGreaterThanEqual; |
| 5656 |
else |
| 5657 |
binOp = spv::OpSGreaterThanEqual; |
| 5658 |
break; |
| 5659 |
case glslang::EOpEqual: |
| 5660 |
case glslang::EOpVectorEqual: |
| 5661 |
if (isFloat) |
| 5662 |
binOp = spv::OpFOrdEqual; |
| 5663 |
else if (isBool) |
| 5664 |
binOp = spv::OpLogicalEqual; |
| 5665 |
else |
| 5666 |
binOp = spv::OpIEqual; |
| 5667 |
break; |
| 5668 |
case glslang::EOpNotEqual: |
| 5669 |
case glslang::EOpVectorNotEqual: |
| 5670 |
if (isFloat) |
| 5671 |
binOp = spv::OpFUnordNotEqual; |
| 5672 |
else if (isBool) |
| 5673 |
binOp = spv::OpLogicalNotEqual; |
| 5674 |
else |
| 5675 |
binOp = spv::OpINotEqual; |
| 5676 |
break; |
| 5677 |
default: |
| 5678 |
break; |
| 5679 |
} |
| 5680 |
|
| 5681 |
if (binOp != spv::OpNop) { |
| 5682 |
spv::Id result = builder.createBinOp(binOp, typeId, left, right); |
| 5683 |
decorations.addNoContraction(builder, result); |
| 5684 |
decorations.addNonUniform(builder, result); |
| 5685 |
return builder.setPrecision(result, decorations.precision); |
| 5686 |
} |
| 5687 |
|
| 5688 |
return 0; |
| 5689 |
} |
| 5690 |
|
| 5691 |
// |
| 5692 |
// Translate AST matrix operation to SPV operation, already having SPV-based operands/types. |
| 5693 |
// These can be any of: |
| 5694 |
// |
| 5695 |
// matrix * scalar |
| 5696 |
// scalar * matrix |
| 5697 |
// matrix * matrix linear algebraic |
| 5698 |
// matrix * vector |
| 5699 |
// vector * matrix |
| 5700 |
// matrix * matrix componentwise |
| 5701 |
// matrix op matrix op in {+, -, /} |
| 5702 |
// matrix op scalar op in {+, -, /} |
| 5703 |
// scalar op matrix op in {+, -, /} |
| 5704 |
// |
| 5705 |
spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 5706 |
spv::Id left, spv::Id right) |
| 5707 |
{ |
| 5708 |
bool firstClass = true; |
| 5709 |
|
| 5710 |
// First, handle first-class matrix operations (* and matrix/scalar) |
| 5711 |
switch (op) { |
| 5712 |
case spv::OpFDiv: |
| 5713 |
if (builder.isMatrix(left) && builder.isScalar(right)) { |
| 5714 |
// turn matrix / scalar into a multiply... |
| 5715 |
spv::Id resultType = builder.getTypeId(right); |
| 5716 |
right = builder.createBinOp(spv::OpFDiv, resultType, builder.makeFpConstant(resultType, 1.0), right); |
| 5717 |
op = spv::OpMatrixTimesScalar; |
| 5718 |
} else |
| 5719 |
firstClass = false; |
| 5720 |
break; |
| 5721 |
case spv::OpMatrixTimesScalar: |
| 5722 |
if (builder.isMatrix(right) || builder.isCooperativeMatrix(right)) |
| 5723 |
std::swap(left, right); |
| 5724 |
assert(builder.isScalar(right)); |
| 5725 |
break; |
| 5726 |
case spv::OpVectorTimesMatrix: |
| 5727 |
assert(builder.isVector(left)); |
| 5728 |
assert(builder.isMatrix(right)); |
| 5729 |
break; |
| 5730 |
case spv::OpMatrixTimesVector: |
| 5731 |
assert(builder.isMatrix(left)); |
| 5732 |
assert(builder.isVector(right)); |
| 5733 |
break; |
| 5734 |
case spv::OpMatrixTimesMatrix: |
| 5735 |
assert(builder.isMatrix(left)); |
| 5736 |
assert(builder.isMatrix(right)); |
| 5737 |
break; |
| 5738 |
default: |
| 5739 |
firstClass = false; |
| 5740 |
break; |
| 5741 |
} |
| 5742 |
|
| 5743 |
if (builder.isCooperativeMatrix(left) || builder.isCooperativeMatrix(right)) |
| 5744 |
firstClass = true; |
| 5745 |
|
| 5746 |
if (firstClass) { |
| 5747 |
spv::Id result = builder.createBinOp(op, typeId, left, right); |
| 5748 |
decorations.addNoContraction(builder, result); |
| 5749 |
decorations.addNonUniform(builder, result); |
| 5750 |
return builder.setPrecision(result, decorations.precision); |
| 5751 |
} |
| 5752 |
|
| 5753 |
// Handle component-wise +, -, *, %, and / for all combinations of type. |
| 5754 |
// The result type of all of them is the same type as the (a) matrix operand. |
| 5755 |
// The algorithm is to: |
| 5756 |
// - break the matrix(es) into vectors |
| 5757 |
// - smear any scalar to a vector |
| 5758 |
// - do vector operations |
| 5759 |
// - make a matrix out the vector results |
| 5760 |
switch (op) { |
| 5761 |
case spv::OpFAdd: |
| 5762 |
case spv::OpFSub: |
| 5763 |
case spv::OpFDiv: |
| 5764 |
case spv::OpFMod: |
| 5765 |
case spv::OpFMul: |
| 5766 |
{ |
| 5767 |
// one time set up... |
| 5768 |
bool leftMat = builder.isMatrix(left); |
| 5769 |
bool rightMat = builder.isMatrix(right); |
| 5770 |
unsigned int numCols = leftMat ? builder.getNumColumns(left) : builder.getNumColumns(right); |
| 5771 |
int numRows = leftMat ? builder.getNumRows(left) : builder.getNumRows(right); |
| 5772 |
spv::Id scalarType = builder.getScalarTypeId(typeId); |
| 5773 |
spv::Id vecType = builder.makeVectorType(scalarType, numRows); |
| 5774 |
std::vector<spv::Id> results; |
| 5775 |
spv::Id smearVec = spv::NoResult; |
| 5776 |
if (builder.isScalar(left)) |
| 5777 |
smearVec = builder.smearScalar(decorations.precision, left, vecType); |
| 5778 |
else if (builder.isScalar(right)) |
| 5779 |
smearVec = builder.smearScalar(decorations.precision, right, vecType); |
| 5780 |
|
| 5781 |
// do each vector op |
| 5782 |
for (unsigned int c = 0; c < numCols; ++c) { |
| 5783 |
std::vector<unsigned int> indexes; |
| 5784 |
indexes.push_back(c); |
| 5785 |
spv::Id leftVec = leftMat ? builder.createCompositeExtract( left, vecType, indexes) : smearVec; |
| 5786 |
spv::Id rightVec = rightMat ? builder.createCompositeExtract(right, vecType, indexes) : smearVec; |
| 5787 |
spv::Id result = builder.createBinOp(op, vecType, leftVec, rightVec); |
| 5788 |
decorations.addNoContraction(builder, result); |
| 5789 |
decorations.addNonUniform(builder, result); |
| 5790 |
results.push_back(builder.setPrecision(result, decorations.precision)); |
| 5791 |
} |
| 5792 |
|
| 5793 |
// put the pieces together |
| 5794 |
spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
| 5795 |
decorations.addNonUniform(builder, result); |
| 5796 |
return result; |
| 5797 |
} |
| 5798 |
default: |
| 5799 |
assert(0); |
| 5800 |
return spv::NoResult; |
| 5801 |
} |
| 5802 |
} |
| 5803 |
|
| 5804 |
spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId, |
| 5805 |
spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
| 5806 |
{ |
| 5807 |
spv::Op unaryOp = spv::OpNop; |
| 5808 |
int extBuiltins = -1; |
| 5809 |
int libCall = -1; |
| 5810 |
bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 5811 |
bool isFloat = isTypeFloat(typeProxy); |
| 5812 |
|
| 5813 |
switch (op) { |
| 5814 |
case glslang::EOpNegative: |
| 5815 |
if (isFloat) { |
| 5816 |
unaryOp = spv::OpFNegate; |
| 5817 |
if (builder.isMatrixType(typeId)) |
| 5818 |
return createUnaryMatrixOperation(unaryOp, decorations, typeId, operand, typeProxy); |
| 5819 |
} else |
| 5820 |
unaryOp = spv::OpSNegate; |
| 5821 |
break; |
| 5822 |
|
| 5823 |
case glslang::EOpLogicalNot: |
| 5824 |
case glslang::EOpVectorLogicalNot: |
| 5825 |
unaryOp = spv::OpLogicalNot; |
| 5826 |
break; |
| 5827 |
case glslang::EOpBitwiseNot: |
| 5828 |
unaryOp = spv::OpNot; |
| 5829 |
break; |
| 5830 |
|
| 5831 |
case glslang::EOpDeterminant: |
| 5832 |
libCall = spv::GLSLstd450Determinant; |
| 5833 |
break; |
| 5834 |
case glslang::EOpMatrixInverse: |
| 5835 |
libCall = spv::GLSLstd450MatrixInverse; |
| 5836 |
break; |
| 5837 |
case glslang::EOpTranspose: |
| 5838 |
unaryOp = spv::OpTranspose; |
| 5839 |
break; |
| 5840 |
|
| 5841 |
case glslang::EOpRadians: |
| 5842 |
libCall = spv::GLSLstd450Radians; |
| 5843 |
break; |
| 5844 |
case glslang::EOpDegrees: |
| 5845 |
libCall = spv::GLSLstd450Degrees; |
| 5846 |
break; |
| 5847 |
case glslang::EOpSin: |
| 5848 |
libCall = spv::GLSLstd450Sin; |
| 5849 |
break; |
| 5850 |
case glslang::EOpCos: |
| 5851 |
libCall = spv::GLSLstd450Cos; |
| 5852 |
break; |
| 5853 |
case glslang::EOpTan: |
| 5854 |
libCall = spv::GLSLstd450Tan; |
| 5855 |
break; |
| 5856 |
case glslang::EOpAcos: |
| 5857 |
libCall = spv::GLSLstd450Acos; |
| 5858 |
break; |
| 5859 |
case glslang::EOpAsin: |
| 5860 |
libCall = spv::GLSLstd450Asin; |
| 5861 |
break; |
| 5862 |
case glslang::EOpAtan: |
| 5863 |
libCall = spv::GLSLstd450Atan; |
| 5864 |
break; |
| 5865 |
|
| 5866 |
case glslang::EOpAcosh: |
| 5867 |
libCall = spv::GLSLstd450Acosh; |
| 5868 |
break; |
| 5869 |
case glslang::EOpAsinh: |
| 5870 |
libCall = spv::GLSLstd450Asinh; |
| 5871 |
break; |
| 5872 |
case glslang::EOpAtanh: |
| 5873 |
libCall = spv::GLSLstd450Atanh; |
| 5874 |
break; |
| 5875 |
case glslang::EOpTanh: |
| 5876 |
libCall = spv::GLSLstd450Tanh; |
| 5877 |
break; |
| 5878 |
case glslang::EOpCosh: |
| 5879 |
libCall = spv::GLSLstd450Cosh; |
| 5880 |
break; |
| 5881 |
case glslang::EOpSinh: |
| 5882 |
libCall = spv::GLSLstd450Sinh; |
| 5883 |
break; |
| 5884 |
|
| 5885 |
case glslang::EOpLength: |
| 5886 |
libCall = spv::GLSLstd450Length; |
| 5887 |
break; |
| 5888 |
case glslang::EOpNormalize: |
| 5889 |
libCall = spv::GLSLstd450Normalize; |
| 5890 |
break; |
| 5891 |
|
| 5892 |
case glslang::EOpExp: |
| 5893 |
libCall = spv::GLSLstd450Exp; |
| 5894 |
break; |
| 5895 |
case glslang::EOpLog: |
| 5896 |
libCall = spv::GLSLstd450Log; |
| 5897 |
break; |
| 5898 |
case glslang::EOpExp2: |
| 5899 |
libCall = spv::GLSLstd450Exp2; |
| 5900 |
break; |
| 5901 |
case glslang::EOpLog2: |
| 5902 |
libCall = spv::GLSLstd450Log2; |
| 5903 |
break; |
| 5904 |
case glslang::EOpSqrt: |
| 5905 |
libCall = spv::GLSLstd450Sqrt; |
| 5906 |
break; |
| 5907 |
case glslang::EOpInverseSqrt: |
| 5908 |
libCall = spv::GLSLstd450InverseSqrt; |
| 5909 |
break; |
| 5910 |
|
| 5911 |
case glslang::EOpFloor: |
| 5912 |
libCall = spv::GLSLstd450Floor; |
| 5913 |
break; |
| 5914 |
case glslang::EOpTrunc: |
| 5915 |
libCall = spv::GLSLstd450Trunc; |
| 5916 |
break; |
| 5917 |
case glslang::EOpRound: |
| 5918 |
libCall = spv::GLSLstd450Round; |
| 5919 |
break; |
| 5920 |
case glslang::EOpRoundEven: |
| 5921 |
libCall = spv::GLSLstd450RoundEven; |
| 5922 |
break; |
| 5923 |
case glslang::EOpCeil: |
| 5924 |
libCall = spv::GLSLstd450Ceil; |
| 5925 |
break; |
| 5926 |
case glslang::EOpFract: |
| 5927 |
libCall = spv::GLSLstd450Fract; |
| 5928 |
break; |
| 5929 |
|
| 5930 |
case glslang::EOpIsNan: |
| 5931 |
unaryOp = spv::OpIsNan; |
| 5932 |
break; |
| 5933 |
case glslang::EOpIsInf: |
| 5934 |
unaryOp = spv::OpIsInf; |
| 5935 |
break; |
| 5936 |
case glslang::EOpIsFinite: |
| 5937 |
unaryOp = spv::OpIsFinite; |
| 5938 |
break; |
| 5939 |
|
| 5940 |
case glslang::EOpFloatBitsToInt: |
| 5941 |
case glslang::EOpFloatBitsToUint: |
| 5942 |
case glslang::EOpIntBitsToFloat: |
| 5943 |
case glslang::EOpUintBitsToFloat: |
| 5944 |
case glslang::EOpDoubleBitsToInt64: |
| 5945 |
case glslang::EOpDoubleBitsToUint64: |
| 5946 |
case glslang::EOpInt64BitsToDouble: |
| 5947 |
case glslang::EOpUint64BitsToDouble: |
| 5948 |
case glslang::EOpFloat16BitsToInt16: |
| 5949 |
case glslang::EOpFloat16BitsToUint16: |
| 5950 |
case glslang::EOpInt16BitsToFloat16: |
| 5951 |
case glslang::EOpUint16BitsToFloat16: |
| 5952 |
unaryOp = spv::OpBitcast; |
| 5953 |
break; |
| 5954 |
|
| 5955 |
case glslang::EOpPackSnorm2x16: |
| 5956 |
libCall = spv::GLSLstd450PackSnorm2x16; |
| 5957 |
break; |
| 5958 |
case glslang::EOpUnpackSnorm2x16: |
| 5959 |
libCall = spv::GLSLstd450UnpackSnorm2x16; |
| 5960 |
break; |
| 5961 |
case glslang::EOpPackUnorm2x16: |
| 5962 |
libCall = spv::GLSLstd450PackUnorm2x16; |
| 5963 |
break; |
| 5964 |
case glslang::EOpUnpackUnorm2x16: |
| 5965 |
libCall = spv::GLSLstd450UnpackUnorm2x16; |
| 5966 |
break; |
| 5967 |
case glslang::EOpPackHalf2x16: |
| 5968 |
libCall = spv::GLSLstd450PackHalf2x16; |
| 5969 |
break; |
| 5970 |
case glslang::EOpUnpackHalf2x16: |
| 5971 |
libCall = spv::GLSLstd450UnpackHalf2x16; |
| 5972 |
break; |
| 5973 |
#ifndef GLSLANG_WEB |
| 5974 |
case glslang::EOpPackSnorm4x8: |
| 5975 |
libCall = spv::GLSLstd450PackSnorm4x8; |
| 5976 |
break; |
| 5977 |
case glslang::EOpUnpackSnorm4x8: |
| 5978 |
libCall = spv::GLSLstd450UnpackSnorm4x8; |
| 5979 |
break; |
| 5980 |
case glslang::EOpPackUnorm4x8: |
| 5981 |
libCall = spv::GLSLstd450PackUnorm4x8; |
| 5982 |
break; |
| 5983 |
case glslang::EOpUnpackUnorm4x8: |
| 5984 |
libCall = spv::GLSLstd450UnpackUnorm4x8; |
| 5985 |
break; |
| 5986 |
case glslang::EOpPackDouble2x32: |
| 5987 |
libCall = spv::GLSLstd450PackDouble2x32; |
| 5988 |
break; |
| 5989 |
case glslang::EOpUnpackDouble2x32: |
| 5990 |
libCall = spv::GLSLstd450UnpackDouble2x32; |
| 5991 |
break; |
| 5992 |
#endif |
| 5993 |
|
| 5994 |
case glslang::EOpPackInt2x32: |
| 5995 |
case glslang::EOpUnpackInt2x32: |
| 5996 |
case glslang::EOpPackUint2x32: |
| 5997 |
case glslang::EOpUnpackUint2x32: |
| 5998 |
case glslang::EOpPack16: |
| 5999 |
case glslang::EOpPack32: |
| 6000 |
case glslang::EOpPack64: |
| 6001 |
case glslang::EOpUnpack32: |
| 6002 |
case glslang::EOpUnpack16: |
| 6003 |
case glslang::EOpUnpack8: |
| 6004 |
case glslang::EOpPackInt2x16: |
| 6005 |
case glslang::EOpUnpackInt2x16: |
| 6006 |
case glslang::EOpPackUint2x16: |
| 6007 |
case glslang::EOpUnpackUint2x16: |
| 6008 |
case glslang::EOpPackInt4x16: |
| 6009 |
case glslang::EOpUnpackInt4x16: |
| 6010 |
case glslang::EOpPackUint4x16: |
| 6011 |
case glslang::EOpUnpackUint4x16: |
| 6012 |
case glslang::EOpPackFloat2x16: |
| 6013 |
case glslang::EOpUnpackFloat2x16: |
| 6014 |
unaryOp = spv::OpBitcast; |
| 6015 |
break; |
| 6016 |
|
| 6017 |
case glslang::EOpDPdx: |
| 6018 |
unaryOp = spv::OpDPdx; |
| 6019 |
break; |
| 6020 |
case glslang::EOpDPdy: |
| 6021 |
unaryOp = spv::OpDPdy; |
| 6022 |
break; |
| 6023 |
case glslang::EOpFwidth: |
| 6024 |
unaryOp = spv::OpFwidth; |
| 6025 |
break; |
| 6026 |
|
| 6027 |
case glslang::EOpAny: |
| 6028 |
unaryOp = spv::OpAny; |
| 6029 |
break; |
| 6030 |
case glslang::EOpAll: |
| 6031 |
unaryOp = spv::OpAll; |
| 6032 |
break; |
| 6033 |
|
| 6034 |
case glslang::EOpAbs: |
| 6035 |
if (isFloat) |
| 6036 |
libCall = spv::GLSLstd450FAbs; |
| 6037 |
else |
| 6038 |
libCall = spv::GLSLstd450SAbs; |
| 6039 |
break; |
| 6040 |
case glslang::EOpSign: |
| 6041 |
if (isFloat) |
| 6042 |
libCall = spv::GLSLstd450FSign; |
| 6043 |
else |
| 6044 |
libCall = spv::GLSLstd450SSign; |
| 6045 |
break; |
| 6046 |
|
| 6047 |
#ifndef GLSLANG_WEB |
| 6048 |
case glslang::EOpDPdxFine: |
| 6049 |
unaryOp = spv::OpDPdxFine; |
| 6050 |
break; |
| 6051 |
case glslang::EOpDPdyFine: |
| 6052 |
unaryOp = spv::OpDPdyFine; |
| 6053 |
break; |
| 6054 |
case glslang::EOpFwidthFine: |
| 6055 |
unaryOp = spv::OpFwidthFine; |
| 6056 |
break; |
| 6057 |
case glslang::EOpDPdxCoarse: |
| 6058 |
unaryOp = spv::OpDPdxCoarse; |
| 6059 |
break; |
| 6060 |
case glslang::EOpDPdyCoarse: |
| 6061 |
unaryOp = spv::OpDPdyCoarse; |
| 6062 |
break; |
| 6063 |
case glslang::EOpFwidthCoarse: |
| 6064 |
unaryOp = spv::OpFwidthCoarse; |
| 6065 |
break; |
| 6066 |
case glslang::EOpRayQueryProceed: |
| 6067 |
unaryOp = spv::OpRayQueryProceedKHR; |
| 6068 |
break; |
| 6069 |
case glslang::EOpRayQueryGetRayTMin: |
| 6070 |
unaryOp = spv::OpRayQueryGetRayTMinKHR; |
| 6071 |
break; |
| 6072 |
case glslang::EOpRayQueryGetRayFlags: |
| 6073 |
unaryOp = spv::OpRayQueryGetRayFlagsKHR; |
| 6074 |
break; |
| 6075 |
case glslang::EOpRayQueryGetWorldRayOrigin: |
| 6076 |
unaryOp = spv::OpRayQueryGetWorldRayOriginKHR; |
| 6077 |
break; |
| 6078 |
case glslang::EOpRayQueryGetWorldRayDirection: |
| 6079 |
unaryOp = spv::OpRayQueryGetWorldRayDirectionKHR; |
| 6080 |
break; |
| 6081 |
case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 6082 |
unaryOp = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
| 6083 |
break; |
| 6084 |
case glslang::EOpInterpolateAtCentroid: |
| 6085 |
if (typeProxy == glslang::EbtFloat16) |
| 6086 |
builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 6087 |
libCall = spv::GLSLstd450InterpolateAtCentroid; |
| 6088 |
break; |
| 6089 |
case glslang::EOpAtomicCounterIncrement: |
| 6090 |
case glslang::EOpAtomicCounterDecrement: |
| 6091 |
case glslang::EOpAtomicCounter: |
| 6092 |
{ |
| 6093 |
// Handle all of the atomics in one place, in createAtomicOperation() |
| 6094 |
std::vector<spv::Id> operands; |
| 6095 |
operands.push_back(operand); |
| 6096 |
return createAtomicOperation(op, decorations.precision, typeId, operands, typeProxy, lvalueCoherentFlags); |
| 6097 |
} |
| 6098 |
|
| 6099 |
case glslang::EOpBitFieldReverse: |
| 6100 |
unaryOp = spv::OpBitReverse; |
| 6101 |
break; |
| 6102 |
case glslang::EOpBitCount: |
| 6103 |
unaryOp = spv::OpBitCount; |
| 6104 |
break; |
| 6105 |
case glslang::EOpFindLSB: |
| 6106 |
libCall = spv::GLSLstd450FindILsb; |
| 6107 |
break; |
| 6108 |
case glslang::EOpFindMSB: |
| 6109 |
if (isUnsigned) |
| 6110 |
libCall = spv::GLSLstd450FindUMsb; |
| 6111 |
else |
| 6112 |
libCall = spv::GLSLstd450FindSMsb; |
| 6113 |
break; |
| 6114 |
|
| 6115 |
case glslang::EOpCountLeadingZeros: |
| 6116 |
builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 6117 |
builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 6118 |
unaryOp = spv::OpUCountLeadingZerosINTEL; |
| 6119 |
break; |
| 6120 |
|
| 6121 |
case glslang::EOpCountTrailingZeros: |
| 6122 |
builder.addCapability(spv::CapabilityIntegerFunctions2INTEL); |
| 6123 |
builder.addExtension("SPV_INTEL_shader_integer_functions2"); |
| 6124 |
unaryOp = spv::OpUCountTrailingZerosINTEL; |
| 6125 |
break; |
| 6126 |
|
| 6127 |
case glslang::EOpBallot: |
| 6128 |
case glslang::EOpReadFirstInvocation: |
| 6129 |
case glslang::EOpAnyInvocation: |
| 6130 |
case glslang::EOpAllInvocations: |
| 6131 |
case glslang::EOpAllInvocationsEqual: |
| 6132 |
case glslang::EOpMinInvocations: |
| 6133 |
case glslang::EOpMaxInvocations: |
| 6134 |
case glslang::EOpAddInvocations: |
| 6135 |
case glslang::EOpMinInvocationsNonUniform: |
| 6136 |
case glslang::EOpMaxInvocationsNonUniform: |
| 6137 |
case glslang::EOpAddInvocationsNonUniform: |
| 6138 |
case glslang::EOpMinInvocationsInclusiveScan: |
| 6139 |
case glslang::EOpMaxInvocationsInclusiveScan: |
| 6140 |
case glslang::EOpAddInvocationsInclusiveScan: |
| 6141 |
case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6142 |
case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6143 |
case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6144 |
case glslang::EOpMinInvocationsExclusiveScan: |
| 6145 |
case glslang::EOpMaxInvocationsExclusiveScan: |
| 6146 |
case glslang::EOpAddInvocationsExclusiveScan: |
| 6147 |
case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6148 |
case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6149 |
case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6150 |
{ |
| 6151 |
std::vector<spv::Id> operands; |
| 6152 |
operands.push_back(operand); |
| 6153 |
return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 6154 |
} |
| 6155 |
case glslang::EOpSubgroupAll: |
| 6156 |
case glslang::EOpSubgroupAny: |
| 6157 |
case glslang::EOpSubgroupAllEqual: |
| 6158 |
case glslang::EOpSubgroupBroadcastFirst: |
| 6159 |
case glslang::EOpSubgroupBallot: |
| 6160 |
case glslang::EOpSubgroupInverseBallot: |
| 6161 |
case glslang::EOpSubgroupBallotBitCount: |
| 6162 |
case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 6163 |
case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 6164 |
case glslang::EOpSubgroupBallotFindLSB: |
| 6165 |
case glslang::EOpSubgroupBallotFindMSB: |
| 6166 |
case glslang::EOpSubgroupAdd: |
| 6167 |
case glslang::EOpSubgroupMul: |
| 6168 |
case glslang::EOpSubgroupMin: |
| 6169 |
case glslang::EOpSubgroupMax: |
| 6170 |
case glslang::EOpSubgroupAnd: |
| 6171 |
case glslang::EOpSubgroupOr: |
| 6172 |
case glslang::EOpSubgroupXor: |
| 6173 |
case glslang::EOpSubgroupInclusiveAdd: |
| 6174 |
case glslang::EOpSubgroupInclusiveMul: |
| 6175 |
case glslang::EOpSubgroupInclusiveMin: |
| 6176 |
case glslang::EOpSubgroupInclusiveMax: |
| 6177 |
case glslang::EOpSubgroupInclusiveAnd: |
| 6178 |
case glslang::EOpSubgroupInclusiveOr: |
| 6179 |
case glslang::EOpSubgroupInclusiveXor: |
| 6180 |
case glslang::EOpSubgroupExclusiveAdd: |
| 6181 |
case glslang::EOpSubgroupExclusiveMul: |
| 6182 |
case glslang::EOpSubgroupExclusiveMin: |
| 6183 |
case glslang::EOpSubgroupExclusiveMax: |
| 6184 |
case glslang::EOpSubgroupExclusiveAnd: |
| 6185 |
case glslang::EOpSubgroupExclusiveOr: |
| 6186 |
case glslang::EOpSubgroupExclusiveXor: |
| 6187 |
case glslang::EOpSubgroupQuadSwapHorizontal: |
| 6188 |
case glslang::EOpSubgroupQuadSwapVertical: |
| 6189 |
case glslang::EOpSubgroupQuadSwapDiagonal: { |
| 6190 |
std::vector<spv::Id> operands; |
| 6191 |
operands.push_back(operand); |
| 6192 |
return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 6193 |
} |
| 6194 |
case glslang::EOpMbcnt: |
| 6195 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 6196 |
libCall = spv::MbcntAMD; |
| 6197 |
break; |
| 6198 |
|
| 6199 |
case glslang::EOpCubeFaceIndex: |
| 6200 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 6201 |
libCall = spv::CubeFaceIndexAMD; |
| 6202 |
break; |
| 6203 |
|
| 6204 |
case glslang::EOpCubeFaceCoord: |
| 6205 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_gcn_shader); |
| 6206 |
libCall = spv::CubeFaceCoordAMD; |
| 6207 |
break; |
| 6208 |
case glslang::EOpSubgroupPartition: |
| 6209 |
unaryOp = spv::OpGroupNonUniformPartitionNV; |
| 6210 |
break; |
| 6211 |
case glslang::EOpConstructReference: |
| 6212 |
unaryOp = spv::OpBitcast; |
| 6213 |
break; |
| 6214 |
#endif |
| 6215 |
|
| 6216 |
case glslang::EOpCopyObject: |
| 6217 |
unaryOp = spv::OpCopyObject; |
| 6218 |
break; |
| 6219 |
|
| 6220 |
default: |
| 6221 |
return 0; |
| 6222 |
} |
| 6223 |
|
| 6224 |
spv::Id id; |
| 6225 |
if (libCall >= 0) { |
| 6226 |
std::vector<spv::Id> args; |
| 6227 |
args.push_back(operand); |
| 6228 |
id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, args); |
| 6229 |
} else { |
| 6230 |
id = builder.createUnaryOp(unaryOp, typeId, operand); |
| 6231 |
} |
| 6232 |
|
| 6233 |
decorations.addNoContraction(builder, id); |
| 6234 |
decorations.addNonUniform(builder, id); |
| 6235 |
return builder.setPrecision(id, decorations.precision); |
| 6236 |
} |
| 6237 |
|
| 6238 |
// Create a unary operation on a matrix |
| 6239 |
spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId, |
| 6240 |
spv::Id operand, glslang::TBasicType /* typeProxy */) |
| 6241 |
{ |
| 6242 |
// Handle unary operations vector by vector. |
| 6243 |
// The result type is the same type as the original type. |
| 6244 |
// The algorithm is to: |
| 6245 |
// - break the matrix into vectors |
| 6246 |
// - apply the operation to each vector |
| 6247 |
// - make a matrix out the vector results |
| 6248 |
|
| 6249 |
// get the types sorted out |
| 6250 |
int numCols = builder.getNumColumns(operand); |
| 6251 |
int numRows = builder.getNumRows(operand); |
| 6252 |
spv::Id srcVecType = builder.makeVectorType(builder.getScalarTypeId(builder.getTypeId(operand)), numRows); |
| 6253 |
spv::Id destVecType = builder.makeVectorType(builder.getScalarTypeId(typeId), numRows); |
| 6254 |
std::vector<spv::Id> results; |
| 6255 |
|
| 6256 |
// do each vector op |
| 6257 |
for (int c = 0; c < numCols; ++c) { |
| 6258 |
std::vector<unsigned int> indexes; |
| 6259 |
indexes.push_back(c); |
| 6260 |
spv::Id srcVec = builder.createCompositeExtract(operand, srcVecType, indexes); |
| 6261 |
spv::Id destVec = builder.createUnaryOp(op, destVecType, srcVec); |
| 6262 |
decorations.addNoContraction(builder, destVec); |
| 6263 |
decorations.addNonUniform(builder, destVec); |
| 6264 |
results.push_back(builder.setPrecision(destVec, decorations.precision)); |
| 6265 |
} |
| 6266 |
|
| 6267 |
// put the pieces together |
| 6268 |
spv::Id result = builder.setPrecision(builder.createCompositeConstruct(typeId, results), decorations.precision); |
| 6269 |
decorations.addNonUniform(builder, result); |
| 6270 |
return result; |
| 6271 |
} |
| 6272 |
|
| 6273 |
// For converting integers where both the bitwidth and the signedness could |
| 6274 |
// change, but only do the width change here. The caller is still responsible |
| 6275 |
// for the signedness conversion. |
| 6276 |
spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize) |
| 6277 |
{ |
| 6278 |
// Get the result type width, based on the type to convert to. |
| 6279 |
int width = 32; |
| 6280 |
switch(op) { |
| 6281 |
case glslang::EOpConvInt16ToUint8: |
| 6282 |
case glslang::EOpConvIntToUint8: |
| 6283 |
case glslang::EOpConvInt64ToUint8: |
| 6284 |
case glslang::EOpConvUint16ToInt8: |
| 6285 |
case glslang::EOpConvUintToInt8: |
| 6286 |
case glslang::EOpConvUint64ToInt8: |
| 6287 |
width = 8; |
| 6288 |
break; |
| 6289 |
case glslang::EOpConvInt8ToUint16: |
| 6290 |
case glslang::EOpConvIntToUint16: |
| 6291 |
case glslang::EOpConvInt64ToUint16: |
| 6292 |
case glslang::EOpConvUint8ToInt16: |
| 6293 |
case glslang::EOpConvUintToInt16: |
| 6294 |
case glslang::EOpConvUint64ToInt16: |
| 6295 |
width = 16; |
| 6296 |
break; |
| 6297 |
case glslang::EOpConvInt8ToUint: |
| 6298 |
case glslang::EOpConvInt16ToUint: |
| 6299 |
case glslang::EOpConvInt64ToUint: |
| 6300 |
case glslang::EOpConvUint8ToInt: |
| 6301 |
case glslang::EOpConvUint16ToInt: |
| 6302 |
case glslang::EOpConvUint64ToInt: |
| 6303 |
width = 32; |
| 6304 |
break; |
| 6305 |
case glslang::EOpConvInt8ToUint64: |
| 6306 |
case glslang::EOpConvInt16ToUint64: |
| 6307 |
case glslang::EOpConvIntToUint64: |
| 6308 |
case glslang::EOpConvUint8ToInt64: |
| 6309 |
case glslang::EOpConvUint16ToInt64: |
| 6310 |
case glslang::EOpConvUintToInt64: |
| 6311 |
width = 64; |
| 6312 |
break; |
| 6313 |
|
| 6314 |
default: |
| 6315 |
assert(false && "Default missing"); |
| 6316 |
break; |
| 6317 |
} |
| 6318 |
|
| 6319 |
// Get the conversion operation and result type, |
| 6320 |
// based on the target width, but the source type. |
| 6321 |
spv::Id type = spv::NoType; |
| 6322 |
spv::Op convOp = spv::OpNop; |
| 6323 |
switch(op) { |
| 6324 |
case glslang::EOpConvInt8ToUint16: |
| 6325 |
case glslang::EOpConvInt8ToUint: |
| 6326 |
case glslang::EOpConvInt8ToUint64: |
| 6327 |
case glslang::EOpConvInt16ToUint8: |
| 6328 |
case glslang::EOpConvInt16ToUint: |
| 6329 |
case glslang::EOpConvInt16ToUint64: |
| 6330 |
case glslang::EOpConvIntToUint8: |
| 6331 |
case glslang::EOpConvIntToUint16: |
| 6332 |
case glslang::EOpConvIntToUint64: |
| 6333 |
case glslang::EOpConvInt64ToUint8: |
| 6334 |
case glslang::EOpConvInt64ToUint16: |
| 6335 |
case glslang::EOpConvInt64ToUint: |
| 6336 |
convOp = spv::OpSConvert; |
| 6337 |
type = builder.makeIntType(width); |
| 6338 |
break; |
| 6339 |
default: |
| 6340 |
convOp = spv::OpUConvert; |
| 6341 |
type = builder.makeUintType(width); |
| 6342 |
break; |
| 6343 |
} |
| 6344 |
|
| 6345 |
if (vectorSize > 0) |
| 6346 |
type = builder.makeVectorType(type, vectorSize); |
| 6347 |
|
| 6348 |
return builder.createUnaryOp(convOp, type, operand); |
| 6349 |
} |
| 6350 |
|
| 6351 |
spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType, |
| 6352 |
spv::Id operand, glslang::TBasicType typeProxy) |
| 6353 |
{ |
| 6354 |
spv::Op convOp = spv::OpNop; |
| 6355 |
spv::Id zero = 0; |
| 6356 |
spv::Id one = 0; |
| 6357 |
|
| 6358 |
int vectorSize = builder.isVectorType(destType) ? builder.getNumTypeComponents(destType) : 0; |
| 6359 |
|
| 6360 |
switch (op) { |
| 6361 |
case glslang::EOpConvIntToBool: |
| 6362 |
case glslang::EOpConvUintToBool: |
| 6363 |
zero = builder.makeUintConstant(0); |
| 6364 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6365 |
return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6366 |
case glslang::EOpConvFloatToBool: |
| 6367 |
zero = builder.makeFloatConstant(0.0F); |
| 6368 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6369 |
return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
| 6370 |
case glslang::EOpConvBoolToFloat: |
| 6371 |
convOp = spv::OpSelect; |
| 6372 |
zero = builder.makeFloatConstant(0.0F); |
| 6373 |
one = builder.makeFloatConstant(1.0F); |
| 6374 |
break; |
| 6375 |
|
| 6376 |
case glslang::EOpConvBoolToInt: |
| 6377 |
case glslang::EOpConvBoolToInt64: |
| 6378 |
#ifndef GLSLANG_WEB |
| 6379 |
if (op == glslang::EOpConvBoolToInt64) { |
| 6380 |
zero = builder.makeInt64Constant(0); |
| 6381 |
one = builder.makeInt64Constant(1); |
| 6382 |
} else |
| 6383 |
#endif |
| 6384 |
{ |
| 6385 |
zero = builder.makeIntConstant(0); |
| 6386 |
one = builder.makeIntConstant(1); |
| 6387 |
} |
| 6388 |
|
| 6389 |
convOp = spv::OpSelect; |
| 6390 |
break; |
| 6391 |
|
| 6392 |
case glslang::EOpConvBoolToUint: |
| 6393 |
case glslang::EOpConvBoolToUint64: |
| 6394 |
#ifndef GLSLANG_WEB |
| 6395 |
if (op == glslang::EOpConvBoolToUint64) { |
| 6396 |
zero = builder.makeUint64Constant(0); |
| 6397 |
one = builder.makeUint64Constant(1); |
| 6398 |
} else |
| 6399 |
#endif |
| 6400 |
{ |
| 6401 |
zero = builder.makeUintConstant(0); |
| 6402 |
one = builder.makeUintConstant(1); |
| 6403 |
} |
| 6404 |
|
| 6405 |
convOp = spv::OpSelect; |
| 6406 |
break; |
| 6407 |
|
| 6408 |
case glslang::EOpConvInt8ToFloat16: |
| 6409 |
case glslang::EOpConvInt8ToFloat: |
| 6410 |
case glslang::EOpConvInt8ToDouble: |
| 6411 |
case glslang::EOpConvInt16ToFloat16: |
| 6412 |
case glslang::EOpConvInt16ToFloat: |
| 6413 |
case glslang::EOpConvInt16ToDouble: |
| 6414 |
case glslang::EOpConvIntToFloat16: |
| 6415 |
case glslang::EOpConvIntToFloat: |
| 6416 |
case glslang::EOpConvIntToDouble: |
| 6417 |
case glslang::EOpConvInt64ToFloat: |
| 6418 |
case glslang::EOpConvInt64ToDouble: |
| 6419 |
case glslang::EOpConvInt64ToFloat16: |
| 6420 |
convOp = spv::OpConvertSToF; |
| 6421 |
break; |
| 6422 |
|
| 6423 |
case glslang::EOpConvUint8ToFloat16: |
| 6424 |
case glslang::EOpConvUint8ToFloat: |
| 6425 |
case glslang::EOpConvUint8ToDouble: |
| 6426 |
case glslang::EOpConvUint16ToFloat16: |
| 6427 |
case glslang::EOpConvUint16ToFloat: |
| 6428 |
case glslang::EOpConvUint16ToDouble: |
| 6429 |
case glslang::EOpConvUintToFloat16: |
| 6430 |
case glslang::EOpConvUintToFloat: |
| 6431 |
case glslang::EOpConvUintToDouble: |
| 6432 |
case glslang::EOpConvUint64ToFloat: |
| 6433 |
case glslang::EOpConvUint64ToDouble: |
| 6434 |
case glslang::EOpConvUint64ToFloat16: |
| 6435 |
convOp = spv::OpConvertUToF; |
| 6436 |
break; |
| 6437 |
|
| 6438 |
case glslang::EOpConvFloat16ToInt8: |
| 6439 |
case glslang::EOpConvFloatToInt8: |
| 6440 |
case glslang::EOpConvDoubleToInt8: |
| 6441 |
case glslang::EOpConvFloat16ToInt16: |
| 6442 |
case glslang::EOpConvFloatToInt16: |
| 6443 |
case glslang::EOpConvDoubleToInt16: |
| 6444 |
case glslang::EOpConvFloat16ToInt: |
| 6445 |
case glslang::EOpConvFloatToInt: |
| 6446 |
case glslang::EOpConvDoubleToInt: |
| 6447 |
case glslang::EOpConvFloat16ToInt64: |
| 6448 |
case glslang::EOpConvFloatToInt64: |
| 6449 |
case glslang::EOpConvDoubleToInt64: |
| 6450 |
convOp = spv::OpConvertFToS; |
| 6451 |
break; |
| 6452 |
|
| 6453 |
case glslang::EOpConvUint8ToInt8: |
| 6454 |
case glslang::EOpConvInt8ToUint8: |
| 6455 |
case glslang::EOpConvUint16ToInt16: |
| 6456 |
case glslang::EOpConvInt16ToUint16: |
| 6457 |
case glslang::EOpConvUintToInt: |
| 6458 |
case glslang::EOpConvIntToUint: |
| 6459 |
case glslang::EOpConvUint64ToInt64: |
| 6460 |
case glslang::EOpConvInt64ToUint64: |
| 6461 |
if (builder.isInSpecConstCodeGenMode()) { |
| 6462 |
// Build zero scalar or vector for OpIAdd. |
| 6463 |
#ifndef GLSLANG_WEB |
| 6464 |
if(op == glslang::EOpConvUint8ToInt8 || op == glslang::EOpConvInt8ToUint8) { |
| 6465 |
zero = builder.makeUint8Constant(0); |
| 6466 |
} else if (op == glslang::EOpConvUint16ToInt16 || op == glslang::EOpConvInt16ToUint16) { |
| 6467 |
zero = builder.makeUint16Constant(0); |
| 6468 |
} else if (op == glslang::EOpConvUint64ToInt64 || op == glslang::EOpConvInt64ToUint64) { |
| 6469 |
zero = builder.makeUint64Constant(0); |
| 6470 |
} else |
| 6471 |
#endif |
| 6472 |
{ |
| 6473 |
zero = builder.makeUintConstant(0); |
| 6474 |
} |
| 6475 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6476 |
// Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6477 |
// generating for OpSpecConstantOp instruction. |
| 6478 |
return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6479 |
} |
| 6480 |
// For normal run-time conversion instruction, use OpBitcast. |
| 6481 |
convOp = spv::OpBitcast; |
| 6482 |
break; |
| 6483 |
|
| 6484 |
case glslang::EOpConvFloat16ToUint8: |
| 6485 |
case glslang::EOpConvFloatToUint8: |
| 6486 |
case glslang::EOpConvDoubleToUint8: |
| 6487 |
case glslang::EOpConvFloat16ToUint16: |
| 6488 |
case glslang::EOpConvFloatToUint16: |
| 6489 |
case glslang::EOpConvDoubleToUint16: |
| 6490 |
case glslang::EOpConvFloat16ToUint: |
| 6491 |
case glslang::EOpConvFloatToUint: |
| 6492 |
case glslang::EOpConvDoubleToUint: |
| 6493 |
case glslang::EOpConvFloatToUint64: |
| 6494 |
case glslang::EOpConvDoubleToUint64: |
| 6495 |
case glslang::EOpConvFloat16ToUint64: |
| 6496 |
convOp = spv::OpConvertFToU; |
| 6497 |
break; |
| 6498 |
|
| 6499 |
#ifndef GLSLANG_WEB |
| 6500 |
case glslang::EOpConvInt8ToBool: |
| 6501 |
case glslang::EOpConvUint8ToBool: |
| 6502 |
zero = builder.makeUint8Constant(0); |
| 6503 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6504 |
return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6505 |
case glslang::EOpConvInt16ToBool: |
| 6506 |
case glslang::EOpConvUint16ToBool: |
| 6507 |
zero = builder.makeUint16Constant(0); |
| 6508 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6509 |
return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6510 |
case glslang::EOpConvInt64ToBool: |
| 6511 |
case glslang::EOpConvUint64ToBool: |
| 6512 |
zero = builder.makeUint64Constant(0); |
| 6513 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6514 |
return builder.createBinOp(spv::OpINotEqual, destType, operand, zero); |
| 6515 |
case glslang::EOpConvDoubleToBool: |
| 6516 |
zero = builder.makeDoubleConstant(0.0); |
| 6517 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6518 |
return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
| 6519 |
case glslang::EOpConvFloat16ToBool: |
| 6520 |
zero = builder.makeFloat16Constant(0.0F); |
| 6521 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6522 |
return builder.createBinOp(spv::OpFUnordNotEqual, destType, operand, zero); |
| 6523 |
case glslang::EOpConvBoolToDouble: |
| 6524 |
convOp = spv::OpSelect; |
| 6525 |
zero = builder.makeDoubleConstant(0.0); |
| 6526 |
one = builder.makeDoubleConstant(1.0); |
| 6527 |
break; |
| 6528 |
case glslang::EOpConvBoolToFloat16: |
| 6529 |
convOp = spv::OpSelect; |
| 6530 |
zero = builder.makeFloat16Constant(0.0F); |
| 6531 |
one = builder.makeFloat16Constant(1.0F); |
| 6532 |
break; |
| 6533 |
case glslang::EOpConvBoolToInt8: |
| 6534 |
zero = builder.makeInt8Constant(0); |
| 6535 |
one = builder.makeInt8Constant(1); |
| 6536 |
convOp = spv::OpSelect; |
| 6537 |
break; |
| 6538 |
case glslang::EOpConvBoolToUint8: |
| 6539 |
zero = builder.makeUint8Constant(0); |
| 6540 |
one = builder.makeUint8Constant(1); |
| 6541 |
convOp = spv::OpSelect; |
| 6542 |
break; |
| 6543 |
case glslang::EOpConvBoolToInt16: |
| 6544 |
zero = builder.makeInt16Constant(0); |
| 6545 |
one = builder.makeInt16Constant(1); |
| 6546 |
convOp = spv::OpSelect; |
| 6547 |
break; |
| 6548 |
case glslang::EOpConvBoolToUint16: |
| 6549 |
zero = builder.makeUint16Constant(0); |
| 6550 |
one = builder.makeUint16Constant(1); |
| 6551 |
convOp = spv::OpSelect; |
| 6552 |
break; |
| 6553 |
case glslang::EOpConvDoubleToFloat: |
| 6554 |
case glslang::EOpConvFloatToDouble: |
| 6555 |
case glslang::EOpConvDoubleToFloat16: |
| 6556 |
case glslang::EOpConvFloat16ToDouble: |
| 6557 |
case glslang::EOpConvFloatToFloat16: |
| 6558 |
case glslang::EOpConvFloat16ToFloat: |
| 6559 |
convOp = spv::OpFConvert; |
| 6560 |
if (builder.isMatrixType(destType)) |
| 6561 |
return createUnaryMatrixOperation(convOp, decorations, destType, operand, typeProxy); |
| 6562 |
break; |
| 6563 |
|
| 6564 |
case glslang::EOpConvInt8ToInt16: |
| 6565 |
case glslang::EOpConvInt8ToInt: |
| 6566 |
case glslang::EOpConvInt8ToInt64: |
| 6567 |
case glslang::EOpConvInt16ToInt8: |
| 6568 |
case glslang::EOpConvInt16ToInt: |
| 6569 |
case glslang::EOpConvInt16ToInt64: |
| 6570 |
case glslang::EOpConvIntToInt8: |
| 6571 |
case glslang::EOpConvIntToInt16: |
| 6572 |
case glslang::EOpConvIntToInt64: |
| 6573 |
case glslang::EOpConvInt64ToInt8: |
| 6574 |
case glslang::EOpConvInt64ToInt16: |
| 6575 |
case glslang::EOpConvInt64ToInt: |
| 6576 |
convOp = spv::OpSConvert; |
| 6577 |
break; |
| 6578 |
|
| 6579 |
case glslang::EOpConvUint8ToUint16: |
| 6580 |
case glslang::EOpConvUint8ToUint: |
| 6581 |
case glslang::EOpConvUint8ToUint64: |
| 6582 |
case glslang::EOpConvUint16ToUint8: |
| 6583 |
case glslang::EOpConvUint16ToUint: |
| 6584 |
case glslang::EOpConvUint16ToUint64: |
| 6585 |
case glslang::EOpConvUintToUint8: |
| 6586 |
case glslang::EOpConvUintToUint16: |
| 6587 |
case glslang::EOpConvUintToUint64: |
| 6588 |
case glslang::EOpConvUint64ToUint8: |
| 6589 |
case glslang::EOpConvUint64ToUint16: |
| 6590 |
case glslang::EOpConvUint64ToUint: |
| 6591 |
convOp = spv::OpUConvert; |
| 6592 |
break; |
| 6593 |
|
| 6594 |
case glslang::EOpConvInt8ToUint16: |
| 6595 |
case glslang::EOpConvInt8ToUint: |
| 6596 |
case glslang::EOpConvInt8ToUint64: |
| 6597 |
case glslang::EOpConvInt16ToUint8: |
| 6598 |
case glslang::EOpConvInt16ToUint: |
| 6599 |
case glslang::EOpConvInt16ToUint64: |
| 6600 |
case glslang::EOpConvIntToUint8: |
| 6601 |
case glslang::EOpConvIntToUint16: |
| 6602 |
case glslang::EOpConvIntToUint64: |
| 6603 |
case glslang::EOpConvInt64ToUint8: |
| 6604 |
case glslang::EOpConvInt64ToUint16: |
| 6605 |
case glslang::EOpConvInt64ToUint: |
| 6606 |
case glslang::EOpConvUint8ToInt16: |
| 6607 |
case glslang::EOpConvUint8ToInt: |
| 6608 |
case glslang::EOpConvUint8ToInt64: |
| 6609 |
case glslang::EOpConvUint16ToInt8: |
| 6610 |
case glslang::EOpConvUint16ToInt: |
| 6611 |
case glslang::EOpConvUint16ToInt64: |
| 6612 |
case glslang::EOpConvUintToInt8: |
| 6613 |
case glslang::EOpConvUintToInt16: |
| 6614 |
case glslang::EOpConvUintToInt64: |
| 6615 |
case glslang::EOpConvUint64ToInt8: |
| 6616 |
case glslang::EOpConvUint64ToInt16: |
| 6617 |
case glslang::EOpConvUint64ToInt: |
| 6618 |
// OpSConvert/OpUConvert + OpBitCast |
| 6619 |
operand = createIntWidthConversion(op, operand, vectorSize); |
| 6620 |
|
| 6621 |
if (builder.isInSpecConstCodeGenMode()) { |
| 6622 |
// Build zero scalar or vector for OpIAdd. |
| 6623 |
switch(op) { |
| 6624 |
case glslang::EOpConvInt16ToUint8: |
| 6625 |
case glslang::EOpConvIntToUint8: |
| 6626 |
case glslang::EOpConvInt64ToUint8: |
| 6627 |
case glslang::EOpConvUint16ToInt8: |
| 6628 |
case glslang::EOpConvUintToInt8: |
| 6629 |
case glslang::EOpConvUint64ToInt8: |
| 6630 |
zero = builder.makeUint8Constant(0); |
| 6631 |
break; |
| 6632 |
case glslang::EOpConvInt8ToUint16: |
| 6633 |
case glslang::EOpConvIntToUint16: |
| 6634 |
case glslang::EOpConvInt64ToUint16: |
| 6635 |
case glslang::EOpConvUint8ToInt16: |
| 6636 |
case glslang::EOpConvUintToInt16: |
| 6637 |
case glslang::EOpConvUint64ToInt16: |
| 6638 |
zero = builder.makeUint16Constant(0); |
| 6639 |
break; |
| 6640 |
case glslang::EOpConvInt8ToUint: |
| 6641 |
case glslang::EOpConvInt16ToUint: |
| 6642 |
case glslang::EOpConvInt64ToUint: |
| 6643 |
case glslang::EOpConvUint8ToInt: |
| 6644 |
case glslang::EOpConvUint16ToInt: |
| 6645 |
case glslang::EOpConvUint64ToInt: |
| 6646 |
zero = builder.makeUintConstant(0); |
| 6647 |
break; |
| 6648 |
case glslang::EOpConvInt8ToUint64: |
| 6649 |
case glslang::EOpConvInt16ToUint64: |
| 6650 |
case glslang::EOpConvIntToUint64: |
| 6651 |
case glslang::EOpConvUint8ToInt64: |
| 6652 |
case glslang::EOpConvUint16ToInt64: |
| 6653 |
case glslang::EOpConvUintToInt64: |
| 6654 |
zero = builder.makeUint64Constant(0); |
| 6655 |
break; |
| 6656 |
default: |
| 6657 |
assert(false && "Default missing"); |
| 6658 |
break; |
| 6659 |
} |
| 6660 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6661 |
// Use OpIAdd, instead of OpBitcast to do the conversion when |
| 6662 |
// generating for OpSpecConstantOp instruction. |
| 6663 |
return builder.createBinOp(spv::OpIAdd, destType, operand, zero); |
| 6664 |
} |
| 6665 |
// For normal run-time conversion instruction, use OpBitcast. |
| 6666 |
convOp = spv::OpBitcast; |
| 6667 |
break; |
| 6668 |
case glslang::EOpConvUint64ToPtr: |
| 6669 |
convOp = spv::OpConvertUToPtr; |
| 6670 |
break; |
| 6671 |
case glslang::EOpConvPtrToUint64: |
| 6672 |
convOp = spv::OpConvertPtrToU; |
| 6673 |
break; |
| 6674 |
case glslang::EOpConvPtrToUvec2: |
| 6675 |
case glslang::EOpConvUvec2ToPtr: |
| 6676 |
if (builder.isVector(operand)) |
| 6677 |
builder.promoteIncorporatedExtension(spv::E_SPV_EXT_physical_storage_buffer, |
| 6678 |
spv::E_SPV_KHR_physical_storage_buffer, spv::Spv_1_5); |
| 6679 |
convOp = spv::OpBitcast; |
| 6680 |
break; |
| 6681 |
#endif |
| 6682 |
|
| 6683 |
default: |
| 6684 |
break; |
| 6685 |
} |
| 6686 |
|
| 6687 |
spv::Id result = 0; |
| 6688 |
if (convOp == spv::OpNop) |
| 6689 |
return result; |
| 6690 |
|
| 6691 |
if (convOp == spv::OpSelect) { |
| 6692 |
zero = makeSmearedConstant(zero, vectorSize); |
| 6693 |
one = makeSmearedConstant(one, vectorSize); |
| 6694 |
result = builder.createTriOp(convOp, destType, operand, one, zero); |
| 6695 |
} else |
| 6696 |
result = builder.createUnaryOp(convOp, destType, operand); |
| 6697 |
|
| 6698 |
result = builder.setPrecision(result, decorations.precision); |
| 6699 |
decorations.addNonUniform(builder, result); |
| 6700 |
return result; |
| 6701 |
} |
| 6702 |
|
| 6703 |
spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize) |
| 6704 |
{ |
| 6705 |
if (vectorSize == 0) |
| 6706 |
return constant; |
| 6707 |
|
| 6708 |
spv::Id vectorTypeId = builder.makeVectorType(builder.getTypeId(constant), vectorSize); |
| 6709 |
std::vector<spv::Id> components; |
| 6710 |
for (int c = 0; c < vectorSize; ++c) |
| 6711 |
components.push_back(constant); |
| 6712 |
return builder.makeCompositeConstant(vectorTypeId, components); |
| 6713 |
} |
| 6714 |
|
| 6715 |
// For glslang ops that map to SPV atomic opCodes |
| 6716 |
spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration /*precision*/, |
| 6717 |
spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy, |
| 6718 |
const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags) |
| 6719 |
{ |
| 6720 |
spv::Op opCode = spv::OpNop; |
| 6721 |
|
| 6722 |
switch (op) { |
| 6723 |
case glslang::EOpAtomicAdd: |
| 6724 |
case glslang::EOpImageAtomicAdd: |
| 6725 |
case glslang::EOpAtomicCounterAdd: |
| 6726 |
opCode = spv::OpAtomicIAdd; |
| 6727 |
if (typeProxy == glslang::EbtFloat || typeProxy == glslang::EbtDouble) { |
| 6728 |
opCode = spv::OpAtomicFAddEXT; |
| 6729 |
builder.addExtension(spv::E_SPV_EXT_shader_atomic_float_add); |
| 6730 |
if (typeProxy == glslang::EbtFloat) |
| 6731 |
builder.addCapability(spv::CapabilityAtomicFloat32AddEXT); |
| 6732 |
else |
| 6733 |
builder.addCapability(spv::CapabilityAtomicFloat64AddEXT); |
| 6734 |
} |
| 6735 |
break; |
| 6736 |
case glslang::EOpAtomicCounterSubtract: |
| 6737 |
opCode = spv::OpAtomicISub; |
| 6738 |
break; |
| 6739 |
case glslang::EOpAtomicMin: |
| 6740 |
case glslang::EOpImageAtomicMin: |
| 6741 |
case glslang::EOpAtomicCounterMin: |
| 6742 |
opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? |
| 6743 |
spv::OpAtomicUMin : spv::OpAtomicSMin; |
| 6744 |
break; |
| 6745 |
case glslang::EOpAtomicMax: |
| 6746 |
case glslang::EOpImageAtomicMax: |
| 6747 |
case glslang::EOpAtomicCounterMax: |
| 6748 |
opCode = (typeProxy == glslang::EbtUint || typeProxy == glslang::EbtUint64) ? |
| 6749 |
spv::OpAtomicUMax : spv::OpAtomicSMax; |
| 6750 |
break; |
| 6751 |
case glslang::EOpAtomicAnd: |
| 6752 |
case glslang::EOpImageAtomicAnd: |
| 6753 |
case glslang::EOpAtomicCounterAnd: |
| 6754 |
opCode = spv::OpAtomicAnd; |
| 6755 |
break; |
| 6756 |
case glslang::EOpAtomicOr: |
| 6757 |
case glslang::EOpImageAtomicOr: |
| 6758 |
case glslang::EOpAtomicCounterOr: |
| 6759 |
opCode = spv::OpAtomicOr; |
| 6760 |
break; |
| 6761 |
case glslang::EOpAtomicXor: |
| 6762 |
case glslang::EOpImageAtomicXor: |
| 6763 |
case glslang::EOpAtomicCounterXor: |
| 6764 |
opCode = spv::OpAtomicXor; |
| 6765 |
break; |
| 6766 |
case glslang::EOpAtomicExchange: |
| 6767 |
case glslang::EOpImageAtomicExchange: |
| 6768 |
case glslang::EOpAtomicCounterExchange: |
| 6769 |
opCode = spv::OpAtomicExchange; |
| 6770 |
break; |
| 6771 |
case glslang::EOpAtomicCompSwap: |
| 6772 |
case glslang::EOpImageAtomicCompSwap: |
| 6773 |
case glslang::EOpAtomicCounterCompSwap: |
| 6774 |
opCode = spv::OpAtomicCompareExchange; |
| 6775 |
break; |
| 6776 |
case glslang::EOpAtomicCounterIncrement: |
| 6777 |
opCode = spv::OpAtomicIIncrement; |
| 6778 |
break; |
| 6779 |
case glslang::EOpAtomicCounterDecrement: |
| 6780 |
opCode = spv::OpAtomicIDecrement; |
| 6781 |
break; |
| 6782 |
case glslang::EOpAtomicCounter: |
| 6783 |
case glslang::EOpImageAtomicLoad: |
| 6784 |
case glslang::EOpAtomicLoad: |
| 6785 |
opCode = spv::OpAtomicLoad; |
| 6786 |
break; |
| 6787 |
case glslang::EOpAtomicStore: |
| 6788 |
case glslang::EOpImageAtomicStore: |
| 6789 |
opCode = spv::OpAtomicStore; |
| 6790 |
break; |
| 6791 |
default: |
| 6792 |
assert(0); |
| 6793 |
break; |
| 6794 |
} |
| 6795 |
|
| 6796 |
if (typeProxy == glslang::EbtInt64 || typeProxy == glslang::EbtUint64) |
| 6797 |
builder.addCapability(spv::CapabilityInt64Atomics); |
| 6798 |
|
| 6799 |
// Sort out the operands |
| 6800 |
// - mapping from glslang -> SPV |
| 6801 |
// - there are extra SPV operands that are optional in glslang |
| 6802 |
// - compare-exchange swaps the value and comparator |
| 6803 |
// - compare-exchange has an extra memory semantics |
| 6804 |
// - EOpAtomicCounterDecrement needs a post decrement |
| 6805 |
spv::Id pointerId = 0, compareId = 0, valueId = 0; |
| 6806 |
// scope defaults to Device in the old model, QueueFamilyKHR in the new model |
| 6807 |
spv::Id scopeId; |
| 6808 |
if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 6809 |
scopeId = builder.makeUintConstant(spv::ScopeQueueFamilyKHR); |
| 6810 |
} else { |
| 6811 |
scopeId = builder.makeUintConstant(spv::ScopeDevice); |
| 6812 |
} |
| 6813 |
// semantics default to relaxed |
| 6814 |
spv::Id semanticsId = builder.makeUintConstant(lvalueCoherentFlags.isVolatile() && |
| 6815 |
glslangIntermediate->usingVulkanMemoryModel() ? |
| 6816 |
spv::MemorySemanticsVolatileMask : |
| 6817 |
spv::MemorySemanticsMaskNone); |
| 6818 |
spv::Id semanticsId2 = semanticsId; |
| 6819 |
|
| 6820 |
pointerId = operands[0]; |
| 6821 |
if (opCode == spv::OpAtomicIIncrement || opCode == spv::OpAtomicIDecrement) { |
| 6822 |
// no additional operands |
| 6823 |
} else if (opCode == spv::OpAtomicCompareExchange) { |
| 6824 |
compareId = operands[1]; |
| 6825 |
valueId = operands[2]; |
| 6826 |
if (operands.size() > 3) { |
| 6827 |
scopeId = operands[3]; |
| 6828 |
semanticsId = builder.makeUintConstant( |
| 6829 |
builder.getConstantScalar(operands[4]) | builder.getConstantScalar(operands[5])); |
| 6830 |
semanticsId2 = builder.makeUintConstant( |
| 6831 |
builder.getConstantScalar(operands[6]) | builder.getConstantScalar(operands[7])); |
| 6832 |
} |
| 6833 |
} else if (opCode == spv::OpAtomicLoad) { |
| 6834 |
if (operands.size() > 1) { |
| 6835 |
scopeId = operands[1]; |
| 6836 |
semanticsId = builder.makeUintConstant( |
| 6837 |
builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3])); |
| 6838 |
} |
| 6839 |
} else { |
| 6840 |
// atomic store or RMW |
| 6841 |
valueId = operands[1]; |
| 6842 |
if (operands.size() > 2) { |
| 6843 |
scopeId = operands[2]; |
| 6844 |
semanticsId = builder.makeUintConstant |
| 6845 |
(builder.getConstantScalar(operands[3]) | builder.getConstantScalar(operands[4])); |
| 6846 |
} |
| 6847 |
} |
| 6848 |
|
| 6849 |
// Check for capabilities |
| 6850 |
unsigned semanticsImmediate = builder.getConstantScalar(semanticsId) | builder.getConstantScalar(semanticsId2); |
| 6851 |
if (semanticsImmediate & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 6852 |
spv::MemorySemanticsMakeVisibleKHRMask | |
| 6853 |
spv::MemorySemanticsOutputMemoryKHRMask | |
| 6854 |
spv::MemorySemanticsVolatileMask)) { |
| 6855 |
builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 6856 |
} |
| 6857 |
|
| 6858 |
if (glslangIntermediate->usingVulkanMemoryModel() && builder.getConstantScalar(scopeId) == spv::ScopeDevice) { |
| 6859 |
builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 6860 |
} |
| 6861 |
|
| 6862 |
std::vector<spv::Id> spvAtomicOperands; // hold the spv operands |
| 6863 |
spvAtomicOperands.push_back(pointerId); |
| 6864 |
spvAtomicOperands.push_back(scopeId); |
| 6865 |
spvAtomicOperands.push_back(semanticsId); |
| 6866 |
if (opCode == spv::OpAtomicCompareExchange) { |
| 6867 |
spvAtomicOperands.push_back(semanticsId2); |
| 6868 |
spvAtomicOperands.push_back(valueId); |
| 6869 |
spvAtomicOperands.push_back(compareId); |
| 6870 |
} else if (opCode != spv::OpAtomicLoad && opCode != spv::OpAtomicIIncrement && opCode != spv::OpAtomicIDecrement) { |
| 6871 |
spvAtomicOperands.push_back(valueId); |
| 6872 |
} |
| 6873 |
|
| 6874 |
if (opCode == spv::OpAtomicStore) { |
| 6875 |
builder.createNoResultOp(opCode, spvAtomicOperands); |
| 6876 |
return 0; |
| 6877 |
} else { |
| 6878 |
spv::Id resultId = builder.createOp(opCode, typeId, spvAtomicOperands); |
| 6879 |
|
| 6880 |
// GLSL and HLSL atomic-counter decrement return post-decrement value, |
| 6881 |
// while SPIR-V returns pre-decrement value. Translate between these semantics. |
| 6882 |
if (op == glslang::EOpAtomicCounterDecrement) |
| 6883 |
resultId = builder.createBinOp(spv::OpISub, typeId, resultId, builder.makeIntConstant(1)); |
| 6884 |
|
| 6885 |
return resultId; |
| 6886 |
} |
| 6887 |
} |
| 6888 |
|
| 6889 |
// Create group invocation operations. |
| 6890 |
spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId, |
| 6891 |
std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
| 6892 |
{ |
| 6893 |
bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 6894 |
bool isFloat = isTypeFloat(typeProxy); |
| 6895 |
|
| 6896 |
spv::Op opCode = spv::OpNop; |
| 6897 |
std::vector<spv::IdImmediate> spvGroupOperands; |
| 6898 |
spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 6899 |
|
| 6900 |
if (op == glslang::EOpBallot || op == glslang::EOpReadFirstInvocation || |
| 6901 |
op == glslang::EOpReadInvocation) { |
| 6902 |
builder.addExtension(spv::E_SPV_KHR_shader_ballot); |
| 6903 |
builder.addCapability(spv::CapabilitySubgroupBallotKHR); |
| 6904 |
} else if (op == glslang::EOpAnyInvocation || |
| 6905 |
op == glslang::EOpAllInvocations || |
| 6906 |
op == glslang::EOpAllInvocationsEqual) { |
| 6907 |
builder.addExtension(spv::E_SPV_KHR_subgroup_vote); |
| 6908 |
builder.addCapability(spv::CapabilitySubgroupVoteKHR); |
| 6909 |
} else { |
| 6910 |
builder.addCapability(spv::CapabilityGroups); |
| 6911 |
if (op == glslang::EOpMinInvocationsNonUniform || |
| 6912 |
op == glslang::EOpMaxInvocationsNonUniform || |
| 6913 |
op == glslang::EOpAddInvocationsNonUniform || |
| 6914 |
op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 6915 |
op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 6916 |
op == glslang::EOpAddInvocationsInclusiveScanNonUniform || |
| 6917 |
op == glslang::EOpMinInvocationsExclusiveScanNonUniform || |
| 6918 |
op == glslang::EOpMaxInvocationsExclusiveScanNonUniform || |
| 6919 |
op == glslang::EOpAddInvocationsExclusiveScanNonUniform) |
| 6920 |
builder.addExtension(spv::E_SPV_AMD_shader_ballot); |
| 6921 |
|
| 6922 |
switch (op) { |
| 6923 |
case glslang::EOpMinInvocations: |
| 6924 |
case glslang::EOpMaxInvocations: |
| 6925 |
case glslang::EOpAddInvocations: |
| 6926 |
case glslang::EOpMinInvocationsNonUniform: |
| 6927 |
case glslang::EOpMaxInvocationsNonUniform: |
| 6928 |
case glslang::EOpAddInvocationsNonUniform: |
| 6929 |
groupOperation = spv::GroupOperationReduce; |
| 6930 |
break; |
| 6931 |
case glslang::EOpMinInvocationsInclusiveScan: |
| 6932 |
case glslang::EOpMaxInvocationsInclusiveScan: |
| 6933 |
case glslang::EOpAddInvocationsInclusiveScan: |
| 6934 |
case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 6935 |
case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 6936 |
case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 6937 |
groupOperation = spv::GroupOperationInclusiveScan; |
| 6938 |
break; |
| 6939 |
case glslang::EOpMinInvocationsExclusiveScan: |
| 6940 |
case glslang::EOpMaxInvocationsExclusiveScan: |
| 6941 |
case glslang::EOpAddInvocationsExclusiveScan: |
| 6942 |
case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 6943 |
case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 6944 |
case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 6945 |
groupOperation = spv::GroupOperationExclusiveScan; |
| 6946 |
break; |
| 6947 |
default: |
| 6948 |
break; |
| 6949 |
} |
| 6950 |
spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 6951 |
spvGroupOperands.push_back(scope); |
| 6952 |
if (groupOperation != spv::GroupOperationMax) { |
| 6953 |
spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
| 6954 |
spvGroupOperands.push_back(groupOp); |
| 6955 |
} |
| 6956 |
} |
| 6957 |
|
| 6958 |
for (auto opIt = operands.begin(); opIt != operands.end(); ++opIt) { |
| 6959 |
spv::IdImmediate op = { true, *opIt }; |
| 6960 |
spvGroupOperands.push_back(op); |
| 6961 |
} |
| 6962 |
|
| 6963 |
switch (op) { |
| 6964 |
case glslang::EOpAnyInvocation: |
| 6965 |
opCode = spv::OpSubgroupAnyKHR; |
| 6966 |
break; |
| 6967 |
case glslang::EOpAllInvocations: |
| 6968 |
opCode = spv::OpSubgroupAllKHR; |
| 6969 |
break; |
| 6970 |
case glslang::EOpAllInvocationsEqual: |
| 6971 |
opCode = spv::OpSubgroupAllEqualKHR; |
| 6972 |
break; |
| 6973 |
case glslang::EOpReadInvocation: |
| 6974 |
opCode = spv::OpSubgroupReadInvocationKHR; |
| 6975 |
if (builder.isVectorType(typeId)) |
| 6976 |
return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
| 6977 |
break; |
| 6978 |
case glslang::EOpReadFirstInvocation: |
| 6979 |
opCode = spv::OpSubgroupFirstInvocationKHR; |
| 6980 |
break; |
| 6981 |
case glslang::EOpBallot: |
| 6982 |
{ |
| 6983 |
// NOTE: According to the spec, the result type of "OpSubgroupBallotKHR" must be a 4 component vector of 32 |
| 6984 |
// bit integer types. The GLSL built-in function "ballotARB()" assumes the maximum number of invocations in |
| 6985 |
// a subgroup is 64. Thus, we have to convert uvec4.xy to uint64_t as follow: |
| 6986 |
// |
| 6987 |
// result = Bitcast(SubgroupBallotKHR(Predicate).xy) |
| 6988 |
// |
| 6989 |
spv::Id uintType = builder.makeUintType(32); |
| 6990 |
spv::Id uvec4Type = builder.makeVectorType(uintType, 4); |
| 6991 |
spv::Id result = builder.createOp(spv::OpSubgroupBallotKHR, uvec4Type, spvGroupOperands); |
| 6992 |
|
| 6993 |
std::vector<spv::Id> components; |
| 6994 |
components.push_back(builder.createCompositeExtract(result, uintType, 0)); |
| 6995 |
components.push_back(builder.createCompositeExtract(result, uintType, 1)); |
| 6996 |
|
| 6997 |
spv::Id uvec2Type = builder.makeVectorType(uintType, 2); |
| 6998 |
return builder.createUnaryOp(spv::OpBitcast, typeId, |
| 6999 |
builder.createCompositeConstruct(uvec2Type, components)); |
| 7000 |
} |
| 7001 |
|
| 7002 |
case glslang::EOpMinInvocations: |
| 7003 |
case glslang::EOpMaxInvocations: |
| 7004 |
case glslang::EOpAddInvocations: |
| 7005 |
case glslang::EOpMinInvocationsInclusiveScan: |
| 7006 |
case glslang::EOpMaxInvocationsInclusiveScan: |
| 7007 |
case glslang::EOpAddInvocationsInclusiveScan: |
| 7008 |
case glslang::EOpMinInvocationsExclusiveScan: |
| 7009 |
case glslang::EOpMaxInvocationsExclusiveScan: |
| 7010 |
case glslang::EOpAddInvocationsExclusiveScan: |
| 7011 |
if (op == glslang::EOpMinInvocations || |
| 7012 |
op == glslang::EOpMinInvocationsInclusiveScan || |
| 7013 |
op == glslang::EOpMinInvocationsExclusiveScan) { |
| 7014 |
if (isFloat) |
| 7015 |
opCode = spv::OpGroupFMin; |
| 7016 |
else { |
| 7017 |
if (isUnsigned) |
| 7018 |
opCode = spv::OpGroupUMin; |
| 7019 |
else |
| 7020 |
opCode = spv::OpGroupSMin; |
| 7021 |
} |
| 7022 |
} else if (op == glslang::EOpMaxInvocations || |
| 7023 |
op == glslang::EOpMaxInvocationsInclusiveScan || |
| 7024 |
op == glslang::EOpMaxInvocationsExclusiveScan) { |
| 7025 |
if (isFloat) |
| 7026 |
opCode = spv::OpGroupFMax; |
| 7027 |
else { |
| 7028 |
if (isUnsigned) |
| 7029 |
opCode = spv::OpGroupUMax; |
| 7030 |
else |
| 7031 |
opCode = spv::OpGroupSMax; |
| 7032 |
} |
| 7033 |
} else { |
| 7034 |
if (isFloat) |
| 7035 |
opCode = spv::OpGroupFAdd; |
| 7036 |
else |
| 7037 |
opCode = spv::OpGroupIAdd; |
| 7038 |
} |
| 7039 |
|
| 7040 |
if (builder.isVectorType(typeId)) |
| 7041 |
return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
| 7042 |
|
| 7043 |
break; |
| 7044 |
case glslang::EOpMinInvocationsNonUniform: |
| 7045 |
case glslang::EOpMaxInvocationsNonUniform: |
| 7046 |
case glslang::EOpAddInvocationsNonUniform: |
| 7047 |
case glslang::EOpMinInvocationsInclusiveScanNonUniform: |
| 7048 |
case glslang::EOpMaxInvocationsInclusiveScanNonUniform: |
| 7049 |
case glslang::EOpAddInvocationsInclusiveScanNonUniform: |
| 7050 |
case glslang::EOpMinInvocationsExclusiveScanNonUniform: |
| 7051 |
case glslang::EOpMaxInvocationsExclusiveScanNonUniform: |
| 7052 |
case glslang::EOpAddInvocationsExclusiveScanNonUniform: |
| 7053 |
if (op == glslang::EOpMinInvocationsNonUniform || |
| 7054 |
op == glslang::EOpMinInvocationsInclusiveScanNonUniform || |
| 7055 |
op == glslang::EOpMinInvocationsExclusiveScanNonUniform) { |
| 7056 |
if (isFloat) |
| 7057 |
opCode = spv::OpGroupFMinNonUniformAMD; |
| 7058 |
else { |
| 7059 |
if (isUnsigned) |
| 7060 |
opCode = spv::OpGroupUMinNonUniformAMD; |
| 7061 |
else |
| 7062 |
opCode = spv::OpGroupSMinNonUniformAMD; |
| 7063 |
} |
| 7064 |
} |
| 7065 |
else if (op == glslang::EOpMaxInvocationsNonUniform || |
| 7066 |
op == glslang::EOpMaxInvocationsInclusiveScanNonUniform || |
| 7067 |
op == glslang::EOpMaxInvocationsExclusiveScanNonUniform) { |
| 7068 |
if (isFloat) |
| 7069 |
opCode = spv::OpGroupFMaxNonUniformAMD; |
| 7070 |
else { |
| 7071 |
if (isUnsigned) |
| 7072 |
opCode = spv::OpGroupUMaxNonUniformAMD; |
| 7073 |
else |
| 7074 |
opCode = spv::OpGroupSMaxNonUniformAMD; |
| 7075 |
} |
| 7076 |
} |
| 7077 |
else { |
| 7078 |
if (isFloat) |
| 7079 |
opCode = spv::OpGroupFAddNonUniformAMD; |
| 7080 |
else |
| 7081 |
opCode = spv::OpGroupIAddNonUniformAMD; |
| 7082 |
} |
| 7083 |
|
| 7084 |
if (builder.isVectorType(typeId)) |
| 7085 |
return CreateInvocationsVectorOperation(opCode, groupOperation, typeId, operands); |
| 7086 |
|
| 7087 |
break; |
| 7088 |
default: |
| 7089 |
logger->missingFunctionality("invocation operation"); |
| 7090 |
return spv::NoResult; |
| 7091 |
} |
| 7092 |
|
| 7093 |
assert(opCode != spv::OpNop); |
| 7094 |
return builder.createOp(opCode, typeId, spvGroupOperands); |
| 7095 |
} |
| 7096 |
|
| 7097 |
// Create group invocation operations on a vector |
| 7098 |
spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation, |
| 7099 |
spv::Id typeId, std::vector<spv::Id>& operands) |
| 7100 |
{ |
| 7101 |
assert(op == spv::OpGroupFMin || op == spv::OpGroupUMin || op == spv::OpGroupSMin || |
| 7102 |
op == spv::OpGroupFMax || op == spv::OpGroupUMax || op == spv::OpGroupSMax || |
| 7103 |
op == spv::OpGroupFAdd || op == spv::OpGroupIAdd || op == spv::OpGroupBroadcast || |
| 7104 |
op == spv::OpSubgroupReadInvocationKHR || |
| 7105 |
op == spv::OpGroupFMinNonUniformAMD || op == spv::OpGroupUMinNonUniformAMD || |
| 7106 |
op == spv::OpGroupSMinNonUniformAMD || |
| 7107 |
op == spv::OpGroupFMaxNonUniformAMD || op == spv::OpGroupUMaxNonUniformAMD || |
| 7108 |
op == spv::OpGroupSMaxNonUniformAMD || |
| 7109 |
op == spv::OpGroupFAddNonUniformAMD || op == spv::OpGroupIAddNonUniformAMD); |
| 7110 |
|
| 7111 |
// Handle group invocation operations scalar by scalar. |
| 7112 |
// The result type is the same type as the original type. |
| 7113 |
// The algorithm is to: |
| 7114 |
// - break the vector into scalars |
| 7115 |
// - apply the operation to each scalar |
| 7116 |
// - make a vector out the scalar results |
| 7117 |
|
| 7118 |
// get the types sorted out |
| 7119 |
int numComponents = builder.getNumComponents(operands[0]); |
| 7120 |
spv::Id scalarType = builder.getScalarTypeId(builder.getTypeId(operands[0])); |
| 7121 |
std::vector<spv::Id> results; |
| 7122 |
|
| 7123 |
// do each scalar op |
| 7124 |
for (int comp = 0; comp < numComponents; ++comp) { |
| 7125 |
std::vector<unsigned int> indexes; |
| 7126 |
indexes.push_back(comp); |
| 7127 |
spv::IdImmediate scalar = { true, builder.createCompositeExtract(operands[0], scalarType, indexes) }; |
| 7128 |
std::vector<spv::IdImmediate> spvGroupOperands; |
| 7129 |
if (op == spv::OpSubgroupReadInvocationKHR) { |
| 7130 |
spvGroupOperands.push_back(scalar); |
| 7131 |
spv::IdImmediate operand = { true, operands[1] }; |
| 7132 |
spvGroupOperands.push_back(operand); |
| 7133 |
} else if (op == spv::OpGroupBroadcast) { |
| 7134 |
spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7135 |
spvGroupOperands.push_back(scope); |
| 7136 |
spvGroupOperands.push_back(scalar); |
| 7137 |
spv::IdImmediate operand = { true, operands[1] }; |
| 7138 |
spvGroupOperands.push_back(operand); |
| 7139 |
} else { |
| 7140 |
spv::IdImmediate scope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7141 |
spvGroupOperands.push_back(scope); |
| 7142 |
spv::IdImmediate groupOp = { false, (unsigned)groupOperation }; |
| 7143 |
spvGroupOperands.push_back(groupOp); |
| 7144 |
spvGroupOperands.push_back(scalar); |
| 7145 |
} |
| 7146 |
|
| 7147 |
results.push_back(builder.createOp(op, scalarType, spvGroupOperands)); |
| 7148 |
} |
| 7149 |
|
| 7150 |
// put the pieces together |
| 7151 |
return builder.createCompositeConstruct(typeId, results); |
| 7152 |
} |
| 7153 |
|
| 7154 |
// Create subgroup invocation operations. |
| 7155 |
spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId, |
| 7156 |
std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
| 7157 |
{ |
| 7158 |
// Add the required capabilities. |
| 7159 |
switch (op) { |
| 7160 |
case glslang::EOpSubgroupElect: |
| 7161 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7162 |
break; |
| 7163 |
case glslang::EOpSubgroupAll: |
| 7164 |
case glslang::EOpSubgroupAny: |
| 7165 |
case glslang::EOpSubgroupAllEqual: |
| 7166 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7167 |
builder.addCapability(spv::CapabilityGroupNonUniformVote); |
| 7168 |
break; |
| 7169 |
case glslang::EOpSubgroupBroadcast: |
| 7170 |
case glslang::EOpSubgroupBroadcastFirst: |
| 7171 |
case glslang::EOpSubgroupBallot: |
| 7172 |
case glslang::EOpSubgroupInverseBallot: |
| 7173 |
case glslang::EOpSubgroupBallotBitExtract: |
| 7174 |
case glslang::EOpSubgroupBallotBitCount: |
| 7175 |
case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7176 |
case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7177 |
case glslang::EOpSubgroupBallotFindLSB: |
| 7178 |
case glslang::EOpSubgroupBallotFindMSB: |
| 7179 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7180 |
builder.addCapability(spv::CapabilityGroupNonUniformBallot); |
| 7181 |
break; |
| 7182 |
case glslang::EOpSubgroupShuffle: |
| 7183 |
case glslang::EOpSubgroupShuffleXor: |
| 7184 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7185 |
builder.addCapability(spv::CapabilityGroupNonUniformShuffle); |
| 7186 |
break; |
| 7187 |
case glslang::EOpSubgroupShuffleUp: |
| 7188 |
case glslang::EOpSubgroupShuffleDown: |
| 7189 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7190 |
builder.addCapability(spv::CapabilityGroupNonUniformShuffleRelative); |
| 7191 |
break; |
| 7192 |
case glslang::EOpSubgroupAdd: |
| 7193 |
case glslang::EOpSubgroupMul: |
| 7194 |
case glslang::EOpSubgroupMin: |
| 7195 |
case glslang::EOpSubgroupMax: |
| 7196 |
case glslang::EOpSubgroupAnd: |
| 7197 |
case glslang::EOpSubgroupOr: |
| 7198 |
case glslang::EOpSubgroupXor: |
| 7199 |
case glslang::EOpSubgroupInclusiveAdd: |
| 7200 |
case glslang::EOpSubgroupInclusiveMul: |
| 7201 |
case glslang::EOpSubgroupInclusiveMin: |
| 7202 |
case glslang::EOpSubgroupInclusiveMax: |
| 7203 |
case glslang::EOpSubgroupInclusiveAnd: |
| 7204 |
case glslang::EOpSubgroupInclusiveOr: |
| 7205 |
case glslang::EOpSubgroupInclusiveXor: |
| 7206 |
case glslang::EOpSubgroupExclusiveAdd: |
| 7207 |
case glslang::EOpSubgroupExclusiveMul: |
| 7208 |
case glslang::EOpSubgroupExclusiveMin: |
| 7209 |
case glslang::EOpSubgroupExclusiveMax: |
| 7210 |
case glslang::EOpSubgroupExclusiveAnd: |
| 7211 |
case glslang::EOpSubgroupExclusiveOr: |
| 7212 |
case glslang::EOpSubgroupExclusiveXor: |
| 7213 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7214 |
builder.addCapability(spv::CapabilityGroupNonUniformArithmetic); |
| 7215 |
break; |
| 7216 |
case glslang::EOpSubgroupClusteredAdd: |
| 7217 |
case glslang::EOpSubgroupClusteredMul: |
| 7218 |
case glslang::EOpSubgroupClusteredMin: |
| 7219 |
case glslang::EOpSubgroupClusteredMax: |
| 7220 |
case glslang::EOpSubgroupClusteredAnd: |
| 7221 |
case glslang::EOpSubgroupClusteredOr: |
| 7222 |
case glslang::EOpSubgroupClusteredXor: |
| 7223 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7224 |
builder.addCapability(spv::CapabilityGroupNonUniformClustered); |
| 7225 |
break; |
| 7226 |
case glslang::EOpSubgroupQuadBroadcast: |
| 7227 |
case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7228 |
case glslang::EOpSubgroupQuadSwapVertical: |
| 7229 |
case glslang::EOpSubgroupQuadSwapDiagonal: |
| 7230 |
builder.addCapability(spv::CapabilityGroupNonUniform); |
| 7231 |
builder.addCapability(spv::CapabilityGroupNonUniformQuad); |
| 7232 |
break; |
| 7233 |
case glslang::EOpSubgroupPartitionedAdd: |
| 7234 |
case glslang::EOpSubgroupPartitionedMul: |
| 7235 |
case glslang::EOpSubgroupPartitionedMin: |
| 7236 |
case glslang::EOpSubgroupPartitionedMax: |
| 7237 |
case glslang::EOpSubgroupPartitionedAnd: |
| 7238 |
case glslang::EOpSubgroupPartitionedOr: |
| 7239 |
case glslang::EOpSubgroupPartitionedXor: |
| 7240 |
case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7241 |
case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7242 |
case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7243 |
case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7244 |
case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7245 |
case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7246 |
case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7247 |
case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7248 |
case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7249 |
case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7250 |
case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7251 |
case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7252 |
case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7253 |
case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 7254 |
builder.addExtension(spv::E_SPV_NV_shader_subgroup_partitioned); |
| 7255 |
builder.addCapability(spv::CapabilityGroupNonUniformPartitionedNV); |
| 7256 |
break; |
| 7257 |
default: assert(0 && "Unhandled subgroup operation!"); |
| 7258 |
} |
| 7259 |
|
| 7260 |
|
| 7261 |
const bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7262 |
const bool isFloat = isTypeFloat(typeProxy); |
| 7263 |
const bool isBool = typeProxy == glslang::EbtBool; |
| 7264 |
|
| 7265 |
spv::Op opCode = spv::OpNop; |
| 7266 |
|
| 7267 |
// Figure out which opcode to use. |
| 7268 |
switch (op) { |
| 7269 |
case glslang::EOpSubgroupElect: opCode = spv::OpGroupNonUniformElect; break; |
| 7270 |
case glslang::EOpSubgroupAll: opCode = spv::OpGroupNonUniformAll; break; |
| 7271 |
case glslang::EOpSubgroupAny: opCode = spv::OpGroupNonUniformAny; break; |
| 7272 |
case glslang::EOpSubgroupAllEqual: opCode = spv::OpGroupNonUniformAllEqual; break; |
| 7273 |
case glslang::EOpSubgroupBroadcast: opCode = spv::OpGroupNonUniformBroadcast; break; |
| 7274 |
case glslang::EOpSubgroupBroadcastFirst: opCode = spv::OpGroupNonUniformBroadcastFirst; break; |
| 7275 |
case glslang::EOpSubgroupBallot: opCode = spv::OpGroupNonUniformBallot; break; |
| 7276 |
case glslang::EOpSubgroupInverseBallot: opCode = spv::OpGroupNonUniformInverseBallot; break; |
| 7277 |
case glslang::EOpSubgroupBallotBitExtract: opCode = spv::OpGroupNonUniformBallotBitExtract; break; |
| 7278 |
case glslang::EOpSubgroupBallotBitCount: |
| 7279 |
case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7280 |
case glslang::EOpSubgroupBallotExclusiveBitCount: opCode = spv::OpGroupNonUniformBallotBitCount; break; |
| 7281 |
case glslang::EOpSubgroupBallotFindLSB: opCode = spv::OpGroupNonUniformBallotFindLSB; break; |
| 7282 |
case glslang::EOpSubgroupBallotFindMSB: opCode = spv::OpGroupNonUniformBallotFindMSB; break; |
| 7283 |
case glslang::EOpSubgroupShuffle: opCode = spv::OpGroupNonUniformShuffle; break; |
| 7284 |
case glslang::EOpSubgroupShuffleXor: opCode = spv::OpGroupNonUniformShuffleXor; break; |
| 7285 |
case glslang::EOpSubgroupShuffleUp: opCode = spv::OpGroupNonUniformShuffleUp; break; |
| 7286 |
case glslang::EOpSubgroupShuffleDown: opCode = spv::OpGroupNonUniformShuffleDown; break; |
| 7287 |
case glslang::EOpSubgroupAdd: |
| 7288 |
case glslang::EOpSubgroupInclusiveAdd: |
| 7289 |
case glslang::EOpSubgroupExclusiveAdd: |
| 7290 |
case glslang::EOpSubgroupClusteredAdd: |
| 7291 |
case glslang::EOpSubgroupPartitionedAdd: |
| 7292 |
case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7293 |
case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7294 |
if (isFloat) { |
| 7295 |
opCode = spv::OpGroupNonUniformFAdd; |
| 7296 |
} else { |
| 7297 |
opCode = spv::OpGroupNonUniformIAdd; |
| 7298 |
} |
| 7299 |
break; |
| 7300 |
case glslang::EOpSubgroupMul: |
| 7301 |
case glslang::EOpSubgroupInclusiveMul: |
| 7302 |
case glslang::EOpSubgroupExclusiveMul: |
| 7303 |
case glslang::EOpSubgroupClusteredMul: |
| 7304 |
case glslang::EOpSubgroupPartitionedMul: |
| 7305 |
case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7306 |
case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7307 |
if (isFloat) { |
| 7308 |
opCode = spv::OpGroupNonUniformFMul; |
| 7309 |
} else { |
| 7310 |
opCode = spv::OpGroupNonUniformIMul; |
| 7311 |
} |
| 7312 |
break; |
| 7313 |
case glslang::EOpSubgroupMin: |
| 7314 |
case glslang::EOpSubgroupInclusiveMin: |
| 7315 |
case glslang::EOpSubgroupExclusiveMin: |
| 7316 |
case glslang::EOpSubgroupClusteredMin: |
| 7317 |
case glslang::EOpSubgroupPartitionedMin: |
| 7318 |
case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7319 |
case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7320 |
if (isFloat) { |
| 7321 |
opCode = spv::OpGroupNonUniformFMin; |
| 7322 |
} else if (isUnsigned) { |
| 7323 |
opCode = spv::OpGroupNonUniformUMin; |
| 7324 |
} else { |
| 7325 |
opCode = spv::OpGroupNonUniformSMin; |
| 7326 |
} |
| 7327 |
break; |
| 7328 |
case glslang::EOpSubgroupMax: |
| 7329 |
case glslang::EOpSubgroupInclusiveMax: |
| 7330 |
case glslang::EOpSubgroupExclusiveMax: |
| 7331 |
case glslang::EOpSubgroupClusteredMax: |
| 7332 |
case glslang::EOpSubgroupPartitionedMax: |
| 7333 |
case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7334 |
case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7335 |
if (isFloat) { |
| 7336 |
opCode = spv::OpGroupNonUniformFMax; |
| 7337 |
} else if (isUnsigned) { |
| 7338 |
opCode = spv::OpGroupNonUniformUMax; |
| 7339 |
} else { |
| 7340 |
opCode = spv::OpGroupNonUniformSMax; |
| 7341 |
} |
| 7342 |
break; |
| 7343 |
case glslang::EOpSubgroupAnd: |
| 7344 |
case glslang::EOpSubgroupInclusiveAnd: |
| 7345 |
case glslang::EOpSubgroupExclusiveAnd: |
| 7346 |
case glslang::EOpSubgroupClusteredAnd: |
| 7347 |
case glslang::EOpSubgroupPartitionedAnd: |
| 7348 |
case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7349 |
case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7350 |
if (isBool) { |
| 7351 |
opCode = spv::OpGroupNonUniformLogicalAnd; |
| 7352 |
} else { |
| 7353 |
opCode = spv::OpGroupNonUniformBitwiseAnd; |
| 7354 |
} |
| 7355 |
break; |
| 7356 |
case glslang::EOpSubgroupOr: |
| 7357 |
case glslang::EOpSubgroupInclusiveOr: |
| 7358 |
case glslang::EOpSubgroupExclusiveOr: |
| 7359 |
case glslang::EOpSubgroupClusteredOr: |
| 7360 |
case glslang::EOpSubgroupPartitionedOr: |
| 7361 |
case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7362 |
case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7363 |
if (isBool) { |
| 7364 |
opCode = spv::OpGroupNonUniformLogicalOr; |
| 7365 |
} else { |
| 7366 |
opCode = spv::OpGroupNonUniformBitwiseOr; |
| 7367 |
} |
| 7368 |
break; |
| 7369 |
case glslang::EOpSubgroupXor: |
| 7370 |
case glslang::EOpSubgroupInclusiveXor: |
| 7371 |
case glslang::EOpSubgroupExclusiveXor: |
| 7372 |
case glslang::EOpSubgroupClusteredXor: |
| 7373 |
case glslang::EOpSubgroupPartitionedXor: |
| 7374 |
case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7375 |
case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 7376 |
if (isBool) { |
| 7377 |
opCode = spv::OpGroupNonUniformLogicalXor; |
| 7378 |
} else { |
| 7379 |
opCode = spv::OpGroupNonUniformBitwiseXor; |
| 7380 |
} |
| 7381 |
break; |
| 7382 |
case glslang::EOpSubgroupQuadBroadcast: opCode = spv::OpGroupNonUniformQuadBroadcast; break; |
| 7383 |
case glslang::EOpSubgroupQuadSwapHorizontal: |
| 7384 |
case glslang::EOpSubgroupQuadSwapVertical: |
| 7385 |
case glslang::EOpSubgroupQuadSwapDiagonal: opCode = spv::OpGroupNonUniformQuadSwap; break; |
| 7386 |
default: assert(0 && "Unhandled subgroup operation!"); |
| 7387 |
} |
| 7388 |
|
| 7389 |
// get the right Group Operation |
| 7390 |
spv::GroupOperation groupOperation = spv::GroupOperationMax; |
| 7391 |
switch (op) { |
| 7392 |
default: |
| 7393 |
break; |
| 7394 |
case glslang::EOpSubgroupBallotBitCount: |
| 7395 |
case glslang::EOpSubgroupAdd: |
| 7396 |
case glslang::EOpSubgroupMul: |
| 7397 |
case glslang::EOpSubgroupMin: |
| 7398 |
case glslang::EOpSubgroupMax: |
| 7399 |
case glslang::EOpSubgroupAnd: |
| 7400 |
case glslang::EOpSubgroupOr: |
| 7401 |
case glslang::EOpSubgroupXor: |
| 7402 |
groupOperation = spv::GroupOperationReduce; |
| 7403 |
break; |
| 7404 |
case glslang::EOpSubgroupBallotInclusiveBitCount: |
| 7405 |
case glslang::EOpSubgroupInclusiveAdd: |
| 7406 |
case glslang::EOpSubgroupInclusiveMul: |
| 7407 |
case glslang::EOpSubgroupInclusiveMin: |
| 7408 |
case glslang::EOpSubgroupInclusiveMax: |
| 7409 |
case glslang::EOpSubgroupInclusiveAnd: |
| 7410 |
case glslang::EOpSubgroupInclusiveOr: |
| 7411 |
case glslang::EOpSubgroupInclusiveXor: |
| 7412 |
groupOperation = spv::GroupOperationInclusiveScan; |
| 7413 |
break; |
| 7414 |
case glslang::EOpSubgroupBallotExclusiveBitCount: |
| 7415 |
case glslang::EOpSubgroupExclusiveAdd: |
| 7416 |
case glslang::EOpSubgroupExclusiveMul: |
| 7417 |
case glslang::EOpSubgroupExclusiveMin: |
| 7418 |
case glslang::EOpSubgroupExclusiveMax: |
| 7419 |
case glslang::EOpSubgroupExclusiveAnd: |
| 7420 |
case glslang::EOpSubgroupExclusiveOr: |
| 7421 |
case glslang::EOpSubgroupExclusiveXor: |
| 7422 |
groupOperation = spv::GroupOperationExclusiveScan; |
| 7423 |
break; |
| 7424 |
case glslang::EOpSubgroupClusteredAdd: |
| 7425 |
case glslang::EOpSubgroupClusteredMul: |
| 7426 |
case glslang::EOpSubgroupClusteredMin: |
| 7427 |
case glslang::EOpSubgroupClusteredMax: |
| 7428 |
case glslang::EOpSubgroupClusteredAnd: |
| 7429 |
case glslang::EOpSubgroupClusteredOr: |
| 7430 |
case glslang::EOpSubgroupClusteredXor: |
| 7431 |
groupOperation = spv::GroupOperationClusteredReduce; |
| 7432 |
break; |
| 7433 |
case glslang::EOpSubgroupPartitionedAdd: |
| 7434 |
case glslang::EOpSubgroupPartitionedMul: |
| 7435 |
case glslang::EOpSubgroupPartitionedMin: |
| 7436 |
case glslang::EOpSubgroupPartitionedMax: |
| 7437 |
case glslang::EOpSubgroupPartitionedAnd: |
| 7438 |
case glslang::EOpSubgroupPartitionedOr: |
| 7439 |
case glslang::EOpSubgroupPartitionedXor: |
| 7440 |
groupOperation = spv::GroupOperationPartitionedReduceNV; |
| 7441 |
break; |
| 7442 |
case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7443 |
case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7444 |
case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7445 |
case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7446 |
case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7447 |
case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7448 |
case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7449 |
groupOperation = spv::GroupOperationPartitionedInclusiveScanNV; |
| 7450 |
break; |
| 7451 |
case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7452 |
case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7453 |
case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7454 |
case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7455 |
case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7456 |
case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7457 |
case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 7458 |
groupOperation = spv::GroupOperationPartitionedExclusiveScanNV; |
| 7459 |
break; |
| 7460 |
} |
| 7461 |
|
| 7462 |
// build the instruction |
| 7463 |
std::vector<spv::IdImmediate> spvGroupOperands; |
| 7464 |
|
| 7465 |
// Every operation begins with the Execution Scope operand. |
| 7466 |
spv::IdImmediate executionScope = { true, builder.makeUintConstant(spv::ScopeSubgroup) }; |
| 7467 |
spvGroupOperands.push_back(executionScope); |
| 7468 |
|
| 7469 |
// Next, for all operations that use a Group Operation, push that as an operand. |
| 7470 |
if (groupOperation != spv::GroupOperationMax) { |
| 7471 |
spv::IdImmediate groupOperand = { false, (unsigned)groupOperation }; |
| 7472 |
spvGroupOperands.push_back(groupOperand); |
| 7473 |
} |
| 7474 |
|
| 7475 |
// Push back the operands next. |
| 7476 |
for (auto opIt = operands.cbegin(); opIt != operands.cend(); ++opIt) { |
| 7477 |
spv::IdImmediate operand = { true, *opIt }; |
| 7478 |
spvGroupOperands.push_back(operand); |
| 7479 |
} |
| 7480 |
|
| 7481 |
// Some opcodes have additional operands. |
| 7482 |
spv::Id directionId = spv::NoResult; |
| 7483 |
switch (op) { |
| 7484 |
default: break; |
| 7485 |
case glslang::EOpSubgroupQuadSwapHorizontal: directionId = builder.makeUintConstant(0); break; |
| 7486 |
case glslang::EOpSubgroupQuadSwapVertical: directionId = builder.makeUintConstant(1); break; |
| 7487 |
case glslang::EOpSubgroupQuadSwapDiagonal: directionId = builder.makeUintConstant(2); break; |
| 7488 |
} |
| 7489 |
if (directionId != spv::NoResult) { |
| 7490 |
spv::IdImmediate direction = { true, directionId }; |
| 7491 |
spvGroupOperands.push_back(direction); |
| 7492 |
} |
| 7493 |
|
| 7494 |
return builder.createOp(opCode, typeId, spvGroupOperands); |
| 7495 |
} |
| 7496 |
|
| 7497 |
spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision, |
| 7498 |
spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy) |
| 7499 |
{ |
| 7500 |
bool isUnsigned = isTypeUnsignedInt(typeProxy); |
| 7501 |
bool isFloat = isTypeFloat(typeProxy); |
| 7502 |
|
| 7503 |
spv::Op opCode = spv::OpNop; |
| 7504 |
int extBuiltins = -1; |
| 7505 |
int libCall = -1; |
| 7506 |
size_t consumedOperands = operands.size(); |
| 7507 |
spv::Id typeId0 = 0; |
| 7508 |
if (consumedOperands > 0) |
| 7509 |
typeId0 = builder.getTypeId(operands[0]); |
| 7510 |
spv::Id typeId1 = 0; |
| 7511 |
if (consumedOperands > 1) |
| 7512 |
typeId1 = builder.getTypeId(operands[1]); |
| 7513 |
spv::Id frexpIntType = 0; |
| 7514 |
|
| 7515 |
switch (op) { |
| 7516 |
case glslang::EOpMin: |
| 7517 |
if (isFloat) |
| 7518 |
libCall = nanMinMaxClamp ? spv::GLSLstd450NMin : spv::GLSLstd450FMin; |
| 7519 |
else if (isUnsigned) |
| 7520 |
libCall = spv::GLSLstd450UMin; |
| 7521 |
else |
| 7522 |
libCall = spv::GLSLstd450SMin; |
| 7523 |
builder.promoteScalar(precision, operands.front(), operands.back()); |
| 7524 |
break; |
| 7525 |
case glslang::EOpModf: |
| 7526 |
libCall = spv::GLSLstd450Modf; |
| 7527 |
break; |
| 7528 |
case glslang::EOpMax: |
| 7529 |
if (isFloat) |
| 7530 |
libCall = nanMinMaxClamp ? spv::GLSLstd450NMax : spv::GLSLstd450FMax; |
| 7531 |
else if (isUnsigned) |
| 7532 |
libCall = spv::GLSLstd450UMax; |
| 7533 |
else |
| 7534 |
libCall = spv::GLSLstd450SMax; |
| 7535 |
builder.promoteScalar(precision, operands.front(), operands.back()); |
| 7536 |
break; |
| 7537 |
case glslang::EOpPow: |
| 7538 |
libCall = spv::GLSLstd450Pow; |
| 7539 |
break; |
| 7540 |
case glslang::EOpDot: |
| 7541 |
opCode = spv::OpDot; |
| 7542 |
break; |
| 7543 |
case glslang::EOpAtan: |
| 7544 |
libCall = spv::GLSLstd450Atan2; |
| 7545 |
break; |
| 7546 |
|
| 7547 |
case glslang::EOpClamp: |
| 7548 |
if (isFloat) |
| 7549 |
libCall = nanMinMaxClamp ? spv::GLSLstd450NClamp : spv::GLSLstd450FClamp; |
| 7550 |
else if (isUnsigned) |
| 7551 |
libCall = spv::GLSLstd450UClamp; |
| 7552 |
else |
| 7553 |
libCall = spv::GLSLstd450SClamp; |
| 7554 |
builder.promoteScalar(precision, operands.front(), operands[1]); |
| 7555 |
builder.promoteScalar(precision, operands.front(), operands[2]); |
| 7556 |
break; |
| 7557 |
case glslang::EOpMix: |
| 7558 |
if (! builder.isBoolType(builder.getScalarTypeId(builder.getTypeId(operands.back())))) { |
| 7559 |
assert(isFloat); |
| 7560 |
libCall = spv::GLSLstd450FMix; |
| 7561 |
} else { |
| 7562 |
opCode = spv::OpSelect; |
| 7563 |
std::swap(operands.front(), operands.back()); |
| 7564 |
} |
| 7565 |
builder.promoteScalar(precision, operands.front(), operands.back()); |
| 7566 |
break; |
| 7567 |
case glslang::EOpStep: |
| 7568 |
libCall = spv::GLSLstd450Step; |
| 7569 |
builder.promoteScalar(precision, operands.front(), operands.back()); |
| 7570 |
break; |
| 7571 |
case glslang::EOpSmoothStep: |
| 7572 |
libCall = spv::GLSLstd450SmoothStep; |
| 7573 |
builder.promoteScalar(precision, operands[0], operands[2]); |
| 7574 |
builder.promoteScalar(precision, operands[1], operands[2]); |
| 7575 |
break; |
| 7576 |
|
| 7577 |
case glslang::EOpDistance: |
| 7578 |
libCall = spv::GLSLstd450Distance; |
| 7579 |
break; |
| 7580 |
case glslang::EOpCross: |
| 7581 |
libCall = spv::GLSLstd450Cross; |
| 7582 |
break; |
| 7583 |
case glslang::EOpFaceForward: |
| 7584 |
libCall = spv::GLSLstd450FaceForward; |
| 7585 |
break; |
| 7586 |
case glslang::EOpReflect: |
| 7587 |
libCall = spv::GLSLstd450Reflect; |
| 7588 |
break; |
| 7589 |
case glslang::EOpRefract: |
| 7590 |
libCall = spv::GLSLstd450Refract; |
| 7591 |
break; |
| 7592 |
case glslang::EOpBarrier: |
| 7593 |
{ |
| 7594 |
// This is for the extended controlBarrier function, with four operands. |
| 7595 |
// The unextended barrier() goes through createNoArgOperation. |
| 7596 |
assert(operands.size() == 4); |
| 7597 |
unsigned int executionScope = builder.getConstantScalar(operands[0]); |
| 7598 |
unsigned int memoryScope = builder.getConstantScalar(operands[1]); |
| 7599 |
unsigned int semantics = builder.getConstantScalar(operands[2]) | builder.getConstantScalar(operands[3]); |
| 7600 |
builder.createControlBarrier((spv::Scope)executionScope, (spv::Scope)memoryScope, |
| 7601 |
(spv::MemorySemanticsMask)semantics); |
| 7602 |
if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7603 |
spv::MemorySemanticsMakeVisibleKHRMask | |
| 7604 |
spv::MemorySemanticsOutputMemoryKHRMask | |
| 7605 |
spv::MemorySemanticsVolatileMask)) { |
| 7606 |
builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7607 |
} |
| 7608 |
if (glslangIntermediate->usingVulkanMemoryModel() && (executionScope == spv::ScopeDevice || |
| 7609 |
memoryScope == spv::ScopeDevice)) { |
| 7610 |
builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7611 |
} |
| 7612 |
return 0; |
| 7613 |
} |
| 7614 |
break; |
| 7615 |
case glslang::EOpMemoryBarrier: |
| 7616 |
{ |
| 7617 |
// This is for the extended memoryBarrier function, with three operands. |
| 7618 |
// The unextended memoryBarrier() goes through createNoArgOperation. |
| 7619 |
assert(operands.size() == 3); |
| 7620 |
unsigned int memoryScope = builder.getConstantScalar(operands[0]); |
| 7621 |
unsigned int semantics = builder.getConstantScalar(operands[1]) | builder.getConstantScalar(operands[2]); |
| 7622 |
builder.createMemoryBarrier((spv::Scope)memoryScope, (spv::MemorySemanticsMask)semantics); |
| 7623 |
if (semantics & (spv::MemorySemanticsMakeAvailableKHRMask | |
| 7624 |
spv::MemorySemanticsMakeVisibleKHRMask | |
| 7625 |
spv::MemorySemanticsOutputMemoryKHRMask | |
| 7626 |
spv::MemorySemanticsVolatileMask)) { |
| 7627 |
builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7628 |
} |
| 7629 |
if (glslangIntermediate->usingVulkanMemoryModel() && memoryScope == spv::ScopeDevice) { |
| 7630 |
builder.addCapability(spv::CapabilityVulkanMemoryModelDeviceScopeKHR); |
| 7631 |
} |
| 7632 |
return 0; |
| 7633 |
} |
| 7634 |
break; |
| 7635 |
|
| 7636 |
#ifndef GLSLANG_WEB |
| 7637 |
case glslang::EOpInterpolateAtSample: |
| 7638 |
if (typeProxy == glslang::EbtFloat16) |
| 7639 |
builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 7640 |
libCall = spv::GLSLstd450InterpolateAtSample; |
| 7641 |
break; |
| 7642 |
case glslang::EOpInterpolateAtOffset: |
| 7643 |
if (typeProxy == glslang::EbtFloat16) |
| 7644 |
builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 7645 |
libCall = spv::GLSLstd450InterpolateAtOffset; |
| 7646 |
break; |
| 7647 |
case glslang::EOpAddCarry: |
| 7648 |
opCode = spv::OpIAddCarry; |
| 7649 |
typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7650 |
consumedOperands = 2; |
| 7651 |
break; |
| 7652 |
case glslang::EOpSubBorrow: |
| 7653 |
opCode = spv::OpISubBorrow; |
| 7654 |
typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7655 |
consumedOperands = 2; |
| 7656 |
break; |
| 7657 |
case glslang::EOpUMulExtended: |
| 7658 |
opCode = spv::OpUMulExtended; |
| 7659 |
typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7660 |
consumedOperands = 2; |
| 7661 |
break; |
| 7662 |
case glslang::EOpIMulExtended: |
| 7663 |
opCode = spv::OpSMulExtended; |
| 7664 |
typeId = builder.makeStructResultType(typeId0, typeId0); |
| 7665 |
consumedOperands = 2; |
| 7666 |
break; |
| 7667 |
case glslang::EOpBitfieldExtract: |
| 7668 |
if (isUnsigned) |
| 7669 |
opCode = spv::OpBitFieldUExtract; |
| 7670 |
else |
| 7671 |
opCode = spv::OpBitFieldSExtract; |
| 7672 |
break; |
| 7673 |
case glslang::EOpBitfieldInsert: |
| 7674 |
opCode = spv::OpBitFieldInsert; |
| 7675 |
break; |
| 7676 |
|
| 7677 |
case glslang::EOpFma: |
| 7678 |
libCall = spv::GLSLstd450Fma; |
| 7679 |
break; |
| 7680 |
case glslang::EOpFrexp: |
| 7681 |
{ |
| 7682 |
libCall = spv::GLSLstd450FrexpStruct; |
| 7683 |
assert(builder.isPointerType(typeId1)); |
| 7684 |
typeId1 = builder.getContainedTypeId(typeId1); |
| 7685 |
int width = builder.getScalarTypeWidth(typeId1); |
| 7686 |
if (width == 16) |
| 7687 |
// Using 16-bit exp operand, enable extension SPV_AMD_gpu_shader_int16 |
| 7688 |
builder.addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| 7689 |
if (builder.getNumComponents(operands[0]) == 1) |
| 7690 |
frexpIntType = builder.makeIntegerType(width, true); |
| 7691 |
else |
| 7692 |
frexpIntType = builder.makeVectorType(builder.makeIntegerType(width, true), |
| 7693 |
builder.getNumComponents(operands[0])); |
| 7694 |
typeId = builder.makeStructResultType(typeId0, frexpIntType); |
| 7695 |
consumedOperands = 1; |
| 7696 |
} |
| 7697 |
break; |
| 7698 |
case glslang::EOpLdexp: |
| 7699 |
libCall = spv::GLSLstd450Ldexp; |
| 7700 |
break; |
| 7701 |
|
| 7702 |
case glslang::EOpReadInvocation: |
| 7703 |
return createInvocationsOperation(op, typeId, operands, typeProxy); |
| 7704 |
|
| 7705 |
case glslang::EOpSubgroupBroadcast: |
| 7706 |
case glslang::EOpSubgroupBallotBitExtract: |
| 7707 |
case glslang::EOpSubgroupShuffle: |
| 7708 |
case glslang::EOpSubgroupShuffleXor: |
| 7709 |
case glslang::EOpSubgroupShuffleUp: |
| 7710 |
case glslang::EOpSubgroupShuffleDown: |
| 7711 |
case glslang::EOpSubgroupClusteredAdd: |
| 7712 |
case glslang::EOpSubgroupClusteredMul: |
| 7713 |
case glslang::EOpSubgroupClusteredMin: |
| 7714 |
case glslang::EOpSubgroupClusteredMax: |
| 7715 |
case glslang::EOpSubgroupClusteredAnd: |
| 7716 |
case glslang::EOpSubgroupClusteredOr: |
| 7717 |
case glslang::EOpSubgroupClusteredXor: |
| 7718 |
case glslang::EOpSubgroupQuadBroadcast: |
| 7719 |
case glslang::EOpSubgroupPartitionedAdd: |
| 7720 |
case glslang::EOpSubgroupPartitionedMul: |
| 7721 |
case glslang::EOpSubgroupPartitionedMin: |
| 7722 |
case glslang::EOpSubgroupPartitionedMax: |
| 7723 |
case glslang::EOpSubgroupPartitionedAnd: |
| 7724 |
case glslang::EOpSubgroupPartitionedOr: |
| 7725 |
case glslang::EOpSubgroupPartitionedXor: |
| 7726 |
case glslang::EOpSubgroupPartitionedInclusiveAdd: |
| 7727 |
case glslang::EOpSubgroupPartitionedInclusiveMul: |
| 7728 |
case glslang::EOpSubgroupPartitionedInclusiveMin: |
| 7729 |
case glslang::EOpSubgroupPartitionedInclusiveMax: |
| 7730 |
case glslang::EOpSubgroupPartitionedInclusiveAnd: |
| 7731 |
case glslang::EOpSubgroupPartitionedInclusiveOr: |
| 7732 |
case glslang::EOpSubgroupPartitionedInclusiveXor: |
| 7733 |
case glslang::EOpSubgroupPartitionedExclusiveAdd: |
| 7734 |
case glslang::EOpSubgroupPartitionedExclusiveMul: |
| 7735 |
case glslang::EOpSubgroupPartitionedExclusiveMin: |
| 7736 |
case glslang::EOpSubgroupPartitionedExclusiveMax: |
| 7737 |
case glslang::EOpSubgroupPartitionedExclusiveAnd: |
| 7738 |
case glslang::EOpSubgroupPartitionedExclusiveOr: |
| 7739 |
case glslang::EOpSubgroupPartitionedExclusiveXor: |
| 7740 |
return createSubgroupOperation(op, typeId, operands, typeProxy); |
| 7741 |
|
| 7742 |
case glslang::EOpSwizzleInvocations: |
| 7743 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7744 |
libCall = spv::SwizzleInvocationsAMD; |
| 7745 |
break; |
| 7746 |
case glslang::EOpSwizzleInvocationsMasked: |
| 7747 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7748 |
libCall = spv::SwizzleInvocationsMaskedAMD; |
| 7749 |
break; |
| 7750 |
case glslang::EOpWriteInvocation: |
| 7751 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_ballot); |
| 7752 |
libCall = spv::WriteInvocationAMD; |
| 7753 |
break; |
| 7754 |
|
| 7755 |
case glslang::EOpMin3: |
| 7756 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7757 |
if (isFloat) |
| 7758 |
libCall = spv::FMin3AMD; |
| 7759 |
else { |
| 7760 |
if (isUnsigned) |
| 7761 |
libCall = spv::UMin3AMD; |
| 7762 |
else |
| 7763 |
libCall = spv::SMin3AMD; |
| 7764 |
} |
| 7765 |
break; |
| 7766 |
case glslang::EOpMax3: |
| 7767 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7768 |
if (isFloat) |
| 7769 |
libCall = spv::FMax3AMD; |
| 7770 |
else { |
| 7771 |
if (isUnsigned) |
| 7772 |
libCall = spv::UMax3AMD; |
| 7773 |
else |
| 7774 |
libCall = spv::SMax3AMD; |
| 7775 |
} |
| 7776 |
break; |
| 7777 |
case glslang::EOpMid3: |
| 7778 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_trinary_minmax); |
| 7779 |
if (isFloat) |
| 7780 |
libCall = spv::FMid3AMD; |
| 7781 |
else { |
| 7782 |
if (isUnsigned) |
| 7783 |
libCall = spv::UMid3AMD; |
| 7784 |
else |
| 7785 |
libCall = spv::SMid3AMD; |
| 7786 |
} |
| 7787 |
break; |
| 7788 |
|
| 7789 |
case glslang::EOpInterpolateAtVertex: |
| 7790 |
if (typeProxy == glslang::EbtFloat16) |
| 7791 |
builder.addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| 7792 |
extBuiltins = getExtBuiltins(spv::E_SPV_AMD_shader_explicit_vertex_parameter); |
| 7793 |
libCall = spv::InterpolateAtVertexAMD; |
| 7794 |
break; |
| 7795 |
|
| 7796 |
case glslang::EOpReportIntersection: |
| 7797 |
typeId = builder.makeBoolType(); |
| 7798 |
opCode = spv::OpReportIntersectionKHR; |
| 7799 |
break; |
| 7800 |
case glslang::EOpTrace: |
| 7801 |
builder.createNoResultOp(spv::OpTraceRayKHR, operands); |
| 7802 |
return 0; |
| 7803 |
case glslang::EOpExecuteCallable: |
| 7804 |
builder.createNoResultOp(spv::OpExecuteCallableKHR, operands); |
| 7805 |
return 0; |
| 7806 |
|
| 7807 |
case glslang::EOpRayQueryInitialize: |
| 7808 |
builder.createNoResultOp(spv::OpRayQueryInitializeKHR, operands); |
| 7809 |
return 0; |
| 7810 |
case glslang::EOpRayQueryTerminate: |
| 7811 |
builder.createNoResultOp(spv::OpRayQueryTerminateKHR, operands); |
| 7812 |
return 0; |
| 7813 |
case glslang::EOpRayQueryGenerateIntersection: |
| 7814 |
builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR, operands); |
| 7815 |
return 0; |
| 7816 |
case glslang::EOpRayQueryConfirmIntersection: |
| 7817 |
builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR, operands); |
| 7818 |
return 0; |
| 7819 |
case glslang::EOpRayQueryProceed: |
| 7820 |
typeId = builder.makeBoolType(); |
| 7821 |
opCode = spv::OpRayQueryProceedKHR; |
| 7822 |
break; |
| 7823 |
case glslang::EOpRayQueryGetIntersectionType: |
| 7824 |
typeId = builder.makeUintType(32); |
| 7825 |
opCode = spv::OpRayQueryGetIntersectionTypeKHR; |
| 7826 |
break; |
| 7827 |
case glslang::EOpRayQueryGetRayTMin: |
| 7828 |
typeId = builder.makeFloatType(32); |
| 7829 |
opCode = spv::OpRayQueryGetRayTMinKHR; |
| 7830 |
break; |
| 7831 |
case glslang::EOpRayQueryGetRayFlags: |
| 7832 |
typeId = builder.makeIntType(32); |
| 7833 |
opCode = spv::OpRayQueryGetRayFlagsKHR; |
| 7834 |
break; |
| 7835 |
case glslang::EOpRayQueryGetIntersectionT: |
| 7836 |
typeId = builder.makeFloatType(32); |
| 7837 |
opCode = spv::OpRayQueryGetIntersectionTKHR; |
| 7838 |
break; |
| 7839 |
case glslang::EOpRayQueryGetIntersectionInstanceCustomIndex: |
| 7840 |
typeId = builder.makeIntType(32); |
| 7841 |
opCode = spv::OpRayQueryGetIntersectionInstanceCustomIndexKHR; |
| 7842 |
break; |
| 7843 |
case glslang::EOpRayQueryGetIntersectionInstanceId: |
| 7844 |
typeId = builder.makeIntType(32); |
| 7845 |
opCode = spv::OpRayQueryGetIntersectionInstanceIdKHR; |
| 7846 |
break; |
| 7847 |
case glslang::EOpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffset: |
| 7848 |
typeId = builder.makeIntType(32); |
| 7849 |
opCode = spv::OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR; |
| 7850 |
break; |
| 7851 |
case glslang::EOpRayQueryGetIntersectionGeometryIndex: |
| 7852 |
typeId = builder.makeIntType(32); |
| 7853 |
opCode = spv::OpRayQueryGetIntersectionGeometryIndexKHR; |
| 7854 |
break; |
| 7855 |
case glslang::EOpRayQueryGetIntersectionPrimitiveIndex: |
| 7856 |
typeId = builder.makeIntType(32); |
| 7857 |
opCode = spv::OpRayQueryGetIntersectionPrimitiveIndexKHR; |
| 7858 |
break; |
| 7859 |
case glslang::EOpRayQueryGetIntersectionBarycentrics: |
| 7860 |
typeId = builder.makeVectorType(builder.makeFloatType(32), 2); |
| 7861 |
opCode = spv::OpRayQueryGetIntersectionBarycentricsKHR; |
| 7862 |
break; |
| 7863 |
case glslang::EOpRayQueryGetIntersectionFrontFace: |
| 7864 |
typeId = builder.makeBoolType(); |
| 7865 |
opCode = spv::OpRayQueryGetIntersectionFrontFaceKHR; |
| 7866 |
break; |
| 7867 |
case glslang::EOpRayQueryGetIntersectionCandidateAABBOpaque: |
| 7868 |
typeId = builder.makeBoolType(); |
| 7869 |
opCode = spv::OpRayQueryGetIntersectionCandidateAABBOpaqueKHR; |
| 7870 |
break; |
| 7871 |
case glslang::EOpRayQueryGetIntersectionObjectRayDirection: |
| 7872 |
typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7873 |
opCode = spv::OpRayQueryGetIntersectionObjectRayDirectionKHR; |
| 7874 |
break; |
| 7875 |
case glslang::EOpRayQueryGetIntersectionObjectRayOrigin: |
| 7876 |
typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7877 |
opCode = spv::OpRayQueryGetIntersectionObjectRayOriginKHR; |
| 7878 |
break; |
| 7879 |
case glslang::EOpRayQueryGetWorldRayDirection: |
| 7880 |
typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7881 |
opCode = spv::OpRayQueryGetWorldRayDirectionKHR; |
| 7882 |
break; |
| 7883 |
case glslang::EOpRayQueryGetWorldRayOrigin: |
| 7884 |
typeId = builder.makeVectorType(builder.makeFloatType(32), 3); |
| 7885 |
opCode = spv::OpRayQueryGetWorldRayOriginKHR; |
| 7886 |
break; |
| 7887 |
case glslang::EOpRayQueryGetIntersectionObjectToWorld: |
| 7888 |
typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 7889 |
opCode = spv::OpRayQueryGetIntersectionObjectToWorldKHR; |
| 7890 |
break; |
| 7891 |
case glslang::EOpRayQueryGetIntersectionWorldToObject: |
| 7892 |
typeId = builder.makeMatrixType(builder.makeFloatType(32), 4, 3); |
| 7893 |
opCode = spv::OpRayQueryGetIntersectionWorldToObjectKHR; |
| 7894 |
break; |
| 7895 |
case glslang::EOpWritePackedPrimitiveIndices4x8NV: |
| 7896 |
builder.createNoResultOp(spv::OpWritePackedPrimitiveIndices4x8NV, operands); |
| 7897 |
return 0; |
| 7898 |
case glslang::EOpCooperativeMatrixMulAdd: |
| 7899 |
opCode = spv::OpCooperativeMatrixMulAddNV; |
| 7900 |
break; |
| 7901 |
#endif // GLSLANG_WEB |
| 7902 |
default: |
| 7903 |
return 0; |
| 7904 |
} |
| 7905 |
|
| 7906 |
spv::Id id = 0; |
| 7907 |
if (libCall >= 0) { |
| 7908 |
// Use an extended instruction from the standard library. |
| 7909 |
// Construct the call arguments, without modifying the original operands vector. |
| 7910 |
// We might need the remaining arguments, e.g. in the EOpFrexp case. |
| 7911 |
std::vector<spv::Id> callArguments(operands.begin(), operands.begin() + consumedOperands); |
| 7912 |
id = builder.createBuiltinCall(typeId, extBuiltins >= 0 ? extBuiltins : stdBuiltins, libCall, callArguments); |
| 7913 |
} else if (opCode == spv::OpDot && !isFloat) { |
| 7914 |
// int dot(int, int) |
| 7915 |
// NOTE: never called for scalar/vector1, this is turned into simple mul before this can be reached |
| 7916 |
const int componentCount = builder.getNumComponents(operands[0]); |
| 7917 |
spv::Id mulOp = builder.createBinOp(spv::OpIMul, builder.getTypeId(operands[0]), operands[0], operands[1]); |
| 7918 |
builder.setPrecision(mulOp, precision); |
| 7919 |
id = builder.createCompositeExtract(mulOp, typeId, 0); |
| 7920 |
for (int i = 1; i < componentCount; ++i) { |
| 7921 |
builder.setPrecision(id, precision); |
| 7922 |
id = builder.createBinOp(spv::OpIAdd, typeId, id, builder.createCompositeExtract(mulOp, typeId, i)); |
| 7923 |
} |
| 7924 |
} else { |
| 7925 |
switch (consumedOperands) { |
| 7926 |
case 0: |
| 7927 |
// should all be handled by visitAggregate and createNoArgOperation |
| 7928 |
assert(0); |
| 7929 |
return 0; |
| 7930 |
case 1: |
| 7931 |
// should all be handled by createUnaryOperation |
| 7932 |
assert(0); |
| 7933 |
return 0; |
| 7934 |
case 2: |
| 7935 |
id = builder.createBinOp(opCode, typeId, operands[0], operands[1]); |
| 7936 |
break; |
| 7937 |
default: |
| 7938 |
// anything 3 or over doesn't have l-value operands, so all should be consumed |
| 7939 |
assert(consumedOperands == operands.size()); |
| 7940 |
id = builder.createOp(opCode, typeId, operands); |
| 7941 |
break; |
| 7942 |
} |
| 7943 |
} |
| 7944 |
|
| 7945 |
#ifndef GLSLANG_WEB |
| 7946 |
// Decode the return types that were structures |
| 7947 |
switch (op) { |
| 7948 |
case glslang::EOpAddCarry: |
| 7949 |
case glslang::EOpSubBorrow: |
| 7950 |
builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7951 |
id = builder.createCompositeExtract(id, typeId0, 0); |
| 7952 |
break; |
| 7953 |
case glslang::EOpUMulExtended: |
| 7954 |
case glslang::EOpIMulExtended: |
| 7955 |
builder.createStore(builder.createCompositeExtract(id, typeId0, 0), operands[3]); |
| 7956 |
builder.createStore(builder.createCompositeExtract(id, typeId0, 1), operands[2]); |
| 7957 |
break; |
| 7958 |
case glslang::EOpFrexp: |
| 7959 |
{ |
| 7960 |
assert(operands.size() == 2); |
| 7961 |
if (builder.isFloatType(builder.getScalarTypeId(typeId1))) { |
| 7962 |
// "exp" is floating-point type (from HLSL intrinsic) |
| 7963 |
spv::Id member1 = builder.createCompositeExtract(id, frexpIntType, 1); |
| 7964 |
member1 = builder.createUnaryOp(spv::OpConvertSToF, typeId1, member1); |
| 7965 |
builder.createStore(member1, operands[1]); |
| 7966 |
} else |
| 7967 |
// "exp" is integer type (from GLSL built-in function) |
| 7968 |
builder.createStore(builder.createCompositeExtract(id, frexpIntType, 1), operands[1]); |
| 7969 |
id = builder.createCompositeExtract(id, typeId0, 0); |
| 7970 |
} |
| 7971 |
break; |
| 7972 |
default: |
| 7973 |
break; |
| 7974 |
} |
| 7975 |
#endif |
| 7976 |
|
| 7977 |
return builder.setPrecision(id, precision); |
| 7978 |
} |
| 7979 |
|
| 7980 |
// Intrinsics with no arguments (or no return value, and no precision). |
| 7981 |
spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId) |
| 7982 |
{ |
| 7983 |
// GLSL memory barriers use queuefamily scope in new model, device scope in old model |
| 7984 |
spv::Scope memoryBarrierScope = glslangIntermediate->usingVulkanMemoryModel() ? |
| 7985 |
spv::ScopeQueueFamilyKHR : spv::ScopeDevice; |
| 7986 |
|
| 7987 |
switch (op) { |
| 7988 |
case glslang::EOpBarrier: |
| 7989 |
if (glslangIntermediate->getStage() == EShLangTessControl) { |
| 7990 |
if (glslangIntermediate->usingVulkanMemoryModel()) { |
| 7991 |
builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 7992 |
spv::MemorySemanticsOutputMemoryKHRMask | |
| 7993 |
spv::MemorySemanticsAcquireReleaseMask); |
| 7994 |
builder.addCapability(spv::CapabilityVulkanMemoryModelKHR); |
| 7995 |
} else { |
| 7996 |
builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeInvocation, spv::MemorySemanticsMaskNone); |
| 7997 |
} |
| 7998 |
} else { |
| 7999 |
builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 8000 |
spv::MemorySemanticsWorkgroupMemoryMask | |
| 8001 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8002 |
} |
| 8003 |
return 0; |
| 8004 |
case glslang::EOpMemoryBarrier: |
| 8005 |
builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAllMemory | |
| 8006 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8007 |
return 0; |
| 8008 |
case glslang::EOpMemoryBarrierBuffer: |
| 8009 |
builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsUniformMemoryMask | |
| 8010 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8011 |
return 0; |
| 8012 |
case glslang::EOpMemoryBarrierShared: |
| 8013 |
builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8014 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8015 |
return 0; |
| 8016 |
case glslang::EOpGroupMemoryBarrier: |
| 8017 |
builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsAllMemory | |
| 8018 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8019 |
return 0; |
| 8020 |
#ifndef GLSLANG_WEB |
| 8021 |
case glslang::EOpMemoryBarrierAtomicCounter: |
| 8022 |
builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsAtomicCounterMemoryMask | |
| 8023 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8024 |
return 0; |
| 8025 |
case glslang::EOpMemoryBarrierImage: |
| 8026 |
builder.createMemoryBarrier(memoryBarrierScope, spv::MemorySemanticsImageMemoryMask | |
| 8027 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8028 |
return 0; |
| 8029 |
case glslang::EOpAllMemoryBarrierWithGroupSync: |
| 8030 |
builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, |
| 8031 |
spv::MemorySemanticsAllMemory | |
| 8032 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8033 |
return 0; |
| 8034 |
case glslang::EOpDeviceMemoryBarrier: |
| 8035 |
builder.createMemoryBarrier(spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 8036 |
spv::MemorySemanticsImageMemoryMask | |
| 8037 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8038 |
return 0; |
| 8039 |
case glslang::EOpDeviceMemoryBarrierWithGroupSync: |
| 8040 |
builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeDevice, spv::MemorySemanticsUniformMemoryMask | |
| 8041 |
spv::MemorySemanticsImageMemoryMask | |
| 8042 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8043 |
return 0; |
| 8044 |
case glslang::EOpWorkgroupMemoryBarrier: |
| 8045 |
builder.createMemoryBarrier(spv::ScopeWorkgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8046 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8047 |
return 0; |
| 8048 |
case glslang::EOpWorkgroupMemoryBarrierWithGroupSync: |
| 8049 |
builder.createControlBarrier(spv::ScopeWorkgroup, spv::ScopeWorkgroup, |
| 8050 |
spv::MemorySemanticsWorkgroupMemoryMask | |
| 8051 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8052 |
return 0; |
| 8053 |
case glslang::EOpSubgroupBarrier: |
| 8054 |
builder.createControlBarrier(spv::ScopeSubgroup, spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 8055 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8056 |
return spv::NoResult; |
| 8057 |
case glslang::EOpSubgroupMemoryBarrier: |
| 8058 |
builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsAllMemory | |
| 8059 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8060 |
return spv::NoResult; |
| 8061 |
case glslang::EOpSubgroupMemoryBarrierBuffer: |
| 8062 |
builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsUniformMemoryMask | |
| 8063 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8064 |
return spv::NoResult; |
| 8065 |
case glslang::EOpSubgroupMemoryBarrierImage: |
| 8066 |
builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsImageMemoryMask | |
| 8067 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8068 |
return spv::NoResult; |
| 8069 |
case glslang::EOpSubgroupMemoryBarrierShared: |
| 8070 |
builder.createMemoryBarrier(spv::ScopeSubgroup, spv::MemorySemanticsWorkgroupMemoryMask | |
| 8071 |
spv::MemorySemanticsAcquireReleaseMask); |
| 8072 |
return spv::NoResult; |
| 8073 |
|
| 8074 |
case glslang::EOpEmitVertex: |
| 8075 |
builder.createNoResultOp(spv::OpEmitVertex); |
| 8076 |
return 0; |
| 8077 |
case glslang::EOpEndPrimitive: |
| 8078 |
builder.createNoResultOp(spv::OpEndPrimitive); |
| 8079 |
return 0; |
| 8080 |
|
| 8081 |
case glslang::EOpSubgroupElect: { |
| 8082 |
std::vector<spv::Id> operands; |
| 8083 |
return createSubgroupOperation(op, typeId, operands, glslang::EbtVoid); |
| 8084 |
} |
| 8085 |
case glslang::EOpTime: |
| 8086 |
{ |
| 8087 |
std::vector<spv::Id> args; // Dummy arguments |
| 8088 |
spv::Id id = builder.createBuiltinCall(typeId, getExtBuiltins(spv::E_SPV_AMD_gcn_shader), spv::TimeAMD, args); |
| 8089 |
return builder.setPrecision(id, precision); |
| 8090 |
} |
| 8091 |
case glslang::EOpIgnoreIntersection: |
| 8092 |
builder.createNoResultOp(spv::OpIgnoreIntersectionKHR); |
| 8093 |
return 0; |
| 8094 |
case glslang::EOpTerminateRay: |
| 8095 |
builder.createNoResultOp(spv::OpTerminateRayKHR); |
| 8096 |
return 0; |
| 8097 |
case glslang::EOpRayQueryInitialize: |
| 8098 |
builder.createNoResultOp(spv::OpRayQueryInitializeKHR); |
| 8099 |
return 0; |
| 8100 |
case glslang::EOpRayQueryTerminate: |
| 8101 |
builder.createNoResultOp(spv::OpRayQueryTerminateKHR); |
| 8102 |
return 0; |
| 8103 |
case glslang::EOpRayQueryGenerateIntersection: |
| 8104 |
builder.createNoResultOp(spv::OpRayQueryGenerateIntersectionKHR); |
| 8105 |
return 0; |
| 8106 |
case glslang::EOpRayQueryConfirmIntersection: |
| 8107 |
builder.createNoResultOp(spv::OpRayQueryConfirmIntersectionKHR); |
| 8108 |
return 0; |
| 8109 |
case glslang::EOpBeginInvocationInterlock: |
| 8110 |
builder.createNoResultOp(spv::OpBeginInvocationInterlockEXT); |
| 8111 |
return 0; |
| 8112 |
case glslang::EOpEndInvocationInterlock: |
| 8113 |
builder.createNoResultOp(spv::OpEndInvocationInterlockEXT); |
| 8114 |
return 0; |
| 8115 |
|
| 8116 |
case glslang::EOpIsHelperInvocation: |
| 8117 |
{ |
| 8118 |
std::vector<spv::Id> args; // Dummy arguments |
| 8119 |
builder.addExtension(spv::E_SPV_EXT_demote_to_helper_invocation); |
| 8120 |
builder.addCapability(spv::CapabilityDemoteToHelperInvocationEXT); |
| 8121 |
return builder.createOp(spv::OpIsHelperInvocationEXT, typeId, args); |
| 8122 |
} |
| 8123 |
|
| 8124 |
case glslang::EOpReadClockSubgroupKHR: { |
| 8125 |
std::vector<spv::Id> args; |
| 8126 |
args.push_back(builder.makeUintConstant(spv::ScopeSubgroup)); |
| 8127 |
builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 8128 |
builder.addCapability(spv::CapabilityShaderClockKHR); |
| 8129 |
return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 8130 |
} |
| 8131 |
|
| 8132 |
case glslang::EOpReadClockDeviceKHR: { |
| 8133 |
std::vector<spv::Id> args; |
| 8134 |
args.push_back(builder.makeUintConstant(spv::ScopeDevice)); |
| 8135 |
builder.addExtension(spv::E_SPV_KHR_shader_clock); |
| 8136 |
builder.addCapability(spv::CapabilityShaderClockKHR); |
| 8137 |
return builder.createOp(spv::OpReadClockKHR, typeId, args); |
| 8138 |
} |
| 8139 |
#endif |
| 8140 |
default: |
| 8141 |
break; |
| 8142 |
} |
| 8143 |
|
| 8144 |
logger->missingFunctionality("unknown operation with no arguments"); |
| 8145 |
|
| 8146 |
return 0; |
| 8147 |
} |
| 8148 |
|
| 8149 |
spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol) |
| 8150 |
{ |
| 8151 |
auto iter = symbolValues.find(symbol->getId()); |
| 8152 |
spv::Id id; |
| 8153 |
if (symbolValues.end() != iter) { |
| 8154 |
id = iter->second; |
| 8155 |
return id; |
| 8156 |
} |
| 8157 |
|
| 8158 |
// it was not found, create it |
| 8159 |
spv::BuiltIn builtIn = TranslateBuiltInDecoration(symbol->getQualifier().builtIn, false); |
| 8160 |
auto forcedType = getForcedType(symbol->getQualifier().builtIn, symbol->getType()); |
| 8161 |
id = createSpvVariable(symbol, forcedType.first); |
| 8162 |
symbolValues[symbol->getId()] = id; |
| 8163 |
if (forcedType.second != spv::NoType) |
| 8164 |
forceType[id] = forcedType.second; |
| 8165 |
|
| 8166 |
if (symbol->getBasicType() != glslang::EbtBlock) { |
| 8167 |
builder.addDecoration(id, TranslatePrecisionDecoration(symbol->getType())); |
| 8168 |
builder.addDecoration(id, TranslateInterpolationDecoration(symbol->getType().getQualifier())); |
| 8169 |
builder.addDecoration(id, TranslateAuxiliaryStorageDecoration(symbol->getType().getQualifier())); |
| 8170 |
#ifndef GLSLANG_WEB |
| 8171 |
addMeshNVDecoration(id, /*member*/ -1, symbol->getType().getQualifier()); |
| 8172 |
if (symbol->getQualifier().hasComponent()) |
| 8173 |
builder.addDecoration(id, spv::DecorationComponent, symbol->getQualifier().layoutComponent); |
| 8174 |
if (symbol->getQualifier().hasIndex()) |
| 8175 |
builder.addDecoration(id, spv::DecorationIndex, symbol->getQualifier().layoutIndex); |
| 8176 |
#endif |
| 8177 |
if (symbol->getType().getQualifier().hasSpecConstantId()) |
| 8178 |
builder.addDecoration(id, spv::DecorationSpecId, symbol->getType().getQualifier().layoutSpecConstantId); |
| 8179 |
// atomic counters use this: |
| 8180 |
if (symbol->getQualifier().hasOffset()) |
| 8181 |
builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutOffset); |
| 8182 |
} |
| 8183 |
|
| 8184 |
if (symbol->getQualifier().hasLocation()) |
| 8185 |
builder.addDecoration(id, spv::DecorationLocation, symbol->getQualifier().layoutLocation); |
| 8186 |
builder.addDecoration(id, TranslateInvariantDecoration(symbol->getType().getQualifier())); |
| 8187 |
if (symbol->getQualifier().hasStream() && glslangIntermediate->isMultiStream()) { |
| 8188 |
builder.addCapability(spv::CapabilityGeometryStreams); |
| 8189 |
builder.addDecoration(id, spv::DecorationStream, symbol->getQualifier().layoutStream); |
| 8190 |
} |
| 8191 |
if (symbol->getQualifier().hasSet()) |
| 8192 |
builder.addDecoration(id, spv::DecorationDescriptorSet, symbol->getQualifier().layoutSet); |
| 8193 |
else if (IsDescriptorResource(symbol->getType())) { |
| 8194 |
// default to 0 |
| 8195 |
builder.addDecoration(id, spv::DecorationDescriptorSet, 0); |
| 8196 |
} |
| 8197 |
if (symbol->getQualifier().hasBinding()) |
| 8198 |
builder.addDecoration(id, spv::DecorationBinding, symbol->getQualifier().layoutBinding); |
| 8199 |
else if (IsDescriptorResource(symbol->getType())) { |
| 8200 |
// default to 0 |
| 8201 |
builder.addDecoration(id, spv::DecorationBinding, 0); |
| 8202 |
} |
| 8203 |
if (symbol->getQualifier().hasAttachment()) |
| 8204 |
builder.addDecoration(id, spv::DecorationInputAttachmentIndex, symbol->getQualifier().layoutAttachment); |
| 8205 |
if (glslangIntermediate->getXfbMode()) { |
| 8206 |
builder.addCapability(spv::CapabilityTransformFeedback); |
| 8207 |
if (symbol->getQualifier().hasXfbBuffer()) { |
| 8208 |
builder.addDecoration(id, spv::DecorationXfbBuffer, symbol->getQualifier().layoutXfbBuffer); |
| 8209 |
unsigned stride = glslangIntermediate->getXfbStride(symbol->getQualifier().layoutXfbBuffer); |
| 8210 |
if (stride != glslang::TQualifier::layoutXfbStrideEnd) |
| 8211 |
builder.addDecoration(id, spv::DecorationXfbStride, stride); |
| 8212 |
} |
| 8213 |
if (symbol->getQualifier().hasXfbOffset()) |
| 8214 |
builder.addDecoration(id, spv::DecorationOffset, symbol->getQualifier().layoutXfbOffset); |
| 8215 |
} |
| 8216 |
|
| 8217 |
// add built-in variable decoration |
| 8218 |
if (builtIn != spv::BuiltInMax) { |
| 8219 |
builder.addDecoration(id, spv::DecorationBuiltIn, (int)builtIn); |
| 8220 |
} |
| 8221 |
|
| 8222 |
#ifndef GLSLANG_WEB |
| 8223 |
if (symbol->getType().isImage()) { |
| 8224 |
std::vector<spv::Decoration> memory; |
| 8225 |
TranslateMemoryDecoration(symbol->getType().getQualifier(), memory, |
| 8226 |
glslangIntermediate->usingVulkanMemoryModel()); |
| 8227 |
for (unsigned int i = 0; i < memory.size(); ++i) |
| 8228 |
builder.addDecoration(id, memory[i]); |
| 8229 |
} |
| 8230 |
|
| 8231 |
// nonuniform |
| 8232 |
builder.addDecoration(id, TranslateNonUniformDecoration(symbol->getType().getQualifier())); |
| 8233 |
|
| 8234 |
if (builtIn == spv::BuiltInSampleMask) { |
| 8235 |
spv::Decoration decoration; |
| 8236 |
// GL_NV_sample_mask_override_coverage extension |
| 8237 |
if (glslangIntermediate->getLayoutOverrideCoverage()) |
| 8238 |
decoration = (spv::Decoration)spv::DecorationOverrideCoverageNV; |
| 8239 |
else |
| 8240 |
decoration = (spv::Decoration)spv::DecorationMax; |
| 8241 |
builder.addDecoration(id, decoration); |
| 8242 |
if (decoration != spv::DecorationMax) { |
| 8243 |
builder.addCapability(spv::CapabilitySampleMaskOverrideCoverageNV); |
| 8244 |
builder.addExtension(spv::E_SPV_NV_sample_mask_override_coverage); |
| 8245 |
} |
| 8246 |
} |
| 8247 |
else if (builtIn == spv::BuiltInLayer) { |
| 8248 |
// SPV_NV_viewport_array2 extension |
| 8249 |
if (symbol->getQualifier().layoutViewportRelative) { |
| 8250 |
builder.addDecoration(id, (spv::Decoration)spv::DecorationViewportRelativeNV); |
| 8251 |
builder.addCapability(spv::CapabilityShaderViewportMaskNV); |
| 8252 |
builder.addExtension(spv::E_SPV_NV_viewport_array2); |
| 8253 |
} |
| 8254 |
if (symbol->getQualifier().layoutSecondaryViewportRelativeOffset != -2048) { |
| 8255 |
builder.addDecoration(id, (spv::Decoration)spv::DecorationSecondaryViewportRelativeNV, |
| 8256 |
symbol->getQualifier().layoutSecondaryViewportRelativeOffset); |
| 8257 |
builder.addCapability(spv::CapabilityShaderStereoViewNV); |
| 8258 |
builder.addExtension(spv::E_SPV_NV_stereo_view_rendering); |
| 8259 |
} |
| 8260 |
} |
| 8261 |
|
| 8262 |
if (symbol->getQualifier().layoutPassthrough) { |
| 8263 |
builder.addDecoration(id, spv::DecorationPassthroughNV); |
| 8264 |
builder.addCapability(spv::CapabilityGeometryShaderPassthroughNV); |
| 8265 |
builder.addExtension(spv::E_SPV_NV_geometry_shader_passthrough); |
| 8266 |
} |
| 8267 |
if (symbol->getQualifier().pervertexNV) { |
| 8268 |
builder.addDecoration(id, spv::DecorationPerVertexNV); |
| 8269 |
builder.addCapability(spv::CapabilityFragmentBarycentricNV); |
| 8270 |
builder.addExtension(spv::E_SPV_NV_fragment_shader_barycentric); |
| 8271 |
} |
| 8272 |
|
| 8273 |
if (glslangIntermediate->getHlslFunctionality1() && symbol->getType().getQualifier().semanticName != nullptr) { |
| 8274 |
builder.addExtension("SPV_GOOGLE_hlsl_functionality1"); |
| 8275 |
builder.addDecoration(id, (spv::Decoration)spv::DecorationHlslSemanticGOOGLE, |
| 8276 |
symbol->getType().getQualifier().semanticName); |
| 8277 |
} |
| 8278 |
|
| 8279 |
if (symbol->isReference()) { |
| 8280 |
builder.addDecoration(id, symbol->getType().getQualifier().restrict ? |
| 8281 |
spv::DecorationRestrictPointerEXT : spv::DecorationAliasedPointerEXT); |
| 8282 |
} |
| 8283 |
#endif |
| 8284 |
|
| 8285 |
return id; |
| 8286 |
} |
| 8287 |
|
| 8288 |
#ifndef GLSLANG_WEB |
| 8289 |
// add per-primitive, per-view. per-task decorations to a struct member (member >= 0) or an object |
| 8290 |
void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier) |
| 8291 |
{ |
| 8292 |
if (member >= 0) { |
| 8293 |
if (qualifier.perPrimitiveNV) { |
| 8294 |
// Need to add capability/extension for fragment shader. |
| 8295 |
// Mesh shader already adds this by default. |
| 8296 |
if (glslangIntermediate->getStage() == EShLangFragment) { |
| 8297 |
builder.addCapability(spv::CapabilityMeshShadingNV); |
| 8298 |
builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 8299 |
} |
| 8300 |
builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerPrimitiveNV); |
| 8301 |
} |
| 8302 |
if (qualifier.perViewNV) |
| 8303 |
builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerViewNV); |
| 8304 |
if (qualifier.perTaskNV) |
| 8305 |
builder.addMemberDecoration(id, (unsigned)member, spv::DecorationPerTaskNV); |
| 8306 |
} else { |
| 8307 |
if (qualifier.perPrimitiveNV) { |
| 8308 |
// Need to add capability/extension for fragment shader. |
| 8309 |
// Mesh shader already adds this by default. |
| 8310 |
if (glslangIntermediate->getStage() == EShLangFragment) { |
| 8311 |
builder.addCapability(spv::CapabilityMeshShadingNV); |
| 8312 |
builder.addExtension(spv::E_SPV_NV_mesh_shader); |
| 8313 |
} |
| 8314 |
builder.addDecoration(id, spv::DecorationPerPrimitiveNV); |
| 8315 |
} |
| 8316 |
if (qualifier.perViewNV) |
| 8317 |
builder.addDecoration(id, spv::DecorationPerViewNV); |
| 8318 |
if (qualifier.perTaskNV) |
| 8319 |
builder.addDecoration(id, spv::DecorationPerTaskNV); |
| 8320 |
} |
| 8321 |
} |
| 8322 |
#endif |
| 8323 |
|
| 8324 |
// Make a full tree of instructions to build a SPIR-V specialization constant, |
| 8325 |
// or regular constant if possible. |
| 8326 |
// |
| 8327 |
// TBD: this is not yet done, nor verified to be the best design, it does do the leaf symbols though |
| 8328 |
// |
| 8329 |
// Recursively walk the nodes. The nodes form a tree whose leaves are |
| 8330 |
// regular constants, which themselves are trees that createSpvConstant() |
| 8331 |
// recursively walks. So, this function walks the "top" of the tree: |
| 8332 |
// - emit specialization constant-building instructions for specConstant |
| 8333 |
// - when running into a non-spec-constant, switch to createSpvConstant() |
| 8334 |
spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node) |
| 8335 |
{ |
| 8336 |
assert(node.getQualifier().isConstant()); |
| 8337 |
|
| 8338 |
// Handle front-end constants first (non-specialization constants). |
| 8339 |
if (! node.getQualifier().specConstant) { |
| 8340 |
// hand off to the non-spec-constant path |
| 8341 |
assert(node.getAsConstantUnion() != nullptr || node.getAsSymbolNode() != nullptr); |
| 8342 |
int nextConst = 0; |
| 8343 |
return createSpvConstantFromConstUnionArray(node.getType(), node.getAsConstantUnion() ? |
| 8344 |
node.getAsConstantUnion()->getConstArray() : node.getAsSymbolNode()->getConstArray(), |
| 8345 |
nextConst, false); |
| 8346 |
} |
| 8347 |
|
| 8348 |
// We now know we have a specialization constant to build |
| 8349 |
|
| 8350 |
// Extra capabilities may be needed. |
| 8351 |
if (node.getType().contains8BitInt()) |
| 8352 |
builder.addCapability(spv::CapabilityInt8); |
| 8353 |
if (node.getType().contains16BitFloat()) |
| 8354 |
builder.addCapability(spv::CapabilityFloat16); |
| 8355 |
if (node.getType().contains16BitInt()) |
| 8356 |
builder.addCapability(spv::CapabilityInt16); |
| 8357 |
if (node.getType().contains64BitInt()) |
| 8358 |
builder.addCapability(spv::CapabilityInt64); |
| 8359 |
if (node.getType().containsDouble()) |
| 8360 |
builder.addCapability(spv::CapabilityFloat64); |
| 8361 |
|
| 8362 |
// gl_WorkGroupSize is a special case until the front-end handles hierarchical specialization constants, |
| 8363 |
// even then, it's specialization ids are handled by special case syntax in GLSL: layout(local_size_x = ... |
| 8364 |
if (node.getType().getQualifier().builtIn == glslang::EbvWorkGroupSize) { |
| 8365 |
std::vector<spv::Id> dimConstId; |
| 8366 |
for (int dim = 0; dim < 3; ++dim) { |
| 8367 |
bool specConst = (glslangIntermediate->getLocalSizeSpecId(dim) != glslang::TQualifier::layoutNotSet); |
| 8368 |
dimConstId.push_back(builder.makeUintConstant(glslangIntermediate->getLocalSize(dim), specConst)); |
| 8369 |
if (specConst) { |
| 8370 |
builder.addDecoration(dimConstId.back(), spv::DecorationSpecId, |
| 8371 |
glslangIntermediate->getLocalSizeSpecId(dim)); |
| 8372 |
} |
| 8373 |
} |
| 8374 |
return builder.makeCompositeConstant(builder.makeVectorType(builder.makeUintType(32), 3), dimConstId, true); |
| 8375 |
} |
| 8376 |
|
| 8377 |
// An AST node labelled as specialization constant should be a symbol node. |
| 8378 |
// Its initializer should either be a sub tree with constant nodes, or a constant union array. |
| 8379 |
if (auto* sn = node.getAsSymbolNode()) { |
| 8380 |
spv::Id result; |
| 8381 |
if (auto* sub_tree = sn->getConstSubtree()) { |
| 8382 |
// Traverse the constant constructor sub tree like generating normal run-time instructions. |
| 8383 |
// During the AST traversal, if the node is marked as 'specConstant', SpecConstantOpModeGuard |
| 8384 |
// will set the builder into spec constant op instruction generating mode. |
| 8385 |
sub_tree->traverse(this); |
| 8386 |
result = accessChainLoad(sub_tree->getType()); |
| 8387 |
} else if (auto* const_union_array = &sn->getConstArray()) { |
| 8388 |
int nextConst = 0; |
| 8389 |
result = createSpvConstantFromConstUnionArray(sn->getType(), *const_union_array, nextConst, true); |
| 8390 |
} else { |
| 8391 |
logger->missingFunctionality("Invalid initializer for spec onstant."); |
| 8392 |
return spv::NoResult; |
| 8393 |
} |
| 8394 |
builder.addName(result, sn->getName().c_str()); |
| 8395 |
return result; |
| 8396 |
} |
| 8397 |
|
| 8398 |
// Neither a front-end constant node, nor a specialization constant node with constant union array or |
| 8399 |
// constant sub tree as initializer. |
| 8400 |
logger->missingFunctionality("Neither a front-end constant nor a spec constant."); |
| 8401 |
return spv::NoResult; |
| 8402 |
} |
| 8403 |
|
| 8404 |
// Use 'consts' as the flattened glslang source of scalar constants to recursively |
| 8405 |
// build the aggregate SPIR-V constant. |
| 8406 |
// |
| 8407 |
// If there are not enough elements present in 'consts', 0 will be substituted; |
| 8408 |
// an empty 'consts' can be used to create a fully zeroed SPIR-V constant. |
| 8409 |
// |
| 8410 |
spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType, |
| 8411 |
const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant) |
| 8412 |
{ |
| 8413 |
// vector of constants for SPIR-V |
| 8414 |
std::vector<spv::Id> spvConsts; |
| 8415 |
|
| 8416 |
// Type is used for struct and array constants |
| 8417 |
spv::Id typeId = convertGlslangToSpvType(glslangType); |
| 8418 |
|
| 8419 |
if (glslangType.isArray()) { |
| 8420 |
glslang::TType elementType(glslangType, 0); |
| 8421 |
for (int i = 0; i < glslangType.getOuterArraySize(); ++i) |
| 8422 |
spvConsts.push_back(createSpvConstantFromConstUnionArray(elementType, consts, nextConst, false)); |
| 8423 |
} else if (glslangType.isMatrix()) { |
| 8424 |
glslang::TType vectorType(glslangType, 0); |
| 8425 |
for (int col = 0; col < glslangType.getMatrixCols(); ++col) |
| 8426 |
spvConsts.push_back(createSpvConstantFromConstUnionArray(vectorType, consts, nextConst, false)); |
| 8427 |
} else if (glslangType.isCoopMat()) { |
| 8428 |
glslang::TType componentType(glslangType.getBasicType()); |
| 8429 |
spvConsts.push_back(createSpvConstantFromConstUnionArray(componentType, consts, nextConst, false)); |
| 8430 |
} else if (glslangType.isStruct()) { |
| 8431 |
glslang::TVector<glslang::TTypeLoc>::const_iterator iter; |
| 8432 |
for (iter = glslangType.getStruct()->begin(); iter != glslangType.getStruct()->end(); ++iter) |
| 8433 |
spvConsts.push_back(createSpvConstantFromConstUnionArray(*iter->type, consts, nextConst, false)); |
| 8434 |
} else if (glslangType.getVectorSize() > 1) { |
| 8435 |
for (unsigned int i = 0; i < (unsigned int)glslangType.getVectorSize(); ++i) { |
| 8436 |
bool zero = nextConst >= consts.size(); |
| 8437 |
switch (glslangType.getBasicType()) { |
| 8438 |
case glslang::EbtInt: |
| 8439 |
spvConsts.push_back(builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst())); |
| 8440 |
break; |
| 8441 |
case glslang::EbtUint: |
| 8442 |
spvConsts.push_back(builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst())); |
| 8443 |
break; |
| 8444 |
case glslang::EbtFloat: |
| 8445 |
spvConsts.push_back(builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 8446 |
break; |
| 8447 |
case glslang::EbtBool: |
| 8448 |
spvConsts.push_back(builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst())); |
| 8449 |
break; |
| 8450 |
#ifndef GLSLANG_WEB |
| 8451 |
case glslang::EbtInt8: |
| 8452 |
spvConsts.push_back(builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const())); |
| 8453 |
break; |
| 8454 |
case glslang::EbtUint8: |
| 8455 |
spvConsts.push_back(builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const())); |
| 8456 |
break; |
| 8457 |
case glslang::EbtInt16: |
| 8458 |
spvConsts.push_back(builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const())); |
| 8459 |
break; |
| 8460 |
case glslang::EbtUint16: |
| 8461 |
spvConsts.push_back(builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const())); |
| 8462 |
break; |
| 8463 |
case glslang::EbtInt64: |
| 8464 |
spvConsts.push_back(builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const())); |
| 8465 |
break; |
| 8466 |
case glslang::EbtUint64: |
| 8467 |
spvConsts.push_back(builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const())); |
| 8468 |
break; |
| 8469 |
case glslang::EbtDouble: |
| 8470 |
spvConsts.push_back(builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst())); |
| 8471 |
break; |
| 8472 |
case glslang::EbtFloat16: |
| 8473 |
spvConsts.push_back(builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst())); |
| 8474 |
break; |
| 8475 |
#endif |
| 8476 |
default: |
| 8477 |
assert(0); |
| 8478 |
break; |
| 8479 |
} |
| 8480 |
++nextConst; |
| 8481 |
} |
| 8482 |
} else { |
| 8483 |
// we have a non-aggregate (scalar) constant |
| 8484 |
bool zero = nextConst >= consts.size(); |
| 8485 |
spv::Id scalar = 0; |
| 8486 |
switch (glslangType.getBasicType()) { |
| 8487 |
case glslang::EbtInt: |
| 8488 |
scalar = builder.makeIntConstant(zero ? 0 : consts[nextConst].getIConst(), specConstant); |
| 8489 |
break; |
| 8490 |
case glslang::EbtUint: |
| 8491 |
scalar = builder.makeUintConstant(zero ? 0 : consts[nextConst].getUConst(), specConstant); |
| 8492 |
break; |
| 8493 |
case glslang::EbtFloat: |
| 8494 |
scalar = builder.makeFloatConstant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8495 |
break; |
| 8496 |
case glslang::EbtBool: |
| 8497 |
scalar = builder.makeBoolConstant(zero ? false : consts[nextConst].getBConst(), specConstant); |
| 8498 |
break; |
| 8499 |
#ifndef GLSLANG_WEB |
| 8500 |
case glslang::EbtInt8: |
| 8501 |
scalar = builder.makeInt8Constant(zero ? 0 : consts[nextConst].getI8Const(), specConstant); |
| 8502 |
break; |
| 8503 |
case glslang::EbtUint8: |
| 8504 |
scalar = builder.makeUint8Constant(zero ? 0 : consts[nextConst].getU8Const(), specConstant); |
| 8505 |
break; |
| 8506 |
case glslang::EbtInt16: |
| 8507 |
scalar = builder.makeInt16Constant(zero ? 0 : consts[nextConst].getI16Const(), specConstant); |
| 8508 |
break; |
| 8509 |
case glslang::EbtUint16: |
| 8510 |
scalar = builder.makeUint16Constant(zero ? 0 : consts[nextConst].getU16Const(), specConstant); |
| 8511 |
break; |
| 8512 |
case glslang::EbtInt64: |
| 8513 |
scalar = builder.makeInt64Constant(zero ? 0 : consts[nextConst].getI64Const(), specConstant); |
| 8514 |
break; |
| 8515 |
case glslang::EbtUint64: |
| 8516 |
scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8517 |
break; |
| 8518 |
case glslang::EbtDouble: |
| 8519 |
scalar = builder.makeDoubleConstant(zero ? 0.0 : consts[nextConst].getDConst(), specConstant); |
| 8520 |
break; |
| 8521 |
case glslang::EbtFloat16: |
| 8522 |
scalar = builder.makeFloat16Constant(zero ? 0.0F : (float)consts[nextConst].getDConst(), specConstant); |
| 8523 |
break; |
| 8524 |
case glslang::EbtReference: |
| 8525 |
scalar = builder.makeUint64Constant(zero ? 0 : consts[nextConst].getU64Const(), specConstant); |
| 8526 |
scalar = builder.createUnaryOp(spv::OpBitcast, typeId, scalar); |
| 8527 |
break; |
| 8528 |
#endif |
| 8529 |
case glslang::EbtString: |
| 8530 |
scalar = builder.getStringId(consts[nextConst].getSConst()->c_str()); |
| 8531 |
break; |
| 8532 |
default: |
| 8533 |
assert(0); |
| 8534 |
break; |
| 8535 |
} |
| 8536 |
++nextConst; |
| 8537 |
return scalar; |
| 8538 |
} |
| 8539 |
|
| 8540 |
return builder.makeCompositeConstant(typeId, spvConsts); |
| 8541 |
} |
| 8542 |
|
| 8543 |
// Return true if the node is a constant or symbol whose reading has no |
| 8544 |
// non-trivial observable cost or effect. |
| 8545 |
bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node) |
| 8546 |
{ |
| 8547 |
// don't know what this is |
| 8548 |
if (node == nullptr) |
| 8549 |
return false; |
| 8550 |
|
| 8551 |
// a constant is safe |
| 8552 |
if (node->getAsConstantUnion() != nullptr) |
| 8553 |
return true; |
| 8554 |
|
| 8555 |
// not a symbol means non-trivial |
| 8556 |
if (node->getAsSymbolNode() == nullptr) |
| 8557 |
return false; |
| 8558 |
|
| 8559 |
// a symbol, depends on what's being read |
| 8560 |
switch (node->getType().getQualifier().storage) { |
| 8561 |
case glslang::EvqTemporary: |
| 8562 |
case glslang::EvqGlobal: |
| 8563 |
case glslang::EvqIn: |
| 8564 |
case glslang::EvqInOut: |
| 8565 |
case glslang::EvqConst: |
| 8566 |
case glslang::EvqConstReadOnly: |
| 8567 |
case glslang::EvqUniform: |
| 8568 |
return true; |
| 8569 |
default: |
| 8570 |
return false; |
| 8571 |
} |
| 8572 |
} |
| 8573 |
|
| 8574 |
// A node is trivial if it is a single operation with no side effects. |
| 8575 |
// HLSL (and/or vectors) are always trivial, as it does not short circuit. |
| 8576 |
// Otherwise, error on the side of saying non-trivial. |
| 8577 |
// Return true if trivial. |
| 8578 |
bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node) |
| 8579 |
{ |
| 8580 |
if (node == nullptr) |
| 8581 |
return false; |
| 8582 |
|
| 8583 |
// count non scalars as trivial, as well as anything coming from HLSL |
| 8584 |
if (! node->getType().isScalarOrVec1() || glslangIntermediate->getSource() == glslang::EShSourceHlsl) |
| 8585 |
return true; |
| 8586 |
|
| 8587 |
// symbols and constants are trivial |
| 8588 |
if (isTrivialLeaf(node)) |
| 8589 |
return true; |
| 8590 |
|
| 8591 |
// otherwise, it needs to be a simple operation or one or two leaf nodes |
| 8592 |
|
| 8593 |
// not a simple operation |
| 8594 |
const glslang::TIntermBinary* binaryNode = node->getAsBinaryNode(); |
| 8595 |
const glslang::TIntermUnary* unaryNode = node->getAsUnaryNode(); |
| 8596 |
if (binaryNode == nullptr && unaryNode == nullptr) |
| 8597 |
return false; |
| 8598 |
|
| 8599 |
// not on leaf nodes |
| 8600 |
if (binaryNode && (! isTrivialLeaf(binaryNode->getLeft()) || ! isTrivialLeaf(binaryNode->getRight()))) |
| 8601 |
return false; |
| 8602 |
|
| 8603 |
if (unaryNode && ! isTrivialLeaf(unaryNode->getOperand())) { |
| 8604 |
return false; |
| 8605 |
} |
| 8606 |
|
| 8607 |
switch (node->getAsOperator()->getOp()) { |
| 8608 |
case glslang::EOpLogicalNot: |
| 8609 |
case glslang::EOpConvIntToBool: |
| 8610 |
case glslang::EOpConvUintToBool: |
| 8611 |
case glslang::EOpConvFloatToBool: |
| 8612 |
case glslang::EOpConvDoubleToBool: |
| 8613 |
case glslang::EOpEqual: |
| 8614 |
case glslang::EOpNotEqual: |
| 8615 |
case glslang::EOpLessThan: |
| 8616 |
case glslang::EOpGreaterThan: |
| 8617 |
case glslang::EOpLessThanEqual: |
| 8618 |
case glslang::EOpGreaterThanEqual: |
| 8619 |
case glslang::EOpIndexDirect: |
| 8620 |
case glslang::EOpIndexDirectStruct: |
| 8621 |
case glslang::EOpLogicalXor: |
| 8622 |
case glslang::EOpAny: |
| 8623 |
case glslang::EOpAll: |
| 8624 |
return true; |
| 8625 |
default: |
| 8626 |
return false; |
| 8627 |
} |
| 8628 |
} |
| 8629 |
|
| 8630 |
// Emit short-circuiting code, where 'right' is never evaluated unless |
| 8631 |
// the left side is true (for &&) or false (for ||). |
| 8632 |
spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left, |
| 8633 |
glslang::TIntermTyped& right) |
| 8634 |
{ |
| 8635 |
spv::Id boolTypeId = builder.makeBoolType(); |
| 8636 |
|
| 8637 |
// emit left operand |
| 8638 |
builder.clearAccessChain(); |
| 8639 |
left.traverse(this); |
| 8640 |
spv::Id leftId = accessChainLoad(left.getType()); |
| 8641 |
|
| 8642 |
// Operands to accumulate OpPhi operands |
| 8643 |
std::vector<spv::Id> phiOperands; |
| 8644 |
// accumulate left operand's phi information |
| 8645 |
phiOperands.push_back(leftId); |
| 8646 |
phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8647 |
|
| 8648 |
// Make the two kinds of operation symmetric with a "!" |
| 8649 |
// || => emit "if (! left) result = right" |
| 8650 |
// && => emit "if ( left) result = right" |
| 8651 |
// |
| 8652 |
// TODO: this runtime "not" for || could be avoided by adding functionality |
| 8653 |
// to 'builder' to have an "else" without an "then" |
| 8654 |
if (op == glslang::EOpLogicalOr) |
| 8655 |
leftId = builder.createUnaryOp(spv::OpLogicalNot, boolTypeId, leftId); |
| 8656 |
|
| 8657 |
// make an "if" based on the left value |
| 8658 |
spv::Builder::If ifBuilder(leftId, spv::SelectionControlMaskNone, builder); |
| 8659 |
|
| 8660 |
// emit right operand as the "then" part of the "if" |
| 8661 |
builder.clearAccessChain(); |
| 8662 |
right.traverse(this); |
| 8663 |
spv::Id rightId = accessChainLoad(right.getType()); |
| 8664 |
|
| 8665 |
// accumulate left operand's phi information |
| 8666 |
phiOperands.push_back(rightId); |
| 8667 |
phiOperands.push_back(builder.getBuildPoint()->getId()); |
| 8668 |
|
| 8669 |
// finish the "if" |
| 8670 |
ifBuilder.makeEndIf(); |
| 8671 |
|
| 8672 |
// phi together the two results |
| 8673 |
return builder.createOp(spv::OpPhi, boolTypeId, phiOperands); |
| 8674 |
} |
| 8675 |
|
| 8676 |
#ifndef GLSLANG_WEB |
| 8677 |
// Return type Id of the imported set of extended instructions corresponds to the name. |
| 8678 |
// Import this set if it has not been imported yet. |
| 8679 |
spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name) |
| 8680 |
{ |
| 8681 |
if (extBuiltinMap.find(name) != extBuiltinMap.end()) |
| 8682 |
return extBuiltinMap[name]; |
| 8683 |
else { |
| 8684 |
builder.addExtension(name); |
| 8685 |
spv::Id extBuiltins = builder.import(name); |
| 8686 |
extBuiltinMap[name] = extBuiltins; |
| 8687 |
return extBuiltins; |
| 8688 |
} |
| 8689 |
} |
| 8690 |
#endif |
| 8691 |
|
| 8692 |
}; // end anonymous namespace |
| 8693 |
|
| 8694 |
namespace glslang { |
| 8695 |
|
| 8696 |
void GetSpirvVersion(std::string& version) |
| 8697 |
{ |
| 8698 |
const int bufSize = 100; |
| 8699 |
char buf[bufSize]; |
| 8700 |
snprintf(buf, bufSize, "0x%08x, Revision %d", spv::Version, spv::Revision); |
| 8701 |
version = buf; |
| 8702 |
} |
| 8703 |
|
| 8704 |
// For low-order part of the generator's magic number. Bump up |
| 8705 |
// when there is a change in the style (e.g., if SSA form changes, |
| 8706 |
// or a different instruction sequence to do something gets used). |
| 8707 |
int GetSpirvGeneratorVersion() |
| 8708 |
{ |
| 8709 |
// return 1; // start |
| 8710 |
// return 2; // EOpAtomicCounterDecrement gets a post decrement, to map between GLSL -> SPIR-V |
| 8711 |
// return 3; // change/correct barrier-instruction operands, to match memory model group decisions |
| 8712 |
// return 4; // some deeper access chains: for dynamic vector component, and local Boolean component |
| 8713 |
// return 5; // make OpArrayLength result type be an int with signedness of 0 |
| 8714 |
// return 6; // revert version 5 change, which makes a different (new) kind of incorrect code, |
| 8715 |
// versions 4 and 6 each generate OpArrayLength as it has long been done |
| 8716 |
// return 7; // GLSL volatile keyword maps to both SPIR-V decorations Volatile and Coherent |
| 8717 |
// return 8; // switch to new dead block eliminator; use OpUnreachable |
| 8718 |
// return 9; // don't include opaque function parameters in OpEntryPoint global's operand list |
| 8719 |
return 10; // Generate OpFUnordNotEqual for != comparisons |
| 8720 |
} |
| 8721 |
|
| 8722 |
// Write SPIR-V out to a binary file |
| 8723 |
void OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName) |
| 8724 |
{ |
| 8725 |
std::ofstream out; |
| 8726 |
out.open(baseName, std::ios::binary | std::ios::out); |
| 8727 |
if (out.fail()) |
| 8728 |
printf("ERROR: Failed to open file: %s\n", baseName); |
| 8729 |
for (int i = 0; i < (int)spirv.size(); ++i) { |
| 8730 |
unsigned int word = spirv[i]; |
| 8731 |
out.write((const char*)&word, 4); |
| 8732 |
} |
| 8733 |
out.close(); |
| 8734 |
} |
| 8735 |
|
| 8736 |
// Write SPIR-V out to a text file with 32-bit hexadecimal words |
| 8737 |
void OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName) |
| 8738 |
{ |
| 8739 |
#if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE) |
| 8740 |
std::ofstream out; |
| 8741 |
out.open(baseName, std::ios::binary | std::ios::out); |
| 8742 |
if (out.fail()) |
| 8743 |
printf("ERROR: Failed to open file: %s\n", baseName); |
| 8744 |
out << "\t// " << |
| 8745 |
GetSpirvGeneratorVersion() << |
| 8746 |
GLSLANG_VERSION_MAJOR << "." << GLSLANG_VERSION_MINOR << "." << GLSLANG_VERSION_PATCH << |
| 8747 |
GLSLANG_VERSION_FLAVOR << std::endl; |
| 8748 |
if (varName != nullptr) { |
| 8749 |
out << "\t #pragma once" << std::endl; |
| 8750 |
out << "const uint32_t " << varName << "[] = {" << std::endl; |
| 8751 |
} |
| 8752 |
const int WORDS_PER_LINE = 8; |
| 8753 |
for (int i = 0; i < (int)spirv.size(); i += WORDS_PER_LINE) { |
| 8754 |
out << "\t"; |
| 8755 |
for (int j = 0; j < WORDS_PER_LINE && i + j < (int)spirv.size(); ++j) { |
| 8756 |
const unsigned int word = spirv[i + j]; |
| 8757 |
out << "0x" << std::hex << std::setw(8) << std::setfill('0') << word; |
| 8758 |
if (i + j + 1 < (int)spirv.size()) { |
| 8759 |
out << ","; |
| 8760 |
} |
| 8761 |
} |
| 8762 |
out << std::endl; |
| 8763 |
} |
| 8764 |
if (varName != nullptr) { |
| 8765 |
out << "};"; |
| 8766 |
out << std::endl; |
| 8767 |
} |
| 8768 |
out.close(); |
| 8769 |
#endif |
| 8770 |
} |
| 8771 |
|
| 8772 |
// |
| 8773 |
// Set up the glslang traversal |
| 8774 |
// |
| 8775 |
void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options) |
| 8776 |
{ |
| 8777 |
spv::SpvBuildLogger logger; |
| 8778 |
GlslangToSpv(intermediate, spirv, &logger, options); |
| 8779 |
} |
| 8780 |
|
| 8781 |
void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, |
| 8782 |
spv::SpvBuildLogger* logger, SpvOptions* options) |
| 8783 |
{ |
| 8784 |
TIntermNode* root = intermediate.getTreeRoot(); |
| 8785 |
|
| 8786 |
if (root == 0) |
| 8787 |
return; |
| 8788 |
|
| 8789 |
SpvOptions defaultOptions; |
| 8790 |
if (options == nullptr) |
| 8791 |
options = &defaultOptions; |
| 8792 |
|
| 8793 |
GetThreadPoolAllocator().push(); |
| 8794 |
|
| 8795 |
TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options); |
| 8796 |
root->traverse(&it); |
| 8797 |
it.finishSpv(); |
| 8798 |
it.dumpSpv(spirv); |
| 8799 |
|
| 8800 |
#if ENABLE_OPT |
| 8801 |
// If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan |
| 8802 |
// eg. forward and remove memory writes of opaque types. |
| 8803 |
bool prelegalization = intermediate.getSource() == EShSourceHlsl; |
| 8804 |
if ((prelegalization || options->optimizeSize) && !options->disableOptimizer) { |
| 8805 |
SpirvToolsTransform(intermediate, spirv, logger, options); |
| 8806 |
prelegalization = false; |
| 8807 |
} |
| 8808 |
else if (options->stripDebugInfo) { |
| 8809 |
// Strip debug info even if optimization is disabled. |
| 8810 |
SpirvToolsStripDebugInfo(intermediate, spirv, logger); |
| 8811 |
} |
| 8812 |
|
| 8813 |
if (options->validate) |
| 8814 |
SpirvToolsValidate(intermediate, spirv, logger, prelegalization); |
| 8815 |
|
| 8816 |
if (options->disassemble) |
| 8817 |
SpirvToolsDisassemble(std::cout, spirv); |
| 8818 |
|
| 8819 |
#endif |
| 8820 |
|
| 8821 |
GetThreadPoolAllocator().pop(); |
| 8822 |
} |
| 8823 |
|
| 8824 |
}; // end namespace glslang |